@torrent-tv/proxy 2.9.73 → 2.9.75

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.
@@ -12,6 +12,7 @@ import path from "node:path";
12
12
  import { rmSync, statfsSync } from "node:fs";
13
13
  import WebTorrent from "webtorrent";
14
14
  import { logger } from "../utils/logger.js";
15
+ import { SharedPieceStore } from "./piece-store/shared-piece-store.js";
15
16
 
16
17
  // WebTorrent's default download root (see webtorrent lib/torrent.js: TMP =
17
18
  // path.join(os.tmpdir(), 'webtorrent')). We use the default store, so all
@@ -308,6 +309,9 @@ export class TorrentPool {
308
309
  /** Global disk cap in bytes (0 = disabled). */
309
310
  #maxDiskBytes = 0;
310
311
 
312
+ /** Memory budget per torrent for resident pieces; undefined = store default. */
313
+ #memoryBytes;
314
+
311
315
  /** Periodic disk-cap enforcement timer. */
312
316
  #diskSweepTimer = null;
313
317
 
@@ -323,7 +327,9 @@ export class TorrentPool {
323
327
  * default is computed from free disk (min(10 GB, half free)). Pass 0 to
324
328
  * disable the cap.
325
329
  */
326
- constructor({ maxDiskBytes } = {}) {
330
+ constructor({ maxDiskBytes, memoryBytes } = {}) {
331
+ this.#memoryBytes = Number.isFinite(memoryBytes) && memoryBytes > 0 ? memoryBytes : undefined;
332
+
327
333
  // Sweep orphaned torrent data left by a previous hard kill (no graceful
328
334
  // shutdown ran, so destroyAll never cleaned the store). Safe here: no
329
335
  // torrents are loaded yet at construction. Best-effort, synchronous so it
@@ -558,7 +564,17 @@ export class TorrentPool {
558
564
  reject(error);
559
565
  };
560
566
  this.client.once("error", onError);
561
- this.client.add(torrentId, (readyTorrent) => {
567
+ // Our own store, and WebTorrent's piece cache switched off in front of it
568
+ // (`storeCacheSlots: 0`). That cache is what made the thread split fail:
569
+ // it hands out the buffer it keeps and re-slices it later, so moving a
570
+ // piece across threads detached memory still in use. Ours owns what it
571
+ // hands out, holds pieces in shared memory the main thread can read
572
+ // directly, and spills to disk instead of losing them.
573
+ this.client.add(torrentId, {
574
+ store: SharedPieceStore,
575
+ storeCacheSlots: 0,
576
+ storeOpts: { memoryBytes: this.#memoryBytes }
577
+ }, (readyTorrent) => {
562
578
  this.client.off("error", onError);
563
579
  this.torrents.set(key, readyTorrent);
564
580
  this.#lastAccess.set(readyTorrent, Date.now());
@@ -183,20 +183,24 @@ export function createSendStream({ port, requestId }) {
183
183
  return;
184
184
  }
185
185
  inFlight += 1;
186
- // Transfer the underlying memory rather than copying it the whole point
187
- // of the design, and the difference between 4.8 ms and 37 ms per 10 MB.
186
+ // Copy into memory this transport allocated, then transfer THAT.
188
187
  //
189
- // But only memory this chunk owns OUTRIGHT may be transferred. Node hands
190
- // out small buffers from a shared 8 KB pool: several unrelated buffers sit
191
- // in one region, each viewing its own slice (verified: a 1 KB buffer
192
- // reports an 8192-byte region at offset 8). Transferring that region
193
- // detaches it from every other buffer living there which is what broke
194
- // reads in the field 2026-08-02: headers were produced in 325 ms and the
195
- // body never arrived, because chunks from the network are small enough to
196
- // be pooled while local disk reads, which is all the local test exercised,
197
- // are not.
198
- const ownsRegion = bytes.byteOffset === 0 && bytes.byteLength === bytes.buffer.byteLength;
199
- const payload = ownsRegion ? bytes : new Uint8Array(bytes);
188
+ // Transferring the caller's buffer is faster and was what shipped, but it
189
+ // is only correct if the caller owns the memory outright and no test at
190
+ // this boundary can establish that. 2.9.73 tried to decide it by
191
+ // inspection (`byteOffset === 0 && byteLength === buffer.byteLength`),
192
+ // which answers "does this view cover its region", not "did we allocate
193
+ // it". WebTorrent's piece cache returns a buffer covering its whole
194
+ // region and keeps using it, so the check passed and the transfer
195
+ // detached the cache: every later read failed with a detached
196
+ // ArrayBuffer, and because the error never reached the reader it looked
197
+ // like an empty file (`Stream ends prematurely at 0`).
198
+ //
199
+ // The copy costs 3.64 ms per 8 MB on the field host, against 37 ms for a
200
+ // structured clone. It disappears entirely for pieces read through the
201
+ // shared piece store, which the main thread reads by offset without any
202
+ // hand-over at all.
203
+ const payload = new Uint8Array(bytes);
200
204
  port.postMessage(
201
205
  { type: Event.CHUNK, id: requestId, bytes: payload },
202
206
  [payload.buffer]
@@ -1,264 +1,264 @@
1
- /**
2
- * @file Main-thread face of the torrent worker.
3
- *
4
- * Presents the same operations the routes and session manager already use, so
5
- * moving the torrent to its own thread does not ripple through calling code.
6
- * The one unavoidable change is that torrents are named by `sourceKey` instead
7
- * of passed around as objects — objects cannot cross a thread boundary, and
8
- * pretending otherwise would mean copying them on every call.
9
- *
10
- * Reads come back as an ordinary `ReadableStream`, so `/stream` and the codec
11
- * probe consume them exactly as they consume WebTorrent's own streams today.
12
- * What that hides is the part that matters: chunks arrive as transferred
13
- * buffers, never copied — 5.3 ms per 10 MB against 37 ms if cloned and 104 ms
14
- * through a transferable stream (measured 2026-08-02, see `protocol.js`).
15
- */
16
-
17
- import { Worker } from "node:worker_threads";
18
- import { Readable } from "node:stream";
19
- import { fileURLToPath } from "node:url";
20
- import { logger } from "../../utils/logger.js";
21
- import { createCaller, createReceiveStream } from "./channel.js";
22
- import { Command, Event } from "./protocol.js";
23
-
24
- const WORKER_URL = new URL("./worker.js", import.meta.url);
25
-
26
- /**
27
- * Runs the torrent client on its own thread and exposes it to the main thread.
28
- *
29
- * Why this exists at all: profiling during a live seek found the main thread
30
- * ~85% busy with WebTorrent (buffer concatenation ~15%, wire updates ~9%,
31
- * garbage collection ~5%), while three of four cores idled. Serving a segment
32
- * queued behind that work, so reading a finished 10 MB file took 12-23 s where
33
- * handing it to the channel took 125 ms.
34
- */
35
- export class TorrentWorkerClient {
36
- #worker;
37
- #caller;
38
- /** Receive-side handles for in-flight reads, keyed by request id. */
39
- #reads = new Map();
40
- /** Monotonic ids for reads, independent of the caller's own numbering. */
41
- #nextReadId = 0;
42
-
43
- /**
44
- * @param {{ maxDiskBytes?: number }} [options]
45
- */
46
- constructor({ maxDiskBytes } = {}) {
47
- this.#worker = new Worker(fileURLToPath(WORKER_URL), {
48
- workerData: { maxDiskBytes }
49
- });
50
- this.#caller = createCaller(this.#worker);
51
-
52
- this.#worker.on("message", (message) => {
53
- if (this.#caller.handleReply(message)) {
54
- return;
55
- }
56
- switch (message?.type) {
57
- case Event.CHUNK: {
58
- const bytes = message.bytes;
59
- this.#reads.get(message.id)?.push(
60
- new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.length)
61
- );
62
- break;
63
- }
64
- case Event.READ_END:
65
- this.#reads.get(message.id)?.close();
66
- this.#reads.delete(message.id);
67
- break;
68
- case Event.LOG:
69
- logger.info(`torrent-worker: ${message.message}`);
70
- break;
71
- default:
72
- break;
73
- }
74
- });
75
-
76
- this.#worker.on("error", (error) => {
77
- logger.error(`torrent-worker crashed: ${error?.message ?? error}`);
78
- // Fail everything outstanding rather than leaving callers hanging: a dead
79
- // worker will never answer, and a stalled request is worse than an error
80
- // the loading flow can retry.
81
- const reason = new Error("Torrent worker stopped unexpectedly.");
82
- this.#caller.rejectAll(reason);
83
- for (const [, read] of this.#reads) {
84
- read.fail(reason);
85
- }
86
- this.#reads.clear();
87
- });
88
- }
89
-
90
- /**
91
- * Add (or join) a torrent and register it under `sourceKey`.
92
- *
93
- * @param {{ sourceKey: string, sourceType: "magnet" | "torrent", source: string }} params
94
- * @returns {Promise<{ infoHash: string, name: string, files: { index: number, name: string, path: string, length: number }[] }>}
95
- */
96
- async addSource({ sourceKey, sourceType, source }) {
97
- return this.#caller.call(Command.ADD_SOURCE, { sourceKey, sourceType, source });
98
- }
99
-
100
- /**
101
- * The torrent's files, as plain data.
102
- *
103
- * @param {string} sourceKey
104
- * @returns {Promise<{ index: number, name: string, path: string, length: number }[]>}
105
- */
106
- async listFiles(sourceKey) {
107
- return this.#caller.call(Command.LIST_FILES, { sourceKey });
108
- }
109
-
110
- /**
111
- * Claim a file so it is not evicted while being read.
112
- *
113
- * @param {string} sourceKey
114
- * @param {number} fileIndex
115
- * @returns {Promise<void>}
116
- */
117
- async acquireFile(sourceKey, fileIndex) {
118
- await this.#caller.call(Command.ACQUIRE_FILE, { sourceKey, fileIndex });
119
- }
120
-
121
- /**
122
- * Drop a claim taken with {@link acquireFile}.
123
- *
124
- * @param {string} sourceKey
125
- * @param {number} fileIndex
126
- * @returns {Promise<void>}
127
- */
128
- async releaseFile(sourceKey, fileIndex) {
129
- await this.#caller.call(Command.RELEASE_FILE, { sourceKey, fileIndex });
130
- }
131
-
132
- /**
133
- * Live download figures for the progress display.
134
- *
135
- * @param {{ sourceKey: string, fileIndex: number, resumeAnchorByteStart?: number | null }} params
136
- * @returns {Promise<object>}
137
- */
138
- async getFileStats({ sourceKey, fileIndex, resumeAnchorByteStart = null }) {
139
- return this.#caller.call(Command.FILE_STATS, { sourceKey, fileIndex, resumeAnchorByteStart });
140
- }
141
-
142
- /**
143
- * Reorder piece selection around a read position (seek prioritisation).
144
- *
145
- * @param {{ sourceKey: string, fileIndex: number, byteStart: number, windowBytes?: number }} params
146
- * @returns {Promise<void>}
147
- */
148
- async prioritizeByteRange({ sourceKey, fileIndex, byteStart, windowBytes }) {
149
- await this.#caller.call(Command.PRIORITIZE, { sourceKey, fileIndex, byteStart, windowBytes });
150
- }
151
-
152
- /**
153
- * Pre-fetch the head and tail the codec probe needs.
154
- *
155
- * @param {{ sourceKey: string, fileIndex: number, headBytes?: number, tailBytes?: number, timeoutMs?: number }} params
156
- * @returns {Promise<unknown>}
157
- */
158
- async prefetchFileEdges({ sourceKey, fileIndex, headBytes, tailBytes, timeoutMs }) {
159
- return this.#caller.call(Command.PREFETCH_EDGES, {
160
- sourceKey,
161
- fileIndex,
162
- headBytes,
163
- tailBytes,
164
- timeoutMs
165
- });
166
- }
167
-
168
- /**
169
- * Read a byte range as a stream.
170
- *
171
- * Returns immediately with a stream that fills as chunks arrive; cancelling it
172
- * (viewer gone, seek superseded) stops the worker reading, so pieces are not
173
- * fetched for a stream nobody will drain.
174
- *
175
- * @param {{ sourceKey: string, fileIndex: number, start?: number | null, end?: number | null }} params
176
- * @returns {ReadableStream<Uint8Array>}
177
- */
178
- createReadStream({ sourceKey, fileIndex, start = null, end = null }) {
179
- const readId = (this.#nextReadId += 1);
180
- const receive = createReceiveStream({
181
- port: this.#worker,
182
- requestId: readId,
183
- onCancel: () => {
184
- void this.#caller.call(Command.CANCEL_READ, { readId }).catch(() => undefined);
185
- this.#reads.delete(readId);
186
- }
187
- });
188
- this.#reads.set(readId, receive);
189
-
190
- // The worker replies to READ_RANGE only once the body is fully sent; a
191
- // failure before that must surface on the stream, not vanish.
192
- this.#worker.postMessage({
193
- command: Command.READ_RANGE,
194
- id: readId,
195
- params: { sourceKey, fileIndex, start, end }
196
- });
197
-
198
- return receive.stream;
199
- }
200
-
201
- /**
202
- * A stand-in for the WebTorrent torrent object, backed by the worker.
203
- *
204
- * Callers already hold a torrent and reach into `torrent.files[i]` — for the
205
- * length, the name, or a read stream. Handing back an object of the same
206
- * shape keeps every one of those call sites working unchanged, which matters:
207
- * they are spread across the stream route, the subtitle route, the playback
208
- * planner and the health report, and rewriting all of them to thread a
209
- * `sourceKey` through would be a large change with nothing to show for it.
210
- *
211
- * Only what is actually used is provided. Anything else would be a promise we
212
- * cannot keep — the real object lives on the other thread and its methods are
213
- * not reachable from here.
214
- *
215
- * @param {{ sourceKey: string, sourceType: "magnet" | "torrent", source: string }} params
216
- * @returns {Promise<{ infoHash: string, name: string, sourceKey: string, files: object[] }>}
217
- */
218
- async getTorrent({ sourceKey, sourceType, source }) {
219
- const info = await this.addSource({ sourceKey, sourceType, source });
220
- const client = this;
221
- return {
222
- infoHash: info.infoHash,
223
- name: info.name,
224
- // Carried so helpers that receive only the torrent can still name it to
225
- // the worker.
226
- sourceKey,
227
- files: info.files.map((file) => ({
228
- ...file,
229
- /**
230
- * @param {{ start?: number, end?: number }} [options]
231
- * @returns {ReadableStream<Uint8Array>}
232
- */
233
- createReadStream(options = {}) {
234
- // Node stream, not a web one: Fastify replies and the ffmpeg pipe
235
- // both expect that shape, and every existing call site passes the
236
- // result straight to one of them. `Readable.fromWeb` adds no copy —
237
- // it wraps the same buffers.
238
- return Readable.fromWeb(
239
- client.createReadStream({
240
- sourceKey,
241
- fileIndex: file.index,
242
- start: options.start ?? null,
243
- end: options.end ?? null
244
- })
245
- );
246
- }
247
- }))
248
- };
249
- }
250
-
251
- /**
252
- * Shut the torrent client down and stop the thread.
253
- *
254
- * @returns {Promise<void>}
255
- */
256
- async destroyAll() {
257
- try {
258
- await this.#caller.call(Command.DESTROY_ALL, {});
259
- } catch {
260
- // Already gone — termination below is what matters.
261
- }
262
- await this.#worker.terminate();
263
- }
264
- }
1
+ /**
2
+ * @file Main-thread face of the torrent worker.
3
+ *
4
+ * Presents the same operations the routes and session manager already use, so
5
+ * moving the torrent to its own thread does not ripple through calling code.
6
+ * The one unavoidable change is that torrents are named by `sourceKey` instead
7
+ * of passed around as objects — objects cannot cross a thread boundary, and
8
+ * pretending otherwise would mean copying them on every call.
9
+ *
10
+ * Reads come back as an ordinary `ReadableStream`, so `/stream` and the codec
11
+ * probe consume them exactly as they consume WebTorrent's own streams today.
12
+ * What that hides is the part that matters: chunks arrive as transferred
13
+ * buffers, never copied — 5.3 ms per 10 MB against 37 ms if cloned and 104 ms
14
+ * through a transferable stream (measured 2026-08-02, see `protocol.js`).
15
+ */
16
+
17
+ import { Worker } from "node:worker_threads";
18
+ import { Readable } from "node:stream";
19
+ import { fileURLToPath } from "node:url";
20
+ import { logger } from "../../utils/logger.js";
21
+ import { createCaller, createReceiveStream } from "./channel.js";
22
+ import { Command, Event } from "./protocol.js";
23
+
24
+ const WORKER_URL = new URL("./worker.js", import.meta.url);
25
+
26
+ /**
27
+ * Runs the torrent client on its own thread and exposes it to the main thread.
28
+ *
29
+ * Why this exists at all: profiling during a live seek found the main thread
30
+ * ~85% busy with WebTorrent (buffer concatenation ~15%, wire updates ~9%,
31
+ * garbage collection ~5%), while three of four cores idled. Serving a segment
32
+ * queued behind that work, so reading a finished 10 MB file took 12-23 s where
33
+ * handing it to the channel took 125 ms.
34
+ */
35
+ export class TorrentWorkerClient {
36
+ #worker;
37
+ #caller;
38
+ /** Receive-side handles for in-flight reads, keyed by request id. */
39
+ #reads = new Map();
40
+ /** Monotonic ids for reads, independent of the caller's own numbering. */
41
+ #nextReadId = 0;
42
+
43
+ /**
44
+ * @param {{ maxDiskBytes?: number, memoryBytes?: number }} [options]
45
+ */
46
+ constructor({ maxDiskBytes, memoryBytes } = {}) {
47
+ this.#worker = new Worker(fileURLToPath(WORKER_URL), {
48
+ workerData: { maxDiskBytes, memoryBytes }
49
+ });
50
+ this.#caller = createCaller(this.#worker);
51
+
52
+ this.#worker.on("message", (message) => {
53
+ if (this.#caller.handleReply(message)) {
54
+ return;
55
+ }
56
+ switch (message?.type) {
57
+ case Event.CHUNK: {
58
+ const bytes = message.bytes;
59
+ this.#reads.get(message.id)?.push(
60
+ new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.length)
61
+ );
62
+ break;
63
+ }
64
+ case Event.READ_END:
65
+ this.#reads.get(message.id)?.close();
66
+ this.#reads.delete(message.id);
67
+ break;
68
+ case Event.LOG:
69
+ logger.info(`torrent-worker: ${message.message}`);
70
+ break;
71
+ default:
72
+ break;
73
+ }
74
+ });
75
+
76
+ this.#worker.on("error", (error) => {
77
+ logger.error(`torrent-worker crashed: ${error?.message ?? error}`);
78
+ // Fail everything outstanding rather than leaving callers hanging: a dead
79
+ // worker will never answer, and a stalled request is worse than an error
80
+ // the loading flow can retry.
81
+ const reason = new Error("Torrent worker stopped unexpectedly.");
82
+ this.#caller.rejectAll(reason);
83
+ for (const [, read] of this.#reads) {
84
+ read.fail(reason);
85
+ }
86
+ this.#reads.clear();
87
+ });
88
+ }
89
+
90
+ /**
91
+ * Add (or join) a torrent and register it under `sourceKey`.
92
+ *
93
+ * @param {{ sourceKey: string, sourceType: "magnet" | "torrent", source: string }} params
94
+ * @returns {Promise<{ infoHash: string, name: string, files: { index: number, name: string, path: string, length: number }[] }>}
95
+ */
96
+ async addSource({ sourceKey, sourceType, source }) {
97
+ return this.#caller.call(Command.ADD_SOURCE, { sourceKey, sourceType, source });
98
+ }
99
+
100
+ /**
101
+ * The torrent's files, as plain data.
102
+ *
103
+ * @param {string} sourceKey
104
+ * @returns {Promise<{ index: number, name: string, path: string, length: number }[]>}
105
+ */
106
+ async listFiles(sourceKey) {
107
+ return this.#caller.call(Command.LIST_FILES, { sourceKey });
108
+ }
109
+
110
+ /**
111
+ * Claim a file so it is not evicted while being read.
112
+ *
113
+ * @param {string} sourceKey
114
+ * @param {number} fileIndex
115
+ * @returns {Promise<void>}
116
+ */
117
+ async acquireFile(sourceKey, fileIndex) {
118
+ await this.#caller.call(Command.ACQUIRE_FILE, { sourceKey, fileIndex });
119
+ }
120
+
121
+ /**
122
+ * Drop a claim taken with {@link acquireFile}.
123
+ *
124
+ * @param {string} sourceKey
125
+ * @param {number} fileIndex
126
+ * @returns {Promise<void>}
127
+ */
128
+ async releaseFile(sourceKey, fileIndex) {
129
+ await this.#caller.call(Command.RELEASE_FILE, { sourceKey, fileIndex });
130
+ }
131
+
132
+ /**
133
+ * Live download figures for the progress display.
134
+ *
135
+ * @param {{ sourceKey: string, fileIndex: number, resumeAnchorByteStart?: number | null }} params
136
+ * @returns {Promise<object>}
137
+ */
138
+ async getFileStats({ sourceKey, fileIndex, resumeAnchorByteStart = null }) {
139
+ return this.#caller.call(Command.FILE_STATS, { sourceKey, fileIndex, resumeAnchorByteStart });
140
+ }
141
+
142
+ /**
143
+ * Reorder piece selection around a read position (seek prioritisation).
144
+ *
145
+ * @param {{ sourceKey: string, fileIndex: number, byteStart: number, windowBytes?: number }} params
146
+ * @returns {Promise<void>}
147
+ */
148
+ async prioritizeByteRange({ sourceKey, fileIndex, byteStart, windowBytes }) {
149
+ await this.#caller.call(Command.PRIORITIZE, { sourceKey, fileIndex, byteStart, windowBytes });
150
+ }
151
+
152
+ /**
153
+ * Pre-fetch the head and tail the codec probe needs.
154
+ *
155
+ * @param {{ sourceKey: string, fileIndex: number, headBytes?: number, tailBytes?: number, timeoutMs?: number }} params
156
+ * @returns {Promise<unknown>}
157
+ */
158
+ async prefetchFileEdges({ sourceKey, fileIndex, headBytes, tailBytes, timeoutMs }) {
159
+ return this.#caller.call(Command.PREFETCH_EDGES, {
160
+ sourceKey,
161
+ fileIndex,
162
+ headBytes,
163
+ tailBytes,
164
+ timeoutMs
165
+ });
166
+ }
167
+
168
+ /**
169
+ * Read a byte range as a stream.
170
+ *
171
+ * Returns immediately with a stream that fills as chunks arrive; cancelling it
172
+ * (viewer gone, seek superseded) stops the worker reading, so pieces are not
173
+ * fetched for a stream nobody will drain.
174
+ *
175
+ * @param {{ sourceKey: string, fileIndex: number, start?: number | null, end?: number | null }} params
176
+ * @returns {ReadableStream<Uint8Array>}
177
+ */
178
+ createReadStream({ sourceKey, fileIndex, start = null, end = null }) {
179
+ const readId = (this.#nextReadId += 1);
180
+ const receive = createReceiveStream({
181
+ port: this.#worker,
182
+ requestId: readId,
183
+ onCancel: () => {
184
+ void this.#caller.call(Command.CANCEL_READ, { readId }).catch(() => undefined);
185
+ this.#reads.delete(readId);
186
+ }
187
+ });
188
+ this.#reads.set(readId, receive);
189
+
190
+ // The worker replies to READ_RANGE only once the body is fully sent; a
191
+ // failure before that must surface on the stream, not vanish.
192
+ this.#worker.postMessage({
193
+ command: Command.READ_RANGE,
194
+ id: readId,
195
+ params: { sourceKey, fileIndex, start, end }
196
+ });
197
+
198
+ return receive.stream;
199
+ }
200
+
201
+ /**
202
+ * A stand-in for the WebTorrent torrent object, backed by the worker.
203
+ *
204
+ * Callers already hold a torrent and reach into `torrent.files[i]` — for the
205
+ * length, the name, or a read stream. Handing back an object of the same
206
+ * shape keeps every one of those call sites working unchanged, which matters:
207
+ * they are spread across the stream route, the subtitle route, the playback
208
+ * planner and the health report, and rewriting all of them to thread a
209
+ * `sourceKey` through would be a large change with nothing to show for it.
210
+ *
211
+ * Only what is actually used is provided. Anything else would be a promise we
212
+ * cannot keep — the real object lives on the other thread and its methods are
213
+ * not reachable from here.
214
+ *
215
+ * @param {{ sourceKey: string, sourceType: "magnet" | "torrent", source: string }} params
216
+ * @returns {Promise<{ infoHash: string, name: string, sourceKey: string, files: object[] }>}
217
+ */
218
+ async getTorrent({ sourceKey, sourceType, source }) {
219
+ const info = await this.addSource({ sourceKey, sourceType, source });
220
+ const client = this;
221
+ return {
222
+ infoHash: info.infoHash,
223
+ name: info.name,
224
+ // Carried so helpers that receive only the torrent can still name it to
225
+ // the worker.
226
+ sourceKey,
227
+ files: info.files.map((file) => ({
228
+ ...file,
229
+ /**
230
+ * @param {{ start?: number, end?: number }} [options]
231
+ * @returns {ReadableStream<Uint8Array>}
232
+ */
233
+ createReadStream(options = {}) {
234
+ // Node stream, not a web one: Fastify replies and the ffmpeg pipe
235
+ // both expect that shape, and every existing call site passes the
236
+ // result straight to one of them. `Readable.fromWeb` adds no copy —
237
+ // it wraps the same buffers.
238
+ return Readable.fromWeb(
239
+ client.createReadStream({
240
+ sourceKey,
241
+ fileIndex: file.index,
242
+ start: options.start ?? null,
243
+ end: options.end ?? null
244
+ })
245
+ );
246
+ }
247
+ }))
248
+ };
249
+ }
250
+
251
+ /**
252
+ * Shut the torrent client down and stop the thread.
253
+ *
254
+ * @returns {Promise<void>}
255
+ */
256
+ async destroyAll() {
257
+ try {
258
+ await this.#caller.call(Command.DESTROY_ALL, {});
259
+ } catch {
260
+ // Already gone — termination below is what matters.
261
+ }
262
+ await this.#worker.terminate();
263
+ }
264
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * @file Point `webrtc-polyfill` at the JavaScript WebRTC stack, in this thread only.
3
+ *
4
+ * Imported for its side effect, and imported FIRST by `worker.js` — ES module
5
+ * bodies run in import order, so registering the hook here happens before
6
+ * `torrent-pool.js` pulls in WebTorrent, which is what reaches
7
+ * `@thaunknown/simple-peer` and, through it, `webrtc-polyfill`.
8
+ *
9
+ * Scope is deliberately narrow. The hook lives in the worker's isolate, so the
10
+ * main thread keeps using node-datachannel directly for the browser's video
11
+ * channel — see `webrtc-shim.js` for why the two cannot share one process
12
+ * isolate at all.
13
+ */
14
+
15
+ import { registerHooks } from "node:module";
16
+
17
+ const SHIM_URL = new URL("./webrtc-shim.js", import.meta.url).href;
18
+
19
+ registerHooks({
20
+ /**
21
+ * @param {string} specifier
22
+ * @param {object} context
23
+ * @param {(specifier: string, context: object) => { url: string }} nextResolve
24
+ * @returns {{ url: string, shortCircuit?: boolean }}
25
+ */
26
+ resolve(specifier, context, nextResolve) {
27
+ if (specifier === "webrtc-polyfill") {
28
+ return { url: SHIM_URL, shortCircuit: true };
29
+ }
30
+ return nextResolve(specifier, context);
31
+ }
32
+ });