11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst { AbortController, AbortSignal } = require('internal/abort_controller'); 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ciconst { 61cb0ef41Sopenharmony_ci codes: { 71cb0ef41Sopenharmony_ci ERR_INVALID_ARG_VALUE, 81cb0ef41Sopenharmony_ci ERR_INVALID_ARG_TYPE, 91cb0ef41Sopenharmony_ci ERR_MISSING_ARGS, 101cb0ef41Sopenharmony_ci ERR_OUT_OF_RANGE, 111cb0ef41Sopenharmony_ci }, 121cb0ef41Sopenharmony_ci AbortError, 131cb0ef41Sopenharmony_ci} = require('internal/errors'); 141cb0ef41Sopenharmony_ciconst { 151cb0ef41Sopenharmony_ci validateAbortSignal, 161cb0ef41Sopenharmony_ci validateInteger, 171cb0ef41Sopenharmony_ci validateObject, 181cb0ef41Sopenharmony_ci} = require('internal/validators'); 191cb0ef41Sopenharmony_ciconst { kWeakHandler, kResistStopPropagation } = require('internal/event_target'); 201cb0ef41Sopenharmony_ciconst { finished } = require('internal/streams/end-of-stream'); 211cb0ef41Sopenharmony_ciconst staticCompose = require('internal/streams/compose'); 221cb0ef41Sopenharmony_ciconst { 231cb0ef41Sopenharmony_ci addAbortSignalNoValidate, 241cb0ef41Sopenharmony_ci} = require('internal/streams/add-abort-signal'); 251cb0ef41Sopenharmony_ciconst { isWritable, isNodeStream } = require('internal/streams/utils'); 261cb0ef41Sopenharmony_ciconst { deprecate } = require('internal/util'); 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ciconst { 291cb0ef41Sopenharmony_ci ArrayPrototypePush, 301cb0ef41Sopenharmony_ci Boolean, 311cb0ef41Sopenharmony_ci MathFloor, 321cb0ef41Sopenharmony_ci Number, 331cb0ef41Sopenharmony_ci NumberIsNaN, 341cb0ef41Sopenharmony_ci Promise, 351cb0ef41Sopenharmony_ci PromiseReject, 361cb0ef41Sopenharmony_ci PromiseResolve, 371cb0ef41Sopenharmony_ci PromisePrototypeThen, 381cb0ef41Sopenharmony_ci Symbol, 391cb0ef41Sopenharmony_ci} = primordials; 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ciconst kEmpty = Symbol('kEmpty'); 421cb0ef41Sopenharmony_ciconst kEof = Symbol('kEof'); 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_cifunction compose(stream, options) { 451cb0ef41Sopenharmony_ci if (options != null) { 461cb0ef41Sopenharmony_ci validateObject(options, 'options'); 471cb0ef41Sopenharmony_ci } 481cb0ef41Sopenharmony_ci if (options?.signal != null) { 491cb0ef41Sopenharmony_ci validateAbortSignal(options.signal, 'options.signal'); 501cb0ef41Sopenharmony_ci } 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ci if (isNodeStream(stream) && !isWritable(stream)) { 531cb0ef41Sopenharmony_ci throw new ERR_INVALID_ARG_VALUE('stream', stream, 'must be writable'); 541cb0ef41Sopenharmony_ci } 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci const composedStream = staticCompose(this, stream); 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci if (options?.signal) { 591cb0ef41Sopenharmony_ci // Not validating as we already validated before 601cb0ef41Sopenharmony_ci addAbortSignalNoValidate( 611cb0ef41Sopenharmony_ci options.signal, 621cb0ef41Sopenharmony_ci composedStream, 631cb0ef41Sopenharmony_ci ); 641cb0ef41Sopenharmony_ci } 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci return composedStream; 671cb0ef41Sopenharmony_ci} 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_cifunction map(fn, options) { 701cb0ef41Sopenharmony_ci if (typeof fn !== 'function') { 711cb0ef41Sopenharmony_ci throw new ERR_INVALID_ARG_TYPE( 721cb0ef41Sopenharmony_ci 'fn', ['Function', 'AsyncFunction'], fn); 731cb0ef41Sopenharmony_ci } 741cb0ef41Sopenharmony_ci if (options != null) { 751cb0ef41Sopenharmony_ci validateObject(options, 'options'); 761cb0ef41Sopenharmony_ci } 771cb0ef41Sopenharmony_ci if (options?.signal != null) { 781cb0ef41Sopenharmony_ci validateAbortSignal(options.signal, 'options.signal'); 791cb0ef41Sopenharmony_ci } 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ci let concurrency = 1; 821cb0ef41Sopenharmony_ci if (options?.concurrency != null) { 831cb0ef41Sopenharmony_ci concurrency = MathFloor(options.concurrency); 841cb0ef41Sopenharmony_ci } 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ci let highWaterMark = concurrency - 1; 871cb0ef41Sopenharmony_ci if (options?.highWaterMark != null) { 881cb0ef41Sopenharmony_ci highWaterMark = MathFloor(options.highWaterMark); 891cb0ef41Sopenharmony_ci } 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci validateInteger(concurrency, 'options.concurrency', 1); 921cb0ef41Sopenharmony_ci validateInteger(highWaterMark, 'options.highWaterMark', 0); 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ci highWaterMark += concurrency; 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci return async function* map() { 971cb0ef41Sopenharmony_ci const signal = AbortSignal.any([options?.signal].filter(Boolean)); 981cb0ef41Sopenharmony_ci const stream = this; 991cb0ef41Sopenharmony_ci const queue = []; 1001cb0ef41Sopenharmony_ci const signalOpt = { signal }; 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci let next; 1031cb0ef41Sopenharmony_ci let resume; 1041cb0ef41Sopenharmony_ci let done = false; 1051cb0ef41Sopenharmony_ci let cnt = 0; 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ci function onCatch() { 1081cb0ef41Sopenharmony_ci done = true; 1091cb0ef41Sopenharmony_ci afterItemProcessed(); 1101cb0ef41Sopenharmony_ci } 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_ci function afterItemProcessed() { 1131cb0ef41Sopenharmony_ci cnt -= 1; 1141cb0ef41Sopenharmony_ci maybeResume(); 1151cb0ef41Sopenharmony_ci } 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_ci function maybeResume() { 1181cb0ef41Sopenharmony_ci if ( 1191cb0ef41Sopenharmony_ci resume && 1201cb0ef41Sopenharmony_ci !done && 1211cb0ef41Sopenharmony_ci cnt < concurrency && 1221cb0ef41Sopenharmony_ci queue.length < highWaterMark 1231cb0ef41Sopenharmony_ci ) { 1241cb0ef41Sopenharmony_ci resume(); 1251cb0ef41Sopenharmony_ci resume = null; 1261cb0ef41Sopenharmony_ci } 1271cb0ef41Sopenharmony_ci } 1281cb0ef41Sopenharmony_ci 1291cb0ef41Sopenharmony_ci async function pump() { 1301cb0ef41Sopenharmony_ci try { 1311cb0ef41Sopenharmony_ci for await (let val of stream) { 1321cb0ef41Sopenharmony_ci if (done) { 1331cb0ef41Sopenharmony_ci return; 1341cb0ef41Sopenharmony_ci } 1351cb0ef41Sopenharmony_ci 1361cb0ef41Sopenharmony_ci if (signal.aborted) { 1371cb0ef41Sopenharmony_ci throw new AbortError(); 1381cb0ef41Sopenharmony_ci } 1391cb0ef41Sopenharmony_ci 1401cb0ef41Sopenharmony_ci try { 1411cb0ef41Sopenharmony_ci val = fn(val, signalOpt); 1421cb0ef41Sopenharmony_ci 1431cb0ef41Sopenharmony_ci if (val === kEmpty) { 1441cb0ef41Sopenharmony_ci continue; 1451cb0ef41Sopenharmony_ci } 1461cb0ef41Sopenharmony_ci 1471cb0ef41Sopenharmony_ci val = PromiseResolve(val); 1481cb0ef41Sopenharmony_ci } catch (err) { 1491cb0ef41Sopenharmony_ci val = PromiseReject(err); 1501cb0ef41Sopenharmony_ci } 1511cb0ef41Sopenharmony_ci 1521cb0ef41Sopenharmony_ci cnt += 1; 1531cb0ef41Sopenharmony_ci 1541cb0ef41Sopenharmony_ci PromisePrototypeThen(val, afterItemProcessed, onCatch); 1551cb0ef41Sopenharmony_ci 1561cb0ef41Sopenharmony_ci queue.push(val); 1571cb0ef41Sopenharmony_ci if (next) { 1581cb0ef41Sopenharmony_ci next(); 1591cb0ef41Sopenharmony_ci next = null; 1601cb0ef41Sopenharmony_ci } 1611cb0ef41Sopenharmony_ci 1621cb0ef41Sopenharmony_ci if (!done && (queue.length >= highWaterMark || cnt >= concurrency)) { 1631cb0ef41Sopenharmony_ci await new Promise((resolve) => { 1641cb0ef41Sopenharmony_ci resume = resolve; 1651cb0ef41Sopenharmony_ci }); 1661cb0ef41Sopenharmony_ci } 1671cb0ef41Sopenharmony_ci } 1681cb0ef41Sopenharmony_ci queue.push(kEof); 1691cb0ef41Sopenharmony_ci } catch (err) { 1701cb0ef41Sopenharmony_ci const val = PromiseReject(err); 1711cb0ef41Sopenharmony_ci PromisePrototypeThen(val, afterItemProcessed, onCatch); 1721cb0ef41Sopenharmony_ci queue.push(val); 1731cb0ef41Sopenharmony_ci } finally { 1741cb0ef41Sopenharmony_ci done = true; 1751cb0ef41Sopenharmony_ci if (next) { 1761cb0ef41Sopenharmony_ci next(); 1771cb0ef41Sopenharmony_ci next = null; 1781cb0ef41Sopenharmony_ci } 1791cb0ef41Sopenharmony_ci } 1801cb0ef41Sopenharmony_ci } 1811cb0ef41Sopenharmony_ci 1821cb0ef41Sopenharmony_ci pump(); 1831cb0ef41Sopenharmony_ci 1841cb0ef41Sopenharmony_ci try { 1851cb0ef41Sopenharmony_ci while (true) { 1861cb0ef41Sopenharmony_ci while (queue.length > 0) { 1871cb0ef41Sopenharmony_ci const val = await queue[0]; 1881cb0ef41Sopenharmony_ci 1891cb0ef41Sopenharmony_ci if (val === kEof) { 1901cb0ef41Sopenharmony_ci return; 1911cb0ef41Sopenharmony_ci } 1921cb0ef41Sopenharmony_ci 1931cb0ef41Sopenharmony_ci if (signal.aborted) { 1941cb0ef41Sopenharmony_ci throw new AbortError(); 1951cb0ef41Sopenharmony_ci } 1961cb0ef41Sopenharmony_ci 1971cb0ef41Sopenharmony_ci if (val !== kEmpty) { 1981cb0ef41Sopenharmony_ci yield val; 1991cb0ef41Sopenharmony_ci } 2001cb0ef41Sopenharmony_ci 2011cb0ef41Sopenharmony_ci queue.shift(); 2021cb0ef41Sopenharmony_ci maybeResume(); 2031cb0ef41Sopenharmony_ci } 2041cb0ef41Sopenharmony_ci 2051cb0ef41Sopenharmony_ci await new Promise((resolve) => { 2061cb0ef41Sopenharmony_ci next = resolve; 2071cb0ef41Sopenharmony_ci }); 2081cb0ef41Sopenharmony_ci } 2091cb0ef41Sopenharmony_ci } finally { 2101cb0ef41Sopenharmony_ci done = true; 2111cb0ef41Sopenharmony_ci if (resume) { 2121cb0ef41Sopenharmony_ci resume(); 2131cb0ef41Sopenharmony_ci resume = null; 2141cb0ef41Sopenharmony_ci } 2151cb0ef41Sopenharmony_ci } 2161cb0ef41Sopenharmony_ci }.call(this); 2171cb0ef41Sopenharmony_ci} 2181cb0ef41Sopenharmony_ci 2191cb0ef41Sopenharmony_cifunction asIndexedPairs(options = undefined) { 2201cb0ef41Sopenharmony_ci if (options != null) { 2211cb0ef41Sopenharmony_ci validateObject(options, 'options'); 2221cb0ef41Sopenharmony_ci } 2231cb0ef41Sopenharmony_ci if (options?.signal != null) { 2241cb0ef41Sopenharmony_ci validateAbortSignal(options.signal, 'options.signal'); 2251cb0ef41Sopenharmony_ci } 2261cb0ef41Sopenharmony_ci 2271cb0ef41Sopenharmony_ci return async function* asIndexedPairs() { 2281cb0ef41Sopenharmony_ci let index = 0; 2291cb0ef41Sopenharmony_ci for await (const val of this) { 2301cb0ef41Sopenharmony_ci if (options?.signal?.aborted) { 2311cb0ef41Sopenharmony_ci throw new AbortError({ cause: options.signal.reason }); 2321cb0ef41Sopenharmony_ci } 2331cb0ef41Sopenharmony_ci yield [index++, val]; 2341cb0ef41Sopenharmony_ci } 2351cb0ef41Sopenharmony_ci }.call(this); 2361cb0ef41Sopenharmony_ci} 2371cb0ef41Sopenharmony_ci 2381cb0ef41Sopenharmony_ciasync function some(fn, options = undefined) { 2391cb0ef41Sopenharmony_ci for await (const unused of filter.call(this, fn, options)) { 2401cb0ef41Sopenharmony_ci return true; 2411cb0ef41Sopenharmony_ci } 2421cb0ef41Sopenharmony_ci return false; 2431cb0ef41Sopenharmony_ci} 2441cb0ef41Sopenharmony_ci 2451cb0ef41Sopenharmony_ciasync function every(fn, options = undefined) { 2461cb0ef41Sopenharmony_ci if (typeof fn !== 'function') { 2471cb0ef41Sopenharmony_ci throw new ERR_INVALID_ARG_TYPE( 2481cb0ef41Sopenharmony_ci 'fn', ['Function', 'AsyncFunction'], fn); 2491cb0ef41Sopenharmony_ci } 2501cb0ef41Sopenharmony_ci // https://en.wikipedia.org/wiki/De_Morgan%27s_laws 2511cb0ef41Sopenharmony_ci return !(await some.call(this, async (...args) => { 2521cb0ef41Sopenharmony_ci return !(await fn(...args)); 2531cb0ef41Sopenharmony_ci }, options)); 2541cb0ef41Sopenharmony_ci} 2551cb0ef41Sopenharmony_ci 2561cb0ef41Sopenharmony_ciasync function find(fn, options) { 2571cb0ef41Sopenharmony_ci for await (const result of filter.call(this, fn, options)) { 2581cb0ef41Sopenharmony_ci return result; 2591cb0ef41Sopenharmony_ci } 2601cb0ef41Sopenharmony_ci return undefined; 2611cb0ef41Sopenharmony_ci} 2621cb0ef41Sopenharmony_ci 2631cb0ef41Sopenharmony_ciasync function forEach(fn, options) { 2641cb0ef41Sopenharmony_ci if (typeof fn !== 'function') { 2651cb0ef41Sopenharmony_ci throw new ERR_INVALID_ARG_TYPE( 2661cb0ef41Sopenharmony_ci 'fn', ['Function', 'AsyncFunction'], fn); 2671cb0ef41Sopenharmony_ci } 2681cb0ef41Sopenharmony_ci async function forEachFn(value, options) { 2691cb0ef41Sopenharmony_ci await fn(value, options); 2701cb0ef41Sopenharmony_ci return kEmpty; 2711cb0ef41Sopenharmony_ci } 2721cb0ef41Sopenharmony_ci // eslint-disable-next-line no-unused-vars 2731cb0ef41Sopenharmony_ci for await (const unused of map.call(this, forEachFn, options)); 2741cb0ef41Sopenharmony_ci} 2751cb0ef41Sopenharmony_ci 2761cb0ef41Sopenharmony_cifunction filter(fn, options) { 2771cb0ef41Sopenharmony_ci if (typeof fn !== 'function') { 2781cb0ef41Sopenharmony_ci throw new ERR_INVALID_ARG_TYPE( 2791cb0ef41Sopenharmony_ci 'fn', ['Function', 'AsyncFunction'], fn); 2801cb0ef41Sopenharmony_ci } 2811cb0ef41Sopenharmony_ci async function filterFn(value, options) { 2821cb0ef41Sopenharmony_ci if (await fn(value, options)) { 2831cb0ef41Sopenharmony_ci return value; 2841cb0ef41Sopenharmony_ci } 2851cb0ef41Sopenharmony_ci return kEmpty; 2861cb0ef41Sopenharmony_ci } 2871cb0ef41Sopenharmony_ci return map.call(this, filterFn, options); 2881cb0ef41Sopenharmony_ci} 2891cb0ef41Sopenharmony_ci 2901cb0ef41Sopenharmony_ci// Specific to provide better error to reduce since the argument is only 2911cb0ef41Sopenharmony_ci// missing if the stream has no items in it - but the code is still appropriate 2921cb0ef41Sopenharmony_ciclass ReduceAwareErrMissingArgs extends ERR_MISSING_ARGS { 2931cb0ef41Sopenharmony_ci constructor() { 2941cb0ef41Sopenharmony_ci super('reduce'); 2951cb0ef41Sopenharmony_ci this.message = 'Reduce of an empty stream requires an initial value'; 2961cb0ef41Sopenharmony_ci } 2971cb0ef41Sopenharmony_ci} 2981cb0ef41Sopenharmony_ci 2991cb0ef41Sopenharmony_ciasync function reduce(reducer, initialValue, options) { 3001cb0ef41Sopenharmony_ci if (typeof reducer !== 'function') { 3011cb0ef41Sopenharmony_ci throw new ERR_INVALID_ARG_TYPE( 3021cb0ef41Sopenharmony_ci 'reducer', ['Function', 'AsyncFunction'], reducer); 3031cb0ef41Sopenharmony_ci } 3041cb0ef41Sopenharmony_ci if (options != null) { 3051cb0ef41Sopenharmony_ci validateObject(options, 'options'); 3061cb0ef41Sopenharmony_ci } 3071cb0ef41Sopenharmony_ci if (options?.signal != null) { 3081cb0ef41Sopenharmony_ci validateAbortSignal(options.signal, 'options.signal'); 3091cb0ef41Sopenharmony_ci } 3101cb0ef41Sopenharmony_ci 3111cb0ef41Sopenharmony_ci let hasInitialValue = arguments.length > 1; 3121cb0ef41Sopenharmony_ci if (options?.signal?.aborted) { 3131cb0ef41Sopenharmony_ci const err = new AbortError(undefined, { cause: options.signal.reason }); 3141cb0ef41Sopenharmony_ci this.once('error', () => {}); // The error is already propagated 3151cb0ef41Sopenharmony_ci await finished(this.destroy(err)); 3161cb0ef41Sopenharmony_ci throw err; 3171cb0ef41Sopenharmony_ci } 3181cb0ef41Sopenharmony_ci const ac = new AbortController(); 3191cb0ef41Sopenharmony_ci const signal = ac.signal; 3201cb0ef41Sopenharmony_ci if (options?.signal) { 3211cb0ef41Sopenharmony_ci const opts = { once: true, [kWeakHandler]: this, [kResistStopPropagation]: true }; 3221cb0ef41Sopenharmony_ci options.signal.addEventListener('abort', () => ac.abort(), opts); 3231cb0ef41Sopenharmony_ci } 3241cb0ef41Sopenharmony_ci let gotAnyItemFromStream = false; 3251cb0ef41Sopenharmony_ci try { 3261cb0ef41Sopenharmony_ci for await (const value of this) { 3271cb0ef41Sopenharmony_ci gotAnyItemFromStream = true; 3281cb0ef41Sopenharmony_ci if (options?.signal?.aborted) { 3291cb0ef41Sopenharmony_ci throw new AbortError(); 3301cb0ef41Sopenharmony_ci } 3311cb0ef41Sopenharmony_ci if (!hasInitialValue) { 3321cb0ef41Sopenharmony_ci initialValue = value; 3331cb0ef41Sopenharmony_ci hasInitialValue = true; 3341cb0ef41Sopenharmony_ci } else { 3351cb0ef41Sopenharmony_ci initialValue = await reducer(initialValue, value, { signal }); 3361cb0ef41Sopenharmony_ci } 3371cb0ef41Sopenharmony_ci } 3381cb0ef41Sopenharmony_ci if (!gotAnyItemFromStream && !hasInitialValue) { 3391cb0ef41Sopenharmony_ci throw new ReduceAwareErrMissingArgs(); 3401cb0ef41Sopenharmony_ci } 3411cb0ef41Sopenharmony_ci } finally { 3421cb0ef41Sopenharmony_ci ac.abort(); 3431cb0ef41Sopenharmony_ci } 3441cb0ef41Sopenharmony_ci return initialValue; 3451cb0ef41Sopenharmony_ci} 3461cb0ef41Sopenharmony_ci 3471cb0ef41Sopenharmony_ciasync function toArray(options) { 3481cb0ef41Sopenharmony_ci if (options != null) { 3491cb0ef41Sopenharmony_ci validateObject(options, 'options'); 3501cb0ef41Sopenharmony_ci } 3511cb0ef41Sopenharmony_ci if (options?.signal != null) { 3521cb0ef41Sopenharmony_ci validateAbortSignal(options.signal, 'options.signal'); 3531cb0ef41Sopenharmony_ci } 3541cb0ef41Sopenharmony_ci 3551cb0ef41Sopenharmony_ci const result = []; 3561cb0ef41Sopenharmony_ci for await (const val of this) { 3571cb0ef41Sopenharmony_ci if (options?.signal?.aborted) { 3581cb0ef41Sopenharmony_ci throw new AbortError(undefined, { cause: options.signal.reason }); 3591cb0ef41Sopenharmony_ci } 3601cb0ef41Sopenharmony_ci ArrayPrototypePush(result, val); 3611cb0ef41Sopenharmony_ci } 3621cb0ef41Sopenharmony_ci return result; 3631cb0ef41Sopenharmony_ci} 3641cb0ef41Sopenharmony_ci 3651cb0ef41Sopenharmony_cifunction flatMap(fn, options) { 3661cb0ef41Sopenharmony_ci const values = map.call(this, fn, options); 3671cb0ef41Sopenharmony_ci return async function* flatMap() { 3681cb0ef41Sopenharmony_ci for await (const val of values) { 3691cb0ef41Sopenharmony_ci yield* val; 3701cb0ef41Sopenharmony_ci } 3711cb0ef41Sopenharmony_ci }.call(this); 3721cb0ef41Sopenharmony_ci} 3731cb0ef41Sopenharmony_ci 3741cb0ef41Sopenharmony_cifunction toIntegerOrInfinity(number) { 3751cb0ef41Sopenharmony_ci // We coerce here to align with the spec 3761cb0ef41Sopenharmony_ci // https://github.com/tc39/proposal-iterator-helpers/issues/169 3771cb0ef41Sopenharmony_ci number = Number(number); 3781cb0ef41Sopenharmony_ci if (NumberIsNaN(number)) { 3791cb0ef41Sopenharmony_ci return 0; 3801cb0ef41Sopenharmony_ci } 3811cb0ef41Sopenharmony_ci if (number < 0) { 3821cb0ef41Sopenharmony_ci throw new ERR_OUT_OF_RANGE('number', '>= 0', number); 3831cb0ef41Sopenharmony_ci } 3841cb0ef41Sopenharmony_ci return number; 3851cb0ef41Sopenharmony_ci} 3861cb0ef41Sopenharmony_ci 3871cb0ef41Sopenharmony_cifunction drop(number, options = undefined) { 3881cb0ef41Sopenharmony_ci if (options != null) { 3891cb0ef41Sopenharmony_ci validateObject(options, 'options'); 3901cb0ef41Sopenharmony_ci } 3911cb0ef41Sopenharmony_ci if (options?.signal != null) { 3921cb0ef41Sopenharmony_ci validateAbortSignal(options.signal, 'options.signal'); 3931cb0ef41Sopenharmony_ci } 3941cb0ef41Sopenharmony_ci 3951cb0ef41Sopenharmony_ci number = toIntegerOrInfinity(number); 3961cb0ef41Sopenharmony_ci return async function* drop() { 3971cb0ef41Sopenharmony_ci if (options?.signal?.aborted) { 3981cb0ef41Sopenharmony_ci throw new AbortError(); 3991cb0ef41Sopenharmony_ci } 4001cb0ef41Sopenharmony_ci for await (const val of this) { 4011cb0ef41Sopenharmony_ci if (options?.signal?.aborted) { 4021cb0ef41Sopenharmony_ci throw new AbortError(); 4031cb0ef41Sopenharmony_ci } 4041cb0ef41Sopenharmony_ci if (number-- <= 0) { 4051cb0ef41Sopenharmony_ci yield val; 4061cb0ef41Sopenharmony_ci } 4071cb0ef41Sopenharmony_ci } 4081cb0ef41Sopenharmony_ci }.call(this); 4091cb0ef41Sopenharmony_ci} 4101cb0ef41Sopenharmony_ci 4111cb0ef41Sopenharmony_cifunction take(number, options = undefined) { 4121cb0ef41Sopenharmony_ci if (options != null) { 4131cb0ef41Sopenharmony_ci validateObject(options, 'options'); 4141cb0ef41Sopenharmony_ci } 4151cb0ef41Sopenharmony_ci if (options?.signal != null) { 4161cb0ef41Sopenharmony_ci validateAbortSignal(options.signal, 'options.signal'); 4171cb0ef41Sopenharmony_ci } 4181cb0ef41Sopenharmony_ci 4191cb0ef41Sopenharmony_ci number = toIntegerOrInfinity(number); 4201cb0ef41Sopenharmony_ci return async function* take() { 4211cb0ef41Sopenharmony_ci if (options?.signal?.aborted) { 4221cb0ef41Sopenharmony_ci throw new AbortError(); 4231cb0ef41Sopenharmony_ci } 4241cb0ef41Sopenharmony_ci for await (const val of this) { 4251cb0ef41Sopenharmony_ci if (options?.signal?.aborted) { 4261cb0ef41Sopenharmony_ci throw new AbortError(); 4271cb0ef41Sopenharmony_ci } 4281cb0ef41Sopenharmony_ci if (number-- > 0) { 4291cb0ef41Sopenharmony_ci yield val; 4301cb0ef41Sopenharmony_ci } 4311cb0ef41Sopenharmony_ci 4321cb0ef41Sopenharmony_ci // Don't get another item from iterator in case we reached the end 4331cb0ef41Sopenharmony_ci if (number <= 0) { 4341cb0ef41Sopenharmony_ci return; 4351cb0ef41Sopenharmony_ci } 4361cb0ef41Sopenharmony_ci } 4371cb0ef41Sopenharmony_ci }.call(this); 4381cb0ef41Sopenharmony_ci} 4391cb0ef41Sopenharmony_ci 4401cb0ef41Sopenharmony_cimodule.exports.streamReturningOperators = { 4411cb0ef41Sopenharmony_ci asIndexedPairs: deprecate(asIndexedPairs, 'readable.asIndexedPairs will be removed in a future version.'), 4421cb0ef41Sopenharmony_ci drop, 4431cb0ef41Sopenharmony_ci filter, 4441cb0ef41Sopenharmony_ci flatMap, 4451cb0ef41Sopenharmony_ci map, 4461cb0ef41Sopenharmony_ci take, 4471cb0ef41Sopenharmony_ci compose, 4481cb0ef41Sopenharmony_ci}; 4491cb0ef41Sopenharmony_ci 4501cb0ef41Sopenharmony_cimodule.exports.promiseReturningOperators = { 4511cb0ef41Sopenharmony_ci every, 4521cb0ef41Sopenharmony_ci forEach, 4531cb0ef41Sopenharmony_ci reduce, 4541cb0ef41Sopenharmony_ci toArray, 4551cb0ef41Sopenharmony_ci some, 4561cb0ef41Sopenharmony_ci find, 4571cb0ef41Sopenharmony_ci}; 458