11cb0ef41Sopenharmony_ci// Flags: --no-warnings --expose-internals
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst {
91cb0ef41Sopenharmony_ci  newReadableStreamFromStreamReadable,
101cb0ef41Sopenharmony_ci} = require('internal/webstreams/adapters');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst {
131cb0ef41Sopenharmony_ci  Duplex,
141cb0ef41Sopenharmony_ci  Readable,
151cb0ef41Sopenharmony_ci} = require('stream');
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciconst {
181cb0ef41Sopenharmony_ci  kState,
191cb0ef41Sopenharmony_ci} = require('internal/webstreams/util');
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci{
221cb0ef41Sopenharmony_ci  // Canceling the readableStream closes the readable.
231cb0ef41Sopenharmony_ci  const readable = new Readable({
241cb0ef41Sopenharmony_ci    read() {
251cb0ef41Sopenharmony_ci      readable.push('hello');
261cb0ef41Sopenharmony_ci      readable.push(null);
271cb0ef41Sopenharmony_ci    }
281cb0ef41Sopenharmony_ci  });
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  readable.on('close', common.mustCall());
311cb0ef41Sopenharmony_ci  readable.on('end', common.mustNotCall());
321cb0ef41Sopenharmony_ci  readable.on('pause', common.mustCall());
331cb0ef41Sopenharmony_ci  readable.on('resume', common.mustNotCall());
341cb0ef41Sopenharmony_ci  readable.on('error', common.mustCall((error) => {
351cb0ef41Sopenharmony_ci    assert.strictEqual(error.code, 'ABORT_ERR');
361cb0ef41Sopenharmony_ci  }));
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  const readableStream = newReadableStreamFromStreamReadable(readable);
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  readableStream.cancel().then(common.mustCall());
411cb0ef41Sopenharmony_ci}
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci{
441cb0ef41Sopenharmony_ci  // Prematurely destroying the stream.Readable without an error
451cb0ef41Sopenharmony_ci  // closes the ReadableStream with a premature close error but does
461cb0ef41Sopenharmony_ci  // not error the readable.
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  const readable = new Readable({
491cb0ef41Sopenharmony_ci    read() {
501cb0ef41Sopenharmony_ci      readable.push('hello');
511cb0ef41Sopenharmony_ci      readable.push(null);
521cb0ef41Sopenharmony_ci    }
531cb0ef41Sopenharmony_ci  });
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  const readableStream = newReadableStreamFromStreamReadable(readable);
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  assert(!readableStream.locked);
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  const reader = readableStream.getReader();
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  assert.rejects(reader.closed, {
621cb0ef41Sopenharmony_ci    code: 'ABORT_ERR',
631cb0ef41Sopenharmony_ci  });
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci  readable.on('end', common.mustNotCall());
661cb0ef41Sopenharmony_ci  readable.on('error', common.mustNotCall());
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci  readable.on('close', common.mustCall(() => {
691cb0ef41Sopenharmony_ci    assert.strictEqual(readableStream[kState].state, 'errored');
701cb0ef41Sopenharmony_ci  }));
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci  readable.destroy();
731cb0ef41Sopenharmony_ci}
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci{
761cb0ef41Sopenharmony_ci  // Ending the readable without an error just closes the
771cb0ef41Sopenharmony_ci  // readableStream without an error.
781cb0ef41Sopenharmony_ci  const readable = new Readable({
791cb0ef41Sopenharmony_ci    read() {
801cb0ef41Sopenharmony_ci      readable.push('hello');
811cb0ef41Sopenharmony_ci      readable.push(null);
821cb0ef41Sopenharmony_ci    }
831cb0ef41Sopenharmony_ci  });
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci  const readableStream = newReadableStreamFromStreamReadable(readable);
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci  assert(!readableStream.locked);
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci  const reader = readableStream.getReader();
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci  reader.closed.then(common.mustCall());
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci  readable.on('end', common.mustCall());
941cb0ef41Sopenharmony_ci  readable.on('error', common.mustNotCall());
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci  readable.on('close', common.mustCall(() => {
971cb0ef41Sopenharmony_ci    assert.strictEqual(readableStream[kState].state, 'closed');
981cb0ef41Sopenharmony_ci  }));
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci  readable.push(null);
1011cb0ef41Sopenharmony_ci}
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci{
1041cb0ef41Sopenharmony_ci  // Destroying the readable with an error should error the readableStream
1051cb0ef41Sopenharmony_ci  const error = new Error('boom');
1061cb0ef41Sopenharmony_ci  const readable = new Readable({
1071cb0ef41Sopenharmony_ci    read() {
1081cb0ef41Sopenharmony_ci      readable.push('hello');
1091cb0ef41Sopenharmony_ci      readable.push(null);
1101cb0ef41Sopenharmony_ci    }
1111cb0ef41Sopenharmony_ci  });
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci  const readableStream = newReadableStreamFromStreamReadable(readable);
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci  assert(!readableStream.locked);
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ci  const reader = readableStream.getReader();
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci  assert.rejects(reader.closed, error);
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_ci  readable.on('end', common.mustNotCall());
1221cb0ef41Sopenharmony_ci  readable.on('error', common.mustCall((reason) => {
1231cb0ef41Sopenharmony_ci    assert.strictEqual(reason, error);
1241cb0ef41Sopenharmony_ci  }));
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_ci  readable.on('close', common.mustCall(() => {
1271cb0ef41Sopenharmony_ci    assert.strictEqual(readableStream[kState].state, 'errored');
1281cb0ef41Sopenharmony_ci  }));
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci  readable.destroy(error);
1311cb0ef41Sopenharmony_ci}
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_ci{
1341cb0ef41Sopenharmony_ci  const readable = new Readable({
1351cb0ef41Sopenharmony_ci    encoding: 'utf8',
1361cb0ef41Sopenharmony_ci    read() {
1371cb0ef41Sopenharmony_ci      readable.push('hello');
1381cb0ef41Sopenharmony_ci      readable.push(null);
1391cb0ef41Sopenharmony_ci    }
1401cb0ef41Sopenharmony_ci  });
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ci  const readableStream = newReadableStreamFromStreamReadable(readable);
1431cb0ef41Sopenharmony_ci  const reader = readableStream.getReader();
1441cb0ef41Sopenharmony_ci
1451cb0ef41Sopenharmony_ci  readable.on('data', common.mustCall());
1461cb0ef41Sopenharmony_ci  readable.on('end', common.mustCall());
1471cb0ef41Sopenharmony_ci  readable.on('close', common.mustCall());
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ci  (async () => {
1501cb0ef41Sopenharmony_ci    assert.deepStrictEqual(
1511cb0ef41Sopenharmony_ci      await reader.read(),
1521cb0ef41Sopenharmony_ci      { value: 'hello', done: false });
1531cb0ef41Sopenharmony_ci    assert.deepStrictEqual(
1541cb0ef41Sopenharmony_ci      await reader.read(),
1551cb0ef41Sopenharmony_ci      { value: undefined, done: true });
1561cb0ef41Sopenharmony_ci
1571cb0ef41Sopenharmony_ci  })().then(common.mustCall());
1581cb0ef41Sopenharmony_ci}
1591cb0ef41Sopenharmony_ci
1601cb0ef41Sopenharmony_ci{
1611cb0ef41Sopenharmony_ci  const data = {};
1621cb0ef41Sopenharmony_ci  const readable = new Readable({
1631cb0ef41Sopenharmony_ci    objectMode: true,
1641cb0ef41Sopenharmony_ci    read() {
1651cb0ef41Sopenharmony_ci      readable.push(data);
1661cb0ef41Sopenharmony_ci      readable.push(null);
1671cb0ef41Sopenharmony_ci    }
1681cb0ef41Sopenharmony_ci  });
1691cb0ef41Sopenharmony_ci
1701cb0ef41Sopenharmony_ci  assert(readable.readableObjectMode);
1711cb0ef41Sopenharmony_ci
1721cb0ef41Sopenharmony_ci  const readableStream = newReadableStreamFromStreamReadable(readable);
1731cb0ef41Sopenharmony_ci  const reader = readableStream.getReader();
1741cb0ef41Sopenharmony_ci
1751cb0ef41Sopenharmony_ci  readable.on('data', common.mustCall());
1761cb0ef41Sopenharmony_ci  readable.on('end', common.mustCall());
1771cb0ef41Sopenharmony_ci  readable.on('close', common.mustCall());
1781cb0ef41Sopenharmony_ci
1791cb0ef41Sopenharmony_ci  (async () => {
1801cb0ef41Sopenharmony_ci    assert.deepStrictEqual(
1811cb0ef41Sopenharmony_ci      await reader.read(),
1821cb0ef41Sopenharmony_ci      { value: data, done: false });
1831cb0ef41Sopenharmony_ci    assert.deepStrictEqual(
1841cb0ef41Sopenharmony_ci      await reader.read(),
1851cb0ef41Sopenharmony_ci      { value: undefined, done: true });
1861cb0ef41Sopenharmony_ci
1871cb0ef41Sopenharmony_ci  })().then(common.mustCall());
1881cb0ef41Sopenharmony_ci}
1891cb0ef41Sopenharmony_ci
1901cb0ef41Sopenharmony_ci{
1911cb0ef41Sopenharmony_ci  const readable = new Readable();
1921cb0ef41Sopenharmony_ci  readable.destroy();
1931cb0ef41Sopenharmony_ci  const readableStream = newReadableStreamFromStreamReadable(readable);
1941cb0ef41Sopenharmony_ci  const reader = readableStream.getReader();
1951cb0ef41Sopenharmony_ci  reader.closed.then(common.mustCall());
1961cb0ef41Sopenharmony_ci}
1971cb0ef41Sopenharmony_ci
1981cb0ef41Sopenharmony_ci{
1991cb0ef41Sopenharmony_ci  const duplex = new Duplex({ readable: false });
2001cb0ef41Sopenharmony_ci  duplex.destroy();
2011cb0ef41Sopenharmony_ci  const readableStream = newReadableStreamFromStreamReadable(duplex);
2021cb0ef41Sopenharmony_ci  const reader = readableStream.getReader();
2031cb0ef41Sopenharmony_ci  reader.closed.then(common.mustCall());
2041cb0ef41Sopenharmony_ci}
205