11cb0ef41Sopenharmony_ci// Flags: --no-warnings
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst {
81cb0ef41Sopenharmony_ci  arrayBuffer,
91cb0ef41Sopenharmony_ci  blob,
101cb0ef41Sopenharmony_ci  buffer,
111cb0ef41Sopenharmony_ci  text,
121cb0ef41Sopenharmony_ci  json,
131cb0ef41Sopenharmony_ci} = require('stream/consumers');
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciconst {
161cb0ef41Sopenharmony_ci  Readable,
171cb0ef41Sopenharmony_ci  PassThrough
181cb0ef41Sopenharmony_ci} = require('stream');
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciconst {
211cb0ef41Sopenharmony_ci  TransformStream,
221cb0ef41Sopenharmony_ci} = require('stream/web');
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ciconst buf = Buffer.from('hellothere');
251cb0ef41Sopenharmony_ciconst kArrayBuffer =
261cb0ef41Sopenharmony_ci  buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci{
291cb0ef41Sopenharmony_ci  const passthrough = new PassThrough();
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  blob(passthrough).then(common.mustCall(async (blob) => {
321cb0ef41Sopenharmony_ci    assert.strictEqual(blob.size, 10);
331cb0ef41Sopenharmony_ci    assert.deepStrictEqual(await blob.arrayBuffer(), kArrayBuffer);
341cb0ef41Sopenharmony_ci  }));
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  passthrough.write('hello');
371cb0ef41Sopenharmony_ci  setTimeout(() => passthrough.end('there'), 10);
381cb0ef41Sopenharmony_ci}
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci{
411cb0ef41Sopenharmony_ci  const passthrough = new PassThrough();
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  arrayBuffer(passthrough).then(common.mustCall(async (ab) => {
441cb0ef41Sopenharmony_ci    assert.strictEqual(ab.byteLength, 10);
451cb0ef41Sopenharmony_ci    assert.deepStrictEqual(ab, kArrayBuffer);
461cb0ef41Sopenharmony_ci  }));
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  passthrough.write('hello');
491cb0ef41Sopenharmony_ci  setTimeout(() => passthrough.end('there'), 10);
501cb0ef41Sopenharmony_ci}
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci{
531cb0ef41Sopenharmony_ci  const passthrough = new PassThrough();
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  buffer(passthrough).then(common.mustCall(async (buf) => {
561cb0ef41Sopenharmony_ci    assert.strictEqual(buf.byteLength, 10);
571cb0ef41Sopenharmony_ci    assert.deepStrictEqual(buf.buffer, kArrayBuffer);
581cb0ef41Sopenharmony_ci  }));
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  passthrough.write('hello');
611cb0ef41Sopenharmony_ci  setTimeout(() => passthrough.end('there'), 10);
621cb0ef41Sopenharmony_ci}
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci{
661cb0ef41Sopenharmony_ci  const passthrough = new PassThrough();
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci  text(passthrough).then(common.mustCall(async (str) => {
691cb0ef41Sopenharmony_ci    assert.strictEqual(str.length, 10);
701cb0ef41Sopenharmony_ci    assert.strictEqual(str, 'hellothere');
711cb0ef41Sopenharmony_ci  }));
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci  passthrough.write('hello');
741cb0ef41Sopenharmony_ci  setTimeout(() => passthrough.end('there'), 10);
751cb0ef41Sopenharmony_ci}
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci{
781cb0ef41Sopenharmony_ci  const readable = new Readable({
791cb0ef41Sopenharmony_ci    read() {}
801cb0ef41Sopenharmony_ci  });
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci  text(readable).then((data) => {
831cb0ef41Sopenharmony_ci    assert.strictEqual(data, 'foo\ufffd\ufffd\ufffd');
841cb0ef41Sopenharmony_ci  });
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci  readable.push(new Uint8Array([0x66, 0x6f, 0x6f, 0xed, 0xa0, 0x80]));
871cb0ef41Sopenharmony_ci  readable.push(null);
881cb0ef41Sopenharmony_ci}
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci{
911cb0ef41Sopenharmony_ci  const passthrough = new PassThrough();
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci  json(passthrough).then(common.mustCall(async (str) => {
941cb0ef41Sopenharmony_ci    assert.strictEqual(str.length, 10);
951cb0ef41Sopenharmony_ci    assert.strictEqual(str, 'hellothere');
961cb0ef41Sopenharmony_ci  }));
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci  passthrough.write('"hello');
991cb0ef41Sopenharmony_ci  setTimeout(() => passthrough.end('there"'), 10);
1001cb0ef41Sopenharmony_ci}
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci{
1031cb0ef41Sopenharmony_ci  const { writable, readable } = new TransformStream();
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ci  blob(readable).then(common.mustCall(async (blob) => {
1061cb0ef41Sopenharmony_ci    assert.strictEqual(blob.size, 10);
1071cb0ef41Sopenharmony_ci    assert.deepStrictEqual(await blob.arrayBuffer(), kArrayBuffer);
1081cb0ef41Sopenharmony_ci  }));
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ci  const writer = writable.getWriter();
1111cb0ef41Sopenharmony_ci  writer.write('hello');
1121cb0ef41Sopenharmony_ci  setTimeout(() => {
1131cb0ef41Sopenharmony_ci    writer.write('there');
1141cb0ef41Sopenharmony_ci    writer.close();
1151cb0ef41Sopenharmony_ci  }, 10);
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ci  assert.rejects(blob(readable), { code: 'ERR_INVALID_STATE' });
1181cb0ef41Sopenharmony_ci}
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci{
1211cb0ef41Sopenharmony_ci  const { writable, readable } = new TransformStream();
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_ci  arrayBuffer(readable).then(common.mustCall(async (ab) => {
1241cb0ef41Sopenharmony_ci    assert.strictEqual(ab.byteLength, 10);
1251cb0ef41Sopenharmony_ci    assert.deepStrictEqual(ab, kArrayBuffer);
1261cb0ef41Sopenharmony_ci  }));
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ci  const writer = writable.getWriter();
1291cb0ef41Sopenharmony_ci  writer.write('hello');
1301cb0ef41Sopenharmony_ci  setTimeout(() => {
1311cb0ef41Sopenharmony_ci    writer.write('there');
1321cb0ef41Sopenharmony_ci    writer.close();
1331cb0ef41Sopenharmony_ci  }, 10);
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_ci  assert.rejects(arrayBuffer(readable), { code: 'ERR_INVALID_STATE' });
1361cb0ef41Sopenharmony_ci}
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ci{
1391cb0ef41Sopenharmony_ci  const { writable, readable } = new TransformStream();
1401cb0ef41Sopenharmony_ci
1411cb0ef41Sopenharmony_ci  text(readable).then(common.mustCall(async (str) => {
1421cb0ef41Sopenharmony_ci    assert.strictEqual(str.length, 10);
1431cb0ef41Sopenharmony_ci    assert.strictEqual(str, 'hellothere');
1441cb0ef41Sopenharmony_ci  }));
1451cb0ef41Sopenharmony_ci
1461cb0ef41Sopenharmony_ci  const writer = writable.getWriter();
1471cb0ef41Sopenharmony_ci  writer.write('hello');
1481cb0ef41Sopenharmony_ci  setTimeout(() => {
1491cb0ef41Sopenharmony_ci    writer.write('there');
1501cb0ef41Sopenharmony_ci    writer.close();
1511cb0ef41Sopenharmony_ci  }, 10);
1521cb0ef41Sopenharmony_ci
1531cb0ef41Sopenharmony_ci  assert.rejects(text(readable), { code: 'ERR_INVALID_STATE' });
1541cb0ef41Sopenharmony_ci}
1551cb0ef41Sopenharmony_ci
1561cb0ef41Sopenharmony_ci{
1571cb0ef41Sopenharmony_ci  const { writable, readable } = new TransformStream();
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ci  json(readable).then(common.mustCall(async (str) => {
1601cb0ef41Sopenharmony_ci    assert.strictEqual(str.length, 10);
1611cb0ef41Sopenharmony_ci    assert.strictEqual(str, 'hellothere');
1621cb0ef41Sopenharmony_ci  }));
1631cb0ef41Sopenharmony_ci
1641cb0ef41Sopenharmony_ci  const writer = writable.getWriter();
1651cb0ef41Sopenharmony_ci  writer.write('"hello');
1661cb0ef41Sopenharmony_ci  setTimeout(() => {
1671cb0ef41Sopenharmony_ci    writer.write('there"');
1681cb0ef41Sopenharmony_ci    writer.close();
1691cb0ef41Sopenharmony_ci  }, 10);
1701cb0ef41Sopenharmony_ci
1711cb0ef41Sopenharmony_ci  assert.rejects(json(readable), { code: 'ERR_INVALID_STATE' });
1721cb0ef41Sopenharmony_ci}
1731cb0ef41Sopenharmony_ci
1741cb0ef41Sopenharmony_ci{
1751cb0ef41Sopenharmony_ci  const stream = new PassThrough({
1761cb0ef41Sopenharmony_ci    readableObjectMode: true,
1771cb0ef41Sopenharmony_ci    writableObjectMode: true,
1781cb0ef41Sopenharmony_ci  });
1791cb0ef41Sopenharmony_ci
1801cb0ef41Sopenharmony_ci  blob(stream).then(common.mustCall((blob) => {
1811cb0ef41Sopenharmony_ci    assert.strictEqual(blob.size, 30);
1821cb0ef41Sopenharmony_ci  }));
1831cb0ef41Sopenharmony_ci
1841cb0ef41Sopenharmony_ci  stream.write({});
1851cb0ef41Sopenharmony_ci  stream.end({});
1861cb0ef41Sopenharmony_ci}
1871cb0ef41Sopenharmony_ci
1881cb0ef41Sopenharmony_ci{
1891cb0ef41Sopenharmony_ci  const stream = new PassThrough({
1901cb0ef41Sopenharmony_ci    readableObjectMode: true,
1911cb0ef41Sopenharmony_ci    writableObjectMode: true,
1921cb0ef41Sopenharmony_ci  });
1931cb0ef41Sopenharmony_ci
1941cb0ef41Sopenharmony_ci  arrayBuffer(stream).then(common.mustCall((ab) => {
1951cb0ef41Sopenharmony_ci    assert.strictEqual(ab.byteLength, 30);
1961cb0ef41Sopenharmony_ci    assert.strictEqual(
1971cb0ef41Sopenharmony_ci      Buffer.from(ab).toString(),
1981cb0ef41Sopenharmony_ci      '[object Object][object Object]');
1991cb0ef41Sopenharmony_ci  }));
2001cb0ef41Sopenharmony_ci
2011cb0ef41Sopenharmony_ci  stream.write({});
2021cb0ef41Sopenharmony_ci  stream.end({});
2031cb0ef41Sopenharmony_ci}
2041cb0ef41Sopenharmony_ci
2051cb0ef41Sopenharmony_ci{
2061cb0ef41Sopenharmony_ci  const stream = new PassThrough({
2071cb0ef41Sopenharmony_ci    readableObjectMode: true,
2081cb0ef41Sopenharmony_ci    writableObjectMode: true,
2091cb0ef41Sopenharmony_ci  });
2101cb0ef41Sopenharmony_ci
2111cb0ef41Sopenharmony_ci  buffer(stream).then(common.mustCall((buf) => {
2121cb0ef41Sopenharmony_ci    assert.strictEqual(buf.byteLength, 30);
2131cb0ef41Sopenharmony_ci    assert.strictEqual(
2141cb0ef41Sopenharmony_ci      buf.toString(),
2151cb0ef41Sopenharmony_ci      '[object Object][object Object]');
2161cb0ef41Sopenharmony_ci  }));
2171cb0ef41Sopenharmony_ci
2181cb0ef41Sopenharmony_ci  stream.write({});
2191cb0ef41Sopenharmony_ci  stream.end({});
2201cb0ef41Sopenharmony_ci}
2211cb0ef41Sopenharmony_ci
2221cb0ef41Sopenharmony_ci{
2231cb0ef41Sopenharmony_ci  const stream = new PassThrough({
2241cb0ef41Sopenharmony_ci    readableObjectMode: true,
2251cb0ef41Sopenharmony_ci    writableObjectMode: true,
2261cb0ef41Sopenharmony_ci  });
2271cb0ef41Sopenharmony_ci
2281cb0ef41Sopenharmony_ci  assert.rejects(text(stream), {
2291cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
2301cb0ef41Sopenharmony_ci  });
2311cb0ef41Sopenharmony_ci
2321cb0ef41Sopenharmony_ci  stream.write({});
2331cb0ef41Sopenharmony_ci  stream.end({});
2341cb0ef41Sopenharmony_ci}
2351cb0ef41Sopenharmony_ci
2361cb0ef41Sopenharmony_ci{
2371cb0ef41Sopenharmony_ci  const stream = new PassThrough({
2381cb0ef41Sopenharmony_ci    readableObjectMode: true,
2391cb0ef41Sopenharmony_ci    writableObjectMode: true,
2401cb0ef41Sopenharmony_ci  });
2411cb0ef41Sopenharmony_ci
2421cb0ef41Sopenharmony_ci  assert.rejects(json(stream), {
2431cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
2441cb0ef41Sopenharmony_ci  });
2451cb0ef41Sopenharmony_ci
2461cb0ef41Sopenharmony_ci  stream.write({});
2471cb0ef41Sopenharmony_ci  stream.end({});
2481cb0ef41Sopenharmony_ci}
2491cb0ef41Sopenharmony_ci
2501cb0ef41Sopenharmony_ci{
2511cb0ef41Sopenharmony_ci  const stream = new TransformStream();
2521cb0ef41Sopenharmony_ci  text(stream.readable).then(common.mustCall((str) => {
2531cb0ef41Sopenharmony_ci    // Incomplete utf8 character is flushed as a replacement char
2541cb0ef41Sopenharmony_ci    assert.strictEqual(str.charCodeAt(0), 0xfffd);
2551cb0ef41Sopenharmony_ci  }));
2561cb0ef41Sopenharmony_ci  const writer = stream.writable.getWriter();
2571cb0ef41Sopenharmony_ci  Promise.all([
2581cb0ef41Sopenharmony_ci    writer.write(new Uint8Array([0xe2])),
2591cb0ef41Sopenharmony_ci    writer.write(new Uint8Array([0x82])),
2601cb0ef41Sopenharmony_ci    writer.close(),
2611cb0ef41Sopenharmony_ci  ]).then(common.mustCall());
2621cb0ef41Sopenharmony_ci}
263