Files
apes-sidebar/tests/file-stream-route.spec.ts
T
Menghuan1918 376a57bfe5 merge #1027: feat(media) built-in audio/video preview with range support
# Conflicts:
#	CHANGELOG.md
#	CHANGELOG_EN.md
#	docs/external-plugin-guide.md
#	src/index.ts
2026-11-08 12:17:07 +08:00

474 lines
21 KiB
TypeScript

/**
* The raw byte routes are STREAMED, and there is no size cap left.
*
* Both halves of that matter and both are pinned here with a REAL node:http
* server, because the properties under test are wire properties:
*
* - `/sidebar/file` and `/sidebar/html` used to `readFile()` the whole file
* and `res.end()` it, with a 20 MiB `mediaLimit` refusing anything larger.
* A 21 MiB file therefore answered 400 before a single byte moved. Now it
* streams, so what this spec asserts is that the bytes arrive, that the
* length is right, and that the headers survived the rewrite.
* - `writeHead` commits the response, so the ORDER matters: the file is opened
* and awaited BEFORE the status line goes out. An unopenable file must still
* produce the plugin's JSON error envelope rather than a 200 with an empty
* body — that is the regression the `headersSent` guard in wire.ts exists to
* prevent on the failure side.
*/
import { afterEach, describe, expect, it } from 'vitest'
import { mkdirSync, mkdtempSync, symlinkSync, writeFileSync } from 'node:fs'
import { createServer } from 'node:http'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { apply } from '../src/index.ts'
import { writeError, writeJson } from '../src/wire.ts'
import type { SidebarWebRoute } from '../src/context-types.ts'
import { removeTree } from './temp-tree.ts'
interface FakeContext {
connection: { requestRejection: () => undefined }
webServer: {
register: (route: SidebarWebRoute) => () => void
registerUpgrade: () => () => void
}
sessions: { get: (id: string) => { header: { cwd?: string } } | undefined }
tools: { register: () => () => void }
effect: (fn: () => void | (() => void)) => void
inject: (deps: readonly string[], callback: (sctx: never) => void) => () => void
on: () => () => void
get: (key: string) => undefined
}
/** The size the retired `mediaLimit` refused (20 MiB), plus a margin. */
const OVER_OLD_LIMIT = 21 * 1024 * 1024
const roots: string[] = []
const servers: Array<ReturnType<typeof createServer>> = []
afterEach(async () => {
for (const server of servers.splice(0)) {
// Drop the keep-alive sockets before closing: `close()` alone waits for
// them, and undici holds them open far longer than a test should.
await new Promise<void>(resolve => {
server.close(() => resolve())
server.closeAllConnections()
})
}
for (const root of roots.splice(0)) removeTree(root)
})
/** A temp workspace whose session cwd is the returned directory. */
function workspace(name: string): string {
const root = mkdtempSync(join(tmpdir(), `dsh-sidebar-stream-${name}-`))
roots.push(root)
return root
}
/** Mount the plugin and expose its routes over a real HTTP port. */
async function serve(workspace: string): Promise<{ origin: string; routes: SidebarWebRoute[] }> {
const routes: SidebarWebRoute[] = []
const ctx: FakeContext = {
connection: { requestRejection: () => undefined },
webServer: { register: (route) => { routes.push(route); return () => {} }, registerUpgrade: () => () => {} },
sessions: { get: () => ({ header: { cwd: workspace } }) },
tools: { register: () => () => {} },
effect: (fn) => { fn() },
inject: () => () => {},
on: () => () => {},
get: () => undefined,
}
apply(ctx as never, {})
const dispatch = (req: Parameters<SidebarWebRoute['handler']>[0], res: Parameters<SidebarWebRoute['handler']>[1]): void => {
const pathname = new URL(req.url ?? '/', 'http://dsh.internal').pathname
// Longest prefix first: `/sidebar/file` must not swallow `/sidebar/files`.
const matched = routes
.filter(route => (route.kind === 'exact' ? route.path === pathname : pathname === route.path || pathname.startsWith(`${route.path}/`)))
.sort((a, b) => b.path.length - a.path.length)[0]
if (matched === undefined) {
res.writeHead(404)
res.end()
return
}
void matched.handler(req, res)
}
const server = createServer((req, res) => { dispatch(req as never, res as never) })
servers.push(server)
await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve))
const address = server.address()
if (address === null || typeof address === 'string') throw new Error('test setup: no port')
return { origin: `http://127.0.0.1:${address.port}`, routes }
}
/** The `/sidebar/file` URL for one path. */
function fileUrl(origin: string, workspace: string, path: string, extra: Record<string, string> = {}): string {
const params = new URLSearchParams({ sessionId: 's', cwd: workspace, path, ...extra })
return `${origin}/sidebar/file?${params.toString()}`
}
describe('/sidebar/file streams without a size cap', () => {
it('serves a file well past the retired 20 MiB mediaLimit', async () => {
const ws = workspace('large')
const target = join(ws, 'big.bin')
// Sparse on disk, full length on the wire: the point is the BYTE COUNT the
// route now lets through, not the space it costs the test.
writeFileSync(target, '')
const { truncateSync } = await import('node:fs')
truncateSync(target, OVER_OLD_LIMIT)
const { origin } = await serve(ws)
const response = await fetch(fileUrl(origin, ws, target))
expect(response.status).toBe(200)
expect(response.headers.get('content-length')).toBe(String(OVER_OLD_LIMIT))
const body = await response.arrayBuffer()
expect(body.byteLength).toBe(OVER_OLD_LIMIT)
})
it('streams bytes verbatim and keeps the media headers', async () => {
const ws = workspace('small')
const target = join(ws, 'note.txt')
writeFileSync(target, 'hello streamed world\n')
const { origin } = await serve(ws)
const response = await fetch(fileUrl(origin, ws, target))
expect(response.status).toBe(200)
expect(response.headers.get('cache-control')).toBe('no-cache')
expect(response.headers.get('content-length')).toBe('21')
expect(await response.text()).toBe('hello streamed world\n')
// A plain media request must NOT force a download disposition.
expect(response.headers.get('content-disposition')).toBeNull()
})
it('adds the RFC 5987 attachment disposition for ?download=1', async () => {
const ws = workspace('download')
const dir = join(ws, '报告')
mkdirSync(dir, { recursive: true })
const target = join(dir, '数据.txt')
writeFileSync(target, 'x\n')
const { origin } = await serve(ws)
const response = await fetch(fileUrl(origin, ws, target, { download: '1' }))
expect(response.status).toBe(200)
const disposition = response.headers.get('content-disposition') ?? ''
expect(disposition).toContain(`filename*=UTF-8''${encodeURIComponent('数据.txt')}`)
// node:http refuses a header value above U+00FF; the ASCII-only form is
// what keeps a non-latin1 name from turning the download into a 500.
expect([...disposition].every(char => char.codePointAt(0)! <= 0xFF)).toBe(true)
})
it('keeps the SVG sandbox headers on the streamed response', async () => {
const ws = workspace('svg')
const target = join(ws, 'vector.svg')
writeFileSync(target, '<svg xmlns="http://www.w3.org/2000/svg"></svg>')
const { origin } = await serve(ws)
const response = await fetch(fileUrl(origin, ws, target))
expect(response.status).toBe(200)
expect(response.headers.get('content-security-policy')).toBe("sandbox; object-src 'none'")
expect(response.headers.get('x-content-type-options')).toBe('nosniff')
expect(response.headers.get('referrer-policy')).toBe('no-referrer')
})
it.each(['html', 'htm', 'HTML'])('keeps scriptable file previews in an opaque origin (%s)', async extension => {
const ws = workspace('opaque-preview')
const target = join(ws, `preview.${extension}`)
writeFileSync(target, '<script>window.fixture = true</script>')
const { origin } = await serve(ws)
const response = await fetch(fileUrl(origin, ws, target))
expect(response.status).toBe(200)
const policy = response.headers.get('content-security-policy') ?? ''
expect(policy).toContain('sandbox')
expect(policy).not.toContain('allow-same-origin')
expect(response.headers.get('x-content-type-options')).toBe('nosniff')
expect(response.headers.get('referrer-policy')).toBe('no-referrer')
expect(await response.text()).toContain('window.fixture')
})
it.each(['png', 'bin'])('keeps an inert preview free of the CSP sandbox (%s)', async extension => {
// The guard is scoped to scriptable documents: a PNG/unknown binary must
// not gain a sandbox directive (nothing to sandbox, and it would be a
// behaviour change for existing image previews). nosniff is the real
// protection there, and it stays on every response.
const ws = workspace('inert-preview')
const target = join(ws, `preview.${extension}`)
writeFileSync(target, '<script>window.fixture = true</script>')
const { origin } = await serve(ws)
const response = await fetch(fileUrl(origin, ws, target))
expect(response.status).toBe(200)
expect(response.headers.get('content-security-policy')).toBeNull()
expect(response.headers.get('x-content-type-options')).toBe('nosniff')
expect(response.headers.get('referrer-policy')).toBe('no-referrer')
})
it('answers the JSON error envelope when the file cannot be opened', async () => {
const ws = workspace('missing')
const { origin } = await serve(ws)
// The header must not have been written before the stream opened, or this
// would be a 200 with an empty body instead of a usable error.
const response = await fetch(fileUrl(origin, ws, join(ws, 'nope.bin')))
expect(response.status).toBe(400)
expect(response.headers.get('content-type')).toContain('application/json')
const body = await response.json() as { ok: boolean; error?: { code?: string } }
expect(body.ok).toBe(false)
expect(body.error?.code).toBe('fs-error')
})
it('refuses a directory instead of streaming it', async () => {
const ws = workspace('directory')
mkdirSync(join(ws, 'sub'))
const { origin } = await serve(ws)
const response = await fetch(fileUrl(origin, ws, join(ws, 'sub')))
expect(response.status).toBe(400)
expect(((await response.json()) as { error?: { message?: string } }).error?.message).toContain('not a file')
})
it('survives a client that aborts mid-stream', async () => {
const ws = workspace('abort')
const target = join(ws, 'big.bin')
writeFileSync(target, '')
const { truncateSync } = await import('node:fs')
truncateSync(target, OVER_OLD_LIMIT)
const { origin } = await serve(ws)
const controller = new AbortController()
const response = await fetch(fileUrl(origin, ws, target), { signal: controller.signal })
expect(response.status).toBe(200)
// Read one chunk, then walk away: the host must tear the source down
// rather than buffer the remaining 21 MiB nobody is reading.
const reader = response.body!.getReader()
await reader.read()
controller.abort()
await expect(reader.read()).rejects.toBeTruthy()
// The server is still usable afterwards — no wedged socket, no unhandled
// rejection from the pipeline.
const again = await fetch(fileUrl(origin, ws, target))
expect(again.status).toBe(200)
expect(again.headers.get('content-length')).toBe(String(OVER_OLD_LIMIT))
await again.body!.cancel()
})
})
describe('/sidebar/html streams without a size cap', () => {
it.each([
['style.css', 'text/css', 'body { color: red }'],
['app.js', 'text/javascript', 'document.body.dataset.ready = "yes"'],
['module.mjs', 'text/javascript', 'export const ready = true'],
['STYLE.CSS', 'text/css', 'body { color: blue }'],
['data.json', 'application/json', '{"ready":true}'],
['font.woff', 'font/woff', 'woff bytes'],
['font.woff2', 'font/woff2', 'woff2 bytes'],
['font.ttf', 'font/ttf', 'ttf bytes'],
['font.otf', 'font/otf', 'otf bytes'],
['module.wasm', 'application/wasm', 'wasm bytes'],
])('serves relative %s resources with a browser-compatible content type', async (name, type, content) => {
const ws = workspace('html-assets')
writeFileSync(join(ws, 'index.html'), `<link rel="stylesheet" href="style.css"><script src="app.js"></script>`)
writeFileSync(join(ws, name), content)
const { origin } = await serve(ws)
const { encodeHtmlUrl } = await import('../src/html-route.ts')
const page = new URL(encodeHtmlUrl('s', join(ws, 'index.html')), origin)
const response = await fetch(new URL(name, page))
expect(response.status).toBe(200)
expect(response.headers.get('content-type')).toBe(type)
expect(response.headers.get('x-content-type-options')).toBe('nosniff')
expect(await response.text()).toBe(content)
})
it('serves a page past the retired mediaLimit with the sandbox CSP', async () => {
const ws = workspace('html')
const target = join(ws, 'page.html')
// Big enough to have been refused by the old cap, and valid HTML.
const filler = `<!-- ${'x'.repeat(1024)} -->\n`
writeFileSync(target, `<html><body>ok</body></html>\n${filler.repeat(21 * 1024)}`)
const { origin, routes } = await serve(ws)
expect(routes.some(route => route.path === '/sidebar/html' && route.kind === 'prefix')).toBe(true)
// `/sidebar/html` is served by path-encoded URL only (html-route.ts); the
// encoding itself has its own spec, so this reuses the same pair of helpers
// the client does rather than inventing a third spelling.
const { encodeHtmlUrl } = await import('../src/html-route.ts')
const url = new URL(encodeHtmlUrl('s', target), origin)
const response = await fetch(url, { headers: { host: '127.0.0.1' } })
expect(response.status).toBe(200)
expect(response.headers.get('content-security-policy'))
.toBe("sandbox allow-scripts allow-popups allow-downloads allow-modals; object-src 'none'")
expect(Number(response.headers.get('content-length'))).toBeGreaterThan(20 * 1024 * 1024)
await response.body!.cancel()
})
it('serves a file reached through a symlink', async () => {
const ws = workspace('html-link')
const real = join(ws, 'real.css')
writeFileSync(real, 'body { color: red }\n')
symlinkSync(real, join(ws, 'link.css'))
const { origin } = await serve(ws)
// Resolved through the file route (the html route shares the resolver).
const response = await fetch(fileUrl(origin, ws, join(ws, 'link.css')))
expect(response.status).toBe(200)
expect(await response.text()).toBe('body { color: red }\n')
})
})
/**
* Range support on the media route.
*
* A media element asks for `bytes=0-` and then seeks by asking for other
* windows; without 206 + `content-range` the browser can only buffer forward,
* so a clip is watchable but not scrubbable. These cases pin the wire shape:
* the 206 window, the restated `content-length`, the suffix spelling, the
* clamp past EOF, the 416 dead end, and the fallback to a plain 200 for
* spellings the route refuses to guess about.
*/
describe('/sidebar/file honours HTTP Range', () => {
it('answers 206 with exactly the requested window', async () => {
const ws = workspace('range-window')
const target = join(ws, 'clip.mp4')
writeFileSync(target, '0123456789')
const { origin } = await serve(ws)
const response = await fetch(fileUrl(origin, ws, target), { headers: { range: 'bytes=2-5' } })
expect(response.status).toBe(206)
expect(response.headers.get('content-range')).toBe('bytes 2-5/10')
expect(response.headers.get('content-length')).toBe('4')
expect(response.headers.get('accept-ranges')).toBe('bytes')
expect(await response.text()).toBe('2345')
})
it('advertises accept-ranges on the whole-body answer too', async () => {
const ws = workspace('range-advertise')
const target = join(ws, 'note.md')
writeFileSync(target, 'hello\n')
const { origin } = await serve(ws)
const response = await fetch(fileUrl(origin, ws, target))
expect(response.status).toBe(200)
expect(response.headers.get('accept-ranges')).toBe('bytes')
expect(response.headers.get('content-range')).toBeNull()
})
it('counts a suffix range back from EOF', async () => {
const ws = workspace('range-suffix')
const target = join(ws, 'clip.mp4')
writeFileSync(target, '0123456789')
const { origin } = await serve(ws)
const response = await fetch(fileUrl(origin, ws, target), { headers: { range: 'bytes=-3' } })
expect(response.status).toBe(206)
expect(response.headers.get('content-range')).toBe('bytes 7-9/10')
expect(await response.text()).toBe('789')
})
it('clamps an end past EOF instead of refusing the open-ended form', async () => {
const ws = workspace('range-clamp')
const target = join(ws, 'clip.mp4')
writeFileSync(target, '0123456789')
const { origin } = await serve(ws)
const response = await fetch(fileUrl(origin, ws, target), { headers: { range: 'bytes=5-99' } })
expect(response.status).toBe(206)
expect(response.headers.get('content-range')).toBe('bytes 5-9/10')
expect(await response.text()).toBe('56789')
const openEnded = await fetch(fileUrl(origin, ws, target), { headers: { range: 'bytes=7-' } })
expect(openEnded.status).toBe(206)
expect(openEnded.headers.get('content-range')).toBe('bytes 7-9/10')
expect(await openEnded.text()).toBe('789')
})
it('answers 416 when the window starts past EOF', async () => {
const ws = workspace('range-416')
const target = join(ws, 'clip.mp4')
writeFileSync(target, '0123456789')
const { origin } = await serve(ws)
const response = await fetch(fileUrl(origin, ws, target), { headers: { range: 'bytes=20-' } })
expect(response.status).toBe(416)
expect(response.headers.get('content-range')).toBe('bytes */10')
expect(await response.text()).toBe('')
})
it('answers a video container with its real type and a seekable window', async () => {
const ws = workspace('range-video')
const target = join(ws, 'clip.mp4')
writeFileSync(target, '0123456789')
const { origin } = await serve(ws)
const response = await fetch(fileUrl(origin, ws, target), { headers: { range: 'bytes=0-' } })
expect(response.status).toBe(206)
// The demuxer is picked from this header: octet-stream is the bug.
expect(response.headers.get('content-type')).toBe('video/mp4')
expect(response.headers.get('content-range')).toBe('bytes 0-9/10')
expect(await response.text()).toBe('0123456789')
})
it('falls back to 200 for a multi-range or non-bytes header', async () => {
const ws = workspace('range-fallback')
const target = join(ws, 'clip.mp4')
writeFileSync(target, '0123456789')
const { origin } = await serve(ws)
for (const range of ['bytes=0-1,4-5', 'items=0-1', 'bytes=abc']) {
const response = await fetch(fileUrl(origin, ws, target), { headers: { range } })
expect(response.status, range).toBe(200)
expect(await response.text(), range).toBe('0123456789')
}
})
})
describe('byteRangeOf', () => {
it('parses the spellings a browser sends', async () => {
const { byteRangeOf } = await import('../src/file-stream.ts')
expect(byteRangeOf('bytes=0-', 100)).toEqual({ start: 0, end: 99 })
expect(byteRangeOf('bytes=10-19', 100)).toEqual({ start: 10, end: 19 })
expect(byteRangeOf('bytes=-10', 100)).toEqual({ start: 90, end: 99 })
expect(byteRangeOf('bytes=90-999', 100)).toEqual({ start: 90, end: 99 })
expect(byteRangeOf(' BYTES=1-2 ', 100)).toBeUndefined()
})
it('separates "no range" from "past EOF"', async () => {
const { byteRangeOf } = await import('../src/file-stream.ts')
expect(byteRangeOf(undefined, 100)).toBeUndefined()
expect(byteRangeOf('items=0-1', 100)).toBeUndefined()
expect(byteRangeOf('bytes=0-1,4-5', 100)).toBeUndefined()
expect(byteRangeOf('bytes=-', 100)).toBeUndefined()
expect(byteRangeOf('bytes=100-', 100)).toBe('unsatisfiable')
expect(byteRangeOf('bytes=-0', 100)).toBe('unsatisfiable')
expect(byteRangeOf('bytes=0-', 0)).toBe('unsatisfiable')
// Start after end is a contradictory range, not an EOF complaint.
expect(byteRangeOf('bytes=9-2', 100)).toBeUndefined()
})
})
describe('writeJson after the header committed', () => {
it('destroys the response instead of throwing ERR_HTTP_HEADERS_SENT', () => {
// The failure mode this replaces: a streamed route errors mid-flight, its
// catch block calls writeError, writeHead throws, and a truncated download
// becomes a crashed request.
let destroyed = false
const res = {
statusCode: 200,
headersSent: true,
writeHead: () => { throw new Error('ERR_HTTP_HEADERS_SENT') },
write: () => true,
end: () => { throw new Error('end after destroy') },
destroy: () => { destroyed = true },
}
expect(() => { writeJson(res, 500, { ok: false }) }).not.toThrow()
expect(destroyed).toBe(true)
expect(() => { writeError(res, new Error('boom')) }).not.toThrow()
})
it('writes normally while the header is still open', () => {
const written: string[] = []
let status = 0
const res = {
statusCode: 200,
headersSent: false,
writeHead: (code: number) => { status = code },
write: () => true,
end: (chunk?: string) => { written.push(String(chunk ?? '')) },
}
writeJson(res, 418, { ok: false })
expect(status).toBe(418)
expect(written.join('')).toBe('{"ok":false}')
})
})