| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 4db343b commit e11a079
8 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,7 +10,10 @@ const { | |||
| 10 | 10 | }, | |
| 11 | 11 | AbortError, | |
| 12 | 12 | } = require('internal/errors'); | |
| 13 | - const { validateInteger } = require('internal/validators'); | ||
| 13 | + const { | ||
| 14 | + validateAbortSignal, | ||
| 15 | + validateInteger, | ||
| 16 | + } = require('internal/validators'); | ||
| 14 | 17 | const { kWeakHandler } = require('internal/event_target'); | |
| 15 | 18 | const { finished } = require('internal/streams/end-of-stream'); | |
| 16 | 19 | ||
@@ -33,10 +36,12 @@ function map(fn, options) { | |||
| 33 | 36 | throw new ERR_INVALID_ARG_TYPE( | |
| 34 | 37 | 'fn', ['Function', 'AsyncFunction'], fn); | |
| 35 | 38 | } | |
| 36 | - | ||
| 37 | 39 | if (options != null && typeof options !== 'object') { | |
| 38 | 40 | throw new ERR_INVALID_ARG_TYPE('options', ['Object']); | |
| 39 | 41 | } | |
| 42 | + if (options?.signal != null) { | ||
| 43 | + validateAbortSignal(options.signal, 'options.signal'); | ||
| 44 | + } | ||
| 40 | 45 | ||
| 41 | 46 | let concurrency = 1; | |
| 42 | 47 | if (options?.concurrency != null) { | |
@@ -161,17 +166,33 @@ function map(fn, options) { | |||
| 161 | 166 | }.call(this); | |
| 162 | 167 | } | |
| 163 | 168 | ||
| 164 | - async function* asIndexedPairs(options) { | ||
| 165 | - let index = 0; | ||
| 166 | - for await (const val of this) { | ||
| 167 | - if (options?.signal?.aborted) { | ||
| 168 | - throw new AbortError({ cause: options.signal.reason }); | ||
| 169 | - } | ||
| 170 | - yield [index++, val]; | ||
| 169 | + function asIndexedPairs(options) { | ||
| 170 | + if (options != null && typeof options !== 'object') { | ||
| 171 | + throw new ERR_INVALID_ARG_TYPE('options', ['Object']); | ||
| 172 | + } | ||
| 173 | + if (options?.signal != null) { | ||
| 174 | + validateAbortSignal(options.signal, 'options.signal'); | ||
| 171 | 175 | } | |
| 176 | + | ||
| 177 | + return async function* asIndexedPairs() { | ||
| 178 | + let index = 0; | ||
| 179 | + for await (const val of this) { | ||
| 180 | + if (options?.signal?.aborted) { | ||
| 181 | + throw new AbortError({ cause: options.signal.reason }); | ||
| 182 | + } | ||
| 183 | + yield [index++, val]; | ||
| 184 | + } | ||
| 185 | + }.call(this); | ||
| 172 | 186 | } | |
| 173 | 187 | ||
| 174 | 188 | async function some(fn, options) { | |
| 189 | + if (options != null && typeof options !== 'object') { | ||
| 190 | + throw new ERR_INVALID_ARG_TYPE('options', ['Object']); | ||
| 191 | + } | ||
| 192 | + if (options?.signal != null) { | ||
| 193 | + validateAbortSignal(options.signal, 'options.signal'); | ||
| 194 | + } | ||
| 195 | + | ||
| 175 | 196 | // https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.some | |
| 176 | 197 | // Note that some does short circuit but also closes the iterator if it does | |
| 177 | 198 | const ac = new AbortController(); | |
@@ -246,6 +267,13 @@ async function reduce(reducer, initialValue, options) { | |||
| 246 | 267 | throw new ERR_INVALID_ARG_TYPE( | |
| 247 | 268 | 'reducer', ['Function', 'AsyncFunction'], reducer); | |
| 248 | 269 | } | |
| 270 | + if (options != null && typeof options !== 'object') { | ||
| 271 | + throw new ERR_INVALID_ARG_TYPE('options', ['Object']); | ||
| 272 | + } | ||
| 273 | + if (options?.signal != null) { | ||
| 274 | + validateAbortSignal(options.signal, 'options.signal'); | ||
| 275 | + } | ||
| 276 | + | ||
| 249 | 277 | let hasInitialValue = arguments.length > 1; | |
| 250 | 278 | if (options?.signal?.aborted) { | |
| 251 | 279 | const err = new AbortError(undefined, { cause: options.signal.reason }); | |
@@ -283,6 +311,13 @@ async function reduce(reducer, initialValue, options) { | |||
| 283 | 311 | } | |
| 284 | 312 | ||
| 285 | 313 | async function toArray(options) { | |
| 314 | + if (options != null && typeof options !== 'object') { | ||
| 315 | + throw new ERR_INVALID_ARG_TYPE('options', ['Object']); | ||
| 316 | + } | ||
| 317 | + if (options?.signal != null) { | ||
| 318 | + validateAbortSignal(options.signal, 'options.signal'); | ||
| 319 | + } | ||
| 320 | + | ||
| 286 | 321 | const result = []; | |
| 287 | 322 | for await (const val of this) { | |
| 288 | 323 | if (options?.signal?.aborted) { | |
@@ -316,6 +351,13 @@ function toIntegerOrInfinity(number) { | |||
| 316 | 351 | } | |
| 317 | 352 | ||
| 318 | 353 | function drop(number, options) { | |
| 354 | + if (options != null && typeof options !== 'object') { | ||
| 355 | + throw new ERR_INVALID_ARG_TYPE('options', ['Object']); | ||
| 356 | + } | ||
| 357 | + if (options?.signal != null) { | ||
| 358 | + validateAbortSignal(options.signal, 'options.signal'); | ||
| 359 | + } | ||
| 360 | + | ||
| 319 | 361 | number = toIntegerOrInfinity(number); | |
| 320 | 362 | return async function* drop() { | |
| 321 | 363 | if (options?.signal?.aborted) { | |
@@ -332,8 +374,14 @@ function drop(number, options) { | |||
| 332 | 374 | }.call(this); | |
| 333 | 375 | } | |
| 334 | 376 | ||
| 335 | - | ||
| 336 | 377 | function take(number, options) { | |
| 378 | + if (options != null && typeof options !== 'object') { | ||
| 379 | + throw new ERR_INVALID_ARG_TYPE('options', ['Object']); | ||
| 380 | + } | ||
| 381 | + if (options?.signal != null) { | ||
| 382 | + validateAbortSignal(options.signal, 'options.signal'); | ||
| 383 | + } | ||
| 384 | + | ||
| 337 | 385 | number = toIntegerOrInfinity(number); | |
| 338 | 386 | return async function* take() { | |
| 339 | 387 | if (options?.signal?.aborted) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,6 @@ | |||
| 1 | 1 | import '../common/index.mjs'; | |
| 2 | 2 | import { Readable } from 'stream'; | |
| 3 | - import { deepStrictEqual, rejects } from 'assert'; | ||
| 3 | + import { deepStrictEqual, rejects, throws } from 'assert'; | ||
| 4 | 4 | ||
| 5 | 5 | { | |
| 6 | 6 | // asIndexedPairs with a synchronous stream | |
@@ -45,3 +45,9 @@ import { deepStrictEqual, rejects } from 'assert'; | |||
| 45 | 45 | await Readable.from([1, 2, 3]).asIndexedPairs({ signal }).toArray(); | |
| 46 | 46 | }, /AbortError/); | |
| 47 | 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 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -93,4 +93,10 @@ const naturals = () => from(async function*() { | |||
| 93 | 93 | for (const example of invalidArgs) { | |
| 94 | 94 | throws(() => from([]).take(example).toArray(), /ERR_OUT_OF_RANGE/); | |
| 95 | 95 | } | |
| 96 | + | ||
| 97 | + throws(() => Readable.from([1]).drop(1, 1), /ERR_INVALID_ARG_TYPE/); | ||
| 98 | + throws(() => Readable.from([1]).drop(1, { signal: true }), /ERR_INVALID_ARG_TYPE/); | ||
| 99 | + | ||
| 100 | + throws(() => Readable.from([1]).take(1, 1), /ERR_INVALID_ARG_TYPE/); | ||
| 101 | + throws(() => Readable.from([1]).take(1, { signal: true }), /ERR_INVALID_ARG_TYPE/); | ||
| 96 | 102 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -114,6 +114,7 @@ function oneTo5() { | |||
| 114 | 114 | concurrency: 'Foo' | |
| 115 | 115 | }), /ERR_OUT_OF_RANGE/); | |
| 116 | 116 | assert.throws(() => Readable.from([1]).flatMap((x) => x, 1), /ERR_INVALID_ARG_TYPE/); | |
| 117 | + assert.throws(() => Readable.from([1]).flatMap((x) => x, { signal: true }), /ERR_INVALID_ARG_TYPE/); | ||
| 117 | 118 | } | |
| 118 | 119 | { | |
| 119 | 120 | // Test result is a Readable | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -180,6 +180,7 @@ const { setTimeout } = require('timers/promises'); | |||
| 180 | 180 | concurrency: 'Foo' | |
| 181 | 181 | }), /ERR_OUT_OF_RANGE/); | |
| 182 | 182 | assert.throws(() => Readable.from([1]).map((x) => x, 1), /ERR_INVALID_ARG_TYPE/); | |
| 183 | + assert.throws(() => Readable.from([1]).map((x) => x, { signal: true }), /ERR_INVALID_ARG_TYPE/); | ||
| 183 | 184 | } | |
| 184 | 185 | { | |
| 185 | 186 | // Test result is a Readable | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -121,6 +121,8 @@ function sum(p, c) { | |||
| 121 | 121 | // Error cases | |
| 122 | 122 | assert.rejects(() => Readable.from([]).reduce(1), /TypeError/); | |
| 123 | 123 | assert.rejects(() => Readable.from([]).reduce('5'), /TypeError/); | |
| 124 | + assert.rejects(() => Readable.from([]).reduce((x, y) => x + y, 0, 1), /ERR_INVALID_ARG_TYPE/); | ||
| 125 | + assert.rejects(() => Readable.from([]).reduce((x, y) => x + y, 0, { signal: true }), /ERR_INVALID_ARG_TYPE/); | ||
| 124 | 126 | } | |
| 125 | 127 | ||
| 126 | 128 | { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -87,6 +87,17 @@ function oneTo5Async() { | |||
| 87 | 87 | assert.rejects(async () => { | |
| 88 | 88 | await Readable.from([1]).every(1); | |
| 89 | 89 | }, /ERR_INVALID_ARG_TYPE/).then(common.mustCall()); | |
| 90 | + | ||
| 91 | + assert.rejects(async () => { | ||
| 92 | + await Readable.from([1]).every((x) => x, 1); | ||
| 93 | + }, /ERR_INVALID_ARG_TYPE/).then(common.mustCall()); | ||
| 94 | + | ||
| 95 | + assert.rejects(async () => { | ||
| 96 | + await Readable.from([1]).every((x) => x, { | ||
| 97 | + signal: true | ||
| 98 | + }); | ||
| 99 | + }, /ERR_INVALID_ARG_TYPE/).then(common.mustCall()); | ||
| 100 | + | ||
| 90 | 101 | assert.rejects(async () => { | |
| 91 | 102 | await Readable.from([1]).every((x) => x, { | |
| 92 | 103 | concurrency: 'Foo' | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -79,3 +79,15 @@ const assert = require('assert'); | |||
| 79 | 79 | const result = Readable.from([1, 2, 3, 4, 5]).toArray(); | |
| 80 | 80 | assert.strictEqual(result instanceof Promise, true); | |
| 81 | 81 | } | |
| 82 | + { | ||
| 83 | + // Error cases | ||
| 84 | + assert.rejects(async () => { | ||
| 85 | + await Readable.from([1]).toArray(1); | ||
| 86 | + }, /ERR_INVALID_ARG_TYPE/).then(common.mustCall()); | ||
| 87 | + | ||
| 88 | + assert.rejects(async () => { | ||
| 89 | + await Readable.from([1]).toArray({ | ||
| 90 | + signal: true | ||
| 91 | + }); | ||
| 92 | + }, /ERR_INVALID_ARG_TYPE/).then(common.mustCall()); | ||
| 93 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments