@torrent-tv/proxy 2.89.16 → 2.89.18
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/README.md +14 -3
- package/package.json +10 -2
- package/routes/api/client-logs/post.js +9 -2
- package/server.js +0 -11
- package/.claude/commands/opsx/apply.md +0 -155
- package/.claude/commands/opsx/archive.md +0 -160
- package/.claude/commands/opsx/explore.md +0 -174
- package/.claude/commands/opsx/propose.md +0 -109
- package/.claude/commands/opsx/sync.md +0 -143
- package/.claude/skills/openspec-apply-change/SKILL.md +0 -159
- package/.claude/skills/openspec-archive-change/SKILL.md +0 -117
- package/.claude/skills/openspec-explore/SKILL.md +0 -289
- package/.claude/skills/openspec-propose/SKILL.md +0 -113
- package/.claude/skills/openspec-sync-specs/SKILL.md +0 -147
- package/.dockerignore +0 -5
- package/AGENTS.md +0 -363
- package/CHANGELOG.md +0 -2373
- package/Dockerfile +0 -30
- package/biome.json +0 -412
- package/corpus.tmp.json +0 -1
- package/docs/browser-proxy-contract.md +0 -155
- package/docs/container-architecture.md +0 -273
- package/docs/download-architecture.md +0 -261
- package/docs/encode-architecture.md +0 -765
- package/docs/encode-run-state.md +0 -96
- package/docs/logs.md +0 -138
- package/docs/playback-start-forecast.md +0 -113
- package/docs/socks5-egress-research.md +0 -114
- package/docs/storage-architecture.md +0 -273
- package/knip.json +0 -14
- package/main2.cpuprofile +0 -1
- package/openspec/changes/adaptive-bitrate/.openspec.yaml +0 -0
- package/openspec/changes/adaptive-bitrate/design.md +0 -92
- package/openspec/changes/adaptive-bitrate/proposal.md +0 -56
- package/openspec/changes/adaptive-bitrate/specs/adaptive-bitrate/spec.md +0 -51
- package/openspec/changes/adaptive-bitrate/tasks.md +0 -40
- package/openspec/changes/binary-distribution/.openspec.yaml +0 -2
- package/openspec/changes/binary-distribution/proposal.md +0 -53
- package/openspec/changes/chunked-request-bodies/design.md +0 -81
- package/openspec/changes/chunked-request-bodies/proposal.md +0 -53
- package/openspec/changes/chunked-request-bodies/specs/chunked-request-bodies/spec.md +0 -30
- package/openspec/changes/chunked-request-bodies/tasks.md +0 -40
- package/openspec/changes/cold-start/design.md +0 -98
- package/openspec/changes/cold-start/proposal.md +0 -62
- package/openspec/changes/cold-start/specs/cold-start/spec.md +0 -47
- package/openspec/changes/cold-start/tasks.md +0 -48
- package/openspec/changes/disk-cap/.openspec.yaml +0 -2
- package/openspec/changes/disk-cap/proposal.md +0 -37
- package/openspec/changes/disk-cap/specs/disk-cap/spec.md +0 -25
- package/openspec/changes/disk-cap/tasks.md +0 -19
- package/openspec/changes/proxy-observability/.openspec.yaml +0 -2
- package/openspec/changes/proxy-observability/design.md +0 -38
- package/openspec/changes/proxy-observability/proposal.md +0 -49
- package/openspec/changes/proxy-observability/specs/observability/spec.md +0 -33
- package/openspec/changes/proxy-observability/tasks.md +0 -21
- package/openspec/changes/seek-debounce/.openspec.yaml +0 -0
- package/openspec/changes/seek-debounce/design.md +0 -118
- package/openspec/changes/seek-debounce/proposal.md +0 -59
- package/openspec/changes/seek-debounce/specs/seek-debounce/spec.md +0 -41
- package/openspec/changes/seek-debounce/tasks.md +0 -33
- package/openspec/changes/subtitle-language/.openspec.yaml +0 -2
- package/openspec/changes/subtitle-language/proposal.md +0 -50
- package/openspec/changes/subtitle-language/specs/subtitle-language/spec.md +0 -34
- package/openspec/changes/subtitle-language/tasks.md +0 -26
- package/openspec/changes/track-selection/.openspec.yaml +0 -2
- package/openspec/changes/track-selection/design.md +0 -47
- package/openspec/changes/track-selection/proposal.md +0 -45
- package/openspec/changes/track-selection/specs/track-selection/spec.md +0 -41
- package/openspec/changes/track-selection/tasks.md +0 -20
- package/openspec/changes/transcode-quality/.openspec.yaml +0 -2
- package/openspec/changes/transcode-quality/proposal.md +0 -51
- package/openspec/changes/transcode-quality/specs/transcode-quality/spec.md +0 -139
- package/openspec/changes/transcode-quality/tasks.md +0 -63
- package/openspec/config.yaml +0 -38
- package/public/.well-known/appspecific/com.chrome.devtools.json +0 -1
- package/research/delivery-wedge-stand-ladder-2026-09-13.md +0 -329
- package/research/double-spawn-2026-09-10.md +0 -171
- package/research/handover-2026-09-13.md +0 -209
- package/research/handover-reader-claims-removal-2026-09-12.md +0 -166
- package/research/piece-withdrawn-but-still-claimed-2026-09-12.md +0 -175
- package/research/preset-benchmark-size-scaling-2026-08-22.md +0 -55
- package/research/priority-map-is-the-truth-2026-09-12.md +0 -225
- package/research/valgrind-replica-2026-08-27/logs.tar.gz +0 -0
- package/scripts/build-audio-samples.mjs +0 -31
- package/scripts/render-run-graph.js +0 -173
- package/test/asset-paths.test.js +0 -28
- package/test/audio-buffer-continuity.test.js +0 -77
- package/test/audio-calibration.test.js +0 -23
- package/test/audio-inventory.test.js +0 -212
- package/test/audio-track-bitrate.test.js +0 -40
- package/test/audio-warm-recorded.test.js +0 -63
- package/test/auto-quality-step.test.js +0 -762
- package/test/available-share.test.js +0 -76
- package/test/background-fill.test.js +0 -190
- package/test/client-log-file.test.js +0 -126
- package/test/coalesce.test.js +0 -39
- package/test/completed-files.test.js +0 -115
- package/test/concurrent-cost.test.js +0 -148
- package/test/container-media-info.test.js +0 -228
- package/test/contention.test.js +0 -76
- package/test/core-dumps.test.js +0 -57
- package/test/coverage-follows-the-disk.test.js +0 -233
- package/test/coverage-map.test.js +0 -195
- package/test/cue-push-carries-what-the-walk-found.test.js +0 -58
- package/test/cut-times-timeline.test.js +0 -65
- package/test/cuts-follow-published-grid.test.js +0 -109
- package/test/data-channel-frame.test.js +0 -63
- package/test/data-channel-viewer-lifecycle.test.js +0 -193
- package/test/dead-torrent-handle.test.js +0 -52
- package/test/declared-rates.test.js +0 -116
- package/test/declared-tracks.test.js +0 -43
- package/test/decode-cost-fit.test.js +0 -202
- package/test/decode-cost.test.js +0 -475
- package/test/delivery-probe-field.test.js +0 -173
- package/test/delivery-probe.test.js +0 -382
- package/test/demand-register.test.js +0 -206
- package/test/dht-bootstrap.test.js +0 -72
- package/test/drain-threat.test.js +0 -65
- package/test/duplicate-add.test.js +0 -48
- package/test/encode-admission.test.js +0 -170
- package/test/encode-cost.test.js +0 -275
- package/test/encode-exit.test.js +0 -111
- package/test/encode-orchestrator.test.js +0 -426
- package/test/encode-plan-viewers.test.js +0 -725
- package/test/encode-plan.test.js +0 -545
- package/test/encode-run-state.test.js +0 -314
- package/test/encode-run.test.js +0 -248
- package/test/encode-slope.test.js +0 -46
- package/test/encoded-output.test.js +0 -39
- package/test/encoder-kinds.test.js +0 -122
- package/test/encoder-readings.test.js +0 -68
- package/test/episode-naming.test.js +0 -131
- package/test/every-claimant-is-told-its-share.test.js +0 -152
- package/test/failed-encoder-still-serves-what-exists.test.js +0 -354
- package/test/failed-start-breaker.test.js +0 -202
- package/test/fastest-wires.test.js +0 -143
- package/test/ffmpeg-stream-counts.test.js +0 -101
- package/test/file-edges.test.js +0 -191
- package/test/files-in-use.test.js +0 -34
- package/test/fixtures/audio-frame-boundary.json +0 -1
- package/test/fixtures/refused-audio-20261002.json +0 -10
- package/test/fmp4-presentation-clock.test.js +0 -148
- package/test/fmp4-self-contained.test.js +0 -80
- package/test/fmp4-timestamps.test.js +0 -140
- package/test/fmp4-track-completeness.test.js +0 -41
- package/test/format-keyed-outputs.test.js +0 -78
- package/test/health-metrics.test.js +0 -36
- package/test/held-read-ownership.test.js +0 -77
- package/test/held-request-width.test.js +0 -157
- package/test/helpers/encode-run.js +0 -248
- package/test/helpers/held-file.js +0 -77
- package/test/helpers/manager.js +0 -62
- package/test/helpers/matroska-file.js +0 -296
- package/test/helpers/output-spec.js +0 -63
- package/test/host-load.test.js +0 -68
- package/test/index-accuracy-is-about-the-file.test.js +0 -128
- package/test/init-compat.test.js +0 -219
- package/test/input-lost-quiets-the-plan.test.js +0 -261
- package/test/input-unavailable.test.js +0 -48
- package/test/installation-id.test.js +0 -39
- package/test/keeping-period.test.js +0 -83
- package/test/keyframe-index-accuracy.test.js +0 -188
- package/test/keyframe-table-budget.test.js +0 -123
- package/test/keyframes-belong-to-the-file.test.js +0 -249
- package/test/learned-median.test.js +0 -37
- package/test/letting-go-asks-who-is-watching.test.js +0 -191
- package/test/limit-row-by-area.test.js +0 -60
- package/test/link-budget.test.js +0 -111
- package/test/link-probe.test.js +0 -56
- package/test/local-encoding-data.test.js +0 -273
- package/test/logger-repeats.test.js +0 -134
- package/test/look-ahead-coverage.test.js +0 -56
- package/test/manager-surface.test.js +0 -291
- package/test/matroska-blocks.test.js +0 -0
- package/test/matroska-cluster-discovery.test.js +0 -323
- package/test/matroska-cues-track.test.js +0 -192
- package/test/media-time.test.js +0 -62
- package/test/memory-budget.test.js +0 -390
- package/test/move-cost.test.js +0 -216
- package/test/mp4-composition-times.test.js +0 -0
- package/test/mp4-subtitles.test.js +0 -173
- package/test/one-authority.test.js +0 -285
- package/test/one-budget.test.js +0 -149
- package/test/one-output-one-session.test.js +0 -273
- package/test/one-resource-divided.test.js +0 -185
- package/test/one-serving-output-per-height.test.js +0 -458
- package/test/orchestrator-wired.test.js +0 -207
- package/test/output-catalog.test.js +0 -39
- package/test/output-shape.test.js +0 -21
- package/test/output-spec.test.js +0 -251
- package/test/output-speed.test.js +0 -92
- package/test/outputs-being-prepared.test.js +0 -40
- package/test/packet-witness-ring.test.js +0 -236
- package/test/packet-witness.test.js +0 -148
- package/test/piece-completeness.test.js +0 -78
- package/test/piece-disk-store.test.js +0 -401
- package/test/piece-from-whole-file.test.js +0 -129
- package/test/piece-lru.test.js +0 -319
- package/test/piece-reader.test.js +0 -162
- package/test/piece-selection-offset.test.js +0 -85
- package/test/piece-store-eviction.test.js +0 -595
- package/test/piece-store-never-refuses.test.js +0 -153
- package/test/piece-store-reservations.test.js +0 -403
- package/test/piece-store-slow-disk.test.js +0 -159
- package/test/piece-tail.test.js +0 -97
- package/test/plan-host-timings.test.js +0 -68
- package/test/plan-is-steady.test.js +0 -225
- package/test/playback-readiness.test.js +0 -518
- package/test/playback-start-regressions.test.js +0 -101
- package/test/priority-map-download.test.js +0 -174
- package/test/priority-map-emptied.test.js +0 -204
- package/test/priority-map-per-output.test.js +0 -215
- package/test/priority-map.test.js +0 -163
- package/test/probe-wedge-certainty.test.js +0 -60
- package/test/produced-copy-choice.test.js +0 -213
- package/test/published-timeline.test.js +0 -83
- package/test/quality-offer.test.js +0 -160
- package/test/quality-variants.test.js +0 -1274
- package/test/rate-control-in-the-output.test.js +0 -181
- package/test/read-survives-withdrawal.test.js +0 -164
- package/test/read-window.test.js +0 -365
- package/test/reads-not-yet-arrived.test.js +0 -218
- package/test/resume-warm.test.js +0 -39
- package/test/run-graph-drift.test.js +0 -37
- package/test/run-inputs.test.js +0 -80
- package/test/run-intervals.test.js +0 -100
- package/test/run-position-follows-published-grid.test.js +0 -89
- package/test/run-progress.test.js +0 -64
- package/test/same-height-switch.test.js +0 -663
- package/test/seek-frees-the-viewers-requests.test.js +0 -217
- package/test/seek-landing.test.js +0 -109
- package/test/seek-target-not-superseded.test.js +0 -195
- package/test/segment-closed-on-both-branches.test.js +0 -114
- package/test/segment-cut-times.test.js +0 -63
- package/test/segment-demand.test.js +0 -80
- package/test/segment-hold-supersede.test.js +0 -91
- package/test/segment-judged-by-its-own-header.test.js +0 -222
- package/test/segment-serve-wiring.test.js +0 -481
- package/test/segment-store-eviction.test.js +0 -247
- package/test/segment-store.test.js +0 -352
- package/test/segment-true-start.test.js +0 -86
- package/test/segments-are-shared.test.js +0 -176
- package/test/send-chunk-bytes.test.js +0 -86
- package/test/serving-existing-output.test.js +0 -111
- package/test/serving-output.test.js +0 -102
- package/test/shared-piece-store.test.js +0 -310
- package/test/sidecar-naming.test.js +0 -142
- package/test/soundtrack-rate-bound.test.js +0 -289
- package/test/source-file.test.js +0 -133
- package/test/source-files-route.test.js +0 -113
- package/test/source-registry.test.js +0 -18
- package/test/split-a-stretch.test.js +0 -139
- package/test/stale-request-after-seek.test.js +0 -158
- package/test/startup-readings.test.js +0 -124
- package/test/stream-route.test.js +0 -237
- package/test/subtitle-cue-framing.test.js +0 -200
- package/test/subtitle-cue-source.test.js +0 -102
- package/test/subtitle-cue-walk.test.js +0 -385
- package/test/subtitle-cursor.test.js +0 -63
- package/test/subtitle-defaults.test.js +0 -118
- package/test/subtitle-language.test.js +0 -252
- package/test/subtitle-metadata.test.js +0 -15
- package/test/subtitle-plan-waits.test.js +0 -125
- package/test/subtitle-track-numbering.test.js +0 -391
- package/test/supply-margin.test.js +0 -190
- package/test/swarm-follows-readers.test.js +0 -174
- package/test/swarm-reach.test.js +0 -130
- package/test/swarm-selection.test.js +0 -220
- package/test/tail-duplication.test.js +0 -167
- package/test/the-channel-has-both-ends.test.js +0 -91
- package/test/the-torrent-thread-serves-bytes.test.js +0 -107
- package/test/thread-pool.test.js +0 -53
- package/test/timeline.test.js +0 -84
- package/test/torrent-contents.test.js +0 -235
- package/test/torrent-cost.test.js +0 -71
- package/test/torrent-files.test.js +0 -221
- package/test/torrent-naming.test.js +0 -255
- package/test/tracks-begin-together.test.js +0 -209
- package/test/tunnel-renewal.test.js +0 -167
- package/test/two-viewers-one-picture.test.js +0 -508
- package/test/upload-hurry.test.js +0 -186
- package/test/usrsctp-state.test.js +0 -131
- package/test/video-facts.test.js +0 -102
- package/test/viewer-assignments.test.js +0 -143
- package/test/viewer-outputs.test.js +0 -285
- package/test/viewer-presence.test.js +0 -116
- package/test/viewer-requests-admission.test.js +0 -92
- package/test/viewer-starts-on-map.test.js +0 -27
- package/test/viewer-states.test.js +0 -192
- package/test/viewer-subtitle-subscription.test.js +0 -96
- package/test/viewer.test.js +0 -139
- package/test/viewing-generations.test.js +0 -390
- package/test/visible-rung.test.js +0 -37
- package/test/wait-ledger.test.js +0 -100
- package/test/webrtc-shim.test.js +0 -149
- package/test/wedge-certainty.test.js +0 -131
- package/test/whole-file-held-read.test.js +0 -30
- package/test/withdraw-piece-claim.test.js +0 -212
- package/test/worker-channel.test.js +0 -158
|
@@ -1,247 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file What goes first when the disk is short: the viewers decide.
|
|
3
|
-
*
|
|
4
|
-
* The store used to throw away whole outputs by when their directory was last
|
|
5
|
-
* read, which says nothing about what anybody is about to watch. The order here
|
|
6
|
-
* is the priority map's own, read from the other end: nobody's output first,
|
|
7
|
-
* then what is behind the earliest viewer, then what is ahead of the furthest.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import test from "node:test";
|
|
11
|
-
import assert from "node:assert/strict";
|
|
12
|
-
import fs from "node:fs/promises";
|
|
13
|
-
import os from "node:os";
|
|
14
|
-
import path from "node:path";
|
|
15
|
-
import { SegmentStore } from "../services/storage/segment-store/SegmentStore.js";
|
|
16
|
-
|
|
17
|
-
const SEGMENT = 1024;
|
|
18
|
-
|
|
19
|
-
const format = {
|
|
20
|
-
isSegmentFileName: (name) => /^segment-\d{5}\.mp4$/.test(name),
|
|
21
|
-
segmentIndexFromName: (name) => {
|
|
22
|
-
const match = /^segment-(\d{5})\.mp4$/.exec(name);
|
|
23
|
-
return match ? Number(match[1]) : -1;
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* @param {number} [now]
|
|
29
|
-
*/
|
|
30
|
-
async function makeStore(now = 1000) {
|
|
31
|
-
const root = await fs.mkdtemp(path.join(os.tmpdir(), "segment-store-test-"));
|
|
32
|
-
const clock = { at: now };
|
|
33
|
-
const store = new SegmentStore({ root, now: () => clock.at });
|
|
34
|
-
return { store, root, clock };
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* @param {SegmentStore} store
|
|
39
|
-
* @param {string} key
|
|
40
|
-
* @param {number[]} indexes
|
|
41
|
-
*/
|
|
42
|
-
async function fill(store, key, indexes) {
|
|
43
|
-
store.useFormat(key, format);
|
|
44
|
-
const dir = store.directoryFor(key);
|
|
45
|
-
for (const index of indexes) {
|
|
46
|
-
await fs.writeFile(
|
|
47
|
-
path.join(dir, `segment-${String(index).padStart(5, "0")}.mp4`),
|
|
48
|
-
Buffer.alloc(SEGMENT, index % 251)
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* @param {SegmentStore} store
|
|
55
|
-
* @param {string} key
|
|
56
|
-
*/
|
|
57
|
-
async function heldNumbers(store, key) {
|
|
58
|
-
const names = await fs.readdir(store.pathFor(key)).catch(() => []);
|
|
59
|
-
return names
|
|
60
|
-
.filter((name) => format.isSegmentFileName(name))
|
|
61
|
-
.map((name) => format.segmentIndexFromName(name))
|
|
62
|
-
.sort((left, right) => left - right);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
test("what nobody is watching goes before anything anybody is", async () => {
|
|
66
|
-
const { store, root } = await makeStore();
|
|
67
|
-
try {
|
|
68
|
-
await fill(store, "watched", [0, 1, 2, 3]);
|
|
69
|
-
await fill(store, "abandoned", [0, 1, 2, 3]);
|
|
70
|
-
|
|
71
|
-
// Room for five segments of the eight held.
|
|
72
|
-
store.enforce({
|
|
73
|
-
idleMs: Number.POSITIVE_INFINITY,
|
|
74
|
-
maxBytes: 5 * SEGMENT,
|
|
75
|
-
viewersAt: (key) => (key === "watched" ? [2] : [])
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
// Three of eight had to go, and all three came from the output nobody is on.
|
|
79
|
-
assert.deepEqual(
|
|
80
|
-
await heldNumbers(store, "watched"),
|
|
81
|
-
[0, 1, 2, 3],
|
|
82
|
-
"the watched output lost segments while an unwatched one still held some"
|
|
83
|
-
);
|
|
84
|
-
assert.equal((await heldNumbers(store, "abandoned")).length, 1, "the unwatched output kept too much");
|
|
85
|
-
} finally {
|
|
86
|
-
await fs.rm(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 20 });
|
|
87
|
-
}
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
test("behind the viewer goes before ahead of the viewer, furthest behind first", async () => {
|
|
91
|
-
const { store, root } = await makeStore();
|
|
92
|
-
try {
|
|
93
|
-
await fill(store, "one", [0, 1, 2, 3, 4, 5]);
|
|
94
|
-
|
|
95
|
-
// The viewer stands on #3. Room for four of the six.
|
|
96
|
-
store.enforce({
|
|
97
|
-
idleMs: Number.POSITIVE_INFINITY,
|
|
98
|
-
maxBytes: 4 * SEGMENT,
|
|
99
|
-
viewersAt: () => [3]
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
assert.deepEqual(
|
|
103
|
-
await heldNumbers(store, "one"),
|
|
104
|
-
[2, 3, 4, 5],
|
|
105
|
-
"the two furthest behind the viewer should have gone, and nothing ahead"
|
|
106
|
-
);
|
|
107
|
-
} finally {
|
|
108
|
-
await fs.rm(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 20 });
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
test("with nothing left behind, the furthest ahead goes next", async () => {
|
|
113
|
-
const { store, root } = await makeStore();
|
|
114
|
-
try {
|
|
115
|
-
await fill(store, "one", [4, 5, 6, 7, 8]);
|
|
116
|
-
|
|
117
|
-
// The viewer is on #4, so nothing is behind. Room for three of the five.
|
|
118
|
-
store.enforce({
|
|
119
|
-
idleMs: Number.POSITIVE_INFINITY,
|
|
120
|
-
maxBytes: 3 * SEGMENT,
|
|
121
|
-
viewersAt: () => [4]
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
assert.deepEqual(
|
|
125
|
-
await heldNumbers(store, "one"),
|
|
126
|
-
[4, 5, 6],
|
|
127
|
-
"the two furthest ahead should have gone, nearest kept"
|
|
128
|
-
);
|
|
129
|
-
} finally {
|
|
130
|
-
await fs.rm(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 20 });
|
|
131
|
-
}
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
test("the segment a viewer is standing on is never taken", async () => {
|
|
135
|
-
const { store, root } = await makeStore();
|
|
136
|
-
try {
|
|
137
|
-
await fill(store, "one", [0, 1, 2]);
|
|
138
|
-
|
|
139
|
-
// Two viewers, one on each end, and room for one segment only.
|
|
140
|
-
store.enforce({
|
|
141
|
-
idleMs: Number.POSITIVE_INFINITY,
|
|
142
|
-
maxBytes: SEGMENT,
|
|
143
|
-
viewersAt: () => [0, 2]
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
const left = await heldNumbers(store, "one");
|
|
147
|
-
assert.ok(left.includes(0), "the segment the first viewer is on was taken");
|
|
148
|
-
assert.ok(left.includes(2), "the segment the second viewer is on was taken");
|
|
149
|
-
assert.deepEqual(left, [0, 2], "the one between two viewers should have gone");
|
|
150
|
-
} finally {
|
|
151
|
-
await fs.rm(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 20 });
|
|
152
|
-
}
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
test("two viewers of one output: behind the EARLIEST, ahead of the FURTHEST", async () => {
|
|
156
|
-
const { store, root } = await makeStore();
|
|
157
|
-
try {
|
|
158
|
-
await fill(store, "one", [0, 1, 2, 3, 4, 5, 6]);
|
|
159
|
-
|
|
160
|
-
// Viewers on #2 and #5. Behind means below #2; ahead means above #5.
|
|
161
|
-
store.enforce({
|
|
162
|
-
idleMs: Number.POSITIVE_INFINITY,
|
|
163
|
-
maxBytes: 5 * SEGMENT,
|
|
164
|
-
viewersAt: () => [2, 5]
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
assert.deepEqual(
|
|
168
|
-
await heldNumbers(store, "one"),
|
|
169
|
-
[2, 3, 4, 5, 6],
|
|
170
|
-
"what lies behind the earliest viewer must go before anything ahead of the furthest"
|
|
171
|
-
);
|
|
172
|
-
} finally {
|
|
173
|
-
await fs.rm(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 20 });
|
|
174
|
-
}
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
test("an output nobody has read for long enough goes whole, however much room there is", async () => {
|
|
178
|
-
const { store, root, clock } = await makeStore();
|
|
179
|
-
try {
|
|
180
|
-
await fill(store, "stale", [0, 1, 2]);
|
|
181
|
-
clock.at += 60 * 60 * 1000;
|
|
182
|
-
await fill(store, "fresh", [0, 1, 2]);
|
|
183
|
-
|
|
184
|
-
const result = store.enforce({
|
|
185
|
-
idleMs: 30 * 60 * 1000,
|
|
186
|
-
// Room for everything: this is the rule that does not wait for pressure.
|
|
187
|
-
maxBytes: Number.MAX_SAFE_INTEGER,
|
|
188
|
-
viewersAt: () => []
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
assert.equal(result.droppedIdle, 1);
|
|
192
|
-
assert.deepEqual(await heldNumbers(store, "stale"), [], "the stale output was kept");
|
|
193
|
-
assert.deepEqual(await heldNumbers(store, "fresh"), [0, 1, 2], "the fresh output was taken");
|
|
194
|
-
} finally {
|
|
195
|
-
await fs.rm(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 20 });
|
|
196
|
-
}
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
test("told nothing about viewers, it falls back to the oldest directory", async () => {
|
|
200
|
-
const { store, root, clock } = await makeStore();
|
|
201
|
-
try {
|
|
202
|
-
await fill(store, "older", [0, 1, 2]);
|
|
203
|
-
clock.at += 1000;
|
|
204
|
-
await fill(store, "newer", [0, 1, 2]);
|
|
205
|
-
|
|
206
|
-
const result = store.enforce({ idleMs: Number.POSITIVE_INFINITY, maxBytes: 4 * SEGMENT });
|
|
207
|
-
|
|
208
|
-
assert.equal(result.droppedForRoom, 1);
|
|
209
|
-
assert.deepEqual(await heldNumbers(store, "older"), []);
|
|
210
|
-
assert.deepEqual(await heldNumbers(store, "newer"), [0, 1, 2]);
|
|
211
|
-
} finally {
|
|
212
|
-
await fs.rm(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 20 });
|
|
213
|
-
}
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
test("a clean exit leaves nothing of ours, and nothing of anybody else's either", async () => {
|
|
217
|
-
const { store, root } = await makeStore();
|
|
218
|
-
try {
|
|
219
|
-
await fill(store, "one", [0, 1, 2]);
|
|
220
|
-
await fill(store, "two", [0, 1]);
|
|
221
|
-
// A directory this store never adopted. It is either an orphan of a killed
|
|
222
|
-
// process or ANOTHER PROXY'S LIVE OUTPUT, and from here the two are
|
|
223
|
-
// indistinguishable — the root is a fixed path on the machine, not this
|
|
224
|
-
// process's property. So it is left alone: an orphan gets an owner at the
|
|
225
|
-
// next startup sweep and goes with the exit after that, while a live output
|
|
226
|
-
// of another process must not be taken from under it.
|
|
227
|
-
//
|
|
228
|
-
// What this replaced removed the ROOT, recursively, which took both.
|
|
229
|
-
// Measured 2026-09-14 under `node --test`, where a second process is
|
|
230
|
-
// ordinary: 2 to 4 checks of a neighbouring file failed per run, never the
|
|
231
|
-
// same ones, reporting segments missing that they had just written.
|
|
232
|
-
await fs.mkdir(path.join(root, "not-ours"), { recursive: true });
|
|
233
|
-
await fs.writeFile(path.join(root, "not-ours", "segment-00000.mp4"), Buffer.alloc(SEGMENT));
|
|
234
|
-
|
|
235
|
-
assert.equal(store.dropAll("the proxy is shutting down"), 2);
|
|
236
|
-
|
|
237
|
-
assert.deepEqual(await heldNumbers(store, "one"), [], "what this store made is gone");
|
|
238
|
-
assert.deepEqual(await heldNumbers(store, "two"), []);
|
|
239
|
-
assert.ok((await fs.stat(root)).isDirectory(), "the root is the machine's, and it stays");
|
|
240
|
-
assert.ok(
|
|
241
|
-
(await fs.stat(path.join(root, "not-ours", "segment-00000.mp4"))).size > 0,
|
|
242
|
-
"a directory this store never adopted is not its to remove"
|
|
243
|
-
);
|
|
244
|
-
} finally {
|
|
245
|
-
await fs.rm(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 20 });
|
|
246
|
-
}
|
|
247
|
-
});
|
|
@@ -1,352 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file One store of segments, addressed by what they are, and what it makes of
|
|
3
|
-
* what a killed process left behind.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import test from "node:test";
|
|
7
|
-
import assert from "node:assert/strict";
|
|
8
|
-
import { mkdtempSync, mkdirSync, readdirSync, rmSync, writeFileSync } from "node:fs";
|
|
9
|
-
import os from "node:os";
|
|
10
|
-
import path from "node:path";
|
|
11
|
-
import { directoryNameFor, SegmentStore } from "../services/storage/segment-store/SegmentStore.js";
|
|
12
|
-
import { fmp4Format } from "../services/encode/segment-formats/fmp4.js";
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* @returns {{ store: SegmentStore, root: string, lines: string[] }}
|
|
16
|
-
*/
|
|
17
|
-
function storeInATempRoot() {
|
|
18
|
-
const root = mkdtempSync(path.join(os.tmpdir(), "segment-store-"));
|
|
19
|
-
const lines = [];
|
|
20
|
-
const store = new SegmentStore({
|
|
21
|
-
root,
|
|
22
|
-
logger: { info: (line) => lines.push(line), warn: (line) => lines.push(line) }
|
|
23
|
-
});
|
|
24
|
-
return { store, root, lines };
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* @param {string} dir
|
|
29
|
-
* @param {number} index
|
|
30
|
-
* @param {number} bytes
|
|
31
|
-
*/
|
|
32
|
-
function writeSegment(dir, index, bytes = 16) {
|
|
33
|
-
writeFileSync(path.join(dir, fmp4Format.segmentFileName(index)), Buffer.alloc(bytes));
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const KEY = "torrent:abc:fmt=fmp4:grid=kf@0:video-only:v=0/copy";
|
|
37
|
-
|
|
38
|
-
test("a published piece keeps the media intervals the encoding read from it", (t) => {
|
|
39
|
-
// The store does not judge a piece; whether it is whole is the encoding's
|
|
40
|
-
// decision (`encode/piece-completeness.js`), taken before it is published.
|
|
41
|
-
const { store, root } = storeInATempRoot();
|
|
42
|
-
t.after(() => { store.dropAll("the check is over"); rmSync(root, { recursive: true, force: true }); });
|
|
43
|
-
const format = { ...fmp4Format, readMediaRanges: () => assert.fail("the store does not read a piece it was handed") };
|
|
44
|
-
const dir = store.directoryFor(KEY);
|
|
45
|
-
store.useFormat(KEY, format);
|
|
46
|
-
writeFileSync(path.join(dir, "making-0-00000.mp4"), Buffer.from("closed"));
|
|
47
|
-
assert.deepEqual(store.closedBytesOf(KEY, "making-0-00000.mp4"), Buffer.from("closed"));
|
|
48
|
-
const mediaRanges = { tracks: [] };
|
|
49
|
-
assert.equal(store.publish(KEY, "making-0-00000.mp4", format, { mediaRanges }), "segment-00000.mp4");
|
|
50
|
-
assert.equal(store.mediaRangesOf(KEY, 0), mediaRanges);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
test("a stored piece is taken off the disk at its owner's word", (t) => {
|
|
54
|
-
const { store, root, lines } = storeInATempRoot();
|
|
55
|
-
t.after(() => { store.dropAll("the check is over"); rmSync(root, { recursive: true, force: true }); });
|
|
56
|
-
const format = { ...fmp4Format, readMediaRanges: () => ({ tracks: [] }) };
|
|
57
|
-
const dir = store.directoryFor(KEY);
|
|
58
|
-
store.useFormat(KEY, format);
|
|
59
|
-
writeSegment(dir, 71);
|
|
60
|
-
assert.ok(store.mediaRangesOf(KEY, 71));
|
|
61
|
-
store.remove(KEY, 71, "short of its cut");
|
|
62
|
-
assert.equal(store.pathOf(KEY, 71), null);
|
|
63
|
-
assert.equal(store.mediaRangesOf(KEY, 71), undefined);
|
|
64
|
-
assert.ok(lines.some((line) => line.includes("segment 71") && line.includes("short of its cut")));
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
test("two viewers of one output are given the same directory", (t) => {
|
|
68
|
-
const { store, root } = storeInATempRoot();
|
|
69
|
-
t.after(() => {
|
|
70
|
-
// The store lets its directories go, and only then is the root removed: it
|
|
71
|
-
// holds a watch on each of them — that is how a piece closed by a muxer
|
|
72
|
-
// with no channel of its own becomes an event — and a directory with an
|
|
73
|
-
// open watch cannot be removed on Windows.
|
|
74
|
-
store.dropAll("the check is over");
|
|
75
|
-
rmSync(root, { recursive: true, force: true });
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
// Which viewer asked never enters it — the address is what the segments are.
|
|
79
|
-
assert.equal(store.directoryFor(KEY), store.directoryFor(KEY));
|
|
80
|
-
assert.notEqual(store.directoryFor(KEY), store.directoryFor(`${KEY}:other`));
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
test("the directory says what it holds, so a later process can tell", (t) => {
|
|
84
|
-
const { store, root } = storeInATempRoot();
|
|
85
|
-
t.after(() => {
|
|
86
|
-
// The store lets its directories go, and only then is the root removed: it
|
|
87
|
-
// holds a watch on each of them — that is how a piece closed by a muxer
|
|
88
|
-
// with no channel of its own becomes an event — and a directory with an
|
|
89
|
-
// open watch cannot be removed on Windows.
|
|
90
|
-
store.dropAll("the check is over");
|
|
91
|
-
rmSync(root, { recursive: true, force: true });
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
const dir = store.directoryFor(KEY);
|
|
95
|
-
const names = readdirSync(dir);
|
|
96
|
-
assert.deepEqual(names, ["key.txt"]);
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
test("a piece under its served name is finished, and the last one too", (t) => {
|
|
100
|
-
const { store, root } = storeInATempRoot();
|
|
101
|
-
t.after(() => {
|
|
102
|
-
// The store lets its directories go, and only then is the root removed: it
|
|
103
|
-
// holds a watch on each of them — that is how a piece closed by a muxer
|
|
104
|
-
// with no channel of its own becomes an event — and a directory with an
|
|
105
|
-
// open watch cannot be removed on Windows.
|
|
106
|
-
store.dropAll("the check is over");
|
|
107
|
-
rmSync(root, { recursive: true, force: true });
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
const dir = store.directoryFor(KEY);
|
|
111
|
-
store.useFormat(KEY, fmp4Format);
|
|
112
|
-
writeSegment(dir, 0);
|
|
113
|
-
writeSegment(dir, 1);
|
|
114
|
-
writeSegment(dir, 2);
|
|
115
|
-
|
|
116
|
-
// It was "the NEXT number exists", which left the last piece of every run
|
|
117
|
-
// unprovable for ever and — the moment two runs share an output — declared a
|
|
118
|
-
// half-written file finished because somebody else had written the one after
|
|
119
|
-
// it. The name is the proof now, so all three count.
|
|
120
|
-
assert.deepEqual(store.provenNumbers(KEY), [0, 1, 2]);
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
test("a piece still being written is not a piece, and its name says so", (t) => {
|
|
124
|
-
const { store, root } = storeInATempRoot();
|
|
125
|
-
t.after(() => {
|
|
126
|
-
// The store lets its directories go, and only then is the root removed: it
|
|
127
|
-
// holds a watch on each of them — that is how a piece closed by a muxer
|
|
128
|
-
// with no channel of its own becomes an event — and a directory with an
|
|
129
|
-
// open watch cannot be removed on Windows.
|
|
130
|
-
store.dropAll("the check is over");
|
|
131
|
-
rmSync(root, { recursive: true, force: true });
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
const dir = store.directoryFor(KEY);
|
|
135
|
-
store.useFormat(KEY, fmp4Format);
|
|
136
|
-
writeSegment(dir, 0);
|
|
137
|
-
// What ffmpeg is writing into right now, tagged with the run that opened it.
|
|
138
|
-
writeFileSync(path.join(dir, "making-0-00001.mp4"), Buffer.alloc(64));
|
|
139
|
-
|
|
140
|
-
assert.deepEqual(store.provenNumbers(KEY), [0]);
|
|
141
|
-
assert.equal(store.isClosed(KEY, 1), false);
|
|
142
|
-
assert.equal(store.pathOf(KEY, 1), null);
|
|
143
|
-
|
|
144
|
-
// And closing it is one rename, after which it is servable.
|
|
145
|
-
assert.equal(store.publish(KEY, "making-0-00001.mp4", { ...fmp4Format, readMediaRanges: undefined }), "segment-00001.mp4");
|
|
146
|
-
assert.equal(store.isClosed(KEY, 1), true);
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
test("a file of no bytes is not a segment, whatever it is called", (t) => {
|
|
150
|
-
const { store, root } = storeInATempRoot();
|
|
151
|
-
t.after(() => {
|
|
152
|
-
// The store lets its directories go, and only then is the root removed: it
|
|
153
|
-
// holds a watch on each of them — that is how a piece closed by a muxer
|
|
154
|
-
// with no channel of its own becomes an event — and a directory with an
|
|
155
|
-
// open watch cannot be removed on Windows.
|
|
156
|
-
store.dropAll("the check is over");
|
|
157
|
-
rmSync(root, { recursive: true, force: true });
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
const dir = store.directoryFor(KEY);
|
|
161
|
-
store.useFormat(KEY, fmp4Format);
|
|
162
|
-
writeSegment(dir, 0);
|
|
163
|
-
writeSegment(dir, 1);
|
|
164
|
-
writeSegment(dir, 2, 0);
|
|
165
|
-
|
|
166
|
-
// A file of no bytes under a served name cannot arise from this proxy any
|
|
167
|
-
// more — a piece takes that name only on being closed — but a killed process
|
|
168
|
-
// can leave one, and taking it for a segment once closed the only hole in the
|
|
169
|
-
// numbering and convinced the look-ahead the encoder had produced it.
|
|
170
|
-
assert.equal(store.pathOf(KEY, 2), null);
|
|
171
|
-
assert.deepEqual(store.provenNumbers(KEY), [0, 1]);
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
test("clearing up after one run leaves every other run's work alone", (t) => {
|
|
175
|
-
const { store, root } = storeInATempRoot();
|
|
176
|
-
t.after(() => {
|
|
177
|
-
// The store lets its directories go, and only then is the root removed: it
|
|
178
|
-
// holds a watch on each of them — that is how a piece closed by a muxer
|
|
179
|
-
// with no channel of its own becomes an event — and a directory with an
|
|
180
|
-
// open watch cannot be removed on Windows.
|
|
181
|
-
store.dropAll("the check is over");
|
|
182
|
-
rmSync(root, { recursive: true, force: true });
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
const dir = store.directoryFor(KEY);
|
|
186
|
-
store.useFormat(KEY, fmp4Format);
|
|
187
|
-
// Two live runs, each with a piece open, plus finished pieces of both.
|
|
188
|
-
writeSegment(dir, 0);
|
|
189
|
-
writeSegment(dir, 100);
|
|
190
|
-
writeFileSync(path.join(dir, "making-0-00001.mp4"), Buffer.alloc(64));
|
|
191
|
-
writeFileSync(path.join(dir, "making-100-00101.mp4"), Buffer.alloc(64));
|
|
192
|
-
|
|
193
|
-
// The run that began at #0 ends. Its own unfinished piece goes and nothing
|
|
194
|
-
// else does — which the old rule could not manage: it took the highest SERVED
|
|
195
|
-
// name inside the ended run's stretch and judged its bytes, so with the
|
|
196
|
-
// naming rule above it would have removed #0, a piece that run had closed.
|
|
197
|
-
assert.equal(store.clearUpAfter(KEY, 0), 1);
|
|
198
|
-
assert.deepEqual(store.provenNumbers(KEY), [0, 100]);
|
|
199
|
-
assert.equal(
|
|
200
|
-
readdirSync(dir).includes("making-100-00101.mp4"),
|
|
201
|
-
true,
|
|
202
|
-
"the other run is still writing its own"
|
|
203
|
-
);
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
test("a directory that has not moved is not read again", (t) => {
|
|
207
|
-
const { store, root } = storeInATempRoot();
|
|
208
|
-
t.after(() => {
|
|
209
|
-
// The store lets its directories go, and only then is the root removed: it
|
|
210
|
-
// holds a watch on each of them — that is how a piece closed by a muxer
|
|
211
|
-
// with no channel of its own becomes an event — and a directory with an
|
|
212
|
-
// open watch cannot be removed on Windows.
|
|
213
|
-
store.dropAll("the check is over");
|
|
214
|
-
rmSync(root, { recursive: true, force: true });
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
const dir = store.directoryFor(KEY);
|
|
218
|
-
store.useFormat(KEY, fmp4Format);
|
|
219
|
-
writeSegment(dir, 0);
|
|
220
|
-
writeSegment(dir, 1);
|
|
221
|
-
|
|
222
|
-
const first = store.refresh(KEY);
|
|
223
|
-
const second = store.refresh(KEY);
|
|
224
|
-
assert.equal(first, second, "the same reading is handed back, not a fresh listing");
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
test("what a killed process left is found, named and counted", (t) => {
|
|
228
|
-
const { store, root, lines } = storeInATempRoot();
|
|
229
|
-
t.after(() => {
|
|
230
|
-
// The store lets its directories go, and only then is the root removed: it
|
|
231
|
-
// holds a watch on each of them — that is how a piece closed by a muxer
|
|
232
|
-
// with no channel of its own becomes an event — and a directory with an
|
|
233
|
-
// open watch cannot be removed on Windows.
|
|
234
|
-
store.dropAll("the check is over");
|
|
235
|
-
rmSync(root, { recursive: true, force: true });
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
const dir = path.join(root, directoryNameFor(KEY));
|
|
239
|
-
mkdirSync(dir, { recursive: true });
|
|
240
|
-
writeFileSync(path.join(dir, "key.txt"), `${KEY}\n`);
|
|
241
|
-
writeSegment(dir, 0);
|
|
242
|
-
writeSegment(dir, 1);
|
|
243
|
-
const orphan = path.join(root, "0123456789abcdef");
|
|
244
|
-
mkdirSync(orphan, { recursive: true });
|
|
245
|
-
writeSegment(orphan, 0);
|
|
246
|
-
|
|
247
|
-
const swept = store.sweep();
|
|
248
|
-
assert.equal(swept.directories, 2);
|
|
249
|
-
assert.equal(swept.unidentified, 1, "the one with no key file cannot be matched to a request");
|
|
250
|
-
assert.equal(swept.segments, 3);
|
|
251
|
-
assert.match(lines.join("\n"), /startup sweep/);
|
|
252
|
-
assert.match(lines.join("\n"), /ended\s+without anything recording why/);
|
|
253
|
-
});
|
|
254
|
-
|
|
255
|
-
test("what survived a kill is taken back whole, minus what was still being written", (t) => {
|
|
256
|
-
const { store, root } = storeInATempRoot();
|
|
257
|
-
t.after(() => {
|
|
258
|
-
// The store lets its directories go, and only then is the root removed: it
|
|
259
|
-
// holds a watch on each of them — that is how a piece closed by a muxer
|
|
260
|
-
// with no channel of its own becomes an event — and a directory with an
|
|
261
|
-
// open watch cannot be removed on Windows.
|
|
262
|
-
store.dropAll("the check is over");
|
|
263
|
-
rmSync(root, { recursive: true, force: true });
|
|
264
|
-
});
|
|
265
|
-
|
|
266
|
-
const dir = path.join(root, directoryNameFor(KEY));
|
|
267
|
-
mkdirSync(dir, { recursive: true });
|
|
268
|
-
writeFileSync(path.join(dir, "key.txt"), `${KEY}
|
|
269
|
-
`);
|
|
270
|
-
for (let index = 0; index <= 5; index += 1) {
|
|
271
|
-
writeSegment(dir, index);
|
|
272
|
-
}
|
|
273
|
-
// The piece the kernel interrupted. It never had a served name, so it cannot
|
|
274
|
-
// be mistaken for one — which is what the old rule could only guess at, by
|
|
275
|
-
// always throwing the highest number away.
|
|
276
|
-
writeFileSync(path.join(dir, "making-0-00006.mp4"), Buffer.alloc(64));
|
|
277
|
-
|
|
278
|
-
const taken = store.adoptWhatSurvived(() => fmp4Format);
|
|
279
|
-
|
|
280
|
-
assert.equal(taken.adopted, 1);
|
|
281
|
-
assert.equal(taken.unprovenRemoved, 1, "the one under a working name");
|
|
282
|
-
// All six kept rather than five: the highest served name used to be thrown
|
|
283
|
-
// away because nothing could prove it, and on the copy branch that is a piece
|
|
284
|
-
// whose bytes depend only on the source and cost a short machine to remake.
|
|
285
|
-
assert.deepEqual(store.provenNumbers(KEY), [0, 1, 2, 3, 4, 5]);
|
|
286
|
-
});
|
|
287
|
-
|
|
288
|
-
test("a directory that cannot name itself is thrown away rather than served", (t) => {
|
|
289
|
-
const { store, root } = storeInATempRoot();
|
|
290
|
-
t.after(() => {
|
|
291
|
-
// The store lets its directories go, and only then is the root removed: it
|
|
292
|
-
// holds a watch on each of them — that is how a piece closed by a muxer
|
|
293
|
-
// with no channel of its own becomes an event — and a directory with an
|
|
294
|
-
// open watch cannot be removed on Windows.
|
|
295
|
-
store.dropAll("the check is over");
|
|
296
|
-
rmSync(root, { recursive: true, force: true });
|
|
297
|
-
});
|
|
298
|
-
|
|
299
|
-
const orphan = path.join(root, "0123456789abcdef");
|
|
300
|
-
mkdirSync(orphan, { recursive: true });
|
|
301
|
-
writeSegment(orphan, 0);
|
|
302
|
-
|
|
303
|
-
const taken = store.adoptWhatSurvived(() => fmp4Format);
|
|
304
|
-
|
|
305
|
-
assert.equal(taken.adopted, 0);
|
|
306
|
-
assert.equal(taken.dropped, 1);
|
|
307
|
-
assert.equal(readdirSync(root).length, 0);
|
|
308
|
-
});
|
|
309
|
-
|
|
310
|
-
test("an output this proxy can no longer serve is thrown away too", (t) => {
|
|
311
|
-
const { store, root } = storeInATempRoot();
|
|
312
|
-
t.after(() => {
|
|
313
|
-
// The store lets its directories go, and only then is the root removed: it
|
|
314
|
-
// holds a watch on each of them — that is how a piece closed by a muxer
|
|
315
|
-
// with no channel of its own becomes an event — and a directory with an
|
|
316
|
-
// open watch cannot be removed on Windows.
|
|
317
|
-
store.dropAll("the check is over");
|
|
318
|
-
rmSync(root, { recursive: true, force: true });
|
|
319
|
-
});
|
|
320
|
-
|
|
321
|
-
const dir = path.join(root, directoryNameFor(KEY));
|
|
322
|
-
mkdirSync(dir, { recursive: true });
|
|
323
|
-
writeFileSync(path.join(dir, "key.txt"), `${KEY}\n`);
|
|
324
|
-
writeSegment(dir, 0);
|
|
325
|
-
|
|
326
|
-
const taken = store.adoptWhatSurvived(() => null);
|
|
327
|
-
|
|
328
|
-
assert.equal(taken.dropped, 1);
|
|
329
|
-
assert.equal(readdirSync(root).length, 0);
|
|
330
|
-
});
|
|
331
|
-
|
|
332
|
-
test("what the store weighs is reported, and dropping one output frees it", (t) => {
|
|
333
|
-
const { store, root } = storeInATempRoot();
|
|
334
|
-
t.after(() => {
|
|
335
|
-
// The store lets its directories go, and only then is the root removed: it
|
|
336
|
-
// holds a watch on each of them — that is how a piece closed by a muxer
|
|
337
|
-
// with no channel of its own becomes an event — and a directory with an
|
|
338
|
-
// open watch cannot be removed on Windows.
|
|
339
|
-
store.dropAll("the check is over");
|
|
340
|
-
rmSync(root, { recursive: true, force: true });
|
|
341
|
-
});
|
|
342
|
-
|
|
343
|
-
const dir = store.directoryFor(KEY);
|
|
344
|
-
store.useFormat(KEY, fmp4Format);
|
|
345
|
-
writeSegment(dir, 0, 1000);
|
|
346
|
-
writeSegment(dir, 1, 1000);
|
|
347
|
-
|
|
348
|
-
assert.equal(store.stats().bytes, 2000);
|
|
349
|
-
store.drop(KEY, "nobody is watching it");
|
|
350
|
-
assert.equal(store.stats().outputs, 0);
|
|
351
|
-
assert.equal(readdirSync(root).length, 0, "the watched output directory was removed");
|
|
352
|
-
});
|
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file A segment must be stamped with where it really begins.
|
|
3
|
-
*
|
|
4
|
-
* The playlist's answer comes from the container's keyframe index, and an index
|
|
5
|
-
* can be wrong. Measured 2026-08-06 on a Matroska file whose index claimed a
|
|
6
|
-
* keyframe at 157.99 s: the real ones around there were 153.820 and 164.247, so
|
|
7
|
-
* ffmpeg cut at 153.820 and the segment was stamped 157.99 — telling the player
|
|
8
|
-
* that picture belonged 4.17 s later than it did, while the subtitles, taken
|
|
9
|
-
* straight from the source, kept the true times. Speech and text drifted apart
|
|
10
|
-
* by exactly that much.
|
|
11
|
-
*
|
|
12
|
-
* The piece itself knows better: the `segment` muxer records its position as an
|
|
13
|
-
* empty edit at the head of the track's edit list.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
import test from "node:test";
|
|
17
|
-
import assert from "node:assert/strict";
|
|
18
|
-
import { readSelfContainedStartSeconds } from "../services/encode/segment-formats/mp4-boxes.js";
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* A minimal MP4 carrying `moov > mvhd` and `moov > trak > edts > elst`, with a
|
|
22
|
-
* leading empty edit of `offsetSeconds`.
|
|
23
|
-
*
|
|
24
|
-
* @param {{ offsetSeconds: number, timescale?: number, version?: 0 | 1 }} shape
|
|
25
|
-
* @returns {Buffer}
|
|
26
|
-
*/
|
|
27
|
-
function pieceWithEmptyEdit({ offsetSeconds, timescale = 1000, version = 0 }) {
|
|
28
|
-
const box = (type, body) => {
|
|
29
|
-
const head = Buffer.alloc(8);
|
|
30
|
-
head.writeUInt32BE(8 + body.length, 0);
|
|
31
|
-
head.write(type, 4, "latin1");
|
|
32
|
-
return Buffer.concat([head, body]);
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
const mvhd = Buffer.alloc(100);
|
|
36
|
-
mvhd.writeUInt8(0, 0); // version 0
|
|
37
|
-
mvhd.writeUInt32BE(timescale, 12); // timescale
|
|
38
|
-
|
|
39
|
-
const duration = Math.round(offsetSeconds * timescale);
|
|
40
|
-
let elstBody;
|
|
41
|
-
if (version === 1) {
|
|
42
|
-
elstBody = Buffer.alloc(8 + 20);
|
|
43
|
-
elstBody.writeUInt8(1, 0);
|
|
44
|
-
elstBody.writeUInt32BE(1, 4); // one entry
|
|
45
|
-
elstBody.writeBigUInt64BE(BigInt(duration), 8);
|
|
46
|
-
elstBody.writeBigInt64BE(-1n, 16); // empty edit
|
|
47
|
-
} else {
|
|
48
|
-
elstBody = Buffer.alloc(8 + 12);
|
|
49
|
-
elstBody.writeUInt8(0, 0);
|
|
50
|
-
elstBody.writeUInt32BE(1, 4); // one entry
|
|
51
|
-
elstBody.writeUInt32BE(duration, 8);
|
|
52
|
-
elstBody.writeInt32BE(-1, 12); // empty edit
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return Buffer.concat([
|
|
56
|
-
box("ftyp", Buffer.alloc(16)),
|
|
57
|
-
box("moov", Buffer.concat([
|
|
58
|
-
box("mvhd", mvhd),
|
|
59
|
-
box("trak", box("edts", box("elst", elstBody)))
|
|
60
|
-
]))
|
|
61
|
-
]);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
test("the position is read from the empty edit, in seconds", () => {
|
|
65
|
-
assert.equal(readSelfContainedStartSeconds(pieceWithEmptyEdit({ offsetSeconds: 153.82 })), 153.82);
|
|
66
|
-
assert.equal(readSelfContainedStartSeconds(pieceWithEmptyEdit({ offsetSeconds: 0 })), 0);
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
test("a 64-bit edit list is read the same way", () => {
|
|
70
|
-
const piece = pieceWithEmptyEdit({ offsetSeconds: 4321.5, version: 1 });
|
|
71
|
-
assert.equal(readSelfContainedStartSeconds(piece), 4321.5);
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
test("the movie timescale is honoured, not assumed", () => {
|
|
75
|
-
const piece = pieceWithEmptyEdit({ offsetSeconds: 12.5, timescale: 90_000 });
|
|
76
|
-
assert.equal(
|
|
77
|
-
readSelfContainedStartSeconds(piece),
|
|
78
|
-
12.5,
|
|
79
|
-
"reading the duration without dividing by the file's own timescale would give 1 125 000"
|
|
80
|
-
);
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
test("a piece with no edit list says nothing rather than guessing", () => {
|
|
84
|
-
const bare = Buffer.concat([Buffer.alloc(8), Buffer.from("ftyp", "latin1")]);
|
|
85
|
-
assert.equal(readSelfContainedStartSeconds(bare), null);
|
|
86
|
-
});
|