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.

model-viewer: survive an unparseable request target, rescan on nameless changes, exit cleanly on SIGTERM

dee28a2f48349e0
diff --git a/store/viewers/model-viewer/test/helpers.mjs b/store/viewers/model-viewer/test/helpers.mjs
new file mode 100644
index 000000000..b76a6b01f
--- /dev/null
+++ b/store/viewers/model-viewer/test/helpers.mjs
@@ -0,0 +1,133 @@
+// What the tests share: the server started the way Harness starts it, raw HTTP (fetch would tidy the
+// paths a sandbox test needs to send as they are), and a reader for the server-sent events.
+import assert from 'node:assert/strict'
+import { spawn } from 'node:child_process'
+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
+import { request } from 'node:http'
+import { connect, createServer } from 'node:net'
+import { tmpdir } from 'node:os'
+import { dirname, join } from 'node:path'
+import { fileURLToPath, pathToFileURL } from 'node:url'
+
+export const here = dirname(fileURLToPath(import.meta.url))
+export const packageDir = join(here, '..')
+const preload = pathToFileURL(join(here, 'preload.mjs')).href
+
+export const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
+
+export function freePort() {
+ return new Promise((resolve) => {
+ const probe = createServer().listen(0, '127.0.0.1', () => { const { port } = probe.address(); probe.close(() => resolve(port)) })
+ })
+}
+
+/** A scratch workspace; `put` writes a file (making its folders), `done` removes it all. */
+export function scratch(prefix = 'model-viewer-test-') {
+ const dir = mkdtempSync(join(tmpdir(), prefix))
+ const put = (rel, body = '') => { mkdirSync(dirname(join(dir, rel)), { recursive: true }); writeFileSync(join(dir, rel), body) }
+ return { dir, put, done: () => rmSync(dir, { recursive: true, force: true }) }
+}
+
+/**
+ * viewer.mjs on `workspace`, with test/preload.mjs loaded. `env` is merged into this process's
+ * environment (so NODE_V8_COVERAGE reaches the child); a key set to undefined is removed.
+ * `stop()` sends SIGTERM and waits for the exit, which is when V8 writes the child's coverage.
+ */
+export async function startViewer({ workspace, env = {}, cwd } = {}) {
+ const port = await freePort()
+ const merged = { ...process.env, HARNESS_VIEWER_PORT: String(port), HARNESS_WORKSPACE: workspace, ...env }
+ for (const [key, value] of Object.entries(merged)) if (value === undefined) delete merged[key]
+ const child = spawn(process.execPath, ['--import', preload, join(packageDir, 'viewer.mjs')], { env: merged, cwd, stdio: ['ignore', 'pipe', 'pipe'] })
+ let stdout = '', stderr = ''
+ child.stderr.on('data', (d) => { stderr += d })
+ const exited = new Promise((resolve) => child.on('exit', (code, signal) => resolve({ code, signal })))
+ await new Promise((resolve, reject) => {
+ const timer = setTimeout(() => reject(new Error(`the viewer did not start: ${stderr}`)), 15_000)
+ child.stdout.on('data', (d) => { stdout += d; if (stdout.includes('listening')) { clearTimeout(timer); resolve() } })
+ exited.then(({ code }) => { clearTimeout(timer); reject(new Error(`the viewer exited ${code}: ${stderr}`)) })
+ })
+ return {
+ port,
+ base: `http://127.0.0.1:${port}`,
+ child,
+ exited,
+ output: () => ({ stdout, stderr }),
+ async stop() {
+ if (child.exitCode === null && child.signalCode === null) child.kill('SIGTERM')
+ return exited
+ },
+ }
+}
+
+/** One request with the path exactly as given. Resolves { status, headers, body } or { error }. */
+export function raw(port, path, { method = 'GET', headers = {} } = {}) {
+ return new Promise((resolve) => {
+ 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', () => resolve({ status: res.statusCode, headers: res.headers, body: Buffer.concat(chunks) }))
+ })
+ req.on('error', (error) => resolve({ error: error.code ?? error.message }))
+ req.end()
+ })
+}
+
+/** Bytes straight onto the socket: what came back, up to the first line. */
+export function socket(port, text) {
+ return new Promise((resolve) => {
+ const s = connect(port, '127.0.0.1', () => s.write(text))
+ let out = ''
+ s.on('data', (d) => { out += d; if (out.includes('\r\n\r\n')) s.end() })
+ s.on('error', () => {})
+ s.on('close', () => resolve(out.split('\r\n')[0]))
+ })
+}
+
+/** The SSE stream at `path`: every event as { event, data, comment }, and `until` to wait for one. */
+export function events(port, path = '/events') {
+ const list = []
+ const wake = new Set()
+ let buffer = ''
+ let ended = false
+ const req = request({ host: '127.0.0.1', port, path }, (res) => {
+ res.setEncoding('utf8')
+ res.on('data', (chunk) => {
+ buffer += chunk
+ let cut
+ while ((cut = buffer.indexOf('\n\n')) >= 0) {
+ const block = buffer.slice(0, cut)
+ buffer = buffer.slice(cut + 2)
+ const item = { event: null, data: null, comment: null }
+ for (const line of block.split('\n')) {
+ if (line.startsWith(':')) item.comment = line.slice(1).trim()
+ else if (line.startsWith('event: ')) item.event = line.slice(7)
+ else if (line.startsWith('data: ')) item.data = JSON.parse(line.slice(6))
+ }
+ list.push(item)
+ }
+ for (const fn of wake) fn()
+ })
+ res.on('end', () => { ended = true; for (const fn of wake) fn() })
+ })
+ req.on('error', () => { ended = true; for (const fn of wake) fn() })
+ req.end()
+ return {
+ list,
+ get ended() { return ended },
+ /** The first item at index >= `from` passing `predicate`; fails after `ms`. */
+ async until(predicate, { from = 0, ms = 10_000, what = 'an event' } = {}) {
+ const deadline = Date.now() + ms
+ for (;;) {
+ const index = list.findIndex((item, i) => i >= from && predicate(item))
+ if (index >= 0) return { item: list[index], index }
+ const left = deadline - Date.now()
+ assert.ok(left > 0, `timed out waiting for ${what}`)
+ await new Promise((resolve) => { const done = () => { wake.delete(done); clearTimeout(t); resolve() }; const t = setTimeout(done, left); wake.add(done) })
+ }
+ },
+ close() { req.destroy() },
+ }
+}
+
+/** Is a state event (`item.event === 'state'`) whose data passes `predicate`. */
+export const stateWhere = (predicate) => (item) => item.event === 'state' && predicate(item.data)
diff --git a/store/viewers/model-viewer/test/live.test.mjs b/store/viewers/model-viewer/test/live.test.mjs
new file mode 100644
index 000000000..d2e1538f1
--- /dev/null
+++ b/store/viewers/model-viewer/test/live.test.mjs
@@ -0,0 +1,150 @@
+// The live feed: server-sent `state` events while the agent works. A new export, a verdict, a build
+// feed and its pid, frames of a render in progress, a model still being written, a workspace that
+// fs.watch cannot watch (or names no files for), the ping, and a clean end on SIGTERM.
+//
+// npm test
+import assert from 'node:assert/strict'
+import { spawn } from 'node:child_process'
+import { appendFileSync, mkdirSync, writeFileSync } from 'node:fs'
+import { join } from 'node:path'
+import { test } from 'node:test'
+import { events, scratch, sleep, startViewer, stateWhere } from './helpers.mjs'
+
+const paths = (s) => s.models.map((m) => m.path)
+
+test('a new export, a verdict and a build feed arrive as state events; the rest of .harness, ignored trees and no-op changes do not', async () => {
+ const ws = scratch()
+ // the ping every 150 ms instead of 20 s; the build re-check every 60 ms instead of 1.5 s
+ const viewer = await startViewer({ workspace: ws.dir, env: { TEST_TIMERS: '20000=150,1500=60' } })
+ const helper = spawn(process.execPath, ['-e', 'setInterval(() => {}, 1000)'], { stdio: 'ignore' })
+ try {
+ await sleep(200) // the re-check runs with nobody listening, and does nothing
+ const feed = events(viewer.port)
+ const first = await feed.until(stateWhere(() => true), { what: 'the first state' })
+ assert.equal(first.index, 0, 'the current state comes first')
+ assert.deepEqual(paths(first.item.data), [])
+ await feed.until((item) => item.comment === 'ping', { what: 'a ping' })
+
+ ws.put('out/part.glb', 'glTF')
+ let seen = await feed.until(stateWhere((s) => paths(s).includes('out/part.glb')), { what: 'the new export' })
+
+ // nothing the pane shows changes: no event
+ ws.put('notes.txt', 'x')
+ ws.put('node_modules/pkg/index.js', 'x')
+ ws.put('.git/HEAD', 'ref')
+ ws.put('src/__pycache__/m.pyc', 'x')
+ ws.put('.harness/log.txt', 'x')
+ await sleep(900)
+ assert.equal(feed.list.slice(seen.index + 1).filter((item) => item.event === 'state').length, 0)
+
+ ws.put('.harness/verdict.json', JSON.stringify({ spec: 1, ready: false, summary: '1 error' }))
+ seen = await feed.until(stateWhere((s) => s.verdict?.summary === '1 error'), { from: seen.index + 1, what: 'the verdict' })
+
+ ws.put('.harness/build.json', JSON.stringify({ state: 'building', step: 'Export', pid: helper.pid }))
+ seen = await feed.until(stateWhere((s) => s.build?.state === 'building' && s.build.alive === true), { from: seen.index + 1, what: 'the build feed' })
+ // the script dies without a word: the re-check alone notices
+ helper.kill()
+ seen = await feed.until(stateWhere((s) => s.build?.state === 'stopped' && s.build.alive === false), { from: seen.index + 1, what: 'the build reported stopped' })
+
+ ws.put('.harness/build.json', JSON.stringify({ state: 'done', step: 'Export' }))
+ seen = await feed.until(stateWhere((s) => s.build?.state === 'done'), { from: seen.index + 1, what: 'the build done' })
+
+ feed.close()
+ await sleep(200) // the ping and the re-check run with the client gone
+ assert.equal(viewer.child.exitCode, null)
+ } finally {
+ helper.kill()
+ await viewer.stop()
+ ws.done()
+ }
+})
+
+test('frames written into a *-frames folder read as a render in progress, which ends a few seconds after the last frame', async () => {
+ const ws = scratch()
+ mkdirSync(join(ws.dir, 'renders'))
+ const viewer = await startViewer({ workspace: ws.dir })
+ try {
+ const feed = events(viewer.port)
+ await feed.until(stateWhere(() => true))
+ ws.put('renders/spin-frames/0001.png', 'frame')
+ const started = await feed.until(stateWhere((s) => s.inferred !== null), { what: 'the inferred build' })
+ assert.deepEqual(started.item.data.inferred, { state: 'building', step: 'Rendering frames', dir: 'renders/spin-frames' })
+ ws.put('renders/spin-frames/0002.png', 'frame')
+ ws.put('renders/spin-frames/0003.png', 'frame')
+ const ended = await feed.until(stateWhere((s) => s.inferred === null), { from: started.index + 1, ms: 12_000, what: 'the inferred build to end' })
+ assert.deepEqual(ended.item.data.stills, [], 'frames are not stills')
+ const lastFrame = Math.max(...feed.list.slice(0, ended.index).filter((i) => i.event === 'state' && i.data.inferred).map((i) => i.data.now))
+ assert.ok(ended.item.data.now - lastFrame >= 3000, 'the render counts as running for about four seconds after its last frame')
+ } finally {
+ await viewer.stop()
+ ws.done()
+ }
+})
+
+test('a model still being written is announced only once it stops growing', async () => {
+ const ws = scratch()
+ mkdirSync(join(ws.dir, 'out'))
+ // the two looks at the sizes 1.2 s apart instead of 200 ms, so a busy machine cannot slip a look
+ // between two appends
+ const viewer = await startViewer({ workspace: ws.dir, env: { TEST_TIMERS: '200=1200' } })
+ try {
+ const feed = events(viewer.port)
+ await feed.until(stateWhere(() => true))
+ const chunk = Buffer.alloc(1000, 1)
+ writeFileSync(join(ws.dir, 'out/big.glb'), chunk)
+ for (let i = 0; i < 3; i++) { await sleep(500); appendFileSync(join(ws.dir, 'out/big.glb'), chunk) }
+ const announced = await feed.until(stateWhere((s) => paths(s).includes('out/big.glb')), { ms: 15_000, what: 'the model' })
+ assert.equal(announced.item.data.models[0].size, 4000, 'never half a GLB')
+ } finally {
+ await viewer.stop()
+ ws.done()
+ }
+})
+
+test('a workspace fs.watch cannot watch is polled: here, one that does not exist yet', async () => {
+ const ws = scratch()
+ const workspace = join(ws.dir, 'later')
+ const viewer = await startViewer({ workspace, env: { TEST_TIMERS: '2000=100' } })
+ try {
+ assert.match(viewer.output().stdout, /watch failed: .*; polling/)
+ const feed = events(viewer.port)
+ const first = await feed.until(stateWhere(() => true))
+ assert.deepEqual(paths(first.item.data), [])
+ mkdirSync(join(workspace, 'out'), { recursive: true })
+ writeFileSync(join(workspace, 'out/first.glb'), 'glTF')
+ await feed.until(stateWhere((s) => paths(s).includes('out/first.glb')), { what: 'the polled export' })
+ } finally {
+ await viewer.stop()
+ ws.done()
+ }
+})
+
+test('a change fs.watch reports without a file name still makes the pane look again', async () => {
+ const ws = scratch()
+ // fs.watch replaced: nameless changes only, as some platforms report them
+ const viewer = await startViewer({ workspace: ws.dir, env: { TEST_WATCH: 'nameless' } })
+ try {
+ const feed = events(viewer.port)
+ await feed.until(stateWhere(() => true))
+ ws.put('out/named-nowhere.glb', 'glTF')
+ await feed.until(stateWhere((s) => paths(s).includes('out/named-nowhere.glb')), { what: 'the export seen through a nameless change' })
+ } finally {
+ await viewer.stop()
+ ws.done()
+ }
+})
+
+test('SIGTERM ends every event stream and exits 0', async () => {
+ const ws = scratch()
+ const viewer = await startViewer({ workspace: ws.dir })
+ try {
+ const feed = events(viewer.port)
+ await feed.until(stateWhere(() => true))
+ assert.deepEqual(await viewer.stop(), { code: 0, signal: null })
+ const deadline = Date.now() + 5000
+ while (!feed.ended && Date.now() < deadline) await sleep(20)
+ assert.ok(feed.ended, 'the stream was ended by the server')
+ } finally {
+ ws.done()
+ }
+})
diff --git a/store/viewers/model-viewer/test/preload.mjs b/store/viewers/model-viewer/test/preload.mjs
new file mode 100644
index 000000000..f5379edfd
--- /dev/null
+++ b/store/viewers/model-viewer/test/preload.mjs
@@ -0,0 +1,28 @@
+// Loaded into the viewer under test with --import, never in production. It lets a test see what
+// otherwise takes a clock or an operating system to happen:
+//
+// TEST_TIMERS="20000=60,1500=40" setTimeout / setInterval delays, mapped exactly (others unchanged)
+// TEST_WATCH=nameless fs.watch reports changes without a file name, as it may on some
+// platforms, and nothing else
+import { EventEmitter } from 'node:events'
+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)))
+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 === 'nameless') {
+ fs.watch = (_path, _options, listener) => {
+ const watcher = new EventEmitter()
+ watcher.on('change', listener)
+ const tick = setInterval(() => watcher.emit('change', 'rename', null), 500) // slower than the settle, or it never settles
+
+ tick.unref()
+ watcher.close = () => clearInterval(tick)
+ return watcher
+ }
+ syncBuiltinESMExports()
+}
diff --git a/store/viewers/model-viewer/test/server.test.mjs b/store/viewers/model-viewer/test/server.test.mjs
new file mode 100644
index 000000000..461475297
--- /dev/null
+++ b/store/viewers/model-viewer/test/server.test.mjs
@@ -0,0 +1,225 @@
+// The server over HTTP, on scratch workspaces: what /api/state finds and skips, the build feed and
+// its pid, every route, byte ranges, and the sandbox around the workspace, the page and three.js.
+//
+// npm test
+import assert from 'node:assert/strict'
+import { execFileSync, spawn } from 'node:child_process'
+import { mkdirSync, symlinkSync, utimesSync } from 'node:fs'
+import { basename, join } from 'node:path'
+import { after, before, describe, test } from 'node:test'
+import { raw, scratch, socket, startViewer } from './helpers.mjs'
+
+const bytes = Buffer.from(Array.from({ length: 4096 }, (_, i) => i % 256))
+
+describe('a workspace, scanned', () => {
+ let ws, viewer
+ const at = (rel, seconds) => utimesSync(join(ws.dir, rel), 1_800_000_000 + seconds, 1_800_000_000 + seconds)
+ before(async () => {
+ ws = scratch()
+ ws.put('out/old.glb', 'glTF'); at('out/old.glb', 1)
+ ws.put('out/model.gltf', '{}'); at('out/model.gltf', 5)
+ ws.put('out/report.json', JSON.stringify({ size_mm: [2, 0, 2] }))
+ ws.put('renders/turntable.mp4', bytes); at('renders/turntable.mp4', 3)
+ ws.put('renders/report.json', '{}')
+ ws.put('renders/clip.WEBM', 'webm'); at('renders/clip.WEBM', 4)
+ ws.put('renders/shot.PNG', 'png'); at('renders/shot.PNG', 2)
+ ws.put('renders/spin-frames/0001.png', 'frame')
+ ws.put('node_modules/pkg/x.glb', 'skipped')
+ ws.put('textures/wood.jpg', 'skipped')
+ ws.put('.hidden/y.glb', 'dot dir')
+ ws.put('.dot.glb', 'dot file')
+ ws.put('notes.txt', 'not a model')
+ ws.put('a/b/c/d/e/f/six.glb', 'deep enough'); at('a/b/c/d/e/f/six.glb', 0)
+ ws.put('a/b/c/d/e/f/g/seven.glb', 'too deep')
+ ws.put('.harness/verdict.json', JSON.stringify({ spec: 1, ready: true, summary: 'ok' }))
+ symlinkSync(join(ws.dir, 'gone.glb'), join(ws.dir, 'dangling.glb'))
+ execFileSync('mkfifo', [join(ws.dir, 'pipe.glb')])
+ viewer = await startViewer({ workspace: ws.dir })
+ })
+ after(async () => { await viewer?.stop(); ws.done() })
+
+ const state = async () => (await fetch(`${viewer.base}/api/state`)).json()
+
+ test('models, videos and stills newest first, with the report beside a model; skipped trees, dotfiles, depth > 6 and non-files left out', async () => {
+ const response = await fetch(`${viewer.base}/api/state`)
+ assert.equal(response.headers.get('cache-control'), 'no-store')
+ const s = await response.json()
+ assert.equal(s.workspace, basename(ws.dir))
+ assert.deepEqual(s.models.map((m) => m.path), ['out/model.gltf', 'out/old.glb', 'a/b/c/d/e/f/six.glb'])
+ assert.equal(s.models[0].report, 'out/report.json')
+ assert.equal(s.models[0].size, 2)
+ assert.equal(s.models[0].mtime, 1_800_000_005_000)
+ assert.equal(s.models[2].report, undefined)
+ assert.deepEqual(s.videos.map((v) => v.path), ['renders/clip.WEBM', 'renders/turntable.mp4'])
+ assert.equal(s.videos[1].report, undefined, 'a report is only looked for beside a model')
+ assert.deepEqual(s.stills.map((v) => v.path), ['renders/shot.PNG'], 'frames of a render in progress are not stills')
+ assert.deepEqual(s.verdict, { spec: 1, ready: true, summary: 'ok' })
+ assert.equal(s.build, null)
+ assert.equal(s.inferred, null)
+ assert.ok(Math.abs(s.now - Date.now()) < 60_000)
+ })
+
+ test('the build feed: a building pid that is gone reads as stopped; a live, foreign or missing pid still builds', async () => {
+ const helper = spawn(process.execPath, ['-e', 'setInterval(() => {}, 1000)'], { stdio: 'ignore' })
+ const dead = spawn(process.execPath, ['-e', ''], { stdio: 'ignore' })
+ await new Promise((resolve) => dead.on('exit', resolve))
+ try {
+ const feed = async (body) => { ws.put('.harness/build.json', JSON.stringify(body)); return (await state()).build }
+ assert.deepEqual(await feed({ state: 'building', step: 'Export', pid: helper.pid }), { state: 'building', step: 'Export', pid: helper.pid, alive: true })
+ assert.deepEqual(await feed({ state: 'building', pid: dead.pid }), { state: 'stopped', pid: dead.pid, alive: false })
+ // pid 1 belongs to another user: kill(1, 0) is EPERM, which still means alive
+ assert.equal((await feed({ state: 'building', pid: 1 })).state, 'building')
+ assert.deepEqual(await feed({ state: 'building' }), { state: 'building', alive: true }, 'no pid: nothing to check it against')
+ assert.deepEqual(await feed({ state: 'building', pid: 'abc' }), { state: 'building', pid: 'abc', alive: true })
+ assert.deepEqual(await feed({ state: 'building', pid: -4 }), { state: 'building', pid: -4, alive: true })
+ assert.deepEqual(await feed({ state: 'done', pid: dead.pid }), { state: 'done', pid: dead.pid, alive: null }, 'only a building feed is checked')
+ assert.equal(await feed('building'), 'building', 'a feed that is not an object is passed on as it is')
+ ws.put('.harness/build.json', '{ half written')
+ assert.equal((await state()).build, null)
+ } finally {
+ helper.kill()
+ }
+ })
+
+ test('the page, its modules and three.js, with their caching', async () => {
+ for (const path of ['/', '/index.html']) {
+ const page = await fetch(viewer.base + path)
+ assert.equal(page.status, 200)
+ assert.equal(page.headers.get('content-type'), 'text/html; charset=utf-8')
+ assert.equal(page.headers.get('cache-control'), 'no-store')
+ assert.match(await page.text(), /importmap/)
+ }
+ const app = await fetch(`${viewer.base}/app/app.js`)
+ assert.equal(app.status, 200)
+ assert.equal(app.headers.get('content-type'), 'text/javascript; charset=utf-8')
+ assert.equal(app.headers.get('cache-control'), 'no-cache')
+ assert.equal((await fetch(`${viewer.base}/app/style.css`)).headers.get('content-type'), 'text/css; charset=utf-8')
+ for (const path of ['/vendor/three/build/three.module.js', '/vendor/three/examples/jsm/loaders/GLTFLoader.js']) {
+ const r = await fetch(viewer.base + path)
+ assert.equal(r.status, 200, path)
+ assert.equal(r.headers.get('cache-control'), 'max-age=86400')
+ }
+ assert.equal((await fetch(`${viewer.base}/favicon.ico`)).status, 204)
+ })
+
+ test('workspace files under /ws/ and at the root, by type; folders and missing files are not found', async () => {
+ const model = await fetch(`${viewer.base}/ws/out/model.gltf`)
+ assert.equal(model.status, 200)
+ assert.equal(model.headers.get('content-type'), 'model/gltf+json')
+ assert.equal(model.headers.get('cache-control'), 'no-store')
+ assert.equal(model.headers.get('accept-ranges'), 'bytes')
+ assert.equal(model.headers.get('content-length'), '2')
+ assert.equal(await model.text(), '{}')
+ assert.equal((await fetch(`${viewer.base}/ws/renders/shot.PNG`)).headers.get('content-type'), 'image/png', 'extensions in any case')
+ assert.equal((await fetch(`${viewer.base}/ws/notes.txt`)).headers.get('content-type'), 'application/octet-stream')
+ const legacy = await fetch(`${viewer.base}/out/model.gltf`)
+ assert.equal(legacy.status, 200, 'the first version fetched workspace files at the root')
+ assert.equal(await legacy.text(), '{}')
+ for (const path of ['/ws/out', '/ws/', '/ws/out/missing.glb', '/app/', '/app/missing.js', '/vendor/three/build/', '/missing.glb', '/renders']) {
+ const r = await raw(viewer.port, path)
+ assert.equal(r.status, 404, path)
+ }
+ const head = await raw(viewer.port, '/ws/renders/turntable.mp4', { method: 'HEAD' })
+ assert.equal(head.status, 200)
+ assert.equal(head.headers['content-length'], '4096')
+ assert.equal(head.body.length, 0)
+ })
+
+ test('byte ranges: a span, an open end, a suffix, clamped ends, unsatisfiable ones, and what is not a range', async () => {
+ const get = (range, method) => raw(viewer.port, '/ws/renders/turntable.mp4', { method, headers: { range } })
+ const cases = [
+ ['bytes=10-19', 10, 19], ['bytes=4090-', 4090, 4095], ['bytes=-6', 4090, 4095],
+ ['bytes=-99999', 0, 4095], ['bytes=100-99999', 100, 4095], ['bytes=0-0', 0, 0],
+ ]
+ for (const [range, start, end] of cases) {
+ const r = await get(range)
+ assert.equal(r.status, 206, range)
+ assert.equal(r.headers['content-range'], `bytes ${start}-${end}/4096`, range)
+ assert.equal(r.headers['content-length'], String(end - start + 1), range)
+ assert.equal(r.headers['content-type'], 'video/mp4')
+ assert.deepEqual(r.body, bytes.subarray(start, end + 1), range)
+ }
+ for (const range of ['bytes=20-10', 'bytes=4096-', 'bytes=5000-6000', 'bytes=-0']) {
+ const r = await get(range)
+ assert.equal(r.status, 416, range)
+ assert.equal(r.headers['content-range'], 'bytes */4096', range)
+ assert.equal(r.body.length, 0)
+ }
+ for (const range of ['bytes=-', 'bytes=0-1,4-5', 'items=0-1']) {
+ const r = await get(range)
+ assert.equal(r.status, 200, `${range} is not a range this server serves: the whole file`)
+ assert.equal(r.body.length, 4096)
+ }
+ const head = await get('bytes=10-19', 'HEAD')
+ assert.equal(head.status, 206)
+ assert.equal(head.headers['content-length'], '10')
+ assert.equal(head.body.length, 0)
+ })
+
+ test('nothing outside the workspace, the page folder or three\'s build and examples', async () => {
+ const refused = {
+ '/ws/..%2F..%2F..%2Fetc%2Fhosts': 403,
+ '/ws/out/..%2F..%2Fviewer.mjs': 403,
+ '/app/..%2Fviewer.mjs': 403,
+ '/app/..%2F..%2Fpackage.json': 403,
+ '/vendor/three/package.json': 403,
+ '/vendor/three/src/Three.js': 403,
+ '/vendor/three/build/..%2F..%2F..%2Fviewer.mjs': 403,
+ '/..%2F..%2F..%2Fetc%2Fhosts': 404,
+ '/ws/../../../etc/hosts': 404,
+ '/ws/%2e%2e/%2e%2e/%2e%2e/etc/hosts': 404,
+ '/app/../viewer.mjs': 404,
+ }
+ for (const [path, status] of Object.entries(refused)) {
+ const r = await raw(viewer.port, path)
+ assert.equal(r.status, status, path)
+ assert.doesNotMatch(r.body.toString(), /localhost|createServer/, path)
+ }
+ })
+
+ test('a malformed path is a bad request, and an unparseable request target does not take the server down', async () => {
+ assert.equal((await raw(viewer.port, '/ws/%zz')).status, 400)
+ assert.equal(await socket(viewer.port, 'GET http://a:99999/ HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n'), 'HTTP/1.1 400 Bad Request')
+ assert.equal((await fetch(`${viewer.base}/api/state`)).status, 200, 'still serving')
+ assert.equal(viewer.child.exitCode, null)
+ })
+})
+
+test('without HARNESS_WORKSPACE the workspace is the directory the viewer starts in', async () => {
+ const ws = scratch()
+ ws.put('here.glb', 'glTF')
+ const viewer = await startViewer({ workspace: undefined, cwd: ws.dir })
+ try {
+ const s = await (await fetch(`${viewer.base}/api/state`)).json()
+ assert.equal(s.workspace, basename(ws.dir))
+ assert.deepEqual(s.models.map((m) => m.path), ['here.glb'])
+ } finally {
+ await viewer.stop()
+ ws.done()
+ }
+})
+
+test('a workspace larger than the scan budget still answers: the walk stops at 20 000 entries', async () => {
+ const ws = scratch()
+ for (let i = 0; i < 20_002; i++) mkdirSync(join(ws.dir, `d${i}`))
+ const viewer = await startViewer({ workspace: ws.dir })
+ try {
+ const response = await fetch(`${viewer.base}/api/state`)
+ assert.equal(response.status, 200)
+ const s = await response.json()
+ assert.deepEqual([s.models, s.videos, s.stills], [[], [], []])
+ } finally {
+ await viewer.stop()
+ ws.done()
+ }
+})
+
+test('SIGTERM ends the server cleanly', async () => {
+ const ws = scratch()
+ const viewer = await startViewer({ workspace: ws.dir })
+ try {
+ assert.deepEqual(await viewer.stop(), { code: 0, signal: null })
+ } finally {
+ ws.done()
+ }
+})
diff --git a/store/viewers/model-viewer/viewer.mjs b/store/viewers/model-viewer/viewer.mjs
index 617bb28a6..a417754b3 100644
--- a/store/viewers/model-viewer/viewer.mjs
+++ b/store/viewers/model-viewer/viewer.mjs
@@ -122,9 +122,9 @@ function sendFile(req, res, full, cache) {
const clients = new Set()
const server = createServer((req, res) => {
- const url = new URL(req.url ?? '/', `http://127.0.0.1:${port}`)
+ // An absolute-form target (`GET http://a:99999/`) makes URL throw, a bad escape decodeURIComponent.
let path
- try { path = decodeURIComponent(url.pathname) } catch { res.writeHead(400); res.end(); return }
+ try { path = decodeURIComponent(new URL(req.url, `http://127.0.0.1:${port}`).pathname) } catch { res.writeHead(400); res.end(); return }
if (path === '/' || path === '/index.html') return sendFile(req, res, join(WEB, 'index.html'), 'no-store')
if (path.startsWith('/app/')) {
const full = safe(WEB, path.slice(5))
@@ -164,10 +164,10 @@ server.listen(port, '127.0.0.1', () => console.log(`[model-viewer] listening on
// Live: watch, wait for writes to settle, broadcast
let last = ''
-function broadcast(force = false) {
+function broadcast() {
const snapshot = state()
const key = JSON.stringify({ ...snapshot, now: 0 })
- if (!force && key === last) return
+ if (key === last) return
last = key
const message = `event: state\ndata: ${JSON.stringify(snapshot)}\n\n`
for (const client of clients) client.write(message)
@@ -191,8 +191,9 @@ let framesTimer = null
let framesExpiry = null
try {
watch(workspace, { recursive: true }, (_event, name) => {
+ // No name (fs.watch does not promise one): something changed, so look again.
const n = String(name ?? '').split(sep).join('/')
- if (!n || n.includes('node_modules/') || n.startsWith('.git/') || n.includes('__pycache__')) return
+ if (n.includes('node_modules/') || n.startsWith('.git/') || n.includes('__pycache__')) return
if (/-frames\//.test(n)) {
frames = { at: Date.now(), dir: n.slice(0, n.indexOf('-frames/') + 7) }
if (!framesTimer) { broadcast(); framesTimer = setTimeout(() => { framesTimer = null; broadcast() }, 1000) }
@@ -214,3 +215,4 @@ setInterval(() => {
if (build?.state === 'building') broadcast()
}, 1500).unref()
setInterval(() => { for (const client of clients) client.write(': ping\n\n') }, 20_000).unref()
+for (const signal of ['SIGTERM', 'SIGINT']) process.on(signal, () => { for (const client of clients) client.end(); server.close(); process.exit(0) })