@leofcoin/workers 1.5.44 → 1.6.0

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.
@@ -0,0 +1,672 @@
1
+ class EasyWorker {
2
+ #messageEvent = 'message';
3
+ #errorEvent = 'error';
4
+ #isBrowser = false;
5
+ #isWorker = false;
6
+ #messageHandler = null;
7
+ #errorHandler = null;
8
+ worker;
9
+ get isWorker() {
10
+ return this.#isWorker;
11
+ }
12
+ constructor(url, options) {
13
+ return this.#init(url, options);
14
+ }
15
+ #init(url, options = {}) {
16
+ if (url) {
17
+ if (globalThis.Worker) {
18
+ this.#isBrowser = true;
19
+ this.worker = new Worker(url, { ...options });
20
+ }
21
+ else {
22
+ return new Promise((resolve, reject) => {
23
+ import('child_process').then(({ fork }) => {
24
+ const child = fork(url, ['easy-worker-child'], options);
25
+ child.once('error', reject);
26
+ this.worker = child;
27
+ resolve(this);
28
+ }).catch(reject);
29
+ });
30
+ }
31
+ }
32
+ else {
33
+ this.#isWorker = true;
34
+ if (globalThis.process?.argv[2] === 'easy-worker-child') {
35
+ this.worker = process;
36
+ }
37
+ else {
38
+ this.#isBrowser = true;
39
+ this.worker = globalThis;
40
+ }
41
+ }
42
+ return this;
43
+ }
44
+ onmessage(fn) {
45
+ if (this.#isBrowser) {
46
+ this.worker.onmessage = ({ data }) => fn(data);
47
+ }
48
+ else {
49
+ if (this.#messageHandler)
50
+ this.worker.off(this.#messageEvent, this.#messageHandler);
51
+ this.#messageHandler = fn;
52
+ this.worker.on(this.#messageEvent, fn);
53
+ }
54
+ }
55
+ postMessage(message) {
56
+ if (this.#isBrowser)
57
+ this.worker.postMessage(message);
58
+ else
59
+ this.worker.send(message);
60
+ }
61
+ terminate() {
62
+ if (this.#isBrowser)
63
+ this.worker.terminate();
64
+ else
65
+ this.worker.kill();
66
+ }
67
+ onerror(fn) {
68
+ if (this.#isBrowser) {
69
+ this.worker.onerror = fn;
70
+ }
71
+ else {
72
+ if (this.#errorHandler)
73
+ this.worker.off(this.#errorEvent, this.#errorHandler);
74
+ this.#errorHandler = fn;
75
+ this.worker.on(this.#errorEvent, fn);
76
+ }
77
+ }
78
+ /**
79
+ *
80
+ * @param {*} data
81
+ * @returns {Promise} resolves result onmessage & rejects on error
82
+ */
83
+ once(data) {
84
+ return new Promise((resolve, reject) => {
85
+ if (this.#isBrowser) {
86
+ const handleMessage = ({ data: result }) => {
87
+ this.worker.onerror = null;
88
+ resolve(result);
89
+ this.terminate();
90
+ };
91
+ const handleError = (error) => {
92
+ this.worker.onmessage = null;
93
+ reject(error);
94
+ this.terminate();
95
+ };
96
+ this.worker.onmessage = handleMessage;
97
+ this.worker.onerror = handleError;
98
+ }
99
+ else {
100
+ const handleMessage = (result) => {
101
+ this.worker.off(this.#errorEvent, handleError);
102
+ resolve(result);
103
+ this.terminate();
104
+ };
105
+ const handleError = (error) => {
106
+ this.worker.off(this.#messageEvent, handleMessage);
107
+ reject(error);
108
+ this.terminate();
109
+ };
110
+ this.worker.once(this.#messageEvent, handleMessage);
111
+ this.worker.once(this.#errorEvent, handleError);
112
+ }
113
+ this.postMessage(data);
114
+ });
115
+ }
116
+ }
117
+
118
+ /** @leofcoin/binary: dependency-free unsigned framing, with no global byte counters. */
119
+ const MAX_BIGINT = (1n << 256n) - 1n;
120
+ function size(value) {
121
+ if (!Number.isSafeInteger(value) || value < 0)
122
+ throw new RangeError('Expected an unsigned safe integer');
123
+ }
124
+ function bytes(value) {
125
+ if (!(value instanceof Uint8Array))
126
+ throw new TypeError('Expected Uint8Array');
127
+ }
128
+ function varUintLength(value) {
129
+ size(value);
130
+ let length = 1;
131
+ while (value >= 128) {
132
+ value = Math.floor(value / 128);
133
+ length++;
134
+ }
135
+ return length;
136
+ }
137
+ function varBigIntLength(value) {
138
+ if (typeof value !== 'bigint')
139
+ throw new TypeError('Expected BigInt');
140
+ if (value < 0n || value > MAX_BIGINT)
141
+ throw new RangeError('Expected an unsigned 256-bit integer');
142
+ let length = 1;
143
+ // Count whole eight-byte varint groups before the short tail.
144
+ while (value >= 0x100000000000000n) {
145
+ value >>= 56n;
146
+ length += 8;
147
+ }
148
+ while (value >= 128n) {
149
+ value >>= 7n;
150
+ length++;
151
+ }
152
+ return length;
153
+ }
154
+ /** Fixed-capacity writer. finish() is a view; copy it before reusing/resetting the writer. */
155
+ class BinaryWriter {
156
+ #buffer;
157
+ #position = 0;
158
+ constructor(capacity = 64) {
159
+ if (typeof capacity === 'number') {
160
+ size(capacity);
161
+ this.#buffer = new Uint8Array(capacity);
162
+ }
163
+ else {
164
+ bytes(capacity);
165
+ this.#buffer = capacity;
166
+ }
167
+ }
168
+ get position() {
169
+ return this.#position;
170
+ }
171
+ get remaining() {
172
+ return this.#buffer.length - this.#position;
173
+ }
174
+ #require(length) {
175
+ size(length);
176
+ if (length > this.remaining)
177
+ throw new RangeError('Binary output buffer is too small');
178
+ }
179
+ writeVarUint(value) {
180
+ this.#require(varUintLength(value));
181
+ this.#writeVarUint(value);
182
+ return this;
183
+ }
184
+ #writeVarUint(value) {
185
+ while (value >= 128) {
186
+ this.#buffer[this.#position++] = value % 128 | 128;
187
+ value = Math.floor(value / 128);
188
+ }
189
+ this.#buffer[this.#position++] = value;
190
+ }
191
+ writeVarBigInt(value) {
192
+ this.#require(varBigIntLength(value));
193
+ do {
194
+ const byte = Number(value & 127n);
195
+ value >>= 7n;
196
+ this.#buffer[this.#position++] = byte | (value ? 128 : 0);
197
+ } while (value);
198
+ return this;
199
+ }
200
+ writeBytes(value) {
201
+ bytes(value);
202
+ this.#require(value.length);
203
+ this.#buffer.set(value, this.#position);
204
+ this.#position += value.length;
205
+ return this;
206
+ }
207
+ writeFrame(value) {
208
+ bytes(value);
209
+ // Check the complete operation before writing its length prefix.
210
+ const prefixLength = varUintLength(value.length);
211
+ this.#require(prefixLength + value.length);
212
+ const prefixStart = this.#buffer.byteOffset + this.#position;
213
+ if (value.buffer === this.#buffer.buffer &&
214
+ value.byteOffset < prefixStart + prefixLength &&
215
+ value.byteOffset + value.length > prefixStart)
216
+ throw new RangeError('Frame source overlaps its length prefix');
217
+ this.#writeVarUint(value.length);
218
+ this.#buffer.set(value, this.#position);
219
+ this.#position += value.length;
220
+ return this;
221
+ }
222
+ reset() {
223
+ this.#position = 0;
224
+ return this;
225
+ }
226
+ finish(copy = false) {
227
+ const result = this.#buffer.subarray(0, this.#position);
228
+ // Buffer.slice() aliases memory; this constructor copies every Uint8Array subtype.
229
+ return copy ? new Uint8Array(result) : result;
230
+ }
231
+ }
232
+ /** Reads views of the original buffer. Failed operations leave the cursor unchanged. */
233
+ class BinaryReader {
234
+ #buffer;
235
+ #position = 0;
236
+ constructor(buffer) {
237
+ bytes(buffer);
238
+ this.#buffer = buffer;
239
+ }
240
+ get position() {
241
+ return this.#position;
242
+ }
243
+ get remaining() {
244
+ return this.#buffer.length - this.#position;
245
+ }
246
+ readVarUint(canonical = true) {
247
+ let result = 0;
248
+ let multiplier = 1;
249
+ for (let index = 0; index < 8; index++) {
250
+ const cursor = this.#position + index;
251
+ if (cursor >= this.#buffer.length)
252
+ throw new RangeError('Truncated varint');
253
+ const byte = this.#buffer[cursor];
254
+ const payload = byte & 127;
255
+ if (index === 7 && payload > Math.floor((Number.MAX_SAFE_INTEGER - result) / multiplier))
256
+ throw new RangeError('Varint exceeds safe integer range');
257
+ result += payload * multiplier;
258
+ if (byte < 128) {
259
+ if (canonical && index > 0 && payload === 0)
260
+ throw new RangeError('Non-canonical varint');
261
+ this.#position = cursor + 1;
262
+ return result;
263
+ }
264
+ multiplier *= 128;
265
+ }
266
+ throw new RangeError('Varint exceeds safe integer range');
267
+ }
268
+ readVarBigInt(canonical = true) {
269
+ let result = 0n;
270
+ for (let index = 0; index < 37; index++) {
271
+ const cursor = this.#position + index;
272
+ if (cursor >= this.#buffer.length)
273
+ throw new RangeError('Truncated varint');
274
+ const byte = this.#buffer[cursor];
275
+ result |= BigInt(byte & 127) << BigInt(index * 7);
276
+ if (result > MAX_BIGINT)
277
+ throw new RangeError('Varint exceeds 256 bits');
278
+ if (byte < 128) {
279
+ if (canonical && index > 0 && byte === 0)
280
+ throw new RangeError('Non-canonical varint');
281
+ this.#position = cursor + 1;
282
+ return result;
283
+ }
284
+ }
285
+ throw new RangeError('Varint exceeds 256 bits');
286
+ }
287
+ readBytes(length) {
288
+ size(length);
289
+ if (length > this.remaining)
290
+ throw new RangeError('Truncated binary field');
291
+ const start = this.#position;
292
+ this.#position += length;
293
+ return this.#buffer.subarray(start, this.#position);
294
+ }
295
+ readFrame(canonical = true) {
296
+ const start = this.#position;
297
+ try {
298
+ return this.readBytes(this.readVarUint(canonical));
299
+ }
300
+ catch (error) {
301
+ this.#position = start;
302
+ throw error;
303
+ }
304
+ }
305
+ }
306
+ /** Existing smart-concat bytes, with exactly one output allocation and no prefix arrays. */
307
+ function encodeFrames(frames, prefix) {
308
+ if (!Array.isArray(frames))
309
+ throw new TypeError('Expected an array of byte frames');
310
+ let length = 0;
311
+ for (const frame of frames) {
312
+ bytes(frame);
313
+ length += varUintLength(frame.length) + frame.length;
314
+ size(length);
315
+ }
316
+ const writer = new BinaryWriter(length);
317
+ for (const frame of frames)
318
+ writer.writeFrame(frame);
319
+ return writer.finish();
320
+ }
321
+ /** Iterative framing: bounded by the input length, not recursive stack depth. */
322
+ function decodeFrames(buffer, prefixLength = 0, options = {}) {
323
+ const reader = new BinaryReader(buffer);
324
+ reader.readBytes(prefixLength);
325
+ const maxFrames = options.maxFrames ?? buffer.length;
326
+ size(maxFrames);
327
+ const frames = [];
328
+ while (reader.remaining) {
329
+ if (frames.length >= maxFrames)
330
+ throw new RangeError('Too many binary frames');
331
+ frames.push(reader.readFrame(options.canonical ?? true));
332
+ }
333
+ return frames;
334
+ }
335
+
336
+ const MAGIC = new Uint8Array([0x4c, 0x46, 0x43, 1]);
337
+ const utf8 = new TextEncoder();
338
+ const text = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true });
339
+ const defaults = { maxBytes: 16 * 1024 * 1024, maxDepth: 64, maxNodes: 200000 };
340
+ const BIGINT_LIMB = 1n << 49n;
341
+ const BIGINT_MASK = BIGINT_LIMB - 1n;
342
+ const limitsFor = (options) => {
343
+ const limits = { ...defaults, ...options };
344
+ for (const value of Object.values(limits))
345
+ if (!Number.isSafeInteger(value) || value < 1)
346
+ throw new RangeError('Invalid binary limits');
347
+ return limits;
348
+ };
349
+ /** Canonical UTF-8 length without allocating a temporary encoded string. */
350
+ function stringLength(value, maximum) {
351
+ if (value.length > maximum)
352
+ throw new RangeError('Invalid binary string');
353
+ let length = value.length;
354
+ for (let index = 0; index < value.length; index++) {
355
+ const code = value.charCodeAt(index);
356
+ if (code < 0x80)
357
+ continue;
358
+ if (code < 0x800) {
359
+ length++;
360
+ continue;
361
+ }
362
+ if (code >= 0xd800 && code <= 0xdbff) {
363
+ const low = value.charCodeAt(++index);
364
+ if (!(low >= 0xdc00 && low <= 0xdfff))
365
+ throw new RangeError('Invalid binary string');
366
+ length += 2;
367
+ }
368
+ else {
369
+ if (code >= 0xdc00 && code <= 0xdfff)
370
+ throw new RangeError('Invalid binary string');
371
+ length += 2;
372
+ }
373
+ }
374
+ return length;
375
+ }
376
+ /** Versioned canonical typed values. Validation/planning allocates no byte buffers. */
377
+ function encodeValue(value, options = {}) {
378
+ return encodeValueWithPrefix(value, EMPTY, options);
379
+ }
380
+ const EMPTY = new Uint8Array();
381
+ /** Prepend an existing framing/codec header in the same allocation.
382
+ * maxBytes bounds the typed value (including MAGIC), as with encodeValue.
383
+ * The caller-owned prefix is separate framing and is copied, never retained.
384
+ */
385
+ function encodeValueWithPrefix(value, prefix, options = {}) {
386
+ if (!(prefix instanceof Uint8Array))
387
+ throw new TypeError('Expected Uint8Array prefix');
388
+ const limits = limitsFor(options);
389
+ // Flat plan retains validated data-property values; writing never reads user properties again.
390
+ // Internal tag 11 is an untagged map key and is never emitted on the wire.
391
+ const plan = [];
392
+ const ancestors = new Set();
393
+ let size = MAGIC.length;
394
+ let nodes = 0;
395
+ const reserve = (length) => {
396
+ size += length;
397
+ if (size > limits.maxBytes)
398
+ throw new RangeError('Binary value exceeds byte limit');
399
+ };
400
+ const string = (value, tag) => {
401
+ const length = stringLength(value, limits.maxBytes);
402
+ reserve((tag === 7 ? 1 : 0) + varUintLength(length) + length);
403
+ plan.push(tag, value, length);
404
+ };
405
+ const visit = (value, depth) => {
406
+ if (++nodes > limits.maxNodes || depth > limits.maxDepth)
407
+ throw new RangeError('Binary value exceeds complexity limit');
408
+ if (value === undefined || value === null || typeof value === 'boolean') {
409
+ reserve(1);
410
+ plan.push(value === undefined ? 0 : value === null ? 1 : value === false ? 2 : 3);
411
+ return;
412
+ }
413
+ if (typeof value === 'number') {
414
+ if (!Number.isFinite(value))
415
+ throw new TypeError('Binary numbers must be finite');
416
+ reserve(9);
417
+ plan.push(4, value);
418
+ return;
419
+ }
420
+ if (typeof value === 'bigint') {
421
+ const magnitude = value < 0n ? -value : value;
422
+ reserve(1 + varBigIntLength(magnitude));
423
+ plan.push(value < 0n ? 6 : 5, magnitude);
424
+ return;
425
+ }
426
+ if (typeof value === 'string')
427
+ return string(value, 7);
428
+ if (value instanceof Uint8Array) {
429
+ reserve(1 + varUintLength(value.length) + value.length);
430
+ plan.push(8, value, value.length);
431
+ return;
432
+ }
433
+ if (typeof value !== 'object')
434
+ throw new TypeError('Unsupported binary value');
435
+ if (ancestors.has(value))
436
+ throw new TypeError('Cyclic binary value');
437
+ ancestors.add(value);
438
+ try {
439
+ if (Array.isArray(value)) {
440
+ if (value.length > limits.maxNodes - nodes)
441
+ throw new RangeError('Binary array exceeds node limit');
442
+ if (Object.keys(value).length !== value.length || Object.getOwnPropertySymbols(value).length)
443
+ throw new TypeError('Binary arrays cannot have extra properties or holes');
444
+ reserve(1 + varUintLength(value.length));
445
+ plan.push(9, value.length);
446
+ for (let index = 0; index < value.length; index++) {
447
+ const entry = Object.getOwnPropertyDescriptor(value, String(index));
448
+ if (!entry || !('value' in entry))
449
+ throw new TypeError('Sparse arrays and accessors are not binary values');
450
+ visit(entry.value, depth + 1);
451
+ }
452
+ }
453
+ else {
454
+ const prototype = Object.getPrototypeOf(value);
455
+ if (prototype !== Object.prototype && prototype !== null)
456
+ throw new TypeError('Binary maps must be plain objects');
457
+ const keys = Object.keys(value).sort();
458
+ if (Object.getOwnPropertySymbols(value).length)
459
+ throw new TypeError('Symbol keys are not binary values');
460
+ if (keys.length > limits.maxNodes - nodes)
461
+ throw new RangeError('Binary map exceeds node limit');
462
+ reserve(1 + varUintLength(keys.length));
463
+ plan.push(10, keys.length);
464
+ for (const key of keys) {
465
+ const entry = Object.getOwnPropertyDescriptor(value, key);
466
+ if (!('value' in entry))
467
+ throw new TypeError('Accessors are not binary values');
468
+ string(key, 11);
469
+ visit(entry.value, depth + 1);
470
+ }
471
+ }
472
+ }
473
+ finally {
474
+ ancestors.delete(value);
475
+ }
476
+ };
477
+ visit(value, 0);
478
+ const capacity = prefix.length + size;
479
+ if (!Number.isSafeInteger(capacity))
480
+ throw new RangeError('Binary output is too large');
481
+ const output = new Uint8Array(capacity);
482
+ output.set(prefix);
483
+ output.set(MAGIC, prefix.length);
484
+ let position = prefix.length + MAGIC.length;
485
+ let view;
486
+ const uint = (value) => {
487
+ while (value >= 128) {
488
+ output[position++] = value % 128 | 128;
489
+ value = Math.floor(value / 128);
490
+ }
491
+ output[position++] = value;
492
+ };
493
+ for (let cursor = 0; cursor < plan.length;) {
494
+ const tag = plan[cursor++];
495
+ if (tag !== 11)
496
+ output[position++] = tag;
497
+ if (tag < 4)
498
+ continue;
499
+ if (tag === 4) {
500
+ view ??= new DataView(output.buffer);
501
+ view.setFloat64(position, plan[cursor++], false);
502
+ position += 8;
503
+ }
504
+ else if (tag === 5 || tag === 6) {
505
+ let value = plan[cursor++];
506
+ // Seven varint bytes fit exactly in a safe-integer Number (49 bits).
507
+ // Extract one BigInt limb rather than shifting/masking BigInt for every byte.
508
+ while (value >= BIGINT_LIMB) {
509
+ let limb = Number(value & BIGINT_MASK);
510
+ for (let index = 0; index < 7; index++) {
511
+ output[position++] = limb % 128 | 128;
512
+ limb = Math.floor(limb / 128);
513
+ }
514
+ value >>= 49n;
515
+ }
516
+ uint(Number(value));
517
+ }
518
+ else if (tag === 7 || tag === 11) {
519
+ const value = plan[cursor++];
520
+ const length = plan[cursor++];
521
+ uint(length);
522
+ if (length === value.length) {
523
+ for (let index = 0; index < length; index++)
524
+ output[position++] = value.charCodeAt(index);
525
+ }
526
+ else {
527
+ const result = utf8.encodeInto(value, output.subarray(position, position + length));
528
+ if (result.read !== value.length || result.written !== length)
529
+ throw new Error('UTF-8 planning mismatch');
530
+ position += length;
531
+ }
532
+ }
533
+ else if (tag === 8) {
534
+ const value = plan[cursor++];
535
+ const length = plan[cursor++];
536
+ if (value.length !== length)
537
+ throw new RangeError('Byte input changed while encoding');
538
+ uint(length);
539
+ output.set(value, position);
540
+ position += length;
541
+ }
542
+ else {
543
+ uint(plan[cursor++]);
544
+ }
545
+ }
546
+ if (position !== capacity)
547
+ throw new Error('Binary planning mismatch');
548
+ return output;
549
+ }
550
+ /** Strict decoder: byte input, version, canonical ordering, bounds and complete consumption. */
551
+ function decodeValue(input, options = {}) {
552
+ const limits = limitsFor(options);
553
+ if (!(input instanceof Uint8Array))
554
+ throw new TypeError('Wire payload must be Uint8Array');
555
+ if (input.length > limits.maxBytes)
556
+ throw new RangeError('Binary value exceeds byte limit');
557
+ const reader = new BinaryReader(input);
558
+ if (reader.readBytes(MAGIC.length).some((byte, index) => byte !== MAGIC[index]))
559
+ throw new TypeError('Unsupported binary wire version');
560
+ let nodes = 0;
561
+ const string = () => text.decode(reader.readFrame());
562
+ const read = (depth) => {
563
+ if (++nodes > limits.maxNodes || depth > limits.maxDepth)
564
+ throw new RangeError('Binary value exceeds complexity limit');
565
+ const tag = reader.readBytes(1)[0];
566
+ if (tag === 0)
567
+ return undefined;
568
+ if (tag === 1)
569
+ return null;
570
+ if (tag === 2)
571
+ return false;
572
+ if (tag === 3)
573
+ return true;
574
+ if (tag === 4) {
575
+ const bytes = reader.readBytes(8);
576
+ const value = new DataView(bytes.buffer, bytes.byteOffset, 8).getFloat64(0, false);
577
+ if (!Number.isFinite(value))
578
+ throw new TypeError('Binary numbers must be finite');
579
+ return value;
580
+ }
581
+ if (tag === 5 || tag === 6) {
582
+ const value = reader.readVarBigInt();
583
+ if (tag === 6 && value === 0n)
584
+ throw new TypeError('Non-canonical negative zero');
585
+ return tag === 6 ? -value : value;
586
+ }
587
+ if (tag === 7)
588
+ return string();
589
+ if (tag === 8)
590
+ return new Uint8Array(reader.readFrame());
591
+ if (tag === 9 || tag === 10) {
592
+ const count = reader.readVarUint();
593
+ if (count > limits.maxNodes - nodes || count > reader.remaining)
594
+ throw new RangeError('Invalid binary collection size');
595
+ if (tag === 9)
596
+ return Array.from({ length: count }, () => read(depth + 1));
597
+ const result = {};
598
+ let previous;
599
+ for (let index = 0; index < count; index++) {
600
+ const key = string();
601
+ if (previous !== undefined && key <= previous)
602
+ throw new TypeError('Non-canonical binary map keys');
603
+ previous = key;
604
+ Object.defineProperty(result, key, { value: read(depth + 1), enumerable: true, writable: true, configurable: true });
605
+ }
606
+ return result;
607
+ }
608
+ throw new TypeError('Unknown binary value tag');
609
+ };
610
+ const value = read(0);
611
+ if (reader.remaining)
612
+ throw new TypeError('Trailing binary data');
613
+ return value;
614
+ }
615
+ /** Domain separation prevents RPC bytes from being accepted as worker/chat/peer messages. */
616
+ const encodeWire = (channel, value, limits) => encodeValue([channel, value], limits);
617
+ function decodeWire(channel, bytes, limits) {
618
+ const envelope = decodeValue(bytes, limits);
619
+ if (!Array.isArray(envelope) || envelope.length !== 2 || envelope[0] !== channel)
620
+ throw new TypeError('Wrong binary message channel');
621
+ return envelope[1];
622
+ }
623
+
624
+ /** Decoded objects stay inside a process. Every actual IPC payload is canonical bytes. */
625
+ class BinaryWorker {
626
+ #transport;
627
+ #errorHandler;
628
+ constructor(url, options = {}) {
629
+ const transport = new EasyWorker(url, { ...options, serialization: 'advanced' });
630
+ // EasyWorker's parent constructor is asynchronous, the child constructor is not.
631
+ if (transport && typeof transport.then === 'function')
632
+ return transport.then(value => { this.#transport = value; this.worker = value.worker; return this; });
633
+ this.#transport = transport;
634
+ this.worker = transport.worker;
635
+ }
636
+ postMessage(value) { this.#transport.postMessage(encodeWire('worker', value)); }
637
+ onmessage(handler) {
638
+ this.#transport.onmessage(bytes => {
639
+ let value;
640
+ try {
641
+ value = decodeWire('worker', bytes);
642
+ }
643
+ catch (error) {
644
+ if (this.#errorHandler)
645
+ this.#errorHandler(error);
646
+ else
647
+ throw error;
648
+ return;
649
+ }
650
+ handler(value);
651
+ });
652
+ }
653
+ onerror(handler) { this.#errorHandler = handler; this.#transport.onerror(handler); }
654
+ terminate() { return this.#transport.terminate(); }
655
+ async once(value) {
656
+ let timer;
657
+ try {
658
+ return await new Promise((resolve, reject) => {
659
+ timer = setTimeout(() => reject(new Error('Binary worker request timed out')), 10000);
660
+ this.onerror(reject);
661
+ this.onmessage(resolve);
662
+ this.postMessage(value);
663
+ });
664
+ }
665
+ finally {
666
+ clearTimeout(timer);
667
+ this.terminate();
668
+ }
669
+ }
670
+ }
671
+
672
+ export { BinaryWorker as B, encodeValue as a, BinaryReader as b, BinaryWriter as c, decodeFrames as d, encodeFrames as e, encodeValueWithPrefix as f, decodeValue as g, varUintLength as v };
@@ -0,0 +1 @@
1
+ export { B as default } from './binary-worker-BxQdEW3s.js';