mirror of
https://github.com/nodejs/node.git
synced 2026-10-28 10:15:07 +08:00
stream: remove asIndexedPairs
PR-URL: https://github.com/nodejs/node/pull/48150 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
This commit is contained in:
1 parent
d402e2ab78
commit
d4e99b1a66
4 files changed
-125
No files matched your search
@@ -2447,37 +2447,6 @@ import { Readable } from 'node:stream';
|
||||
await Readable.from([1, 2, 3, 4]).take(2).toArray(); // [1, 2]
|
||||
```
|
||||
|
||||
##### `readable.asIndexedPairs([options])`
|
||||
|
||||
<!-- YAML
|
||||
added:
|
||||
- v17.5.0
|
||||
- v16.15.0
|
||||
changes:
|
||||
- version: v20.3.0
|
||||
pr-url: https://github.com/nodejs/node/pull/48102
|
||||
description: Using the `asIndexedPairs` method emits a runtime warning that
|
||||
it will be removed in a future version.
|
||||
-->
|
||||
|
||||
> Stability: 1 - Experimental
|
||||
|
||||
* `options` {Object}
|
||||
* `signal` {AbortSignal} allows destroying the stream if the signal is
|
||||
aborted.
|
||||
* Returns: {Readable} a stream of indexed pairs.
|
||||
|
||||
This method returns a new stream with chunks of the underlying stream paired
|
||||
with a counter in the form `[index, chunk]`. The first index value is 0 and it
|
||||
increases by 1 for each chunk produced.
|
||||
|
||||
```mjs
|
||||
import { Readable } from 'node:stream';
|
||||
|
||||
const pairs = await Readable.from(['a', 'b', 'c']).asIndexedPairs().toArray();
|
||||
console.log(pairs); // [[0, 'a'], [1, 'b'], [2, 'c']]
|
||||
```
|
||||
|
||||
##### `readable.reduce(fn[, initial[, options]])`
|
||||
|
||||
<!-- YAML
|
||||
|
||||
@@ -23,7 +23,6 @@ const {
|
||||
addAbortSignalNoValidate,
|
||||
} = require('internal/streams/add-abort-signal');
|
||||
const { isWritable, isNodeStream } = require('internal/streams/utils');
|
||||
const { deprecate } = require('internal/util');
|
||||
|
||||
const {
|
||||
ArrayPrototypePush,
|
||||
@@ -199,25 +198,6 @@ function map(fn, options) {
|
||||
}.call(this);
|
||||
}
|
||||
|
||||
function asIndexedPairs(options = undefined) {
|
||||
if (options != null) {
|
||||
validateObject(options, 'options');
|
||||
}
|
||||
if (options?.signal != null) {
|
||||
validateAbortSignal(options.signal, 'options.signal');
|
||||
}
|
||||
|
||||
return async function* asIndexedPairs() {
|
||||
let index = 0;
|
||||
for await (const val of this) {
|
||||
if (options?.signal?.aborted) {
|
||||
throw new AbortError({ cause: options.signal.reason });
|
||||
}
|
||||
yield [index++, val];
|
||||
}
|
||||
}.call(this);
|
||||
}
|
||||
|
||||
async function some(fn, options = undefined) {
|
||||
for await (const unused of filter.call(this, fn, options)) {
|
||||
return true;
|
||||
@@ -421,7 +401,6 @@ function take(number, options = undefined) {
|
||||
}
|
||||
|
||||
module.exports.streamReturningOperators = {
|
||||
asIndexedPairs: deprecate(asIndexedPairs, 'readable.asIndexedPairs will be removed in a future version.'),
|
||||
drop,
|
||||
filter,
|
||||
flatMap,
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
import '../common/index.mjs';
|
||||
import { Readable } from 'stream';
|
||||
import { deepStrictEqual, rejects, throws } from 'assert';
|
||||
|
||||
{
|
||||
// asIndexedPairs with a synchronous stream
|
||||
const pairs = await Readable.from([1, 2, 3]).asIndexedPairs().toArray();
|
||||
deepStrictEqual(pairs, [[0, 1], [1, 2], [2, 3]]);
|
||||
const empty = await Readable.from([]).asIndexedPairs().toArray();
|
||||
deepStrictEqual(empty, []);
|
||||
}
|
||||
|
||||
{
|
||||
// asIndexedPairs works an asynchronous streams
|
||||
const asyncFrom = (...args) => Readable.from(...args).map(async (x) => x);
|
||||
const pairs = await asyncFrom([1, 2, 3]).asIndexedPairs().toArray();
|
||||
deepStrictEqual(pairs, [[0, 1], [1, 2], [2, 3]]);
|
||||
const empty = await asyncFrom([]).asIndexedPairs().toArray();
|
||||
deepStrictEqual(empty, []);
|
||||
}
|
||||
|
||||
{
|
||||
// Does not enumerate an infinite stream
|
||||
const infinite = () => Readable.from(async function* () {
|
||||
while (true) yield 1;
|
||||
}());
|
||||
const pairs = await infinite().asIndexedPairs().take(3).toArray();
|
||||
deepStrictEqual(pairs, [[0, 1], [1, 1], [2, 1]]);
|
||||
const empty = await infinite().asIndexedPairs().take(0).toArray();
|
||||
deepStrictEqual(empty, []);
|
||||
}
|
||||
|
||||
{
|
||||
// AbortSignal
|
||||
await rejects(async () => {
|
||||
const ac = new AbortController();
|
||||
const { signal } = ac;
|
||||
const p = Readable.from([1, 2, 3]).asIndexedPairs({ signal }).toArray();
|
||||
ac.abort();
|
||||
await p;
|
||||
}, { name: 'AbortError' });
|
||||
|
||||
await rejects(async () => {
|
||||
const signal = AbortSignal.abort();
|
||||
await Readable.from([1, 2, 3]).asIndexedPairs({ signal }).toArray();
|
||||
}, /AbortError/);
|
||||
}
|
||||
|
||||
{
|
||||
// Error cases
|
||||
throws(() => Readable.from([1]).asIndexedPairs(1), /ERR_INVALID_ARG_TYPE/);
|
||||
throws(() => Readable.from([1]).asIndexedPairs({ signal: true }), /ERR_INVALID_ARG_TYPE/);
|
||||
}
|
||||
@@ -48,26 +48,6 @@ import assert from 'assert';
|
||||
|
||||
// Note all the tests that check AsyncIterator's prototype itself and things
|
||||
// that happen before stream conversion were not ported.
|
||||
{
|
||||
// asIndexedPairs/is-function
|
||||
assert.strictEqual(typeof Readable.prototype.asIndexedPairs, 'function');
|
||||
// asIndexedPairs/indexed-pairs.js
|
||||
const iterator = Readable.from([0, 1]);
|
||||
const indexedPairs = iterator.asIndexedPairs();
|
||||
|
||||
for await (const [i, v] of indexedPairs) {
|
||||
assert.strictEqual(i, v);
|
||||
}
|
||||
// asIndexedPairs/length.js
|
||||
assert.strictEqual(Readable.prototype.asIndexedPairs.length, 0);
|
||||
const descriptor = Object.getOwnPropertyDescriptor(
|
||||
Readable.prototype,
|
||||
'asIndexedPairs'
|
||||
);
|
||||
assert.strictEqual(descriptor.enumerable, false);
|
||||
assert.strictEqual(descriptor.configurable, true);
|
||||
assert.strictEqual(descriptor.writable, true);
|
||||
}
|
||||
{
|
||||
// drop/length
|
||||
assert.strictEqual(Readable.prototype.drop.length, 1);
|
||||
|
||||
Reference in new issue
Block a user