Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

video-viewer: a bad request, Range or sidecar no longer takes the pane down

dee673c47801f9f
diff --git a/store/viewers/video-viewer/lib/library.mjs b/store/viewers/video-viewer/lib/library.mjs
index 6f925b165..7d04902c6 100644
--- a/store/viewers/video-viewer/lib/library.mjs
+++ b/store/viewers/video-viewer/lib/library.mjs
@@ -127,7 +127,7 @@ export function createLibrary(workspace, options = {}) {
const partialRoots = [] // { qdirRel, scene, abs }
let entries = 0
const visit = (dir, depth) => {
- if (depth > MAX_DEPTH || entries > MAX_ENTRIES) return
+ if (depth > MAX_DEPTH) return
let names
try { names = readdirSync(dir) } catch { return }
for (const name of names) {
@@ -236,7 +236,7 @@ export function createLibrary(workspace, options = {}) {
return beats
}
- function renderEntry(item) {
+ function renderEntry(item, extras = true) {
const { rel, abs, st, kind } = item
const info = probed(abs, st, kind)
const parts = kind === 'image' ? null : manimParts(rel)
@@ -263,7 +263,7 @@ export function createLibrary(workspace, options = {}) {
beats: null,
changes: null,
}
- if (kind === 'video' && entry.complete) {
+ if (extras && kind === 'video' && entry.complete) {
const sidecar = sidecarFor(rel, st)
const beats = (sidecar && beatsFromSidecar(sidecar, fps, entry.frames)) || beatsFor(parts, st, entry.frames)
let sections = sectionsFor(parts, st, fps)
@@ -412,13 +412,15 @@ export function createLibrary(workspace, options = {}) {
function scan() {
const { media, partialRoots } = walk()
- const renders = media.map(renderEntry).sort((a, b) => b.mtimeMs - a.mtimeMs)
+ // A sidecar or a sections file that is not the shape written costs that render its chapters and
+ // beats, never the library: the render is still listed, as itself.
+ const renders = media.map((item) => { try { return renderEntry(item) } catch { return renderEntry(item, false) } }).sort((a, b) => b.mtimeMs - a.mtimeMs)
const live = liveFromFiles(partialRoots, renders)
const status = liveFromStatus(renders)
if (status) {
const i = live.findIndex((l) => l.key === status.key)
// A plain `manim render` after the wrapper's last run is newer news than its status file.
- const filesNewer = i >= 0 && (live[i].updatedAtMs ?? 0) > (status.updatedAtMs ?? 0) + 1000 && status.state !== 'rendering'
+ const filesNewer = i >= 0 && live[i].updatedAtMs > status.updatedAtMs + 1000 && status.state !== 'rendering'
if (status.done) { if (i >= 0 && live[i].state !== 'rendering') live.splice(i, 1) } else if (i >= 0) { if (!filesNewer) live[i] = status }
else live.push(status)
}
@@ -428,12 +430,12 @@ export function createLibrary(workspace, options = {}) {
return {
workspace: { name: basename(workspace), path: workspace },
renders,
- live: live.sort((a, b) => (b.updatedAtMs ?? 0) - (a.updatedAtMs ?? 0)),
+ live: live.sort((a, b) => b.updatedAtMs - a.updatedAtMs),
scenes,
verdict: verdict && typeof verdict === 'object' ? { ready: Boolean(verdict.ready), summary: verdict.summary ?? '', artifact: verdict.artifact ?? null, updatedAt: verdict.updatedAt ?? null } : null,
}
}
- return { scan, probe: (rel) => { const abs = join(workspace, rel); const st = statOrNull(abs); return st ? probed(abs, st, ext(rel) === '.gif' ? 'gif' : 'video') : null } }
+ return { scan }
}
diff --git a/store/viewers/video-viewer/test/library-edges.test.mjs b/store/viewers/video-viewer/test/library-edges.test.mjs
new file mode 100644
index 000000000..28a4427cb
--- /dev/null
+++ b/store/viewers/video-viewer/test/library-edges.test.mjs
@@ -0,0 +1,472 @@
+// The library on every shape a workspace can be in: what the walk lists and skips, where chapters and
+// beats come from (the wrapper's sidecar, Manim's own files) and when they are not trusted, what changed
+// since the render before, renders in progress from the files and from the wrapper's status, and
+// malformed files that must cost a render its extras rather than the pane its library.
+//
+// node --test test/*.test.mjs
+import assert from 'node:assert/strict'
+import { chmodSync, symlinkSync, utimesSync, writeFileSync } from 'node:fs'
+import { createServer } from 'node:net'
+import { join } from 'node:path'
+import { after, test } from 'node:test'
+import { createLibrary, diffRenders, manimParts, qualityParts, readClipList } from '../lib/library.mjs'
+import { box, gif, mp4, png, u32, videoTrak, workspace } from './media.mjs'
+
+const MIN = 60_000
+const spaces = []
+function scratch() {
+ const ws = workspace('video-viewer-library-')
+ spaces.push(ws)
+ const json = (rel, value, mtimeMs) => ws.put(rel, typeof value === 'string' ? value : JSON.stringify(value), mtimeMs)
+ const scan = (options) => createLibrary(ws.dir, options).scan()
+ return { ...ws, json, scan }
+}
+after(() => { for (const ws of spaces) ws.done() })
+const render = (lib, path) => lib.renders.find((r) => r.path === path)
+const ftyp = box('ftyp', Buffer.from('isom', 'latin1'), u32(512))
+const mvhd = (ts = 15360, dur = 15360) => box('mvhd', u32(0), u32(0), u32(0), u32(ts), u32(dur), Buffer.alloc(80))
+/** A whole movie whose track has no timing table: no frame count, no rate of its own. */
+const noStts = (frames) => Buffer.concat([ftyp, box('moov', mvhd(), videoTrak({ frames, stts: false }))])
+/** A whole movie with frames but no media or movie header: no duration anywhere, no rate of its own. */
+const noTimes = (frames) => Buffer.concat([ftyp, box('moov', box('trak', box('mdia',
+ box('hdlr', u32(0), u32(0), Buffer.from('vide', 'latin1'), Buffer.alloc(12)),
+ box('minf', box('stbl', box('stts', u32(0), u32(1), u32(frames), u32(1)))))))])
+
+test('paths: Manim layouts with and without a media folder, qualities, clip lists', () => {
+ assert.deepEqual(manimParts('videos/proof/720p30/Proof.mov'), { media: '', module: 'proof', quality: '720p30', scene: 'Proof', file: 'Proof', key: 'videos/proof/Proof', qdir: 'videos/proof/720p30' })
+ assert.deepEqual(qualityParts('1080p60'), { height: 1080, fps: 60 })
+ assert.equal(qualityParts('hd'), null)
+ assert.equal(qualityParts(undefined), null)
+ const ws = scratch()
+ assert.equal(readClipList(join(ws.dir, 'missing.txt')), null)
+})
+
+test('diffRenders: nothing to compare, contiguous and separate changes, chapters added, removed and retimed', () => {
+ const beats = (list) => { let s = 0; return list.map(([name, frames]) => { const b = { name, fp: name, start: s, frames }; s += frames; return b }) }
+ assert.deepEqual(diffRenders({ duration: null, frames: null, beats: null, sections: null }, { mtimeMs: 5, duration: null, frames: null, beats: null, sections: null }),
+ { at: 5, durationDelta: 0, framesDelta: 0, ranges: null, sections: null, same: false }, 'without beats a render is never known to be the same')
+
+ const prev = { duration: 2, frames: 30, beats: beats([['h1', 10], ['h2', 20]]), sections: null }
+ assert.deepEqual(diffRenders(prev, { duration: 1, frames: 12, beats: beats([['n1', 8], ['n2', 4]]), sections: null }).ranges, [[0, 12]], 'adjacent new animations are one range')
+ assert.deepEqual(diffRenders(prev, { duration: 3, frames: 43, beats: beats([['n1', 8], ['h1', 10], ['h2', 20], ['n3', 5]]), sections: null }).ranges, [[0, 8], [38, 43]])
+
+ const A = { name: 'A', start: 0, frames: 10 }, B = { name: 'B', start: 10, frames: 20 }
+ const withSections = (sections, list = [['h1', 10], ['h2', 20]]) => ({ duration: 2, frames: 30, beats: beats(list), sections })
+ assert.deepEqual(diffRenders(withSections(null), withSections([A, B])).sections, { added: ['A', 'B'], removed: [], changed: [] })
+ assert.deepEqual(diffRenders(withSections([A, B]), withSections(null)).sections, { added: [], removed: ['A', 'B'], changed: [] })
+ const retimed = diffRenders(withSections([A, B]), withSections([A, { ...B, frames: 25 }]))
+ assert.deepEqual(retimed.sections, { added: [], removed: [], changed: ['B'] })
+ assert.equal(retimed.same, false)
+ const touched = diffRenders(withSections([A, B]), withSections([A, B], [['n1', 10], ['h2', 20]]))
+ assert.deepEqual(touched.sections.changed, ['A'], 'a new animation inside a chapter changes it; the chapter after it is untouched')
+ const late = diffRenders(withSections([A, B]), withSections([A, B], [['h1', 10], ['h2', 20], ['n3', 5]]))
+ assert.deepEqual(late.sections.changed, [], 'an animation after every chapter touches none')
+ const same = diffRenders(withSections([A, B]), withSections([A, B]))
+ assert.equal(same.same, true)
+ assert.equal(diffRenders({ ...withSections([A]), beats: null }, withSections([A])).sections.changed.length, 0, 'no ranges: only retiming changes a chapter')
+})
+
+test('the walk lists videos, GIFs and Manim stills, and skips what is not a render', async () => {
+ const ws = scratch()
+ const T = Date.now() - 5 * MIN
+ ws.put('README', 'no extension', T)
+ ws.put('clip.gif', gif({ delays: [10, 10] }), T)
+ ws.put('images/proof/Proof_ManimCE_v0.19.0.png', png({ width: 800, height: 450 }), T)
+ ws.put('images/loose.png', png({ complete: false }), T)
+ ws.put('art/cover.png', png(), T)
+ ws.put('.cache-like/x.mp4', mp4({ frames: 1 }), T)
+ ws.put('node_modules/pkg/x.mp4', mp4({ frames: 1 }), T)
+ ws.put('out/videos/proof/480p15/sections/Proof_0000_Intro.mp4', mp4({ frames: 1 }), T)
+ ws.put('1/2/3/4/5/6/7/8/9/deep.mp4', mp4({ frames: 1 }), T)
+ ws.put('1/2/3/4/5/6/7/8/9/10/deeper.mp4', mp4({ frames: 1 }), T)
+ ws.put('out/videos/half/480p15/Half.mp4', mp4({ frames: 30 }).subarray(0, 100), T)
+ ws.put('renders/half.mp4', Buffer.alloc(8), T)
+ symlinkSync(join(ws.dir, 'nowhere.mp4'), join(ws.dir, 'broken.mp4'))
+ const socket = createServer().listen(join(ws.dir, 'socket.mp4'))
+ await new Promise((ok) => socket.on('listening', ok))
+ try {
+ const lib = ws.scan()
+ assert.deepEqual(lib.renders.map((r) => r.path).sort(), ['1/2/3/4/5/6/7/8/9/deep.mp4', 'clip.gif', 'images/loose.png', 'images/proof/Proof_ManimCE_v0.19.0.png', 'out/videos/half/480p15/Half.mp4', 'renders/half.mp4'])
+ assert.deepEqual(render(lib, 'clip.gif'), { path: 'clip.gif', kind: 'gif', key: 'clip.gif', title: 'clip', module: null, quality: null, size: gif({ delays: [10, 10] }).length, mtimeMs: T, complete: true, width: 32, height: 24, fps: 10, frames: 2, duration: 0.2, audio: false, sections: null, beats: null, changes: null })
+ assert.deepEqual(render(lib, 'images/proof/Proof_ManimCE_v0.19.0.png'), { path: 'images/proof/Proof_ManimCE_v0.19.0.png', kind: 'image', key: 'images/proof/Proof_ManimCE_v0.19.0.png', title: 'Proof', module: 'proof', quality: null, size: png().length, mtimeMs: T, complete: true, width: 800, height: 450, fps: null, frames: null, duration: null, audio: false, sections: null, beats: null, changes: null })
+ const loose = render(lib, 'images/loose.png')
+ assert.deepEqual([loose.title, loose.module, loose.complete], ['loose', null, false])
+ const half = render(lib, 'out/videos/half/480p15/Half.mp4')
+ assert.deepEqual([half.complete, half.fps, half.height, half.width, half.beats], [false, 15, 480, null, null], 'a movie mid-write: what its quality folder says, nothing more')
+ const plain = render(lib, 'renders/half.mp4')
+ assert.deepEqual([plain.complete, plain.fps, plain.height], [false, null, null])
+ } finally {
+ socket.close()
+ }
+})
+
+test('the walk stops after 30,000 entries', () => {
+ const ws = scratch()
+ for (let i = 0; i < 30_001; i++) writeFileSync(join(ws.dir, `c${String(i).padStart(5, '0')}.mp4`), '')
+ // the root counts as nothing; every name read counts one, and the 30,001st ends the walk
+ assert.equal(ws.scan().renders.length, 30_000)
+})
+
+test('a file is probed again only when its size or time changes', () => {
+ const ws = scratch()
+ const T = Date.now() - 5 * MIN
+ const lib = createLibrary(ws.dir)
+ ws.put('a.mp4', mp4({ frames: 10 }), T)
+ assert.equal(lib.scan().renders[0].frames, 10)
+ ws.put('a.mp4', mp4({ frames: 20 }), T) // same size, same time: the answer is the cached one
+ assert.equal(lib.scan().renders[0].frames, 10)
+ utimesSync(join(ws.dir, 'a.mp4'), (T + 1000) / 1000, (T + 1000) / 1000)
+ assert.equal(lib.scan().renders[0].frames, 20)
+ ws.put('a.mp4', mp4({ frames: 20, audio: true }), T + 1000) // same time, new size
+ assert.equal(lib.scan().renders[0].audio, true)
+})
+
+test('Manim folders that cannot be read are skipped, not fatal', () => {
+ const ws = scratch()
+ const T = Date.now() - 5 * MIN
+ ws.put('out/videos/a/480p15/A.mp4', mp4({ frames: 15 }), T)
+ ws.put('out/videos/a/480p15/partial_movie_files/not-a-scene.txt', 'x', T)
+ symlinkSync(join(ws.dir, 'nowhere'), join(ws.dir, 'out/videos/a/480p15/partial_movie_files/Broken'))
+ ws.put('out/videos/a/480p15/partial_movie_files/Locked/uncached_00000.mp4', mp4({ frames: 15 }))
+ ws.put('out/videos/b/480p15/partial_movie_files/B/uncached_00000.mp4', mp4({ frames: 15 }))
+ const locked = join(ws.dir, 'out/videos/a/480p15/partial_movie_files/Locked')
+ const lockedRoot = join(ws.dir, 'out/videos/b/480p15/partial_movie_files')
+ chmodSync(locked, 0o000)
+ chmodSync(lockedRoot, 0o000)
+ try {
+ const lib = ws.scan()
+ assert.deepEqual(lib.renders.map((r) => r.path), ['out/videos/a/480p15/A.mp4'])
+ assert.deepEqual(lib.live, [], 'clips that cannot be listed are no render in progress')
+ } finally {
+ chmodSync(locked, 0o755)
+ chmodSync(lockedRoot, 0o755)
+ }
+})
+
+test('beats from the wrapper\'s sidecar: clip files, run times, and when the sidecar is not believed', () => {
+ const ws = scratch()
+ const T = Date.now() - 10 * MIN
+ // run times only (no clips folder), animations that are not about a clip ignored
+ ws.put('out/videos/s/480p15/One.mp4', mp4({ frames: 45 }), T)
+ ws.json('.harness/renders/out/videos/s/480p15/One.mp4.json', { clips: ['a.mp4', 'b.mp4'], animations: [null, { index: 7, label: 'Write', runTime: 1, clip: 'a.mp4', section: 0 }, { label: 'no clip' }, { runTime: 2, clip: 'b.mp4' }] }, T + 50)
+ // a clips folder: a whole clip gives its frames, a clip mid-write or missing falls back to its run time
+ const q = 'out/videos/s/720p30'
+ ws.put(`${q}/Two.mp4`, mp4({ frames: 60, fps: 30 }), T)
+ ws.put(`${q}/partial_movie_files/Two/h1.mp4`, mp4({ frames: 20, fps: 30 }), T - 5000)
+ ws.put(`${q}/partial_movie_files/Two/h2.mp4`, Buffer.from('not yet a movie, still being written'), T - 5000)
+ ws.json(`.harness/renders/${q}/Two.mp4.json`, {
+ tool: 'manim', source: 'scenes/s.py', tookMs: 1234, clipsDir: `${q}/partial_movie_files/Two`,
+ clips: [{ name: 'h1.mp4', size: 1 }, { name: 'h2.mp4' }, { name: 'h3.mp4' }],
+ animations: [{ index: 0, label: 'A', clip: 'h1.mp4', runTime: 9 }, { index: 1, label: 'B', clip: 'h2.mp4', runTime: 1 }, { index: 2, clip: 'h3.mp4', runTime: 1 / 3 }],
+ sections: [{ name: 'Intro', animation: 0 }, { name: 'Rest', animation: 1 }, { name: 'Beyond', animation: 9 }],
+ }, T + 50)
+ // a sidecar that does not add up, is older than the movie, or is not a sidecar
+ ws.put('out/videos/s/480p15/Short.mp4', mp4({ frames: 45 }), T)
+ ws.json('.harness/renders/out/videos/s/480p15/Short.mp4.json', { clips: ['a.mp4'], animations: [{ clip: 'a.mp4', runTime: 2 }] }, T + 50)
+ ws.put('out/videos/s/480p15/NoTime.mp4', mp4({ frames: 45 }), T)
+ ws.json('.harness/renders/out/videos/s/480p15/NoTime.mp4.json', { clips: ['a.mp4'] }, T + 50)
+ ws.put('out/videos/s/480p15/NullClip.mp4', mp4({ frames: 45 }), T)
+ ws.json('.harness/renders/out/videos/s/480p15/NullClip.mp4.json', { clips: [null] }, T + 50)
+ ws.put('out/stale.mp4', mp4({ frames: 15 }), T)
+ ws.json('.harness/renders/out/stale.mp4.json', { clips: ['a.mp4'], animations: [{ clip: 'a.mp4', runTime: 1 }] }, T - 5000)
+ for (const [name, body] of [['bad', 'not json'], ['null', 'null'], ['number', '5'], ['noclips', { clips: 'a.mp4' }]]) {
+ ws.put(`out/${name}.mp4`, mp4({ frames: 15 }), T)
+ ws.json(`.harness/renders/out/${name}.mp4.json`, body, T + 50)
+ }
+ // no rate anywhere (not a Manim path, no timing table): run times cannot become frames
+ ws.put('renders/plain.mp4', noStts(15), T)
+ ws.json('.harness/renders/renders/plain.mp4.json', { clips: [{ name: 'x.mp4' }], animations: [{ clip: 'x.mp4', runTime: 1 }] }, T + 50)
+ // no frame count: beats are not checked against it, and chapters past the last beat start at 0
+ ws.put('out/videos/s/480p15/Open.mp4', noStts(0), T)
+ ws.json('.harness/renders/out/videos/s/480p15/Open.mp4.json', { clips: ['c1.mp4'], animations: [{ clip: 'c1.mp4', runTime: 1, index: 0 }], sections: [{ name: 'A', animation: 0 }, { name: 'B', animation: 5 }] }, T + 50)
+ // one chapter is no chapters
+ ws.put('out/videos/s/480p15/Single.mp4', mp4({ frames: 15 }), T)
+ ws.json('.harness/renders/out/videos/s/480p15/Single.mp4.json', { clips: ['c1.mp4'], animations: [{ clip: 'c1.mp4', runTime: 1 }], sections: [{ name: 'Only', animation: 0 }] }, T + 50)
+
+ const lib = ws.scan()
+ const one = render(lib, 'out/videos/s/480p15/One.mp4')
+ assert.deepEqual(one.beats, [{ name: 'a.mp4', start: 0, frames: 15, label: 'Write' }, { name: 'b.mp4', start: 15, frames: 30, label: null }])
+ assert.deepEqual([one.sections, one.tool, one.source, one.tookMs, one.changes], [null, null, null, null, null])
+
+ const two = render(lib, `${q}/Two.mp4`)
+ assert.deepEqual(two.beats, [{ name: 'h1.mp4', start: 0, frames: 20, label: 'A' }, { name: 'h2.mp4', start: 20, frames: 30, label: 'B' }, { name: 'h3.mp4', start: 50, frames: 10, label: null }])
+ assert.deepEqual(two.sections, [
+ { index: 0, name: 'Intro', type: null, start: 0, frames: 20, video: null },
+ { index: 1, name: 'Rest', type: null, start: 20, frames: 40, video: null },
+ { index: 2, name: 'Beyond', type: null, start: 60, frames: 0, video: null },
+ ])
+ assert.deepEqual([two.tool, two.source, two.tookMs], ['manim', 'scenes/s.py', 1234])
+
+ for (const path of ['out/videos/s/480p15/Short.mp4', 'out/videos/s/480p15/NoTime.mp4', 'out/videos/s/480p15/NullClip.mp4', 'out/stale.mp4', 'out/bad.mp4', 'out/null.mp4', 'out/number.mp4', 'out/noclips.mp4', 'renders/plain.mp4']) {
+ assert.equal(render(lib, path).beats, null, path)
+ }
+ assert.equal(render(lib, 'out/stale.mp4').tool, null, 'a sidecar older than its movie describes an earlier render')
+
+ const open = render(lib, 'out/videos/s/480p15/Open.mp4')
+ assert.equal(open.frames, null)
+ assert.deepEqual(open.beats, [{ name: 'c1.mp4', start: 0, frames: 15, label: null }])
+ assert.deepEqual(open.sections.map((s) => [s.name, s.start, s.frames]), [['A', 0, 0], ['B', 0, 0]])
+ assert.equal(render(lib, 'out/videos/s/480p15/Single.mp4').sections, null)
+})
+
+test('chapters from Manim\'s sections file, and when it is not believed', () => {
+ const ws = scratch()
+ const T = Date.now() - 10 * MIN
+ const q = 'media/videos/sec/480p15'
+ ws.put(`${q}/Sec.mp4`, mp4({ frames: 25 }), T)
+ ws.json(`${q}/sections/Sec.json`, [{ name: 'Intro', type: 'default.normal', nb_frames: '10', video: 'Sec_0000_Intro.mp4' }, { duration: '1' }, { nb_frames: 'x', duration: 'x' }], T + 50)
+ for (const [scene, body, at] of [['Stale', [{ name: 'A', nb_frames: 1 }], T - 5000], ['Object', { name: 'A' }, T], ['Empty', [], T]]) {
+ ws.put(`${q}/${scene}.mp4`, mp4({ frames: 15 }), T)
+ ws.json(`${q}/sections/${scene}.json`, body, at)
+ }
+ // a quality folder that says 0 fps, and a movie with no rate of its own: 15 fps is assumed
+ ws.put('media/videos/sec/480p0/Zero.mp4', noStts(0), T)
+ ws.json('media/videos/sec/480p0/sections/Zero.json', [{ duration: 2 }], T)
+
+ const lib = ws.scan()
+ assert.deepEqual(render(lib, `${q}/Sec.mp4`).sections, [
+ { index: 0, name: 'Intro', type: 'default.normal', start: 0, frames: 10, video: `${q}/sections/Sec_0000_Intro.mp4` },
+ { index: 1, name: 'Section 2', type: null, start: 10, frames: 15, video: null },
+ { index: 2, name: 'Section 3', type: null, start: 25, frames: 0, video: null },
+ ])
+ assert.equal(render(lib, `${q}/Sec.mp4`).key, 'media/videos/sec/Sec')
+ for (const scene of ['Stale', 'Object', 'Empty']) assert.equal(render(lib, `${q}/${scene}.mp4`).sections, null, scene)
+ assert.deepEqual(render(lib, 'media/videos/sec/480p0/Zero.mp4').sections.map((s) => s.frames), [30])
+})
+
+test('beats from Manim\'s clip list, and when it is not believed', () => {
+ const ws = scratch()
+ const T = Date.now() - 10 * MIN
+ const q = 'out/videos/list/480p15'
+ const scene = (name, { frames = 45, clips = {}, list = Object.keys(clips).map((c) => `file 'file:/Users/example/elsewhere/${c}'`).join('\n'), listAt = T - 1000 } = {}) => {
+ ws.put(`${q}/${name}.mp4`, mp4({ frames }), T)
+ for (const [clip, data] of Object.entries(clips)) if (data) ws.put(`${q}/partial_movie_files/${name}/${clip}`, data, T - 2000)
+ if (list !== null) ws.put(`${q}/partial_movie_files/${name}/partial_movie_file_list.txt`, `# ffmpeg concat list\n${list}\n`, listAt)
+ }
+ const good = { 'uncached_00000.mp4': mp4({ frames: 20 }), 'abc.mp4': mp4({ frames: 25 }) }
+ scene('Good', { clips: good })
+ scene('NoList', { clips: good, list: null })
+ scene('ListAfter', { clips: good, listAt: T + 5000 })
+ scene('ListLongBefore', { clips: good, listAt: T - 40 * MIN })
+ scene('ListEmpty', { clips: good, list: '' })
+ scene('ListLocked', { clips: good })
+ scene('ClipMissing', { clips: { 'uncached_00000.mp4': mp4({ frames: 20 }), 'abc.mp4': null } })
+ scene('ClipWriting', { clips: { 'uncached_00000.mp4': mp4({ frames: 20 }), 'abc.mp4': Buffer.from('half a movie, being written now') } })
+ scene('ClipNoFrames', { clips: { 'uncached_00000.mp4': mp4({ frames: 20 }), 'abc.mp4': noTimes(0) } })
+ scene('Mismatch', { frames: 60, clips: good })
+ const lockedList = join(ws.dir, q, 'partial_movie_files/ListLocked/partial_movie_file_list.txt')
+ chmodSync(lockedList, 0o000)
+ try {
+ const lib = ws.scan()
+ assert.deepEqual(render(lib, `${q}/Good.mp4`).beats, [{ name: 'uncached_00000.mp4', start: 0, frames: 20, label: null }, { name: 'abc.mp4', start: 20, frames: 25, label: null }])
+ for (const name of ['NoList', 'ListAfter', 'ListLongBefore', 'ListEmpty', 'ListLocked', 'ClipMissing', 'ClipWriting', 'ClipNoFrames', 'Mismatch']) {
+ assert.equal(render(lib, `${q}/${name}.mp4`).beats, null, name)
+ }
+ assert.deepEqual(lib.live, [], 'clips older than their movie are not a render in progress')
+ } finally {
+ chmodSync(lockedList, 0o644)
+ }
+})
+
+test('what changed since the render before: from the sidecar\'s record, and from the last scan', () => {
+ const ws = scratch()
+ const T = Date.now() - 10 * MIN
+ const q = 'out/videos/change/480p15'
+ // The wrapper's record: animations added at the start and the end, a chapter added, one removed.
+ ws.put(`${q}/Changed.mp4`, mp4({ frames: 75 }), T)
+ ws.json(`.harness/renders/${q}/Changed.mp4.json`, {
+ duration: 4, renderedAtMs: T + 60,
+ clips: [{ name: '111_a.mp4', size: 1 }, { name: '222_b.mp4', size: 1 }, { name: '333_c.mp4', size: 1 }, { name: '444_d.mp4', size: 1 }],
+ animations: [0, 1, 2, 3].map((i) => ({ index: i, runTime: i === 1 ? 2 : 1, clip: ['111_a.mp4', '222_b.mp4', '333_c.mp4', '444_d.mp4'][i] })),
+ sections: [{ name: 'Setup', animation: 0 }, { name: 'Main', animation: 1 }, { name: 'New', animation: 3 }],
+ previous: { duration: 3, clips: [{ name: '999_old.mp4', size: 1 }, { name: '222_b.mp4', size: 1 }, { name: '333_c.mp4', size: 1 }], sections: [{ name: 'Setup' }, { name: 'Main' }, { name: 'Gone' }] },
+ }, T + 60)
+ // The same clips as before, recorded without sizes, sections or durations, on a movie with no duration.
+ ws.put(`${q}/Same.mp4`, noTimes(15), T)
+ ws.json(`.harness/renders/${q}/Same.mp4.json`, { clips: [{ name: 'uncached_00000.mp4' }], animations: [{ clip: 'uncached_00000.mp4', runTime: 1 }], previous: { clips: [{ name: 'uncached_00000.mp4' }] } }, T + 60)
+ // The same clips, a chapter gone.
+ ws.put(`${q}/Gone.mp4`, mp4({ frames: 15 }), T)
+ ws.json(`.harness/renders/${q}/Gone.mp4.json`, { clips: ['h.mp4'], animations: [{ clip: 'h.mp4', runTime: 1 }], previous: { clips: [{ name: 'h.mp4' }], sections: [{ name: 'Old' }] } }, T + 60)
+ // The same clips and the same chapters.
+ ws.put(`${q}/Kept.mp4`, mp4({ frames: 30 }), T)
+ ws.json(`.harness/renders/${q}/Kept.mp4.json`, { clips: ['h1.mp4', 'h2.mp4'], animations: [{ clip: 'h1.mp4', runTime: 1, index: 0 }, { clip: 'h2.mp4', runTime: 1, index: 1 }], sections: [{ name: 'A', animation: 0 }, { name: 'B', animation: 1 }], previous: { clips: [{ name: 'h1.mp4' }, { name: 'h2.mp4' }], sections: [{ name: 'A' }, { name: 'B' }] } }, T + 60)
+ // No wrapper: what changed is what this library saw change.
+ ws.put('out/plain.mp4', mp4({ frames: 30 }), T)
+
+ const lib = createLibrary(ws.dir)
+ let scan = lib.scan()
+ assert.deepEqual(render(scan, `${q}/Changed.mp4`).changes, { at: T + 60, durationDelta: 1, framesDelta: null, ranges: [[0, 15], [60, 75]], sections: { added: ['New'], removed: ['Gone'], changed: ['Setup'] }, same: false })
+ assert.deepEqual(render(scan, `${q}/Same.mp4`).changes, { at: T, durationDelta: null, framesDelta: null, ranges: [], sections: null, same: true })
+ assert.deepEqual(render(scan, `${q}/Gone.mp4`).changes.sections, { added: [], removed: ['Old'], changed: [] })
+ assert.equal(render(scan, `${q}/Gone.mp4`).changes.same, false)
+ assert.equal(render(scan, `${q}/Kept.mp4`).changes.same, true)
+ assert.equal(render(scan, 'out/plain.mp4').changes, null, 'nothing seen before')
+
+ // Every movie rendered again, a minute later.
+ for (const [path, data] of [[`${q}/Same.mp4`, noTimes(15)], [`${q}/Gone.mp4`, mp4({ frames: 15 })], ['out/plain.mp4', mp4({ frames: 45 })]]) ws.put(path, data, T + MIN)
+ ws.json(`.harness/renders/${q}/Same.mp4.json`, { clips: [{ name: 'uncached_00000.mp4' }], animations: [{ clip: 'uncached_00000.mp4', runTime: 1 }], previous: { clips: [{ name: 'uncached_00000.mp4' }] } }, T + MIN + 60)
+ ws.json(`.harness/renders/${q}/Gone.mp4.json`, { clips: ['h.mp4'], animations: [{ clip: 'h.mp4', runTime: 1 }], previous: { clips: [{ name: 'h.mp4' }], sections: [{ name: 'Old' }] } }, T + MIN + 60)
+ scan = lib.scan()
+ assert.equal(render(scan, `${q}/Same.mp4`).changes.durationDelta, 0, 'no duration then, none now')
+ assert.equal(render(scan, `${q}/Gone.mp4`).changes.durationDelta, 0)
+ assert.deepEqual(render(scan, 'out/plain.mp4').changes, { at: T + MIN, durationDelta: 1, framesDelta: 15, ranges: null, sections: null, same: false })
+ scan = lib.scan()
+ assert.equal(render(scan, 'out/plain.mp4').changes.framesDelta, 15, 'kept until the next render')
+})
+
+test('renders in progress, judged from the clips on disk', () => {
+ const ws = scratch()
+ const now = Date.now()
+ const T = now - 10 * MIN
+ const q = 'out/videos/live/480p15'
+ // an earlier movie (as .mov), two numbered clips since (one still being written), and what is not a new clip
+ ws.put(`${q}/Live.mov`, mp4({ frames: 45 }), T)
+ ws.put(`${q}/partial_movie_files/Live/uncached_00000.mp4`, mp4({ frames: 15 }), now - 3000)
+ ws.put(`${q}/partial_movie_files/Live/uncached_00002.mp4`, Buffer.from('half a clip being written now'), now - 1000)
+ ws.put(`${q}/partial_movie_files/Live/old.mp4`, mp4({ frames: 15 }), T - 1000)
+ ws.put(`${q}/partial_movie_files/Live/notes.txt`, 'x', now)
+ ws.put(`${q}/partial_movie_files/Live/partial_movie_file_list.txt`, "file 'a.mp4'\nfile 'b.mp4'\nfile 'c.mp4'\n", T - 1000)
+ symlinkSync(join(ws.dir, 'nowhere.mp4'), join(ws.dir, q, 'partial_movie_files/Live/gone.mp4'))
+ // hashed clips in the order they were made, after a GIF movie whose wrapper recorded its animations
+ ws.put(`${q}/Gif.gif`, gif(), T)
+ ws.json(`.harness/renders/${q}/Gif.gif.json`, { animations: [{}, {}, {}] }, T)
+ // made b, then a; b touched last: neither name order nor time order is the order they were made in.
+ // (Times only move forward here: on macOS a time set before a file's birth moves its birth time too.)
+ ws.put(`${q}/partial_movie_files/Gif/b_made_first.mp4`, mp4({ frames: 15 }))
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 20)
+ ws.put(`${q}/partial_movie_files/Gif/a_made_second.mp4`, mp4({ frames: 15 }), now + 50)
+ utimesSync(join(ws.dir, q, 'partial_movie_files/Gif/b_made_first.mp4'), (now + 1000) / 1000, (now + 1000) / 1000)
+ // a first render: no movie yet, and sections on (so Manim's clip list says nothing about the whole)
+ ws.put(`${q}/partial_movie_files/Fresh/uncached_00000.mp4`, mp4({ frames: 15 }), now - 1000)
+ ws.put(`${q}/partial_movie_files/Fresh/partial_movie_file_list.txt`, "file 'a.mp4'\n", now - 1000)
+ ws.json(`${q}/sections/Fresh.json`, [{ name: 'A', nb_frames: 15 }], now - 1000)
+ // abandoned long ago
+ ws.put(`${q}/partial_movie_files/Old/uncached_00000.mp4`, mp4({ frames: 15 }), now - 30 * MIN)
+ // clips outside Manim's layout
+ ws.put('renders/partial_movie_files/Clip/uncached_00000.mp4', mp4({ frames: 15 }), now - 500)
+
+ const byKey = (lib) => Object.fromEntries(lib.live.map((l) => [l.key, l]))
+ let live = byKey(ws.scan())
+ assert.deepEqual(Object.keys(live).sort(), ['out/videos/live/Fresh', 'out/videos/live/Gif', 'out/videos/live/Live', 'renders/Clip'])
+ const l = live['out/videos/live/Live']
+ assert.deepEqual({ ...l, clips: l.clips.map((c) => [c.path.split('/').pop(), c.ready, c.frames]), startedAtMs: undefined, updatedAtMs: undefined }, {
+ key: 'out/videos/live/Live', title: 'Live', module: 'live', quality: '480p15', output: `${q}/Live.mov`, source: 'files', state: 'rendering',
+ startedAtMs: undefined, updatedAtMs: undefined, animation: 3, expected: 3, current: null, sections: [], animations: [],
+ clips: [['uncached_00000.mp4', true, 15], ['uncached_00002.mp4', false, null]], contiguous: false, error: null,
+ })
+ assert.equal(l.updatedAtMs, now - 1000)
+ const g = live['out/videos/live/Gif']
+ assert.deepEqual([g.output, g.animation, g.expected, g.contiguous, g.clips.map((c) => c.path.split('/').pop())], [`${q}/Gif.gif`, 2, 3, false, ['b_made_first.mp4', 'a_made_second.mp4']])
+ const f = live['out/videos/live/Fresh']
+ assert.deepEqual([f.output, f.expected, f.contiguous, f.animation], [`${q}/Fresh.mp4`, null, true, 1])
+ const c = live['renders/Clip']
+ assert.deepEqual([c.title, c.module, c.quality, c.output, c.expected], ['Clip', null, null, 'renders/Clip.mp4', null])
+
+ live = byKey(ws.scan({ now: () => now + 2 * MIN }))
+ assert.equal(live['out/videos/live/Live'].state, 'stalled', 'no new clip for a while')
+})
+
+test('a render the wrapper reports in .harness/render.json', () => {
+ const now = Date.now()
+ const statusOf = (body, { at, files, now: clock } = {}) => {
+ const ws = scratch()
+ if (files) files(ws)
+ ws.json('.harness/render.json', body, at)
+ return ws.scan(clock ? { now: clock } : undefined).live
+ }
+ for (const body of ['not json', 'null', '5', {}]) assert.deepEqual(statusOf(body), [], JSON.stringify(body))
+
+ const [full] = statusOf({
+ state: 'rendering', pid: process.pid, scene: 'S', output: 'out/videos/m/480p15/S.mp4', startedAtMs: now - 5000, updatedAtMs: now - 100,
+ animation: '2', expected: '5', current: 'Write(Text)', quality: '1080p60', source: 'scenes/m.py',
+ sections: [null, { name: 5 }, { name: 'Skipped', skipped: true }, { name: 'Intro', animation: '1' }, { name: 'Body' }],
+ animations: [null, 'x', { index: 0, label: 'Create', runTime: 1, clip: 'c.mp4', section: 0 }, { index: 1 }],
+ clips: ['out/missing.mp4', 5, 'out/clip.mp4'], error: null,
+ }, { files: (ws) => ws.put('out/clip.mp4', mp4({ frames: 15 }), now - 60_000) })
+ assert.deepEqual(full, {
+ key: 'out/videos/m/S', title: 'S', module: 'm', quality: '1080p60', output: 'out/videos/m/480p15/S.mp4', sourceFile: 'scenes/m.py', source: 'status',
+ state: 'rendering', startedAtMs: now - 5000, updatedAtMs: now - 100, finishedAtMs: null, animation: 2, expected: 5, current: 'Write(Text)',
+ sections: [{ name: 'Intro', animation: 1 }, { name: 'Body', animation: 0 }],
+ animations: [{ index: 0, label: 'Create', runTime: 1, clip: 'c.mp4', section: 0 }, { index: 1, label: null, runTime: null, clip: null, section: null }],
+ clips: [{ path: 'out/missing.mp4', ready: false }, { path: 'out/clip.mp4', ready: true, frames: 15, fps: 15, duration: 1, width: 854, height: 480, mtimeMs: now - 60_000 }],
+ contiguous: true, error: null,
+ })
+
+ const at = now - 30_000
+ const [bare] = statusOf({ state: 'rendering', pid: 'not a pid' }, { at })
+ assert.deepEqual(bare, { key: 'render', title: 'Render', module: null, quality: null, output: null, sourceFile: null, source: 'status', state: 'stopped', startedAtMs: null, updatedAtMs: at, finishedAtMs: null, animation: 0, expected: null, current: null, sections: [], animations: [], clips: [], contiguous: true, error: null })
+ assert.equal(statusOf({ state: 'rendering', pid: -1 })[0].state, 'stopped')
+ assert.equal(statusOf({ state: 'rendering', pid: 1 })[0].state, 'rendering', 'a process this user may not signal is still alive')
+ assert.equal(statusOf({ state: 'rendering', scene: 'Quiet' }, { at: now - 2 * MIN })[0].state, 'stalled', 'no pid, no update for a while')
+ const [quiet] = statusOf({ state: 'rendering', scene: 'Quiet', updatedAtMs: now - 2 * MIN }, { at: now })
+ assert.deepEqual([quiet.state, quiet.key, quiet.title], ['stalled', 'Quiet', 'Quiet'])
+ assert.equal(statusOf({ state: 'rendering', updatedAtMs: now })[0].state, 'rendering')
+
+ const output = 'out/videos/m/480p15/S.mp4'
+ assert.deepEqual(statusOf({ state: 'done', output }), [])
+ assert.equal(statusOf({ state: 'failed', output, finishedAtMs: now - 5000, error: { message: 'boom' } })[0].error.message, 'boom')
+ assert.deepEqual(statusOf({ state: 'failed', output, finishedAtMs: now - 5000 }, { files: (ws) => ws.put(output, mp4({ frames: 15 }), now - 1000) }), [], 'a newer render of the same scene replaces the failure')
+ assert.equal(statusOf({ state: 'failed', output, updatedAtMs: now - 5000 }, { files: (ws) => ws.put(output, mp4({ frames: 15 }), now - 9000) }).length, 1, 'an older render does not')
+ assert.deepEqual(statusOf({ state: 'failed', output }, { at: now - 25 * MIN }), [], 'a failure is forgotten after a while')
+})
+
+test('the status and the files, about the same render', () => {
+ const now = Date.now()
+ const q = 'out/videos/m/480p15'
+ const both = (status, { clipAt = now - 500, clock } = {}) => {
+ const ws = scratch()
+ ws.put(`${q}/partial_movie_files/S/uncached_00000.mp4`, mp4({ frames: 15 }), clipAt)
+ ws.json('.harness/render.json', { output: `${q}/S.mp4`, ...status })
+ return ws.scan(clock ? { now: clock } : undefined).live
+ }
+ assert.deepEqual(both({ state: 'failed', updatedAtMs: now - 5000 }).map((l) => l.source), ['files'], 'a plain render after the wrapper\'s failed run is newer news')
+ assert.deepEqual(both({ state: 'failed', updatedAtMs: now - 800 }).map((l) => l.source), ['status'])
+ assert.deepEqual(both({ state: 'rendering', updatedAtMs: now - 5000, pid: process.pid }).map((l) => l.source), ['status'], 'the wrapper is still running: its word stands')
+ assert.deepEqual(both({ state: 'done' }).map((l) => l.source), ['files'], 'clips still arriving after a done status: rendering again')
+ assert.deepEqual(both({ state: 'done' }, { clock: () => now + 2 * MIN }), [], 'stalled clips after a done status are what the render left behind')
+ const ws = scratch()
+ ws.json('.harness/render.json', { state: 'failed', output: 'renders/x.mp4', updatedAtMs: now })
+ ws.put('out/videos/m/480p15/partial_movie_files/Other/uncached_00000.mp4', mp4({ frames: 15 }), now - 1000)
+ assert.deepEqual(ws.scan().live.map((l) => [l.source, l.key]), [['status', 'renders/x.mp4'], ['files', 'out/videos/m/Other']], 'two renders, newest first')
+})
+
+test('the verdict and the scene count', () => {
+ const ws = scratch()
+ assert.deepEqual([ws.scan().verdict, ws.scan().scenes], [null, 0])
+ ws.json('.harness/verdict.json', { ready: 1, summary: '2 renders', artifact: 'out/a.mp4', updatedAt: '2026-09-17T00:00:00Z' })
+ ws.put('scenes/a.py', 'x')
+ ws.put('scenes/b.py', 'x')
+ ws.put('scenes/notes.md', 'x')
+ assert.deepEqual([ws.scan().verdict, ws.scan().scenes], [{ ready: true, summary: '2 renders', artifact: 'out/a.mp4', updatedAt: '2026-09-17T00:00:00Z' }, 2])
+ ws.json('.harness/verdict.json', {})
+ assert.deepEqual(ws.scan().verdict, { ready: false, summary: '', artifact: null, updatedAt: null })
+ ws.json('.harness/verdict.json', '5')
+ assert.equal(ws.scan().verdict, null)
+})
+
+test('a sidecar or sections file that is not the shape written costs that render its extras, never the library', () => {
+ const ws = scratch()
+ const T = Date.now() - 10 * MIN
+ const q = 'out/videos/bad/480p15'
+ const cases = {
+ Animations: { clips: ['a.mp4'], animations: { a: 1 } },
+ ClipName: { clipsDir: `${q}/partial_movie_files/ClipName`, clips: [{ name: 5 }] },
+ Previous: { clips: ['a.mp4'], animations: [{ clip: 'a.mp4', runTime: 1 }], previous: { clips: [null] } },
+ SectionEntry: { clips: ['a.mp4'], animations: [{ clip: 'a.mp4', runTime: 1 }], sections: [null, null] },
+ }
+ for (const [scene, sidecar] of Object.entries(cases)) {
+ ws.put(`${q}/${scene}.mp4`, mp4({ frames: 15 }), T)
+ ws.json(`.harness/renders/${q}/${scene}.mp4.json`, sidecar, T + 50)
+ }
+ ws.put(`${q}/Sections.mp4`, mp4({ frames: 15 }), T)
+ ws.json(`${q}/sections/Sections.json`, [null], T + 50)
+ ws.put('out/good.mp4', mp4({ frames: 30 }), T)
+
+ const lib = ws.scan()
+ assert.equal(lib.renders.length, 6)
+ for (const scene of [...Object.keys(cases), 'Sections']) {
+ const r = render(lib, `${q}/${scene}.mp4`)
+ assert.deepEqual([r.complete, r.frames, r.title, r.beats, r.sections, r.changes], [true, 15, scene, null, null, null], scene)
+ }
+ assert.equal(render(lib, 'out/good.mp4').frames, 30)
+})
diff --git a/store/viewers/video-viewer/test/media.mjs b/store/viewers/video-viewer/test/media.mjs
new file mode 100644
index 000000000..72210ab7c
--- /dev/null
+++ b/store/viewers/video-viewer/test/media.mjs
@@ -0,0 +1,137 @@
+// Synthetic media and scratch workspaces for the tests: MP4s with a real moov box, GIFs and PNGs with
+// real headers, and a viewer process started the way Harness starts it. No Manim, no ffmpeg.
+import { spawn } from 'node:child_process'
+import { mkdirSync, mkdtempSync, rmSync, utimesSync, writeFileSync } from 'node:fs'
+import { request } from 'node:http'
+import { createServer } from 'node:net'
+import { tmpdir } from 'node:os'
+import { dirname, join } from 'node:path'
+import { fileURLToPath, pathToFileURL } from 'node:url'
+
+export const PACKAGE = join(dirname(fileURLToPath(import.meta.url)), '..')
+
+export const u32 = (n) => { const b = Buffer.alloc(4); b.writeUInt32BE(n); return b }
+export const u16 = (n) => { const b = Buffer.alloc(2); b.writeUInt16BE(n); return b }
+export const box = (type, ...parts) => { const body = Buffer.concat(parts); return Buffer.concat([u32(8 + body.length), Buffer.from(type, 'latin1'), body]) }
+
+/** A video trak: tkhd (size), mdia with mdhd, hdlr and an stbl whose stts gives the frames. */
+export function videoTrak({ frames, fps = 15, width = 854, height = 480, ts = 15360, handler = 'vide', tkhd = true, stsd = true, stts = true, mdhdVersion = 0 } = {}) {
+ const delta = ts / fps, dur = frames * delta
+ const mdhd = mdhdVersion === 1
+ ? box('mdhd', Buffer.from([1, 0, 0, 0]), Buffer.alloc(16), u32(ts), u32(0), u32(dur), u16(0), u16(0))
+ : box('mdhd', u32(0), u32(0), u32(0), u32(ts), u32(dur), u16(0), u16(0))
+ return box('trak',
+ ...(tkhd ? [box('tkhd', u32(0), u32(0), u32(0), u32(1), u32(0), u32(dur), Buffer.alloc(8), u16(0), u16(0), u16(0), u16(0), Buffer.alloc(36), u32(width * 65536), u32(height * 65536))] : []),
+ box('mdia', mdhd,
+ box('hdlr', u32(0), u32(0), Buffer.from(handler, 'latin1'), Buffer.alloc(12), Buffer.from([0])),
+ box('minf', box('stbl',
+ ...(stsd ? [box('stsd', u32(0), u32(1), u32(16), Buffer.from('avc1', 'latin1'), Buffer.alloc(8))] : []),
+ ...(stts ? [box('stts', u32(0), u32(1), u32(frames), u32(delta))] : [])))))
+}
+
+/** A minimal MP4: ftyp, mdat, moov with one video track (and optionally a sound track). */
+export function mp4({ frames, fps = 15, width = 854, height = 480, audio = false, moovFirst = false }) {
+ const ts = 15360, dur = frames * (ts / fps)
+ const moov = box('moov', box('mvhd', u32(0), u32(0), u32(0), u32(ts), u32(dur), Buffer.alloc(80)), videoTrak({ frames, fps, width, height, ts }), ...(audio ? [videoTrak({ frames, fps, ts, handler: 'soun' })] : []))
+ const ftyp = box('ftyp', Buffer.from('isom', 'latin1'), u32(512))
+ const mdat = box('mdat', Buffer.alloc(64))
+ return moovFirst ? Buffer.concat([ftyp, moov, mdat]) : Buffer.concat([ftyp, mdat, moov])
+}
+
+/** A GIF of `delays` frames (hundredths of a second each; 0 means "unset"), 1×1, with or without a trailer. */
+export function gif({ delays = [10, 10], globalTable = true, localTable = false, trailer = true, width = 32, height = 24 } = {}) {
+ const parts = [Buffer.from('GIF89a', 'latin1'), Buffer.from([width & 255, width >> 8, height & 255, height >> 8, globalTable ? 0x80 : 0, 0, 0])]
+ if (globalTable) parts.push(Buffer.alloc(6)) // 2 colours
+ parts.push(Buffer.from([0x21, 0xff, 0x0b, ...Buffer.from('NETSCAPE2.0', 'latin1'), 0x03, 0x01, 0x00, 0x00, 0x00])) // an extension that is not a delay
+ for (const delay of delays) {
+ parts.push(Buffer.from([0x21, 0xf9, 0x04, 0x00, delay & 255, delay >> 8, 0x00, 0x00]))
+ parts.push(Buffer.from([0x2c, 0, 0, 0, 0, 1, 0, 1, 0, localTable ? 0x80 : 0]))
+ if (localTable) parts.push(Buffer.alloc(6))
+ parts.push(Buffer.from([0x02, 0x02, 0x44, 0x01, 0x00]))
+ }
+ if (trailer) parts.push(Buffer.from([0x3b]))
+ return Buffer.concat(parts)
+}
+
+/** A PNG with an IHDR and, when `complete`, an IEND chunk at the end. */
+export function png({ width = 640, height = 360, complete = true } = {}) {
+ const chunk = (type, data) => Buffer.concat([u32(data.length), Buffer.from(type, 'latin1'), data, u32(0)])
+ return Buffer.concat([Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), chunk('IHDR', Buffer.concat([u32(width), u32(height), Buffer.from([8, 6, 0, 0, 0])])), chunk('IDAT', Buffer.alloc(4)), ...(complete ? [chunk('IEND', Buffer.alloc(0))] : [])])
+}
+
+export function workspace(prefix = 'video-viewer-test-') {
+ const dir = mkdtempSync(join(tmpdir(), prefix))
+ const put = (rel, data, mtimeMs) => {
+ const abs = join(dir, rel)
+ mkdirSync(dirname(abs), { recursive: true })
+ writeFileSync(abs, data)
+ if (mtimeMs) utimesSync(abs, mtimeMs / 1000, mtimeMs / 1000)
+ return abs
+ }
+ return { dir, put, done: () => rmSync(dir, { recursive: true, force: true }) }
+}
+
+export const freePort = () => new Promise((ok) => { const s = createServer().listen(0, '127.0.0.1', () => { const { port } = s.address(); s.close(() => ok(port)) }) })
+
+export async function until(check, what, ms = 8000) {
+ const deadline = Date.now() + ms
+ for (;;) {
+ const value = await check()
+ if (value) return value
+ if (Date.now() > deadline) throw new Error(`timed out waiting for ${what}`)
+ await new Promise((r) => setTimeout(r, 25))
+ }
+}
+
+/**
+ * The viewer as Harness runs it: viewer.sh with a port and a workspace, the test preload imported
+ * (it shortens the timers named in TEST_TIMERS). stop() sends SIGTERM and waits for a clean exit, which
+ * is also when V8 writes the process's coverage.
+ */
+export async function startViewer(ws, env = {}) {
+ const port = await freePort()
+ const preload = pathToFileURL(join(PACKAGE, 'test', 'preload.mjs')).href
+ const child = spawn(join(PACKAGE, 'viewer.sh'), {
+ env: { ...process.env, NODE_OPTIONS: `${process.env.NODE_OPTIONS ?? ''} --import=${preload}`, HARNESS_VIEWER_PORT: String(port), HARNESS_WORKSPACE: ws, ...env },
+ stdio: ['ignore', 'pipe', 'pipe'],
+ })
+ let output = ''
+ child.stdout.on('data', (d) => { output += d })
+ child.stderr.on('data', (d) => { output += d })
+ const exited = new Promise((ok) => child.on('exit', (code, signal) => ok({ code, signal })))
+ await Promise.race([
+ until(() => output.includes('listening'), 'the viewer to listen', 15_000),
+ exited.then((e) => { throw new Error(`viewer exited ${JSON.stringify(e)}: ${output}`) }),
+ ])
+ const get = (path, { method = 'GET', headers = {}, body } = {}) => new Promise((ok, fail) => {
+ const req = request({ host: '127.0.0.1', port, path, method, headers }, (res) => {
+ const chunks = []
+ res.on('data', (c) => chunks.push(c))
+ res.on('end', () => ok({ status: res.statusCode, headers: res.headers, body: Buffer.concat(chunks) }))
+ res.on('error', fail)
+ })
+ req.on('error', fail)
+ req.end(body)
+ })
+ /** An event stream: `text` grows as events arrive; wait(predicate) resolves with the text once it matches. */
+ const events = (path = '/events') => new Promise((ok, fail) => {
+ const stream = { text: '', ended: false }
+ const req = request({ host: '127.0.0.1', port, path }, (res) => {
+ res.setEncoding('utf8')
+ res.on('data', (c) => { stream.text += c })
+ res.on('end', () => { stream.ended = true })
+ res.on('error', () => { stream.ended = true })
+ stream.status = res.statusCode
+ stream.headers = res.headers
+ ok(stream)
+ })
+ req.on('error', fail)
+ req.end()
+ stream.close = () => req.destroy()
+ stream.wait = (predicate, what, ms) => until(() => predicate(stream.text), what, ms)
+ /** Every `library` event's data, parsed. */
+ stream.libraries = () => stream.text.split('\n\n').filter((b) => b.includes('event: library')).map((b) => JSON.parse(b.split('\ndata: ')[1]))
+ })
+ const stop = async (signal = 'SIGTERM') => { if (child.exitCode === null && child.signalCode === null) child.kill(signal); return exited }
+ return { port, child, get, events, stop, exited, output: () => output }
+}
diff --git a/store/viewers/video-viewer/test/no-birthtime.test.mjs b/store/viewers/video-viewer/test/no-birthtime.test.mjs
new file mode 100644
index 000000000..5dfa78aa0
--- /dev/null
+++ b/store/viewers/video-viewer/test/no-birthtime.test.mjs
@@ -0,0 +1,31 @@
+// Clips in progress are ordered by when they were made; a filesystem that keeps no birth time (some
+// Linux mounts report 0) orders them by their modification time instead. This file stands in for such
+// a filesystem by patching statSync before the library is loaded, so it runs in a process of its own.
+//
+// node --test test/*.test.mjs
+import assert from 'node:assert/strict'
+import fs from 'node:fs'
+import { syncBuiltinESMExports } from 'node:module'
+import { after, test } from 'node:test'
+import { mp4, workspace } from './media.mjs'
+
+const statSync = fs.statSync
+fs.statSync = (...args) => { const st = statSync(...args); st.birthtimeMs = 0; return st }
+syncBuiltinESMExports()
+const { createLibrary } = await import('../lib/library.mjs')
+
+const ws = workspace('video-viewer-no-birth-')
+after(() => ws.done())
+
+test('without birth times, clips in progress are in modification order', () => {
+ const now = Date.now()
+ const q = 'out/videos/b/480p15/partial_movie_files/B'
+ // times only move forward: on macOS a time set before a file's birth moves its birth time too
+ ws.put(`${q}/made-first-touched-last.mp4`, mp4({ frames: 15 }), now + 1000)
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 20)
+ ws.put(`${q}/made-last-touched-first.mp4`, mp4({ frames: 15 }), now + 50)
+ const [live] = createLibrary(ws.dir).scan().live
+ assert.deepEqual(live.clips.map((c) => c.path.split('/').pop()), ['made-last-touched-first.mp4', 'made-first-touched-last.mp4'])
+ assert.equal(live.startedAtMs, now + 50)
+ assert.equal(live.updatedAtMs, now + 1000)
+})
diff --git a/store/viewers/video-viewer/test/preload.mjs b/store/viewers/video-viewer/test/preload.mjs
new file mode 100644
index 000000000..f2d3b4f99
--- /dev/null
+++ b/store/viewers/video-viewer/test/preload.mjs
@@ -0,0 +1,25 @@
+// Loaded with --import into a viewer under test (never by the viewer itself):
+//
+// TEST_TIMERS="250=1500,20000=100" a timer asked for with one delay runs with the other, so a test
+// sees a ping, a poll or a settled burst without waiting for it
+// TEST_WATCH=null-name the watcher also reports one change with no file name, which
+// fs.watch is allowed to do
+import fs from 'node:fs'
+import { syncBuiltinESMExports } from 'node:module'
+
+const delays = new Map(String(process.env.TEST_TIMERS ?? '').split(',').filter(Boolean).map((pair) => pair.split('=').map(Number)))
+const later = globalThis.setTimeout
+for (const name of ['setTimeout', 'setInterval']) {
+ const original = globalThis[name]
+ globalThis[name] = (fn, ms, ...args) => original(fn, delays.get(ms) ?? ms, ...args)
+}
+
+if (process.env.TEST_WATCH === 'null-name') {
+ const watch = fs.watch
+ fs.watch = (...args) => {
+ const watcher = watch(...args)
+ later(() => watcher.emit('change', 'rename', null), 50)
+ return watcher
+ }
+ syncBuiltinESMExports()
+}
diff --git a/store/viewers/video-viewer/test/server.test.mjs b/store/viewers/video-viewer/test/server.test.mjs
new file mode 100644
index 000000000..232f7d056
--- /dev/null
+++ b/store/viewers/video-viewer/test/server.test.mjs
@@ -0,0 +1,233 @@
+// The server as Harness runs it: viewer.sh with a port and a scratch workspace. The page, workspace
+// files with byte ranges and nothing outside the workspace, stills from the page, the library pushed
+// over server-sent events as the workspace changes, and requests that are the client's mistake
+// answered instead of taking the pane down.
+//
+// node --test test/*.test.mjs
+import assert from 'node:assert/strict'
+import { spawn } from 'node:child_process'
+import { chmodSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
+import { join } from 'node:path'
+import { after, before, describe, test } from 'node:test'
+import { mp4, png, startViewer, until, workspace } from './media.mjs'
+
+describe('a viewer on a workspace with renders', () => {
+ let ws, viewer
+ before(async () => {
+ ws = workspace()
+ ws.put('out/intro.mp4', mp4({ frames: 30 }))
+ ws.put('out/range.bin', Buffer.from('0123456789'))
+ ws.put('out/empty.mp4', Buffer.alloc(0))
+ ws.put('out/locked.mp4', mp4({ frames: 5 }))
+ chmodSync(join(ws.dir, 'out/locked.mp4'), 0o000)
+ mkdirSync(join(ws.dir, 'out/folder.mp4'))
+ viewer = await startViewer(ws.dir, { TEST_TIMERS: '20000=100,2000=100', TEST_WATCH: 'null-name' })
+ })
+ after(async () => {
+ await viewer?.stop()
+ chmodSync(join(ws.dir, 'out/locked.mp4'), 0o644)
+ ws.done()
+ })
+
+ test('serves the page and its files, and only its files', async () => {
+ for (const path of ['/', '/index.html']) {
+ const page = await viewer.get(path)
+ assert.equal(page.status, 200, path)
+ assert.equal(page.headers['content-type'], 'text/html; charset=utf-8')
+ assert.match(page.body.toString(), /app\.js/)
+ }
+ const script = await viewer.get('/ui/app.js')
+ assert.equal(script.status, 200)
+ assert.equal(script.headers['content-type'], 'text/javascript; charset=utf-8')
+ assert.equal((await viewer.get('/ui/%2e%2e%2fviewer.mjs')).status, 404, 'nothing beside the page')
+ assert.equal((await viewer.get('/ui/nope.js')).status, 404)
+ })
+
+ test('serves workspace files with byte ranges, at /ws/ and at the root', async () => {
+ const whole = await viewer.get('/ws/out/intro.mp4')
+ assert.equal(whole.status, 200)
+ assert.equal(whole.headers['content-type'], 'video/mp4')
+ assert.equal(whole.headers['accept-ranges'], 'bytes')
+ assert.equal(whole.headers['cache-control'], 'no-cache')
+ assert.deepEqual(whole.body, readFileSync(join(ws.dir, 'out/intro.mp4')))
+ assert.deepEqual((await viewer.get('/out/intro.mp4')).body, whole.body, 'the first version linked files at the root')
+
+ const part = async (range) => {
+ const r = await viewer.get('/ws/out/range.bin', { headers: { range } })
+ return [r.status, r.headers['content-range'] ?? null, r.body.toString()]
+ }
+ assert.deepEqual(await part('bytes=2-4'), [206, 'bytes 2-4/10', '234'])
+ assert.deepEqual(await part('bytes=7-'), [206, 'bytes 7-9/10', '789'], 'open-ended')
+ assert.deepEqual(await part('bytes=6-99'), [206, 'bytes 6-9/10', '6789'], 'clamped to the end')
+ assert.deepEqual(await part('bytes=-3'), [206, 'bytes 7-9/10', '789'], 'the last three bytes')
+ assert.deepEqual(await part('bytes=-30'), [206, 'bytes 0-9/10', '0123456789'], 'a suffix longer than the file')
+ assert.deepEqual(await part('bytes=5-3'), [416, 'bytes */10', ''])
+ assert.deepEqual(await part('bytes=10-'), [416, 'bytes */10', ''])
+ assert.deepEqual(await part('bytes=0-1,4-5'), [200, null, '0123456789'], 'a multipart range is answered with the whole file')
+ const head = await viewer.get('/ws/out/range.bin', { method: 'HEAD', headers: { range: 'bytes=0-1' } })
+ assert.equal(head.status, 200)
+ assert.equal(head.headers['content-length'], '10')
+ assert.equal(head.headers['content-type'], 'application/octet-stream')
+ const empty = await viewer.get('/ws/out/empty.mp4', { headers: { range: 'bytes=0-' } })
+ assert.equal(empty.status, 200, 'an empty file has no range to give')
+ assert.equal(empty.body.length, 0)
+ })
+
+ test('a Range that names no byte is served whole instead of killing the server', async () => {
+ const r = await viewer.get('/ws/out/range.bin', { headers: { range: 'bytes=-' } })
+ assert.equal(r.status, 200)
+ assert.equal(r.body.toString(), '0123456789')
+ assert.equal((await viewer.get('/api/library')).status, 200, 'still up')
+ })
+
+ test('a malformed path or request line is a 400, and the server stays up', async () => {
+ assert.equal((await viewer.get('/%zz')).status, 400)
+ assert.equal((await viewer.get('/ui/%E0%A4%A')).status, 400)
+ assert.equal((await viewer.get('http://a:99999/')).status, 400, 'an absolute-form target that is not a URL')
+ assert.equal((await viewer.get('/api/library')).status, 200)
+ })
+
+ test('nothing outside the workspace, nothing that is not a file', async () => {
+ assert.equal((await viewer.get('/ws/..%2F..%2F..%2Fetc%2Fhosts')).status, 403)
+ assert.equal((await viewer.get('/..%2F..%2Fetc%2Fhosts')).status, 403)
+ assert.equal((await viewer.get('/ws/out/missing.mp4')).status, 404)
+ assert.equal((await viewer.get('/ws/out/folder.mp4')).status, 404)
+ assert.equal((await viewer.get('/api/still')).status, 404, 'a GET is a workspace path, not the still endpoint')
+ })
+
+ test('a file that cannot be read drops the connection rather than hanging it', async () => {
+ await assert.rejects(viewer.get('/ws/out/locked.mp4'))
+ await assert.rejects(viewer.get('/ws/out/locked.mp4', { headers: { range: 'bytes=0-3' } }))
+ })
+
+ test('stills: only from the page, only PNG, named safely, into .harness/stills', async () => {
+ const still = (query, body, headers = { 'x-video-viewer': 'still' }) => viewer.get(`/api/still${query}`, { method: 'POST', headers, body })
+ assert.equal((await still('?name=x', png(), {})).status, 403, 'another origin cannot send the header')
+ assert.equal((await still('?name=x', Buffer.from('PNG'))).status, 400, 'too short')
+ assert.equal((await still('?name=x', Buffer.from('GIF89a..........'))).status, 400, 'not a PNG')
+
+ const saved = await still('?name=Intro%20%2F..%2F..%2Fframe%2012', png())
+ assert.equal(saved.status, 200)
+ const answer = JSON.parse(saved.body)
+ assert.equal(answer.path, '.harness/stills/Intro-..-..-frame-12.png')
+ assert.equal(answer.abs, join(ws.dir, answer.path))
+ assert.deepEqual(readFileSync(answer.abs), png())
+ assert.equal(JSON.parse((await still('', png())).body).path, '.harness/stills/frame.png', 'no name')
+ assert.equal(JSON.parse((await still('?name=...', png())).body).path, '.harness/stills/frame.png', 'a name that is only dots')
+ })
+
+ test('a still larger than 64 MiB is cut off', async () => {
+ const body = Buffer.alloc(64 * 1024 * 1024 + 1024)
+ png().copy(body)
+ const r = await viewer.get('/api/still?name=huge', { method: 'POST', headers: { 'x-video-viewer': 'still' }, body }).catch((error) => error)
+ assert.ok(r instanceof Error || r.status !== 200, 'no 200 for an oversized still')
+ assert.equal(existsSync(join(ws.dir, '.harness/stills/huge.png')), false)
+ })
+
+ test('the library is served and pushed: first the current one, then every change, with pings between', async () => {
+ const library = JSON.parse((await viewer.get('/api/library')).body)
+ assert.deepEqual(library.renders.map((r) => r.path).sort(), ['out/empty.mp4', 'out/intro.mp4', 'out/locked.mp4'])
+ assert.equal(library.workspace.path, ws.dir)
+
+ const stream = await viewer.events()
+ assert.equal(stream.headers['content-type'], 'text/event-stream')
+ await stream.wait((t) => t.startsWith('retry: 1000\nevent: library\ndata: '), 'the first event')
+ await stream.wait((t) => t.includes(': ping'), 'a ping')
+ ws.put('notes.txt', 'not a render')
+ ws.put('node_modules/pkg/clip.mp4', mp4({ frames: 1 }))
+ ws.put('.harness/other.json', '{}')
+ ws.put('scenes/proof.py', 'class Proof: pass')
+ await stream.wait(() => stream.libraries().some((l) => l.scenes === 1), 'a library event with the new scene')
+ ws.put('out/second.mp4', mp4({ frames: 45 }))
+ await stream.wait(() => stream.libraries().some((l) => l.renders.some((r) => r.path === 'out/second.mp4')), 'the new render')
+ const last = stream.libraries().at(-1)
+ assert.equal(last.renders.some((r) => r.path.startsWith('node_modules')), false)
+ stream.close()
+ })
+
+ test('a render whose process dies is reported stopped with no file changing', async () => {
+ const sleeper = spawn(process.execPath, ['-e', 'setTimeout(() => {}, 60_000)'], { stdio: 'ignore' })
+ const stream = await viewer.events()
+ ws.put('.harness/render.json', JSON.stringify({ state: 'rendering', pid: sleeper.pid, scene: 'Proof', output: 'out/videos/proof/480p15/Proof.mp4', updatedAtMs: Date.now() }))
+ await stream.wait(() => stream.libraries().some((l) => l.live[0]?.state === 'rendering'), 'the render in progress')
+ const exited = new Promise((ok) => sleeper.on('exit', ok))
+ sleeper.kill('SIGKILL')
+ await exited
+ await stream.wait(() => stream.libraries().some((l) => l.live[0]?.state === 'stopped'), 'the render reported stopped')
+ // A stopped render is still re-read on the clock for a while, but the same library is not pushed again.
+ const events = stream.libraries().length
+ const pings = stream.text.split(': ping').length
+ await stream.wait((t) => t.split(': ping').length >= pings + 5, 'five more pings')
+ assert.equal(stream.libraries().length, events)
+ stream.close()
+ rmSync(join(ws.dir, '.harness/render.json'))
+ })
+
+ test('a still that cannot be written is a 500', async () => {
+ rmSync(join(ws.dir, '.harness'), { recursive: true, force: true })
+ writeFileSync(join(ws.dir, '.harness'), 'a file where the folder should be')
+ const r = await viewer.get('/api/still?name=x', { method: 'POST', headers: { 'x-video-viewer': 'still' }, body: png() })
+ assert.equal(r.status, 500)
+ rmSync(join(ws.dir, '.harness'))
+ })
+
+ test('SIGTERM ends the event streams and exits cleanly', async () => {
+ const stream = await viewer.events()
+ await stream.wait((t) => t.includes('event: library'), 'the first event')
+ assert.deepEqual(await viewer.stop('SIGTERM'), { code: 0, signal: null })
+ await until(() => stream.ended, 'the stream to end')
+ })
+})
+
+test('a sidecar that is not the shape written does not keep the pane from starting', async () => {
+ const ws = workspace()
+ ws.put('out/videos/m/480p15/S.mp4', mp4({ frames: 15 }), Date.now() - 60_000)
+ ws.put('.harness/renders/out/videos/m/480p15/S.mp4.json', JSON.stringify({ clips: ['a.mp4'], animations: { not: 'a list' } }))
+ const viewer = await startViewer(ws.dir)
+ try {
+ const library = JSON.parse((await viewer.get('/api/library')).body)
+ assert.deepEqual(library.renders.map((r) => [r.path, r.complete, r.frames, r.beats]), [['out/videos/m/480p15/S.mp4', true, 15, null]])
+ } finally {
+ await viewer.stop()
+ ws.done()
+ }
+})
+
+test('a long burst of writes is announced without waiting for it to settle', async () => {
+ const ws = workspace()
+ // 250 ms is the settle delay; stretched, only the "a beat after it began" rule can answer in time.
+ const viewer = await startViewer(ws.dir, { TEST_TIMERS: '250=6000' })
+ try {
+ const stream = await viewer.events()
+ await stream.wait((t) => t.includes('event: library'), 'the first event')
+ ws.put('out/a.mp4', mp4({ frames: 10 }))
+ await new Promise((r) => setTimeout(r, 2000))
+ const written = Date.now()
+ ws.put('out/b.mp4', mp4({ frames: 10 }))
+ await stream.wait(() => stream.libraries().some((l) => l.renders.length === 2), 'both renders', 10_000)
+ assert.ok(Date.now() - written < 5000, `announced ${Date.now() - written} ms after the write, not held for the settle delay`)
+ stream.close()
+ } finally {
+ await viewer.stop()
+ ws.done()
+ }
+})
+
+test('a workspace that cannot be watched is polled', async () => {
+ const ws = workspace()
+ const missing = join(ws.dir, 'not-yet')
+ const viewer = await startViewer(missing, { TEST_TIMERS: '1500=100' })
+ try {
+ assert.match(viewer.output(), /watch failed \(.*\); polling instead/)
+ const stream = await viewer.events()
+ await stream.wait((t) => t.includes('event: library'), 'the first event')
+ assert.deepEqual(stream.libraries()[0].renders, [])
+ ws.put('not-yet/out/late.mp4', mp4({ frames: 10 }))
+ await stream.wait(() => stream.libraries().some((l) => l.renders.some((r) => r.path === 'out/late.mp4')), 'the render found by polling')
+ stream.close()
+ assert.deepEqual(await viewer.stop('SIGINT'), { code: 0, signal: null })
+ } finally {
+ await viewer.stop()
+ ws.done()
+ }
+})
diff --git a/store/viewers/video-viewer/viewer.mjs b/store/viewers/video-viewer/viewer.mjs
index 6416bf901..bccc4f935 100644
--- a/store/viewers/video-viewer/viewer.mjs
+++ b/store/viewers/video-viewer/viewer.mjs
@@ -46,8 +46,9 @@ function serveFile(req, res, full) {
const type = TYPES[extname(full).toLowerCase()] ?? 'application/octet-stream'
const base = { 'content-type': type, 'accept-ranges': 'bytes', 'cache-control': 'no-cache', 'last-modified': st.mtime.toUTCString() }
if (req.method === 'HEAD') { res.writeHead(200, { ...base, 'content-length': size }); return res.end() }
+ // `bytes=-` names no byte at all; like any Range this server cannot satisfy, it gets the whole file.
const range = /^bytes=(\d*)-(\d*)$/.exec(req.headers.range ?? '')
- if (range && size > 0) {
+ if (range && (range[1] || range[2]) && size > 0) {
let start = range[1] ? Number(range[1]) : NaN, end = range[2] ? Number(range[2]) : NaN
if (Number.isNaN(start)) { start = Math.max(0, size - end); end = size - 1 } else end = Number.isNaN(end) ? size - 1 : Math.min(end, size - 1)
if (start > end || start >= size) { res.writeHead(416, { 'content-range': `bytes */${size}` }); return res.end() }
@@ -59,8 +60,7 @@ function serveFile(req, res, full) {
}
function publish() {
- let next
- try { next = library.scan() } catch (error) { console.log(`[video-viewer] scan failed: ${error.message}`); return }
+ const next = library.scan()
const json = JSON.stringify(next)
if (json === currentJson) return
current = next; currentJson = json
@@ -68,11 +68,13 @@ function publish() {
}
const server = createServer((req, res) => {
- const url = new URL(req.url ?? '/', `http://127.0.0.1:${port}`)
- const path = url.pathname
+ // A request line that is not a URL (`GET http://a:99999/`) or a path that is not UTF-8 (`/%zz`) is the
+ // client's mistake: answer it, never throw out of the handler and take the pane down.
+ let url, path
+ try { url = new URL(req.url, `http://127.0.0.1:${port}`); path = decodeURIComponent(url.pathname) } catch { return send(res, 400, 'bad request') }
if (path === '/' || path === '/index.html') return serveFile(req, res, join(here, 'ui', 'index.html'))
if (path.startsWith('/ui/')) {
- const full = inside(join(here, 'ui'), decodeURIComponent(path.slice(4)))
+ const full = inside(join(here, 'ui'), path.slice(4))
return full ? serveFile(req, res, full) : send(res, 404, 'not found')
}
if (path === '/api/library') return send(res, 200, currentJson, { 'content-type': 'application/json' })
@@ -102,7 +104,7 @@ const server = createServer((req, res) => {
})
return
}
- const rel = decodeURIComponent(path.startsWith('/ws/') ? path.slice(4) : path.slice(1))
+ const rel = path.startsWith('/ws/') ? path.slice(4) : path.slice(1)
const full = inside(workspace, rel)
if (!full) return send(res, 403, 'outside the workspace')
serveFile(req, res, full)
@@ -131,6 +133,6 @@ try {
setInterval(publish, 1500).unref()
}
// While a render is running, time alone changes the answer (a render that stops writing is stalled).
-setInterval(() => { if (current.live.some((l) => l.state === 'rendering' || Date.now() - (l.updatedAtMs ?? 0) < STALL_MS + 5000)) publish() }, 2000).unref()
+setInterval(() => { if (current.live.some((l) => l.state === 'rendering' || Date.now() - l.updatedAtMs < STALL_MS + 5000)) publish() }, 2000).unref()
setInterval(() => { for (const c of clients) c.write(': ping\n\n') }, 20_000).unref()
for (const signal of ['SIGTERM', 'SIGINT']) process.on(signal, () => { for (const c of clients) c.end(); server.close(); process.exit(0) })