11cb0ef41Sopenharmony_ci// Flags: --expose-internals --no-warnings
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst {
81cb0ef41Sopenharmony_ci  ReadableStream,
91cb0ef41Sopenharmony_ci  TransformStream,
101cb0ef41Sopenharmony_ci  TransformStreamDefaultController,
111cb0ef41Sopenharmony_ci} = require('stream/web');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst {
141cb0ef41Sopenharmony_ci  createReadStream,
151cb0ef41Sopenharmony_ci  readFileSync,
161cb0ef41Sopenharmony_ci} = require('fs');
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciconst {
191cb0ef41Sopenharmony_ci  kTransfer,
201cb0ef41Sopenharmony_ci} = require('internal/worker/js_transferable');
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ciconst {
231cb0ef41Sopenharmony_ci  inspect,
241cb0ef41Sopenharmony_ci} = require('util');
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciassert.throws(() => new TransformStream({ readableType: 1 }), {
271cb0ef41Sopenharmony_ci  code: 'ERR_INVALID_ARG_VALUE',
281cb0ef41Sopenharmony_ci});
291cb0ef41Sopenharmony_ciassert.throws(() => new TransformStream({ writableType: 1 }), {
301cb0ef41Sopenharmony_ci  code: 'ERR_INVALID_ARG_VALUE',
311cb0ef41Sopenharmony_ci});
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci{
351cb0ef41Sopenharmony_ci  const stream = new TransformStream();
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  async function test(stream) {
381cb0ef41Sopenharmony_ci    const writer = stream.writable.getWriter();
391cb0ef41Sopenharmony_ci    const reader = stream.readable.getReader();
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci    const { 1: result } = await Promise.all([
421cb0ef41Sopenharmony_ci      writer.write('hello'),
431cb0ef41Sopenharmony_ci      reader.read(),
441cb0ef41Sopenharmony_ci    ]);
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci    assert.strictEqual(result.value, 'hello');
471cb0ef41Sopenharmony_ci  }
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  test(stream).then(common.mustCall());
501cb0ef41Sopenharmony_ci}
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ciclass Transform {
531cb0ef41Sopenharmony_ci  start(controller) {
541cb0ef41Sopenharmony_ci    this.started = true;
551cb0ef41Sopenharmony_ci  }
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  async transform(chunk, controller) {
581cb0ef41Sopenharmony_ci    controller.enqueue(chunk.toUpperCase());
591cb0ef41Sopenharmony_ci  }
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  async flush() {
621cb0ef41Sopenharmony_ci    this.flushed = true;
631cb0ef41Sopenharmony_ci  }
641cb0ef41Sopenharmony_ci}
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci{
671cb0ef41Sopenharmony_ci  const transform = new Transform();
681cb0ef41Sopenharmony_ci  const stream = new TransformStream(transform);
691cb0ef41Sopenharmony_ci  assert(transform.started);
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci  async function test(stream) {
721cb0ef41Sopenharmony_ci    const writer = stream.writable.getWriter();
731cb0ef41Sopenharmony_ci    const reader = stream.readable.getReader();
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci    const { 1: result } = await Promise.all([
761cb0ef41Sopenharmony_ci      writer.write('hello'),
771cb0ef41Sopenharmony_ci      reader.read(),
781cb0ef41Sopenharmony_ci    ]);
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci    assert.strictEqual(result.value, 'HELLO');
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci    await writer.close();
831cb0ef41Sopenharmony_ci  }
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci  test(stream).then(common.mustCall(() => {
861cb0ef41Sopenharmony_ci    assert(transform.flushed);
871cb0ef41Sopenharmony_ci  }));
881cb0ef41Sopenharmony_ci}
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ciclass Source {
911cb0ef41Sopenharmony_ci  constructor() {
921cb0ef41Sopenharmony_ci    this.cancelCalled = false;
931cb0ef41Sopenharmony_ci  }
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci  start(controller) {
961cb0ef41Sopenharmony_ci    this.stream = createReadStream(__filename);
971cb0ef41Sopenharmony_ci    this.stream.on('data', (chunk) => {
981cb0ef41Sopenharmony_ci      controller.enqueue(chunk.toString());
991cb0ef41Sopenharmony_ci    });
1001cb0ef41Sopenharmony_ci    this.stream.once('end', () => {
1011cb0ef41Sopenharmony_ci      if (!this.cancelCalled)
1021cb0ef41Sopenharmony_ci        controller.close();
1031cb0ef41Sopenharmony_ci    });
1041cb0ef41Sopenharmony_ci    this.stream.once('error', (error) => {
1051cb0ef41Sopenharmony_ci      controller.error(error);
1061cb0ef41Sopenharmony_ci    });
1071cb0ef41Sopenharmony_ci  }
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ci  cancel() {
1101cb0ef41Sopenharmony_ci    this.cancelCalled = true;
1111cb0ef41Sopenharmony_ci  }
1121cb0ef41Sopenharmony_ci}
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ci{
1151cb0ef41Sopenharmony_ci  const instream = new ReadableStream(new Source());
1161cb0ef41Sopenharmony_ci  const tstream = new TransformStream(new Transform());
1171cb0ef41Sopenharmony_ci  const r = instream.pipeThrough(tstream);
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci  async function read(stream) {
1201cb0ef41Sopenharmony_ci    let res = '';
1211cb0ef41Sopenharmony_ci    for await (const chunk of stream)
1221cb0ef41Sopenharmony_ci      res += chunk;
1231cb0ef41Sopenharmony_ci    return res;
1241cb0ef41Sopenharmony_ci  }
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_ci  read(r).then(common.mustCall((data) => {
1271cb0ef41Sopenharmony_ci    const check = readFileSync(__filename);
1281cb0ef41Sopenharmony_ci    assert.strictEqual(check.toString().toUpperCase(), data);
1291cb0ef41Sopenharmony_ci  }));
1301cb0ef41Sopenharmony_ci}
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci{
1331cb0ef41Sopenharmony_ci  assert.throws(() => Reflect.get(TransformStream.prototype, 'readable', {}), {
1341cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
1351cb0ef41Sopenharmony_ci  });
1361cb0ef41Sopenharmony_ci  assert.throws(() => Reflect.get(TransformStream.prototype, 'writable', {}), {
1371cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
1381cb0ef41Sopenharmony_ci  });
1391cb0ef41Sopenharmony_ci  assert.throws(() => TransformStream.prototype[kTransfer]({}), {
1401cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
1411cb0ef41Sopenharmony_ci  });
1421cb0ef41Sopenharmony_ci
1431cb0ef41Sopenharmony_ci  assert.throws(() => {
1441cb0ef41Sopenharmony_ci    Reflect.get(TransformStreamDefaultController.prototype, 'desiredSize', {});
1451cb0ef41Sopenharmony_ci  }, {
1461cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
1471cb0ef41Sopenharmony_ci  });
1481cb0ef41Sopenharmony_ci  assert.throws(() => {
1491cb0ef41Sopenharmony_ci    TransformStreamDefaultController.prototype.enqueue({});
1501cb0ef41Sopenharmony_ci  }, {
1511cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
1521cb0ef41Sopenharmony_ci  });
1531cb0ef41Sopenharmony_ci  assert.throws(() => {
1541cb0ef41Sopenharmony_ci    TransformStreamDefaultController.prototype.error({});
1551cb0ef41Sopenharmony_ci  }, {
1561cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
1571cb0ef41Sopenharmony_ci  });
1581cb0ef41Sopenharmony_ci  assert.throws(() => {
1591cb0ef41Sopenharmony_ci    TransformStreamDefaultController.prototype.terminate({});
1601cb0ef41Sopenharmony_ci  }, {
1611cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_THIS',
1621cb0ef41Sopenharmony_ci  });
1631cb0ef41Sopenharmony_ci
1641cb0ef41Sopenharmony_ci  assert.throws(() => new TransformStreamDefaultController(), {
1651cb0ef41Sopenharmony_ci    code: 'ERR_ILLEGAL_CONSTRUCTOR',
1661cb0ef41Sopenharmony_ci  });
1671cb0ef41Sopenharmony_ci}
1681cb0ef41Sopenharmony_ci
1691cb0ef41Sopenharmony_ci{
1701cb0ef41Sopenharmony_ci  let controller;
1711cb0ef41Sopenharmony_ci  const transform = new TransformStream({
1721cb0ef41Sopenharmony_ci    start(c) {
1731cb0ef41Sopenharmony_ci      controller = c;
1741cb0ef41Sopenharmony_ci    }
1751cb0ef41Sopenharmony_ci  });
1761cb0ef41Sopenharmony_ci
1771cb0ef41Sopenharmony_ci  assert.match(inspect(transform), /TransformStream/);
1781cb0ef41Sopenharmony_ci  assert.match(inspect(transform, { depth: null }), /TransformStream/);
1791cb0ef41Sopenharmony_ci  assert.match(inspect(transform, { depth: 0 }), /TransformStream \[/);
1801cb0ef41Sopenharmony_ci
1811cb0ef41Sopenharmony_ci  assert.match(inspect(controller), /TransformStreamDefaultController/);
1821cb0ef41Sopenharmony_ci  assert.match(
1831cb0ef41Sopenharmony_ci    inspect(controller, { depth: null }),
1841cb0ef41Sopenharmony_ci    /TransformStreamDefaultController/);
1851cb0ef41Sopenharmony_ci  assert.match(
1861cb0ef41Sopenharmony_ci    inspect(controller, { depth: 0 }),
1871cb0ef41Sopenharmony_ci    /TransformStreamDefaultController \[/);
1881cb0ef41Sopenharmony_ci}
1891cb0ef41Sopenharmony_ci
1901cb0ef41Sopenharmony_ci{
1911cb0ef41Sopenharmony_ci  Object.defineProperty(Object.prototype, 'type', {
1921cb0ef41Sopenharmony_ci    get: common.mustNotCall('get %Object.prototype%.type'),
1931cb0ef41Sopenharmony_ci    set: common.mustNotCall('set %Object.prototype%.type'),
1941cb0ef41Sopenharmony_ci    configurable: true,
1951cb0ef41Sopenharmony_ci  });
1961cb0ef41Sopenharmony_ci
1971cb0ef41Sopenharmony_ci  new TransformStream({
1981cb0ef41Sopenharmony_ci    transform(chunk, controller) {
1991cb0ef41Sopenharmony_ci      controller.enqueue(chunk);
2001cb0ef41Sopenharmony_ci    },
2011cb0ef41Sopenharmony_ci    flush(controller) {
2021cb0ef41Sopenharmony_ci      controller.terminate();
2031cb0ef41Sopenharmony_ci    }
2041cb0ef41Sopenharmony_ci  });
2051cb0ef41Sopenharmony_ci
2061cb0ef41Sopenharmony_ci  delete Object.prototype.type;
2071cb0ef41Sopenharmony_ci}
208