@blockcast/fec-worker 0.1.0 → 0.3.0-main.10140e847083
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 +28 -23
- package/dist/block-order.d.ts +22 -0
- package/dist/block-order.d.ts.map +1 -0
- package/dist/block-order.js +48 -0
- package/dist/block-order.js.map +1 -0
- package/dist/fec-worker-client.d.ts +23 -20
- package/dist/fec-worker-client.d.ts.map +1 -1
- package/dist/fec-worker-client.js +102 -54
- package/dist/fec-worker-client.js.map +1 -1
- package/dist/fec-worker-types.d.ts +162 -88
- package/dist/fec-worker-types.d.ts.map +1 -1
- package/dist/fec-worker-types.js +2 -2
- package/dist/fec-worker.js +328 -195
- package/dist/fec-worker.js.map +4 -4
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/shred-fec-worker.js +23 -2
- package/dist/shred-fec-worker.js.map +2 -2
- package/dist/shred-worker-types.d.ts +16 -1
- package/dist/shred-worker-types.d.ts.map +1 -1
- package/dist/shred-worker-types.js +16 -1
- package/dist/shred-worker-types.js.map +1 -1
- package/package.json +10 -8
- package/src/block-order.ts +59 -0
- package/src/fec-worker-client.ts +107 -56
- package/src/fec-worker-types.ts +161 -84
- package/src/fec-worker.ts +517 -468
- package/src/index.ts +3 -0
- package/src/shred-fec-worker.ts +34 -3
- package/src/shred-worker-types.ts +16 -1
- package/src/__tests__/fec-worker-alta.test.ts +0 -231
- package/src/__tests__/fec-worker-cleanup-integration.test.ts +0 -250
- package/src/__tests__/fec-worker-pending-queue.test.ts +0 -315
- package/src/__tests__/fec-worker-repair-size.test.ts +0 -1029
- package/src/__tests__/fec-worker-wasm-contract.test.ts +0 -84
- package/src/__tests__/shred-fec-worker.test.ts +0 -448
- package/src/fec-worker-client.test.ts +0 -530
- package/src/fec-worker-types.test.ts +0 -400
- package/src/shred-worker-client.test.ts +0 -182
- package/src/transfer.test.ts +0 -24
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
* path is bit-identical to the proven on-thread path.
|
|
27
27
|
*
|
|
28
28
|
* v3 (28-byte header):
|
|
29
|
-
* [0] version u8 (
|
|
29
|
+
* [0] version u8 (3 or 4; any other version is dropped, not misframed)
|
|
30
30
|
* [1-8] slot u64 LE
|
|
31
31
|
* [9-12] fecSetIndex u32 LE
|
|
32
32
|
* [13-16] localIndex u32 LE (data: 0..numData; coding: numData+position)
|
|
@@ -39,8 +39,23 @@
|
|
|
39
39
|
* DATA_COMPLETE (last data shred in the set) is passed to `add_shred` as its
|
|
40
40
|
* `lastInSet` argument — the WASM uses it to infer numData for a set that
|
|
41
41
|
* received only data shreds (no coding).
|
|
42
|
+
*
|
|
43
|
+
* v4 has the same 28-byte header, but its body is the full canonical Agave
|
|
44
|
+
* shred (signature, headers, erasure shard, chained Merkle root, proof, and
|
|
45
|
+
* the retransmitter signature on resigned variants) — the form a validator TVU
|
|
46
|
+
* accepts (BLO-41383). The worker recovers the exact v3 erasure-shard view
|
|
47
|
+
* from it with the forwarder's own `erasure_shard()` slice, so both versions
|
|
48
|
+
* reach `add_shred` byte-identical:
|
|
49
|
+
* variant = shred[64]; data 0x9_/0xb_, coding 0x6_/0x7_ (chained merkle;
|
|
50
|
+
* 0xb_/0x7_ resigned), must agree with IS_CODING_SHRED
|
|
51
|
+
* length = 1203 (data) / 1228 (coding), exactly
|
|
52
|
+
* shard start = 64 (data: after the signature) / 89 (coding headers)
|
|
53
|
+
* shard end = length - (32 root + 20 * (variant & 0x0f) proof
|
|
54
|
+
* + 64 retransmitter signature if resigned)
|
|
42
55
|
*/
|
|
43
56
|
export const SHRED_WIRE_VERSION = 3;
|
|
57
|
+
/** Wire version whose body is the full canonical shred (see above). */
|
|
58
|
+
export const SHRED_WIRE_VERSION_FULL = 4;
|
|
44
59
|
export const SHRED_HEADER_LEN = 28;
|
|
45
60
|
export const SHRED_FLAG_DATA_COMPLETE = 0x01;
|
|
46
61
|
export const SHRED_FLAG_IS_CODING = 0x02;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shred-worker-types.js","sourceRoot":"","sources":["../src/shred-worker-types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,+EAA+E;AAE/E
|
|
1
|
+
{"version":3,"file":"shred-worker-types.js","sourceRoot":"","sources":["../src/shred-worker-types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,+EAA+E;AAE/E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,CAAC;AACpC,uEAAuE;AACvE,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,CAAC;AACzC,MAAM,CAAC,MAAM,gBAAgB,GAAG,EAAE,CAAC;AACnC,MAAM,CAAC,MAAM,wBAAwB,GAAG,IAAI,CAAC;AAC7C,MAAM,CAAC,MAAM,oBAAoB,GAAG,IAAI,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blockcast/fec-worker",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Worker-per-track FEC decode with zero-copy ArrayBuffer transfer
|
|
3
|
+
"version": "0.3.0-main.10140e847083",
|
|
4
|
+
"description": "Worker-per-track FEC decode with zero-copy ArrayBuffer transfer",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -19,7 +19,12 @@
|
|
|
19
19
|
},
|
|
20
20
|
"files": [
|
|
21
21
|
"dist",
|
|
22
|
-
"src"
|
|
22
|
+
"src",
|
|
23
|
+
"!src/**/*.test.ts",
|
|
24
|
+
"!src/**/*.spec.ts",
|
|
25
|
+
"!src/**/__tests__",
|
|
26
|
+
"!dist/**/*.test.*",
|
|
27
|
+
"!dist/**/*.spec.*"
|
|
23
28
|
],
|
|
24
29
|
"scripts": {
|
|
25
30
|
"build": "tsc && node build.mjs",
|
|
@@ -29,9 +34,7 @@
|
|
|
29
34
|
"test": "vitest run",
|
|
30
35
|
"test:watch": "vitest"
|
|
31
36
|
},
|
|
32
|
-
"dependencies": {
|
|
33
|
-
"@blockcast/mmt-alta-parse": "^0.1.0"
|
|
34
|
-
},
|
|
37
|
+
"dependencies": {},
|
|
35
38
|
"devDependencies": {
|
|
36
39
|
"esbuild": "^0.28.0",
|
|
37
40
|
"typescript": "^5.3.0",
|
|
@@ -42,8 +45,7 @@
|
|
|
42
45
|
"worker",
|
|
43
46
|
"raptorq",
|
|
44
47
|
"zero-copy",
|
|
45
|
-
"transferable"
|
|
46
|
-
"alta"
|
|
48
|
+
"transferable"
|
|
47
49
|
],
|
|
48
50
|
"license": "Apache-2.0",
|
|
49
51
|
"repository": {
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Order of iso-ssbg1-v1 FEC blocks on the 32-bit SS_ID (ISO/IEC 23008-1:2023
|
|
3
|
+
* C.5.2/C.5.3; RFC 1982 serial arithmetic, SERIAL_BITS = 32; BLO-35597 iso:5).
|
|
4
|
+
* The K-stride (SBN * K) order of the retired k-padded-v1 profile is gone.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const SPAN = 0x1_0000_0000;
|
|
8
|
+
const HALF = 0x8000_0000;
|
|
9
|
+
|
|
10
|
+
// --- iso-ssbg1-v1 (ISO/IEC 23008-1 C.3.3 ssbg_mode1; C.5.2/C.5.3) ---------
|
|
11
|
+
//
|
|
12
|
+
// Under the ISO profile SS_ID increments by one per FEC source packet and
|
|
13
|
+
// wraps at 2^32, and a block is the SSB_length consecutive SS_IDs
|
|
14
|
+
// `[SS_Start, SS_Start + SSB_length)`. Blocks are keyed and ordered by their
|
|
15
|
+
// SS_Start, never by an SBN derived from K: the catalog K is only the maximum
|
|
16
|
+
// block length (kMax). The canonical helpers are @blockcast/mmt-base
|
|
17
|
+
// `serial32Distance` / `serial32Precedes` / `isoBlockContains` /
|
|
18
|
+
// `isoBlocksBehindLowerBound`; this bundle carries its own copies (it does not
|
|
19
|
+
// depend on mmt-base), and block-order.test.ts pins them to the same vectors.
|
|
20
|
+
|
|
21
|
+
/** Forward RFC 1982 distance from `from` to `to` on the 32-bit SS_ID, in 0..2^32-1. */
|
|
22
|
+
export function serialDistance(from: number, to: number): number {
|
|
23
|
+
return (to - from + SPAN) % SPAN;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Whether `a` precedes `b` under RFC 1982 serial comparison (SERIAL_BITS = 32). */
|
|
27
|
+
export function serialPrecedes(a: number, b: number): boolean {
|
|
28
|
+
const distance = serialDistance(a, b);
|
|
29
|
+
return distance !== 0 && distance < HALF;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Whether `ssId` is one of the `ssbLength` consecutive SS_IDs starting at `ssStart`, mod 2^32. */
|
|
33
|
+
export function isoBlockContains(ssStart: number, ssbLength: number, ssId: number): boolean {
|
|
34
|
+
return serialDistance(ssStart, ssId) < ssbLength;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Whether two SS_ID ranges `[start, start + length)` share any SS_ID, mod 2^32. */
|
|
38
|
+
export function isoBlocksOverlap(
|
|
39
|
+
aStart: number,
|
|
40
|
+
aLength: number,
|
|
41
|
+
bStart: number,
|
|
42
|
+
bLength: number,
|
|
43
|
+
): boolean {
|
|
44
|
+
return isoBlockContains(aStart, aLength, bStart) || isoBlockContains(bStart, bLength, aStart);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* How many blocks SS_ID `a` lies behind SS_ID `b` (0 unless `a` precedes `b`):
|
|
49
|
+
* a lower bound, because an iso-ssbg1-v1 block holds at most `kMax` source
|
|
50
|
+
* symbols, so `n * kMax` SS_IDs always span at least `n` blocks. This is the
|
|
51
|
+
* one interleave-horizon metric every ISO receiver layer shares (decoder
|
|
52
|
+
* retention, worker retirement, ordered-relay settlement).
|
|
53
|
+
*/
|
|
54
|
+
export function isoBlocksBehind(a: number, b: number, kMax: number): number {
|
|
55
|
+
if (!Number.isSafeInteger(kMax) || kMax <= 0) {
|
|
56
|
+
throw new Error(`iso-ssbg1-v1 block horizon needs a positive maximum K (got ${kMax})`);
|
|
57
|
+
}
|
|
58
|
+
return serialPrecedes(a, b) ? Math.floor(serialDistance(a, b) / kMax) : 0;
|
|
59
|
+
}
|
package/src/fec-worker-client.ts
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
|
|
11
11
|
import type {
|
|
12
12
|
FecWorkerCommand,
|
|
13
|
+
FecSymbolCommand,
|
|
13
14
|
FecWorkerEvent,
|
|
14
15
|
FecTrackConfig,
|
|
15
16
|
FecTrackStats,
|
|
@@ -23,7 +24,6 @@ export class FecWorkerClient {
|
|
|
23
24
|
#onFrame: ((data: ArrayBuffer, meta: FrameMeta) => void) | null = null;
|
|
24
25
|
#onSnapshot: ((stats: FecTrackStats) => void) | null = null;
|
|
25
26
|
#onBlockUpdate: ((block: FecBlockSnapshot) => void) | null = null;
|
|
26
|
-
#onGreenFill: ((sbn: number, data: ArrayBuffer) => void) | null = null;
|
|
27
27
|
#onCleanupComplete: ((sbn: number) => void) | null = null;
|
|
28
28
|
#onError: ((message: string, code?: FecWorkerErrorCode) => void) | null = null;
|
|
29
29
|
#cleanupInFlight: Set<number> | null = null;
|
|
@@ -31,6 +31,20 @@ export class FecWorkerClient {
|
|
|
31
31
|
#pendingResets = 0;
|
|
32
32
|
#resetFailed = false;
|
|
33
33
|
#disposed = false;
|
|
34
|
+
// Symbols fed during the current task, sent as one feedSymbols message
|
|
35
|
+
// when the task's microtasks run (BLO-36599). A packet stream delivers many
|
|
36
|
+
// symbols per task, and each postMessage is a structured-clone round trip
|
|
37
|
+
// on the page thread; the batch boundary is the task, not a size or timer.
|
|
38
|
+
#pendingSymbols: FecSymbolCommand[] = [];
|
|
39
|
+
#pendingTransfer: Transferable[] = [];
|
|
40
|
+
#flushQueued = false;
|
|
41
|
+
// Batching starts only once the Worker announced it accepts feedSymbols.
|
|
42
|
+
#batchSymbols = false;
|
|
43
|
+
// The Worker posts blockUpdate events only to a client that subscribed,
|
|
44
|
+
// which this client does once a callback is registered and the Worker
|
|
45
|
+
// announced the command (BLO-36599).
|
|
46
|
+
#blockUpdatesAnnounced = false;
|
|
47
|
+
#blockUpdatesSubscribed = false;
|
|
34
48
|
|
|
35
49
|
/**
|
|
36
50
|
* Create a FecWorkerClient.
|
|
@@ -54,62 +68,38 @@ export class FecWorkerClient {
|
|
|
54
68
|
|
|
55
69
|
/**
|
|
56
70
|
* Configure the Worker with FEC parameters and WASM module.
|
|
71
|
+
* `interleaveMs: 0` is a valid no-span catalog value; in that mode the
|
|
72
|
+
* caller supplies a positive receiver-local `deliveryWindowMs`.
|
|
57
73
|
* wasmModule is transferred (not neutered — WebAssembly.Module is shareable).
|
|
58
|
-
* altaPublicKey is PKCS#8 SPKI DER bytes (self-describing — algorithm detected from OID).
|
|
59
|
-
* Transferred (detached from sender).
|
|
60
|
-
* altaWasmModule is structured-cloneable (NOT transferable — do not add to transfer list).
|
|
61
74
|
*/
|
|
62
75
|
configure(
|
|
63
76
|
config: FecTrackConfig,
|
|
64
77
|
wasmModule: WebAssembly.Module,
|
|
65
|
-
altaPublicKey?: ArrayBuffer,
|
|
66
|
-
altaWasmModule?: WebAssembly.Module,
|
|
67
78
|
): void {
|
|
68
79
|
if (this.#disposed) throw new Error("[FecWorkerClient] disposed");
|
|
80
|
+
this.#flushSymbols();
|
|
69
81
|
const cmd: FecWorkerCommand = {
|
|
70
82
|
type: "configure",
|
|
71
83
|
config,
|
|
72
84
|
wasmModule,
|
|
73
|
-
altaPublicKey,
|
|
74
|
-
altaWasmModule,
|
|
75
85
|
};
|
|
76
86
|
// WebAssembly.Module is structured-cloneable, NOT transferable.
|
|
77
87
|
// Pass it in the message body (Chrome clones it automatically).
|
|
78
|
-
|
|
79
|
-
const transferList: Transferable[] = [];
|
|
80
|
-
if (altaPublicKey) transferList.push(altaPublicKey);
|
|
81
|
-
// altaWasmModule is structured-cloneable — NOT added to transferList
|
|
82
|
-
this.#worker.postMessage(cmd, transferList);
|
|
88
|
+
this.#worker.postMessage(cmd);
|
|
83
89
|
}
|
|
84
90
|
|
|
85
91
|
/**
|
|
86
92
|
* Feed a source symbol to the Worker. data ArrayBuffer is transferred (zero-copy).
|
|
87
|
-
* Caller must not access data after this call — it is
|
|
88
|
-
*
|
|
89
|
-
* Optional `alta` carries the detached ALTA authenticator bytes and the
|
|
90
|
-
* byte offset in `data` where the signed payload ends. The auth buffer is
|
|
91
|
-
* transferred zero-copy alongside `data`. Use {@link getMmtpAltaTrailer}
|
|
92
|
-
* upstream (mmtp-router) to extract it from the wire packet.
|
|
93
|
+
* Caller must not access data after this call — it is transferred with the
|
|
94
|
+
* task's symbol batch, before any later command of this client.
|
|
93
95
|
*/
|
|
94
96
|
feedSource(
|
|
95
97
|
ssId: number,
|
|
96
98
|
data: ArrayBuffer,
|
|
97
99
|
ts: number,
|
|
98
|
-
alta?: { auth: ArrayBuffer; payloadEnd: number },
|
|
99
100
|
): void {
|
|
100
|
-
if (this.#disposed)
|
|
101
|
-
|
|
102
|
-
? {
|
|
103
|
-
type: "feedSource",
|
|
104
|
-
ssId,
|
|
105
|
-
data,
|
|
106
|
-
ts,
|
|
107
|
-
altaAuth: alta.auth,
|
|
108
|
-
altaPayloadEnd: alta.payloadEnd,
|
|
109
|
-
}
|
|
110
|
-
: { type: "feedSource", ssId, data, ts };
|
|
111
|
-
const transfer: Transferable[] = alta ? [data, alta.auth] : [data];
|
|
112
|
-
this.#worker.postMessage(cmd, transfer);
|
|
101
|
+
if (this.#disposed) throw new Error("[FecWorkerClient] feedSource on a disposed client");
|
|
102
|
+
this.#queueSymbol({ type: "feedSource", ssId, data, ts }, data);
|
|
113
103
|
}
|
|
114
104
|
|
|
115
105
|
/**
|
|
@@ -117,8 +107,8 @@ export class FecWorkerClient {
|
|
|
117
107
|
* Caller must not access data after this call — it is detached.
|
|
118
108
|
*
|
|
119
109
|
* Carries the wire-level Repair FEC Payload ID fields (ISO 23008-1 §C.5.3):
|
|
120
|
-
* - ssStart: SS_ID of first source symbol in the block (
|
|
121
|
-
* - ssbLength:
|
|
110
|
+
* - ssStart: SS_ID of first source symbol in the block (any uint32; the block key)
|
|
111
|
+
* - ssbLength: authored source-symbol count (1..configured K)
|
|
122
112
|
* - rsId: repair symbol index within the block
|
|
123
113
|
*/
|
|
124
114
|
feedRepair(
|
|
@@ -128,17 +118,38 @@ export class FecWorkerClient {
|
|
|
128
118
|
data: ArrayBuffer,
|
|
129
119
|
ts: number,
|
|
130
120
|
): void {
|
|
131
|
-
if (this.#disposed)
|
|
121
|
+
if (this.#disposed) throw new Error("[FecWorkerClient] feedRepair on a disposed client");
|
|
122
|
+
this.#queueSymbol({ type: "feedRepair", ssStart, ssbLength, rsId, data, ts }, data);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
#queueSymbol(symbol: FecSymbolCommand, data: ArrayBuffer): void {
|
|
126
|
+
if (!this.#batchSymbols) {
|
|
127
|
+
this.#worker.postMessage(symbol, [data]);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
this.#pendingSymbols.push(symbol);
|
|
131
|
+
this.#pendingTransfer.push(data);
|
|
132
|
+
if (this.#flushQueued) return;
|
|
133
|
+
this.#flushQueued = true;
|
|
134
|
+
queueMicrotask(() => {
|
|
135
|
+
this.#flushQueued = false;
|
|
136
|
+
this.#flushSymbols();
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Send the symbols fed so far. Every other command calls this first, so a
|
|
142
|
+
* cleanup barrier, reset or signature still follows all earlier symbols.
|
|
143
|
+
*/
|
|
144
|
+
#flushSymbols(): void {
|
|
145
|
+
if (this.#pendingSymbols.length === 0 || this.#disposed) return;
|
|
146
|
+
const symbols = this.#pendingSymbols;
|
|
147
|
+
const transfer = this.#pendingTransfer;
|
|
148
|
+
this.#pendingSymbols = [];
|
|
149
|
+
this.#pendingTransfer = [];
|
|
132
150
|
this.#worker.postMessage(
|
|
133
|
-
{
|
|
134
|
-
|
|
135
|
-
ssStart,
|
|
136
|
-
ssbLength,
|
|
137
|
-
rsId,
|
|
138
|
-
data,
|
|
139
|
-
ts,
|
|
140
|
-
} satisfies FecWorkerCommand,
|
|
141
|
-
[data],
|
|
151
|
+
{ type: "feedSymbols", symbols } satisfies FecWorkerCommand,
|
|
152
|
+
transfer,
|
|
142
153
|
);
|
|
143
154
|
}
|
|
144
155
|
|
|
@@ -146,7 +157,8 @@ export class FecWorkerClient {
|
|
|
146
157
|
* Submit relay block signature for verification. sig and hash are transferred.
|
|
147
158
|
*/
|
|
148
159
|
relayBlockSig(sbn: number, sig: ArrayBuffer, hash: ArrayBuffer): void {
|
|
149
|
-
if (this.#disposed)
|
|
160
|
+
if (this.#disposed) throw new Error("[FecWorkerClient] relayBlockSig on a disposed client");
|
|
161
|
+
this.#flushSymbols();
|
|
150
162
|
this.#worker.postMessage(
|
|
151
163
|
{ type: "relayBlockSig", sbn, sig, hash } satisfies FecWorkerCommand,
|
|
152
164
|
[sig, hash],
|
|
@@ -154,13 +166,14 @@ export class FecWorkerClient {
|
|
|
154
166
|
}
|
|
155
167
|
|
|
156
168
|
/**
|
|
157
|
-
* Trigger cleanup after a block's inactivity deadline.
|
|
169
|
+
* Trigger cleanup after a block's inactivity deadline. `currentSbn` is the
|
|
170
|
+
* block key, the block's SS_Start.
|
|
158
171
|
*
|
|
159
172
|
* While one queue-drain barrier is in flight, later SBNs are coalesced into
|
|
160
173
|
* the next command without losing their individual acknowledgement identity.
|
|
161
174
|
*/
|
|
162
175
|
cleanup(currentSbn: number): void {
|
|
163
|
-
if (this.#disposed)
|
|
176
|
+
if (this.#disposed) throw new Error("[FecWorkerClient] cleanup on a disposed client");
|
|
164
177
|
if (
|
|
165
178
|
!Number.isInteger(currentSbn) ||
|
|
166
179
|
currentSbn < 0 ||
|
|
@@ -177,7 +190,8 @@ export class FecWorkerClient {
|
|
|
177
190
|
|
|
178
191
|
/** Reset this track's decoder epoch while preserving Worker configuration. */
|
|
179
192
|
reset(): void {
|
|
180
|
-
if (this.#disposed)
|
|
193
|
+
if (this.#disposed) throw new Error("[FecWorkerClient] reset on a disposed client");
|
|
194
|
+
this.#flushSymbols();
|
|
181
195
|
this.#pendingResets++;
|
|
182
196
|
this.#cleanupInFlight = null;
|
|
183
197
|
this.#queuedCleanupSbns.clear();
|
|
@@ -185,6 +199,9 @@ export class FecWorkerClient {
|
|
|
185
199
|
}
|
|
186
200
|
|
|
187
201
|
#postCleanup(sbns: Set<number>): void {
|
|
202
|
+
// The cleanup is a queue-drain barrier: every symbol fed before it is
|
|
203
|
+
// posted must reach the Worker first.
|
|
204
|
+
this.#flushSymbols();
|
|
188
205
|
this.#cleanupInFlight = sbns;
|
|
189
206
|
const orderedSbns = [...sbns].sort((a, b) => a - b);
|
|
190
207
|
this.#worker.postMessage(
|
|
@@ -202,14 +219,28 @@ export class FecWorkerClient {
|
|
|
202
219
|
this.#onSnapshot = cb;
|
|
203
220
|
}
|
|
204
221
|
|
|
205
|
-
/**
|
|
222
|
+
/**
|
|
223
|
+
* Register callback for individual block state changes. The Worker posts
|
|
224
|
+
* them only once asked, so registering subscribes (after the Worker
|
|
225
|
+
* announces `subscribeBlockUpdates`). An older Worker that does not
|
|
226
|
+
* announce it posts them unasked.
|
|
227
|
+
*/
|
|
206
228
|
onBlockUpdate(cb: (block: FecBlockSnapshot) => void): void {
|
|
207
229
|
this.#onBlockUpdate = cb;
|
|
230
|
+
this.#subscribeBlockUpdates();
|
|
208
231
|
}
|
|
209
232
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
233
|
+
#subscribeBlockUpdates(): void {
|
|
234
|
+
if (
|
|
235
|
+
this.#disposed ||
|
|
236
|
+
this.#blockUpdatesSubscribed ||
|
|
237
|
+
!this.#blockUpdatesAnnounced ||
|
|
238
|
+
!this.#onBlockUpdate
|
|
239
|
+
) return;
|
|
240
|
+
this.#blockUpdatesSubscribed = true;
|
|
241
|
+
this.#worker.postMessage(
|
|
242
|
+
{ type: "subscribeBlockUpdates" } satisfies FecWorkerCommand,
|
|
243
|
+
);
|
|
213
244
|
}
|
|
214
245
|
|
|
215
246
|
/** Register callback fired after the Worker drains all commands queued before cleanup. */
|
|
@@ -222,6 +253,16 @@ export class FecWorkerClient {
|
|
|
222
253
|
this.#onError = cb;
|
|
223
254
|
}
|
|
224
255
|
|
|
256
|
+
/**
|
|
257
|
+
* True once dispose() ran. A disposed client cannot decode, fill, or
|
|
258
|
+
* acknowledge anything, so routing symbols to it discards them; every
|
|
259
|
+
* command method (feedSource, feedRepair, relayBlockSig, cleanup, reset)
|
|
260
|
+
* throws rather than pretend otherwise. Callers check this first.
|
|
261
|
+
*/
|
|
262
|
+
get disposed(): boolean {
|
|
263
|
+
return this.#disposed;
|
|
264
|
+
}
|
|
265
|
+
|
|
225
266
|
/**
|
|
226
267
|
* Dispose the Worker. Sends dispose command then terminates.
|
|
227
268
|
* Idempotent — safe to call multiple times.
|
|
@@ -229,6 +270,9 @@ export class FecWorkerClient {
|
|
|
229
270
|
dispose(): void {
|
|
230
271
|
if (this.#disposed) return;
|
|
231
272
|
this.#disposed = true;
|
|
273
|
+
// A terminated Worker decodes nothing; symbols still batched are dropped.
|
|
274
|
+
this.#pendingSymbols = [];
|
|
275
|
+
this.#pendingTransfer = [];
|
|
232
276
|
this.#cleanupInFlight = null;
|
|
233
277
|
this.#queuedCleanupSbns.clear();
|
|
234
278
|
this.#worker.postMessage({ type: "dispose" } satisfies FecWorkerCommand);
|
|
@@ -236,6 +280,16 @@ export class FecWorkerClient {
|
|
|
236
280
|
}
|
|
237
281
|
|
|
238
282
|
#handleEvent(event: FecWorkerEvent): void {
|
|
283
|
+
if (event.type === "capabilities") {
|
|
284
|
+
if (Array.isArray(event.commands) && event.commands.includes("feedSymbols")) {
|
|
285
|
+
this.#batchSymbols = true;
|
|
286
|
+
}
|
|
287
|
+
if (Array.isArray(event.commands) && event.commands.includes("subscribeBlockUpdates")) {
|
|
288
|
+
this.#blockUpdatesAnnounced = true;
|
|
289
|
+
this.#subscribeBlockUpdates();
|
|
290
|
+
}
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
239
293
|
if (event.type === "resetComplete") {
|
|
240
294
|
this.#pendingResets = Math.max(0, this.#pendingResets - 1);
|
|
241
295
|
this.#resetFailed = false;
|
|
@@ -261,9 +315,6 @@ export class FecWorkerClient {
|
|
|
261
315
|
case "blockUpdate":
|
|
262
316
|
this.#onBlockUpdate?.(event.block);
|
|
263
317
|
break;
|
|
264
|
-
case "greenFill":
|
|
265
|
-
this.#onGreenFill?.(event.sbn, event.data);
|
|
266
|
-
break;
|
|
267
318
|
case "cleanupComplete":
|
|
268
319
|
for (const sbn of event.sbns) {
|
|
269
320
|
this.#onCleanupComplete?.(sbn);
|