@camstack/addon-pipeline 1.2.137 → 1.2.139
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/dist/audio-analyzer/index.js +38 -86
- package/dist/audio-analyzer/index.mjs +36 -84
- package/dist/detection-pipeline/index.js +12 -12
- package/dist/detection-pipeline/index.mjs +5 -5
- package/dist/{dist-g0Or5_pR.mjs → dist-CTmOaCwF.mjs} +163 -3
- package/dist/{dist-4EMKvxCp.js → dist-WUZH-lAM.js} +168 -2
- package/dist/{event-loop-stall-monitor-CskL9Lgu.mjs → event-loop-stall-monitor-BMtR9qZR.mjs} +1 -1
- package/dist/{event-loop-stall-monitor-DDm07Ei6.js → event-loop-stall-monitor-CoM21UjL.js} +1 -1
- package/dist/{lazy-sharp-BlA9PxWk.js → lazy-sharp-1WF7ZnxZ.js} +1 -1
- package/dist/motion-wasm/index.js +2 -2
- package/dist/motion-wasm/index.mjs +1 -1
- package/dist/{node-BOHn8gpk.js → node-BAo2ivVu.js} +1 -1
- package/dist/{node-Bo1g8hLO.mjs → node-CQcgxO8l.mjs} +1 -1
- package/dist/pipeline-runner/index.js +55 -12
- package/dist/pipeline-runner/index.mjs +54 -11
- package/dist/{process-memory-DmrHWybb.mjs → process-memory-BPEKERfe.mjs} +1 -1
- package/dist/{process-memory-C3084DOt.js → process-memory-BZvkQCLP.js} +1 -1
- package/dist/recorder/index.js +6 -6
- package/dist/recorder/index.mjs +3 -3
- package/dist/{segment-demux-js-BrQ_A0oi.js → segment-demux-js-B1L1pfoc.js} +1 -1
- package/dist/{segment-demux-js-C-a2pghM.mjs → segment-demux-js-CubhFNwb.mjs} +1 -1
- package/dist/session-decode/decode-worker-child.js +115 -5
- package/dist/session-decode/decode-worker-child.mjs +114 -4
- package/dist/stream-broker/_stub.js +2 -2
- package/dist/stream-broker/{_virtual_mf-localSharedImportMap___mfe_internal__addon_stream_broker_widgets-AH6OJ3hJ.mjs → _virtual_mf-localSharedImportMap___mfe_internal__addon_stream_broker_widgets-DJ4k5ZBb.mjs} +3 -3
- package/dist/stream-broker/_virtual_mf___mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_camstack_mf_1_types__loadShare__.js-xdmO8wYp.mjs +26 -0
- package/dist/stream-broker/_virtual_mf___mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_camstack_mf_1_ui_mf_2_library__loadShare__.js-DPpl_mtg.mjs +26 -0
- package/dist/stream-broker/demux-worker-child.js +1 -1
- package/dist/stream-broker/demux-worker-child.mjs +1 -1
- package/dist/stream-broker/{hostInit-XNyn-FTM.mjs → hostInit-oy-XDnw6.mjs} +3 -3
- package/dist/stream-broker/index.js +170 -62
- package/dist/stream-broker/index.mjs +170 -62
- package/dist/stream-broker/remoteEntry.js +1 -1
- package/dist/{worker-protocol-BLfaD5uC.js → worker-protocol-C-0LYdSt.js} +2 -1
- package/dist/{worker-protocol-vc8WWhjx.mjs → worker-protocol-DsuI4ifI.mjs} +2 -1
- package/package.json +5 -3
- package/dist/addon-utils-C6b5Tnia.js +0 -625
- package/dist/addon-utils-C8KTuPdE.mjs +0 -583
- package/dist/stream-broker/_virtual_mf___mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_camstack_mf_1_types__loadShare__.js-CQyiEd0l.mjs +0 -26
- package/dist/stream-broker/_virtual_mf___mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_camstack_mf_1_ui_mf_2_library__loadShare__.js-BrTM_tsb.mjs +0 -26
|
@@ -1,625 +0,0 @@
|
|
|
1
|
-
const require_dist = require("./dist-4EMKvxCp.js");
|
|
2
|
-
require("node:crypto");
|
|
3
|
-
let node_fs = require("node:fs");
|
|
4
|
-
node_fs = require_dist.__toESM(node_fs, 1);
|
|
5
|
-
let node_path = require("node:path");
|
|
6
|
-
node_path = require_dist.__toESM(node_path, 1);
|
|
7
|
-
let node_util = require("node:util");
|
|
8
|
-
let node_zlib = require("node:zlib");
|
|
9
|
-
//#region ../system/dist/file-data-plane-BhKdxJgf.mjs
|
|
10
|
-
function isNonEmptyFile(filePath) {
|
|
11
|
-
return node_fs.existsSync(filePath) && node_fs.statSync(filePath).size > 0;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Sibling files of a single-file (non-directory) format — extra files the
|
|
15
|
-
* format needs, fetched from the same remote directory as `url` and stored
|
|
16
|
-
* flat alongside the main file in `modelsDir`. Catalog-declared via
|
|
17
|
-
* `formatEntry.files`, e.g. OpenVINO IR lists its `.bin` weights next to the
|
|
18
|
-
* `.xml`. The downloader stays format-agnostic; the convention lives in the
|
|
19
|
-
* catalog data (like `MLPACKAGE_FILES` for the directory case).
|
|
20
|
-
*/
|
|
21
|
-
function siblingFilesFor(formatEntry) {
|
|
22
|
-
return formatEntry.isDirectory ? [] : formatEntry.files ?? [];
|
|
23
|
-
}
|
|
24
|
-
/** Resolve a sibling's remote URL relative to the main file's directory. */
|
|
25
|
-
function siblingUrl(mainUrl, sibling) {
|
|
26
|
-
return mainUrl.replace(/[^/]+$/, sibling);
|
|
27
|
-
}
|
|
28
|
-
/** Build fetch headers, including HF auth token for huggingface.co URLs */
|
|
29
|
-
function buildHeaders(url) {
|
|
30
|
-
const headers = { "User-Agent": "CamStack/1.0" };
|
|
31
|
-
const hfToken = process.env["HF_TOKEN"] ?? process.env["HUGGING_FACE_HUB_TOKEN"];
|
|
32
|
-
if (hfToken && url.includes("huggingface.co")) headers["Authorization"] = `Bearer ${hfToken}`;
|
|
33
|
-
return headers;
|
|
34
|
-
}
|
|
35
|
-
var DEFAULT_MAX_REDIRECTS = 5;
|
|
36
|
-
function normalizeDownloadOptions(third) {
|
|
37
|
-
if (typeof third === "function") return { onProgress: third };
|
|
38
|
-
return third ?? {};
|
|
39
|
-
}
|
|
40
|
-
function isRedirectStatus(status) {
|
|
41
|
-
return status === 301 || status === 302 || status === 303 || status === 307 || status === 308;
|
|
42
|
-
}
|
|
43
|
-
function resolveRedirectUrl(current, location) {
|
|
44
|
-
return new URL(location, current);
|
|
45
|
-
}
|
|
46
|
-
async function downloadFile(url, destPath, onProgressOrOptions) {
|
|
47
|
-
if (node_fs.existsSync(destPath)) return destPath;
|
|
48
|
-
const opts = normalizeDownloadOptions(onProgressOrOptions);
|
|
49
|
-
const fetchImpl = opts.fetchImpl ?? fetch;
|
|
50
|
-
const maxRedirects = opts.maxRedirects ?? DEFAULT_MAX_REDIRECTS;
|
|
51
|
-
node_fs.mkdirSync(node_path.dirname(destPath), { recursive: true });
|
|
52
|
-
const tmpPath = destPath + ".downloading";
|
|
53
|
-
try {
|
|
54
|
-
let current = url;
|
|
55
|
-
const seen = /* @__PURE__ */ new Set();
|
|
56
|
-
let response;
|
|
57
|
-
const manual = opts.redirectPolicy !== void 0;
|
|
58
|
-
for (let hop = 0; hop <= maxRedirects; hop++) {
|
|
59
|
-
const parsed = new URL(current);
|
|
60
|
-
if (opts.redirectPolicy) opts.redirectPolicy(parsed, hop);
|
|
61
|
-
if (seen.has(parsed.href)) throw new Error(`Redirect loop detected at ${parsed.href}`);
|
|
62
|
-
seen.add(parsed.href);
|
|
63
|
-
const controller = opts.timeoutMs !== void 0 ? new AbortController() : void 0;
|
|
64
|
-
const timer = controller && opts.timeoutMs !== void 0 ? setTimeout(() => controller.abort(), opts.timeoutMs) : void 0;
|
|
65
|
-
try {
|
|
66
|
-
response = await fetchImpl(current, {
|
|
67
|
-
redirect: manual ? "manual" : "follow",
|
|
68
|
-
headers: buildHeaders(current),
|
|
69
|
-
...controller ? { signal: controller.signal } : {}
|
|
70
|
-
});
|
|
71
|
-
} finally {
|
|
72
|
-
if (timer) clearTimeout(timer);
|
|
73
|
-
}
|
|
74
|
-
if (manual && isRedirectStatus(response.status)) {
|
|
75
|
-
const location = response.headers.get("location");
|
|
76
|
-
if (!location) throw new Error(`Redirect ${response.status} missing Location header`);
|
|
77
|
-
if (hop >= maxRedirects) throw new Error(`Too many redirects (max ${maxRedirects}) downloading ${url}`);
|
|
78
|
-
current = resolveRedirectUrl(current, location).href;
|
|
79
|
-
continue;
|
|
80
|
-
}
|
|
81
|
-
break;
|
|
82
|
-
}
|
|
83
|
-
if (!response) throw new Error(`No response downloading ${url}`);
|
|
84
|
-
if (manual && isRedirectStatus(response.status)) throw new Error(`Too many redirects (max ${maxRedirects}) downloading ${url}`);
|
|
85
|
-
if (!response.ok) throw new Error(`HTTP ${response.status} downloading ${url}`);
|
|
86
|
-
if (!response.body) throw new Error(`No response body from ${url}`);
|
|
87
|
-
const total = parseInt(response.headers.get("content-length") ?? "0", 10);
|
|
88
|
-
let downloaded = 0;
|
|
89
|
-
const fileStream = node_fs.createWriteStream(tmpPath);
|
|
90
|
-
const reader = response.body.getReader();
|
|
91
|
-
try {
|
|
92
|
-
for (;;) {
|
|
93
|
-
const { done, value } = await reader.read();
|
|
94
|
-
if (done || !value) break;
|
|
95
|
-
downloaded += value.length;
|
|
96
|
-
if (opts.maxBytes !== void 0 && downloaded > opts.maxBytes) throw new Error(`Downloaded ${downloaded} bytes exceeds the ${opts.maxBytes}-byte limit`);
|
|
97
|
-
fileStream.write(value);
|
|
98
|
-
opts.onProgress?.(downloaded, total);
|
|
99
|
-
}
|
|
100
|
-
} finally {
|
|
101
|
-
fileStream.end();
|
|
102
|
-
await new Promise((resolve, reject) => {
|
|
103
|
-
fileStream.on("finish", resolve);
|
|
104
|
-
fileStream.on("error", reject);
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
node_fs.renameSync(tmpPath, destPath);
|
|
108
|
-
return destPath;
|
|
109
|
-
} catch (err) {
|
|
110
|
-
try {
|
|
111
|
-
node_fs.unlinkSync(tmpPath);
|
|
112
|
-
} catch {}
|
|
113
|
-
throw err;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* Download every file in a HuggingFace directory bundle (e.g.,
|
|
118
|
-
* `.mlpackage` / OpenVINO IR pair) atomically. `knownFiles` lists the
|
|
119
|
-
* relative paths inside the directory; the function fetches each from
|
|
120
|
-
* `${url}/${file}` and renames the staging directory only on full
|
|
121
|
-
* success. Mirrors `ModelDownloadService.downloadDirectory` but
|
|
122
|
-
* exposed as a standalone for catalog-less callers.
|
|
123
|
-
*/
|
|
124
|
-
async function downloadDirectory(url, destDir, knownFiles, onProgress) {
|
|
125
|
-
const match = url.match(/huggingface\.co\/([^/]+\/[^/]+)\/resolve\/main\/(.+)/);
|
|
126
|
-
if (!match) throw new Error(`Cannot parse HuggingFace URL: ${url}`);
|
|
127
|
-
const [, repo, dirPath] = match;
|
|
128
|
-
const files = (knownFiles ?? []).map((f) => ({
|
|
129
|
-
relativePath: f,
|
|
130
|
-
fileUrl: `https://huggingface.co/${repo}/resolve/main/${dirPath}/${f}`
|
|
131
|
-
}));
|
|
132
|
-
if (files.length === 0) throw new Error(`Directory bundle requires explicit \`files\` list (got none for ${url})`);
|
|
133
|
-
const tmpDir = destDir + ".downloading";
|
|
134
|
-
node_fs.rmSync(tmpDir, {
|
|
135
|
-
recursive: true,
|
|
136
|
-
force: true
|
|
137
|
-
});
|
|
138
|
-
node_fs.mkdirSync(tmpDir, { recursive: true });
|
|
139
|
-
let totalDownloaded = 0;
|
|
140
|
-
try {
|
|
141
|
-
for (const file of files) {
|
|
142
|
-
const destPath = node_path.join(tmpDir, file.relativePath);
|
|
143
|
-
node_fs.mkdirSync(node_path.dirname(destPath), { recursive: true });
|
|
144
|
-
await downloadFile(file.fileUrl, destPath, (downloaded, _total) => {
|
|
145
|
-
onProgress?.(totalDownloaded + downloaded, void 0);
|
|
146
|
-
});
|
|
147
|
-
totalDownloaded += node_fs.statSync(destPath).size;
|
|
148
|
-
}
|
|
149
|
-
node_fs.rmSync(destDir, {
|
|
150
|
-
recursive: true,
|
|
151
|
-
force: true
|
|
152
|
-
});
|
|
153
|
-
node_fs.renameSync(tmpDir, destDir);
|
|
154
|
-
} catch (err) {
|
|
155
|
-
node_fs.rmSync(tmpDir, {
|
|
156
|
-
recursive: true,
|
|
157
|
-
force: true
|
|
158
|
-
});
|
|
159
|
-
throw err;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* Resolve a `ModelCatalogEntry` against `modelsDir`: download model file
|
|
164
|
-
* (or directory bundle) + extra files (labels JSON, charset dict, …),
|
|
165
|
-
* skip if already on disk. Returns the local model path.
|
|
166
|
-
*/
|
|
167
|
-
async function ensureModel(modelsDir, entry, format, onProgress) {
|
|
168
|
-
const formatEntry = entry.formats[format];
|
|
169
|
-
if (!formatEntry) throw new Error(`Model "${entry.id}" has no ${format} format. Available: ${Object.keys(entry.formats).join(", ")}`);
|
|
170
|
-
if (entry.extraFiles) for (const extra of entry.extraFiles) await downloadFile(extra.url, node_path.join(modelsDir, extra.filename));
|
|
171
|
-
const filename = formatEntry.url.split("/").pop() ?? `${entry.id}.${format}`;
|
|
172
|
-
const modelPath = node_path.join(modelsDir, filename);
|
|
173
|
-
const siblings = siblingFilesFor(formatEntry);
|
|
174
|
-
if (node_fs.existsSync(modelPath)) if (formatEntry.isDirectory && !node_fs.existsSync(node_path.join(modelPath, "Manifest.json"))) node_fs.rmSync(modelPath, {
|
|
175
|
-
recursive: true,
|
|
176
|
-
force: true
|
|
177
|
-
});
|
|
178
|
-
else if (siblings.some((f) => !isNonEmptyFile(node_path.join(modelsDir, f)))) {} else return modelPath;
|
|
179
|
-
node_fs.mkdirSync(modelsDir, { recursive: true });
|
|
180
|
-
if (formatEntry.isDirectory) await downloadDirectory(formatEntry.url, modelPath, formatEntry.files, onProgress);
|
|
181
|
-
else {
|
|
182
|
-
await downloadFile(formatEntry.url, modelPath, (downloaded, total) => onProgress?.(downloaded, total === 0 ? void 0 : total));
|
|
183
|
-
for (const sibling of siblings) await downloadFile(siblingUrl(formatEntry.url, sibling), node_path.join(modelsDir, sibling));
|
|
184
|
-
}
|
|
185
|
-
return modelPath;
|
|
186
|
-
}
|
|
187
|
-
/** Compute the on-disk path for a given model + format, even when not yet downloaded. */
|
|
188
|
-
function getModelFilePath(modelsDir, entry, format) {
|
|
189
|
-
const formatEntry = entry.formats[format];
|
|
190
|
-
if (!formatEntry) return null;
|
|
191
|
-
const filename = formatEntry.url.split("/").pop() ?? `${entry.id}.${format}`;
|
|
192
|
-
return node_path.join(modelsDir, filename);
|
|
193
|
-
}
|
|
194
|
-
/** True iff the model file (or `Manifest.json` for directory bundles) exists and is non-empty. */
|
|
195
|
-
function isModelDownloaded(modelsDir, entry, format) {
|
|
196
|
-
const formatEntry = entry.formats[format];
|
|
197
|
-
if (!formatEntry) return false;
|
|
198
|
-
const modelPath = getModelFilePath(modelsDir, entry, format);
|
|
199
|
-
if (!modelPath || !node_fs.existsSync(modelPath)) return false;
|
|
200
|
-
if (formatEntry.isDirectory) return node_fs.existsSync(node_path.join(modelPath, "Manifest.json"));
|
|
201
|
-
if (node_fs.statSync(modelPath).size <= 0) return false;
|
|
202
|
-
return siblingFilesFor(formatEntry).every((f) => isNonEmptyFile(node_path.join(modelsDir, f)));
|
|
203
|
-
}
|
|
204
|
-
/** Remove the on-disk model file/directory. Returns true if something was deleted. */
|
|
205
|
-
function deleteModelFromDisk(modelsDir, entry, format) {
|
|
206
|
-
const modelPath = getModelFilePath(modelsDir, entry, format);
|
|
207
|
-
if (!modelPath || !node_fs.existsSync(modelPath)) return false;
|
|
208
|
-
const formatEntry = entry.formats[format];
|
|
209
|
-
if (formatEntry?.isDirectory) node_fs.rmSync(modelPath, {
|
|
210
|
-
recursive: true,
|
|
211
|
-
force: true
|
|
212
|
-
});
|
|
213
|
-
else {
|
|
214
|
-
node_fs.unlinkSync(modelPath);
|
|
215
|
-
if (formatEntry) for (const sibling of siblingFilesFor(formatEntry)) {
|
|
216
|
-
const sibPath = node_path.join(modelsDir, sibling);
|
|
217
|
-
if (node_fs.existsSync(sibPath)) node_fs.unlinkSync(sibPath);
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
return true;
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* Map a rel path to one candidate absolute path PER root, keeping only roots the
|
|
224
|
-
* path stays within (traversal guard). The handler serves the first candidate
|
|
225
|
-
* that exists. Rejects a path that escapes every root.
|
|
226
|
-
*/
|
|
227
|
-
function resolveFilePath(roots, rel) {
|
|
228
|
-
if (rel.length === 0) return { error: "forbidden" };
|
|
229
|
-
const candidates = [];
|
|
230
|
-
for (const rootDir of roots) {
|
|
231
|
-
const root = node_path.default.resolve(rootDir);
|
|
232
|
-
const abs = node_path.default.resolve(root, rel);
|
|
233
|
-
if (abs === root || abs.startsWith(root + node_path.default.sep)) candidates.push(abs);
|
|
234
|
-
}
|
|
235
|
-
if (candidates.length === 0) return { error: "forbidden" };
|
|
236
|
-
return { candidates };
|
|
237
|
-
}
|
|
238
|
-
var DEFAULT_CONTENT_TYPES = {
|
|
239
|
-
".m3u8": "application/vnd.apple.mpegurl",
|
|
240
|
-
".m4s": "video/mp4",
|
|
241
|
-
".mp4": "video/mp4",
|
|
242
|
-
".idx": "application/octet-stream",
|
|
243
|
-
".html": "text/html; charset=utf-8",
|
|
244
|
-
".js": "application/javascript; charset=utf-8",
|
|
245
|
-
".mjs": "application/javascript; charset=utf-8",
|
|
246
|
-
".css": "text/css; charset=utf-8",
|
|
247
|
-
".json": "application/json; charset=utf-8",
|
|
248
|
-
".map": "application/json; charset=utf-8",
|
|
249
|
-
".svg": "image/svg+xml",
|
|
250
|
-
".png": "image/png",
|
|
251
|
-
".jpg": "image/jpeg",
|
|
252
|
-
".jpeg": "image/jpeg",
|
|
253
|
-
".ico": "image/x-icon",
|
|
254
|
-
".woff2": "font/woff2",
|
|
255
|
-
".woff": "font/woff",
|
|
256
|
-
".ttf": "font/ttf"
|
|
257
|
-
};
|
|
258
|
-
function contentTypeFor(filePath, overrides) {
|
|
259
|
-
const ext = node_path.default.extname(filePath).toLowerCase();
|
|
260
|
-
return overrides?.[ext] ?? DEFAULT_CONTENT_TYPES[ext] ?? "application/octet-stream";
|
|
261
|
-
}
|
|
262
|
-
/** Parse an HTTP `Range` header against a known size. Null = serve whole file. */
|
|
263
|
-
function parseRangeHeader(header, size) {
|
|
264
|
-
if (!header) return null;
|
|
265
|
-
const m = /^bytes=(\d*)-(\d*)$/.exec(header.trim());
|
|
266
|
-
if (!m) return null;
|
|
267
|
-
const [, rawStart, rawEnd] = m;
|
|
268
|
-
if (rawStart === "" && rawEnd === "") return null;
|
|
269
|
-
let start;
|
|
270
|
-
let end;
|
|
271
|
-
if (rawStart === "") {
|
|
272
|
-
const n = Number(rawEnd);
|
|
273
|
-
if (n <= 0) return null;
|
|
274
|
-
start = Math.max(0, size - n);
|
|
275
|
-
end = size - 1;
|
|
276
|
-
} else {
|
|
277
|
-
start = Number(rawStart);
|
|
278
|
-
end = rawEnd === "" ? size - 1 : Number(rawEnd);
|
|
279
|
-
}
|
|
280
|
-
if (!Number.isFinite(start) || !Number.isFinite(end) || start > end || start >= size) return null;
|
|
281
|
-
return {
|
|
282
|
-
start,
|
|
283
|
-
end: Math.min(end, size - 1)
|
|
284
|
-
};
|
|
285
|
-
}
|
|
286
|
-
/**
|
|
287
|
-
* Content-coding for addon-served static assets: `Accept-Encoding` negotiation,
|
|
288
|
-
* an eligibility rule, and a BOUNDED cache of the compressed bytes.
|
|
289
|
-
*
|
|
290
|
-
* ## Why the ADDON owns the encoding
|
|
291
|
-
*
|
|
292
|
-
* The hub's `/addon/:addonId/*` bridge is registered `{ compress: false }`. That
|
|
293
|
-
* is not an oversight — on 2026-07-16 the global `@fastify/compress` was found
|
|
294
|
-
* emitting an EMPTY brotli stream (`content-encoding: br`, `content-length: 0`)
|
|
295
|
-
* for any compressible body over its 1 KiB threshold once the body had crossed
|
|
296
|
-
* the UDS route bridge of a FORKED addon. It silently broke the notifier SVG
|
|
297
|
-
* icons; the deploy-bundle pull route had already dodged the same class with
|
|
298
|
-
* `compress: false`. The resolution then, and the contract now, is that an addon
|
|
299
|
-
* route owns its content-coding END TO END: it negotiates, it compresses, it
|
|
300
|
-
* stamps `content-encoding` + `Vary`, and the hub pipes those bytes through
|
|
301
|
-
* untouched (`proxyToUpstream` replays the upstream headers verbatim).
|
|
302
|
-
*
|
|
303
|
-
* So compression belongs HERE, in the shared file data-plane, and nowhere above
|
|
304
|
-
* it. Anything that re-compresses on top of this reintroduces the empty-brotli
|
|
305
|
-
* bug rather than doubling the ratio.
|
|
306
|
-
*
|
|
307
|
-
* ## Why the bytes are cached, and why the cache is bounded
|
|
308
|
-
*
|
|
309
|
-
* Measured on the stream-broker embed bundle (`index-<hash>.js`, 1,443,142 B):
|
|
310
|
-
* gzip-6 → 360,868 B, brotli-5 → 327,419 B in 27 ms, brotli-11 → 293,677 B in
|
|
311
|
-
* 2,233 ms. Quality 5 is the request-path choice: 77% off the wire for 27 ms
|
|
312
|
-
* paid ONCE. Quality 11 buys another 2.3% for eighty times the CPU, on a path
|
|
313
|
-
* where a real client is waiting.
|
|
314
|
-
*
|
|
315
|
-
* The cache is an LRU bounded by BOTH entry count and total bytes, and it
|
|
316
|
-
* refuses assets over {@link COMPRESSION_MAX_ASSET_BYTES} outright. An unbounded
|
|
317
|
-
* `Map` keyed by URL is how a long-lived process walks into an OOM, and this
|
|
318
|
-
* repo has already spent a session on one.
|
|
319
|
-
*
|
|
320
|
-
* The key carries `mtimeMs` and `size`, so a rebuild that rewrites an asset in
|
|
321
|
-
* place under an UNCHANGED name (an SPA shell, a service worker) can never be
|
|
322
|
-
* served from the previous build's bytes.
|
|
323
|
-
*/
|
|
324
|
-
var brotliAsync = (0, node_util.promisify)(node_zlib.brotliCompress);
|
|
325
|
-
var gzipAsync = (0, node_util.promisify)(node_zlib.gzip);
|
|
326
|
-
/** LRU bounds. Both are enforced; whichever binds first evicts. */
|
|
327
|
-
var MAX_CACHE_ENTRIES = 64;
|
|
328
|
-
var MAX_CACHE_BYTES = 32 * 1024 * 1024;
|
|
329
|
-
/** Brotli quality — see the file header for the measured trade. */
|
|
330
|
-
var BROTLI_QUALITY = 5;
|
|
331
|
-
/**
|
|
332
|
-
* Content types worth compressing. An ALLOW-list, not a deny-list: an unknown
|
|
333
|
-
* type (`application/octet-stream`) is assumed to be opaque binary. This is what
|
|
334
|
-
* keeps jpeg/png/webp/mp4/m4s/woff2 out — running brotli over them burns CPU to
|
|
335
|
-
* make the body marginally BIGGER.
|
|
336
|
-
*/
|
|
337
|
-
var COMPRESSIBLE_EXACT = new Set([
|
|
338
|
-
"application/javascript",
|
|
339
|
-
"application/ecmascript",
|
|
340
|
-
"application/json",
|
|
341
|
-
"application/manifest+json",
|
|
342
|
-
"application/wasm",
|
|
343
|
-
"application/xml",
|
|
344
|
-
"application/xhtml+xml",
|
|
345
|
-
"application/vnd.apple.mpegurl",
|
|
346
|
-
"image/svg+xml"
|
|
347
|
-
]);
|
|
348
|
-
/** Is a body of this content type worth compressing at all? */
|
|
349
|
-
function isCompressibleContentType(contentType) {
|
|
350
|
-
const base = contentType.split(";")[0]?.trim().toLowerCase() ?? "";
|
|
351
|
-
if (base.length === 0) return false;
|
|
352
|
-
if (base.startsWith("text/")) return true;
|
|
353
|
-
if (base.endsWith("+json") || base.endsWith("+xml")) return true;
|
|
354
|
-
return COMPRESSIBLE_EXACT.has(base);
|
|
355
|
-
}
|
|
356
|
-
/** Parse `Accept-Encoding` into tokens with their q-values (default 1). */
|
|
357
|
-
function parseAcceptEncoding(header) {
|
|
358
|
-
if (header === void 0 || header.trim().length === 0) return [];
|
|
359
|
-
const out = [];
|
|
360
|
-
for (const part of header.split(",")) {
|
|
361
|
-
const [rawToken, ...params] = part.split(";");
|
|
362
|
-
const token = rawToken?.trim().toLowerCase() ?? "";
|
|
363
|
-
if (token.length === 0) continue;
|
|
364
|
-
let q = 1;
|
|
365
|
-
for (const param of params) {
|
|
366
|
-
const [name, value] = param.split("=");
|
|
367
|
-
if (name?.trim().toLowerCase() !== "q") continue;
|
|
368
|
-
const parsed = Number(value?.trim());
|
|
369
|
-
q = Number.isFinite(parsed) ? parsed : 1;
|
|
370
|
-
}
|
|
371
|
-
out.push({
|
|
372
|
-
token,
|
|
373
|
-
q
|
|
374
|
-
});
|
|
375
|
-
}
|
|
376
|
-
return out;
|
|
377
|
-
}
|
|
378
|
-
function accepts(prefs, token) {
|
|
379
|
-
const exact = prefs.find((p) => p.token === token);
|
|
380
|
-
if (exact) return exact.q > 0;
|
|
381
|
-
const wildcard = prefs.find((p) => p.token === "*");
|
|
382
|
-
return wildcard !== void 0 && wildcard.q > 0;
|
|
383
|
-
}
|
|
384
|
-
/**
|
|
385
|
-
* Pick a coding for this request: brotli when offered, else gzip, else identity.
|
|
386
|
-
*
|
|
387
|
-
* Returns `null` for identity — including when the header is ABSENT. A client
|
|
388
|
-
* that offers nothing gets the raw bytes; guessing on its behalf is how a
|
|
389
|
-
* non-negotiating consumer (a native fetch, a probe, an OTA puller) ends up with
|
|
390
|
-
* a body it cannot read.
|
|
391
|
-
*/
|
|
392
|
-
function negotiateContentEncoding(acceptEncoding) {
|
|
393
|
-
const prefs = parseAcceptEncoding(acceptEncoding);
|
|
394
|
-
if (prefs.length === 0) return null;
|
|
395
|
-
if (accepts(prefs, "br")) return "br";
|
|
396
|
-
if (accepts(prefs, "gzip")) return "gzip";
|
|
397
|
-
return null;
|
|
398
|
-
}
|
|
399
|
-
var cache = /* @__PURE__ */ new Map();
|
|
400
|
-
var inFlight = /* @__PURE__ */ new Map();
|
|
401
|
-
var compressions = 0;
|
|
402
|
-
var hits = 0;
|
|
403
|
-
var cachedBytes = 0;
|
|
404
|
-
function cacheKey(key) {
|
|
405
|
-
return `${key.encoding}|${String(key.mtimeMs)}|${String(key.size)}|${key.path}`;
|
|
406
|
-
}
|
|
407
|
-
/** Evict least-recently-used entries until both bounds hold. */
|
|
408
|
-
function evictToBounds() {
|
|
409
|
-
for (const [k, buf] of cache) {
|
|
410
|
-
if (cache.size <= MAX_CACHE_ENTRIES && cachedBytes <= MAX_CACHE_BYTES) return;
|
|
411
|
-
cache.delete(k);
|
|
412
|
-
cachedBytes -= buf.byteLength;
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
async function compressBody(raw, encoding) {
|
|
416
|
-
if (encoding === "gzip") return await gzipAsync(raw);
|
|
417
|
-
return await brotliAsync(raw, { params: {
|
|
418
|
-
[node_zlib.constants.BROTLI_PARAM_QUALITY]: BROTLI_QUALITY,
|
|
419
|
-
[node_zlib.constants.BROTLI_PARAM_SIZE_HINT]: raw.byteLength
|
|
420
|
-
} });
|
|
421
|
-
}
|
|
422
|
-
/**
|
|
423
|
-
* Compressed bytes for one asset, compressing at most once per key.
|
|
424
|
-
*
|
|
425
|
-
* `null` means "send the original" — the asset is too large to buffer, the
|
|
426
|
-
* source could not be read, or compression did not actually shrink it. Every
|
|
427
|
-
* failure degrades to identity, so this can only ever be an optimisation.
|
|
428
|
-
*
|
|
429
|
-
* Concurrent callers for the same key share ONE compression: a cold cache hit by
|
|
430
|
-
* a dozen parallel asset requests must not run zlib a dozen times.
|
|
431
|
-
*/
|
|
432
|
-
async function getCompressedAsset(key, readSource) {
|
|
433
|
-
if (key.size > 8388608) return null;
|
|
434
|
-
const id = cacheKey(key);
|
|
435
|
-
const cached = cache.get(id);
|
|
436
|
-
if (cached) {
|
|
437
|
-
cache.delete(id);
|
|
438
|
-
cache.set(id, cached);
|
|
439
|
-
hits += 1;
|
|
440
|
-
return cached;
|
|
441
|
-
}
|
|
442
|
-
const pending = inFlight.get(id) ?? (async () => {
|
|
443
|
-
let raw;
|
|
444
|
-
try {
|
|
445
|
-
raw = await readSource();
|
|
446
|
-
} catch {
|
|
447
|
-
return null;
|
|
448
|
-
}
|
|
449
|
-
const out = await compressBody(raw, key.encoding);
|
|
450
|
-
compressions += 1;
|
|
451
|
-
if (out.byteLength >= raw.byteLength) return null;
|
|
452
|
-
cache.set(id, out);
|
|
453
|
-
cachedBytes += out.byteLength;
|
|
454
|
-
evictToBounds();
|
|
455
|
-
return out;
|
|
456
|
-
})();
|
|
457
|
-
inFlight.set(id, pending);
|
|
458
|
-
try {
|
|
459
|
-
return await pending;
|
|
460
|
-
} catch {
|
|
461
|
-
return null;
|
|
462
|
-
} finally {
|
|
463
|
-
inFlight.delete(id);
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
/**
|
|
467
|
-
* A ready-made data-plane request handler that serves files from a set of roots
|
|
468
|
-
* with HTTP `Range`, for addons whose data-plane is "stream files off disk"
|
|
469
|
-
* (recording playback, scrub-frame stores, exported clips).
|
|
470
|
-
*
|
|
471
|
-
* This is the addon-side handler the addon hands to `ctx.dataPlane.serve({ handler })`.
|
|
472
|
-
* It receives the REAL Node `req`/`res`, resolves the request path against the
|
|
473
|
-
* addon's roots (traversal-guarded), and streams the file. NO token and NO CORS:
|
|
474
|
-
* the hub already authenticated the caller and the data plane is same-origin
|
|
475
|
-
* through the hub's port. Reuses the shared Range/content-type/traversal helpers
|
|
476
|
-
* so there is one implementation across the standalone file-server and this.
|
|
477
|
-
*
|
|
478
|
-
* It also owns its own CONTENT-CODING. The hub bridge is `{ compress: false }`
|
|
479
|
-
* because the global compressor emits an empty brotli stream over the forked-
|
|
480
|
-
* addon route bridge, so nothing above this layer will ever compress an addon
|
|
481
|
-
* body — see `asset-compression.ts` for the incident and the contract.
|
|
482
|
-
*/
|
|
483
|
-
/** Build a `(req, res)` handler that serves `getRoots()` files with Range. */
|
|
484
|
-
function createFileDataPlaneHandler(opts) {
|
|
485
|
-
return async (req, res) => {
|
|
486
|
-
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
487
|
-
res.writeHead(405).end();
|
|
488
|
-
return;
|
|
489
|
-
}
|
|
490
|
-
const urlPath = (req.url ?? "/").split("?")[0] ?? "/";
|
|
491
|
-
let rel;
|
|
492
|
-
try {
|
|
493
|
-
rel = decodeURIComponent(urlPath.replace(/^\/+/, ""));
|
|
494
|
-
} catch {
|
|
495
|
-
res.writeHead(400).end();
|
|
496
|
-
return;
|
|
497
|
-
}
|
|
498
|
-
if (rel.length === 0) {
|
|
499
|
-
res.writeHead(404).end();
|
|
500
|
-
return;
|
|
501
|
-
}
|
|
502
|
-
const resolved = resolveFilePath(opts.getRoots(), rel);
|
|
503
|
-
if ("error" in resolved) {
|
|
504
|
-
res.writeHead(403).end();
|
|
505
|
-
return;
|
|
506
|
-
}
|
|
507
|
-
let absPath = null;
|
|
508
|
-
let size = 0;
|
|
509
|
-
let mtimeMs = 0;
|
|
510
|
-
for (const candidate of resolved.candidates) try {
|
|
511
|
-
const st = await node_fs.promises.stat(candidate);
|
|
512
|
-
if (st.isFile()) {
|
|
513
|
-
absPath = candidate;
|
|
514
|
-
size = st.size;
|
|
515
|
-
mtimeMs = st.mtimeMs;
|
|
516
|
-
break;
|
|
517
|
-
}
|
|
518
|
-
} catch {}
|
|
519
|
-
if (absPath === null) {
|
|
520
|
-
res.writeHead(404).end();
|
|
521
|
-
return;
|
|
522
|
-
}
|
|
523
|
-
const contentType = contentTypeFor(absPath, opts.contentTypes);
|
|
524
|
-
const compressible = isCompressibleContentType(contentType);
|
|
525
|
-
const baseHeaders = {
|
|
526
|
-
"content-type": contentType,
|
|
527
|
-
"accept-ranges": "bytes",
|
|
528
|
-
"cache-control": opts.cacheControl?.(rel) ?? "no-cache",
|
|
529
|
-
...compressible ? { vary: "accept-encoding" } : {}
|
|
530
|
-
};
|
|
531
|
-
const range = parseRangeHeader(req.headers.range, size);
|
|
532
|
-
if (range) {
|
|
533
|
-
res.writeHead(206, {
|
|
534
|
-
...baseHeaders,
|
|
535
|
-
"content-range": `bytes ${range.start}-${range.end}/${size}`,
|
|
536
|
-
"content-length": String(range.end - range.start + 1)
|
|
537
|
-
});
|
|
538
|
-
if (req.method === "HEAD") {
|
|
539
|
-
res.end();
|
|
540
|
-
return;
|
|
541
|
-
}
|
|
542
|
-
(0, node_fs.createReadStream)(absPath, {
|
|
543
|
-
start: range.start,
|
|
544
|
-
end: range.end
|
|
545
|
-
}).pipe(res);
|
|
546
|
-
return;
|
|
547
|
-
}
|
|
548
|
-
const encoding = compressible ? negotiateContentEncoding(req.headers["accept-encoding"]) : null;
|
|
549
|
-
if (encoding !== null && size >= 1024) {
|
|
550
|
-
const filePath = absPath;
|
|
551
|
-
const body = await getCompressedAsset({
|
|
552
|
-
path: filePath,
|
|
553
|
-
mtimeMs,
|
|
554
|
-
size,
|
|
555
|
-
encoding
|
|
556
|
-
}, () => node_fs.promises.readFile(filePath));
|
|
557
|
-
if (body !== null) {
|
|
558
|
-
res.writeHead(200, {
|
|
559
|
-
...baseHeaders,
|
|
560
|
-
"content-encoding": encoding,
|
|
561
|
-
"accept-ranges": "none",
|
|
562
|
-
"content-length": String(body.byteLength)
|
|
563
|
-
});
|
|
564
|
-
if (req.method === "HEAD") {
|
|
565
|
-
res.end();
|
|
566
|
-
return;
|
|
567
|
-
}
|
|
568
|
-
res.end(body);
|
|
569
|
-
return;
|
|
570
|
-
}
|
|
571
|
-
}
|
|
572
|
-
res.writeHead(200, {
|
|
573
|
-
...baseHeaders,
|
|
574
|
-
"content-length": String(size)
|
|
575
|
-
});
|
|
576
|
-
if (req.method === "HEAD") {
|
|
577
|
-
res.end();
|
|
578
|
-
return;
|
|
579
|
-
}
|
|
580
|
-
(0, node_fs.createReadStream)(absPath).pipe(res);
|
|
581
|
-
};
|
|
582
|
-
}
|
|
583
|
-
//#endregion
|
|
584
|
-
Object.defineProperty(exports, "contentTypeFor", {
|
|
585
|
-
enumerable: true,
|
|
586
|
-
get: function() {
|
|
587
|
-
return contentTypeFor;
|
|
588
|
-
}
|
|
589
|
-
});
|
|
590
|
-
Object.defineProperty(exports, "createFileDataPlaneHandler", {
|
|
591
|
-
enumerable: true,
|
|
592
|
-
get: function() {
|
|
593
|
-
return createFileDataPlaneHandler;
|
|
594
|
-
}
|
|
595
|
-
});
|
|
596
|
-
Object.defineProperty(exports, "deleteModelFromDisk", {
|
|
597
|
-
enumerable: true,
|
|
598
|
-
get: function() {
|
|
599
|
-
return deleteModelFromDisk;
|
|
600
|
-
}
|
|
601
|
-
});
|
|
602
|
-
Object.defineProperty(exports, "downloadFile", {
|
|
603
|
-
enumerable: true,
|
|
604
|
-
get: function() {
|
|
605
|
-
return downloadFile;
|
|
606
|
-
}
|
|
607
|
-
});
|
|
608
|
-
Object.defineProperty(exports, "ensureModel", {
|
|
609
|
-
enumerable: true,
|
|
610
|
-
get: function() {
|
|
611
|
-
return ensureModel;
|
|
612
|
-
}
|
|
613
|
-
});
|
|
614
|
-
Object.defineProperty(exports, "isModelDownloaded", {
|
|
615
|
-
enumerable: true,
|
|
616
|
-
get: function() {
|
|
617
|
-
return isModelDownloaded;
|
|
618
|
-
}
|
|
619
|
-
});
|
|
620
|
-
Object.defineProperty(exports, "parseRangeHeader", {
|
|
621
|
-
enumerable: true,
|
|
622
|
-
get: function() {
|
|
623
|
-
return parseRangeHeader;
|
|
624
|
-
}
|
|
625
|
-
});
|