Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(website): make two build-time checks fail loudly instead of quietly

Etienne Lescot7cc0a6753c96
diff --git a/website/scripts/check-media-budget.mjs b/website/scripts/check-media-budget.mjs
index 7b9bc44a3..0d008fe53 100644
--- a/website/scripts/check-media-budget.mjs
+++ b/website/scripts/check-media-budget.mjs
@@ -63,21 +63,51 @@ function walk(dir) {
return entries.flatMap((e) => (e.isDirectory() ? walk(join(dir, e.name)) : [join(dir, e.name)]));
}
+/** The boxes a track's `hdlr` can be nested inside. Everything else is skipped
+ * whole — including `mdat`, which is the point. */
+const MP4_CONTAINERS = new Set(["moov", "trak", "mdia"]);
+
/**
* True if the MP4 declares a sound track. Reads the `hdlr` boxes rather than
* shelling out to ffprobe, which is not guaranteed on a CI runner and would make
* this check quietly skippable — which is how the guarantee would be lost.
+ *
+ * Walked as a box tree, not searched for as a byte pattern. `mdat` is compressed
+ * picture: the twelve bytes that spell a sound handler can occur in it by
+ * chance, and the failure that would produce — a silent clip rejected for an
+ * audio track it does not have — is unreproducible and looks like a bug in this
+ * file rather than in the clip.
*/
function hasAudioTrack(file) {
const buf = readFileSync(file);
- for (let i = 0; i + 4 <= buf.length; i++) {
- if (buf[i] === 0x68 && buf[i + 1] === 0x64 && buf[i + 2] === 0x6c && buf[i + 3] === 0x72) {
- // hdlr: 4 size + 4 type + 4 version/flags + 4 predefined, then handler_type
- const handler = buf.subarray(i + 12, i + 16).toString("latin1");
- if (handler === "soun") return true;
+ const walk = (start, end) => {
+ let pos = start;
+ while (pos + 8 <= end) {
+ let size = buf.readUInt32BE(pos);
+ let head = 8;
+ if (size === 1) {
+ if (pos + 16 > end) return false;
+ size = Number(buf.readBigUInt64BE(pos + 8));
+ head = 16;
+ } else if (size === 0) {
+ size = end - pos; // the last box, extending to the end of the file
+ }
+ // A size that runs past its parent means the file is not what it says
+ // it is; stop rather than resync, which is how a scan gets back into
+ // payload bytes.
+ if (size < head || pos + size > end) return false;
+ const type = buf.toString("latin1", pos + 4, pos + 8);
+ if (type === "hdlr") {
+ // FullBox: 4 version/flags, 4 pre_defined, then handler_type.
+ if (buf.toString("latin1", pos + head + 8, pos + head + 12) === "soun") return true;
+ } else if (MP4_CONTAINERS.has(type) && walk(pos + head, pos + size)) {
+ return true;
+ }
+ pos += size;
}
- }
- return false;
+ return false;
+ };
+ return walk(0, buf.length);
}
/**
diff --git a/website/scripts/gen-recreation.mjs b/website/scripts/gen-recreation.mjs
index bbfca399b..077a2ff52 100644
--- a/website/scripts/gen-recreation.mjs
+++ b/website/scripts/gen-recreation.mjs
@@ -445,10 +445,17 @@ function rulerFor(worldPx) {
/** Width at which the step drops to the next entry down. Below it the 1920 world
* applies; at or above it the world is the viewport and the rule re-steps. */
-const WIDE_BREAKPOINT_PX = Math.ceil(
- (MIN_LABEL_GAP_PX * totalSec) /
- TICK_STEPS_SEC[TICK_STEPS_SEC.indexOf(rulerFor(1920).stepSec) - 1],
-);
+const wideStep = TICK_STEPS_SEC.indexOf(rulerFor(1920).stepSec);
+// Guarded, because the failure is silent otherwise: at index 0 the lookup below
+// is TICK_STEPS_SEC[-1], the division is NaN, and JSON.stringify writes NaN as
+// `null` — so the breakpoint would ship as null rather than fail the build.
+if (wideStep < 1) {
+ throw new Error(
+ `the 1920 ruler already uses the finest step (${rulerFor(1920).stepSec}s), so there is ` +
+ "no next entry down for the wide breakpoint to be the width of",
+ );
+}
+const WIDE_BREAKPOINT_PX = Math.ceil((MIN_LABEL_GAP_PX * totalSec) / TICK_STEPS_SEC[wideStep - 1]);
const RULER = {
minLabelGapPx: MIN_LABEL_GAP_PX,