• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import '../common/index.mjs';
2import { Readable } from 'stream';
3import { deepStrictEqual, rejects, throws } from 'assert';
4
5{
6  // asIndexedPairs with a synchronous stream
7  const pairs = await Readable.from([1, 2, 3]).asIndexedPairs().toArray();
8  deepStrictEqual(pairs, [[0, 1], [1, 2], [2, 3]]);
9  const empty = await Readable.from([]).asIndexedPairs().toArray();
10  deepStrictEqual(empty, []);
11}
12
13{
14  // asIndexedPairs works an asynchronous streams
15  const asyncFrom = (...args) => Readable.from(...args).map(async (x) => x);
16  const pairs = await asyncFrom([1, 2, 3]).asIndexedPairs().toArray();
17  deepStrictEqual(pairs, [[0, 1], [1, 2], [2, 3]]);
18  const empty = await asyncFrom([]).asIndexedPairs().toArray();
19  deepStrictEqual(empty, []);
20}
21
22{
23  // Does not enumerate an infinite stream
24  const infinite = () => Readable.from(async function* () {
25    while (true) yield 1;
26  }());
27  const pairs = await infinite().asIndexedPairs().take(3).toArray();
28  deepStrictEqual(pairs, [[0, 1], [1, 1], [2, 1]]);
29  const empty = await infinite().asIndexedPairs().take(0).toArray();
30  deepStrictEqual(empty, []);
31}
32
33{
34  // AbortSignal
35  await rejects(async () => {
36    const ac = new AbortController();
37    const { signal } = ac;
38    const p = Readable.from([1, 2, 3]).asIndexedPairs({ signal }).toArray();
39    ac.abort();
40    await p;
41  }, { name: 'AbortError' });
42
43  await rejects(async () => {
44    const signal = AbortSignal.abort();
45    await Readable.from([1, 2, 3]).asIndexedPairs({ signal }).toArray();
46  }, /AbortError/);
47}
48
49{
50  // Error cases
51  throws(() => Readable.from([1]).asIndexedPairs(1), /ERR_INVALID_ARG_TYPE/);
52  throws(() => Readable.from([1]).asIndexedPairs({ signal: true }), /ERR_INVALID_ARG_TYPE/);
53}
54