redjs 0.3.0

Full-control Rust embedding layer over Bun's JavaScriptCore prebuilt. C ABI designed for zero-alloc C# (NativeAOT) codegen bindings.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
// WHATWG Streams — the promise-shaped surface.
//
// This file is deliberately thin and deliberately JavaScript. Every DECISION it
// makes is delegated to `RedReadableCore` / `RedWritableCore` (Rust,
// src/webapi/streams.rs): the queue and its running total, desiredSize, whether
// to pull, whether a close may land yet, and whether backpressure changed. What
// is left here is promise sequencing over USER CALLBACKS — `start`, `pull`,
// `write`, `transform`, `size` — which cannot leave JS, because a promise and a
// chunk are both JS values.
//
// Chunk VALUES never cross the boundary at all: they sit in `_values` beside
// the Rust queue, kept in lockstep by index. So there is no marshalling per
// chunk and no GC protect/unprotect per chunk either.
//
// Deliberate narrowings, each with its spec reference:
//
//  * BYTE STREAMS. `new ReadableStream(src, …)` with `src.type === 'bytes'`
//    (Streams §4.2.4, the underlying byte source) and BYOB readers
//    (`getReader({ mode: 'byob' })`, §4.3) are REFUSED with a named error.
//    Byte streams are a second controller, a second reader type and an
//    auto-allocation path; half of one is worse than none, because the failure
//    shows up as a wrong `byteLength` far from here.
//  * `ReadableStreamDefaultController.prototype.byobRequest` — same reason.
//  * `WritableStreamDefaultController.prototype.signal` (§5.3) is present and
//    aborts, but the `AbortSignal` it exposes is the runtime's own; there is no
//    reason-carrying abort of an in-flight `write()` beyond that.

(function (g) {
  'use strict';

  if (typeof g.RedReadableCore !== 'function' || typeof g.RedWritableCore !== 'function') {
    throw new Error('streams.js requires the native stream cores (webapi::streams::install)');
  }

  const READABLE = 0, CLOSED = 1, ERRORED = 2;
  const W_WRITABLE = 0, W_ERRORING = 1, W_ERRORED = 2, W_CLOSED = 3;

  function deferred() {
    let resolve, reject;
    const promise = new Promise((res, rej) => { resolve = res; reject = rej; });
    // A rejected `closed`/`ready` nobody is awaiting yet must not surface as an
    // unhandled rejection; the spec marks these handled for exactly this reason.
    promise.catch(() => {});
    return { promise, resolve, reject };
  }

  function validateHighWaterMark(value, fallback) {
    const hwm = value === undefined ? fallback : Number(value);
    if (Number.isNaN(hwm) || hwm < 0) {
      throw new RangeError('highWaterMark must be a non-negative number, got ' + value);
    }
    return hwm;
  }

  function sizeOf(strategy) {
    if (strategy && strategy.size !== undefined) {
      const fn = strategy.size;
      if (typeof fn !== 'function') throw new TypeError('strategy.size must be a function');
      return fn;
    }
    return () => 1;
  }

  // ── ReadableStream ────────────────────────────────────────────────────────

  class ReadableStreamDefaultController {
    constructor() { throw new TypeError('Illegal constructor'); }

    get desiredSize() { return this._core.desiredSize; }

    enqueue(chunk) {
      if (!this._core.canCloseOrEnqueue) {
        throw new TypeError('Cannot enqueue: the stream is closed, closing or errored');
      }
      const stream = this._stream;
      // A waiting reader takes the chunk directly — it never enters the queue,
      // so the size algorithm is not consulted for it (spec §4.9.5).
      if (stream._readRequests.length > 0) {
        const req = stream._readRequests.shift();
        req.resolve({ value: chunk, done: false });
        pullIfNeeded(this);
        return;
      }
      let size;
      try {
        size = this._sizeAlgorithm(chunk);
      } catch (e) {
        errorController(this, e);
        throw e;
      }
      const rc = this._core.enqueue(size);
      if (rc === 1) {
        const e = new RangeError('The queuing strategy returned a size that is not a finite, non-negative number: ' + size);
        errorController(this, e);
        throw e;
      }
      if (rc === 2) throw new TypeError('Cannot enqueue: the stream is closed, closing or errored');
      this._values.push(chunk);
      pullIfNeeded(this);
    }

    close() {
      if (!this._core.canCloseOrEnqueue) {
        throw new TypeError('Cannot close: the stream is already closed, closing or errored');
      }
      // Rust decides whether the close lands now or waits for the queue to
      // drain; closing early would silently truncate the stream.
      if (this._core.requestClose()) closeStream(this._stream);
    }

    error(e) { errorController(this, e); }
  }

  function errorController(controller, reason) {
    if (controller._core.state !== READABLE) return;
    controller._core.resetQueue();
    controller._values.length = 0;
    errorStream(controller._stream, reason);
  }

  function errorStream(stream, reason) {
    if (stream._controller._core.state !== READABLE) return;
    stream._controller._core.state = ERRORED;
    stream._storedError = reason;
    for (const req of stream._readRequests) req.reject(reason);
    stream._readRequests.length = 0;
    if (stream._closedDeferred) stream._closedDeferred.reject(reason);
  }

  function closeStream(stream) {
    const core = stream._controller._core;
    if (core.state !== READABLE) return;
    core.state = CLOSED;
    for (const req of stream._readRequests) req.resolve({ value: undefined, done: true });
    stream._readRequests.length = 0;
    if (stream._closedDeferred) stream._closedDeferred.resolve(undefined);
  }

  function pullIfNeeded(controller) {
    if (!controller._core.beginPull(controller._stream._readRequests.length)) return;
    runPull(controller);
  }

  function runPull(controller) {
    let result;
    try {
      result = controller._pullAlgorithm(controller);
    } catch (e) {
      errorController(controller, e);
      return;
    }
    Promise.resolve(result).then(
      () => {
        // Rust remembered any demand that arrived mid-pull; replaying it is what
        // keeps a stream from stalling with a reader waiting.
        if (controller._core.endPull(controller._stream._readRequests.length)) runPull(controller);
      },
      (e) => errorController(controller, e),
    );
  }

  class ReadableStreamDefaultReader {
    constructor(stream) {
      if (!(stream instanceof ReadableStream)) {
        throw new TypeError('ReadableStreamDefaultReader requires a ReadableStream');
      }
      if (stream.locked) throw new TypeError('This ReadableStream is already locked to a reader');
      this._stream = stream;
      stream._reader = this;
      stream._controller._core.locked = true;
      this._closed = deferred();
      stream._closedDeferred = this._closed;
      const state = stream._controller._core.state;
      if (state === CLOSED) this._closed.resolve(undefined);
      else if (state === ERRORED) this._closed.reject(stream._storedError);
    }

    get closed() { return this._closed.promise; }

    read() {
      const stream = this._stream;
      if (stream === null) {
        return Promise.reject(new TypeError('This reader has been released'));
      }
      const controller = stream._controller;
      const core = controller._core;
      core.disturbed = true;
      if (core.state === ERRORED) return Promise.reject(stream._storedError);
      if (core.queueLength > 0) {
        const chunk = controller._values.shift();
        core.dequeue();
        if (core.closeIfDrained()) closeStream(stream);
        else pullIfNeeded(controller);
        return Promise.resolve({ value: chunk, done: false });
      }
      if (core.state === CLOSED) return Promise.resolve({ value: undefined, done: true });
      const req = deferred();
      stream._readRequests.push(req);
      pullIfNeeded(controller);
      return req.promise;
    }

    cancel(reason) {
      if (this._stream === null) {
        return Promise.reject(new TypeError('This reader has been released'));
      }
      return cancelStream(this._stream, reason);
    }

    releaseLock() {
      const stream = this._stream;
      if (stream === null) return;
      if (stream._readRequests.length > 0) {
        throw new TypeError('Cannot release a reader with pending read requests');
      }
      stream._controller._core.locked = false;
      stream._reader = null;
      stream._closedDeferred = null;
      this._stream = null;
      // Spec: a released reader's `closed` rejects, so an awaiting caller finds
      // out rather than waiting for a promise nothing will ever settle.
      this._closed.reject(new TypeError('This reader has been released'));
    }
  }

  function cancelStream(stream, reason) {
    const controller = stream._controller;
    const core = controller._core;
    core.disturbed = true;
    if (core.state === CLOSED) return Promise.resolve(undefined);
    if (core.state === ERRORED) return Promise.reject(stream._storedError);
    core.resetQueue();
    controller._values.length = 0;
    closeStream(stream);
    let result;
    try {
      result = controller._cancelAlgorithm(reason);
    } catch (e) {
      return Promise.reject(e);
    }
    return Promise.resolve(result).then(() => undefined);
  }

  class ReadableStream {
    constructor(underlyingSource = {}, strategy = {}) {
      const source = underlyingSource || {};
      if (source.type !== undefined) {
        if (String(source.type) === 'bytes') {
          throw new TypeError(
            'ReadableStream: byte streams (type: "bytes") are not implemented in this runtime. ' +
            'A byte stream is a second controller, a second reader type and BYOB auto-allocation; ' +
            'a partial one fails as a wrong byteLength far from the stream. Use a default stream ' +
            'and enqueue Uint8Array chunks.');
        }
        throw new RangeError('ReadableStream: the only defined underlying source type is "bytes"');
      }

      const hwm = validateHighWaterMark(strategy && strategy.highWaterMark, 1);
      this._storedError = undefined;
      this._readRequests = [];
      this._reader = null;
      this._closedDeferred = null;

      const controller = Object.create(ReadableStreamDefaultController.prototype);
      controller._core = new RedReadableCore(hwm);
      controller._values = [];
      controller._stream = this;
      controller._sizeAlgorithm = sizeOf(strategy);
      controller._pullAlgorithm = typeof source.pull === 'function'
        ? (c) => source.pull(c) : () => undefined;
      controller._cancelAlgorithm = typeof source.cancel === 'function'
        ? (r) => source.cancel(r) : () => undefined;
      this._controller = controller;

      let started;
      try {
        started = typeof source.start === 'function' ? source.start(controller) : undefined;
      } catch (e) {
        errorController(controller, e);
        return;
      }
      Promise.resolve(started).then(
        () => {
          controller._core.started = true;
          pullIfNeeded(controller);
        },
        (e) => errorController(controller, e),
      );
    }

    get locked() { return this._controller._core.locked; }

    getReader(options) {
      if (options && options.mode !== undefined) {
        if (String(options.mode) === 'byob') {
          throw new TypeError(
            'ReadableStream.getReader: BYOB readers are not implemented in this runtime — see the ' +
            'byte-stream refusal in the ReadableStream constructor.');
        }
        throw new TypeError('ReadableStream.getReader: the only defined mode is "byob"');
      }
      return new ReadableStreamDefaultReader(this);
    }

    cancel(reason) {
      if (this.locked) return Promise.reject(new TypeError('Cannot cancel a locked ReadableStream'));
      return cancelStream(this, reason);
    }

    tee() {
      const reader = this.getReader();
      let reading = false, canceled1 = false, canceled2 = false;
      let reason1, reason2;
      let c1, c2;
      const cancelDeferred = deferred();

      function pullBoth() {
        if (reading) return;
        reading = true;
        return reader.read().then(
          ({ value, done }) => {
            reading = false;
            if (done) {
              if (!canceled1) c1.close();
              if (!canceled2) c2.close();
              return;
            }
            // Both branches get the SAME value; tee does not clone (spec §4.6).
            if (!canceled1) c1.enqueue(value);
            if (!canceled2) c2.enqueue(value);
          },
          (e) => {
            reading = false;
            if (!canceled1) c1.error(e);
            if (!canceled2) c2.error(e);
          },
        );
      }

      const branch1 = new ReadableStream({
        start(c) { c1 = c; },
        pull: pullBoth,
        cancel(r) {
          canceled1 = true; reason1 = r;
          // The source is only cancelled once BOTH branches have given up.
          if (canceled2) cancelDeferred.resolve(cancelStream(this === undefined ? branch1 : branch1, [reason1, reason2]));
          return cancelDeferred.promise;
        },
      });
      const branch2 = new ReadableStream({
        start(c) { c2 = c; },
        pull: pullBoth,
        cancel(r) {
          canceled2 = true; reason2 = r;
          if (canceled1) cancelDeferred.resolve(reader.cancel([reason1, reason2]));
          return cancelDeferred.promise;
        },
      });
      return [branch1, branch2];
    }

    pipeTo(destination, options = {}) {
      if (!(destination instanceof WritableStream)) {
        return Promise.reject(new TypeError('pipeTo requires a WritableStream'));
      }
      if (this.locked) return Promise.reject(new TypeError('Cannot pipe a locked ReadableStream'));
      if (destination.locked) return Promise.reject(new TypeError('Cannot pipe to a locked WritableStream'));
      const opts = options || {};
      const preventClose = !!opts.preventClose;
      const preventAbort = !!opts.preventAbort;
      const preventCancel = !!opts.preventCancel;
      const signal = opts.signal;

      const reader = this.getReader();
      const writer = destination.getWriter();
      let aborted = false;

      const onAbort = () => {
        aborted = true;
        const reason = (signal && signal.reason) !== undefined ? signal.reason
          : new Error('The pipe was aborted');
        if (!preventAbort) writer.abort(reason);
        if (!preventCancel) reader.cancel(reason);
      };
      if (signal) {
        if (signal.aborted) onAbort();
        else if (typeof signal.addEventListener === 'function') {
          signal.addEventListener('abort', onAbort, { once: true });
        }
      }

      const step = () => {
        if (aborted) return Promise.reject(new Error('The pipe was aborted'));
        // Honouring `ready` is what makes the pipe respect backpressure rather
        // than reading the source as fast as it will go.
        return writer.ready
          .then(() => reader.read())
          .then(({ value, done }) => {
            if (done) return null;
            return writer.write(value).then(step);
          });
      };

      return step().then(
        () => {
          reader.releaseLock();
          if (preventClose) { writer.releaseLock(); return undefined; }
          return writer.close().then(() => { writer.releaseLock(); });
        },
        (e) => {
          if (!preventAbort) { try { writer.abort(e); } catch (_) {} }
          if (!preventCancel) { try { reader.cancel(e); } catch (_) {} }
          try { reader.releaseLock(); } catch (_) {}
          try { writer.releaseLock(); } catch (_) {}
          throw e;
        },
      );
    }

    pipeThrough(transform, options) {
      if (!transform || !(transform.writable instanceof WritableStream) ||
          !(transform.readable instanceof ReadableStream)) {
        throw new TypeError('pipeThrough requires { writable, readable }');
      }
      if (this.locked) throw new TypeError('Cannot pipe a locked ReadableStream');
      // The returned promise is intentionally not awaited (spec §4.5): errors
      // surface on the readable end. Marking it handled keeps a piped error from
      // becoming an unhandled rejection as well as a stream error.
      this.pipeTo(transform.writable, options).catch(() => {});
      return transform.readable;
    }

    values(options) {
      const reader = this.getReader();
      const preventCancel = !!(options && options.preventCancel);
      return {
        next() {
          return reader.read().then((r) => {
            if (r.done) reader.releaseLock();
            return r;
          });
        },
        return(value) {
          if (!preventCancel) reader.cancel(value);
          reader.releaseLock();
          return Promise.resolve({ value, done: true });
        },
        [Symbol.asyncIterator]() { return this; },
      };
    }
  }
  ReadableStream.prototype[Symbol.asyncIterator] = ReadableStream.prototype.values;

  Object.defineProperty(ReadableStream, 'from', {
    writable: true,
    configurable: true,
    value: function from(asyncIterable) {
      if (asyncIterable == null) throw new TypeError('ReadableStream.from: the argument is not iterable');
      let method = asyncIterable[Symbol.asyncIterator];
      let iterator;
      if (method != null) {
        if (typeof method !== 'function') throw new TypeError('ReadableStream.from: @@asyncIterator is not a function');
        iterator = method.call(asyncIterable);
      } else {
        method = asyncIterable[Symbol.iterator];
        if (typeof method !== 'function') throw new TypeError('ReadableStream.from: the argument is not iterable');
        const sync = method.call(asyncIterable);
        if (sync === null || typeof sync !== 'object') throw new TypeError('ReadableStream.from: the iterator is not an object');
        iterator = {
          next: (v) => Promise.resolve(sync.next(v)).then((r) =>
            Promise.resolve(r.value).then((value) => ({ value, done: !!r.done }))),
          return: typeof sync.return === 'function' ? (v) => Promise.resolve(sync.return(v)) : undefined,
        };
      }
      if (iterator === null || typeof iterator !== 'object') throw new TypeError('ReadableStream.from: the iterator is not an object');
      const next = iterator.next;
      return new ReadableStream({
        pull(controller) {
          return Promise.resolve(next.call(iterator)).then((r) => {
            if (r === null || typeof r !== 'object') throw new TypeError('ReadableStream.from: the iterator result is not an object');
            if (r.done) controller.close();
            else controller.enqueue(r.value);
          });
        },
        cancel(reason) {
          const ret = iterator.return;
          if (ret == null) return undefined;
          return Promise.resolve(ret.call(iterator, reason)).then((r) => {
            if (r === null || typeof r !== 'object') throw new TypeError('ReadableStream.from: the return() result is not an object');
          });
        },
      }, { highWaterMark: 0 });
    },
  });

  // ── WritableStream ────────────────────────────────────────────────────────

  class WritableStreamDefaultController {
    constructor() { throw new TypeError('Illegal constructor'); }
    get signal() { return this._abortController ? this._abortController.signal : undefined; }
    error(e) {
      if (this._core.state === W_WRITABLE) startErroring(this._stream, e);
    }
  }

  function startErroring(stream, reason) {
    const core = stream._core;
    if (core.state !== W_WRITABLE) return;
    core.state = W_ERRORING;
    stream._storedError = reason;
    if (stream._writer) stream._writer._rejectReady(reason);
    if (!core.inFlightWrite && !core.inFlightClose) finishErroring(stream);
  }

  function finishErroring(stream) {
    const core = stream._core;
    core.state = W_ERRORED;
    core.resetQueue();
    for (const req of stream._writeRequests) req.reject(stream._storedError);
    stream._writeRequests.length = 0;
    stream._chunks.length = 0;
    if (stream._closeRequest) {
      stream._closeRequest.reject(stream._storedError);
      stream._closeRequest = null;
    }
    if (stream._writer) stream._writer._rejectClosed(stream._storedError);
    const pending = stream._pendingAbort;
    stream._pendingAbort = null;
    if (pending) {
      let result;
      try {
        result = stream._controller._abortAlgorithm(pending.reason);
      } catch (e) {
        pending.reject(e);
        return;
      }
      Promise.resolve(result).then(() => pending.resolve(undefined), (e) => pending.reject(e));
    }
  }

  function advanceQueue(stream) {
    const core = stream._core;
    if (!core.started || core.inFlightWrite || core.inFlightClose) return;
    if (core.state === W_ERRORING) { finishErroring(stream); return; }
    if (core.queueLength === 0) {
      if (core.closeRequested && stream._closeRequest) processClose(stream);
      return;
    }
    const chunk = stream._chunks.shift();
    const request = stream._writeRequests.shift();
    core.inFlightWrite = true;
    let result;
    try {
      result = stream._controller._writeAlgorithm(chunk, stream._controller);
    } catch (e) {
      core.inFlightWrite = false;
      core.dequeue();
      request.reject(e);
      startErroring(stream, e);
      return;
    }
    Promise.resolve(result).then(
      () => {
        core.inFlightWrite = false;
        core.dequeue();
        if (core.updateBackpressure() && stream._writer) stream._writer._syncReady();
        request.resolve(undefined);
        advanceQueue(stream);
      },
      (e) => {
        core.inFlightWrite = false;
        core.dequeue();
        request.reject(e);
        startErroring(stream, e);
      },
    );
  }

  function processClose(stream) {
    const core = stream._core;
    core.inFlightClose = true;
    const request = stream._closeRequest;
    stream._closeRequest = null;
    let result;
    try {
      // A close algorithm that throws SYNCHRONOUSLY has to land here, not
      // escape into whatever reaction called advanceQueue. When it escaped, the
      // close request was never settled and the stream hung forever with no
      // error anywhere — which is exactly how a truncated DecompressionStream
      // presented before this guard existed.
      result = stream._controller._closeAlgorithm();
    } catch (e) {
      core.inFlightClose = false;
      request.reject(e);
      startErroring(stream, e);
      return;
    }
    Promise.resolve(result).then(
      () => {
        core.inFlightClose = false;
        core.state = W_CLOSED;
        request.resolve(undefined);
        if (stream._writer) stream._writer._resolveClosed();
      },
      (e) => {
        core.inFlightClose = false;
        request.reject(e);
        startErroring(stream, e);
      },
    );
  }

  class WritableStreamDefaultWriter {
    constructor(stream) {
      if (!(stream instanceof WritableStream)) {
        throw new TypeError('WritableStreamDefaultWriter requires a WritableStream');
      }
      if (stream.locked) throw new TypeError('This WritableStream is already locked to a writer');
      this._stream = stream;
      stream._writer = this;
      stream._core.locked = true;
      this._closedD = deferred();
      this._readyD = deferred();
      const state = stream._core.state;
      if (state === W_ERRORED || state === W_ERRORING) {
        this._closedD.reject(stream._storedError);
        this._readyD.reject(stream._storedError);
      } else if (state === W_CLOSED) {
        this._closedD.resolve(undefined);
        this._readyD.resolve(undefined);
      } else if (!stream._core.backpressure) {
        this._readyD.resolve(undefined);
      }
    }

    get closed() { return this._closedD.promise; }
    get ready() { return this._readyD.promise; }
    get desiredSize() {
      if (this._stream === null) throw new TypeError('This writer has been released');
      const core = this._stream._core;
      if (core.state === W_ERRORED || core.state === W_ERRORING) return null;
      if (core.state === W_CLOSED) return 0;
      return core.desiredSize;
    }

    _syncReady() {
      const core = this._stream._core;
      if (core.backpressure) this._readyD = deferred();
      else this._readyD.resolve(undefined);
    }
    _rejectReady(e) {
      this._readyD.reject(e);
      this._readyD = deferred();
      this._readyD.reject(e);
    }
    _rejectClosed(e) { this._closedD.reject(e); }
    _resolveClosed() { this._closedD.resolve(undefined); }

    write(chunk) {
      const stream = this._stream;
      if (stream === null) return Promise.reject(new TypeError('This writer has been released'));
      const core = stream._core;
      if (core.state === W_ERRORED || core.state === W_ERRORING) {
        return Promise.reject(stream._storedError);
      }
      if (core.state === W_CLOSED || core.closeRequested) {
        return Promise.reject(new TypeError('Cannot write to a closing or closed WritableStream'));
      }
      let size;
      try {
        size = stream._controller._sizeAlgorithm(chunk);
      } catch (e) {
        startErroring(stream, e);
        return Promise.reject(e);
      }
      if (core.enqueue(size) === 1) {
        const e = new RangeError('The queuing strategy returned a size that is not a finite, non-negative number: ' + size);
        startErroring(stream, e);
        return Promise.reject(e);
      }
      stream._chunks.push(chunk);
      const request = deferred();
      stream._writeRequests.push(request);
      if (core.updateBackpressure()) this._syncReady();
      advanceQueue(stream);
      return request.promise;
    }

    close() {
      const stream = this._stream;
      if (stream === null) return Promise.reject(new TypeError('This writer has been released'));
      const core = stream._core;
      if (core.closeRequested || core.state === W_CLOSED) {
        return Promise.reject(new TypeError('This WritableStream is already closing or closed'));
      }
      if (core.state === W_ERRORED || core.state === W_ERRORING) {
        return Promise.reject(stream._storedError);
      }
      core.closeRequested = true;
      const request = deferred();
      stream._closeRequest = request;
      // Releasing `ready` on close is what lets `await writer.ready` after a
      // close settle instead of hanging forever.
      if (core.backpressure) { core.updateBackpressure(); this._readyD.resolve(undefined); }
      advanceQueue(stream);
      return request.promise;
    }

    abort(reason) {
      const stream = this._stream;
      if (stream === null) return Promise.reject(new TypeError('This writer has been released'));
      return abortStream(stream, reason);
    }

    releaseLock() {
      const stream = this._stream;
      if (stream === null) return;
      stream._core.locked = false;
      stream._writer = null;
      this._stream = null;
      const e = new TypeError('This writer has been released');
      this._readyD.reject(e);
      this._closedD.reject(e);
    }
  }

  function abortStream(stream, reason) {
    const core = stream._core;
    if (core.state === W_CLOSED || core.state === W_ERRORED) return Promise.resolve(undefined);
    if (stream._pendingAbort) return stream._pendingAbort.promise;
    const request = deferred();
    stream._pendingAbort = { promise: request.promise, resolve: request.resolve, reject: request.reject, reason };
    startErroring(stream, reason !== undefined ? reason : new TypeError('The stream was aborted'));
    return request.promise;
  }

  class WritableStream {
    constructor(underlyingSink = {}, strategy = {}) {
      const sink = underlyingSink || {};
      if (sink.type !== undefined) {
        throw new RangeError('WritableStream: an underlying sink has no defined `type`');
      }
      const hwm = validateHighWaterMark(strategy && strategy.highWaterMark, 1);
      this._core = new RedWritableCore(hwm);
      this._chunks = [];
      this._writeRequests = [];
      this._closeRequest = null;
      this._pendingAbort = null;
      this._storedError = undefined;
      this._writer = null;

      const controller = Object.create(WritableStreamDefaultController.prototype);
      controller._core = this._core;
      controller._stream = this;
      controller._sizeAlgorithm = sizeOf(strategy);
      controller._writeAlgorithm = typeof sink.write === 'function'
        ? (chunk, c) => sink.write(chunk, c) : () => undefined;
      controller._closeAlgorithm = typeof sink.close === 'function'
        ? () => sink.close() : () => undefined;
      controller._abortAlgorithm = typeof sink.abort === 'function'
        ? (r) => sink.abort(r) : () => undefined;
      controller._abortController = typeof g.AbortController === 'function' ? new g.AbortController() : null;
      this._controller = controller;

      let started;
      try {
        started = typeof sink.start === 'function' ? sink.start(controller) : undefined;
      } catch (e) {
        startErroring(this, e);
        return;
      }
      Promise.resolve(started).then(
        () => {
          this._core.started = true;
          if (this._core.updateBackpressure() && this._writer) this._writer._syncReady();
          advanceQueue(this);
        },
        (e) => { this._core.started = true; startErroring(this, e); },
      );
    }

    get locked() { return this._core.locked; }
    getWriter() { return new WritableStreamDefaultWriter(this); }
    abort(reason) {
      if (this.locked) return Promise.reject(new TypeError('Cannot abort a locked WritableStream'));
      return abortStream(this, reason);
    }
    close() {
      if (this.locked) return Promise.reject(new TypeError('Cannot close a locked WritableStream'));
      const writer = this.getWriter();
      const p = writer.close();
      // Keep the lock for the duration; releasing here would reject `p`.
      return p.then(
        (v) => { writer.releaseLock(); return v; },
        (e) => { writer.releaseLock(); throw e; },
      );
    }
  }

  // ── TransformStream ───────────────────────────────────────────────────────

  class TransformStreamDefaultController {
    constructor() { throw new TypeError('Illegal constructor'); }
    get desiredSize() { return this._readableController.desiredSize; }
    enqueue(chunk) { this._readableController.enqueue(chunk); }
    error(e) {
      this._readableController.error(e);
      if (this._stream._writable._core.state === W_WRITABLE) startErroring(this._stream._writable, e);
    }
    terminate() {
      try { this._readableController.close(); } catch (_) {}
      const w = this._stream._writable;
      if (w._core.state === W_WRITABLE) {
        startErroring(w, new TypeError('The transform stream was terminated'));
      }
    }
  }

  class TransformStream {
    constructor(transformer = {}, writableStrategy = {}, readableStrategy = {}) {
      const t = transformer || {};
      if (t.readableType !== undefined || t.writableType !== undefined) {
        throw new RangeError('TransformStream: readableType/writableType are reserved and must be undefined');
      }
      const controller = Object.create(TransformStreamDefaultController.prototype);
      controller._stream = this;

      this._readable = new ReadableStream({
        start: (c) => { controller._readableController = c; },
      }, readableStrategy);

      const transformAlgorithm = typeof t.transform === 'function'
        ? (chunk) => t.transform(chunk, controller)
        // Spec §"identity transform": with no `transform`, chunks pass through.
        : (chunk) => { controller.enqueue(chunk); };
      const flushAlgorithm = typeof t.flush === 'function' ? () => t.flush(controller) : () => undefined;

      this._writable = new WritableStream({
        write: (chunk) => Promise.resolve(transformAlgorithm(chunk)).catch((e) => {
          controller.error(e);
          throw e;
        }),
        close: () => {
          // `flush` may throw synchronously — a native codec reporting a
          // truncated stream does exactly that. Error the READABLE end first, so
          // whoever is reading finds out, then let the rejection travel.
          let flushed;
          try {
            flushed = flushAlgorithm();
          } catch (e) {
            controller._readableController.error(e);
            throw e;
          }
          return Promise.resolve(flushed).then(
            () => { try { controller._readableController.close(); } catch (_) {} },
            (e) => { controller._readableController.error(e); throw e; },
          );
        },
        abort: (reason) => { controller._readableController.error(reason); },
      }, writableStrategy);

      this._controller = controller;
      if (typeof t.start === 'function') t.start(controller);
    }

    get readable() { return this._readable; }
    get writable() { return this._writable; }
  }

  // ── Queuing strategies ────────────────────────────────────────────────────

  class CountQueuingStrategy {
    constructor(init) {
      if (!init || init.highWaterMark === undefined) {
        throw new TypeError('CountQueuingStrategy requires { highWaterMark }');
      }
      this._hwm = Number(init.highWaterMark);
    }
    get highWaterMark() { return this._hwm; }
    // Spec: `size` is a getter returning a function that is NOT bound to the
    // instance, so `strategy.size` can be pulled off and called standalone.
    get size() { return () => 1; }
  }

  class ByteLengthQueuingStrategy {
    constructor(init) {
      if (!init || init.highWaterMark === undefined) {
        throw new TypeError('ByteLengthQueuingStrategy requires { highWaterMark }');
      }
      this._hwm = Number(init.highWaterMark);
    }
    get highWaterMark() { return this._hwm; }
    get size() { return (chunk) => chunk.byteLength; }
  }

  // ── CompressionStream / DecompressionStream ───────────────────────────────

  function makeCodecStream(format, decompress) {
    const zip = new g.RedZip(format, decompress);
    return new TransformStream({
      transform(chunk, controller) {
        const out = zip.push(chunk);
        // A deflate block is not emitted until it is worth emitting, so most
        // pushes legitimately produce nothing. Enqueueing an empty chunk would
        // make every consumer see spurious zero-length reads.
        if (out.byteLength > 0) controller.enqueue(out);
      },
      flush(controller) {
        const out = zip.finish();
        if (out.byteLength > 0) controller.enqueue(out);
      },
    });
  }

  class CompressionStream {
    constructor(format) {
      const t = makeCodecStream(String(format), false);
      this._t = t;
    }
    get readable() { return this._t.readable; }
    get writable() { return this._t.writable; }
  }

  class DecompressionStream {
    constructor(format) {
      const t = makeCodecStream(String(format), true);
      this._t = t;
    }
    get readable() { return this._t.readable; }
    get writable() { return this._t.writable; }
  }

  // ── Publish ───────────────────────────────────────────────────────────────

  const exports = {
    ReadableStream, ReadableStreamDefaultReader, ReadableStreamDefaultController,
    WritableStream, WritableStreamDefaultWriter, WritableStreamDefaultController,
    TransformStream, TransformStreamDefaultController,
    CountQueuingStrategy, ByteLengthQueuingStrategy,
    CompressionStream, DecompressionStream,
  };
  for (const name of Object.keys(exports)) {
    // WebIDL: every interface prototype says its name to
    // `Object.prototype.toString` (webapi/interface_tags.rs).
    Object.defineProperty(exports[name].prototype, Symbol.toStringTag, { value: name, configurable: true });
    Object.defineProperty(g, name, { value: exports[name], writable: true, configurable: true });
  }

  // `Blob.prototype.stream` is native (webapi/blob.rs): 64 KiB chunks, and a
  // path-backed File read off the JS thread. It builds its stream from a
  // factory made right after this script runs.
})(globalThis);