11cb0ef41Sopenharmony_ci// Flags: --expose-internals --no-warnings
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ciconst { isDisturbed, isErrored, isReadable } = require('stream');
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst {
81cb0ef41Sopenharmony_ci  isPromise,
91cb0ef41Sopenharmony_ci} = require('util/types');
101cb0ef41Sopenharmony_ciconst {
111cb0ef41Sopenharmony_ci  setImmediate: delay
121cb0ef41Sopenharmony_ci} = require('timers/promises');
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciconst {
151cb0ef41Sopenharmony_ci  ByteLengthQueuingStrategy,
161cb0ef41Sopenharmony_ci  CountQueuingStrategy,
171cb0ef41Sopenharmony_ci  ReadableStream,
181cb0ef41Sopenharmony_ci  ReadableStreamDefaultReader,
191cb0ef41Sopenharmony_ci  ReadableStreamDefaultController,
201cb0ef41Sopenharmony_ci  ReadableByteStreamController,
211cb0ef41Sopenharmony_ci  ReadableStreamBYOBReader,
221cb0ef41Sopenharmony_ci  ReadableStreamBYOBRequest,
231cb0ef41Sopenharmony_ci  WritableStream,
241cb0ef41Sopenharmony_ci} = require('stream/web');
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciconst {
271cb0ef41Sopenharmony_ci  readableStreamPipeTo,
281cb0ef41Sopenharmony_ci  readableStreamTee,
291cb0ef41Sopenharmony_ci  readableByteStreamControllerConvertPullIntoDescriptor,
301cb0ef41Sopenharmony_ci  readableStreamDefaultControllerEnqueue,
311cb0ef41Sopenharmony_ci  readableByteStreamControllerEnqueue,
321cb0ef41Sopenharmony_ci  readableStreamDefaultControllerCanCloseOrEnqueue,
331cb0ef41Sopenharmony_ci  readableByteStreamControllerClose,
341cb0ef41Sopenharmony_ci  readableByteStreamControllerRespond,
351cb0ef41Sopenharmony_ci  readableStreamReaderGenericRelease,
361cb0ef41Sopenharmony_ci} = require('internal/webstreams/readablestream');
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ciconst {
391cb0ef41Sopenharmony_ci  kState
401cb0ef41Sopenharmony_ci} = require('internal/webstreams/util');
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ciconst {
431cb0ef41Sopenharmony_ci  createReadStream,
441cb0ef41Sopenharmony_ci  readFileSync,
451cb0ef41Sopenharmony_ci} = require('fs');
461cb0ef41Sopenharmony_ciconst {
471cb0ef41Sopenharmony_ci  Buffer,
481cb0ef41Sopenharmony_ci} = require('buffer');
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ciconst {
511cb0ef41Sopenharmony_ci  kTransfer,
521cb0ef41Sopenharmony_ci} = require('internal/worker/js_transferable');
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ciconst {
551cb0ef41Sopenharmony_ci  inspect,
561cb0ef41Sopenharmony_ci} = require('util');
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci{
591cb0ef41Sopenharmony_ci  const r = new ReadableStream();
601cb0ef41Sopenharmony_ci  assert.strictEqual(typeof r.locked, 'boolean');
611cb0ef41Sopenharmony_ci  assert.strictEqual(typeof r.cancel, 'function');
621cb0ef41Sopenharmony_ci  assert.strictEqual(typeof r.getReader, 'function');
631cb0ef41Sopenharmony_ci  assert.strictEqual(typeof r.pipeThrough, 'function');
641cb0ef41Sopenharmony_ci  assert.strictEqual(typeof r.pipeTo, 'function');
651cb0ef41Sopenharmony_ci  assert.strictEqual(typeof r.tee, 'function');
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci  ['', null, 'asdf'].forEach((mode) => {
681cb0ef41Sopenharmony_ci    assert.throws(() => r.getReader({ mode }), {
691cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_VALUE',
701cb0ef41Sopenharmony_ci    });
711cb0ef41Sopenharmony_ci  });
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci  [1, 'asdf'].forEach((options) => {
741cb0ef41Sopenharmony_ci    assert.throws(() => r.getReader(options), {
751cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
761cb0ef41Sopenharmony_ci    });
771cb0ef41Sopenharmony_ci  });
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci  assert(!r.locked);
801cb0ef41Sopenharmony_ci  r.getReader();
811cb0ef41Sopenharmony_ci  assert(r.locked);
821cb0ef41Sopenharmony_ci}
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci{
851cb0ef41Sopenharmony_ci  // Throw error and return rejected promise in `cancel()` method
861cb0ef41Sopenharmony_ci  // would execute same cleanup code
871cb0ef41Sopenharmony_ci  const r1 = new ReadableStream({
881cb0ef41Sopenharmony_ci    cancel: () => {
891cb0ef41Sopenharmony_ci      return Promise.reject('Cancel Error');
901cb0ef41Sopenharmony_ci    },
911cb0ef41Sopenharmony_ci  });
921cb0ef41Sopenharmony_ci  r1.cancel().finally(common.mustCall(() => {
931cb0ef41Sopenharmony_ci    const controllerState = r1[kState].controller[kState];
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci    assert.strictEqual(controllerState.pullAlgorithm, undefined);
961cb0ef41Sopenharmony_ci    assert.strictEqual(controllerState.cancelAlgorithm, undefined);
971cb0ef41Sopenharmony_ci    assert.strictEqual(controllerState.sizeAlgorithm, undefined);
981cb0ef41Sopenharmony_ci  })).catch(() => {});
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci  const r2 = new ReadableStream({
1011cb0ef41Sopenharmony_ci    cancel() {
1021cb0ef41Sopenharmony_ci      throw new Error('Cancel Error');
1031cb0ef41Sopenharmony_ci    }
1041cb0ef41Sopenharmony_ci  });
1051cb0ef41Sopenharmony_ci  r2.cancel().finally(common.mustCall(() => {
1061cb0ef41Sopenharmony_ci    const controllerState = r2[kState].controller[kState];
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci    assert.strictEqual(controllerState.pullAlgorithm, undefined);
1091cb0ef41Sopenharmony_ci    assert.strictEqual(controllerState.cancelAlgorithm, undefined);
1101cb0ef41Sopenharmony_ci    assert.strictEqual(controllerState.sizeAlgorithm, undefined);
1111cb0ef41Sopenharmony_ci  })).catch(() => {});
1121cb0ef41Sopenharmony_ci}
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ci{
1151cb0ef41Sopenharmony_ci  const source = {
1161cb0ef41Sopenharmony_ci    start: common.mustCall((controller) => {
1171cb0ef41Sopenharmony_ci      assert(controller instanceof ReadableStreamDefaultController);
1181cb0ef41Sopenharmony_ci    }),
1191cb0ef41Sopenharmony_ci    pull: common.mustCall((controller) => {
1201cb0ef41Sopenharmony_ci      assert(controller instanceof ReadableStreamDefaultController);
1211cb0ef41Sopenharmony_ci    }),
1221cb0ef41Sopenharmony_ci    cancel: common.mustNotCall(),
1231cb0ef41Sopenharmony_ci  };
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci  new ReadableStream(source);
1261cb0ef41Sopenharmony_ci}
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ci{
1291cb0ef41Sopenharmony_ci  const source = {
1301cb0ef41Sopenharmony_ci    start: common.mustCall(async (controller) => {
1311cb0ef41Sopenharmony_ci      assert(controller instanceof ReadableStreamDefaultController);
1321cb0ef41Sopenharmony_ci    }),
1331cb0ef41Sopenharmony_ci    pull: common.mustCall(async (controller) => {
1341cb0ef41Sopenharmony_ci      assert(controller instanceof ReadableStreamDefaultController);
1351cb0ef41Sopenharmony_ci    }),
1361cb0ef41Sopenharmony_ci    cancel: common.mustNotCall(),
1371cb0ef41Sopenharmony_ci  };
1381cb0ef41Sopenharmony_ci
1391cb0ef41Sopenharmony_ci  new ReadableStream(source);
1401cb0ef41Sopenharmony_ci}
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ci{
1431cb0ef41Sopenharmony_ci  const source = {
1441cb0ef41Sopenharmony_ci    start: common.mustCall((controller) => {
1451cb0ef41Sopenharmony_ci      assert(controller instanceof ReadableByteStreamController);
1461cb0ef41Sopenharmony_ci    }),
1471cb0ef41Sopenharmony_ci    pull: common.mustNotCall(),
1481cb0ef41Sopenharmony_ci    cancel: common.mustNotCall(),
1491cb0ef41Sopenharmony_ci    type: 'bytes',
1501cb0ef41Sopenharmony_ci  };
1511cb0ef41Sopenharmony_ci
1521cb0ef41Sopenharmony_ci  new ReadableStream(source);
1531cb0ef41Sopenharmony_ci}
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ci{
1561cb0ef41Sopenharmony_ci  const source = {
1571cb0ef41Sopenharmony_ci    start: common.mustCall(async (controller) => {
1581cb0ef41Sopenharmony_ci      assert(controller instanceof ReadableByteStreamController);
1591cb0ef41Sopenharmony_ci    }),
1601cb0ef41Sopenharmony_ci    pull: common.mustNotCall(),
1611cb0ef41Sopenharmony_ci    cancel: common.mustNotCall(),
1621cb0ef41Sopenharmony_ci    type: 'bytes',
1631cb0ef41Sopenharmony_ci  };
1641cb0ef41Sopenharmony_ci
1651cb0ef41Sopenharmony_ci  new ReadableStream(source);
1661cb0ef41Sopenharmony_ci}
1671cb0ef41Sopenharmony_ci
1681cb0ef41Sopenharmony_ci{
1691cb0ef41Sopenharmony_ci  const source = {
1701cb0ef41Sopenharmony_ci    start: common.mustCall(async (controller) => {
1711cb0ef41Sopenharmony_ci      assert(controller instanceof ReadableByteStreamController);
1721cb0ef41Sopenharmony_ci    }),
1731cb0ef41Sopenharmony_ci    pull: common.mustCall(async (controller) => {
1741cb0ef41Sopenharmony_ci      assert(controller instanceof ReadableByteStreamController);
1751cb0ef41Sopenharmony_ci    }),
1761cb0ef41Sopenharmony_ci    cancel: common.mustNotCall(),
1771cb0ef41Sopenharmony_ci    type: 'bytes',
1781cb0ef41Sopenharmony_ci  };
1791cb0ef41Sopenharmony_ci
1801cb0ef41Sopenharmony_ci  new ReadableStream(source, { highWaterMark: 10 });
1811cb0ef41Sopenharmony_ci}
1821cb0ef41Sopenharmony_ci
1831cb0ef41Sopenharmony_ci{
1841cb0ef41Sopenharmony_ci  // These are silly but they should all work per spec
1851cb0ef41Sopenharmony_ci  new ReadableStream(1);
1861cb0ef41Sopenharmony_ci  new ReadableStream('hello');
1871cb0ef41Sopenharmony_ci  new ReadableStream(false);
1881cb0ef41Sopenharmony_ci  new ReadableStream([]);
1891cb0ef41Sopenharmony_ci  new ReadableStream(1, 1);
1901cb0ef41Sopenharmony_ci  new ReadableStream(1, 'hello');
1911cb0ef41Sopenharmony_ci  new ReadableStream(1, false);
1921cb0ef41Sopenharmony_ci  new ReadableStream(1, []);
1931cb0ef41Sopenharmony_ci}
1941cb0ef41Sopenharmony_ci
1951cb0ef41Sopenharmony_ci['a', {}, false].forEach((size) => {
1961cb0ef41Sopenharmony_ci  assert.throws(() => {
1971cb0ef41Sopenharmony_ci    new ReadableStream({}, { size });
1981cb0ef41Sopenharmony_ci  }, {
1991cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
2001cb0ef41Sopenharmony_ci  });
2011cb0ef41Sopenharmony_ci});
2021cb0ef41Sopenharmony_ci
2031cb0ef41Sopenharmony_ci['a', {}].forEach((highWaterMark) => {
2041cb0ef41Sopenharmony_ci  assert.throws(() => {
2051cb0ef41Sopenharmony_ci    new ReadableStream({}, { highWaterMark });
2061cb0ef41Sopenharmony_ci  }, {
2071cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_VALUE',
2081cb0ef41Sopenharmony_ci  });
2091cb0ef41Sopenharmony_ci
2101cb0ef41Sopenharmony_ci  assert.throws(() => {
2111cb0ef41Sopenharmony_ci    new ReadableStream({ type: 'bytes' }, { highWaterMark });
2121cb0ef41Sopenharmony_ci  }, {
2131cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_VALUE',
2141cb0ef41Sopenharmony_ci  });
2151cb0ef41Sopenharmony_ci});
2161cb0ef41Sopenharmony_ci
2171cb0ef41Sopenharmony_ci[-1, NaN].forEach((highWaterMark) => {
2181cb0ef41Sopenharmony_ci  assert.throws(() => {
2191cb0ef41Sopenharmony_ci    new ReadableStream({}, { highWaterMark });
2201cb0ef41Sopenharmony_ci  }, {
2211cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_VALUE',
2221cb0ef41Sopenharmony_ci  });
2231cb0ef41Sopenharmony_ci
2241cb0ef41Sopenharmony_ci  assert.throws(() => {
2251cb0ef41Sopenharmony_ci    new ReadableStream({ type: 'bytes' }, { highWaterMark });
2261cb0ef41Sopenharmony_ci  }, {
2271cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_VALUE',
2281cb0ef41Sopenharmony_ci  });
2291cb0ef41Sopenharmony_ci});
2301cb0ef41Sopenharmony_ci
2311cb0ef41Sopenharmony_ci{
2321cb0ef41Sopenharmony_ci  new ReadableStream({}, new ByteLengthQueuingStrategy({ highWaterMark: 1 }));
2331cb0ef41Sopenharmony_ci  new ReadableStream({}, new CountQueuingStrategy({ highWaterMark: 1 }));
2341cb0ef41Sopenharmony_ci}
2351cb0ef41Sopenharmony_ci
2361cb0ef41Sopenharmony_ci{
2371cb0ef41Sopenharmony_ci  const strategy = new ByteLengthQueuingStrategy({ highWaterMark: 1 });
2381cb0ef41Sopenharmony_ci  assert.strictEqual(strategy.highWaterMark, 1);
2391cb0ef41Sopenharmony_ci  assert.strictEqual(strategy.size(new ArrayBuffer(10)), 10);
2401cb0ef41Sopenharmony_ci
2411cb0ef41Sopenharmony_ci  const { size } = strategy;
2421cb0ef41Sopenharmony_ci  assert.strictEqual(size(new ArrayBuffer(10)), 10);
2431cb0ef41Sopenharmony_ci}
2441cb0ef41Sopenharmony_ci
2451cb0ef41Sopenharmony_ci{
2461cb0ef41Sopenharmony_ci  const strategy = new CountQueuingStrategy({ highWaterMark: 1 });
2471cb0ef41Sopenharmony_ci  assert.strictEqual(strategy.highWaterMark, 1);
2481cb0ef41Sopenharmony_ci  assert.strictEqual(strategy.size(new ArrayBuffer(10)), 1);
2491cb0ef41Sopenharmony_ci
2501cb0ef41Sopenharmony_ci  const { size } = strategy;
2511cb0ef41Sopenharmony_ci  assert.strictEqual(size(new ArrayBuffer(10)), 1);
2521cb0ef41Sopenharmony_ci}
2531cb0ef41Sopenharmony_ci
2541cb0ef41Sopenharmony_ci{
2551cb0ef41Sopenharmony_ci  const r = new ReadableStream({
2561cb0ef41Sopenharmony_ci    async start() {
2571cb0ef41Sopenharmony_ci      throw new Error('boom');
2581cb0ef41Sopenharmony_ci    }
2591cb0ef41Sopenharmony_ci  });
2601cb0ef41Sopenharmony_ci
2611cb0ef41Sopenharmony_ci  setImmediate(() => {
2621cb0ef41Sopenharmony_ci    assert.strictEqual(r[kState].state, 'errored');
2631cb0ef41Sopenharmony_ci    assert.match(r[kState].storedError?.message, /boom/);
2641cb0ef41Sopenharmony_ci  });
2651cb0ef41Sopenharmony_ci}
2661cb0ef41Sopenharmony_ci
2671cb0ef41Sopenharmony_ci{
2681cb0ef41Sopenharmony_ci  const data = Buffer.from('hello');
2691cb0ef41Sopenharmony_ci  const r = new ReadableStream({
2701cb0ef41Sopenharmony_ci    start(controller) {
2711cb0ef41Sopenharmony_ci      controller.enqueue(data);
2721cb0ef41Sopenharmony_ci      controller.close();
2731cb0ef41Sopenharmony_ci    },
2741cb0ef41Sopenharmony_ci  });
2751cb0ef41Sopenharmony_ci
2761cb0ef41Sopenharmony_ci  (async function read() {
2771cb0ef41Sopenharmony_ci    const reader = r.getReader();
2781cb0ef41Sopenharmony_ci    let res = await reader.read();
2791cb0ef41Sopenharmony_ci    if (res.done) return;
2801cb0ef41Sopenharmony_ci    const buf = Buffer.from(res.value);
2811cb0ef41Sopenharmony_ci    assert.strictEqual(buf.toString(), data.toString());
2821cb0ef41Sopenharmony_ci    res = await reader.read();
2831cb0ef41Sopenharmony_ci    assert(res.done);
2841cb0ef41Sopenharmony_ci  })().then(common.mustCall());
2851cb0ef41Sopenharmony_ci}
2861cb0ef41Sopenharmony_ci
2871cb0ef41Sopenharmony_ci{
2881cb0ef41Sopenharmony_ci  const r = new ReadableStream({
2891cb0ef41Sopenharmony_ci    start(controller) {
2901cb0ef41Sopenharmony_ci      controller.close();
2911cb0ef41Sopenharmony_ci    },
2921cb0ef41Sopenharmony_ci  });
2931cb0ef41Sopenharmony_ci
2941cb0ef41Sopenharmony_ci  (async function read() {
2951cb0ef41Sopenharmony_ci    const reader = r.getReader();
2961cb0ef41Sopenharmony_ci    const res = await reader.read();
2971cb0ef41Sopenharmony_ci    assert(res.done);
2981cb0ef41Sopenharmony_ci  })().then(common.mustCall());
2991cb0ef41Sopenharmony_ci}
3001cb0ef41Sopenharmony_ci
3011cb0ef41Sopenharmony_ciassert.throws(() => {
3021cb0ef41Sopenharmony_ci  new ReadableStream({
3031cb0ef41Sopenharmony_ci    get start() { throw new Error('boom1'); }
3041cb0ef41Sopenharmony_ci  }, {
3051cb0ef41Sopenharmony_ci    get size() { throw new Error('boom2'); }
3061cb0ef41Sopenharmony_ci  });
3071cb0ef41Sopenharmony_ci}, /boom2/);
3081cb0ef41Sopenharmony_ci
3091cb0ef41Sopenharmony_ci{
3101cb0ef41Sopenharmony_ci  const stream = new ReadableStream();
3111cb0ef41Sopenharmony_ci  const reader = stream.getReader();
3121cb0ef41Sopenharmony_ci
3131cb0ef41Sopenharmony_ci  assert(stream.locked);
3141cb0ef41Sopenharmony_ci  assert.strictEqual(reader[kState].stream, stream);
3151cb0ef41Sopenharmony_ci  assert.strictEqual(stream[kState].reader, reader);
3161cb0ef41Sopenharmony_ci
3171cb0ef41Sopenharmony_ci  assert.throws(() => stream.getReader(), {
3181cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_STATE',
3191cb0ef41Sopenharmony_ci  });
3201cb0ef41Sopenharmony_ci
3211cb0ef41Sopenharmony_ci  assert(reader instanceof ReadableStreamDefaultReader);
3221cb0ef41Sopenharmony_ci
3231cb0ef41Sopenharmony_ci  assert(isPromise(reader.closed));
3241cb0ef41Sopenharmony_ci  assert.strictEqual(typeof reader.cancel, 'function');
3251cb0ef41Sopenharmony_ci  assert.strictEqual(typeof reader.read, 'function');
3261cb0ef41Sopenharmony_ci  assert.strictEqual(typeof reader.releaseLock, 'function');
3271cb0ef41Sopenharmony_ci
3281cb0ef41Sopenharmony_ci  const read1 = reader.read();
3291cb0ef41Sopenharmony_ci  const read2 = reader.read();
3301cb0ef41Sopenharmony_ci
3311cb0ef41Sopenharmony_ci  read1.then(common.mustNotCall(), common.mustCall());
3321cb0ef41Sopenharmony_ci  read2.then(common.mustNotCall(), common.mustCall());
3331cb0ef41Sopenharmony_ci
3341cb0ef41Sopenharmony_ci  assert.notStrictEqual(read1, read2);
3351cb0ef41Sopenharmony_ci
3361cb0ef41Sopenharmony_ci  assert.strictEqual(reader[kState].readRequests.length, 2);
3371cb0ef41Sopenharmony_ci
3381cb0ef41Sopenharmony_ci  delay().then(common.mustCall());
3391cb0ef41Sopenharmony_ci
3401cb0ef41Sopenharmony_ci  assert(stream.locked);
3411cb0ef41Sopenharmony_ci  reader.releaseLock();
3421cb0ef41Sopenharmony_ci  assert(!stream.locked);
3431cb0ef41Sopenharmony_ci}
3441cb0ef41Sopenharmony_ci
3451cb0ef41Sopenharmony_ci{
3461cb0ef41Sopenharmony_ci  const stream = new ReadableStream();
3471cb0ef41Sopenharmony_ci  const reader = stream.getReader();
3481cb0ef41Sopenharmony_ci  const closedBefore = reader.closed;
3491cb0ef41Sopenharmony_ci  assert(stream.locked);
3501cb0ef41Sopenharmony_ci  reader.releaseLock();
3511cb0ef41Sopenharmony_ci  assert(!stream.locked);
3521cb0ef41Sopenharmony_ci  const closedAfter = reader.closed;
3531cb0ef41Sopenharmony_ci
3541cb0ef41Sopenharmony_ci  assert.strictEqual(closedBefore, closedAfter);
3551cb0ef41Sopenharmony_ci
3561cb0ef41Sopenharmony_ci  assert.rejects(reader.read(), {
3571cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_STATE',
3581cb0ef41Sopenharmony_ci  });
3591cb0ef41Sopenharmony_ci
3601cb0ef41Sopenharmony_ci  assert.rejects(closedBefore, {
3611cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_STATE',
3621cb0ef41Sopenharmony_ci  });
3631cb0ef41Sopenharmony_ci}
3641cb0ef41Sopenharmony_ci
3651cb0ef41Sopenharmony_ci{
3661cb0ef41Sopenharmony_ci  const stream = new ReadableStream();
3671cb0ef41Sopenharmony_ci  const iterable = stream.values();
3681cb0ef41Sopenharmony_ci  readableStreamReaderGenericRelease(stream[kState].reader);
3691cb0ef41Sopenharmony_ci  assert.rejects(iterable.next(), {
3701cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_STATE',
3711cb0ef41Sopenharmony_ci  }).then(common.mustCall());
3721cb0ef41Sopenharmony_ci}
3731cb0ef41Sopenharmony_ci
3741cb0ef41Sopenharmony_ci{
3751cb0ef41Sopenharmony_ci  const stream = new ReadableStream();
3761cb0ef41Sopenharmony_ci  const iterable = stream.values();
3771cb0ef41Sopenharmony_ci  readableStreamReaderGenericRelease(stream[kState].reader);
3781cb0ef41Sopenharmony_ci  assert.rejects(iterable.return(), {
3791cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_STATE',
3801cb0ef41Sopenharmony_ci  }).then(common.mustCall());
3811cb0ef41Sopenharmony_ci}
3821cb0ef41Sopenharmony_ci
3831cb0ef41Sopenharmony_ci{
3841cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
3851cb0ef41Sopenharmony_ci    start(controller) {
3861cb0ef41Sopenharmony_ci      controller.enqueue(Buffer.from('hello'));
3871cb0ef41Sopenharmony_ci    }
3881cb0ef41Sopenharmony_ci  });
3891cb0ef41Sopenharmony_ci
3901cb0ef41Sopenharmony_ci  const reader = stream.getReader();
3911cb0ef41Sopenharmony_ci
3921cb0ef41Sopenharmony_ci  assert.rejects(stream.cancel(), {
3931cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_STATE',
3941cb0ef41Sopenharmony_ci  });
3951cb0ef41Sopenharmony_ci
3961cb0ef41Sopenharmony_ci  reader.cancel();
3971cb0ef41Sopenharmony_ci
3981cb0ef41Sopenharmony_ci  reader.read().then(common.mustCall(({ value, done }) => {
3991cb0ef41Sopenharmony_ci    assert.strictEqual(value, undefined);
4001cb0ef41Sopenharmony_ci    assert(done);
4011cb0ef41Sopenharmony_ci  }));
4021cb0ef41Sopenharmony_ci}
4031cb0ef41Sopenharmony_ci
4041cb0ef41Sopenharmony_ci{
4051cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
4061cb0ef41Sopenharmony_ci    start(controller) {
4071cb0ef41Sopenharmony_ci      controller.close();
4081cb0ef41Sopenharmony_ci    }
4091cb0ef41Sopenharmony_ci  });
4101cb0ef41Sopenharmony_ci  assert(!stream.locked);
4111cb0ef41Sopenharmony_ci
4121cb0ef41Sopenharmony_ci  const cancel1 = stream.cancel();
4131cb0ef41Sopenharmony_ci  const cancel2 = stream.cancel();
4141cb0ef41Sopenharmony_ci
4151cb0ef41Sopenharmony_ci  assert.notStrictEqual(cancel1, cancel2);
4161cb0ef41Sopenharmony_ci
4171cb0ef41Sopenharmony_ci  Promise.all([cancel1, cancel2]).then(common.mustCall((res) => {
4181cb0ef41Sopenharmony_ci    assert.deepStrictEqual(res, [undefined, undefined]);
4191cb0ef41Sopenharmony_ci  }));
4201cb0ef41Sopenharmony_ci}
4211cb0ef41Sopenharmony_ci
4221cb0ef41Sopenharmony_ci{
4231cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
4241cb0ef41Sopenharmony_ci    start(controller) {
4251cb0ef41Sopenharmony_ci      controller.close();
4261cb0ef41Sopenharmony_ci    }
4271cb0ef41Sopenharmony_ci  });
4281cb0ef41Sopenharmony_ci
4291cb0ef41Sopenharmony_ci  stream.getReader().releaseLock();
4301cb0ef41Sopenharmony_ci  stream.getReader().releaseLock();
4311cb0ef41Sopenharmony_ci  stream.getReader();
4321cb0ef41Sopenharmony_ci}
4331cb0ef41Sopenharmony_ci
4341cb0ef41Sopenharmony_ci{
4351cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
4361cb0ef41Sopenharmony_ci    start(controller) {
4371cb0ef41Sopenharmony_ci      controller.close();
4381cb0ef41Sopenharmony_ci    }
4391cb0ef41Sopenharmony_ci  });
4401cb0ef41Sopenharmony_ci
4411cb0ef41Sopenharmony_ci  stream.getReader();
4421cb0ef41Sopenharmony_ci
4431cb0ef41Sopenharmony_ci  assert.throws(() => stream.getReader(), {
4441cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_STATE',
4451cb0ef41Sopenharmony_ci  });
4461cb0ef41Sopenharmony_ci}
4471cb0ef41Sopenharmony_ci
4481cb0ef41Sopenharmony_ci{
4491cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
4501cb0ef41Sopenharmony_ci    start(controller) {
4511cb0ef41Sopenharmony_ci      controller.close();
4521cb0ef41Sopenharmony_ci    },
4531cb0ef41Sopenharmony_ci  });
4541cb0ef41Sopenharmony_ci
4551cb0ef41Sopenharmony_ci  const reader = stream.getReader();
4561cb0ef41Sopenharmony_ci
4571cb0ef41Sopenharmony_ci  reader.closed.then(common.mustCall());
4581cb0ef41Sopenharmony_ci
4591cb0ef41Sopenharmony_ci  reader.read().then(common.mustCall(({ value, done }) => {
4601cb0ef41Sopenharmony_ci    assert.strictEqual(value, undefined);
4611cb0ef41Sopenharmony_ci    assert(done);
4621cb0ef41Sopenharmony_ci    reader.read().then(common.mustCall(({ value, done }) => {
4631cb0ef41Sopenharmony_ci      assert.strictEqual(value, undefined);
4641cb0ef41Sopenharmony_ci      assert(done);
4651cb0ef41Sopenharmony_ci    }));
4661cb0ef41Sopenharmony_ci  }));
4671cb0ef41Sopenharmony_ci}
4681cb0ef41Sopenharmony_ci
4691cb0ef41Sopenharmony_ci{
4701cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
4711cb0ef41Sopenharmony_ci    start(controller) {
4721cb0ef41Sopenharmony_ci      controller.close();
4731cb0ef41Sopenharmony_ci    },
4741cb0ef41Sopenharmony_ci  });
4751cb0ef41Sopenharmony_ci
4761cb0ef41Sopenharmony_ci  const reader = stream.getReader();
4771cb0ef41Sopenharmony_ci
4781cb0ef41Sopenharmony_ci  const closedBefore = reader.closed;
4791cb0ef41Sopenharmony_ci  reader.releaseLock();
4801cb0ef41Sopenharmony_ci  const closedAfter = reader.closed;
4811cb0ef41Sopenharmony_ci  assert.notStrictEqual(closedBefore, closedAfter);
4821cb0ef41Sopenharmony_ci
4831cb0ef41Sopenharmony_ci  closedBefore.then(common.mustCall());
4841cb0ef41Sopenharmony_ci  assert.rejects(closedAfter, {
4851cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_STATE',
4861cb0ef41Sopenharmony_ci  });
4871cb0ef41Sopenharmony_ci}
4881cb0ef41Sopenharmony_ci
4891cb0ef41Sopenharmony_ci{
4901cb0ef41Sopenharmony_ci  let c;
4911cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
4921cb0ef41Sopenharmony_ci    start(controller) {
4931cb0ef41Sopenharmony_ci      c = controller;
4941cb0ef41Sopenharmony_ci    },
4951cb0ef41Sopenharmony_ci  });
4961cb0ef41Sopenharmony_ci
4971cb0ef41Sopenharmony_ci  const reader = stream.getReader();
4981cb0ef41Sopenharmony_ci  c.close();
4991cb0ef41Sopenharmony_ci
5001cb0ef41Sopenharmony_ci  const closedBefore = reader.closed;
5011cb0ef41Sopenharmony_ci  reader.releaseLock();
5021cb0ef41Sopenharmony_ci  const closedAfter = reader.closed;
5031cb0ef41Sopenharmony_ci  assert.notStrictEqual(closedBefore, closedAfter);
5041cb0ef41Sopenharmony_ci
5051cb0ef41Sopenharmony_ci  closedBefore.then(common.mustCall());
5061cb0ef41Sopenharmony_ci  assert.rejects(closedAfter, {
5071cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_STATE',
5081cb0ef41Sopenharmony_ci  });
5091cb0ef41Sopenharmony_ci}
5101cb0ef41Sopenharmony_ci
5111cb0ef41Sopenharmony_ci{
5121cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
5131cb0ef41Sopenharmony_ci    start(controller) {
5141cb0ef41Sopenharmony_ci      controller.close();
5151cb0ef41Sopenharmony_ci    },
5161cb0ef41Sopenharmony_ci  });
5171cb0ef41Sopenharmony_ci
5181cb0ef41Sopenharmony_ci  const reader = stream.getReader();
5191cb0ef41Sopenharmony_ci
5201cb0ef41Sopenharmony_ci  const cancel1 = reader.cancel();
5211cb0ef41Sopenharmony_ci  const cancel2 = reader.cancel();
5221cb0ef41Sopenharmony_ci  const closed = reader.closed;
5231cb0ef41Sopenharmony_ci
5241cb0ef41Sopenharmony_ci  assert.notStrictEqual(cancel1, cancel2);
5251cb0ef41Sopenharmony_ci  assert.notStrictEqual(cancel1, closed);
5261cb0ef41Sopenharmony_ci  assert.notStrictEqual(cancel2, closed);
5271cb0ef41Sopenharmony_ci
5281cb0ef41Sopenharmony_ci  Promise.all([cancel1, cancel2]).then(common.mustCall((res) => {
5291cb0ef41Sopenharmony_ci    assert.deepStrictEqual(res, [undefined, undefined]);
5301cb0ef41Sopenharmony_ci  }));
5311cb0ef41Sopenharmony_ci}
5321cb0ef41Sopenharmony_ci
5331cb0ef41Sopenharmony_ci{
5341cb0ef41Sopenharmony_ci  let c;
5351cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
5361cb0ef41Sopenharmony_ci    start(controller) {
5371cb0ef41Sopenharmony_ci      c = controller;
5381cb0ef41Sopenharmony_ci    },
5391cb0ef41Sopenharmony_ci  });
5401cb0ef41Sopenharmony_ci
5411cb0ef41Sopenharmony_ci  const reader = stream.getReader();
5421cb0ef41Sopenharmony_ci  c.close();
5431cb0ef41Sopenharmony_ci
5441cb0ef41Sopenharmony_ci  const cancel1 = reader.cancel();
5451cb0ef41Sopenharmony_ci  const cancel2 = reader.cancel();
5461cb0ef41Sopenharmony_ci  const closed = reader.closed;
5471cb0ef41Sopenharmony_ci
5481cb0ef41Sopenharmony_ci  assert.notStrictEqual(cancel1, cancel2);
5491cb0ef41Sopenharmony_ci  assert.notStrictEqual(cancel1, closed);
5501cb0ef41Sopenharmony_ci  assert.notStrictEqual(cancel2, closed);
5511cb0ef41Sopenharmony_ci
5521cb0ef41Sopenharmony_ci  Promise.all([cancel1, cancel2]).then(common.mustCall((res) => {
5531cb0ef41Sopenharmony_ci    assert.deepStrictEqual(res, [undefined, undefined]);
5541cb0ef41Sopenharmony_ci  }));
5551cb0ef41Sopenharmony_ci}
5561cb0ef41Sopenharmony_ci
5571cb0ef41Sopenharmony_ci{
5581cb0ef41Sopenharmony_ci  const stream = new ReadableStream();
5591cb0ef41Sopenharmony_ci  const cancel1 = stream.cancel();
5601cb0ef41Sopenharmony_ci  const cancel2 = stream.cancel();
5611cb0ef41Sopenharmony_ci  assert.notStrictEqual(cancel1, cancel2);
5621cb0ef41Sopenharmony_ci
5631cb0ef41Sopenharmony_ci  Promise.all([cancel1, cancel2]).then(common.mustCall((res) => {
5641cb0ef41Sopenharmony_ci    assert.deepStrictEqual(res, [undefined, undefined]);
5651cb0ef41Sopenharmony_ci  }));
5661cb0ef41Sopenharmony_ci
5671cb0ef41Sopenharmony_ci  stream.getReader().read().then(common.mustCall(({ value, done }) => {
5681cb0ef41Sopenharmony_ci    assert.strictEqual(value, undefined);
5691cb0ef41Sopenharmony_ci    assert(done);
5701cb0ef41Sopenharmony_ci  }));
5711cb0ef41Sopenharmony_ci}
5721cb0ef41Sopenharmony_ci
5731cb0ef41Sopenharmony_ci{
5741cb0ef41Sopenharmony_ci  const error = new Error('boom');
5751cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
5761cb0ef41Sopenharmony_ci    start(controller) {
5771cb0ef41Sopenharmony_ci      controller.error(error);
5781cb0ef41Sopenharmony_ci    }
5791cb0ef41Sopenharmony_ci  });
5801cb0ef41Sopenharmony_ci  stream.getReader().releaseLock();
5811cb0ef41Sopenharmony_ci  const reader = stream.getReader();
5821cb0ef41Sopenharmony_ci  assert.rejects(reader.closed, error);
5831cb0ef41Sopenharmony_ci  assert.rejects(reader.read(), error);
5841cb0ef41Sopenharmony_ci  assert.rejects(reader.read(), error);
5851cb0ef41Sopenharmony_ci}
5861cb0ef41Sopenharmony_ci
5871cb0ef41Sopenharmony_ci{
5881cb0ef41Sopenharmony_ci  const error = new Error('boom');
5891cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
5901cb0ef41Sopenharmony_ci    start(controller) {
5911cb0ef41Sopenharmony_ci      controller.error(error);
5921cb0ef41Sopenharmony_ci    }
5931cb0ef41Sopenharmony_ci  });
5941cb0ef41Sopenharmony_ci  const reader = stream.getReader();
5951cb0ef41Sopenharmony_ci  const cancel1 = reader.cancel();
5961cb0ef41Sopenharmony_ci  const cancel2 = reader.cancel();
5971cb0ef41Sopenharmony_ci  assert.notStrictEqual(cancel1, cancel2);
5981cb0ef41Sopenharmony_ci  assert.rejects(cancel1, error);
5991cb0ef41Sopenharmony_ci  assert.rejects(cancel2, error);
6001cb0ef41Sopenharmony_ci}
6011cb0ef41Sopenharmony_ci
6021cb0ef41Sopenharmony_ci{
6031cb0ef41Sopenharmony_ci  const error = new Error('boom');
6041cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
6051cb0ef41Sopenharmony_ci    async start(controller) {
6061cb0ef41Sopenharmony_ci      throw error;
6071cb0ef41Sopenharmony_ci    }
6081cb0ef41Sopenharmony_ci  });
6091cb0ef41Sopenharmony_ci  stream.getReader().releaseLock();
6101cb0ef41Sopenharmony_ci  const reader = stream.getReader();
6111cb0ef41Sopenharmony_ci  assert.rejects(reader.closed, error);
6121cb0ef41Sopenharmony_ci  assert.rejects(reader.read(), error);
6131cb0ef41Sopenharmony_ci  assert.rejects(reader.read(), error);
6141cb0ef41Sopenharmony_ci}
6151cb0ef41Sopenharmony_ci
6161cb0ef41Sopenharmony_ci{
6171cb0ef41Sopenharmony_ci  const buf1 = Buffer.from('hello');
6181cb0ef41Sopenharmony_ci  const buf2 = Buffer.from('there');
6191cb0ef41Sopenharmony_ci  let doClose;
6201cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
6211cb0ef41Sopenharmony_ci    start(controller) {
6221cb0ef41Sopenharmony_ci      controller.enqueue(buf1);
6231cb0ef41Sopenharmony_ci      controller.enqueue(buf2);
6241cb0ef41Sopenharmony_ci      doClose = controller.close.bind(controller);
6251cb0ef41Sopenharmony_ci    }
6261cb0ef41Sopenharmony_ci  });
6271cb0ef41Sopenharmony_ci  const reader = stream.getReader();
6281cb0ef41Sopenharmony_ci  doClose();
6291cb0ef41Sopenharmony_ci  reader.read().then(common.mustCall(({ value, done }) => {
6301cb0ef41Sopenharmony_ci    assert.deepStrictEqual(value, buf1);
6311cb0ef41Sopenharmony_ci    assert(!done);
6321cb0ef41Sopenharmony_ci    reader.read().then(common.mustCall(({ value, done }) => {
6331cb0ef41Sopenharmony_ci      assert.deepStrictEqual(value, buf2);
6341cb0ef41Sopenharmony_ci      assert(!done);
6351cb0ef41Sopenharmony_ci      reader.read().then(common.mustCall(({ value, done }) => {
6361cb0ef41Sopenharmony_ci        assert.strictEqual(value, undefined);
6371cb0ef41Sopenharmony_ci        assert(done);
6381cb0ef41Sopenharmony_ci      }));
6391cb0ef41Sopenharmony_ci    }));
6401cb0ef41Sopenharmony_ci  }));
6411cb0ef41Sopenharmony_ci}
6421cb0ef41Sopenharmony_ci
6431cb0ef41Sopenharmony_ci{
6441cb0ef41Sopenharmony_ci  const buf1 = Buffer.from('hello');
6451cb0ef41Sopenharmony_ci  const buf2 = Buffer.from('there');
6461cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
6471cb0ef41Sopenharmony_ci    start(controller) {
6481cb0ef41Sopenharmony_ci      controller.enqueue(buf1);
6491cb0ef41Sopenharmony_ci      controller.enqueue(buf2);
6501cb0ef41Sopenharmony_ci    }
6511cb0ef41Sopenharmony_ci  });
6521cb0ef41Sopenharmony_ci  const reader = stream.getReader();
6531cb0ef41Sopenharmony_ci  reader.read().then(common.mustCall(({ value, done }) => {
6541cb0ef41Sopenharmony_ci    assert.deepStrictEqual(value, buf1);
6551cb0ef41Sopenharmony_ci    assert(!done);
6561cb0ef41Sopenharmony_ci    reader.read().then(common.mustCall(({ value, done }) => {
6571cb0ef41Sopenharmony_ci      assert.deepStrictEqual(value, buf2);
6581cb0ef41Sopenharmony_ci      assert(!done);
6591cb0ef41Sopenharmony_ci      reader.read().then(common.mustNotCall());
6601cb0ef41Sopenharmony_ci      delay().then(common.mustCall());
6611cb0ef41Sopenharmony_ci    }));
6621cb0ef41Sopenharmony_ci  }));
6631cb0ef41Sopenharmony_ci}
6641cb0ef41Sopenharmony_ci
6651cb0ef41Sopenharmony_ci{
6661cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
6671cb0ef41Sopenharmony_ci    start(controller) {
6681cb0ef41Sopenharmony_ci      controller.enqueue('a');
6691cb0ef41Sopenharmony_ci      controller.enqueue('b');
6701cb0ef41Sopenharmony_ci      controller.close();
6711cb0ef41Sopenharmony_ci    }
6721cb0ef41Sopenharmony_ci  });
6731cb0ef41Sopenharmony_ci
6741cb0ef41Sopenharmony_ci  const { 0: s1, 1: s2 } = stream.tee();
6751cb0ef41Sopenharmony_ci
6761cb0ef41Sopenharmony_ci  assert(s1 instanceof ReadableStream);
6771cb0ef41Sopenharmony_ci  assert(s2 instanceof ReadableStream);
6781cb0ef41Sopenharmony_ci
6791cb0ef41Sopenharmony_ci  async function read(stream) {
6801cb0ef41Sopenharmony_ci    const reader = stream.getReader();
6811cb0ef41Sopenharmony_ci    assert.deepStrictEqual(
6821cb0ef41Sopenharmony_ci      await reader.read(), { value: 'a', done: false });
6831cb0ef41Sopenharmony_ci    assert.deepStrictEqual(
6841cb0ef41Sopenharmony_ci      await reader.read(), { value: 'b', done: false });
6851cb0ef41Sopenharmony_ci    assert.deepStrictEqual(
6861cb0ef41Sopenharmony_ci      await reader.read(), { value: undefined, done: true });
6871cb0ef41Sopenharmony_ci  }
6881cb0ef41Sopenharmony_ci
6891cb0ef41Sopenharmony_ci  Promise.all([
6901cb0ef41Sopenharmony_ci    read(s1),
6911cb0ef41Sopenharmony_ci    read(s2),
6921cb0ef41Sopenharmony_ci  ]).then(common.mustCall());
6931cb0ef41Sopenharmony_ci}
6941cb0ef41Sopenharmony_ci
6951cb0ef41Sopenharmony_ci{
6961cb0ef41Sopenharmony_ci  const error = new Error('boom');
6971cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
6981cb0ef41Sopenharmony_ci    start(controller) {
6991cb0ef41Sopenharmony_ci      controller.enqueue('a');
7001cb0ef41Sopenharmony_ci      controller.enqueue('b');
7011cb0ef41Sopenharmony_ci    },
7021cb0ef41Sopenharmony_ci    pull() { throw error; }
7031cb0ef41Sopenharmony_ci  });
7041cb0ef41Sopenharmony_ci
7051cb0ef41Sopenharmony_ci  const { 0: s1, 1: s2 } = stream.tee();
7061cb0ef41Sopenharmony_ci
7071cb0ef41Sopenharmony_ci  assert(stream.locked);
7081cb0ef41Sopenharmony_ci
7091cb0ef41Sopenharmony_ci  assert(s1 instanceof ReadableStream);
7101cb0ef41Sopenharmony_ci  assert(s2 instanceof ReadableStream);
7111cb0ef41Sopenharmony_ci
7121cb0ef41Sopenharmony_ci  const reader1 = s1.getReader();
7131cb0ef41Sopenharmony_ci  const reader2 = s2.getReader();
7141cb0ef41Sopenharmony_ci
7151cb0ef41Sopenharmony_ci  const closed1 = reader1.closed;
7161cb0ef41Sopenharmony_ci  const closed2 = reader2.closed;
7171cb0ef41Sopenharmony_ci
7181cb0ef41Sopenharmony_ci  assert.notStrictEqual(closed1, closed2);
7191cb0ef41Sopenharmony_ci
7201cb0ef41Sopenharmony_ci  assert.rejects(closed1, error);
7211cb0ef41Sopenharmony_ci  assert.rejects(closed2, error);
7221cb0ef41Sopenharmony_ci}
7231cb0ef41Sopenharmony_ci
7241cb0ef41Sopenharmony_ci{
7251cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
7261cb0ef41Sopenharmony_ci    start(controller) {
7271cb0ef41Sopenharmony_ci      controller.enqueue('a');
7281cb0ef41Sopenharmony_ci      controller.enqueue('b');
7291cb0ef41Sopenharmony_ci      controller.close();
7301cb0ef41Sopenharmony_ci    }
7311cb0ef41Sopenharmony_ci  });
7321cb0ef41Sopenharmony_ci
7331cb0ef41Sopenharmony_ci  const { 0: s1, 1: s2 } = stream.tee();
7341cb0ef41Sopenharmony_ci
7351cb0ef41Sopenharmony_ci  assert(s1 instanceof ReadableStream);
7361cb0ef41Sopenharmony_ci  assert(s2 instanceof ReadableStream);
7371cb0ef41Sopenharmony_ci
7381cb0ef41Sopenharmony_ci  s2.cancel();
7391cb0ef41Sopenharmony_ci
7401cb0ef41Sopenharmony_ci  async function read(stream, canceled = false) {
7411cb0ef41Sopenharmony_ci    const reader = stream.getReader();
7421cb0ef41Sopenharmony_ci    if (!canceled) {
7431cb0ef41Sopenharmony_ci      assert.deepStrictEqual(
7441cb0ef41Sopenharmony_ci        await reader.read(), { value: 'a', done: false });
7451cb0ef41Sopenharmony_ci      assert.deepStrictEqual(
7461cb0ef41Sopenharmony_ci        await reader.read(), { value: 'b', done: false });
7471cb0ef41Sopenharmony_ci    }
7481cb0ef41Sopenharmony_ci    assert.deepStrictEqual(
7491cb0ef41Sopenharmony_ci      await reader.read(), { value: undefined, done: true });
7501cb0ef41Sopenharmony_ci  }
7511cb0ef41Sopenharmony_ci
7521cb0ef41Sopenharmony_ci  Promise.all([
7531cb0ef41Sopenharmony_ci    read(s1),
7541cb0ef41Sopenharmony_ci    read(s2, true),
7551cb0ef41Sopenharmony_ci  ]).then(common.mustCall());
7561cb0ef41Sopenharmony_ci}
7571cb0ef41Sopenharmony_ci
7581cb0ef41Sopenharmony_ci{
7591cb0ef41Sopenharmony_ci  const error1 = new Error('boom1');
7601cb0ef41Sopenharmony_ci  const error2 = new Error('boom2');
7611cb0ef41Sopenharmony_ci
7621cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
7631cb0ef41Sopenharmony_ci    cancel(reason) {
7641cb0ef41Sopenharmony_ci      assert.deepStrictEqual(reason, [error1, error2]);
7651cb0ef41Sopenharmony_ci    }
7661cb0ef41Sopenharmony_ci  });
7671cb0ef41Sopenharmony_ci
7681cb0ef41Sopenharmony_ci  const { 0: s1, 1: s2 } = stream.tee();
7691cb0ef41Sopenharmony_ci  s1.cancel(error1);
7701cb0ef41Sopenharmony_ci  s2.cancel(error2);
7711cb0ef41Sopenharmony_ci}
7721cb0ef41Sopenharmony_ci
7731cb0ef41Sopenharmony_ci{
7741cb0ef41Sopenharmony_ci  const error1 = new Error('boom1');
7751cb0ef41Sopenharmony_ci  const error2 = new Error('boom2');
7761cb0ef41Sopenharmony_ci
7771cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
7781cb0ef41Sopenharmony_ci    cancel(reason) {
7791cb0ef41Sopenharmony_ci      assert.deepStrictEqual(reason, [error1, error2]);
7801cb0ef41Sopenharmony_ci    }
7811cb0ef41Sopenharmony_ci  });
7821cb0ef41Sopenharmony_ci
7831cb0ef41Sopenharmony_ci  const { 0: s1, 1: s2 } = stream.tee();
7841cb0ef41Sopenharmony_ci  s2.cancel(error2);
7851cb0ef41Sopenharmony_ci  s1.cancel(error1);
7861cb0ef41Sopenharmony_ci}
7871cb0ef41Sopenharmony_ci
7881cb0ef41Sopenharmony_ci{
7891cb0ef41Sopenharmony_ci  const error = new Error('boom1');
7901cb0ef41Sopenharmony_ci
7911cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
7921cb0ef41Sopenharmony_ci    cancel() {
7931cb0ef41Sopenharmony_ci      throw error;
7941cb0ef41Sopenharmony_ci    }
7951cb0ef41Sopenharmony_ci  });
7961cb0ef41Sopenharmony_ci
7971cb0ef41Sopenharmony_ci  const { 0: s1, 1: s2 } = stream.tee();
7981cb0ef41Sopenharmony_ci
7991cb0ef41Sopenharmony_ci  assert.rejects(s1.cancel(), error);
8001cb0ef41Sopenharmony_ci  assert.rejects(s2.cancel(), error);
8011cb0ef41Sopenharmony_ci}
8021cb0ef41Sopenharmony_ci
8031cb0ef41Sopenharmony_ci{
8041cb0ef41Sopenharmony_ci  const error = new Error('boom1');
8051cb0ef41Sopenharmony_ci  let c;
8061cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
8071cb0ef41Sopenharmony_ci    start(controller) {
8081cb0ef41Sopenharmony_ci      c = controller;
8091cb0ef41Sopenharmony_ci    }
8101cb0ef41Sopenharmony_ci  });
8111cb0ef41Sopenharmony_ci
8121cb0ef41Sopenharmony_ci  const { 0: s1, 1: s2 } = stream.tee();
8131cb0ef41Sopenharmony_ci  c.error(error);
8141cb0ef41Sopenharmony_ci
8151cb0ef41Sopenharmony_ci  assert.rejects(s1.cancel(), error);
8161cb0ef41Sopenharmony_ci  assert.rejects(s2.cancel(), error);
8171cb0ef41Sopenharmony_ci}
8181cb0ef41Sopenharmony_ci
8191cb0ef41Sopenharmony_ci{
8201cb0ef41Sopenharmony_ci  const error = new Error('boom1');
8211cb0ef41Sopenharmony_ci  let c;
8221cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
8231cb0ef41Sopenharmony_ci    start(controller) {
8241cb0ef41Sopenharmony_ci      c = controller;
8251cb0ef41Sopenharmony_ci    }
8261cb0ef41Sopenharmony_ci  });
8271cb0ef41Sopenharmony_ci
8281cb0ef41Sopenharmony_ci  const { 0: s1, 1: s2 } = stream.tee();
8291cb0ef41Sopenharmony_ci
8301cb0ef41Sopenharmony_ci  const reader1 = s1.getReader();
8311cb0ef41Sopenharmony_ci  const reader2 = s2.getReader();
8321cb0ef41Sopenharmony_ci
8331cb0ef41Sopenharmony_ci  assert.rejects(reader1.closed, error);
8341cb0ef41Sopenharmony_ci  assert.rejects(reader2.closed, error);
8351cb0ef41Sopenharmony_ci
8361cb0ef41Sopenharmony_ci  assert.rejects(reader1.read(), error);
8371cb0ef41Sopenharmony_ci  assert.rejects(reader2.read(), error);
8381cb0ef41Sopenharmony_ci
8391cb0ef41Sopenharmony_ci  setImmediate(() => c.error(error));
8401cb0ef41Sopenharmony_ci}
8411cb0ef41Sopenharmony_ci
8421cb0ef41Sopenharmony_ci{
8431cb0ef41Sopenharmony_ci  let pullCount = 0;
8441cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
8451cb0ef41Sopenharmony_ci    pull(controller) {
8461cb0ef41Sopenharmony_ci      if (pullCount)
8471cb0ef41Sopenharmony_ci        controller.enqueue(pullCount);
8481cb0ef41Sopenharmony_ci      pullCount++;
8491cb0ef41Sopenharmony_ci    },
8501cb0ef41Sopenharmony_ci  });
8511cb0ef41Sopenharmony_ci
8521cb0ef41Sopenharmony_ci  const reader = stream.getReader();
8531cb0ef41Sopenharmony_ci
8541cb0ef41Sopenharmony_ci  queueMicrotask(common.mustCall(() => {
8551cb0ef41Sopenharmony_ci    assert.strictEqual(pullCount, 1);
8561cb0ef41Sopenharmony_ci    reader.read().then(common.mustCall(({ value, done }) => {
8571cb0ef41Sopenharmony_ci      assert.strictEqual(value, 1);
8581cb0ef41Sopenharmony_ci      assert(!done);
8591cb0ef41Sopenharmony_ci
8601cb0ef41Sopenharmony_ci      reader.read().then(common.mustCall(({ value, done }) => {
8611cb0ef41Sopenharmony_ci        assert.strictEqual(value, 2);
8621cb0ef41Sopenharmony_ci        assert(!done);
8631cb0ef41Sopenharmony_ci      }));
8641cb0ef41Sopenharmony_ci
8651cb0ef41Sopenharmony_ci    }));
8661cb0ef41Sopenharmony_ci  }));
8671cb0ef41Sopenharmony_ci}
8681cb0ef41Sopenharmony_ci
8691cb0ef41Sopenharmony_ci{
8701cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
8711cb0ef41Sopenharmony_ci    start(controller) {
8721cb0ef41Sopenharmony_ci      controller.enqueue('a');
8731cb0ef41Sopenharmony_ci    },
8741cb0ef41Sopenharmony_ci    pull: common.mustCall(),
8751cb0ef41Sopenharmony_ci  });
8761cb0ef41Sopenharmony_ci
8771cb0ef41Sopenharmony_ci  stream.getReader().read().then(common.mustCall(({ value, done }) => {
8781cb0ef41Sopenharmony_ci    assert.strictEqual(value, 'a');
8791cb0ef41Sopenharmony_ci    assert(!done);
8801cb0ef41Sopenharmony_ci  }));
8811cb0ef41Sopenharmony_ci}
8821cb0ef41Sopenharmony_ci
8831cb0ef41Sopenharmony_ci{
8841cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
8851cb0ef41Sopenharmony_ci    start(controller) {
8861cb0ef41Sopenharmony_ci      controller.enqueue('a');
8871cb0ef41Sopenharmony_ci      controller.enqueue('b');
8881cb0ef41Sopenharmony_ci    },
8891cb0ef41Sopenharmony_ci    pull: common.mustCall(),
8901cb0ef41Sopenharmony_ci  });
8911cb0ef41Sopenharmony_ci
8921cb0ef41Sopenharmony_ci  const reader = stream.getReader();
8931cb0ef41Sopenharmony_ci  reader.read().then(common.mustCall(({ value, done }) => {
8941cb0ef41Sopenharmony_ci    assert.strictEqual(value, 'a');
8951cb0ef41Sopenharmony_ci    assert(!done);
8961cb0ef41Sopenharmony_ci
8971cb0ef41Sopenharmony_ci    reader.read().then(common.mustCall(({ value, done }) => {
8981cb0ef41Sopenharmony_ci      assert.strictEqual(value, 'b');
8991cb0ef41Sopenharmony_ci      assert(!done);
9001cb0ef41Sopenharmony_ci    }));
9011cb0ef41Sopenharmony_ci  }));
9021cb0ef41Sopenharmony_ci}
9031cb0ef41Sopenharmony_ci
9041cb0ef41Sopenharmony_ci{
9051cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
9061cb0ef41Sopenharmony_ci    start(controller) {
9071cb0ef41Sopenharmony_ci      controller.enqueue('a');
9081cb0ef41Sopenharmony_ci      controller.enqueue('b');
9091cb0ef41Sopenharmony_ci      controller.close();
9101cb0ef41Sopenharmony_ci    },
9111cb0ef41Sopenharmony_ci    pull: common.mustNotCall(),
9121cb0ef41Sopenharmony_ci  });
9131cb0ef41Sopenharmony_ci
9141cb0ef41Sopenharmony_ci  const reader = stream.getReader();
9151cb0ef41Sopenharmony_ci  reader.read().then(common.mustCall(({ value, done }) => {
9161cb0ef41Sopenharmony_ci    assert.strictEqual(value, 'a');
9171cb0ef41Sopenharmony_ci    assert(!done);
9181cb0ef41Sopenharmony_ci
9191cb0ef41Sopenharmony_ci    reader.read().then(common.mustCall(({ value, done }) => {
9201cb0ef41Sopenharmony_ci      assert.strictEqual(value, 'b');
9211cb0ef41Sopenharmony_ci      assert(!done);
9221cb0ef41Sopenharmony_ci
9231cb0ef41Sopenharmony_ci      reader.read().then(common.mustCall(({ value, done }) => {
9241cb0ef41Sopenharmony_ci        assert.strictEqual(value, undefined);
9251cb0ef41Sopenharmony_ci        assert(done);
9261cb0ef41Sopenharmony_ci      }));
9271cb0ef41Sopenharmony_ci
9281cb0ef41Sopenharmony_ci    }));
9291cb0ef41Sopenharmony_ci  }));
9301cb0ef41Sopenharmony_ci}
9311cb0ef41Sopenharmony_ci
9321cb0ef41Sopenharmony_ci{
9331cb0ef41Sopenharmony_ci  let res;
9341cb0ef41Sopenharmony_ci  let promise;
9351cb0ef41Sopenharmony_ci  let calls = 0;
9361cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
9371cb0ef41Sopenharmony_ci    pull(controller) {
9381cb0ef41Sopenharmony_ci      controller.enqueue(++calls);
9391cb0ef41Sopenharmony_ci      promise = new Promise((resolve) => res = resolve);
9401cb0ef41Sopenharmony_ci      return promise;
9411cb0ef41Sopenharmony_ci    }
9421cb0ef41Sopenharmony_ci  });
9431cb0ef41Sopenharmony_ci
9441cb0ef41Sopenharmony_ci  const reader = stream.getReader();
9451cb0ef41Sopenharmony_ci
9461cb0ef41Sopenharmony_ci  (async () => {
9471cb0ef41Sopenharmony_ci    await reader.read();
9481cb0ef41Sopenharmony_ci    assert.strictEqual(calls, 1);
9491cb0ef41Sopenharmony_ci    await delay();
9501cb0ef41Sopenharmony_ci    assert.strictEqual(calls, 1);
9511cb0ef41Sopenharmony_ci    res();
9521cb0ef41Sopenharmony_ci    await delay();
9531cb0ef41Sopenharmony_ci    assert.strictEqual(calls, 2);
9541cb0ef41Sopenharmony_ci  })().then(common.mustCall());
9551cb0ef41Sopenharmony_ci}
9561cb0ef41Sopenharmony_ci
9571cb0ef41Sopenharmony_ci{
9581cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
9591cb0ef41Sopenharmony_ci    start(controller) {
9601cb0ef41Sopenharmony_ci      controller.enqueue('a');
9611cb0ef41Sopenharmony_ci      controller.enqueue('b');
9621cb0ef41Sopenharmony_ci      controller.enqueue('c');
9631cb0ef41Sopenharmony_ci    },
9641cb0ef41Sopenharmony_ci    pull: common.mustCall(4),
9651cb0ef41Sopenharmony_ci  }, {
9661cb0ef41Sopenharmony_ci    highWaterMark: Infinity,
9671cb0ef41Sopenharmony_ci    size() { return 1; }
9681cb0ef41Sopenharmony_ci  });
9691cb0ef41Sopenharmony_ci
9701cb0ef41Sopenharmony_ci  const reader = stream.getReader();
9711cb0ef41Sopenharmony_ci  (async () => {
9721cb0ef41Sopenharmony_ci    await delay();
9731cb0ef41Sopenharmony_ci    await reader.read();
9741cb0ef41Sopenharmony_ci    await reader.read();
9751cb0ef41Sopenharmony_ci    await reader.read();
9761cb0ef41Sopenharmony_ci  })().then(common.mustCall());
9771cb0ef41Sopenharmony_ci}
9781cb0ef41Sopenharmony_ci
9791cb0ef41Sopenharmony_ci{
9801cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
9811cb0ef41Sopenharmony_ci    start(controller) {
9821cb0ef41Sopenharmony_ci      controller.enqueue('a');
9831cb0ef41Sopenharmony_ci      controller.enqueue('b');
9841cb0ef41Sopenharmony_ci      controller.enqueue('c');
9851cb0ef41Sopenharmony_ci      controller.close();
9861cb0ef41Sopenharmony_ci    },
9871cb0ef41Sopenharmony_ci    pull: common.mustNotCall(),
9881cb0ef41Sopenharmony_ci  }, {
9891cb0ef41Sopenharmony_ci    highWaterMark: Infinity,
9901cb0ef41Sopenharmony_ci    size() { return 1; }
9911cb0ef41Sopenharmony_ci  });
9921cb0ef41Sopenharmony_ci
9931cb0ef41Sopenharmony_ci  const reader = stream.getReader();
9941cb0ef41Sopenharmony_ci  (async () => {
9951cb0ef41Sopenharmony_ci    await delay();
9961cb0ef41Sopenharmony_ci    await reader.read();
9971cb0ef41Sopenharmony_ci    await reader.read();
9981cb0ef41Sopenharmony_ci    await reader.read();
9991cb0ef41Sopenharmony_ci  })().then(common.mustCall());
10001cb0ef41Sopenharmony_ci}
10011cb0ef41Sopenharmony_ci
10021cb0ef41Sopenharmony_ci{
10031cb0ef41Sopenharmony_ci  let calls = 0;
10041cb0ef41Sopenharmony_ci  let res;
10051cb0ef41Sopenharmony_ci  const ready = new Promise((resolve) => res = resolve);
10061cb0ef41Sopenharmony_ci
10071cb0ef41Sopenharmony_ci  new ReadableStream({
10081cb0ef41Sopenharmony_ci    pull(controller) {
10091cb0ef41Sopenharmony_ci      controller.enqueue(++calls);
10101cb0ef41Sopenharmony_ci      if (calls === 4)
10111cb0ef41Sopenharmony_ci        res();
10121cb0ef41Sopenharmony_ci    }
10131cb0ef41Sopenharmony_ci  }, {
10141cb0ef41Sopenharmony_ci    size() { return 1; },
10151cb0ef41Sopenharmony_ci    highWaterMark: 4
10161cb0ef41Sopenharmony_ci  });
10171cb0ef41Sopenharmony_ci
10181cb0ef41Sopenharmony_ci  ready.then(common.mustCall(() => {
10191cb0ef41Sopenharmony_ci    assert.strictEqual(calls, 4);
10201cb0ef41Sopenharmony_ci  }));
10211cb0ef41Sopenharmony_ci}
10221cb0ef41Sopenharmony_ci
10231cb0ef41Sopenharmony_ci{
10241cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
10251cb0ef41Sopenharmony_ci    pull: common.mustCall((controller) => controller.close())
10261cb0ef41Sopenharmony_ci  });
10271cb0ef41Sopenharmony_ci
10281cb0ef41Sopenharmony_ci  const reader = stream.getReader();
10291cb0ef41Sopenharmony_ci
10301cb0ef41Sopenharmony_ci  reader.closed.then(common.mustCall());
10311cb0ef41Sopenharmony_ci}
10321cb0ef41Sopenharmony_ci
10331cb0ef41Sopenharmony_ci{
10341cb0ef41Sopenharmony_ci  const error = new Error('boom');
10351cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
10361cb0ef41Sopenharmony_ci    pull: common.mustCall((controller) => controller.error(error))
10371cb0ef41Sopenharmony_ci  });
10381cb0ef41Sopenharmony_ci
10391cb0ef41Sopenharmony_ci  const reader = stream.getReader();
10401cb0ef41Sopenharmony_ci
10411cb0ef41Sopenharmony_ci  assert.rejects(reader.closed, error);
10421cb0ef41Sopenharmony_ci}
10431cb0ef41Sopenharmony_ci
10441cb0ef41Sopenharmony_ci{
10451cb0ef41Sopenharmony_ci  const error = new Error('boom');
10461cb0ef41Sopenharmony_ci  const error2 = new Error('boom2');
10471cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
10481cb0ef41Sopenharmony_ci    pull: common.mustCall((controller) => {
10491cb0ef41Sopenharmony_ci      controller.error(error);
10501cb0ef41Sopenharmony_ci      throw error2;
10511cb0ef41Sopenharmony_ci    })
10521cb0ef41Sopenharmony_ci  });
10531cb0ef41Sopenharmony_ci
10541cb0ef41Sopenharmony_ci  const reader = stream.getReader();
10551cb0ef41Sopenharmony_ci
10561cb0ef41Sopenharmony_ci  assert.rejects(reader.closed, error);
10571cb0ef41Sopenharmony_ci}
10581cb0ef41Sopenharmony_ci
10591cb0ef41Sopenharmony_ci{
10601cb0ef41Sopenharmony_ci  let startCalled = false;
10611cb0ef41Sopenharmony_ci  new ReadableStream({
10621cb0ef41Sopenharmony_ci    start: common.mustCall((controller) => {
10631cb0ef41Sopenharmony_ci      controller.enqueue('a');
10641cb0ef41Sopenharmony_ci      controller.close();
10651cb0ef41Sopenharmony_ci      assert.throws(() => controller.enqueue('b'), {
10661cb0ef41Sopenharmony_ci        code: 'ERR_INVALID_STATE'
10671cb0ef41Sopenharmony_ci      });
10681cb0ef41Sopenharmony_ci      startCalled = true;
10691cb0ef41Sopenharmony_ci    })
10701cb0ef41Sopenharmony_ci  });
10711cb0ef41Sopenharmony_ci  assert(startCalled);
10721cb0ef41Sopenharmony_ci}
10731cb0ef41Sopenharmony_ci
10741cb0ef41Sopenharmony_ci{
10751cb0ef41Sopenharmony_ci  let startCalled = false;
10761cb0ef41Sopenharmony_ci  new ReadableStream({
10771cb0ef41Sopenharmony_ci    start: common.mustCall((controller) => {
10781cb0ef41Sopenharmony_ci      controller.close();
10791cb0ef41Sopenharmony_ci      assert.throws(() => controller.enqueue('b'), {
10801cb0ef41Sopenharmony_ci        code: 'ERR_INVALID_STATE'
10811cb0ef41Sopenharmony_ci      });
10821cb0ef41Sopenharmony_ci      startCalled = true;
10831cb0ef41Sopenharmony_ci    })
10841cb0ef41Sopenharmony_ci  });
10851cb0ef41Sopenharmony_ci  assert(startCalled);
10861cb0ef41Sopenharmony_ci}
10871cb0ef41Sopenharmony_ci
10881cb0ef41Sopenharmony_ci{
10891cb0ef41Sopenharmony_ci  class Source {
10901cb0ef41Sopenharmony_ci    startCalled = false;
10911cb0ef41Sopenharmony_ci    pullCalled = false;
10921cb0ef41Sopenharmony_ci    cancelCalled = false;
10931cb0ef41Sopenharmony_ci
10941cb0ef41Sopenharmony_ci    start(controller) {
10951cb0ef41Sopenharmony_ci      assert.strictEqual(this, source);
10961cb0ef41Sopenharmony_ci      this.startCalled = true;
10971cb0ef41Sopenharmony_ci      controller.enqueue('a');
10981cb0ef41Sopenharmony_ci    }
10991cb0ef41Sopenharmony_ci
11001cb0ef41Sopenharmony_ci    pull() {
11011cb0ef41Sopenharmony_ci      assert.strictEqual(this, source);
11021cb0ef41Sopenharmony_ci      this.pullCalled = true;
11031cb0ef41Sopenharmony_ci    }
11041cb0ef41Sopenharmony_ci
11051cb0ef41Sopenharmony_ci    cancel() {
11061cb0ef41Sopenharmony_ci      assert.strictEqual(this, source);
11071cb0ef41Sopenharmony_ci      this.cancelCalled = true;
11081cb0ef41Sopenharmony_ci    }
11091cb0ef41Sopenharmony_ci  }
11101cb0ef41Sopenharmony_ci
11111cb0ef41Sopenharmony_ci  const source = new Source();
11121cb0ef41Sopenharmony_ci
11131cb0ef41Sopenharmony_ci  const stream = new ReadableStream(source);
11141cb0ef41Sopenharmony_ci  const reader = stream.getReader();
11151cb0ef41Sopenharmony_ci
11161cb0ef41Sopenharmony_ci  (async () => {
11171cb0ef41Sopenharmony_ci    await reader.read();
11181cb0ef41Sopenharmony_ci    reader.releaseLock();
11191cb0ef41Sopenharmony_ci    stream.cancel();
11201cb0ef41Sopenharmony_ci    assert(source.startCalled);
11211cb0ef41Sopenharmony_ci    assert(source.pullCalled);
11221cb0ef41Sopenharmony_ci    assert(source.cancelCalled);
11231cb0ef41Sopenharmony_ci  })().then(common.mustCall());
11241cb0ef41Sopenharmony_ci}
11251cb0ef41Sopenharmony_ci
11261cb0ef41Sopenharmony_ci{
11271cb0ef41Sopenharmony_ci  let startCalled = false;
11281cb0ef41Sopenharmony_ci  new ReadableStream({
11291cb0ef41Sopenharmony_ci    start(controller) {
11301cb0ef41Sopenharmony_ci      assert.strictEqual(controller.desiredSize, 10);
11311cb0ef41Sopenharmony_ci      controller.close();
11321cb0ef41Sopenharmony_ci      assert.strictEqual(controller.desiredSize, 0);
11331cb0ef41Sopenharmony_ci      startCalled = true;
11341cb0ef41Sopenharmony_ci    }
11351cb0ef41Sopenharmony_ci  }, {
11361cb0ef41Sopenharmony_ci    highWaterMark: 10
11371cb0ef41Sopenharmony_ci  });
11381cb0ef41Sopenharmony_ci  assert(startCalled);
11391cb0ef41Sopenharmony_ci}
11401cb0ef41Sopenharmony_ci
11411cb0ef41Sopenharmony_ci{
11421cb0ef41Sopenharmony_ci  let startCalled = false;
11431cb0ef41Sopenharmony_ci  new ReadableStream({
11441cb0ef41Sopenharmony_ci    start(controller) {
11451cb0ef41Sopenharmony_ci      assert.strictEqual(controller.desiredSize, 10);
11461cb0ef41Sopenharmony_ci      controller.error();
11471cb0ef41Sopenharmony_ci      assert.strictEqual(controller.desiredSize, null);
11481cb0ef41Sopenharmony_ci      startCalled = true;
11491cb0ef41Sopenharmony_ci    }
11501cb0ef41Sopenharmony_ci  }, {
11511cb0ef41Sopenharmony_ci    highWaterMark: 10
11521cb0ef41Sopenharmony_ci  });
11531cb0ef41Sopenharmony_ci  assert(startCalled);
11541cb0ef41Sopenharmony_ci}
11551cb0ef41Sopenharmony_ci
11561cb0ef41Sopenharmony_ci{
11571cb0ef41Sopenharmony_ci  class Foo extends ReadableStream {}
11581cb0ef41Sopenharmony_ci  const foo = new Foo();
11591cb0ef41Sopenharmony_ci  foo.getReader();
11601cb0ef41Sopenharmony_ci}
11611cb0ef41Sopenharmony_ci
11621cb0ef41Sopenharmony_ci{
11631cb0ef41Sopenharmony_ci  let startCalled = false;
11641cb0ef41Sopenharmony_ci  new ReadableStream({
11651cb0ef41Sopenharmony_ci    start(controller) {
11661cb0ef41Sopenharmony_ci      assert.strictEqual(controller.desiredSize, 1);
11671cb0ef41Sopenharmony_ci      controller.enqueue('a');
11681cb0ef41Sopenharmony_ci      assert.strictEqual(controller.desiredSize, 0);
11691cb0ef41Sopenharmony_ci      controller.enqueue('a');
11701cb0ef41Sopenharmony_ci      assert.strictEqual(controller.desiredSize, -1);
11711cb0ef41Sopenharmony_ci      controller.enqueue('a');
11721cb0ef41Sopenharmony_ci      assert.strictEqual(controller.desiredSize, -2);
11731cb0ef41Sopenharmony_ci      controller.enqueue('a');
11741cb0ef41Sopenharmony_ci      assert.strictEqual(controller.desiredSize, -3);
11751cb0ef41Sopenharmony_ci      startCalled = true;
11761cb0ef41Sopenharmony_ci    }
11771cb0ef41Sopenharmony_ci  });
11781cb0ef41Sopenharmony_ci  assert(startCalled);
11791cb0ef41Sopenharmony_ci}
11801cb0ef41Sopenharmony_ci
11811cb0ef41Sopenharmony_ci{
11821cb0ef41Sopenharmony_ci  let c;
11831cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
11841cb0ef41Sopenharmony_ci    start(controller) {
11851cb0ef41Sopenharmony_ci      c = controller;
11861cb0ef41Sopenharmony_ci    }
11871cb0ef41Sopenharmony_ci  });
11881cb0ef41Sopenharmony_ci
11891cb0ef41Sopenharmony_ci  const reader = stream.getReader();
11901cb0ef41Sopenharmony_ci
11911cb0ef41Sopenharmony_ci  (async () => {
11921cb0ef41Sopenharmony_ci    assert.strictEqual(c.desiredSize, 1);
11931cb0ef41Sopenharmony_ci    c.enqueue(1);
11941cb0ef41Sopenharmony_ci    assert.strictEqual(c.desiredSize, 0);
11951cb0ef41Sopenharmony_ci    await reader.read();
11961cb0ef41Sopenharmony_ci    assert.strictEqual(c.desiredSize, 1);
11971cb0ef41Sopenharmony_ci    c.enqueue(1);
11981cb0ef41Sopenharmony_ci    c.enqueue(1);
11991cb0ef41Sopenharmony_ci    assert.strictEqual(c.desiredSize, -1);
12001cb0ef41Sopenharmony_ci    await reader.read();
12011cb0ef41Sopenharmony_ci    assert.strictEqual(c.desiredSize, 0);
12021cb0ef41Sopenharmony_ci    await reader.read();
12031cb0ef41Sopenharmony_ci    assert.strictEqual(c.desiredSize, 1);
12041cb0ef41Sopenharmony_ci  })().then(common.mustCall());
12051cb0ef41Sopenharmony_ci}
12061cb0ef41Sopenharmony_ci
12071cb0ef41Sopenharmony_ci{
12081cb0ef41Sopenharmony_ci  let c;
12091cb0ef41Sopenharmony_ci  new ReadableStream({
12101cb0ef41Sopenharmony_ci    start(controller) {
12111cb0ef41Sopenharmony_ci      c = controller;
12121cb0ef41Sopenharmony_ci    }
12131cb0ef41Sopenharmony_ci  });
12141cb0ef41Sopenharmony_ci  assert(c instanceof ReadableStreamDefaultController);
12151cb0ef41Sopenharmony_ci  assert.strictEqual(typeof c.desiredSize, 'number');
12161cb0ef41Sopenharmony_ci  assert.strictEqual(typeof c.enqueue, 'function');
12171cb0ef41Sopenharmony_ci  assert.strictEqual(typeof c.close, 'function');
12181cb0ef41Sopenharmony_ci  assert.strictEqual(typeof c.error, 'function');
12191cb0ef41Sopenharmony_ci}
12201cb0ef41Sopenharmony_ci
12211cb0ef41Sopenharmony_ciclass Source {
12221cb0ef41Sopenharmony_ci  constructor() {
12231cb0ef41Sopenharmony_ci    this.cancelCalled = false;
12241cb0ef41Sopenharmony_ci  }
12251cb0ef41Sopenharmony_ci
12261cb0ef41Sopenharmony_ci  start(controller) {
12271cb0ef41Sopenharmony_ci    this.stream = createReadStream(__filename);
12281cb0ef41Sopenharmony_ci    this.stream.on('data', (chunk) => {
12291cb0ef41Sopenharmony_ci      controller.enqueue(chunk);
12301cb0ef41Sopenharmony_ci    });
12311cb0ef41Sopenharmony_ci    this.stream.once('end', () => {
12321cb0ef41Sopenharmony_ci      if (!this.cancelCalled)
12331cb0ef41Sopenharmony_ci        controller.close();
12341cb0ef41Sopenharmony_ci    });
12351cb0ef41Sopenharmony_ci    this.stream.once('error', (error) => {
12361cb0ef41Sopenharmony_ci      controller.error(error);
12371cb0ef41Sopenharmony_ci    });
12381cb0ef41Sopenharmony_ci  }
12391cb0ef41Sopenharmony_ci
12401cb0ef41Sopenharmony_ci  cancel() {
12411cb0ef41Sopenharmony_ci    this.cancelCalled = true;
12421cb0ef41Sopenharmony_ci  }
12431cb0ef41Sopenharmony_ci}
12441cb0ef41Sopenharmony_ci
12451cb0ef41Sopenharmony_ci{
12461cb0ef41Sopenharmony_ci  const source = new Source();
12471cb0ef41Sopenharmony_ci  const stream = new ReadableStream(source);
12481cb0ef41Sopenharmony_ci
12491cb0ef41Sopenharmony_ci  async function read(stream) {
12501cb0ef41Sopenharmony_ci    const reader = stream.getReader();
12511cb0ef41Sopenharmony_ci    const chunks = [];
12521cb0ef41Sopenharmony_ci    let read = await reader.read();
12531cb0ef41Sopenharmony_ci    while (!read.done) {
12541cb0ef41Sopenharmony_ci      chunks.push(Buffer.from(read.value));
12551cb0ef41Sopenharmony_ci      read = await reader.read();
12561cb0ef41Sopenharmony_ci    }
12571cb0ef41Sopenharmony_ci    return Buffer.concat(chunks);
12581cb0ef41Sopenharmony_ci  }
12591cb0ef41Sopenharmony_ci
12601cb0ef41Sopenharmony_ci  read(stream).then(common.mustCall((data) => {
12611cb0ef41Sopenharmony_ci    const check = readFileSync(__filename);
12621cb0ef41Sopenharmony_ci    assert.deepStrictEqual(data, check);
12631cb0ef41Sopenharmony_ci  }));
12641cb0ef41Sopenharmony_ci}
12651cb0ef41Sopenharmony_ci
12661cb0ef41Sopenharmony_ci{
12671cb0ef41Sopenharmony_ci  const source = new Source();
12681cb0ef41Sopenharmony_ci  const stream = new ReadableStream(source);
12691cb0ef41Sopenharmony_ci
12701cb0ef41Sopenharmony_ci  async function read(stream) {
12711cb0ef41Sopenharmony_ci    const chunks = [];
12721cb0ef41Sopenharmony_ci    for await (const chunk of stream)
12731cb0ef41Sopenharmony_ci      chunks.push(chunk);
12741cb0ef41Sopenharmony_ci    return Buffer.concat(chunks);
12751cb0ef41Sopenharmony_ci  }
12761cb0ef41Sopenharmony_ci
12771cb0ef41Sopenharmony_ci  read(stream).then(common.mustCall((data) => {
12781cb0ef41Sopenharmony_ci    const check = readFileSync(__filename);
12791cb0ef41Sopenharmony_ci    assert.deepStrictEqual(data, check);
12801cb0ef41Sopenharmony_ci
12811cb0ef41Sopenharmony_ci    assert.strictEqual(stream[kState].state, 'closed');
12821cb0ef41Sopenharmony_ci    assert(!stream.locked);
12831cb0ef41Sopenharmony_ci  }));
12841cb0ef41Sopenharmony_ci}
12851cb0ef41Sopenharmony_ci
12861cb0ef41Sopenharmony_ci{
12871cb0ef41Sopenharmony_ci  const source = new Source();
12881cb0ef41Sopenharmony_ci  const stream = new ReadableStream(source);
12891cb0ef41Sopenharmony_ci
12901cb0ef41Sopenharmony_ci  [1, false, ''].forEach((options) => {
12911cb0ef41Sopenharmony_ci    assert.throws(() => stream.values(options), {
12921cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
12931cb0ef41Sopenharmony_ci    });
12941cb0ef41Sopenharmony_ci  });
12951cb0ef41Sopenharmony_ci
12961cb0ef41Sopenharmony_ci  async function read(stream) {
12971cb0ef41Sopenharmony_ci    for await (const _ of stream.values({ preventCancel: true }))
12981cb0ef41Sopenharmony_ci      return;
12991cb0ef41Sopenharmony_ci  }
13001cb0ef41Sopenharmony_ci
13011cb0ef41Sopenharmony_ci  read(stream).then(common.mustCall((data) => {
13021cb0ef41Sopenharmony_ci    assert.strictEqual(stream[kState].state, 'readable');
13031cb0ef41Sopenharmony_ci  }));
13041cb0ef41Sopenharmony_ci}
13051cb0ef41Sopenharmony_ci
13061cb0ef41Sopenharmony_ci{
13071cb0ef41Sopenharmony_ci  const source = new Source();
13081cb0ef41Sopenharmony_ci  const stream = new ReadableStream(source);
13091cb0ef41Sopenharmony_ci
13101cb0ef41Sopenharmony_ci  async function read(stream) {
13111cb0ef41Sopenharmony_ci    for await (const _ of stream.values({ preventCancel: false }))
13121cb0ef41Sopenharmony_ci      return;
13131cb0ef41Sopenharmony_ci  }
13141cb0ef41Sopenharmony_ci
13151cb0ef41Sopenharmony_ci  read(stream).then(common.mustCall((data) => {
13161cb0ef41Sopenharmony_ci    assert.strictEqual(stream[kState].state, 'closed');
13171cb0ef41Sopenharmony_ci  }));
13181cb0ef41Sopenharmony_ci}
13191cb0ef41Sopenharmony_ci
13201cb0ef41Sopenharmony_ci{
13211cb0ef41Sopenharmony_ci  const source = new Source();
13221cb0ef41Sopenharmony_ci  const stream = new ReadableStream(source);
13231cb0ef41Sopenharmony_ci
13241cb0ef41Sopenharmony_ci  const error = new Error('boom');
13251cb0ef41Sopenharmony_ci
13261cb0ef41Sopenharmony_ci  async function read(stream) {
13271cb0ef41Sopenharmony_ci    // eslint-disable-next-line no-unused-vars
13281cb0ef41Sopenharmony_ci    for await (const _ of stream.values({ preventCancel: true }))
13291cb0ef41Sopenharmony_ci      throw error;
13301cb0ef41Sopenharmony_ci  }
13311cb0ef41Sopenharmony_ci
13321cb0ef41Sopenharmony_ci  assert.rejects(read(stream), error).then(common.mustCall(() => {
13331cb0ef41Sopenharmony_ci    assert.strictEqual(stream[kState].state, 'readable');
13341cb0ef41Sopenharmony_ci  }));
13351cb0ef41Sopenharmony_ci}
13361cb0ef41Sopenharmony_ci
13371cb0ef41Sopenharmony_ci{
13381cb0ef41Sopenharmony_ci  const source = new Source();
13391cb0ef41Sopenharmony_ci  const stream = new ReadableStream(source);
13401cb0ef41Sopenharmony_ci
13411cb0ef41Sopenharmony_ci  const error = new Error('boom');
13421cb0ef41Sopenharmony_ci
13431cb0ef41Sopenharmony_ci  async function read(stream) {
13441cb0ef41Sopenharmony_ci    // eslint-disable-next-line no-unused-vars
13451cb0ef41Sopenharmony_ci    for await (const _ of stream.values({ preventCancel: false }))
13461cb0ef41Sopenharmony_ci      throw error;
13471cb0ef41Sopenharmony_ci  }
13481cb0ef41Sopenharmony_ci
13491cb0ef41Sopenharmony_ci  assert.rejects(read(stream), error).then(common.mustCall(() => {
13501cb0ef41Sopenharmony_ci    assert.strictEqual(stream[kState].state, 'closed');
13511cb0ef41Sopenharmony_ci  }));
13521cb0ef41Sopenharmony_ci}
13531cb0ef41Sopenharmony_ci
13541cb0ef41Sopenharmony_ci{
13551cb0ef41Sopenharmony_ci  assert.throws(() => Reflect.get(ReadableStream.prototype, 'locked', {}), {
13561cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
13571cb0ef41Sopenharmony_ci  });
13581cb0ef41Sopenharmony_ci  assert.rejects(() => ReadableStream.prototype.cancel.call({}), {
13591cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
13601cb0ef41Sopenharmony_ci  });
13611cb0ef41Sopenharmony_ci  assert.throws(() => ReadableStream.prototype.getReader.call({}), {
13621cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
13631cb0ef41Sopenharmony_ci  });
13641cb0ef41Sopenharmony_ci  assert.throws(() => ReadableStream.prototype.tee.call({}), {
13651cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
13661cb0ef41Sopenharmony_ci  });
13671cb0ef41Sopenharmony_ci  assert.throws(() => ReadableStream.prototype.values.call({}), {
13681cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
13691cb0ef41Sopenharmony_ci  });
13701cb0ef41Sopenharmony_ci  assert.throws(() => ReadableStream.prototype[kTransfer].call({}), {
13711cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
13721cb0ef41Sopenharmony_ci  });
13731cb0ef41Sopenharmony_ci  assert.rejects(() => ReadableStreamDefaultReader.prototype.read.call({}), {
13741cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
13751cb0ef41Sopenharmony_ci  });
13761cb0ef41Sopenharmony_ci  assert.rejects(() => ReadableStreamDefaultReader.prototype.cancel.call({}), {
13771cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
13781cb0ef41Sopenharmony_ci  });
13791cb0ef41Sopenharmony_ci  assert.rejects(() => {
13801cb0ef41Sopenharmony_ci    return Reflect.get(ReadableStreamDefaultReader.prototype, 'closed');
13811cb0ef41Sopenharmony_ci  }, {
13821cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
13831cb0ef41Sopenharmony_ci  });
13841cb0ef41Sopenharmony_ci  assert.throws(() => {
13851cb0ef41Sopenharmony_ci    ReadableStreamDefaultReader.prototype.releaseLock.call({});
13861cb0ef41Sopenharmony_ci  }, {
13871cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
13881cb0ef41Sopenharmony_ci  });
13891cb0ef41Sopenharmony_ci  assert.rejects(() => ReadableStreamBYOBReader.prototype.read.call({}), {
13901cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
13911cb0ef41Sopenharmony_ci  });
13921cb0ef41Sopenharmony_ci  assert.throws(() => {
13931cb0ef41Sopenharmony_ci    ReadableStreamBYOBReader.prototype.releaseLock.call({});
13941cb0ef41Sopenharmony_ci  }, {
13951cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
13961cb0ef41Sopenharmony_ci  });
13971cb0ef41Sopenharmony_ci  assert.rejects(() => {
13981cb0ef41Sopenharmony_ci    return Reflect.get(ReadableStreamBYOBReader.prototype, 'closed');
13991cb0ef41Sopenharmony_ci  }, {
14001cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
14011cb0ef41Sopenharmony_ci  });
14021cb0ef41Sopenharmony_ci  assert.rejects(() => ReadableStreamBYOBReader.prototype.cancel.call({}), {
14031cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
14041cb0ef41Sopenharmony_ci  });
14051cb0ef41Sopenharmony_ci
14061cb0ef41Sopenharmony_ci  assert.throws(() => {
14071cb0ef41Sopenharmony_ci    Reflect.get(ReadableByteStreamController.prototype, 'byobRequest', {});
14081cb0ef41Sopenharmony_ci  }, {
14091cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
14101cb0ef41Sopenharmony_ci  });
14111cb0ef41Sopenharmony_ci  assert.throws(() => {
14121cb0ef41Sopenharmony_ci    Reflect.get(ReadableByteStreamController.prototype, 'desiredSize', {});
14131cb0ef41Sopenharmony_ci  }, {
14141cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
14151cb0ef41Sopenharmony_ci  });
14161cb0ef41Sopenharmony_ci  assert.throws(() => {
14171cb0ef41Sopenharmony_ci    ReadableByteStreamController.prototype.close.call({});
14181cb0ef41Sopenharmony_ci  }, {
14191cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
14201cb0ef41Sopenharmony_ci  });
14211cb0ef41Sopenharmony_ci  assert.throws(() => {
14221cb0ef41Sopenharmony_ci    ReadableByteStreamController.prototype.enqueue.call({});
14231cb0ef41Sopenharmony_ci  }, {
14241cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
14251cb0ef41Sopenharmony_ci  });
14261cb0ef41Sopenharmony_ci  assert.throws(() => {
14271cb0ef41Sopenharmony_ci    ReadableByteStreamController.prototype.error.call({});
14281cb0ef41Sopenharmony_ci  }, {
14291cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
14301cb0ef41Sopenharmony_ci  });
14311cb0ef41Sopenharmony_ci
14321cb0ef41Sopenharmony_ci  assert.throws(() => new ReadableStreamBYOBRequest(), {
14331cb0ef41Sopenharmony_ci    code: 'ERR_ILLEGAL_CONSTRUCTOR',
14341cb0ef41Sopenharmony_ci  });
14351cb0ef41Sopenharmony_ci
14361cb0ef41Sopenharmony_ci  assert.throws(() => new ReadableStreamDefaultController(), {
14371cb0ef41Sopenharmony_ci    code: 'ERR_ILLEGAL_CONSTRUCTOR',
14381cb0ef41Sopenharmony_ci  });
14391cb0ef41Sopenharmony_ci
14401cb0ef41Sopenharmony_ci  assert.throws(() => new ReadableByteStreamController(), {
14411cb0ef41Sopenharmony_ci    code: 'ERR_ILLEGAL_CONSTRUCTOR',
14421cb0ef41Sopenharmony_ci  });
14431cb0ef41Sopenharmony_ci}
14441cb0ef41Sopenharmony_ci
14451cb0ef41Sopenharmony_ci{
14461cb0ef41Sopenharmony_ci  let controller;
14471cb0ef41Sopenharmony_ci  const readable = new ReadableStream({
14481cb0ef41Sopenharmony_ci    start(c) { controller = c; }
14491cb0ef41Sopenharmony_ci  });
14501cb0ef41Sopenharmony_ci
14511cb0ef41Sopenharmony_ci  assert.strictEqual(
14521cb0ef41Sopenharmony_ci    inspect(readable),
14531cb0ef41Sopenharmony_ci    'ReadableStream { locked: false, state: \'readable\', ' +
14541cb0ef41Sopenharmony_ci    'supportsBYOB: false }');
14551cb0ef41Sopenharmony_ci  assert.strictEqual(
14561cb0ef41Sopenharmony_ci    inspect(readable, { depth: null }),
14571cb0ef41Sopenharmony_ci    'ReadableStream { locked: false, state: \'readable\', ' +
14581cb0ef41Sopenharmony_ci    'supportsBYOB: false }');
14591cb0ef41Sopenharmony_ci  assert.strictEqual(
14601cb0ef41Sopenharmony_ci    inspect(readable, { depth: 0 }),
14611cb0ef41Sopenharmony_ci    'ReadableStream [Object]');
14621cb0ef41Sopenharmony_ci
14631cb0ef41Sopenharmony_ci  assert.strictEqual(
14641cb0ef41Sopenharmony_ci    inspect(controller),
14651cb0ef41Sopenharmony_ci    'ReadableStreamDefaultController {}');
14661cb0ef41Sopenharmony_ci  assert.strictEqual(
14671cb0ef41Sopenharmony_ci    inspect(controller, { depth: null }),
14681cb0ef41Sopenharmony_ci    'ReadableStreamDefaultController {}');
14691cb0ef41Sopenharmony_ci  assert.strictEqual(
14701cb0ef41Sopenharmony_ci    inspect(controller, { depth: 0 }),
14711cb0ef41Sopenharmony_ci    'ReadableStreamDefaultController {}');
14721cb0ef41Sopenharmony_ci
14731cb0ef41Sopenharmony_ci  const reader = readable.getReader();
14741cb0ef41Sopenharmony_ci
14751cb0ef41Sopenharmony_ci  assert.match(
14761cb0ef41Sopenharmony_ci    inspect(reader),
14771cb0ef41Sopenharmony_ci    /ReadableStreamDefaultReader/);
14781cb0ef41Sopenharmony_ci  assert.match(
14791cb0ef41Sopenharmony_ci    inspect(reader, { depth: null }),
14801cb0ef41Sopenharmony_ci    /ReadableStreamDefaultReader/);
14811cb0ef41Sopenharmony_ci  assert.match(
14821cb0ef41Sopenharmony_ci    inspect(reader, { depth: 0 }),
14831cb0ef41Sopenharmony_ci    /ReadableStreamDefaultReader/);
14841cb0ef41Sopenharmony_ci
14851cb0ef41Sopenharmony_ci  assert.rejects(readableStreamPipeTo(1), {
14861cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
14871cb0ef41Sopenharmony_ci  });
14881cb0ef41Sopenharmony_ci
14891cb0ef41Sopenharmony_ci  assert.rejects(readableStreamPipeTo(new ReadableStream(), 1), {
14901cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
14911cb0ef41Sopenharmony_ci  });
14921cb0ef41Sopenharmony_ci
14931cb0ef41Sopenharmony_ci  assert.rejects(
14941cb0ef41Sopenharmony_ci    readableStreamPipeTo(
14951cb0ef41Sopenharmony_ci      new ReadableStream(),
14961cb0ef41Sopenharmony_ci      new WritableStream(),
14971cb0ef41Sopenharmony_ci      false,
14981cb0ef41Sopenharmony_ci      false,
14991cb0ef41Sopenharmony_ci      false,
15001cb0ef41Sopenharmony_ci      {}),
15011cb0ef41Sopenharmony_ci    {
15021cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
15031cb0ef41Sopenharmony_ci    });
15041cb0ef41Sopenharmony_ci}
15051cb0ef41Sopenharmony_ci
15061cb0ef41Sopenharmony_ci{
15071cb0ef41Sopenharmony_ci  const readable = new ReadableStream();
15081cb0ef41Sopenharmony_ci  const reader = readable.getReader();
15091cb0ef41Sopenharmony_ci  reader.releaseLock();
15101cb0ef41Sopenharmony_ci  reader.releaseLock();
15111cb0ef41Sopenharmony_ci  assert.rejects(reader.read(), {
15121cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_STATE',
15131cb0ef41Sopenharmony_ci  });
15141cb0ef41Sopenharmony_ci  assert.rejects(reader.cancel(), {
15151cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_STATE',
15161cb0ef41Sopenharmony_ci  });
15171cb0ef41Sopenharmony_ci}
15181cb0ef41Sopenharmony_ci
15191cb0ef41Sopenharmony_ci{
15201cb0ef41Sopenharmony_ci  // Test tee() cloneForBranch2 argument
15211cb0ef41Sopenharmony_ci  const readable = new ReadableStream({
15221cb0ef41Sopenharmony_ci    start(controller) {
15231cb0ef41Sopenharmony_ci      controller.enqueue('hello');
15241cb0ef41Sopenharmony_ci    }
15251cb0ef41Sopenharmony_ci  });
15261cb0ef41Sopenharmony_ci  const [r1, r2] = readableStreamTee(readable, true);
15271cb0ef41Sopenharmony_ci  r1.getReader().read().then(
15281cb0ef41Sopenharmony_ci    common.mustCall(({ value }) => assert.strictEqual(value, 'hello')));
15291cb0ef41Sopenharmony_ci  r2.getReader().read().then(
15301cb0ef41Sopenharmony_ci    common.mustCall(({ value }) => assert.strictEqual(value, 'hello')));
15311cb0ef41Sopenharmony_ci}
15321cb0ef41Sopenharmony_ci
15331cb0ef41Sopenharmony_ci{
15341cb0ef41Sopenharmony_ci  assert.throws(() => {
15351cb0ef41Sopenharmony_ci    readableByteStreamControllerConvertPullIntoDescriptor({
15361cb0ef41Sopenharmony_ci      bytesFilled: 10,
15371cb0ef41Sopenharmony_ci      byteLength: 5
15381cb0ef41Sopenharmony_ci    });
15391cb0ef41Sopenharmony_ci  }, {
15401cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_STATE',
15411cb0ef41Sopenharmony_ci  });
15421cb0ef41Sopenharmony_ci}
15431cb0ef41Sopenharmony_ci
15441cb0ef41Sopenharmony_ci{
15451cb0ef41Sopenharmony_ci  let controller;
15461cb0ef41Sopenharmony_ci  const readable = new ReadableStream({
15471cb0ef41Sopenharmony_ci    start(c) { controller = c; }
15481cb0ef41Sopenharmony_ci  });
15491cb0ef41Sopenharmony_ci
15501cb0ef41Sopenharmony_ci  controller[kState].pendingPullIntos = [{}];
15511cb0ef41Sopenharmony_ci  assert.throws(() => readableByteStreamControllerRespond(controller, 0), {
15521cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_VALUE',
15531cb0ef41Sopenharmony_ci  });
15541cb0ef41Sopenharmony_ci
15551cb0ef41Sopenharmony_ci  readable.cancel().then(common.mustCall());
15561cb0ef41Sopenharmony_ci
15571cb0ef41Sopenharmony_ci  assert.throws(() => readableByteStreamControllerRespond(controller, 1), {
15581cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_VALUE',
15591cb0ef41Sopenharmony_ci  });
15601cb0ef41Sopenharmony_ci
15611cb0ef41Sopenharmony_ci  assert(!readableStreamDefaultControllerCanCloseOrEnqueue(controller));
15621cb0ef41Sopenharmony_ci  readableStreamDefaultControllerEnqueue(controller);
15631cb0ef41Sopenharmony_ci  readableByteStreamControllerClose(controller);
15641cb0ef41Sopenharmony_ci  readableByteStreamControllerEnqueue(controller, new Uint8Array(1));
15651cb0ef41Sopenharmony_ci}
15661cb0ef41Sopenharmony_ci
15671cb0ef41Sopenharmony_ci{
15681cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
15691cb0ef41Sopenharmony_ci    start(controller) {
15701cb0ef41Sopenharmony_ci      controller.enqueue('a');
15711cb0ef41Sopenharmony_ci      controller.close();
15721cb0ef41Sopenharmony_ci    },
15731cb0ef41Sopenharmony_ci    pull: common.mustNotCall(),
15741cb0ef41Sopenharmony_ci  });
15751cb0ef41Sopenharmony_ci
15761cb0ef41Sopenharmony_ci  const reader = stream.getReader();
15771cb0ef41Sopenharmony_ci  (async () => {
15781cb0ef41Sopenharmony_ci    isDisturbed(stream, false);
15791cb0ef41Sopenharmony_ci    await reader.read();
15801cb0ef41Sopenharmony_ci    isDisturbed(stream, true);
15811cb0ef41Sopenharmony_ci  })().then(common.mustCall());
15821cb0ef41Sopenharmony_ci}
15831cb0ef41Sopenharmony_ci
15841cb0ef41Sopenharmony_ci{
15851cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
15861cb0ef41Sopenharmony_ci    start(controller) {
15871cb0ef41Sopenharmony_ci      controller.close();
15881cb0ef41Sopenharmony_ci    },
15891cb0ef41Sopenharmony_ci    pull: common.mustNotCall(),
15901cb0ef41Sopenharmony_ci  });
15911cb0ef41Sopenharmony_ci
15921cb0ef41Sopenharmony_ci  const reader = stream.getReader();
15931cb0ef41Sopenharmony_ci  (async () => {
15941cb0ef41Sopenharmony_ci    isDisturbed(stream, false);
15951cb0ef41Sopenharmony_ci    await reader.read();
15961cb0ef41Sopenharmony_ci    isDisturbed(stream, true);
15971cb0ef41Sopenharmony_ci  })().then(common.mustCall());
15981cb0ef41Sopenharmony_ci}
15991cb0ef41Sopenharmony_ci
16001cb0ef41Sopenharmony_ci{
16011cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
16021cb0ef41Sopenharmony_ci    start(controller) {
16031cb0ef41Sopenharmony_ci    },
16041cb0ef41Sopenharmony_ci    pull: common.mustNotCall(),
16051cb0ef41Sopenharmony_ci  });
16061cb0ef41Sopenharmony_ci  stream.cancel();
16071cb0ef41Sopenharmony_ci
16081cb0ef41Sopenharmony_ci  const reader = stream.getReader();
16091cb0ef41Sopenharmony_ci  (async () => {
16101cb0ef41Sopenharmony_ci    isDisturbed(stream, false);
16111cb0ef41Sopenharmony_ci    await reader.read();
16121cb0ef41Sopenharmony_ci    isDisturbed(stream, true);
16131cb0ef41Sopenharmony_ci  })().then(common.mustCall());
16141cb0ef41Sopenharmony_ci}
16151cb0ef41Sopenharmony_ci
16161cb0ef41Sopenharmony_ci{
16171cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
16181cb0ef41Sopenharmony_ci    pull: common.mustCall((controller) => {
16191cb0ef41Sopenharmony_ci      controller.error(new Error());
16201cb0ef41Sopenharmony_ci    }),
16211cb0ef41Sopenharmony_ci  });
16221cb0ef41Sopenharmony_ci
16231cb0ef41Sopenharmony_ci  const reader = stream.getReader();
16241cb0ef41Sopenharmony_ci  (async () => {
16251cb0ef41Sopenharmony_ci    isErrored(stream, false);
16261cb0ef41Sopenharmony_ci    await reader.read().catch(common.mustCall());
16271cb0ef41Sopenharmony_ci    isErrored(stream, true);
16281cb0ef41Sopenharmony_ci  })().then(common.mustCall());
16291cb0ef41Sopenharmony_ci}
16301cb0ef41Sopenharmony_ci
16311cb0ef41Sopenharmony_ci{
16321cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
16331cb0ef41Sopenharmony_ci    pull: common.mustCall((controller) => {
16341cb0ef41Sopenharmony_ci      controller.error(new Error());
16351cb0ef41Sopenharmony_ci    }),
16361cb0ef41Sopenharmony_ci  });
16371cb0ef41Sopenharmony_ci
16381cb0ef41Sopenharmony_ci  const reader = stream.getReader();
16391cb0ef41Sopenharmony_ci  (async () => {
16401cb0ef41Sopenharmony_ci    isReadable(stream, true);
16411cb0ef41Sopenharmony_ci    await reader.read().catch(common.mustCall());
16421cb0ef41Sopenharmony_ci    isReadable(stream, false);
16431cb0ef41Sopenharmony_ci  })().then(common.mustCall());
16441cb0ef41Sopenharmony_ci}
16451cb0ef41Sopenharmony_ci
16461cb0ef41Sopenharmony_ci{
16471cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
16481cb0ef41Sopenharmony_ci    type: 'bytes',
16491cb0ef41Sopenharmony_ci    start(controller) {
16501cb0ef41Sopenharmony_ci      controller.close();
16511cb0ef41Sopenharmony_ci    }
16521cb0ef41Sopenharmony_ci  });
16531cb0ef41Sopenharmony_ci
16541cb0ef41Sopenharmony_ci  const buffer = new ArrayBuffer(1024);
16551cb0ef41Sopenharmony_ci  const reader = stream.getReader({ mode: 'byob' });
16561cb0ef41Sopenharmony_ci
16571cb0ef41Sopenharmony_ci  reader.read(new DataView(buffer))
16581cb0ef41Sopenharmony_ci    .then(common.mustCall());
16591cb0ef41Sopenharmony_ci}
16601cb0ef41Sopenharmony_ci
16611cb0ef41Sopenharmony_ci{
16621cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
16631cb0ef41Sopenharmony_ci    type: 'bytes',
16641cb0ef41Sopenharmony_ci    autoAllocateChunkSize: 128,
16651cb0ef41Sopenharmony_ci    pull: common.mustCall((controller) => {
16661cb0ef41Sopenharmony_ci      const view = controller.byobRequest.view;
16671cb0ef41Sopenharmony_ci      const dest = new Uint8Array(
16681cb0ef41Sopenharmony_ci        view.buffer,
16691cb0ef41Sopenharmony_ci        view.byteOffset,
16701cb0ef41Sopenharmony_ci        view.byteLength
16711cb0ef41Sopenharmony_ci      );
16721cb0ef41Sopenharmony_ci      dest.fill(1);
16731cb0ef41Sopenharmony_ci      controller.byobRequest.respondWithNewView(dest);
16741cb0ef41Sopenharmony_ci    }),
16751cb0ef41Sopenharmony_ci  });
16761cb0ef41Sopenharmony_ci
16771cb0ef41Sopenharmony_ci  const reader = stream.getReader({ mode: 'byob' });
16781cb0ef41Sopenharmony_ci
16791cb0ef41Sopenharmony_ci  const buffer = new ArrayBuffer(10);
16801cb0ef41Sopenharmony_ci  const view = new Uint8Array(
16811cb0ef41Sopenharmony_ci    buffer,
16821cb0ef41Sopenharmony_ci    1,
16831cb0ef41Sopenharmony_ci    3
16841cb0ef41Sopenharmony_ci  );
16851cb0ef41Sopenharmony_ci
16861cb0ef41Sopenharmony_ci  reader.read(view).then(common.mustCall(({ value }) => {
16871cb0ef41Sopenharmony_ci    assert.deepStrictEqual(value, new Uint8Array([1, 1, 1]));
16881cb0ef41Sopenharmony_ci  }));
16891cb0ef41Sopenharmony_ci}
1690