11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
51cb0ef41Sopenharmony_ciconst {
61cb0ef41Sopenharmony_ci  Readable,
71cb0ef41Sopenharmony_ci} = require('stream');
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ciconst { setTimeout } = require('timers/promises');
101cb0ef41Sopenharmony_ciconst { createReadStream } = require('fs');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_cifunction oneTo5() {
131cb0ef41Sopenharmony_ci  return Readable.from([1, 2, 3, 4, 5]);
141cb0ef41Sopenharmony_ci}
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci{
171cb0ef41Sopenharmony_ci  // flatMap works on synchronous streams with a synchronous mapper
181cb0ef41Sopenharmony_ci  (async () => {
191cb0ef41Sopenharmony_ci    assert.deepStrictEqual(
201cb0ef41Sopenharmony_ci      await oneTo5().flatMap((x) => [x + x]).toArray(),
211cb0ef41Sopenharmony_ci      [2, 4, 6, 8, 10]
221cb0ef41Sopenharmony_ci    );
231cb0ef41Sopenharmony_ci    assert.deepStrictEqual(
241cb0ef41Sopenharmony_ci      await oneTo5().flatMap(() => []).toArray(),
251cb0ef41Sopenharmony_ci      []
261cb0ef41Sopenharmony_ci    );
271cb0ef41Sopenharmony_ci    assert.deepStrictEqual(
281cb0ef41Sopenharmony_ci      await oneTo5().flatMap((x) => [x, x]).toArray(),
291cb0ef41Sopenharmony_ci      [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
301cb0ef41Sopenharmony_ci    );
311cb0ef41Sopenharmony_ci  })().then(common.mustCall());
321cb0ef41Sopenharmony_ci}
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci{
361cb0ef41Sopenharmony_ci  // flatMap works on sync/async streams with an asynchronous mapper
371cb0ef41Sopenharmony_ci  (async () => {
381cb0ef41Sopenharmony_ci    assert.deepStrictEqual(
391cb0ef41Sopenharmony_ci      await oneTo5().flatMap(async (x) => [x, x]).toArray(),
401cb0ef41Sopenharmony_ci      [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
411cb0ef41Sopenharmony_ci    );
421cb0ef41Sopenharmony_ci    const asyncOneTo5 = oneTo5().map(async (x) => x);
431cb0ef41Sopenharmony_ci    assert.deepStrictEqual(
441cb0ef41Sopenharmony_ci      await asyncOneTo5.flatMap(async (x) => [x, x]).toArray(),
451cb0ef41Sopenharmony_ci      [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
461cb0ef41Sopenharmony_ci    );
471cb0ef41Sopenharmony_ci  })().then(common.mustCall());
481cb0ef41Sopenharmony_ci}
491cb0ef41Sopenharmony_ci{
501cb0ef41Sopenharmony_ci  // flatMap works on a stream where mapping returns a stream
511cb0ef41Sopenharmony_ci  (async () => {
521cb0ef41Sopenharmony_ci    const result = await oneTo5().flatMap(async (x) => {
531cb0ef41Sopenharmony_ci      return Readable.from([x, x]);
541cb0ef41Sopenharmony_ci    }).toArray();
551cb0ef41Sopenharmony_ci    assert.deepStrictEqual(result, [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]);
561cb0ef41Sopenharmony_ci  })().then(common.mustCall());
571cb0ef41Sopenharmony_ci  // flatMap works on an objectMode stream where mappign returns a stream
581cb0ef41Sopenharmony_ci  (async () => {
591cb0ef41Sopenharmony_ci    const result = await oneTo5().flatMap(() => {
601cb0ef41Sopenharmony_ci      return createReadStream(fixtures.path('x.txt'));
611cb0ef41Sopenharmony_ci    }).toArray();
621cb0ef41Sopenharmony_ci    // The resultant stream is in object mode so toArray shouldn't flatten
631cb0ef41Sopenharmony_ci    assert.strictEqual(result.length, 5);
641cb0ef41Sopenharmony_ci    assert.deepStrictEqual(
651cb0ef41Sopenharmony_ci      Buffer.concat(result).toString(),
661cb0ef41Sopenharmony_ci      'xyz\n'.repeat(5)
671cb0ef41Sopenharmony_ci    );
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci  })().then(common.mustCall());
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci}
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci{
741cb0ef41Sopenharmony_ci  // Concurrency + AbortSignal
751cb0ef41Sopenharmony_ci  const ac = new AbortController();
761cb0ef41Sopenharmony_ci  const stream = oneTo5().flatMap(common.mustNotCall(async (_, { signal }) => {
771cb0ef41Sopenharmony_ci    await setTimeout(100, { signal });
781cb0ef41Sopenharmony_ci  }), { signal: ac.signal, concurrency: 2 });
791cb0ef41Sopenharmony_ci  // pump
801cb0ef41Sopenharmony_ci  assert.rejects(async () => {
811cb0ef41Sopenharmony_ci    for await (const item of stream) {
821cb0ef41Sopenharmony_ci      // nope
831cb0ef41Sopenharmony_ci      console.log(item);
841cb0ef41Sopenharmony_ci    }
851cb0ef41Sopenharmony_ci  }, {
861cb0ef41Sopenharmony_ci    name: 'AbortError',
871cb0ef41Sopenharmony_ci  }).then(common.mustCall());
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci  queueMicrotask(() => {
901cb0ef41Sopenharmony_ci    ac.abort();
911cb0ef41Sopenharmony_ci  });
921cb0ef41Sopenharmony_ci}
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci{
951cb0ef41Sopenharmony_ci  // Already aborted AbortSignal
961cb0ef41Sopenharmony_ci  const stream = oneTo5().flatMap(common.mustNotCall(async (_, { signal }) => {
971cb0ef41Sopenharmony_ci    await setTimeout(100, { signal });
981cb0ef41Sopenharmony_ci  }), { signal: AbortSignal.abort() });
991cb0ef41Sopenharmony_ci  // pump
1001cb0ef41Sopenharmony_ci  assert.rejects(async () => {
1011cb0ef41Sopenharmony_ci    for await (const item of stream) {
1021cb0ef41Sopenharmony_ci      // nope
1031cb0ef41Sopenharmony_ci      console.log(item);
1041cb0ef41Sopenharmony_ci    }
1051cb0ef41Sopenharmony_ci  }, {
1061cb0ef41Sopenharmony_ci    name: 'AbortError',
1071cb0ef41Sopenharmony_ci  }).then(common.mustCall());
1081cb0ef41Sopenharmony_ci}
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ci{
1111cb0ef41Sopenharmony_ci  // Error cases
1121cb0ef41Sopenharmony_ci  assert.throws(() => Readable.from([1]).flatMap(1), /ERR_INVALID_ARG_TYPE/);
1131cb0ef41Sopenharmony_ci  assert.throws(() => Readable.from([1]).flatMap((x) => x, {
1141cb0ef41Sopenharmony_ci    concurrency: 'Foo'
1151cb0ef41Sopenharmony_ci  }), /ERR_OUT_OF_RANGE/);
1161cb0ef41Sopenharmony_ci  assert.throws(() => Readable.from([1]).flatMap((x) => x, 1), /ERR_INVALID_ARG_TYPE/);
1171cb0ef41Sopenharmony_ci  assert.throws(() => Readable.from([1]).flatMap((x) => x, { signal: true }), /ERR_INVALID_ARG_TYPE/);
1181cb0ef41Sopenharmony_ci}
1191cb0ef41Sopenharmony_ci{
1201cb0ef41Sopenharmony_ci  // Test result is a Readable
1211cb0ef41Sopenharmony_ci  const stream = oneTo5().flatMap((x) => x);
1221cb0ef41Sopenharmony_ci  assert.strictEqual(stream.readable, true);
1231cb0ef41Sopenharmony_ci}
1241cb0ef41Sopenharmony_ci{
1251cb0ef41Sopenharmony_ci  const stream = oneTo5();
1261cb0ef41Sopenharmony_ci  Object.defineProperty(stream, 'map', {
1271cb0ef41Sopenharmony_ci    value: common.mustNotCall(),
1281cb0ef41Sopenharmony_ci  });
1291cb0ef41Sopenharmony_ci  // Check that map isn't getting called.
1301cb0ef41Sopenharmony_ci  stream.flatMap(() => true);
1311cb0ef41Sopenharmony_ci}
132