rushes 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/marketplace.json +14 -0
- package/.claude-plugin/plugin.json +10 -0
- package/.mcp.json +8 -0
- package/AGENTS.md +110 -0
- package/LICENSE +21 -0
- package/README.md +204 -0
- package/dist/cli/demo.js +339 -0
- package/dist/cli/doctor.js +307 -0
- package/dist/cli/index.js +11 -0
- package/dist/cli/main.js +469 -0
- package/dist/core/batches.js +58 -0
- package/dist/core/errors.js +37 -0
- package/dist/core/exportNotes.js +132 -0
- package/dist/core/ids.js +42 -0
- package/dist/core/media.js +128 -0
- package/dist/core/notes.js +117 -0
- package/dist/core/paths.js +17 -0
- package/dist/core/project.js +200 -0
- package/dist/core/schema.js +220 -0
- package/dist/core/script.js +113 -0
- package/dist/core/store.js +132 -0
- package/dist/core/tabs.js +24 -0
- package/dist/mcp/client.js +50 -0
- package/dist/mcp/ensure.js +69 -0
- package/dist/mcp/stdio.js +47 -0
- package/dist/mcp/tools.js +191 -0
- package/dist/server/app.js +710 -0
- package/dist/server/assets.js +234 -0
- package/dist/server/files.js +170 -0
- package/dist/server/lock.js +148 -0
- package/dist/server/loudness.js +331 -0
- package/dist/server/proxy.js +575 -0
- package/dist/server/reveal.js +71 -0
- package/dist/server/start.js +125 -0
- package/dist/server/watch.js +133 -0
- package/dist/setup/env.js +42 -0
- package/dist/setup/harnesses.js +93 -0
- package/dist/setup/setup.js +92 -0
- package/package.json +54 -0
- package/skills/rushes/SKILL.md +30 -0
- package/web-dist/assets/figtree-latin-400-normal-BD4aNku5.woff +0 -0
- package/web-dist/assets/figtree-latin-400-normal-g7Dtegnw.woff2 +0 -0
- package/web-dist/assets/figtree-latin-500-normal-BWnGEVsr.woff2 +0 -0
- package/web-dist/assets/figtree-latin-500-normal-B_yy1phn.woff +0 -0
- package/web-dist/assets/figtree-latin-600-normal-BM_oTo4n.woff +0 -0
- package/web-dist/assets/figtree-latin-600-normal-Cv_xCTDl.woff2 +0 -0
- package/web-dist/assets/figtree-latin-700-normal-BCCt2T1q.woff +0 -0
- package/web-dist/assets/figtree-latin-700-normal-th6qEP7c.woff2 +0 -0
- package/web-dist/assets/figtree-latin-ext-400-normal-CXAzuTZb.woff2 +0 -0
- package/web-dist/assets/figtree-latin-ext-400-normal-Gbh-3PTk.woff +0 -0
- package/web-dist/assets/figtree-latin-ext-500-normal-BOEJvrb_.woff +0 -0
- package/web-dist/assets/figtree-latin-ext-500-normal-Du0rIuHj.woff2 +0 -0
- package/web-dist/assets/figtree-latin-ext-600-normal-CQqsimlV.woff +0 -0
- package/web-dist/assets/figtree-latin-ext-600-normal-DJlfifZj.woff2 +0 -0
- package/web-dist/assets/figtree-latin-ext-700-normal-BoDi4KL2.woff +0 -0
- package/web-dist/assets/figtree-latin-ext-700-normal-CS4sQkGV.woff2 +0 -0
- package/web-dist/assets/index-BjDIT_wL.css +1 -0
- package/web-dist/assets/index-DF6Rl-Ac.js +3 -0
- package/web-dist/assets/jetbrains-mono-cyrillic-400-normal-BEIGL1Tu.woff2 +0 -0
- package/web-dist/assets/jetbrains-mono-cyrillic-400-normal-ugxPyKxw.woff +0 -0
- package/web-dist/assets/jetbrains-mono-cyrillic-500-normal-DJqRU3vO.woff +0 -0
- package/web-dist/assets/jetbrains-mono-cyrillic-500-normal-DmUKJPL_.woff2 +0 -0
- package/web-dist/assets/jetbrains-mono-greek-400-normal-B9oWc5Lo.woff +0 -0
- package/web-dist/assets/jetbrains-mono-greek-400-normal-C190GLew.woff2 +0 -0
- package/web-dist/assets/jetbrains-mono-greek-500-normal-D7SFKleX.woff +0 -0
- package/web-dist/assets/jetbrains-mono-greek-500-normal-JpySY46c.woff2 +0 -0
- package/web-dist/assets/jetbrains-mono-latin-400-normal-6-qcROiO.woff +0 -0
- package/web-dist/assets/jetbrains-mono-latin-400-normal-V6pRDFza.woff2 +0 -0
- package/web-dist/assets/jetbrains-mono-latin-500-normal-BWZEU5yA.woff2 +0 -0
- package/web-dist/assets/jetbrains-mono-latin-500-normal-CJOVTJB7.woff +0 -0
- package/web-dist/assets/jetbrains-mono-latin-ext-400-normal-Bc8Ftmh3.woff2 +0 -0
- package/web-dist/assets/jetbrains-mono-latin-ext-400-normal-fXTG6kC5.woff +0 -0
- package/web-dist/assets/jetbrains-mono-latin-ext-500-normal-Cut-4mMH.woff2 +0 -0
- package/web-dist/assets/jetbrains-mono-latin-ext-500-normal-ckzbgY84.woff +0 -0
- package/web-dist/assets/jetbrains-mono-vietnamese-400-normal-CqNFfHCs.woff +0 -0
- package/web-dist/assets/jetbrains-mono-vietnamese-500-normal-DNRqzVM1.woff +0 -0
- package/web-dist/index.html +14 -0
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { readdir, stat } from "node:fs/promises";
|
|
2
|
+
import { basename, extname, join } from "node:path";
|
|
3
|
+
import { fromManifestPath } from "../core/paths.js";
|
|
4
|
+
import { GRAB_PATH, SCREENSHOT_PATH, registeredMedia } from "./files.js";
|
|
5
|
+
import { PROXY_PATH } from "./proxy.js";
|
|
6
|
+
/**
|
|
7
|
+
* "hero-60s_v3_00m12.10s_f726.png": the time is frame/fps, rounded to hundredths before
|
|
8
|
+
* splitting into minutes and seconds -- the same rule fmt() uses, so 59.999s carries into the
|
|
9
|
+
* next minute rather than printing 00m60.00s.
|
|
10
|
+
*/
|
|
11
|
+
export function screenshotName(video, version, frame, fps) {
|
|
12
|
+
const t = frame / fps;
|
|
13
|
+
const cs = Math.round(Math.max(0, t) * 100);
|
|
14
|
+
const m = Math.floor(cs / 6000);
|
|
15
|
+
const s = ((cs - m * 6000) / 100).toFixed(2).padStart(5, "0");
|
|
16
|
+
const mm = String(m).padStart(2, "0");
|
|
17
|
+
return `${video}_${version}_${mm}m${s}s_f${frame}.png`;
|
|
18
|
+
}
|
|
19
|
+
/** The fps to use for a grab or screenshot: the version's own, falling back to the project's. */
|
|
20
|
+
export function fpsFor(project, video, version) {
|
|
21
|
+
const v = project.videos.find((x) => x.id === video)?.versions.find((x) => x.id === version);
|
|
22
|
+
return v?.fps ?? project.fps;
|
|
23
|
+
}
|
|
24
|
+
// Parses a name screenshotName() itself produced.
|
|
25
|
+
const NEW_NAME = /^([a-z0-9][a-z0-9-]*)_(v\d+)_(\d{2})m(\d{2}\.\d{2})s_f(\d+)\.png$/;
|
|
26
|
+
// Parses a Plan 1-2 grab name: {video}_{version}_f{frame}.png.
|
|
27
|
+
const OLD_NAME = /^([a-z0-9][a-z0-9-]*)_(v\d+)_f(\d+)\.png$/;
|
|
28
|
+
async function statInfo(abs) {
|
|
29
|
+
try {
|
|
30
|
+
const info = await stat(abs);
|
|
31
|
+
return { size: info.size, modified: info.mtime.toISOString(), mtimeMs: info.mtimeMs, missing: false };
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return { size: null, modified: null, mtimeMs: 0, missing: true };
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/** The two screenshot directories, as (directory, manifest prefix, safe-name regex) triples. */
|
|
38
|
+
const SCREENSHOT_DIRS = [
|
|
39
|
+
["screenshots", "screenshots/", SCREENSHOT_PATH],
|
|
40
|
+
[".rushes/grabs", ".rushes/grabs/", GRAB_PATH],
|
|
41
|
+
];
|
|
42
|
+
/** Names (not full paths) of every *.png directly inside `dir`, never recursing. Empty when the directory doesn't exist. */
|
|
43
|
+
async function pngNames(store, dir) {
|
|
44
|
+
try {
|
|
45
|
+
return (await readdir(join(store.root, dir))).filter((n) => n.toLowerCase().endsWith(".png"));
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
return [];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Every *.png directly inside one screenshots directory, skipping anything whose name `safe`
|
|
53
|
+
* wouldn't let /media serve. Stats every candidate in parallel; the returned order matches the
|
|
54
|
+
* order `names` came in, so the caller's own sort (by modified time) sees a stable input. A name
|
|
55
|
+
* that parses as video_version_time_frame (or the older video_version_frame) carries video,
|
|
56
|
+
* version, frame and t; anything else is still listed, just without those fields.
|
|
57
|
+
*/
|
|
58
|
+
async function scanScreenshotDir(store, names, manifestPrefix, safe, project) {
|
|
59
|
+
const candidates = names.map((name) => ({ name, path: `${manifestPrefix}${name}` })).filter(({ path }) => safe.test(path));
|
|
60
|
+
const results = await Promise.all(candidates.map(async ({ name, path }) => {
|
|
61
|
+
const abs = fromManifestPath(store.root, path);
|
|
62
|
+
const info = await statInfo(abs);
|
|
63
|
+
if (info.missing)
|
|
64
|
+
return null; // vanished between the directory read and the stat
|
|
65
|
+
const asset = { kind: "screenshot", path, abs, name, size: info.size, modified: info.modified, missing: false };
|
|
66
|
+
const newMatch = NEW_NAME.exec(name);
|
|
67
|
+
const oldMatch = newMatch ? null : OLD_NAME.exec(name);
|
|
68
|
+
if (newMatch) {
|
|
69
|
+
asset.video = newMatch[1];
|
|
70
|
+
asset.version = newMatch[2];
|
|
71
|
+
asset.frame = Number(newMatch[5]);
|
|
72
|
+
asset.t = asset.frame / fpsFor(project, asset.video, asset.version);
|
|
73
|
+
}
|
|
74
|
+
else if (oldMatch) {
|
|
75
|
+
asset.video = oldMatch[1];
|
|
76
|
+
asset.version = oldMatch[2];
|
|
77
|
+
asset.frame = Number(oldMatch[3]);
|
|
78
|
+
asset.t = asset.frame / fpsFor(project, asset.video, asset.version);
|
|
79
|
+
}
|
|
80
|
+
return { asset, mtimeMs: info.mtimeMs };
|
|
81
|
+
}));
|
|
82
|
+
return results.filter((x) => x !== null);
|
|
83
|
+
}
|
|
84
|
+
async function fileAsset(store, kind, path, extra) {
|
|
85
|
+
const abs = fromManifestPath(store.root, path);
|
|
86
|
+
const info = await statInfo(abs);
|
|
87
|
+
return { kind, path, abs, name: basename(path), size: info.size, modified: info.modified, missing: info.missing, ...extra };
|
|
88
|
+
}
|
|
89
|
+
async function registeredFileAsset(store, f) {
|
|
90
|
+
const abs = fromManifestPath(store.root, f.file);
|
|
91
|
+
const info = await statInfo(abs);
|
|
92
|
+
// `name` stays the file's own basename -- Download, Save-as and the dashboard's extension
|
|
93
|
+
// checks (isPreviewable, Open) all read it, the same as a take or variant already does -- and
|
|
94
|
+
// `f.name` (the display name the agent or CLI gave it) goes in `label` instead (I2).
|
|
95
|
+
const asset = {
|
|
96
|
+
kind: f.kind, path: f.file, abs, name: basename(f.file), label: f.name, size: info.size, modified: info.modified, missing: info.missing, note: f.note,
|
|
97
|
+
};
|
|
98
|
+
if (f.video)
|
|
99
|
+
asset.video = f.video;
|
|
100
|
+
return asset;
|
|
101
|
+
}
|
|
102
|
+
/** §16.2's new kinds, in the order the library shows them. */
|
|
103
|
+
const LIBRARY_KINDS = ["doc", "image", "caption", "export", "delivery", "edit"];
|
|
104
|
+
const DOC_EXT = new Set(["md", "txt", "pdf"]);
|
|
105
|
+
const CAPTION_EXT = new Set(["srt", "vtt"]);
|
|
106
|
+
/**
|
|
107
|
+
* Names (not full paths) of every regular file directly inside `dir`, optionally filtered to
|
|
108
|
+
* `exts` (lower-case, no dot). Never recurses, never follows a symlink, and skips hidden names,
|
|
109
|
+
* the same cheap, one-level readdir the screenshot scan already does. Empty when `dir` doesn't
|
|
110
|
+
* exist. Sorted by name, so auto-discovered files always list in a stable order.
|
|
111
|
+
*/
|
|
112
|
+
async function dirFileNames(store, dir, exts) {
|
|
113
|
+
let entries;
|
|
114
|
+
try {
|
|
115
|
+
entries = await readdir(join(store.root, dir), { withFileTypes: true });
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
return [];
|
|
119
|
+
}
|
|
120
|
+
const names = [];
|
|
121
|
+
for (const d of entries) {
|
|
122
|
+
if (d.isSymbolicLink() || d.isDirectory() || !d.isFile())
|
|
123
|
+
continue;
|
|
124
|
+
if (d.name.startsWith("."))
|
|
125
|
+
continue;
|
|
126
|
+
if (exts && !exts.has(extname(d.name).toLowerCase().slice(1)))
|
|
127
|
+
continue;
|
|
128
|
+
names.push(d.name);
|
|
129
|
+
}
|
|
130
|
+
return names.sort((a, b) => a.localeCompare(b));
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* §16.2's auto-discovered paths: every *.md/.txt/.pdf directly in the project root (`doc`), every
|
|
134
|
+
* *.srt/.vtt directly in the root (`caption`), and every file directly in `exports/` (`export`).
|
|
135
|
+
* Hidden files, anything inside `.rushes/`, and anything in a deeper sub-folder never appear,
|
|
136
|
+
* because this only ever reads one directory level.
|
|
137
|
+
*/
|
|
138
|
+
async function discoveredLibraryPaths(store) {
|
|
139
|
+
// One readdir of the project root serves both doc and caption (§16.2's two root extension
|
|
140
|
+
// sets), rather than reading the same directory twice over just to apply a different filter.
|
|
141
|
+
const [rootNames, exportNames] = await Promise.all([dirFileNames(store, "."), dirFileNames(store, "exports")]);
|
|
142
|
+
const extOf = (n) => extname(n).toLowerCase().slice(1);
|
|
143
|
+
const doc = rootNames.filter((n) => DOC_EXT.has(extOf(n)));
|
|
144
|
+
const caption = rootNames.filter((n) => CAPTION_EXT.has(extOf(n)));
|
|
145
|
+
return { doc, caption, export: exportNames.map((n) => `exports/${n}`) };
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* The library assets (doc, image, caption, export, delivery, edit), one kind at a time in
|
|
149
|
+
* LIBRARY_KINDS order. Within a kind, registered files come first in manifest order, then
|
|
150
|
+
* auto-discovered files sorted by name; a path that's both registered and auto-discovered is
|
|
151
|
+
* listed once, with the registered metadata (§16.2).
|
|
152
|
+
*/
|
|
153
|
+
async function libraryAssets(store, project) {
|
|
154
|
+
const discovered = await discoveredLibraryPaths(store);
|
|
155
|
+
const autoByKind = { doc: discovered.doc, caption: discovered.caption, export: discovered.export };
|
|
156
|
+
const out = [];
|
|
157
|
+
for (const kind of LIBRARY_KINDS) {
|
|
158
|
+
const registered = project.files.filter((f) => f.kind === kind);
|
|
159
|
+
out.push(...(await Promise.all(registered.map((f) => registeredFileAsset(store, f)))));
|
|
160
|
+
const registeredPaths = new Set(registered.map((f) => f.file));
|
|
161
|
+
const auto = (autoByKind[kind] ?? []).filter((path) => !registeredPaths.has(path));
|
|
162
|
+
out.push(...(await Promise.all(auto.map((path) => fileAsset(store, kind, path, {})))));
|
|
163
|
+
}
|
|
164
|
+
return out;
|
|
165
|
+
}
|
|
166
|
+
function variantEntries(project, stage) {
|
|
167
|
+
return project.lanes
|
|
168
|
+
.filter((lane) => lane.stage === stage)
|
|
169
|
+
.flatMap((lane) => lane.variants.map((variant) => ({
|
|
170
|
+
lane: lane.id,
|
|
171
|
+
laneName: lane.name,
|
|
172
|
+
variant: variant.id,
|
|
173
|
+
label: variant.name,
|
|
174
|
+
meta: Object.keys(variant.meta).length > 0 ? variant.meta : undefined,
|
|
175
|
+
file: variant.file,
|
|
176
|
+
})));
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Every asset in the project: screenshots (newest modified first), then cuts (by video in
|
|
180
|
+
* project order, newest version first), then proxies (§19.5, the same order, only for a cut that
|
|
181
|
+
* has one recorded), then takes, voice, music and sfx, each in manifest
|
|
182
|
+
* order. Callers never need to sort this themselves. Every file this returns has been `stat`ed,
|
|
183
|
+
* in parallel within each group, so a large project doesn't pay for it one file at a time.
|
|
184
|
+
*/
|
|
185
|
+
export async function listAssets(store, project, script) {
|
|
186
|
+
const cutEntries = project.videos.flatMap((video) => [...video.versions].reverse().map((version) => ({ video: video.id, version: version.id, file: version.file })));
|
|
187
|
+
const proxyEntries = project.videos.flatMap((video) =>
|
|
188
|
+
// Only a file this server would have written into proxies/: a hand-edited record pointing
|
|
189
|
+
// anywhere else (the original, say) is never offered for deletion as a proxy.
|
|
190
|
+
[...video.versions].reverse().flatMap((version) => version.proxy && PROXY_PATH.test(version.proxy.file) ? [{ video: video.id, version: version.id, proxy: version.proxy }] : []));
|
|
191
|
+
// A take has no name of its own in script.json (just an id and the text it was read against),
|
|
192
|
+
// so its label is its ordinal within the section -- "Take 1", "Take 2" -- rather than the file
|
|
193
|
+
// name Download/Save-as still need under `name`.
|
|
194
|
+
const takeEntries = script.sections.flatMap((section) => section.takes.map((take, i) => ({ section: section.id, file: take.file, label: `Take ${i + 1}` })));
|
|
195
|
+
const [[freshNames, oldNames], cuts, proxies, takes, voice, music, sfx] = await Promise.all([
|
|
196
|
+
Promise.all([pngNames(store, SCREENSHOT_DIRS[0][0]), pngNames(store, SCREENSHOT_DIRS[1][0])]),
|
|
197
|
+
Promise.all(cutEntries.map((e) => fileAsset(store, "cut", e.file, { video: e.video, version: e.version }))),
|
|
198
|
+
Promise.all(proxyEntries.map((e) => fileAsset(store, "proxy", e.proxy.file, { video: e.video, version: e.version, width: e.proxy.width, height: e.proxy.height }))),
|
|
199
|
+
Promise.all(takeEntries.map((e) => fileAsset(store, "take", e.file, { section: e.section, label: e.label }))),
|
|
200
|
+
Promise.all(variantEntries(project, "voice").map((e) => fileAsset(store, "voice", e.file, { lane: e.lane, laneName: e.laneName, variant: e.variant, label: e.label, meta: e.meta }))),
|
|
201
|
+
Promise.all(variantEntries(project, "music").map((e) => fileAsset(store, "music", e.file, { lane: e.lane, laneName: e.laneName, variant: e.variant, label: e.label, meta: e.meta }))),
|
|
202
|
+
Promise.all(variantEntries(project, "sfx").map((e) => fileAsset(store, "sfx", e.file, { lane: e.lane, laneName: e.laneName, variant: e.variant, label: e.label, meta: e.meta }))),
|
|
203
|
+
]);
|
|
204
|
+
const [fresh, old] = await Promise.all([
|
|
205
|
+
scanScreenshotDir(store, freshNames, SCREENSHOT_DIRS[0][1], SCREENSHOT_DIRS[0][2], project),
|
|
206
|
+
scanScreenshotDir(store, oldNames, SCREENSHOT_DIRS[1][1], SCREENSHOT_DIRS[1][2], project),
|
|
207
|
+
]);
|
|
208
|
+
const screenshots = [...fresh, ...old].sort((a, b) => b.mtimeMs - a.mtimeMs).map((x) => x.asset);
|
|
209
|
+
const library = await libraryAssets(store, project);
|
|
210
|
+
return [...screenshots, ...cuts, ...proxies, ...takes, ...voice, ...music, ...sfx, ...library];
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Every path `/api/assets` would list, without stat'ing any of them: registered media (cuts,
|
|
214
|
+
* takes, variants -- present whether or not the file is actually there) plus every *.png
|
|
215
|
+
* directly inside screenshots/ and .rushes/grabs/ whose name the matching safe-name regex would
|
|
216
|
+
* let /media serve. POST /api/reveal uses this to check a path cheaply, stat'ing only the one
|
|
217
|
+
* file it actually needs instead of every asset in the project.
|
|
218
|
+
*/
|
|
219
|
+
export async function candidatePaths(store, project, script) {
|
|
220
|
+
const paths = registeredMedia(project, script);
|
|
221
|
+
const nameLists = await Promise.all(SCREENSHOT_DIRS.map(([dir]) => pngNames(store, dir)));
|
|
222
|
+
for (let i = 0; i < SCREENSHOT_DIRS.length; i++) {
|
|
223
|
+
const [, manifestPrefix, safe] = SCREENSHOT_DIRS[i];
|
|
224
|
+
for (const name of nameLists[i]) {
|
|
225
|
+
const path = `${manifestPrefix}${name}`;
|
|
226
|
+
if (safe.test(path))
|
|
227
|
+
paths.add(path);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
const discovered = await discoveredLibraryPaths(store);
|
|
231
|
+
for (const path of [...discovered.doc, ...discovered.caption, ...discovered.export])
|
|
232
|
+
paths.add(path);
|
|
233
|
+
return paths;
|
|
234
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { createReadStream } from "node:fs";
|
|
2
|
+
import { stat } from "node:fs/promises";
|
|
3
|
+
import { extname, join, normalize, sep } from "node:path";
|
|
4
|
+
import { Readable } from "node:stream";
|
|
5
|
+
import { PROXY_PATH } from "./proxy.js";
|
|
6
|
+
export const CONTENT_TYPES = {
|
|
7
|
+
".mp4": "video/mp4",
|
|
8
|
+
".m4v": "video/mp4",
|
|
9
|
+
".mov": "video/quicktime",
|
|
10
|
+
".webm": "video/webm",
|
|
11
|
+
".mkv": "video/x-matroska",
|
|
12
|
+
".wav": "audio/wav",
|
|
13
|
+
".mp3": "audio/mpeg",
|
|
14
|
+
".m4a": "audio/mp4",
|
|
15
|
+
".aac": "audio/aac",
|
|
16
|
+
".ogg": "audio/ogg",
|
|
17
|
+
".flac": "audio/flac",
|
|
18
|
+
".aif": "audio/aiff",
|
|
19
|
+
".aiff": "audio/aiff",
|
|
20
|
+
".opus": "audio/ogg",
|
|
21
|
+
".png": "image/png",
|
|
22
|
+
".jpg": "image/jpeg",
|
|
23
|
+
".jpeg": "image/jpeg",
|
|
24
|
+
".html": "text/html; charset=utf-8",
|
|
25
|
+
".js": "text/javascript; charset=utf-8",
|
|
26
|
+
".css": "text/css; charset=utf-8",
|
|
27
|
+
".svg": "image/svg+xml",
|
|
28
|
+
".woff2": "font/woff2",
|
|
29
|
+
".woff": "font/woff",
|
|
30
|
+
".json": "application/json",
|
|
31
|
+
".md": "text/markdown; charset=utf-8",
|
|
32
|
+
".txt": "text/plain; charset=utf-8",
|
|
33
|
+
".srt": "text/plain; charset=utf-8",
|
|
34
|
+
".vtt": "text/vtt; charset=utf-8",
|
|
35
|
+
".pdf": "application/pdf",
|
|
36
|
+
".gif": "image/gif",
|
|
37
|
+
".webp": "image/webp",
|
|
38
|
+
".prproj": "application/octet-stream",
|
|
39
|
+
".drp": "application/octet-stream",
|
|
40
|
+
};
|
|
41
|
+
export function contentType(path) {
|
|
42
|
+
return CONTENT_TYPES[extname(path).toLowerCase()] ?? "application/octet-stream";
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Whether `type` (as `contentType()` returns it, e.g. "image/png" or "text/plain; charset=utf-8")
|
|
46
|
+
* is safe to serve inline from `/media` -- i.e. a browser rendering it directly could never run
|
|
47
|
+
* script or load further subresources from it. Only images other than SVG (an SVG can carry
|
|
48
|
+
* `<script>`), audio, video, plain text, Markdown and PDF qualify; everything else -- HTML, XML,
|
|
49
|
+
* SVG, or any type this server doesn't otherwise recognise -- is not, however it got onto disk
|
|
50
|
+
* (registered, auto-discovered in exports/, or otherwise).
|
|
51
|
+
*/
|
|
52
|
+
export function isInlineSafeType(type) {
|
|
53
|
+
return /^(image\/(?!svg)|audio\/|video\/|text\/plain\b|text\/markdown\b|application\/pdf\b)/.test(type);
|
|
54
|
+
}
|
|
55
|
+
/** The security headers set on every /media response, safe or not: `sandbox` plus `default-src
|
|
56
|
+
* 'none'` means even a type this function is wrong about can't run script or fetch anything if
|
|
57
|
+
* a browser is ever tricked into treating the response as a document, and `nosniff` stops the
|
|
58
|
+
* browser from guessing past a deliberately generic `application/octet-stream`. */
|
|
59
|
+
export function mediaSecurityHeaders() {
|
|
60
|
+
return { "content-security-policy": "sandbox; default-src 'none'", "x-content-type-options": "nosniff" };
|
|
61
|
+
}
|
|
62
|
+
/** Grab files the server itself writes: .rushes/grabs/<safe name>.png */
|
|
63
|
+
export const GRAB_PATH = /^\.rushes\/grabs\/[a-z0-9][a-z0-9_-]*\.png$/;
|
|
64
|
+
/** Screenshots: screenshots/<safe name>.png. Dots are allowed, for names like "00m12.05s". */
|
|
65
|
+
export const SCREENSHOT_PATH = /^screenshots\/[a-z0-9][a-z0-9._-]*\.png$/;
|
|
66
|
+
/** Every media path the project has registered. Only these (and grabs) may be served. */
|
|
67
|
+
export function registeredMedia(project, script) {
|
|
68
|
+
const files = new Set();
|
|
69
|
+
for (const v of project.videos) {
|
|
70
|
+
for (const ver of v.versions) {
|
|
71
|
+
files.add(ver.file);
|
|
72
|
+
// §19.5: a cut's proxy plays in Picture just like the cut itself -- but only a name this
|
|
73
|
+
// server would write, so a hand-edited record can't open up any other file.
|
|
74
|
+
if (ver.proxy && PROXY_PATH.test(ver.proxy.file))
|
|
75
|
+
files.add(ver.proxy.file);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
for (const l of project.lanes)
|
|
79
|
+
for (const variant of l.variants)
|
|
80
|
+
files.add(variant.file);
|
|
81
|
+
for (const s of script.sections)
|
|
82
|
+
for (const t of s.takes)
|
|
83
|
+
files.add(t.file);
|
|
84
|
+
for (const f of project.files)
|
|
85
|
+
files.add(f.file);
|
|
86
|
+
return files;
|
|
87
|
+
}
|
|
88
|
+
/** Parse a single "bytes=a-b" range against a file size. Returns null for a missing or unusable range. */
|
|
89
|
+
export function parseRange(header, size) {
|
|
90
|
+
const m = header?.match(/^bytes=(\d*)-(\d*)$/);
|
|
91
|
+
if (!m || (m[1] === "" && m[2] === ""))
|
|
92
|
+
return null;
|
|
93
|
+
let start;
|
|
94
|
+
let end;
|
|
95
|
+
if (m[1] === "") {
|
|
96
|
+
// Suffix range: the last N bytes.
|
|
97
|
+
const n = Number(m[2]);
|
|
98
|
+
start = Math.max(0, size - n);
|
|
99
|
+
end = size - 1;
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
start = Number(m[1]);
|
|
103
|
+
end = m[2] === "" ? size - 1 : Math.min(Number(m[2]), size - 1);
|
|
104
|
+
}
|
|
105
|
+
if (start > end || start >= size)
|
|
106
|
+
return null;
|
|
107
|
+
return { start, end };
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Stream a file, honouring a byte Range so video can seek. Returns 404 JSON
|
|
111
|
+
* when the file is missing, and 416 when the range can't be satisfied.
|
|
112
|
+
*/
|
|
113
|
+
export async function sendFile(absPath, rangeHeader, type = contentType(absPath)) {
|
|
114
|
+
let size;
|
|
115
|
+
try {
|
|
116
|
+
const info = await stat(absPath);
|
|
117
|
+
if (!info.isFile())
|
|
118
|
+
throw new Error("not a file");
|
|
119
|
+
size = info.size;
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
return Response.json({ error: "missing_file", message: "That file isn't on disk any more" }, { status: 404 });
|
|
123
|
+
}
|
|
124
|
+
const headers = { "content-type": type, "accept-ranges": "bytes", "cache-control": "no-cache" };
|
|
125
|
+
if (rangeHeader) {
|
|
126
|
+
const range = parseRange(rangeHeader, size);
|
|
127
|
+
if (!range)
|
|
128
|
+
return new Response(null, { status: 416, headers: { "content-range": `bytes */${size}` } });
|
|
129
|
+
const stream = Readable.toWeb(createReadStream(absPath, { start: range.start, end: range.end }));
|
|
130
|
+
return new Response(stream, {
|
|
131
|
+
status: 206,
|
|
132
|
+
headers: { ...headers, "content-range": `bytes ${range.start}-${range.end}/${size}`, "content-length": String(range.end - range.start + 1) },
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
const stream = Readable.toWeb(createReadStream(absPath));
|
|
136
|
+
return new Response(stream, { status: 200, headers: { ...headers, "content-length": String(size) } });
|
|
137
|
+
}
|
|
138
|
+
/** Resolve `rel` inside `dir`, or null if it would escape it. */
|
|
139
|
+
export function inside(dir, rel) {
|
|
140
|
+
const full = normalize(join(dir, rel));
|
|
141
|
+
return full === dir || full.startsWith(dir.endsWith(sep) ? dir : dir + sep) ? full : null;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* `Content-Disposition: attachment` for a download, per RFC 6266 and RFC 5987: an ASCII-safe
|
|
145
|
+
* `filename` fallback plus a UTF-8, percent-encoded `filename*` for browsers that use it. The
|
|
146
|
+
* fallback replaces every non-ASCII character, every C0 control character and DEL, `"` and `\`
|
|
147
|
+
* with `_`, so the quoted string stays valid (and never injects a header-breaking newline) no
|
|
148
|
+
* matter what the real name contains. `filename*` is already percent-encoded, so it's safe as is.
|
|
149
|
+
*/
|
|
150
|
+
// String.prototype.toWellFormed() is ES2024; this project's configured lib is ES2022, even
|
|
151
|
+
// though Node 20.19+ and 22.12+ (its floor) both have the method at runtime. Called through
|
|
152
|
+
// this narrow cast rather than bumping the whole project's lib target (which would also drop
|
|
153
|
+
// the DOM globals -- fetch, Response, … -- bundled implicitly into the default ES2022 lib).
|
|
154
|
+
function toWellFormed(s) {
|
|
155
|
+
return s.toWellFormed();
|
|
156
|
+
}
|
|
157
|
+
export function contentDisposition(filename) {
|
|
158
|
+
// toWellFormed() swaps any unpaired UTF-16 surrogate for U+FFFD. A lone surrogate is legal
|
|
159
|
+
// in a JS string (and so in a filename read off disk) but isn't valid UTF-8, so
|
|
160
|
+
// encodeURIComponent() throws on one outright -- before this ever gets as far as deciding
|
|
161
|
+
// whether the name is safe to serve.
|
|
162
|
+
const safe = toWellFormed(filename);
|
|
163
|
+
let ascii = "";
|
|
164
|
+
for (const ch of safe) {
|
|
165
|
+
const code = ch.codePointAt(0);
|
|
166
|
+
ascii += code <= 0x1f || code >= 0x7f || ch === '"' || ch === "\\" ? "_" : ch;
|
|
167
|
+
}
|
|
168
|
+
const encoded = encodeURIComponent(safe).replace(/['()*]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`);
|
|
169
|
+
return `attachment; filename="${ascii}"; filename*=UTF-8''${encoded}`;
|
|
170
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { link, readFile, realpath, rm, writeFile } from "node:fs/promises";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
4
|
+
import { RUSHES_DIR } from "../core/store.js";
|
|
5
|
+
import { RushesError } from "../core/errors.js";
|
|
6
|
+
/** A server for this project is already running at `url`. */
|
|
7
|
+
export class AlreadyRunningError extends RushesError {
|
|
8
|
+
url;
|
|
9
|
+
constructor(url) {
|
|
10
|
+
super(`Rushes is already running for this project at ${url}`, 409, "already_running", { url });
|
|
11
|
+
this.url = url;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The one name a project folder goes by: its real path, so a symlinked path
|
|
16
|
+
* and the real one share a server. When the folder doesn't exist yet, its
|
|
17
|
+
* nearest existing parent is resolved instead and the rest is kept as given,
|
|
18
|
+
* which is the path the server will have once it creates the folder.
|
|
19
|
+
*/
|
|
20
|
+
export async function canonicalRoot(dir) {
|
|
21
|
+
const abs = resolve(dir);
|
|
22
|
+
const missing = [];
|
|
23
|
+
for (let at = abs;; at = dirname(at)) {
|
|
24
|
+
try {
|
|
25
|
+
return join(await realpath(at), ...missing);
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
if (dirname(at) === at)
|
|
29
|
+
return abs;
|
|
30
|
+
missing.unshift(basename(at));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export function lockPath(root) {
|
|
35
|
+
return join(root, RUSHES_DIR, "server.json");
|
|
36
|
+
}
|
|
37
|
+
function alive(pid) {
|
|
38
|
+
try {
|
|
39
|
+
process.kill(pid, 0);
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
return e.code === "EPERM";
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/** The lock file as written, whether or not its process is alive. Null when missing or unreadable. */
|
|
47
|
+
async function readLockFile(root) {
|
|
48
|
+
try {
|
|
49
|
+
const lock = JSON.parse(await readFile(lockPath(root), "utf8"));
|
|
50
|
+
return typeof lock.port === "number" && typeof lock.pid === "number" ? lock : null;
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/** The running server's lock, or null when there is none or its process has gone. */
|
|
57
|
+
export async function readLock(root) {
|
|
58
|
+
const lock = await readLockFile(root);
|
|
59
|
+
return lock && alive(lock.pid) ? lock : null;
|
|
60
|
+
}
|
|
61
|
+
/** Does the server on this port answer as Rushes for exactly this root? */
|
|
62
|
+
export async function isRushesFor(port, root) {
|
|
63
|
+
try {
|
|
64
|
+
const res = await fetch(`http://127.0.0.1:${port}/api/health`, { signal: AbortSignal.timeout(1500) });
|
|
65
|
+
const health = (await res.json());
|
|
66
|
+
return health.app === "rushes" && health.root === root;
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/** Errors from link() that mean the volume can't hard-link (exFAT, FAT32, some network shares). */
|
|
73
|
+
const NO_HARD_LINKS = new Set(["EPERM", "ENOTSUP", "EOPNOTSUPP", "EXDEV", "ENOSYS", "EMLINK"]);
|
|
74
|
+
/**
|
|
75
|
+
* Put the lock in place if nothing is there. Returns false when a lock already
|
|
76
|
+
* exists. Prefers a hard link (the lock appears whole or not at all) and falls
|
|
77
|
+
* back to an exclusive create on volumes that can't hard-link.
|
|
78
|
+
*/
|
|
79
|
+
async function claim(tmp, path, text, linkInto) {
|
|
80
|
+
try {
|
|
81
|
+
await linkInto(tmp, path);
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
catch (e) {
|
|
85
|
+
const code = e.code;
|
|
86
|
+
if (code === "EEXIST")
|
|
87
|
+
return false;
|
|
88
|
+
if (!code || !NO_HARD_LINKS.has(code))
|
|
89
|
+
throw e;
|
|
90
|
+
}
|
|
91
|
+
try {
|
|
92
|
+
await writeFile(path, text, { encoding: "utf8", flag: "wx" });
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
catch (e) {
|
|
96
|
+
if (e.code === "EEXIST")
|
|
97
|
+
return false;
|
|
98
|
+
throw e;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Claim the project's lock. The lock is written to a temporary file and then
|
|
103
|
+
* hard-linked into place, so it appears complete or not at all, and only one
|
|
104
|
+
* claimant can win the link. A lock whose server is alive and serving this
|
|
105
|
+
* root wins over us (AlreadyRunningError). A stale one is removed, but only if
|
|
106
|
+
* it is still the same lock we judged stale. After winning, we re-read the
|
|
107
|
+
* lock once: if a racing claimant replaced it, we go round again and defer to
|
|
108
|
+
* that server. `opts.link` is for tests.
|
|
109
|
+
*/
|
|
110
|
+
export async function writeLock(root, port, opts = {}) {
|
|
111
|
+
const lock = { port, pid: process.pid, startedAt: new Date().toISOString(), token: randomUUID() };
|
|
112
|
+
const path = lockPath(root);
|
|
113
|
+
const tmp = `${path}.${lock.token}.tmp`;
|
|
114
|
+
const text = JSON.stringify(lock, null, 2) + "\n";
|
|
115
|
+
await writeFile(tmp, text, "utf8");
|
|
116
|
+
try {
|
|
117
|
+
for (let attempt = 0; attempt < 5; attempt++) {
|
|
118
|
+
if (!(await claim(tmp, path, text, opts.link ?? link))) {
|
|
119
|
+
const existing = await readLockFile(root);
|
|
120
|
+
if (existing && alive(existing.pid) && (await isRushesFor(existing.port, root))) {
|
|
121
|
+
throw new AlreadyRunningError(`http://127.0.0.1:${existing.port}`);
|
|
122
|
+
}
|
|
123
|
+
// Stale: remove it only if it's still the lock we just judged.
|
|
124
|
+
const again = await readLockFile(root);
|
|
125
|
+
if (!existing || (again && again.token === existing.token && again.pid === existing.pid && again.startedAt === existing.startedAt)) {
|
|
126
|
+
await rm(path, { force: true });
|
|
127
|
+
}
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
await new Promise((r) => setTimeout(r, 25));
|
|
131
|
+
if ((await readLockFile(root))?.token === lock.token)
|
|
132
|
+
return lock;
|
|
133
|
+
}
|
|
134
|
+
throw new Error(`Couldn't claim ${path}: other servers kept replacing it`);
|
|
135
|
+
}
|
|
136
|
+
finally {
|
|
137
|
+
await rm(tmp, { force: true });
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/** Remove the lock only if it is ours (same token, or same pid for locks without one), so a newer server's lock survives. */
|
|
141
|
+
export async function removeLock(root, token) {
|
|
142
|
+
const lock = await readLockFile(root);
|
|
143
|
+
if (!lock)
|
|
144
|
+
return;
|
|
145
|
+
const ours = token !== undefined && lock.token !== undefined ? lock.token === token : lock.pid === process.pid;
|
|
146
|
+
if (ours)
|
|
147
|
+
await rm(lockPath(root), { force: true });
|
|
148
|
+
}
|