@0xbow/privacy-pools-core-sdk 1.0.3 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/{fetchArtifacts.esm-aHubC6Nb.js → fetchArtifacts.esm-B6qveiM8.js} +2 -2
- package/dist/esm/{fetchArtifacts.esm-aHubC6Nb.js.map → fetchArtifacts.esm-B6qveiM8.js.map} +1 -1
- package/dist/esm/{fetchArtifacts.node-DUdhNM9Z.js → fetchArtifacts.node-BPQQPsnb.js} +2 -2
- package/dist/esm/{fetchArtifacts.node-DUdhNM9Z.js.map → fetchArtifacts.node-BPQQPsnb.js.map} +1 -1
- package/dist/esm/{index-BQLhaPQL.js → index-CRtEyHEf.js} +2880 -78
- package/dist/esm/index-CRtEyHEf.js.map +1 -0
- package/dist/esm/index.mjs +1 -1
- package/dist/index.d.mts +83 -8
- package/dist/node/{fetchArtifacts.esm-CRX34p8g.js → fetchArtifacts.esm-z-KXbilc.js} +2 -2
- package/dist/node/{fetchArtifacts.esm-CRX34p8g.js.map → fetchArtifacts.esm-z-KXbilc.js.map} +1 -1
- package/dist/node/{fetchArtifacts.node-CuPs4jZ1.js → fetchArtifacts.node-DvqhqpW9.js} +2 -2
- package/dist/node/{fetchArtifacts.node-CuPs4jZ1.js.map → fetchArtifacts.node-DvqhqpW9.js.map} +1 -1
- package/dist/node/{index-DB1uYJPF.js → index-BsmEKESv.js} +2880 -78
- package/dist/node/index-BsmEKESv.js.map +1 -0
- package/dist/node/index.mjs +1 -1
- package/dist/types/core/account.service.d.ts +4 -0
- package/dist/types/core/data.service.d.ts +28 -8
- package/dist/types/{fetchArtifacts.esm-CzBDJOwj.js → fetchArtifacts.esm-DF01Zpo3.js} +1 -1
- package/dist/types/{fetchArtifacts.node-CtMsXhR9.js → fetchArtifacts.node-BO6FBCAw.js} +1 -1
- package/dist/types/{index-BL6xN-P5.js → index-CH7gk4sK.js} +2879 -77
- package/dist/types/index.js +1 -1
- package/dist/types/types/index.d.ts +1 -0
- package/dist/types/types/rateLimit.d.ts +51 -0
- package/package.json +4 -1
- package/src/core/account.service.ts +46 -24
- package/src/core/contracts.service.ts +2 -6
- package/src/core/data.service.ts +324 -95
- package/src/providers/blockchainProvider.ts +1 -3
- package/src/types/index.ts +1 -0
- package/src/types/rateLimit.ts +66 -0
- package/dist/esm/index-BQLhaPQL.js.map +0 -1
- package/dist/node/index-DB1uYJPF.js.map +0 -1
- package/dist/types/core/test.d.ts +0 -1
|
@@ -4,6 +4,18 @@ import require$$2 from 'assert';
|
|
|
4
4
|
import { keccak256, encodeAbiParameters, numberToHex, createPublicClient, http, createWalletClient, getAddress, bytesToNumber as bytesToNumber$1, parseAbiItem } from 'viem';
|
|
5
5
|
import { mainnet } from 'viem/chains';
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Default log fetch configuration
|
|
9
|
+
*/
|
|
10
|
+
const DEFAULT_LOG_FETCH_CONFIG = {
|
|
11
|
+
blockChunkSize: 10000,
|
|
12
|
+
concurrency: 3,
|
|
13
|
+
chunkDelayMs: 0,
|
|
14
|
+
retryOnFailure: true,
|
|
15
|
+
maxRetries: 3,
|
|
16
|
+
retryBaseDelayMs: 1000,
|
|
17
|
+
};
|
|
18
|
+
|
|
7
19
|
const version = '2.22.14';
|
|
8
20
|
|
|
9
21
|
let errorConfig = {
|
|
@@ -45803,10 +45815,10 @@ const circuitToAsset = {
|
|
|
45803
45815
|
|
|
45804
45816
|
async function importFetchVersionedArtifact(isBrowser) {
|
|
45805
45817
|
if (isBrowser) {
|
|
45806
|
-
return import('./fetchArtifacts.esm-
|
|
45818
|
+
return import('./fetchArtifacts.esm-B6qveiM8.js');
|
|
45807
45819
|
}
|
|
45808
45820
|
else {
|
|
45809
|
-
return import('./fetchArtifacts.node-
|
|
45821
|
+
return import('./fetchArtifacts.node-BPQQPsnb.js');
|
|
45810
45822
|
}
|
|
45811
45823
|
}
|
|
45812
45824
|
|
|
@@ -69280,6 +69292,2675 @@ class AccountError extends SDKError {
|
|
|
69280
69292
|
}
|
|
69281
69293
|
}
|
|
69282
69294
|
|
|
69295
|
+
/**
|
|
69296
|
+
* Creates a continuation function with some arguments already applied.
|
|
69297
|
+
*
|
|
69298
|
+
* Useful as a shorthand when combined with other control flow functions. Any
|
|
69299
|
+
* arguments passed to the returned function are added to the arguments
|
|
69300
|
+
* originally passed to apply.
|
|
69301
|
+
*
|
|
69302
|
+
* @name apply
|
|
69303
|
+
* @static
|
|
69304
|
+
* @memberOf module:Utils
|
|
69305
|
+
* @method
|
|
69306
|
+
* @category Util
|
|
69307
|
+
* @param {Function} fn - The function you want to eventually apply all
|
|
69308
|
+
* arguments to. Invokes with (arguments...).
|
|
69309
|
+
* @param {...*} arguments... - Any number of arguments to automatically apply
|
|
69310
|
+
* when the continuation is called.
|
|
69311
|
+
* @returns {Function} the partially-applied function
|
|
69312
|
+
* @example
|
|
69313
|
+
*
|
|
69314
|
+
* // using apply
|
|
69315
|
+
* async.parallel([
|
|
69316
|
+
* async.apply(fs.writeFile, 'testfile1', 'test1'),
|
|
69317
|
+
* async.apply(fs.writeFile, 'testfile2', 'test2')
|
|
69318
|
+
* ]);
|
|
69319
|
+
*
|
|
69320
|
+
*
|
|
69321
|
+
* // the same process without using apply
|
|
69322
|
+
* async.parallel([
|
|
69323
|
+
* function(callback) {
|
|
69324
|
+
* fs.writeFile('testfile1', 'test1', callback);
|
|
69325
|
+
* },
|
|
69326
|
+
* function(callback) {
|
|
69327
|
+
* fs.writeFile('testfile2', 'test2', callback);
|
|
69328
|
+
* }
|
|
69329
|
+
* ]);
|
|
69330
|
+
*
|
|
69331
|
+
* // It's possible to pass any number of additional arguments when calling the
|
|
69332
|
+
* // continuation:
|
|
69333
|
+
*
|
|
69334
|
+
* node> var fn = async.apply(sys.puts, 'one');
|
|
69335
|
+
* node> fn('two', 'three');
|
|
69336
|
+
* one
|
|
69337
|
+
* two
|
|
69338
|
+
* three
|
|
69339
|
+
*/
|
|
69340
|
+
|
|
69341
|
+
function initialParams (fn) {
|
|
69342
|
+
return function (...args/*, callback*/) {
|
|
69343
|
+
var callback = args.pop();
|
|
69344
|
+
return fn.call(this, args, callback);
|
|
69345
|
+
};
|
|
69346
|
+
}
|
|
69347
|
+
|
|
69348
|
+
/* istanbul ignore file */
|
|
69349
|
+
|
|
69350
|
+
var hasQueueMicrotask = typeof queueMicrotask === 'function' && queueMicrotask;
|
|
69351
|
+
var hasSetImmediate = typeof setImmediate === 'function' && setImmediate;
|
|
69352
|
+
var hasNextTick = typeof process === 'object' && typeof process.nextTick === 'function';
|
|
69353
|
+
|
|
69354
|
+
function fallback(fn) {
|
|
69355
|
+
setTimeout(fn, 0);
|
|
69356
|
+
}
|
|
69357
|
+
|
|
69358
|
+
function wrap(defer) {
|
|
69359
|
+
return (fn, ...args) => defer(() => fn(...args));
|
|
69360
|
+
}
|
|
69361
|
+
|
|
69362
|
+
var _defer$1;
|
|
69363
|
+
|
|
69364
|
+
if (hasQueueMicrotask) {
|
|
69365
|
+
_defer$1 = queueMicrotask;
|
|
69366
|
+
} else if (hasSetImmediate) {
|
|
69367
|
+
_defer$1 = setImmediate;
|
|
69368
|
+
} else if (hasNextTick) {
|
|
69369
|
+
_defer$1 = process.nextTick;
|
|
69370
|
+
} else {
|
|
69371
|
+
_defer$1 = fallback;
|
|
69372
|
+
}
|
|
69373
|
+
|
|
69374
|
+
var setImmediate$1 = wrap(_defer$1);
|
|
69375
|
+
|
|
69376
|
+
/**
|
|
69377
|
+
* Take a sync function and make it async, passing its return value to a
|
|
69378
|
+
* callback. This is useful for plugging sync functions into a waterfall,
|
|
69379
|
+
* series, or other async functions. Any arguments passed to the generated
|
|
69380
|
+
* function will be passed to the wrapped function (except for the final
|
|
69381
|
+
* callback argument). Errors thrown will be passed to the callback.
|
|
69382
|
+
*
|
|
69383
|
+
* If the function passed to `asyncify` returns a Promise, that promises's
|
|
69384
|
+
* resolved/rejected state will be used to call the callback, rather than simply
|
|
69385
|
+
* the synchronous return value.
|
|
69386
|
+
*
|
|
69387
|
+
* This also means you can asyncify ES2017 `async` functions.
|
|
69388
|
+
*
|
|
69389
|
+
* @name asyncify
|
|
69390
|
+
* @static
|
|
69391
|
+
* @memberOf module:Utils
|
|
69392
|
+
* @method
|
|
69393
|
+
* @alias wrapSync
|
|
69394
|
+
* @category Util
|
|
69395
|
+
* @param {Function} func - The synchronous function, or Promise-returning
|
|
69396
|
+
* function to convert to an {@link AsyncFunction}.
|
|
69397
|
+
* @returns {AsyncFunction} An asynchronous wrapper of the `func`. To be
|
|
69398
|
+
* invoked with `(args..., callback)`.
|
|
69399
|
+
* @example
|
|
69400
|
+
*
|
|
69401
|
+
* // passing a regular synchronous function
|
|
69402
|
+
* async.waterfall([
|
|
69403
|
+
* async.apply(fs.readFile, filename, "utf8"),
|
|
69404
|
+
* async.asyncify(JSON.parse),
|
|
69405
|
+
* function (data, next) {
|
|
69406
|
+
* // data is the result of parsing the text.
|
|
69407
|
+
* // If there was a parsing error, it would have been caught.
|
|
69408
|
+
* }
|
|
69409
|
+
* ], callback);
|
|
69410
|
+
*
|
|
69411
|
+
* // passing a function returning a promise
|
|
69412
|
+
* async.waterfall([
|
|
69413
|
+
* async.apply(fs.readFile, filename, "utf8"),
|
|
69414
|
+
* async.asyncify(function (contents) {
|
|
69415
|
+
* return db.model.create(contents);
|
|
69416
|
+
* }),
|
|
69417
|
+
* function (model, next) {
|
|
69418
|
+
* // `model` is the instantiated model object.
|
|
69419
|
+
* // If there was an error, this function would be skipped.
|
|
69420
|
+
* }
|
|
69421
|
+
* ], callback);
|
|
69422
|
+
*
|
|
69423
|
+
* // es2017 example, though `asyncify` is not needed if your JS environment
|
|
69424
|
+
* // supports async functions out of the box
|
|
69425
|
+
* var q = async.queue(async.asyncify(async function(file) {
|
|
69426
|
+
* var intermediateStep = await processFile(file);
|
|
69427
|
+
* return await somePromise(intermediateStep)
|
|
69428
|
+
* }));
|
|
69429
|
+
*
|
|
69430
|
+
* q.push(files);
|
|
69431
|
+
*/
|
|
69432
|
+
function asyncify(func) {
|
|
69433
|
+
if (isAsync(func)) {
|
|
69434
|
+
return function (...args/*, callback*/) {
|
|
69435
|
+
const callback = args.pop();
|
|
69436
|
+
const promise = func.apply(this, args);
|
|
69437
|
+
return handlePromise(promise, callback)
|
|
69438
|
+
}
|
|
69439
|
+
}
|
|
69440
|
+
|
|
69441
|
+
return initialParams(function (args, callback) {
|
|
69442
|
+
var result;
|
|
69443
|
+
try {
|
|
69444
|
+
result = func.apply(this, args);
|
|
69445
|
+
} catch (e) {
|
|
69446
|
+
return callback(e);
|
|
69447
|
+
}
|
|
69448
|
+
// if result is Promise object
|
|
69449
|
+
if (result && typeof result.then === 'function') {
|
|
69450
|
+
return handlePromise(result, callback)
|
|
69451
|
+
} else {
|
|
69452
|
+
callback(null, result);
|
|
69453
|
+
}
|
|
69454
|
+
});
|
|
69455
|
+
}
|
|
69456
|
+
|
|
69457
|
+
function handlePromise(promise, callback) {
|
|
69458
|
+
return promise.then(value => {
|
|
69459
|
+
invokeCallback(callback, null, value);
|
|
69460
|
+
}, err => {
|
|
69461
|
+
invokeCallback(callback, err && (err instanceof Error || err.message) ? err : new Error(err));
|
|
69462
|
+
});
|
|
69463
|
+
}
|
|
69464
|
+
|
|
69465
|
+
function invokeCallback(callback, error, value) {
|
|
69466
|
+
try {
|
|
69467
|
+
callback(error, value);
|
|
69468
|
+
} catch (err) {
|
|
69469
|
+
setImmediate$1(e => { throw e }, err);
|
|
69470
|
+
}
|
|
69471
|
+
}
|
|
69472
|
+
|
|
69473
|
+
function isAsync(fn) {
|
|
69474
|
+
return fn[Symbol.toStringTag] === 'AsyncFunction';
|
|
69475
|
+
}
|
|
69476
|
+
|
|
69477
|
+
function isAsyncGenerator(fn) {
|
|
69478
|
+
return fn[Symbol.toStringTag] === 'AsyncGenerator';
|
|
69479
|
+
}
|
|
69480
|
+
|
|
69481
|
+
function isAsyncIterable(obj) {
|
|
69482
|
+
return typeof obj[Symbol.asyncIterator] === 'function';
|
|
69483
|
+
}
|
|
69484
|
+
|
|
69485
|
+
function wrapAsync(asyncFn) {
|
|
69486
|
+
if (typeof asyncFn !== 'function') throw new Error('expected a function')
|
|
69487
|
+
return isAsync(asyncFn) ? asyncify(asyncFn) : asyncFn;
|
|
69488
|
+
}
|
|
69489
|
+
|
|
69490
|
+
// conditionally promisify a function.
|
|
69491
|
+
// only return a promise if a callback is omitted
|
|
69492
|
+
function awaitify (asyncFn, arity) {
|
|
69493
|
+
if (!arity) arity = asyncFn.length;
|
|
69494
|
+
if (!arity) throw new Error('arity is undefined')
|
|
69495
|
+
function awaitable (...args) {
|
|
69496
|
+
if (typeof args[arity - 1] === 'function') {
|
|
69497
|
+
return asyncFn.apply(this, args)
|
|
69498
|
+
}
|
|
69499
|
+
|
|
69500
|
+
return new Promise((resolve, reject) => {
|
|
69501
|
+
args[arity - 1] = (err, ...cbArgs) => {
|
|
69502
|
+
if (err) return reject(err)
|
|
69503
|
+
resolve(cbArgs.length > 1 ? cbArgs : cbArgs[0]);
|
|
69504
|
+
};
|
|
69505
|
+
asyncFn.apply(this, args);
|
|
69506
|
+
})
|
|
69507
|
+
}
|
|
69508
|
+
|
|
69509
|
+
return awaitable
|
|
69510
|
+
}
|
|
69511
|
+
|
|
69512
|
+
function _asyncMap(eachfn, arr, iteratee, callback) {
|
|
69513
|
+
arr = arr || [];
|
|
69514
|
+
var results = [];
|
|
69515
|
+
var counter = 0;
|
|
69516
|
+
var _iteratee = wrapAsync(iteratee);
|
|
69517
|
+
|
|
69518
|
+
return eachfn(arr, (value, _, iterCb) => {
|
|
69519
|
+
var index = counter++;
|
|
69520
|
+
_iteratee(value, (err, v) => {
|
|
69521
|
+
results[index] = v;
|
|
69522
|
+
iterCb(err);
|
|
69523
|
+
});
|
|
69524
|
+
}, err => {
|
|
69525
|
+
callback(err, results);
|
|
69526
|
+
});
|
|
69527
|
+
}
|
|
69528
|
+
|
|
69529
|
+
function isArrayLike(value) {
|
|
69530
|
+
return value &&
|
|
69531
|
+
typeof value.length === 'number' &&
|
|
69532
|
+
value.length >= 0 &&
|
|
69533
|
+
value.length % 1 === 0;
|
|
69534
|
+
}
|
|
69535
|
+
|
|
69536
|
+
// A temporary value used to identify if the loop should be broken.
|
|
69537
|
+
// See #1064, #1293
|
|
69538
|
+
const breakLoop = {};
|
|
69539
|
+
|
|
69540
|
+
function once(fn) {
|
|
69541
|
+
function wrapper (...args) {
|
|
69542
|
+
if (fn === null) return;
|
|
69543
|
+
var callFn = fn;
|
|
69544
|
+
fn = null;
|
|
69545
|
+
callFn.apply(this, args);
|
|
69546
|
+
}
|
|
69547
|
+
Object.assign(wrapper, fn);
|
|
69548
|
+
return wrapper
|
|
69549
|
+
}
|
|
69550
|
+
|
|
69551
|
+
function getIterator (coll) {
|
|
69552
|
+
return coll[Symbol.iterator] && coll[Symbol.iterator]();
|
|
69553
|
+
}
|
|
69554
|
+
|
|
69555
|
+
function createArrayIterator(coll) {
|
|
69556
|
+
var i = -1;
|
|
69557
|
+
var len = coll.length;
|
|
69558
|
+
return function next() {
|
|
69559
|
+
return ++i < len ? {value: coll[i], key: i} : null;
|
|
69560
|
+
}
|
|
69561
|
+
}
|
|
69562
|
+
|
|
69563
|
+
function createES2015Iterator(iterator) {
|
|
69564
|
+
var i = -1;
|
|
69565
|
+
return function next() {
|
|
69566
|
+
var item = iterator.next();
|
|
69567
|
+
if (item.done)
|
|
69568
|
+
return null;
|
|
69569
|
+
i++;
|
|
69570
|
+
return {value: item.value, key: i};
|
|
69571
|
+
}
|
|
69572
|
+
}
|
|
69573
|
+
|
|
69574
|
+
function createObjectIterator(obj) {
|
|
69575
|
+
var okeys = obj ? Object.keys(obj) : [];
|
|
69576
|
+
var i = -1;
|
|
69577
|
+
var len = okeys.length;
|
|
69578
|
+
return function next() {
|
|
69579
|
+
var key = okeys[++i];
|
|
69580
|
+
if (key === '__proto__') {
|
|
69581
|
+
return next();
|
|
69582
|
+
}
|
|
69583
|
+
return i < len ? {value: obj[key], key} : null;
|
|
69584
|
+
};
|
|
69585
|
+
}
|
|
69586
|
+
|
|
69587
|
+
function createIterator(coll) {
|
|
69588
|
+
if (isArrayLike(coll)) {
|
|
69589
|
+
return createArrayIterator(coll);
|
|
69590
|
+
}
|
|
69591
|
+
|
|
69592
|
+
var iterator = getIterator(coll);
|
|
69593
|
+
return iterator ? createES2015Iterator(iterator) : createObjectIterator(coll);
|
|
69594
|
+
}
|
|
69595
|
+
|
|
69596
|
+
function onlyOnce(fn) {
|
|
69597
|
+
return function (...args) {
|
|
69598
|
+
if (fn === null) throw new Error("Callback was already called.");
|
|
69599
|
+
var callFn = fn;
|
|
69600
|
+
fn = null;
|
|
69601
|
+
callFn.apply(this, args);
|
|
69602
|
+
};
|
|
69603
|
+
}
|
|
69604
|
+
|
|
69605
|
+
// for async generators
|
|
69606
|
+
function asyncEachOfLimit(generator, limit, iteratee, callback) {
|
|
69607
|
+
let done = false;
|
|
69608
|
+
let canceled = false;
|
|
69609
|
+
let awaiting = false;
|
|
69610
|
+
let running = 0;
|
|
69611
|
+
let idx = 0;
|
|
69612
|
+
|
|
69613
|
+
function replenish() {
|
|
69614
|
+
//console.log('replenish')
|
|
69615
|
+
if (running >= limit || awaiting || done) return
|
|
69616
|
+
//console.log('replenish awaiting')
|
|
69617
|
+
awaiting = true;
|
|
69618
|
+
generator.next().then(({value, done: iterDone}) => {
|
|
69619
|
+
//console.log('got value', value)
|
|
69620
|
+
if (canceled || done) return
|
|
69621
|
+
awaiting = false;
|
|
69622
|
+
if (iterDone) {
|
|
69623
|
+
done = true;
|
|
69624
|
+
if (running <= 0) {
|
|
69625
|
+
//console.log('done nextCb')
|
|
69626
|
+
callback(null);
|
|
69627
|
+
}
|
|
69628
|
+
return;
|
|
69629
|
+
}
|
|
69630
|
+
running++;
|
|
69631
|
+
iteratee(value, idx, iterateeCallback);
|
|
69632
|
+
idx++;
|
|
69633
|
+
replenish();
|
|
69634
|
+
}).catch(handleError);
|
|
69635
|
+
}
|
|
69636
|
+
|
|
69637
|
+
function iterateeCallback(err, result) {
|
|
69638
|
+
//console.log('iterateeCallback')
|
|
69639
|
+
running -= 1;
|
|
69640
|
+
if (canceled) return
|
|
69641
|
+
if (err) return handleError(err)
|
|
69642
|
+
|
|
69643
|
+
if (err === false) {
|
|
69644
|
+
done = true;
|
|
69645
|
+
canceled = true;
|
|
69646
|
+
return
|
|
69647
|
+
}
|
|
69648
|
+
|
|
69649
|
+
if (result === breakLoop || (done && running <= 0)) {
|
|
69650
|
+
done = true;
|
|
69651
|
+
//console.log('done iterCb')
|
|
69652
|
+
return callback(null);
|
|
69653
|
+
}
|
|
69654
|
+
replenish();
|
|
69655
|
+
}
|
|
69656
|
+
|
|
69657
|
+
function handleError(err) {
|
|
69658
|
+
if (canceled) return
|
|
69659
|
+
awaiting = false;
|
|
69660
|
+
done = true;
|
|
69661
|
+
callback(err);
|
|
69662
|
+
}
|
|
69663
|
+
|
|
69664
|
+
replenish();
|
|
69665
|
+
}
|
|
69666
|
+
|
|
69667
|
+
var eachOfLimit$2 = (limit) => {
|
|
69668
|
+
return (obj, iteratee, callback) => {
|
|
69669
|
+
callback = once(callback);
|
|
69670
|
+
if (limit <= 0) {
|
|
69671
|
+
throw new RangeError('concurrency limit cannot be less than 1')
|
|
69672
|
+
}
|
|
69673
|
+
if (!obj) {
|
|
69674
|
+
return callback(null);
|
|
69675
|
+
}
|
|
69676
|
+
if (isAsyncGenerator(obj)) {
|
|
69677
|
+
return asyncEachOfLimit(obj, limit, iteratee, callback)
|
|
69678
|
+
}
|
|
69679
|
+
if (isAsyncIterable(obj)) {
|
|
69680
|
+
return asyncEachOfLimit(obj[Symbol.asyncIterator](), limit, iteratee, callback)
|
|
69681
|
+
}
|
|
69682
|
+
var nextElem = createIterator(obj);
|
|
69683
|
+
var done = false;
|
|
69684
|
+
var canceled = false;
|
|
69685
|
+
var running = 0;
|
|
69686
|
+
var looping = false;
|
|
69687
|
+
|
|
69688
|
+
function iterateeCallback(err, value) {
|
|
69689
|
+
if (canceled) return
|
|
69690
|
+
running -= 1;
|
|
69691
|
+
if (err) {
|
|
69692
|
+
done = true;
|
|
69693
|
+
callback(err);
|
|
69694
|
+
}
|
|
69695
|
+
else if (err === false) {
|
|
69696
|
+
done = true;
|
|
69697
|
+
canceled = true;
|
|
69698
|
+
}
|
|
69699
|
+
else if (value === breakLoop || (done && running <= 0)) {
|
|
69700
|
+
done = true;
|
|
69701
|
+
return callback(null);
|
|
69702
|
+
}
|
|
69703
|
+
else if (!looping) {
|
|
69704
|
+
replenish();
|
|
69705
|
+
}
|
|
69706
|
+
}
|
|
69707
|
+
|
|
69708
|
+
function replenish () {
|
|
69709
|
+
looping = true;
|
|
69710
|
+
while (running < limit && !done) {
|
|
69711
|
+
var elem = nextElem();
|
|
69712
|
+
if (elem === null) {
|
|
69713
|
+
done = true;
|
|
69714
|
+
if (running <= 0) {
|
|
69715
|
+
callback(null);
|
|
69716
|
+
}
|
|
69717
|
+
return;
|
|
69718
|
+
}
|
|
69719
|
+
running += 1;
|
|
69720
|
+
iteratee(elem.value, elem.key, onlyOnce(iterateeCallback));
|
|
69721
|
+
}
|
|
69722
|
+
looping = false;
|
|
69723
|
+
}
|
|
69724
|
+
|
|
69725
|
+
replenish();
|
|
69726
|
+
};
|
|
69727
|
+
};
|
|
69728
|
+
|
|
69729
|
+
/**
|
|
69730
|
+
* The same as [`eachOf`]{@link module:Collections.eachOf} but runs a maximum of `limit` async operations at a
|
|
69731
|
+
* time.
|
|
69732
|
+
*
|
|
69733
|
+
* @name eachOfLimit
|
|
69734
|
+
* @static
|
|
69735
|
+
* @memberOf module:Collections
|
|
69736
|
+
* @method
|
|
69737
|
+
* @see [async.eachOf]{@link module:Collections.eachOf}
|
|
69738
|
+
* @alias forEachOfLimit
|
|
69739
|
+
* @category Collection
|
|
69740
|
+
* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|
69741
|
+
* @param {number} limit - The maximum number of async operations at a time.
|
|
69742
|
+
* @param {AsyncFunction} iteratee - An async function to apply to each
|
|
69743
|
+
* item in `coll`. The `key` is the item's key, or index in the case of an
|
|
69744
|
+
* array.
|
|
69745
|
+
* Invoked with (item, key, callback).
|
|
69746
|
+
* @param {Function} [callback] - A callback which is called when all
|
|
69747
|
+
* `iteratee` functions have finished, or an error occurs. Invoked with (err).
|
|
69748
|
+
* @returns {Promise} a promise, if a callback is omitted
|
|
69749
|
+
*/
|
|
69750
|
+
function eachOfLimit(coll, limit, iteratee, callback) {
|
|
69751
|
+
return eachOfLimit$2(limit)(coll, wrapAsync(iteratee), callback);
|
|
69752
|
+
}
|
|
69753
|
+
|
|
69754
|
+
var eachOfLimit$1 = awaitify(eachOfLimit, 4);
|
|
69755
|
+
|
|
69756
|
+
// eachOf implementation optimized for array-likes
|
|
69757
|
+
function eachOfArrayLike(coll, iteratee, callback) {
|
|
69758
|
+
callback = once(callback);
|
|
69759
|
+
var index = 0,
|
|
69760
|
+
completed = 0,
|
|
69761
|
+
{length} = coll,
|
|
69762
|
+
canceled = false;
|
|
69763
|
+
if (length === 0) {
|
|
69764
|
+
callback(null);
|
|
69765
|
+
}
|
|
69766
|
+
|
|
69767
|
+
function iteratorCallback(err, value) {
|
|
69768
|
+
if (err === false) {
|
|
69769
|
+
canceled = true;
|
|
69770
|
+
}
|
|
69771
|
+
if (canceled === true) return
|
|
69772
|
+
if (err) {
|
|
69773
|
+
callback(err);
|
|
69774
|
+
} else if ((++completed === length) || value === breakLoop) {
|
|
69775
|
+
callback(null);
|
|
69776
|
+
}
|
|
69777
|
+
}
|
|
69778
|
+
|
|
69779
|
+
for (; index < length; index++) {
|
|
69780
|
+
iteratee(coll[index], index, onlyOnce(iteratorCallback));
|
|
69781
|
+
}
|
|
69782
|
+
}
|
|
69783
|
+
|
|
69784
|
+
// a generic version of eachOf which can handle array, object, and iterator cases.
|
|
69785
|
+
function eachOfGeneric (coll, iteratee, callback) {
|
|
69786
|
+
return eachOfLimit$1(coll, Infinity, iteratee, callback);
|
|
69787
|
+
}
|
|
69788
|
+
|
|
69789
|
+
/**
|
|
69790
|
+
* Like [`each`]{@link module:Collections.each}, except that it passes the key (or index) as the second argument
|
|
69791
|
+
* to the iteratee.
|
|
69792
|
+
*
|
|
69793
|
+
* @name eachOf
|
|
69794
|
+
* @static
|
|
69795
|
+
* @memberOf module:Collections
|
|
69796
|
+
* @method
|
|
69797
|
+
* @alias forEachOf
|
|
69798
|
+
* @category Collection
|
|
69799
|
+
* @see [async.each]{@link module:Collections.each}
|
|
69800
|
+
* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|
69801
|
+
* @param {AsyncFunction} iteratee - A function to apply to each
|
|
69802
|
+
* item in `coll`.
|
|
69803
|
+
* The `key` is the item's key, or index in the case of an array.
|
|
69804
|
+
* Invoked with (item, key, callback).
|
|
69805
|
+
* @param {Function} [callback] - A callback which is called when all
|
|
69806
|
+
* `iteratee` functions have finished, or an error occurs. Invoked with (err).
|
|
69807
|
+
* @returns {Promise} a promise, if a callback is omitted
|
|
69808
|
+
* @example
|
|
69809
|
+
*
|
|
69810
|
+
* // dev.json is a file containing a valid json object config for dev environment
|
|
69811
|
+
* // dev.json is a file containing a valid json object config for test environment
|
|
69812
|
+
* // prod.json is a file containing a valid json object config for prod environment
|
|
69813
|
+
* // invalid.json is a file with a malformed json object
|
|
69814
|
+
*
|
|
69815
|
+
* let configs = {}; //global variable
|
|
69816
|
+
* let validConfigFileMap = {dev: 'dev.json', test: 'test.json', prod: 'prod.json'};
|
|
69817
|
+
* let invalidConfigFileMap = {dev: 'dev.json', test: 'test.json', invalid: 'invalid.json'};
|
|
69818
|
+
*
|
|
69819
|
+
* // asynchronous function that reads a json file and parses the contents as json object
|
|
69820
|
+
* function parseFile(file, key, callback) {
|
|
69821
|
+
* fs.readFile(file, "utf8", function(err, data) {
|
|
69822
|
+
* if (err) return calback(err);
|
|
69823
|
+
* try {
|
|
69824
|
+
* configs[key] = JSON.parse(data);
|
|
69825
|
+
* } catch (e) {
|
|
69826
|
+
* return callback(e);
|
|
69827
|
+
* }
|
|
69828
|
+
* callback();
|
|
69829
|
+
* });
|
|
69830
|
+
* }
|
|
69831
|
+
*
|
|
69832
|
+
* // Using callbacks
|
|
69833
|
+
* async.forEachOf(validConfigFileMap, parseFile, function (err) {
|
|
69834
|
+
* if (err) {
|
|
69835
|
+
* console.error(err);
|
|
69836
|
+
* } else {
|
|
69837
|
+
* console.log(configs);
|
|
69838
|
+
* // configs is now a map of JSON data, e.g.
|
|
69839
|
+
* // { dev: //parsed dev.json, test: //parsed test.json, prod: //parsed prod.json}
|
|
69840
|
+
* }
|
|
69841
|
+
* });
|
|
69842
|
+
*
|
|
69843
|
+
* //Error handing
|
|
69844
|
+
* async.forEachOf(invalidConfigFileMap, parseFile, function (err) {
|
|
69845
|
+
* if (err) {
|
|
69846
|
+
* console.error(err);
|
|
69847
|
+
* // JSON parse error exception
|
|
69848
|
+
* } else {
|
|
69849
|
+
* console.log(configs);
|
|
69850
|
+
* }
|
|
69851
|
+
* });
|
|
69852
|
+
*
|
|
69853
|
+
* // Using Promises
|
|
69854
|
+
* async.forEachOf(validConfigFileMap, parseFile)
|
|
69855
|
+
* .then( () => {
|
|
69856
|
+
* console.log(configs);
|
|
69857
|
+
* // configs is now a map of JSON data, e.g.
|
|
69858
|
+
* // { dev: //parsed dev.json, test: //parsed test.json, prod: //parsed prod.json}
|
|
69859
|
+
* }).catch( err => {
|
|
69860
|
+
* console.error(err);
|
|
69861
|
+
* });
|
|
69862
|
+
*
|
|
69863
|
+
* //Error handing
|
|
69864
|
+
* async.forEachOf(invalidConfigFileMap, parseFile)
|
|
69865
|
+
* .then( () => {
|
|
69866
|
+
* console.log(configs);
|
|
69867
|
+
* }).catch( err => {
|
|
69868
|
+
* console.error(err);
|
|
69869
|
+
* // JSON parse error exception
|
|
69870
|
+
* });
|
|
69871
|
+
*
|
|
69872
|
+
* // Using async/await
|
|
69873
|
+
* async () => {
|
|
69874
|
+
* try {
|
|
69875
|
+
* let result = await async.forEachOf(validConfigFileMap, parseFile);
|
|
69876
|
+
* console.log(configs);
|
|
69877
|
+
* // configs is now a map of JSON data, e.g.
|
|
69878
|
+
* // { dev: //parsed dev.json, test: //parsed test.json, prod: //parsed prod.json}
|
|
69879
|
+
* }
|
|
69880
|
+
* catch (err) {
|
|
69881
|
+
* console.log(err);
|
|
69882
|
+
* }
|
|
69883
|
+
* }
|
|
69884
|
+
*
|
|
69885
|
+
* //Error handing
|
|
69886
|
+
* async () => {
|
|
69887
|
+
* try {
|
|
69888
|
+
* let result = await async.forEachOf(invalidConfigFileMap, parseFile);
|
|
69889
|
+
* console.log(configs);
|
|
69890
|
+
* }
|
|
69891
|
+
* catch (err) {
|
|
69892
|
+
* console.log(err);
|
|
69893
|
+
* // JSON parse error exception
|
|
69894
|
+
* }
|
|
69895
|
+
* }
|
|
69896
|
+
*
|
|
69897
|
+
*/
|
|
69898
|
+
function eachOf(coll, iteratee, callback) {
|
|
69899
|
+
var eachOfImplementation = isArrayLike(coll) ? eachOfArrayLike : eachOfGeneric;
|
|
69900
|
+
return eachOfImplementation(coll, wrapAsync(iteratee), callback);
|
|
69901
|
+
}
|
|
69902
|
+
|
|
69903
|
+
var eachOf$1 = awaitify(eachOf, 3);
|
|
69904
|
+
|
|
69905
|
+
/**
|
|
69906
|
+
* Produces a new collection of values by mapping each value in `coll` through
|
|
69907
|
+
* the `iteratee` function. The `iteratee` is called with an item from `coll`
|
|
69908
|
+
* and a callback for when it has finished processing. Each of these callbacks
|
|
69909
|
+
* takes 2 arguments: an `error`, and the transformed item from `coll`. If
|
|
69910
|
+
* `iteratee` passes an error to its callback, the main `callback` (for the
|
|
69911
|
+
* `map` function) is immediately called with the error.
|
|
69912
|
+
*
|
|
69913
|
+
* Note, that since this function applies the `iteratee` to each item in
|
|
69914
|
+
* parallel, there is no guarantee that the `iteratee` functions will complete
|
|
69915
|
+
* in order. However, the results array will be in the same order as the
|
|
69916
|
+
* original `coll`.
|
|
69917
|
+
*
|
|
69918
|
+
* If `map` is passed an Object, the results will be an Array. The results
|
|
69919
|
+
* will roughly be in the order of the original Objects' keys (but this can
|
|
69920
|
+
* vary across JavaScript engines).
|
|
69921
|
+
*
|
|
69922
|
+
* @name map
|
|
69923
|
+
* @static
|
|
69924
|
+
* @memberOf module:Collections
|
|
69925
|
+
* @method
|
|
69926
|
+
* @category Collection
|
|
69927
|
+
* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|
69928
|
+
* @param {AsyncFunction} iteratee - An async function to apply to each item in
|
|
69929
|
+
* `coll`.
|
|
69930
|
+
* The iteratee should complete with the transformed item.
|
|
69931
|
+
* Invoked with (item, callback).
|
|
69932
|
+
* @param {Function} [callback] - A callback which is called when all `iteratee`
|
|
69933
|
+
* functions have finished, or an error occurs. Results is an Array of the
|
|
69934
|
+
* transformed items from the `coll`. Invoked with (err, results).
|
|
69935
|
+
* @returns {Promise} a promise, if no callback is passed
|
|
69936
|
+
* @example
|
|
69937
|
+
*
|
|
69938
|
+
* // file1.txt is a file that is 1000 bytes in size
|
|
69939
|
+
* // file2.txt is a file that is 2000 bytes in size
|
|
69940
|
+
* // file3.txt is a file that is 3000 bytes in size
|
|
69941
|
+
* // file4.txt does not exist
|
|
69942
|
+
*
|
|
69943
|
+
* const fileList = ['file1.txt','file2.txt','file3.txt'];
|
|
69944
|
+
* const withMissingFileList = ['file1.txt','file2.txt','file4.txt'];
|
|
69945
|
+
*
|
|
69946
|
+
* // asynchronous function that returns the file size in bytes
|
|
69947
|
+
* function getFileSizeInBytes(file, callback) {
|
|
69948
|
+
* fs.stat(file, function(err, stat) {
|
|
69949
|
+
* if (err) {
|
|
69950
|
+
* return callback(err);
|
|
69951
|
+
* }
|
|
69952
|
+
* callback(null, stat.size);
|
|
69953
|
+
* });
|
|
69954
|
+
* }
|
|
69955
|
+
*
|
|
69956
|
+
* // Using callbacks
|
|
69957
|
+
* async.map(fileList, getFileSizeInBytes, function(err, results) {
|
|
69958
|
+
* if (err) {
|
|
69959
|
+
* console.log(err);
|
|
69960
|
+
* } else {
|
|
69961
|
+
* console.log(results);
|
|
69962
|
+
* // results is now an array of the file size in bytes for each file, e.g.
|
|
69963
|
+
* // [ 1000, 2000, 3000]
|
|
69964
|
+
* }
|
|
69965
|
+
* });
|
|
69966
|
+
*
|
|
69967
|
+
* // Error Handling
|
|
69968
|
+
* async.map(withMissingFileList, getFileSizeInBytes, function(err, results) {
|
|
69969
|
+
* if (err) {
|
|
69970
|
+
* console.log(err);
|
|
69971
|
+
* // [ Error: ENOENT: no such file or directory ]
|
|
69972
|
+
* } else {
|
|
69973
|
+
* console.log(results);
|
|
69974
|
+
* }
|
|
69975
|
+
* });
|
|
69976
|
+
*
|
|
69977
|
+
* // Using Promises
|
|
69978
|
+
* async.map(fileList, getFileSizeInBytes)
|
|
69979
|
+
* .then( results => {
|
|
69980
|
+
* console.log(results);
|
|
69981
|
+
* // results is now an array of the file size in bytes for each file, e.g.
|
|
69982
|
+
* // [ 1000, 2000, 3000]
|
|
69983
|
+
* }).catch( err => {
|
|
69984
|
+
* console.log(err);
|
|
69985
|
+
* });
|
|
69986
|
+
*
|
|
69987
|
+
* // Error Handling
|
|
69988
|
+
* async.map(withMissingFileList, getFileSizeInBytes)
|
|
69989
|
+
* .then( results => {
|
|
69990
|
+
* console.log(results);
|
|
69991
|
+
* }).catch( err => {
|
|
69992
|
+
* console.log(err);
|
|
69993
|
+
* // [ Error: ENOENT: no such file or directory ]
|
|
69994
|
+
* });
|
|
69995
|
+
*
|
|
69996
|
+
* // Using async/await
|
|
69997
|
+
* async () => {
|
|
69998
|
+
* try {
|
|
69999
|
+
* let results = await async.map(fileList, getFileSizeInBytes);
|
|
70000
|
+
* console.log(results);
|
|
70001
|
+
* // results is now an array of the file size in bytes for each file, e.g.
|
|
70002
|
+
* // [ 1000, 2000, 3000]
|
|
70003
|
+
* }
|
|
70004
|
+
* catch (err) {
|
|
70005
|
+
* console.log(err);
|
|
70006
|
+
* }
|
|
70007
|
+
* }
|
|
70008
|
+
*
|
|
70009
|
+
* // Error Handling
|
|
70010
|
+
* async () => {
|
|
70011
|
+
* try {
|
|
70012
|
+
* let results = await async.map(withMissingFileList, getFileSizeInBytes);
|
|
70013
|
+
* console.log(results);
|
|
70014
|
+
* }
|
|
70015
|
+
* catch (err) {
|
|
70016
|
+
* console.log(err);
|
|
70017
|
+
* // [ Error: ENOENT: no such file or directory ]
|
|
70018
|
+
* }
|
|
70019
|
+
* }
|
|
70020
|
+
*
|
|
70021
|
+
*/
|
|
70022
|
+
function map (coll, iteratee, callback) {
|
|
70023
|
+
return _asyncMap(eachOf$1, coll, iteratee, callback)
|
|
70024
|
+
}
|
|
70025
|
+
var map$1 = awaitify(map, 3);
|
|
70026
|
+
|
|
70027
|
+
/**
|
|
70028
|
+
* The same as [`eachOf`]{@link module:Collections.eachOf} but runs only a single async operation at a time.
|
|
70029
|
+
*
|
|
70030
|
+
* @name eachOfSeries
|
|
70031
|
+
* @static
|
|
70032
|
+
* @memberOf module:Collections
|
|
70033
|
+
* @method
|
|
70034
|
+
* @see [async.eachOf]{@link module:Collections.eachOf}
|
|
70035
|
+
* @alias forEachOfSeries
|
|
70036
|
+
* @category Collection
|
|
70037
|
+
* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|
70038
|
+
* @param {AsyncFunction} iteratee - An async function to apply to each item in
|
|
70039
|
+
* `coll`.
|
|
70040
|
+
* Invoked with (item, key, callback).
|
|
70041
|
+
* @param {Function} [callback] - A callback which is called when all `iteratee`
|
|
70042
|
+
* functions have finished, or an error occurs. Invoked with (err).
|
|
70043
|
+
* @returns {Promise} a promise, if a callback is omitted
|
|
70044
|
+
*/
|
|
70045
|
+
function eachOfSeries(coll, iteratee, callback) {
|
|
70046
|
+
return eachOfLimit$1(coll, 1, iteratee, callback)
|
|
70047
|
+
}
|
|
70048
|
+
var eachOfSeries$1 = awaitify(eachOfSeries, 3);
|
|
70049
|
+
|
|
70050
|
+
/**
|
|
70051
|
+
* The same as [`map`]{@link module:Collections.map} but runs only a single async operation at a time.
|
|
70052
|
+
*
|
|
70053
|
+
* @name mapSeries
|
|
70054
|
+
* @static
|
|
70055
|
+
* @memberOf module:Collections
|
|
70056
|
+
* @method
|
|
70057
|
+
* @see [async.map]{@link module:Collections.map}
|
|
70058
|
+
* @category Collection
|
|
70059
|
+
* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|
70060
|
+
* @param {AsyncFunction} iteratee - An async function to apply to each item in
|
|
70061
|
+
* `coll`.
|
|
70062
|
+
* The iteratee should complete with the transformed item.
|
|
70063
|
+
* Invoked with (item, callback).
|
|
70064
|
+
* @param {Function} [callback] - A callback which is called when all `iteratee`
|
|
70065
|
+
* functions have finished, or an error occurs. Results is an array of the
|
|
70066
|
+
* transformed items from the `coll`. Invoked with (err, results).
|
|
70067
|
+
* @returns {Promise} a promise, if no callback is passed
|
|
70068
|
+
*/
|
|
70069
|
+
function mapSeries (coll, iteratee, callback) {
|
|
70070
|
+
return _asyncMap(eachOfSeries$1, coll, iteratee, callback)
|
|
70071
|
+
}
|
|
70072
|
+
awaitify(mapSeries, 3);
|
|
70073
|
+
|
|
70074
|
+
/**
|
|
70075
|
+
* Reduces `coll` into a single value using an async `iteratee` to return each
|
|
70076
|
+
* successive step. `memo` is the initial state of the reduction. This function
|
|
70077
|
+
* only operates in series.
|
|
70078
|
+
*
|
|
70079
|
+
* For performance reasons, it may make sense to split a call to this function
|
|
70080
|
+
* into a parallel map, and then use the normal `Array.prototype.reduce` on the
|
|
70081
|
+
* results. This function is for situations where each step in the reduction
|
|
70082
|
+
* needs to be async; if you can get the data before reducing it, then it's
|
|
70083
|
+
* probably a good idea to do so.
|
|
70084
|
+
*
|
|
70085
|
+
* @name reduce
|
|
70086
|
+
* @static
|
|
70087
|
+
* @memberOf module:Collections
|
|
70088
|
+
* @method
|
|
70089
|
+
* @alias inject
|
|
70090
|
+
* @alias foldl
|
|
70091
|
+
* @category Collection
|
|
70092
|
+
* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|
70093
|
+
* @param {*} memo - The initial state of the reduction.
|
|
70094
|
+
* @param {AsyncFunction} iteratee - A function applied to each item in the
|
|
70095
|
+
* array to produce the next step in the reduction.
|
|
70096
|
+
* The `iteratee` should complete with the next state of the reduction.
|
|
70097
|
+
* If the iteratee completes with an error, the reduction is stopped and the
|
|
70098
|
+
* main `callback` is immediately called with the error.
|
|
70099
|
+
* Invoked with (memo, item, callback).
|
|
70100
|
+
* @param {Function} [callback] - A callback which is called after all the
|
|
70101
|
+
* `iteratee` functions have finished. Result is the reduced value. Invoked with
|
|
70102
|
+
* (err, result).
|
|
70103
|
+
* @returns {Promise} a promise, if no callback is passed
|
|
70104
|
+
* @example
|
|
70105
|
+
*
|
|
70106
|
+
* // file1.txt is a file that is 1000 bytes in size
|
|
70107
|
+
* // file2.txt is a file that is 2000 bytes in size
|
|
70108
|
+
* // file3.txt is a file that is 3000 bytes in size
|
|
70109
|
+
* // file4.txt does not exist
|
|
70110
|
+
*
|
|
70111
|
+
* const fileList = ['file1.txt','file2.txt','file3.txt'];
|
|
70112
|
+
* const withMissingFileList = ['file1.txt','file2.txt','file3.txt', 'file4.txt'];
|
|
70113
|
+
*
|
|
70114
|
+
* // asynchronous function that computes the file size in bytes
|
|
70115
|
+
* // file size is added to the memoized value, then returned
|
|
70116
|
+
* function getFileSizeInBytes(memo, file, callback) {
|
|
70117
|
+
* fs.stat(file, function(err, stat) {
|
|
70118
|
+
* if (err) {
|
|
70119
|
+
* return callback(err);
|
|
70120
|
+
* }
|
|
70121
|
+
* callback(null, memo + stat.size);
|
|
70122
|
+
* });
|
|
70123
|
+
* }
|
|
70124
|
+
*
|
|
70125
|
+
* // Using callbacks
|
|
70126
|
+
* async.reduce(fileList, 0, getFileSizeInBytes, function(err, result) {
|
|
70127
|
+
* if (err) {
|
|
70128
|
+
* console.log(err);
|
|
70129
|
+
* } else {
|
|
70130
|
+
* console.log(result);
|
|
70131
|
+
* // 6000
|
|
70132
|
+
* // which is the sum of the file sizes of the three files
|
|
70133
|
+
* }
|
|
70134
|
+
* });
|
|
70135
|
+
*
|
|
70136
|
+
* // Error Handling
|
|
70137
|
+
* async.reduce(withMissingFileList, 0, getFileSizeInBytes, function(err, result) {
|
|
70138
|
+
* if (err) {
|
|
70139
|
+
* console.log(err);
|
|
70140
|
+
* // [ Error: ENOENT: no such file or directory ]
|
|
70141
|
+
* } else {
|
|
70142
|
+
* console.log(result);
|
|
70143
|
+
* }
|
|
70144
|
+
* });
|
|
70145
|
+
*
|
|
70146
|
+
* // Using Promises
|
|
70147
|
+
* async.reduce(fileList, 0, getFileSizeInBytes)
|
|
70148
|
+
* .then( result => {
|
|
70149
|
+
* console.log(result);
|
|
70150
|
+
* // 6000
|
|
70151
|
+
* // which is the sum of the file sizes of the three files
|
|
70152
|
+
* }).catch( err => {
|
|
70153
|
+
* console.log(err);
|
|
70154
|
+
* });
|
|
70155
|
+
*
|
|
70156
|
+
* // Error Handling
|
|
70157
|
+
* async.reduce(withMissingFileList, 0, getFileSizeInBytes)
|
|
70158
|
+
* .then( result => {
|
|
70159
|
+
* console.log(result);
|
|
70160
|
+
* }).catch( err => {
|
|
70161
|
+
* console.log(err);
|
|
70162
|
+
* // [ Error: ENOENT: no such file or directory ]
|
|
70163
|
+
* });
|
|
70164
|
+
*
|
|
70165
|
+
* // Using async/await
|
|
70166
|
+
* async () => {
|
|
70167
|
+
* try {
|
|
70168
|
+
* let result = await async.reduce(fileList, 0, getFileSizeInBytes);
|
|
70169
|
+
* console.log(result);
|
|
70170
|
+
* // 6000
|
|
70171
|
+
* // which is the sum of the file sizes of the three files
|
|
70172
|
+
* }
|
|
70173
|
+
* catch (err) {
|
|
70174
|
+
* console.log(err);
|
|
70175
|
+
* }
|
|
70176
|
+
* }
|
|
70177
|
+
*
|
|
70178
|
+
* // Error Handling
|
|
70179
|
+
* async () => {
|
|
70180
|
+
* try {
|
|
70181
|
+
* let result = await async.reduce(withMissingFileList, 0, getFileSizeInBytes);
|
|
70182
|
+
* console.log(result);
|
|
70183
|
+
* }
|
|
70184
|
+
* catch (err) {
|
|
70185
|
+
* console.log(err);
|
|
70186
|
+
* // [ Error: ENOENT: no such file or directory ]
|
|
70187
|
+
* }
|
|
70188
|
+
* }
|
|
70189
|
+
*
|
|
70190
|
+
*/
|
|
70191
|
+
function reduce(coll, memo, iteratee, callback) {
|
|
70192
|
+
callback = once(callback);
|
|
70193
|
+
var _iteratee = wrapAsync(iteratee);
|
|
70194
|
+
return eachOfSeries$1(coll, (x, i, iterCb) => {
|
|
70195
|
+
_iteratee(memo, x, (err, v) => {
|
|
70196
|
+
memo = v;
|
|
70197
|
+
iterCb(err);
|
|
70198
|
+
});
|
|
70199
|
+
}, err => callback(err, memo));
|
|
70200
|
+
}
|
|
70201
|
+
awaitify(reduce, 4);
|
|
70202
|
+
|
|
70203
|
+
/**
|
|
70204
|
+
* The same as [`map`]{@link module:Collections.map} but runs a maximum of `limit` async operations at a time.
|
|
70205
|
+
*
|
|
70206
|
+
* @name mapLimit
|
|
70207
|
+
* @static
|
|
70208
|
+
* @memberOf module:Collections
|
|
70209
|
+
* @method
|
|
70210
|
+
* @see [async.map]{@link module:Collections.map}
|
|
70211
|
+
* @category Collection
|
|
70212
|
+
* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|
70213
|
+
* @param {number} limit - The maximum number of async operations at a time.
|
|
70214
|
+
* @param {AsyncFunction} iteratee - An async function to apply to each item in
|
|
70215
|
+
* `coll`.
|
|
70216
|
+
* The iteratee should complete with the transformed item.
|
|
70217
|
+
* Invoked with (item, callback).
|
|
70218
|
+
* @param {Function} [callback] - A callback which is called when all `iteratee`
|
|
70219
|
+
* functions have finished, or an error occurs. Results is an array of the
|
|
70220
|
+
* transformed items from the `coll`. Invoked with (err, results).
|
|
70221
|
+
* @returns {Promise} a promise, if no callback is passed
|
|
70222
|
+
*/
|
|
70223
|
+
function mapLimit (coll, limit, iteratee, callback) {
|
|
70224
|
+
return _asyncMap(eachOfLimit$2(limit), coll, iteratee, callback)
|
|
70225
|
+
}
|
|
70226
|
+
var mapLimit$1 = awaitify(mapLimit, 4);
|
|
70227
|
+
|
|
70228
|
+
/**
|
|
70229
|
+
* The same as [`concat`]{@link module:Collections.concat} but runs a maximum of `limit` async operations at a time.
|
|
70230
|
+
*
|
|
70231
|
+
* @name concatLimit
|
|
70232
|
+
* @static
|
|
70233
|
+
* @memberOf module:Collections
|
|
70234
|
+
* @method
|
|
70235
|
+
* @see [async.concat]{@link module:Collections.concat}
|
|
70236
|
+
* @category Collection
|
|
70237
|
+
* @alias flatMapLimit
|
|
70238
|
+
* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|
70239
|
+
* @param {number} limit - The maximum number of async operations at a time.
|
|
70240
|
+
* @param {AsyncFunction} iteratee - A function to apply to each item in `coll`,
|
|
70241
|
+
* which should use an array as its result. Invoked with (item, callback).
|
|
70242
|
+
* @param {Function} [callback] - A callback which is called after all the
|
|
70243
|
+
* `iteratee` functions have finished, or an error occurs. Results is an array
|
|
70244
|
+
* containing the concatenated results of the `iteratee` function. Invoked with
|
|
70245
|
+
* (err, results).
|
|
70246
|
+
* @returns A Promise, if no callback is passed
|
|
70247
|
+
*/
|
|
70248
|
+
function concatLimit(coll, limit, iteratee, callback) {
|
|
70249
|
+
var _iteratee = wrapAsync(iteratee);
|
|
70250
|
+
return mapLimit$1(coll, limit, (val, iterCb) => {
|
|
70251
|
+
_iteratee(val, (err, ...args) => {
|
|
70252
|
+
if (err) return iterCb(err);
|
|
70253
|
+
return iterCb(err, args);
|
|
70254
|
+
});
|
|
70255
|
+
}, (err, mapResults) => {
|
|
70256
|
+
var result = [];
|
|
70257
|
+
for (var i = 0; i < mapResults.length; i++) {
|
|
70258
|
+
if (mapResults[i]) {
|
|
70259
|
+
result = result.concat(...mapResults[i]);
|
|
70260
|
+
}
|
|
70261
|
+
}
|
|
70262
|
+
|
|
70263
|
+
return callback(err, result);
|
|
70264
|
+
});
|
|
70265
|
+
}
|
|
70266
|
+
var concatLimit$1 = awaitify(concatLimit, 4);
|
|
70267
|
+
|
|
70268
|
+
/**
|
|
70269
|
+
* Applies `iteratee` to each item in `coll`, concatenating the results. Returns
|
|
70270
|
+
* the concatenated list. The `iteratee`s are called in parallel, and the
|
|
70271
|
+
* results are concatenated as they return. The results array will be returned in
|
|
70272
|
+
* the original order of `coll` passed to the `iteratee` function.
|
|
70273
|
+
*
|
|
70274
|
+
* @name concat
|
|
70275
|
+
* @static
|
|
70276
|
+
* @memberOf module:Collections
|
|
70277
|
+
* @method
|
|
70278
|
+
* @category Collection
|
|
70279
|
+
* @alias flatMap
|
|
70280
|
+
* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|
70281
|
+
* @param {AsyncFunction} iteratee - A function to apply to each item in `coll`,
|
|
70282
|
+
* which should use an array as its result. Invoked with (item, callback).
|
|
70283
|
+
* @param {Function} [callback] - A callback which is called after all the
|
|
70284
|
+
* `iteratee` functions have finished, or an error occurs. Results is an array
|
|
70285
|
+
* containing the concatenated results of the `iteratee` function. Invoked with
|
|
70286
|
+
* (err, results).
|
|
70287
|
+
* @returns A Promise, if no callback is passed
|
|
70288
|
+
* @example
|
|
70289
|
+
*
|
|
70290
|
+
* // dir1 is a directory that contains file1.txt, file2.txt
|
|
70291
|
+
* // dir2 is a directory that contains file3.txt, file4.txt
|
|
70292
|
+
* // dir3 is a directory that contains file5.txt
|
|
70293
|
+
* // dir4 does not exist
|
|
70294
|
+
*
|
|
70295
|
+
* let directoryList = ['dir1','dir2','dir3'];
|
|
70296
|
+
* let withMissingDirectoryList = ['dir1','dir2','dir3', 'dir4'];
|
|
70297
|
+
*
|
|
70298
|
+
* // Using callbacks
|
|
70299
|
+
* async.concat(directoryList, fs.readdir, function(err, results) {
|
|
70300
|
+
* if (err) {
|
|
70301
|
+
* console.log(err);
|
|
70302
|
+
* } else {
|
|
70303
|
+
* console.log(results);
|
|
70304
|
+
* // [ 'file1.txt', 'file2.txt', 'file3.txt', 'file4.txt', file5.txt ]
|
|
70305
|
+
* }
|
|
70306
|
+
* });
|
|
70307
|
+
*
|
|
70308
|
+
* // Error Handling
|
|
70309
|
+
* async.concat(withMissingDirectoryList, fs.readdir, function(err, results) {
|
|
70310
|
+
* if (err) {
|
|
70311
|
+
* console.log(err);
|
|
70312
|
+
* // [ Error: ENOENT: no such file or directory ]
|
|
70313
|
+
* // since dir4 does not exist
|
|
70314
|
+
* } else {
|
|
70315
|
+
* console.log(results);
|
|
70316
|
+
* }
|
|
70317
|
+
* });
|
|
70318
|
+
*
|
|
70319
|
+
* // Using Promises
|
|
70320
|
+
* async.concat(directoryList, fs.readdir)
|
|
70321
|
+
* .then(results => {
|
|
70322
|
+
* console.log(results);
|
|
70323
|
+
* // [ 'file1.txt', 'file2.txt', 'file3.txt', 'file4.txt', file5.txt ]
|
|
70324
|
+
* }).catch(err => {
|
|
70325
|
+
* console.log(err);
|
|
70326
|
+
* });
|
|
70327
|
+
*
|
|
70328
|
+
* // Error Handling
|
|
70329
|
+
* async.concat(withMissingDirectoryList, fs.readdir)
|
|
70330
|
+
* .then(results => {
|
|
70331
|
+
* console.log(results);
|
|
70332
|
+
* }).catch(err => {
|
|
70333
|
+
* console.log(err);
|
|
70334
|
+
* // [ Error: ENOENT: no such file or directory ]
|
|
70335
|
+
* // since dir4 does not exist
|
|
70336
|
+
* });
|
|
70337
|
+
*
|
|
70338
|
+
* // Using async/await
|
|
70339
|
+
* async () => {
|
|
70340
|
+
* try {
|
|
70341
|
+
* let results = await async.concat(directoryList, fs.readdir);
|
|
70342
|
+
* console.log(results);
|
|
70343
|
+
* // [ 'file1.txt', 'file2.txt', 'file3.txt', 'file4.txt', file5.txt ]
|
|
70344
|
+
* } catch (err) {
|
|
70345
|
+
* console.log(err);
|
|
70346
|
+
* }
|
|
70347
|
+
* }
|
|
70348
|
+
*
|
|
70349
|
+
* // Error Handling
|
|
70350
|
+
* async () => {
|
|
70351
|
+
* try {
|
|
70352
|
+
* let results = await async.concat(withMissingDirectoryList, fs.readdir);
|
|
70353
|
+
* console.log(results);
|
|
70354
|
+
* } catch (err) {
|
|
70355
|
+
* console.log(err);
|
|
70356
|
+
* // [ Error: ENOENT: no such file or directory ]
|
|
70357
|
+
* // since dir4 does not exist
|
|
70358
|
+
* }
|
|
70359
|
+
* }
|
|
70360
|
+
*
|
|
70361
|
+
*/
|
|
70362
|
+
function concat(coll, iteratee, callback) {
|
|
70363
|
+
return concatLimit$1(coll, Infinity, iteratee, callback)
|
|
70364
|
+
}
|
|
70365
|
+
awaitify(concat, 3);
|
|
70366
|
+
|
|
70367
|
+
/**
|
|
70368
|
+
* The same as [`concat`]{@link module:Collections.concat} but runs only a single async operation at a time.
|
|
70369
|
+
*
|
|
70370
|
+
* @name concatSeries
|
|
70371
|
+
* @static
|
|
70372
|
+
* @memberOf module:Collections
|
|
70373
|
+
* @method
|
|
70374
|
+
* @see [async.concat]{@link module:Collections.concat}
|
|
70375
|
+
* @category Collection
|
|
70376
|
+
* @alias flatMapSeries
|
|
70377
|
+
* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|
70378
|
+
* @param {AsyncFunction} iteratee - A function to apply to each item in `coll`.
|
|
70379
|
+
* The iteratee should complete with an array an array of results.
|
|
70380
|
+
* Invoked with (item, callback).
|
|
70381
|
+
* @param {Function} [callback] - A callback which is called after all the
|
|
70382
|
+
* `iteratee` functions have finished, or an error occurs. Results is an array
|
|
70383
|
+
* containing the concatenated results of the `iteratee` function. Invoked with
|
|
70384
|
+
* (err, results).
|
|
70385
|
+
* @returns A Promise, if no callback is passed
|
|
70386
|
+
*/
|
|
70387
|
+
function concatSeries(coll, iteratee, callback) {
|
|
70388
|
+
return concatLimit$1(coll, 1, iteratee, callback)
|
|
70389
|
+
}
|
|
70390
|
+
awaitify(concatSeries, 3);
|
|
70391
|
+
|
|
70392
|
+
function _createTester(check, getResult) {
|
|
70393
|
+
return (eachfn, arr, _iteratee, cb) => {
|
|
70394
|
+
var testPassed = false;
|
|
70395
|
+
var testResult;
|
|
70396
|
+
const iteratee = wrapAsync(_iteratee);
|
|
70397
|
+
eachfn(arr, (value, _, callback) => {
|
|
70398
|
+
iteratee(value, (err, result) => {
|
|
70399
|
+
if (err || err === false) return callback(err);
|
|
70400
|
+
|
|
70401
|
+
if (check(result) && !testResult) {
|
|
70402
|
+
testPassed = true;
|
|
70403
|
+
testResult = getResult(true, value);
|
|
70404
|
+
return callback(null, breakLoop);
|
|
70405
|
+
}
|
|
70406
|
+
callback();
|
|
70407
|
+
});
|
|
70408
|
+
}, err => {
|
|
70409
|
+
if (err) return cb(err);
|
|
70410
|
+
cb(null, testPassed ? testResult : getResult(false));
|
|
70411
|
+
});
|
|
70412
|
+
};
|
|
70413
|
+
}
|
|
70414
|
+
|
|
70415
|
+
/**
|
|
70416
|
+
* Returns the first value in `coll` that passes an async truth test. The
|
|
70417
|
+
* `iteratee` is applied in parallel, meaning the first iteratee to return
|
|
70418
|
+
* `true` will fire the detect `callback` with that result. That means the
|
|
70419
|
+
* result might not be the first item in the original `coll` (in terms of order)
|
|
70420
|
+
* that passes the test.
|
|
70421
|
+
|
|
70422
|
+
* If order within the original `coll` is important, then look at
|
|
70423
|
+
* [`detectSeries`]{@link module:Collections.detectSeries}.
|
|
70424
|
+
*
|
|
70425
|
+
* @name detect
|
|
70426
|
+
* @static
|
|
70427
|
+
* @memberOf module:Collections
|
|
70428
|
+
* @method
|
|
70429
|
+
* @alias find
|
|
70430
|
+
* @category Collections
|
|
70431
|
+
* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|
70432
|
+
* @param {AsyncFunction} iteratee - A truth test to apply to each item in `coll`.
|
|
70433
|
+
* The iteratee must complete with a boolean value as its result.
|
|
70434
|
+
* Invoked with (item, callback).
|
|
70435
|
+
* @param {Function} [callback] - A callback which is called as soon as any
|
|
70436
|
+
* iteratee returns `true`, or after all the `iteratee` functions have finished.
|
|
70437
|
+
* Result will be the first item in the array that passes the truth test
|
|
70438
|
+
* (iteratee) or the value `undefined` if none passed. Invoked with
|
|
70439
|
+
* (err, result).
|
|
70440
|
+
* @returns {Promise} a promise, if a callback is omitted
|
|
70441
|
+
* @example
|
|
70442
|
+
*
|
|
70443
|
+
* // dir1 is a directory that contains file1.txt, file2.txt
|
|
70444
|
+
* // dir2 is a directory that contains file3.txt, file4.txt
|
|
70445
|
+
* // dir3 is a directory that contains file5.txt
|
|
70446
|
+
*
|
|
70447
|
+
* // asynchronous function that checks if a file exists
|
|
70448
|
+
* function fileExists(file, callback) {
|
|
70449
|
+
* fs.access(file, fs.constants.F_OK, (err) => {
|
|
70450
|
+
* callback(null, !err);
|
|
70451
|
+
* });
|
|
70452
|
+
* }
|
|
70453
|
+
*
|
|
70454
|
+
* async.detect(['file3.txt','file2.txt','dir1/file1.txt'], fileExists,
|
|
70455
|
+
* function(err, result) {
|
|
70456
|
+
* console.log(result);
|
|
70457
|
+
* // dir1/file1.txt
|
|
70458
|
+
* // result now equals the first file in the list that exists
|
|
70459
|
+
* }
|
|
70460
|
+
*);
|
|
70461
|
+
*
|
|
70462
|
+
* // Using Promises
|
|
70463
|
+
* async.detect(['file3.txt','file2.txt','dir1/file1.txt'], fileExists)
|
|
70464
|
+
* .then(result => {
|
|
70465
|
+
* console.log(result);
|
|
70466
|
+
* // dir1/file1.txt
|
|
70467
|
+
* // result now equals the first file in the list that exists
|
|
70468
|
+
* }).catch(err => {
|
|
70469
|
+
* console.log(err);
|
|
70470
|
+
* });
|
|
70471
|
+
*
|
|
70472
|
+
* // Using async/await
|
|
70473
|
+
* async () => {
|
|
70474
|
+
* try {
|
|
70475
|
+
* let result = await async.detect(['file3.txt','file2.txt','dir1/file1.txt'], fileExists);
|
|
70476
|
+
* console.log(result);
|
|
70477
|
+
* // dir1/file1.txt
|
|
70478
|
+
* // result now equals the file in the list that exists
|
|
70479
|
+
* }
|
|
70480
|
+
* catch (err) {
|
|
70481
|
+
* console.log(err);
|
|
70482
|
+
* }
|
|
70483
|
+
* }
|
|
70484
|
+
*
|
|
70485
|
+
*/
|
|
70486
|
+
function detect(coll, iteratee, callback) {
|
|
70487
|
+
return _createTester(bool => bool, (res, item) => item)(eachOf$1, coll, iteratee, callback)
|
|
70488
|
+
}
|
|
70489
|
+
awaitify(detect, 3);
|
|
70490
|
+
|
|
70491
|
+
/**
|
|
70492
|
+
* The same as [`detect`]{@link module:Collections.detect} but runs a maximum of `limit` async operations at a
|
|
70493
|
+
* time.
|
|
70494
|
+
*
|
|
70495
|
+
* @name detectLimit
|
|
70496
|
+
* @static
|
|
70497
|
+
* @memberOf module:Collections
|
|
70498
|
+
* @method
|
|
70499
|
+
* @see [async.detect]{@link module:Collections.detect}
|
|
70500
|
+
* @alias findLimit
|
|
70501
|
+
* @category Collections
|
|
70502
|
+
* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|
70503
|
+
* @param {number} limit - The maximum number of async operations at a time.
|
|
70504
|
+
* @param {AsyncFunction} iteratee - A truth test to apply to each item in `coll`.
|
|
70505
|
+
* The iteratee must complete with a boolean value as its result.
|
|
70506
|
+
* Invoked with (item, callback).
|
|
70507
|
+
* @param {Function} [callback] - A callback which is called as soon as any
|
|
70508
|
+
* iteratee returns `true`, or after all the `iteratee` functions have finished.
|
|
70509
|
+
* Result will be the first item in the array that passes the truth test
|
|
70510
|
+
* (iteratee) or the value `undefined` if none passed. Invoked with
|
|
70511
|
+
* (err, result).
|
|
70512
|
+
* @returns {Promise} a promise, if a callback is omitted
|
|
70513
|
+
*/
|
|
70514
|
+
function detectLimit(coll, limit, iteratee, callback) {
|
|
70515
|
+
return _createTester(bool => bool, (res, item) => item)(eachOfLimit$2(limit), coll, iteratee, callback)
|
|
70516
|
+
}
|
|
70517
|
+
awaitify(detectLimit, 4);
|
|
70518
|
+
|
|
70519
|
+
/**
|
|
70520
|
+
* The same as [`detect`]{@link module:Collections.detect} but runs only a single async operation at a time.
|
|
70521
|
+
*
|
|
70522
|
+
* @name detectSeries
|
|
70523
|
+
* @static
|
|
70524
|
+
* @memberOf module:Collections
|
|
70525
|
+
* @method
|
|
70526
|
+
* @see [async.detect]{@link module:Collections.detect}
|
|
70527
|
+
* @alias findSeries
|
|
70528
|
+
* @category Collections
|
|
70529
|
+
* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|
70530
|
+
* @param {AsyncFunction} iteratee - A truth test to apply to each item in `coll`.
|
|
70531
|
+
* The iteratee must complete with a boolean value as its result.
|
|
70532
|
+
* Invoked with (item, callback).
|
|
70533
|
+
* @param {Function} [callback] - A callback which is called as soon as any
|
|
70534
|
+
* iteratee returns `true`, or after all the `iteratee` functions have finished.
|
|
70535
|
+
* Result will be the first item in the array that passes the truth test
|
|
70536
|
+
* (iteratee) or the value `undefined` if none passed. Invoked with
|
|
70537
|
+
* (err, result).
|
|
70538
|
+
* @returns {Promise} a promise, if a callback is omitted
|
|
70539
|
+
*/
|
|
70540
|
+
function detectSeries(coll, iteratee, callback) {
|
|
70541
|
+
return _createTester(bool => bool, (res, item) => item)(eachOfLimit$2(1), coll, iteratee, callback)
|
|
70542
|
+
}
|
|
70543
|
+
|
|
70544
|
+
awaitify(detectSeries, 3);
|
|
70545
|
+
|
|
70546
|
+
/**
|
|
70547
|
+
* The post-check version of [`whilst`]{@link module:ControlFlow.whilst}. To reflect the difference in
|
|
70548
|
+
* the order of operations, the arguments `test` and `iteratee` are switched.
|
|
70549
|
+
*
|
|
70550
|
+
* `doWhilst` is to `whilst` as `do while` is to `while` in plain JavaScript.
|
|
70551
|
+
*
|
|
70552
|
+
* @name doWhilst
|
|
70553
|
+
* @static
|
|
70554
|
+
* @memberOf module:ControlFlow
|
|
70555
|
+
* @method
|
|
70556
|
+
* @see [async.whilst]{@link module:ControlFlow.whilst}
|
|
70557
|
+
* @category Control Flow
|
|
70558
|
+
* @param {AsyncFunction} iteratee - A function which is called each time `test`
|
|
70559
|
+
* passes. Invoked with (callback).
|
|
70560
|
+
* @param {AsyncFunction} test - asynchronous truth test to perform after each
|
|
70561
|
+
* execution of `iteratee`. Invoked with (...args, callback), where `...args` are the
|
|
70562
|
+
* non-error args from the previous callback of `iteratee`.
|
|
70563
|
+
* @param {Function} [callback] - A callback which is called after the test
|
|
70564
|
+
* function has failed and repeated execution of `iteratee` has stopped.
|
|
70565
|
+
* `callback` will be passed an error and any arguments passed to the final
|
|
70566
|
+
* `iteratee`'s callback. Invoked with (err, [results]);
|
|
70567
|
+
* @returns {Promise} a promise, if no callback is passed
|
|
70568
|
+
*/
|
|
70569
|
+
function doWhilst(iteratee, test, callback) {
|
|
70570
|
+
callback = onlyOnce(callback);
|
|
70571
|
+
var _fn = wrapAsync(iteratee);
|
|
70572
|
+
var _test = wrapAsync(test);
|
|
70573
|
+
var results;
|
|
70574
|
+
|
|
70575
|
+
function next(err, ...args) {
|
|
70576
|
+
if (err) return callback(err);
|
|
70577
|
+
if (err === false) return;
|
|
70578
|
+
results = args;
|
|
70579
|
+
_test(...args, check);
|
|
70580
|
+
}
|
|
70581
|
+
|
|
70582
|
+
function check(err, truth) {
|
|
70583
|
+
if (err) return callback(err);
|
|
70584
|
+
if (err === false) return;
|
|
70585
|
+
if (!truth) return callback(null, ...results);
|
|
70586
|
+
_fn(next);
|
|
70587
|
+
}
|
|
70588
|
+
|
|
70589
|
+
return check(null, true);
|
|
70590
|
+
}
|
|
70591
|
+
|
|
70592
|
+
awaitify(doWhilst, 3);
|
|
70593
|
+
|
|
70594
|
+
function _withoutIndex(iteratee) {
|
|
70595
|
+
return (value, index, callback) => iteratee(value, callback);
|
|
70596
|
+
}
|
|
70597
|
+
|
|
70598
|
+
/**
|
|
70599
|
+
* Applies the function `iteratee` to each item in `coll`, in parallel.
|
|
70600
|
+
* The `iteratee` is called with an item from the list, and a callback for when
|
|
70601
|
+
* it has finished. If the `iteratee` passes an error to its `callback`, the
|
|
70602
|
+
* main `callback` (for the `each` function) is immediately called with the
|
|
70603
|
+
* error.
|
|
70604
|
+
*
|
|
70605
|
+
* Note, that since this function applies `iteratee` to each item in parallel,
|
|
70606
|
+
* there is no guarantee that the iteratee functions will complete in order.
|
|
70607
|
+
*
|
|
70608
|
+
* @name each
|
|
70609
|
+
* @static
|
|
70610
|
+
* @memberOf module:Collections
|
|
70611
|
+
* @method
|
|
70612
|
+
* @alias forEach
|
|
70613
|
+
* @category Collection
|
|
70614
|
+
* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|
70615
|
+
* @param {AsyncFunction} iteratee - An async function to apply to
|
|
70616
|
+
* each item in `coll`. Invoked with (item, callback).
|
|
70617
|
+
* The array index is not passed to the iteratee.
|
|
70618
|
+
* If you need the index, use `eachOf`.
|
|
70619
|
+
* @param {Function} [callback] - A callback which is called when all
|
|
70620
|
+
* `iteratee` functions have finished, or an error occurs. Invoked with (err).
|
|
70621
|
+
* @returns {Promise} a promise, if a callback is omitted
|
|
70622
|
+
* @example
|
|
70623
|
+
*
|
|
70624
|
+
* // dir1 is a directory that contains file1.txt, file2.txt
|
|
70625
|
+
* // dir2 is a directory that contains file3.txt, file4.txt
|
|
70626
|
+
* // dir3 is a directory that contains file5.txt
|
|
70627
|
+
* // dir4 does not exist
|
|
70628
|
+
*
|
|
70629
|
+
* const fileList = [ 'dir1/file2.txt', 'dir2/file3.txt', 'dir/file5.txt'];
|
|
70630
|
+
* const withMissingFileList = ['dir1/file1.txt', 'dir4/file2.txt'];
|
|
70631
|
+
*
|
|
70632
|
+
* // asynchronous function that deletes a file
|
|
70633
|
+
* const deleteFile = function(file, callback) {
|
|
70634
|
+
* fs.unlink(file, callback);
|
|
70635
|
+
* };
|
|
70636
|
+
*
|
|
70637
|
+
* // Using callbacks
|
|
70638
|
+
* async.each(fileList, deleteFile, function(err) {
|
|
70639
|
+
* if( err ) {
|
|
70640
|
+
* console.log(err);
|
|
70641
|
+
* } else {
|
|
70642
|
+
* console.log('All files have been deleted successfully');
|
|
70643
|
+
* }
|
|
70644
|
+
* });
|
|
70645
|
+
*
|
|
70646
|
+
* // Error Handling
|
|
70647
|
+
* async.each(withMissingFileList, deleteFile, function(err){
|
|
70648
|
+
* console.log(err);
|
|
70649
|
+
* // [ Error: ENOENT: no such file or directory ]
|
|
70650
|
+
* // since dir4/file2.txt does not exist
|
|
70651
|
+
* // dir1/file1.txt could have been deleted
|
|
70652
|
+
* });
|
|
70653
|
+
*
|
|
70654
|
+
* // Using Promises
|
|
70655
|
+
* async.each(fileList, deleteFile)
|
|
70656
|
+
* .then( () => {
|
|
70657
|
+
* console.log('All files have been deleted successfully');
|
|
70658
|
+
* }).catch( err => {
|
|
70659
|
+
* console.log(err);
|
|
70660
|
+
* });
|
|
70661
|
+
*
|
|
70662
|
+
* // Error Handling
|
|
70663
|
+
* async.each(fileList, deleteFile)
|
|
70664
|
+
* .then( () => {
|
|
70665
|
+
* console.log('All files have been deleted successfully');
|
|
70666
|
+
* }).catch( err => {
|
|
70667
|
+
* console.log(err);
|
|
70668
|
+
* // [ Error: ENOENT: no such file or directory ]
|
|
70669
|
+
* // since dir4/file2.txt does not exist
|
|
70670
|
+
* // dir1/file1.txt could have been deleted
|
|
70671
|
+
* });
|
|
70672
|
+
*
|
|
70673
|
+
* // Using async/await
|
|
70674
|
+
* async () => {
|
|
70675
|
+
* try {
|
|
70676
|
+
* await async.each(files, deleteFile);
|
|
70677
|
+
* }
|
|
70678
|
+
* catch (err) {
|
|
70679
|
+
* console.log(err);
|
|
70680
|
+
* }
|
|
70681
|
+
* }
|
|
70682
|
+
*
|
|
70683
|
+
* // Error Handling
|
|
70684
|
+
* async () => {
|
|
70685
|
+
* try {
|
|
70686
|
+
* await async.each(withMissingFileList, deleteFile);
|
|
70687
|
+
* }
|
|
70688
|
+
* catch (err) {
|
|
70689
|
+
* console.log(err);
|
|
70690
|
+
* // [ Error: ENOENT: no such file or directory ]
|
|
70691
|
+
* // since dir4/file2.txt does not exist
|
|
70692
|
+
* // dir1/file1.txt could have been deleted
|
|
70693
|
+
* }
|
|
70694
|
+
* }
|
|
70695
|
+
*
|
|
70696
|
+
*/
|
|
70697
|
+
function eachLimit$2(coll, iteratee, callback) {
|
|
70698
|
+
return eachOf$1(coll, _withoutIndex(wrapAsync(iteratee)), callback);
|
|
70699
|
+
}
|
|
70700
|
+
|
|
70701
|
+
awaitify(eachLimit$2, 3);
|
|
70702
|
+
|
|
70703
|
+
/**
|
|
70704
|
+
* The same as [`each`]{@link module:Collections.each} but runs a maximum of `limit` async operations at a time.
|
|
70705
|
+
*
|
|
70706
|
+
* @name eachLimit
|
|
70707
|
+
* @static
|
|
70708
|
+
* @memberOf module:Collections
|
|
70709
|
+
* @method
|
|
70710
|
+
* @see [async.each]{@link module:Collections.each}
|
|
70711
|
+
* @alias forEachLimit
|
|
70712
|
+
* @category Collection
|
|
70713
|
+
* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|
70714
|
+
* @param {number} limit - The maximum number of async operations at a time.
|
|
70715
|
+
* @param {AsyncFunction} iteratee - An async function to apply to each item in
|
|
70716
|
+
* `coll`.
|
|
70717
|
+
* The array index is not passed to the iteratee.
|
|
70718
|
+
* If you need the index, use `eachOfLimit`.
|
|
70719
|
+
* Invoked with (item, callback).
|
|
70720
|
+
* @param {Function} [callback] - A callback which is called when all
|
|
70721
|
+
* `iteratee` functions have finished, or an error occurs. Invoked with (err).
|
|
70722
|
+
* @returns {Promise} a promise, if a callback is omitted
|
|
70723
|
+
*/
|
|
70724
|
+
function eachLimit(coll, limit, iteratee, callback) {
|
|
70725
|
+
return eachOfLimit$2(limit)(coll, _withoutIndex(wrapAsync(iteratee)), callback);
|
|
70726
|
+
}
|
|
70727
|
+
var eachLimit$1 = awaitify(eachLimit, 4);
|
|
70728
|
+
|
|
70729
|
+
/**
|
|
70730
|
+
* The same as [`each`]{@link module:Collections.each} but runs only a single async operation at a time.
|
|
70731
|
+
*
|
|
70732
|
+
* Note, that unlike [`each`]{@link module:Collections.each}, this function applies iteratee to each item
|
|
70733
|
+
* in series and therefore the iteratee functions will complete in order.
|
|
70734
|
+
|
|
70735
|
+
* @name eachSeries
|
|
70736
|
+
* @static
|
|
70737
|
+
* @memberOf module:Collections
|
|
70738
|
+
* @method
|
|
70739
|
+
* @see [async.each]{@link module:Collections.each}
|
|
70740
|
+
* @alias forEachSeries
|
|
70741
|
+
* @category Collection
|
|
70742
|
+
* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|
70743
|
+
* @param {AsyncFunction} iteratee - An async function to apply to each
|
|
70744
|
+
* item in `coll`.
|
|
70745
|
+
* The array index is not passed to the iteratee.
|
|
70746
|
+
* If you need the index, use `eachOfSeries`.
|
|
70747
|
+
* Invoked with (item, callback).
|
|
70748
|
+
* @param {Function} [callback] - A callback which is called when all
|
|
70749
|
+
* `iteratee` functions have finished, or an error occurs. Invoked with (err).
|
|
70750
|
+
* @returns {Promise} a promise, if a callback is omitted
|
|
70751
|
+
*/
|
|
70752
|
+
function eachSeries(coll, iteratee, callback) {
|
|
70753
|
+
return eachLimit$1(coll, 1, iteratee, callback)
|
|
70754
|
+
}
|
|
70755
|
+
var eachSeries$1 = awaitify(eachSeries, 3);
|
|
70756
|
+
|
|
70757
|
+
/**
|
|
70758
|
+
* Wrap an async function and ensure it calls its callback on a later tick of
|
|
70759
|
+
* the event loop. If the function already calls its callback on a next tick,
|
|
70760
|
+
* no extra deferral is added. This is useful for preventing stack overflows
|
|
70761
|
+
* (`RangeError: Maximum call stack size exceeded`) and generally keeping
|
|
70762
|
+
* [Zalgo](http://blog.izs.me/post/59142742143/designing-apis-for-asynchrony)
|
|
70763
|
+
* contained. ES2017 `async` functions are returned as-is -- they are immune
|
|
70764
|
+
* to Zalgo's corrupting influences, as they always resolve on a later tick.
|
|
70765
|
+
*
|
|
70766
|
+
* @name ensureAsync
|
|
70767
|
+
* @static
|
|
70768
|
+
* @memberOf module:Utils
|
|
70769
|
+
* @method
|
|
70770
|
+
* @category Util
|
|
70771
|
+
* @param {AsyncFunction} fn - an async function, one that expects a node-style
|
|
70772
|
+
* callback as its last argument.
|
|
70773
|
+
* @returns {AsyncFunction} Returns a wrapped function with the exact same call
|
|
70774
|
+
* signature as the function passed in.
|
|
70775
|
+
* @example
|
|
70776
|
+
*
|
|
70777
|
+
* function sometimesAsync(arg, callback) {
|
|
70778
|
+
* if (cache[arg]) {
|
|
70779
|
+
* return callback(null, cache[arg]); // this would be synchronous!!
|
|
70780
|
+
* } else {
|
|
70781
|
+
* doSomeIO(arg, callback); // this IO would be asynchronous
|
|
70782
|
+
* }
|
|
70783
|
+
* }
|
|
70784
|
+
*
|
|
70785
|
+
* // this has a risk of stack overflows if many results are cached in a row
|
|
70786
|
+
* async.mapSeries(args, sometimesAsync, done);
|
|
70787
|
+
*
|
|
70788
|
+
* // this will defer sometimesAsync's callback if necessary,
|
|
70789
|
+
* // preventing stack overflows
|
|
70790
|
+
* async.mapSeries(args, async.ensureAsync(sometimesAsync), done);
|
|
70791
|
+
*/
|
|
70792
|
+
function ensureAsync(fn) {
|
|
70793
|
+
if (isAsync(fn)) return fn;
|
|
70794
|
+
return function (...args/*, callback*/) {
|
|
70795
|
+
var callback = args.pop();
|
|
70796
|
+
var sync = true;
|
|
70797
|
+
args.push((...innerArgs) => {
|
|
70798
|
+
if (sync) {
|
|
70799
|
+
setImmediate$1(() => callback(...innerArgs));
|
|
70800
|
+
} else {
|
|
70801
|
+
callback(...innerArgs);
|
|
70802
|
+
}
|
|
70803
|
+
});
|
|
70804
|
+
fn.apply(this, args);
|
|
70805
|
+
sync = false;
|
|
70806
|
+
};
|
|
70807
|
+
}
|
|
70808
|
+
|
|
70809
|
+
/**
|
|
70810
|
+
* Returns `true` if every element in `coll` satisfies an async test. If any
|
|
70811
|
+
* iteratee call returns `false`, the main `callback` is immediately called.
|
|
70812
|
+
*
|
|
70813
|
+
* @name every
|
|
70814
|
+
* @static
|
|
70815
|
+
* @memberOf module:Collections
|
|
70816
|
+
* @method
|
|
70817
|
+
* @alias all
|
|
70818
|
+
* @category Collection
|
|
70819
|
+
* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|
70820
|
+
* @param {AsyncFunction} iteratee - An async truth test to apply to each item
|
|
70821
|
+
* in the collection in parallel.
|
|
70822
|
+
* The iteratee must complete with a boolean result value.
|
|
70823
|
+
* Invoked with (item, callback).
|
|
70824
|
+
* @param {Function} [callback] - A callback which is called after all the
|
|
70825
|
+
* `iteratee` functions have finished. Result will be either `true` or `false`
|
|
70826
|
+
* depending on the values of the async tests. Invoked with (err, result).
|
|
70827
|
+
* @returns {Promise} a promise, if no callback provided
|
|
70828
|
+
* @example
|
|
70829
|
+
*
|
|
70830
|
+
* // dir1 is a directory that contains file1.txt, file2.txt
|
|
70831
|
+
* // dir2 is a directory that contains file3.txt, file4.txt
|
|
70832
|
+
* // dir3 is a directory that contains file5.txt
|
|
70833
|
+
* // dir4 does not exist
|
|
70834
|
+
*
|
|
70835
|
+
* const fileList = ['dir1/file1.txt','dir2/file3.txt','dir3/file5.txt'];
|
|
70836
|
+
* const withMissingFileList = ['file1.txt','file2.txt','file4.txt'];
|
|
70837
|
+
*
|
|
70838
|
+
* // asynchronous function that checks if a file exists
|
|
70839
|
+
* function fileExists(file, callback) {
|
|
70840
|
+
* fs.access(file, fs.constants.F_OK, (err) => {
|
|
70841
|
+
* callback(null, !err);
|
|
70842
|
+
* });
|
|
70843
|
+
* }
|
|
70844
|
+
*
|
|
70845
|
+
* // Using callbacks
|
|
70846
|
+
* async.every(fileList, fileExists, function(err, result) {
|
|
70847
|
+
* console.log(result);
|
|
70848
|
+
* // true
|
|
70849
|
+
* // result is true since every file exists
|
|
70850
|
+
* });
|
|
70851
|
+
*
|
|
70852
|
+
* async.every(withMissingFileList, fileExists, function(err, result) {
|
|
70853
|
+
* console.log(result);
|
|
70854
|
+
* // false
|
|
70855
|
+
* // result is false since NOT every file exists
|
|
70856
|
+
* });
|
|
70857
|
+
*
|
|
70858
|
+
* // Using Promises
|
|
70859
|
+
* async.every(fileList, fileExists)
|
|
70860
|
+
* .then( result => {
|
|
70861
|
+
* console.log(result);
|
|
70862
|
+
* // true
|
|
70863
|
+
* // result is true since every file exists
|
|
70864
|
+
* }).catch( err => {
|
|
70865
|
+
* console.log(err);
|
|
70866
|
+
* });
|
|
70867
|
+
*
|
|
70868
|
+
* async.every(withMissingFileList, fileExists)
|
|
70869
|
+
* .then( result => {
|
|
70870
|
+
* console.log(result);
|
|
70871
|
+
* // false
|
|
70872
|
+
* // result is false since NOT every file exists
|
|
70873
|
+
* }).catch( err => {
|
|
70874
|
+
* console.log(err);
|
|
70875
|
+
* });
|
|
70876
|
+
*
|
|
70877
|
+
* // Using async/await
|
|
70878
|
+
* async () => {
|
|
70879
|
+
* try {
|
|
70880
|
+
* let result = await async.every(fileList, fileExists);
|
|
70881
|
+
* console.log(result);
|
|
70882
|
+
* // true
|
|
70883
|
+
* // result is true since every file exists
|
|
70884
|
+
* }
|
|
70885
|
+
* catch (err) {
|
|
70886
|
+
* console.log(err);
|
|
70887
|
+
* }
|
|
70888
|
+
* }
|
|
70889
|
+
*
|
|
70890
|
+
* async () => {
|
|
70891
|
+
* try {
|
|
70892
|
+
* let result = await async.every(withMissingFileList, fileExists);
|
|
70893
|
+
* console.log(result);
|
|
70894
|
+
* // false
|
|
70895
|
+
* // result is false since NOT every file exists
|
|
70896
|
+
* }
|
|
70897
|
+
* catch (err) {
|
|
70898
|
+
* console.log(err);
|
|
70899
|
+
* }
|
|
70900
|
+
* }
|
|
70901
|
+
*
|
|
70902
|
+
*/
|
|
70903
|
+
function every(coll, iteratee, callback) {
|
|
70904
|
+
return _createTester(bool => !bool, res => !res)(eachOf$1, coll, iteratee, callback)
|
|
70905
|
+
}
|
|
70906
|
+
awaitify(every, 3);
|
|
70907
|
+
|
|
70908
|
+
/**
|
|
70909
|
+
* The same as [`every`]{@link module:Collections.every} but runs a maximum of `limit` async operations at a time.
|
|
70910
|
+
*
|
|
70911
|
+
* @name everyLimit
|
|
70912
|
+
* @static
|
|
70913
|
+
* @memberOf module:Collections
|
|
70914
|
+
* @method
|
|
70915
|
+
* @see [async.every]{@link module:Collections.every}
|
|
70916
|
+
* @alias allLimit
|
|
70917
|
+
* @category Collection
|
|
70918
|
+
* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|
70919
|
+
* @param {number} limit - The maximum number of async operations at a time.
|
|
70920
|
+
* @param {AsyncFunction} iteratee - An async truth test to apply to each item
|
|
70921
|
+
* in the collection in parallel.
|
|
70922
|
+
* The iteratee must complete with a boolean result value.
|
|
70923
|
+
* Invoked with (item, callback).
|
|
70924
|
+
* @param {Function} [callback] - A callback which is called after all the
|
|
70925
|
+
* `iteratee` functions have finished. Result will be either `true` or `false`
|
|
70926
|
+
* depending on the values of the async tests. Invoked with (err, result).
|
|
70927
|
+
* @returns {Promise} a promise, if no callback provided
|
|
70928
|
+
*/
|
|
70929
|
+
function everyLimit(coll, limit, iteratee, callback) {
|
|
70930
|
+
return _createTester(bool => !bool, res => !res)(eachOfLimit$2(limit), coll, iteratee, callback)
|
|
70931
|
+
}
|
|
70932
|
+
awaitify(everyLimit, 4);
|
|
70933
|
+
|
|
70934
|
+
/**
|
|
70935
|
+
* The same as [`every`]{@link module:Collections.every} but runs only a single async operation at a time.
|
|
70936
|
+
*
|
|
70937
|
+
* @name everySeries
|
|
70938
|
+
* @static
|
|
70939
|
+
* @memberOf module:Collections
|
|
70940
|
+
* @method
|
|
70941
|
+
* @see [async.every]{@link module:Collections.every}
|
|
70942
|
+
* @alias allSeries
|
|
70943
|
+
* @category Collection
|
|
70944
|
+
* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|
70945
|
+
* @param {AsyncFunction} iteratee - An async truth test to apply to each item
|
|
70946
|
+
* in the collection in series.
|
|
70947
|
+
* The iteratee must complete with a boolean result value.
|
|
70948
|
+
* Invoked with (item, callback).
|
|
70949
|
+
* @param {Function} [callback] - A callback which is called after all the
|
|
70950
|
+
* `iteratee` functions have finished. Result will be either `true` or `false`
|
|
70951
|
+
* depending on the values of the async tests. Invoked with (err, result).
|
|
70952
|
+
* @returns {Promise} a promise, if no callback provided
|
|
70953
|
+
*/
|
|
70954
|
+
function everySeries(coll, iteratee, callback) {
|
|
70955
|
+
return _createTester(bool => !bool, res => !res)(eachOfSeries$1, coll, iteratee, callback)
|
|
70956
|
+
}
|
|
70957
|
+
awaitify(everySeries, 3);
|
|
70958
|
+
|
|
70959
|
+
function filterArray(eachfn, arr, iteratee, callback) {
|
|
70960
|
+
var truthValues = new Array(arr.length);
|
|
70961
|
+
eachfn(arr, (x, index, iterCb) => {
|
|
70962
|
+
iteratee(x, (err, v) => {
|
|
70963
|
+
truthValues[index] = !!v;
|
|
70964
|
+
iterCb(err);
|
|
70965
|
+
});
|
|
70966
|
+
}, err => {
|
|
70967
|
+
if (err) return callback(err);
|
|
70968
|
+
var results = [];
|
|
70969
|
+
for (var i = 0; i < arr.length; i++) {
|
|
70970
|
+
if (truthValues[i]) results.push(arr[i]);
|
|
70971
|
+
}
|
|
70972
|
+
callback(null, results);
|
|
70973
|
+
});
|
|
70974
|
+
}
|
|
70975
|
+
|
|
70976
|
+
function filterGeneric(eachfn, coll, iteratee, callback) {
|
|
70977
|
+
var results = [];
|
|
70978
|
+
eachfn(coll, (x, index, iterCb) => {
|
|
70979
|
+
iteratee(x, (err, v) => {
|
|
70980
|
+
if (err) return iterCb(err);
|
|
70981
|
+
if (v) {
|
|
70982
|
+
results.push({index, value: x});
|
|
70983
|
+
}
|
|
70984
|
+
iterCb(err);
|
|
70985
|
+
});
|
|
70986
|
+
}, err => {
|
|
70987
|
+
if (err) return callback(err);
|
|
70988
|
+
callback(null, results
|
|
70989
|
+
.sort((a, b) => a.index - b.index)
|
|
70990
|
+
.map(v => v.value));
|
|
70991
|
+
});
|
|
70992
|
+
}
|
|
70993
|
+
|
|
70994
|
+
function _filter(eachfn, coll, iteratee, callback) {
|
|
70995
|
+
var filter = isArrayLike(coll) ? filterArray : filterGeneric;
|
|
70996
|
+
return filter(eachfn, coll, wrapAsync(iteratee), callback);
|
|
70997
|
+
}
|
|
70998
|
+
|
|
70999
|
+
/**
|
|
71000
|
+
* Returns a new array of all the values in `coll` which pass an async truth
|
|
71001
|
+
* test. This operation is performed in parallel, but the results array will be
|
|
71002
|
+
* in the same order as the original.
|
|
71003
|
+
*
|
|
71004
|
+
* @name filter
|
|
71005
|
+
* @static
|
|
71006
|
+
* @memberOf module:Collections
|
|
71007
|
+
* @method
|
|
71008
|
+
* @alias select
|
|
71009
|
+
* @category Collection
|
|
71010
|
+
* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|
71011
|
+
* @param {Function} iteratee - A truth test to apply to each item in `coll`.
|
|
71012
|
+
* The `iteratee` is passed a `callback(err, truthValue)`, which must be called
|
|
71013
|
+
* with a boolean argument once it has completed. Invoked with (item, callback).
|
|
71014
|
+
* @param {Function} [callback] - A callback which is called after all the
|
|
71015
|
+
* `iteratee` functions have finished. Invoked with (err, results).
|
|
71016
|
+
* @returns {Promise} a promise, if no callback provided
|
|
71017
|
+
* @example
|
|
71018
|
+
*
|
|
71019
|
+
* // dir1 is a directory that contains file1.txt, file2.txt
|
|
71020
|
+
* // dir2 is a directory that contains file3.txt, file4.txt
|
|
71021
|
+
* // dir3 is a directory that contains file5.txt
|
|
71022
|
+
*
|
|
71023
|
+
* const files = ['dir1/file1.txt','dir2/file3.txt','dir3/file6.txt'];
|
|
71024
|
+
*
|
|
71025
|
+
* // asynchronous function that checks if a file exists
|
|
71026
|
+
* function fileExists(file, callback) {
|
|
71027
|
+
* fs.access(file, fs.constants.F_OK, (err) => {
|
|
71028
|
+
* callback(null, !err);
|
|
71029
|
+
* });
|
|
71030
|
+
* }
|
|
71031
|
+
*
|
|
71032
|
+
* // Using callbacks
|
|
71033
|
+
* async.filter(files, fileExists, function(err, results) {
|
|
71034
|
+
* if(err) {
|
|
71035
|
+
* console.log(err);
|
|
71036
|
+
* } else {
|
|
71037
|
+
* console.log(results);
|
|
71038
|
+
* // [ 'dir1/file1.txt', 'dir2/file3.txt' ]
|
|
71039
|
+
* // results is now an array of the existing files
|
|
71040
|
+
* }
|
|
71041
|
+
* });
|
|
71042
|
+
*
|
|
71043
|
+
* // Using Promises
|
|
71044
|
+
* async.filter(files, fileExists)
|
|
71045
|
+
* .then(results => {
|
|
71046
|
+
* console.log(results);
|
|
71047
|
+
* // [ 'dir1/file1.txt', 'dir2/file3.txt' ]
|
|
71048
|
+
* // results is now an array of the existing files
|
|
71049
|
+
* }).catch(err => {
|
|
71050
|
+
* console.log(err);
|
|
71051
|
+
* });
|
|
71052
|
+
*
|
|
71053
|
+
* // Using async/await
|
|
71054
|
+
* async () => {
|
|
71055
|
+
* try {
|
|
71056
|
+
* let results = await async.filter(files, fileExists);
|
|
71057
|
+
* console.log(results);
|
|
71058
|
+
* // [ 'dir1/file1.txt', 'dir2/file3.txt' ]
|
|
71059
|
+
* // results is now an array of the existing files
|
|
71060
|
+
* }
|
|
71061
|
+
* catch (err) {
|
|
71062
|
+
* console.log(err);
|
|
71063
|
+
* }
|
|
71064
|
+
* }
|
|
71065
|
+
*
|
|
71066
|
+
*/
|
|
71067
|
+
function filter (coll, iteratee, callback) {
|
|
71068
|
+
return _filter(eachOf$1, coll, iteratee, callback)
|
|
71069
|
+
}
|
|
71070
|
+
awaitify(filter, 3);
|
|
71071
|
+
|
|
71072
|
+
/**
|
|
71073
|
+
* The same as [`filter`]{@link module:Collections.filter} but runs a maximum of `limit` async operations at a
|
|
71074
|
+
* time.
|
|
71075
|
+
*
|
|
71076
|
+
* @name filterLimit
|
|
71077
|
+
* @static
|
|
71078
|
+
* @memberOf module:Collections
|
|
71079
|
+
* @method
|
|
71080
|
+
* @see [async.filter]{@link module:Collections.filter}
|
|
71081
|
+
* @alias selectLimit
|
|
71082
|
+
* @category Collection
|
|
71083
|
+
* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|
71084
|
+
* @param {number} limit - The maximum number of async operations at a time.
|
|
71085
|
+
* @param {Function} iteratee - A truth test to apply to each item in `coll`.
|
|
71086
|
+
* The `iteratee` is passed a `callback(err, truthValue)`, which must be called
|
|
71087
|
+
* with a boolean argument once it has completed. Invoked with (item, callback).
|
|
71088
|
+
* @param {Function} [callback] - A callback which is called after all the
|
|
71089
|
+
* `iteratee` functions have finished. Invoked with (err, results).
|
|
71090
|
+
* @returns {Promise} a promise, if no callback provided
|
|
71091
|
+
*/
|
|
71092
|
+
function filterLimit (coll, limit, iteratee, callback) {
|
|
71093
|
+
return _filter(eachOfLimit$2(limit), coll, iteratee, callback)
|
|
71094
|
+
}
|
|
71095
|
+
awaitify(filterLimit, 4);
|
|
71096
|
+
|
|
71097
|
+
/**
|
|
71098
|
+
* The same as [`filter`]{@link module:Collections.filter} but runs only a single async operation at a time.
|
|
71099
|
+
*
|
|
71100
|
+
* @name filterSeries
|
|
71101
|
+
* @static
|
|
71102
|
+
* @memberOf module:Collections
|
|
71103
|
+
* @method
|
|
71104
|
+
* @see [async.filter]{@link module:Collections.filter}
|
|
71105
|
+
* @alias selectSeries
|
|
71106
|
+
* @category Collection
|
|
71107
|
+
* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|
71108
|
+
* @param {Function} iteratee - A truth test to apply to each item in `coll`.
|
|
71109
|
+
* The `iteratee` is passed a `callback(err, truthValue)`, which must be called
|
|
71110
|
+
* with a boolean argument once it has completed. Invoked with (item, callback).
|
|
71111
|
+
* @param {Function} [callback] - A callback which is called after all the
|
|
71112
|
+
* `iteratee` functions have finished. Invoked with (err, results)
|
|
71113
|
+
* @returns {Promise} a promise, if no callback provided
|
|
71114
|
+
*/
|
|
71115
|
+
function filterSeries (coll, iteratee, callback) {
|
|
71116
|
+
return _filter(eachOfSeries$1, coll, iteratee, callback)
|
|
71117
|
+
}
|
|
71118
|
+
awaitify(filterSeries, 3);
|
|
71119
|
+
|
|
71120
|
+
/**
|
|
71121
|
+
* Calls the asynchronous function `fn` with a callback parameter that allows it
|
|
71122
|
+
* to call itself again, in series, indefinitely.
|
|
71123
|
+
|
|
71124
|
+
* If an error is passed to the callback then `errback` is called with the
|
|
71125
|
+
* error, and execution stops, otherwise it will never be called.
|
|
71126
|
+
*
|
|
71127
|
+
* @name forever
|
|
71128
|
+
* @static
|
|
71129
|
+
* @memberOf module:ControlFlow
|
|
71130
|
+
* @method
|
|
71131
|
+
* @category Control Flow
|
|
71132
|
+
* @param {AsyncFunction} fn - an async function to call repeatedly.
|
|
71133
|
+
* Invoked with (next).
|
|
71134
|
+
* @param {Function} [errback] - when `fn` passes an error to it's callback,
|
|
71135
|
+
* this function will be called, and execution stops. Invoked with (err).
|
|
71136
|
+
* @returns {Promise} a promise that rejects if an error occurs and an errback
|
|
71137
|
+
* is not passed
|
|
71138
|
+
* @example
|
|
71139
|
+
*
|
|
71140
|
+
* async.forever(
|
|
71141
|
+
* function(next) {
|
|
71142
|
+
* // next is suitable for passing to things that need a callback(err [, whatever]);
|
|
71143
|
+
* // it will result in this function being called again.
|
|
71144
|
+
* },
|
|
71145
|
+
* function(err) {
|
|
71146
|
+
* // if next is called with a value in its first parameter, it will appear
|
|
71147
|
+
* // in here as 'err', and execution will stop.
|
|
71148
|
+
* }
|
|
71149
|
+
* );
|
|
71150
|
+
*/
|
|
71151
|
+
function forever(fn, errback) {
|
|
71152
|
+
var done = onlyOnce(errback);
|
|
71153
|
+
var task = wrapAsync(ensureAsync(fn));
|
|
71154
|
+
|
|
71155
|
+
function next(err) {
|
|
71156
|
+
if (err) return done(err);
|
|
71157
|
+
if (err === false) return;
|
|
71158
|
+
task(next);
|
|
71159
|
+
}
|
|
71160
|
+
return next();
|
|
71161
|
+
}
|
|
71162
|
+
awaitify(forever, 2);
|
|
71163
|
+
|
|
71164
|
+
/**
|
|
71165
|
+
* The same as [`groupBy`]{@link module:Collections.groupBy} but runs a maximum of `limit` async operations at a time.
|
|
71166
|
+
*
|
|
71167
|
+
* @name groupByLimit
|
|
71168
|
+
* @static
|
|
71169
|
+
* @memberOf module:Collections
|
|
71170
|
+
* @method
|
|
71171
|
+
* @see [async.groupBy]{@link module:Collections.groupBy}
|
|
71172
|
+
* @category Collection
|
|
71173
|
+
* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|
71174
|
+
* @param {number} limit - The maximum number of async operations at a time.
|
|
71175
|
+
* @param {AsyncFunction} iteratee - An async function to apply to each item in
|
|
71176
|
+
* `coll`.
|
|
71177
|
+
* The iteratee should complete with a `key` to group the value under.
|
|
71178
|
+
* Invoked with (value, callback).
|
|
71179
|
+
* @param {Function} [callback] - A callback which is called when all `iteratee`
|
|
71180
|
+
* functions have finished, or an error occurs. Result is an `Object` whoses
|
|
71181
|
+
* properties are arrays of values which returned the corresponding key.
|
|
71182
|
+
* @returns {Promise} a promise, if no callback is passed
|
|
71183
|
+
*/
|
|
71184
|
+
function groupByLimit(coll, limit, iteratee, callback) {
|
|
71185
|
+
var _iteratee = wrapAsync(iteratee);
|
|
71186
|
+
return mapLimit$1(coll, limit, (val, iterCb) => {
|
|
71187
|
+
_iteratee(val, (err, key) => {
|
|
71188
|
+
if (err) return iterCb(err);
|
|
71189
|
+
return iterCb(err, {key, val});
|
|
71190
|
+
});
|
|
71191
|
+
}, (err, mapResults) => {
|
|
71192
|
+
var result = {};
|
|
71193
|
+
// from MDN, handle object having an `hasOwnProperty` prop
|
|
71194
|
+
var {hasOwnProperty} = Object.prototype;
|
|
71195
|
+
|
|
71196
|
+
for (var i = 0; i < mapResults.length; i++) {
|
|
71197
|
+
if (mapResults[i]) {
|
|
71198
|
+
var {key} = mapResults[i];
|
|
71199
|
+
var {val} = mapResults[i];
|
|
71200
|
+
|
|
71201
|
+
if (hasOwnProperty.call(result, key)) {
|
|
71202
|
+
result[key].push(val);
|
|
71203
|
+
} else {
|
|
71204
|
+
result[key] = [val];
|
|
71205
|
+
}
|
|
71206
|
+
}
|
|
71207
|
+
}
|
|
71208
|
+
|
|
71209
|
+
return callback(err, result);
|
|
71210
|
+
});
|
|
71211
|
+
}
|
|
71212
|
+
|
|
71213
|
+
awaitify(groupByLimit, 4);
|
|
71214
|
+
|
|
71215
|
+
/**
|
|
71216
|
+
* The same as [`mapValues`]{@link module:Collections.mapValues} but runs a maximum of `limit` async operations at a
|
|
71217
|
+
* time.
|
|
71218
|
+
*
|
|
71219
|
+
* @name mapValuesLimit
|
|
71220
|
+
* @static
|
|
71221
|
+
* @memberOf module:Collections
|
|
71222
|
+
* @method
|
|
71223
|
+
* @see [async.mapValues]{@link module:Collections.mapValues}
|
|
71224
|
+
* @category Collection
|
|
71225
|
+
* @param {Object} obj - A collection to iterate over.
|
|
71226
|
+
* @param {number} limit - The maximum number of async operations at a time.
|
|
71227
|
+
* @param {AsyncFunction} iteratee - A function to apply to each value and key
|
|
71228
|
+
* in `coll`.
|
|
71229
|
+
* The iteratee should complete with the transformed value as its result.
|
|
71230
|
+
* Invoked with (value, key, callback).
|
|
71231
|
+
* @param {Function} [callback] - A callback which is called when all `iteratee`
|
|
71232
|
+
* functions have finished, or an error occurs. `result` is a new object consisting
|
|
71233
|
+
* of each key from `obj`, with each transformed value on the right-hand side.
|
|
71234
|
+
* Invoked with (err, result).
|
|
71235
|
+
* @returns {Promise} a promise, if no callback is passed
|
|
71236
|
+
*/
|
|
71237
|
+
function mapValuesLimit(obj, limit, iteratee, callback) {
|
|
71238
|
+
callback = once(callback);
|
|
71239
|
+
var newObj = {};
|
|
71240
|
+
var _iteratee = wrapAsync(iteratee);
|
|
71241
|
+
return eachOfLimit$2(limit)(obj, (val, key, next) => {
|
|
71242
|
+
_iteratee(val, key, (err, result) => {
|
|
71243
|
+
if (err) return next(err);
|
|
71244
|
+
newObj[key] = result;
|
|
71245
|
+
next(err);
|
|
71246
|
+
});
|
|
71247
|
+
}, err => callback(err, newObj));
|
|
71248
|
+
}
|
|
71249
|
+
|
|
71250
|
+
awaitify(mapValuesLimit, 4);
|
|
71251
|
+
|
|
71252
|
+
if (hasNextTick) {
|
|
71253
|
+
process.nextTick;
|
|
71254
|
+
} else if (hasSetImmediate) {
|
|
71255
|
+
setImmediate;
|
|
71256
|
+
} else ;
|
|
71257
|
+
|
|
71258
|
+
awaitify((eachfn, tasks, callback) => {
|
|
71259
|
+
var results = isArrayLike(tasks) ? [] : {};
|
|
71260
|
+
|
|
71261
|
+
eachfn(tasks, (task, key, taskCb) => {
|
|
71262
|
+
wrapAsync(task)((err, ...result) => {
|
|
71263
|
+
if (result.length < 2) {
|
|
71264
|
+
[result] = result;
|
|
71265
|
+
}
|
|
71266
|
+
results[key] = result;
|
|
71267
|
+
taskCb(err);
|
|
71268
|
+
});
|
|
71269
|
+
}, err => callback(err, results));
|
|
71270
|
+
}, 3);
|
|
71271
|
+
|
|
71272
|
+
/**
|
|
71273
|
+
* Runs the `tasks` array of functions in parallel, without waiting until the
|
|
71274
|
+
* previous function has completed. Once any of the `tasks` complete or pass an
|
|
71275
|
+
* error to its callback, the main `callback` is immediately called. It's
|
|
71276
|
+
* equivalent to `Promise.race()`.
|
|
71277
|
+
*
|
|
71278
|
+
* @name race
|
|
71279
|
+
* @static
|
|
71280
|
+
* @memberOf module:ControlFlow
|
|
71281
|
+
* @method
|
|
71282
|
+
* @category Control Flow
|
|
71283
|
+
* @param {Array} tasks - An array containing [async functions]{@link AsyncFunction}
|
|
71284
|
+
* to run. Each function can complete with an optional `result` value.
|
|
71285
|
+
* @param {Function} callback - A callback to run once any of the functions have
|
|
71286
|
+
* completed. This function gets an error or result from the first function that
|
|
71287
|
+
* completed. Invoked with (err, result).
|
|
71288
|
+
* @returns {Promise} a promise, if a callback is omitted
|
|
71289
|
+
* @example
|
|
71290
|
+
*
|
|
71291
|
+
* async.race([
|
|
71292
|
+
* function(callback) {
|
|
71293
|
+
* setTimeout(function() {
|
|
71294
|
+
* callback(null, 'one');
|
|
71295
|
+
* }, 200);
|
|
71296
|
+
* },
|
|
71297
|
+
* function(callback) {
|
|
71298
|
+
* setTimeout(function() {
|
|
71299
|
+
* callback(null, 'two');
|
|
71300
|
+
* }, 100);
|
|
71301
|
+
* }
|
|
71302
|
+
* ],
|
|
71303
|
+
* // main callback
|
|
71304
|
+
* function(err, result) {
|
|
71305
|
+
* // the result will be equal to 'two' as it finishes earlier
|
|
71306
|
+
* });
|
|
71307
|
+
*/
|
|
71308
|
+
function race(tasks, callback) {
|
|
71309
|
+
callback = once(callback);
|
|
71310
|
+
if (!Array.isArray(tasks)) return callback(new TypeError('First argument to race must be an array of functions'));
|
|
71311
|
+
if (!tasks.length) return callback();
|
|
71312
|
+
for (var i = 0, l = tasks.length; i < l; i++) {
|
|
71313
|
+
wrapAsync(tasks[i])(callback);
|
|
71314
|
+
}
|
|
71315
|
+
}
|
|
71316
|
+
|
|
71317
|
+
awaitify(race, 2);
|
|
71318
|
+
|
|
71319
|
+
function reject$2(eachfn, arr, _iteratee, callback) {
|
|
71320
|
+
const iteratee = wrapAsync(_iteratee);
|
|
71321
|
+
return _filter(eachfn, arr, (value, cb) => {
|
|
71322
|
+
iteratee(value, (err, v) => {
|
|
71323
|
+
cb(err, !v);
|
|
71324
|
+
});
|
|
71325
|
+
}, callback);
|
|
71326
|
+
}
|
|
71327
|
+
|
|
71328
|
+
/**
|
|
71329
|
+
* The opposite of [`filter`]{@link module:Collections.filter}. Removes values that pass an `async` truth test.
|
|
71330
|
+
*
|
|
71331
|
+
* @name reject
|
|
71332
|
+
* @static
|
|
71333
|
+
* @memberOf module:Collections
|
|
71334
|
+
* @method
|
|
71335
|
+
* @see [async.filter]{@link module:Collections.filter}
|
|
71336
|
+
* @category Collection
|
|
71337
|
+
* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|
71338
|
+
* @param {Function} iteratee - An async truth test to apply to each item in
|
|
71339
|
+
* `coll`.
|
|
71340
|
+
* The should complete with a boolean value as its `result`.
|
|
71341
|
+
* Invoked with (item, callback).
|
|
71342
|
+
* @param {Function} [callback] - A callback which is called after all the
|
|
71343
|
+
* `iteratee` functions have finished. Invoked with (err, results).
|
|
71344
|
+
* @returns {Promise} a promise, if no callback is passed
|
|
71345
|
+
* @example
|
|
71346
|
+
*
|
|
71347
|
+
* // dir1 is a directory that contains file1.txt, file2.txt
|
|
71348
|
+
* // dir2 is a directory that contains file3.txt, file4.txt
|
|
71349
|
+
* // dir3 is a directory that contains file5.txt
|
|
71350
|
+
*
|
|
71351
|
+
* const fileList = ['dir1/file1.txt','dir2/file3.txt','dir3/file6.txt'];
|
|
71352
|
+
*
|
|
71353
|
+
* // asynchronous function that checks if a file exists
|
|
71354
|
+
* function fileExists(file, callback) {
|
|
71355
|
+
* fs.access(file, fs.constants.F_OK, (err) => {
|
|
71356
|
+
* callback(null, !err);
|
|
71357
|
+
* });
|
|
71358
|
+
* }
|
|
71359
|
+
*
|
|
71360
|
+
* // Using callbacks
|
|
71361
|
+
* async.reject(fileList, fileExists, function(err, results) {
|
|
71362
|
+
* // [ 'dir3/file6.txt' ]
|
|
71363
|
+
* // results now equals an array of the non-existing files
|
|
71364
|
+
* });
|
|
71365
|
+
*
|
|
71366
|
+
* // Using Promises
|
|
71367
|
+
* async.reject(fileList, fileExists)
|
|
71368
|
+
* .then( results => {
|
|
71369
|
+
* console.log(results);
|
|
71370
|
+
* // [ 'dir3/file6.txt' ]
|
|
71371
|
+
* // results now equals an array of the non-existing files
|
|
71372
|
+
* }).catch( err => {
|
|
71373
|
+
* console.log(err);
|
|
71374
|
+
* });
|
|
71375
|
+
*
|
|
71376
|
+
* // Using async/await
|
|
71377
|
+
* async () => {
|
|
71378
|
+
* try {
|
|
71379
|
+
* let results = await async.reject(fileList, fileExists);
|
|
71380
|
+
* console.log(results);
|
|
71381
|
+
* // [ 'dir3/file6.txt' ]
|
|
71382
|
+
* // results now equals an array of the non-existing files
|
|
71383
|
+
* }
|
|
71384
|
+
* catch (err) {
|
|
71385
|
+
* console.log(err);
|
|
71386
|
+
* }
|
|
71387
|
+
* }
|
|
71388
|
+
*
|
|
71389
|
+
*/
|
|
71390
|
+
function reject (coll, iteratee, callback) {
|
|
71391
|
+
return reject$2(eachOf$1, coll, iteratee, callback)
|
|
71392
|
+
}
|
|
71393
|
+
awaitify(reject, 3);
|
|
71394
|
+
|
|
71395
|
+
/**
|
|
71396
|
+
* The same as [`reject`]{@link module:Collections.reject} but runs a maximum of `limit` async operations at a
|
|
71397
|
+
* time.
|
|
71398
|
+
*
|
|
71399
|
+
* @name rejectLimit
|
|
71400
|
+
* @static
|
|
71401
|
+
* @memberOf module:Collections
|
|
71402
|
+
* @method
|
|
71403
|
+
* @see [async.reject]{@link module:Collections.reject}
|
|
71404
|
+
* @category Collection
|
|
71405
|
+
* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|
71406
|
+
* @param {number} limit - The maximum number of async operations at a time.
|
|
71407
|
+
* @param {Function} iteratee - An async truth test to apply to each item in
|
|
71408
|
+
* `coll`.
|
|
71409
|
+
* The should complete with a boolean value as its `result`.
|
|
71410
|
+
* Invoked with (item, callback).
|
|
71411
|
+
* @param {Function} [callback] - A callback which is called after all the
|
|
71412
|
+
* `iteratee` functions have finished. Invoked with (err, results).
|
|
71413
|
+
* @returns {Promise} a promise, if no callback is passed
|
|
71414
|
+
*/
|
|
71415
|
+
function rejectLimit (coll, limit, iteratee, callback) {
|
|
71416
|
+
return reject$2(eachOfLimit$2(limit), coll, iteratee, callback)
|
|
71417
|
+
}
|
|
71418
|
+
awaitify(rejectLimit, 4);
|
|
71419
|
+
|
|
71420
|
+
/**
|
|
71421
|
+
* The same as [`reject`]{@link module:Collections.reject} but runs only a single async operation at a time.
|
|
71422
|
+
*
|
|
71423
|
+
* @name rejectSeries
|
|
71424
|
+
* @static
|
|
71425
|
+
* @memberOf module:Collections
|
|
71426
|
+
* @method
|
|
71427
|
+
* @see [async.reject]{@link module:Collections.reject}
|
|
71428
|
+
* @category Collection
|
|
71429
|
+
* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|
71430
|
+
* @param {Function} iteratee - An async truth test to apply to each item in
|
|
71431
|
+
* `coll`.
|
|
71432
|
+
* The should complete with a boolean value as its `result`.
|
|
71433
|
+
* Invoked with (item, callback).
|
|
71434
|
+
* @param {Function} [callback] - A callback which is called after all the
|
|
71435
|
+
* `iteratee` functions have finished. Invoked with (err, results).
|
|
71436
|
+
* @returns {Promise} a promise, if no callback is passed
|
|
71437
|
+
*/
|
|
71438
|
+
function rejectSeries (coll, iteratee, callback) {
|
|
71439
|
+
return reject$2(eachOfSeries$1, coll, iteratee, callback)
|
|
71440
|
+
}
|
|
71441
|
+
awaitify(rejectSeries, 3);
|
|
71442
|
+
|
|
71443
|
+
/**
|
|
71444
|
+
* Returns `true` if at least one element in the `coll` satisfies an async test.
|
|
71445
|
+
* If any iteratee call returns `true`, the main `callback` is immediately
|
|
71446
|
+
* called.
|
|
71447
|
+
*
|
|
71448
|
+
* @name some
|
|
71449
|
+
* @static
|
|
71450
|
+
* @memberOf module:Collections
|
|
71451
|
+
* @method
|
|
71452
|
+
* @alias any
|
|
71453
|
+
* @category Collection
|
|
71454
|
+
* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|
71455
|
+
* @param {AsyncFunction} iteratee - An async truth test to apply to each item
|
|
71456
|
+
* in the collections in parallel.
|
|
71457
|
+
* The iteratee should complete with a boolean `result` value.
|
|
71458
|
+
* Invoked with (item, callback).
|
|
71459
|
+
* @param {Function} [callback] - A callback which is called as soon as any
|
|
71460
|
+
* iteratee returns `true`, or after all the iteratee functions have finished.
|
|
71461
|
+
* Result will be either `true` or `false` depending on the values of the async
|
|
71462
|
+
* tests. Invoked with (err, result).
|
|
71463
|
+
* @returns {Promise} a promise, if no callback provided
|
|
71464
|
+
* @example
|
|
71465
|
+
*
|
|
71466
|
+
* // dir1 is a directory that contains file1.txt, file2.txt
|
|
71467
|
+
* // dir2 is a directory that contains file3.txt, file4.txt
|
|
71468
|
+
* // dir3 is a directory that contains file5.txt
|
|
71469
|
+
* // dir4 does not exist
|
|
71470
|
+
*
|
|
71471
|
+
* // asynchronous function that checks if a file exists
|
|
71472
|
+
* function fileExists(file, callback) {
|
|
71473
|
+
* fs.access(file, fs.constants.F_OK, (err) => {
|
|
71474
|
+
* callback(null, !err);
|
|
71475
|
+
* });
|
|
71476
|
+
* }
|
|
71477
|
+
*
|
|
71478
|
+
* // Using callbacks
|
|
71479
|
+
* async.some(['dir1/missing.txt','dir2/missing.txt','dir3/file5.txt'], fileExists,
|
|
71480
|
+
* function(err, result) {
|
|
71481
|
+
* console.log(result);
|
|
71482
|
+
* // true
|
|
71483
|
+
* // result is true since some file in the list exists
|
|
71484
|
+
* }
|
|
71485
|
+
*);
|
|
71486
|
+
*
|
|
71487
|
+
* async.some(['dir1/missing.txt','dir2/missing.txt','dir4/missing.txt'], fileExists,
|
|
71488
|
+
* function(err, result) {
|
|
71489
|
+
* console.log(result);
|
|
71490
|
+
* // false
|
|
71491
|
+
* // result is false since none of the files exists
|
|
71492
|
+
* }
|
|
71493
|
+
*);
|
|
71494
|
+
*
|
|
71495
|
+
* // Using Promises
|
|
71496
|
+
* async.some(['dir1/missing.txt','dir2/missing.txt','dir3/file5.txt'], fileExists)
|
|
71497
|
+
* .then( result => {
|
|
71498
|
+
* console.log(result);
|
|
71499
|
+
* // true
|
|
71500
|
+
* // result is true since some file in the list exists
|
|
71501
|
+
* }).catch( err => {
|
|
71502
|
+
* console.log(err);
|
|
71503
|
+
* });
|
|
71504
|
+
*
|
|
71505
|
+
* async.some(['dir1/missing.txt','dir2/missing.txt','dir4/missing.txt'], fileExists)
|
|
71506
|
+
* .then( result => {
|
|
71507
|
+
* console.log(result);
|
|
71508
|
+
* // false
|
|
71509
|
+
* // result is false since none of the files exists
|
|
71510
|
+
* }).catch( err => {
|
|
71511
|
+
* console.log(err);
|
|
71512
|
+
* });
|
|
71513
|
+
*
|
|
71514
|
+
* // Using async/await
|
|
71515
|
+
* async () => {
|
|
71516
|
+
* try {
|
|
71517
|
+
* let result = await async.some(['dir1/missing.txt','dir2/missing.txt','dir3/file5.txt'], fileExists);
|
|
71518
|
+
* console.log(result);
|
|
71519
|
+
* // true
|
|
71520
|
+
* // result is true since some file in the list exists
|
|
71521
|
+
* }
|
|
71522
|
+
* catch (err) {
|
|
71523
|
+
* console.log(err);
|
|
71524
|
+
* }
|
|
71525
|
+
* }
|
|
71526
|
+
*
|
|
71527
|
+
* async () => {
|
|
71528
|
+
* try {
|
|
71529
|
+
* let result = await async.some(['dir1/missing.txt','dir2/missing.txt','dir4/missing.txt'], fileExists);
|
|
71530
|
+
* console.log(result);
|
|
71531
|
+
* // false
|
|
71532
|
+
* // result is false since none of the files exists
|
|
71533
|
+
* }
|
|
71534
|
+
* catch (err) {
|
|
71535
|
+
* console.log(err);
|
|
71536
|
+
* }
|
|
71537
|
+
* }
|
|
71538
|
+
*
|
|
71539
|
+
*/
|
|
71540
|
+
function some(coll, iteratee, callback) {
|
|
71541
|
+
return _createTester(Boolean, res => res)(eachOf$1, coll, iteratee, callback)
|
|
71542
|
+
}
|
|
71543
|
+
awaitify(some, 3);
|
|
71544
|
+
|
|
71545
|
+
/**
|
|
71546
|
+
* The same as [`some`]{@link module:Collections.some} but runs a maximum of `limit` async operations at a time.
|
|
71547
|
+
*
|
|
71548
|
+
* @name someLimit
|
|
71549
|
+
* @static
|
|
71550
|
+
* @memberOf module:Collections
|
|
71551
|
+
* @method
|
|
71552
|
+
* @see [async.some]{@link module:Collections.some}
|
|
71553
|
+
* @alias anyLimit
|
|
71554
|
+
* @category Collection
|
|
71555
|
+
* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|
71556
|
+
* @param {number} limit - The maximum number of async operations at a time.
|
|
71557
|
+
* @param {AsyncFunction} iteratee - An async truth test to apply to each item
|
|
71558
|
+
* in the collections in parallel.
|
|
71559
|
+
* The iteratee should complete with a boolean `result` value.
|
|
71560
|
+
* Invoked with (item, callback).
|
|
71561
|
+
* @param {Function} [callback] - A callback which is called as soon as any
|
|
71562
|
+
* iteratee returns `true`, or after all the iteratee functions have finished.
|
|
71563
|
+
* Result will be either `true` or `false` depending on the values of the async
|
|
71564
|
+
* tests. Invoked with (err, result).
|
|
71565
|
+
* @returns {Promise} a promise, if no callback provided
|
|
71566
|
+
*/
|
|
71567
|
+
function someLimit(coll, limit, iteratee, callback) {
|
|
71568
|
+
return _createTester(Boolean, res => res)(eachOfLimit$2(limit), coll, iteratee, callback)
|
|
71569
|
+
}
|
|
71570
|
+
awaitify(someLimit, 4);
|
|
71571
|
+
|
|
71572
|
+
/**
|
|
71573
|
+
* The same as [`some`]{@link module:Collections.some} but runs only a single async operation at a time.
|
|
71574
|
+
*
|
|
71575
|
+
* @name someSeries
|
|
71576
|
+
* @static
|
|
71577
|
+
* @memberOf module:Collections
|
|
71578
|
+
* @method
|
|
71579
|
+
* @see [async.some]{@link module:Collections.some}
|
|
71580
|
+
* @alias anySeries
|
|
71581
|
+
* @category Collection
|
|
71582
|
+
* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|
71583
|
+
* @param {AsyncFunction} iteratee - An async truth test to apply to each item
|
|
71584
|
+
* in the collections in series.
|
|
71585
|
+
* The iteratee should complete with a boolean `result` value.
|
|
71586
|
+
* Invoked with (item, callback).
|
|
71587
|
+
* @param {Function} [callback] - A callback which is called as soon as any
|
|
71588
|
+
* iteratee returns `true`, or after all the iteratee functions have finished.
|
|
71589
|
+
* Result will be either `true` or `false` depending on the values of the async
|
|
71590
|
+
* tests. Invoked with (err, result).
|
|
71591
|
+
* @returns {Promise} a promise, if no callback provided
|
|
71592
|
+
*/
|
|
71593
|
+
function someSeries(coll, iteratee, callback) {
|
|
71594
|
+
return _createTester(Boolean, res => res)(eachOfSeries$1, coll, iteratee, callback)
|
|
71595
|
+
}
|
|
71596
|
+
awaitify(someSeries, 3);
|
|
71597
|
+
|
|
71598
|
+
/**
|
|
71599
|
+
* Sorts a list by the results of running each `coll` value through an async
|
|
71600
|
+
* `iteratee`.
|
|
71601
|
+
*
|
|
71602
|
+
* @name sortBy
|
|
71603
|
+
* @static
|
|
71604
|
+
* @memberOf module:Collections
|
|
71605
|
+
* @method
|
|
71606
|
+
* @category Collection
|
|
71607
|
+
* @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
|
71608
|
+
* @param {AsyncFunction} iteratee - An async function to apply to each item in
|
|
71609
|
+
* `coll`.
|
|
71610
|
+
* The iteratee should complete with a value to use as the sort criteria as
|
|
71611
|
+
* its `result`.
|
|
71612
|
+
* Invoked with (item, callback).
|
|
71613
|
+
* @param {Function} callback - A callback which is called after all the
|
|
71614
|
+
* `iteratee` functions have finished, or an error occurs. Results is the items
|
|
71615
|
+
* from the original `coll` sorted by the values returned by the `iteratee`
|
|
71616
|
+
* calls. Invoked with (err, results).
|
|
71617
|
+
* @returns {Promise} a promise, if no callback passed
|
|
71618
|
+
* @example
|
|
71619
|
+
*
|
|
71620
|
+
* // bigfile.txt is a file that is 251100 bytes in size
|
|
71621
|
+
* // mediumfile.txt is a file that is 11000 bytes in size
|
|
71622
|
+
* // smallfile.txt is a file that is 121 bytes in size
|
|
71623
|
+
*
|
|
71624
|
+
* // asynchronous function that returns the file size in bytes
|
|
71625
|
+
* function getFileSizeInBytes(file, callback) {
|
|
71626
|
+
* fs.stat(file, function(err, stat) {
|
|
71627
|
+
* if (err) {
|
|
71628
|
+
* return callback(err);
|
|
71629
|
+
* }
|
|
71630
|
+
* callback(null, stat.size);
|
|
71631
|
+
* });
|
|
71632
|
+
* }
|
|
71633
|
+
*
|
|
71634
|
+
* // Using callbacks
|
|
71635
|
+
* async.sortBy(['mediumfile.txt','smallfile.txt','bigfile.txt'], getFileSizeInBytes,
|
|
71636
|
+
* function(err, results) {
|
|
71637
|
+
* if (err) {
|
|
71638
|
+
* console.log(err);
|
|
71639
|
+
* } else {
|
|
71640
|
+
* console.log(results);
|
|
71641
|
+
* // results is now the original array of files sorted by
|
|
71642
|
+
* // file size (ascending by default), e.g.
|
|
71643
|
+
* // [ 'smallfile.txt', 'mediumfile.txt', 'bigfile.txt']
|
|
71644
|
+
* }
|
|
71645
|
+
* }
|
|
71646
|
+
* );
|
|
71647
|
+
*
|
|
71648
|
+
* // By modifying the callback parameter the
|
|
71649
|
+
* // sorting order can be influenced:
|
|
71650
|
+
*
|
|
71651
|
+
* // ascending order
|
|
71652
|
+
* async.sortBy(['mediumfile.txt','smallfile.txt','bigfile.txt'], function(file, callback) {
|
|
71653
|
+
* getFileSizeInBytes(file, function(getFileSizeErr, fileSize) {
|
|
71654
|
+
* if (getFileSizeErr) return callback(getFileSizeErr);
|
|
71655
|
+
* callback(null, fileSize);
|
|
71656
|
+
* });
|
|
71657
|
+
* }, function(err, results) {
|
|
71658
|
+
* if (err) {
|
|
71659
|
+
* console.log(err);
|
|
71660
|
+
* } else {
|
|
71661
|
+
* console.log(results);
|
|
71662
|
+
* // results is now the original array of files sorted by
|
|
71663
|
+
* // file size (ascending by default), e.g.
|
|
71664
|
+
* // [ 'smallfile.txt', 'mediumfile.txt', 'bigfile.txt']
|
|
71665
|
+
* }
|
|
71666
|
+
* }
|
|
71667
|
+
* );
|
|
71668
|
+
*
|
|
71669
|
+
* // descending order
|
|
71670
|
+
* async.sortBy(['bigfile.txt','mediumfile.txt','smallfile.txt'], function(file, callback) {
|
|
71671
|
+
* getFileSizeInBytes(file, function(getFileSizeErr, fileSize) {
|
|
71672
|
+
* if (getFileSizeErr) {
|
|
71673
|
+
* return callback(getFileSizeErr);
|
|
71674
|
+
* }
|
|
71675
|
+
* callback(null, fileSize * -1);
|
|
71676
|
+
* });
|
|
71677
|
+
* }, function(err, results) {
|
|
71678
|
+
* if (err) {
|
|
71679
|
+
* console.log(err);
|
|
71680
|
+
* } else {
|
|
71681
|
+
* console.log(results);
|
|
71682
|
+
* // results is now the original array of files sorted by
|
|
71683
|
+
* // file size (ascending by default), e.g.
|
|
71684
|
+
* // [ 'bigfile.txt', 'mediumfile.txt', 'smallfile.txt']
|
|
71685
|
+
* }
|
|
71686
|
+
* }
|
|
71687
|
+
* );
|
|
71688
|
+
*
|
|
71689
|
+
* // Error handling
|
|
71690
|
+
* async.sortBy(['mediumfile.txt','smallfile.txt','missingfile.txt'], getFileSizeInBytes,
|
|
71691
|
+
* function(err, results) {
|
|
71692
|
+
* if (err) {
|
|
71693
|
+
* console.log(err);
|
|
71694
|
+
* // [ Error: ENOENT: no such file or directory ]
|
|
71695
|
+
* } else {
|
|
71696
|
+
* console.log(results);
|
|
71697
|
+
* }
|
|
71698
|
+
* }
|
|
71699
|
+
* );
|
|
71700
|
+
*
|
|
71701
|
+
* // Using Promises
|
|
71702
|
+
* async.sortBy(['mediumfile.txt','smallfile.txt','bigfile.txt'], getFileSizeInBytes)
|
|
71703
|
+
* .then( results => {
|
|
71704
|
+
* console.log(results);
|
|
71705
|
+
* // results is now the original array of files sorted by
|
|
71706
|
+
* // file size (ascending by default), e.g.
|
|
71707
|
+
* // [ 'smallfile.txt', 'mediumfile.txt', 'bigfile.txt']
|
|
71708
|
+
* }).catch( err => {
|
|
71709
|
+
* console.log(err);
|
|
71710
|
+
* });
|
|
71711
|
+
*
|
|
71712
|
+
* // Error handling
|
|
71713
|
+
* async.sortBy(['mediumfile.txt','smallfile.txt','missingfile.txt'], getFileSizeInBytes)
|
|
71714
|
+
* .then( results => {
|
|
71715
|
+
* console.log(results);
|
|
71716
|
+
* }).catch( err => {
|
|
71717
|
+
* console.log(err);
|
|
71718
|
+
* // [ Error: ENOENT: no such file or directory ]
|
|
71719
|
+
* });
|
|
71720
|
+
*
|
|
71721
|
+
* // Using async/await
|
|
71722
|
+
* (async () => {
|
|
71723
|
+
* try {
|
|
71724
|
+
* let results = await async.sortBy(['bigfile.txt','mediumfile.txt','smallfile.txt'], getFileSizeInBytes);
|
|
71725
|
+
* console.log(results);
|
|
71726
|
+
* // results is now the original array of files sorted by
|
|
71727
|
+
* // file size (ascending by default), e.g.
|
|
71728
|
+
* // [ 'smallfile.txt', 'mediumfile.txt', 'bigfile.txt']
|
|
71729
|
+
* }
|
|
71730
|
+
* catch (err) {
|
|
71731
|
+
* console.log(err);
|
|
71732
|
+
* }
|
|
71733
|
+
* })();
|
|
71734
|
+
*
|
|
71735
|
+
* // Error handling
|
|
71736
|
+
* async () => {
|
|
71737
|
+
* try {
|
|
71738
|
+
* let results = await async.sortBy(['missingfile.txt','mediumfile.txt','smallfile.txt'], getFileSizeInBytes);
|
|
71739
|
+
* console.log(results);
|
|
71740
|
+
* }
|
|
71741
|
+
* catch (err) {
|
|
71742
|
+
* console.log(err);
|
|
71743
|
+
* // [ Error: ENOENT: no such file or directory ]
|
|
71744
|
+
* }
|
|
71745
|
+
* }
|
|
71746
|
+
*
|
|
71747
|
+
*/
|
|
71748
|
+
function sortBy (coll, iteratee, callback) {
|
|
71749
|
+
var _iteratee = wrapAsync(iteratee);
|
|
71750
|
+
return map$1(coll, (x, iterCb) => {
|
|
71751
|
+
_iteratee(x, (err, criteria) => {
|
|
71752
|
+
if (err) return iterCb(err);
|
|
71753
|
+
iterCb(err, {value: x, criteria});
|
|
71754
|
+
});
|
|
71755
|
+
}, (err, results) => {
|
|
71756
|
+
if (err) return callback(err);
|
|
71757
|
+
callback(null, results.sort(comparator).map(v => v.value));
|
|
71758
|
+
});
|
|
71759
|
+
|
|
71760
|
+
function comparator(left, right) {
|
|
71761
|
+
var a = left.criteria, b = right.criteria;
|
|
71762
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
71763
|
+
}
|
|
71764
|
+
}
|
|
71765
|
+
awaitify(sortBy, 3);
|
|
71766
|
+
|
|
71767
|
+
/**
|
|
71768
|
+
* It runs each task in series but stops whenever any of the functions were
|
|
71769
|
+
* successful. If one of the tasks were successful, the `callback` will be
|
|
71770
|
+
* passed the result of the successful task. If all tasks fail, the callback
|
|
71771
|
+
* will be passed the error and result (if any) of the final attempt.
|
|
71772
|
+
*
|
|
71773
|
+
* @name tryEach
|
|
71774
|
+
* @static
|
|
71775
|
+
* @memberOf module:ControlFlow
|
|
71776
|
+
* @method
|
|
71777
|
+
* @category Control Flow
|
|
71778
|
+
* @param {Array|Iterable|AsyncIterable|Object} tasks - A collection containing functions to
|
|
71779
|
+
* run, each function is passed a `callback(err, result)` it must call on
|
|
71780
|
+
* completion with an error `err` (which can be `null`) and an optional `result`
|
|
71781
|
+
* value.
|
|
71782
|
+
* @param {Function} [callback] - An optional callback which is called when one
|
|
71783
|
+
* of the tasks has succeeded, or all have failed. It receives the `err` and
|
|
71784
|
+
* `result` arguments of the last attempt at completing the `task`. Invoked with
|
|
71785
|
+
* (err, results).
|
|
71786
|
+
* @returns {Promise} a promise, if no callback is passed
|
|
71787
|
+
* @example
|
|
71788
|
+
* async.tryEach([
|
|
71789
|
+
* function getDataFromFirstWebsite(callback) {
|
|
71790
|
+
* // Try getting the data from the first website
|
|
71791
|
+
* callback(err, data);
|
|
71792
|
+
* },
|
|
71793
|
+
* function getDataFromSecondWebsite(callback) {
|
|
71794
|
+
* // First website failed,
|
|
71795
|
+
* // Try getting the data from the backup website
|
|
71796
|
+
* callback(err, data);
|
|
71797
|
+
* }
|
|
71798
|
+
* ],
|
|
71799
|
+
* // optional callback
|
|
71800
|
+
* function(err, results) {
|
|
71801
|
+
* Now do something with the data.
|
|
71802
|
+
* });
|
|
71803
|
+
*
|
|
71804
|
+
*/
|
|
71805
|
+
function tryEach(tasks, callback) {
|
|
71806
|
+
var error = null;
|
|
71807
|
+
var result;
|
|
71808
|
+
return eachSeries$1(tasks, (task, taskCb) => {
|
|
71809
|
+
wrapAsync(task)((err, ...args) => {
|
|
71810
|
+
if (err === false) return taskCb(err);
|
|
71811
|
+
|
|
71812
|
+
if (args.length < 2) {
|
|
71813
|
+
[result] = args;
|
|
71814
|
+
} else {
|
|
71815
|
+
result = args;
|
|
71816
|
+
}
|
|
71817
|
+
error = err;
|
|
71818
|
+
taskCb(err ? null : {});
|
|
71819
|
+
});
|
|
71820
|
+
}, () => callback(error, result));
|
|
71821
|
+
}
|
|
71822
|
+
|
|
71823
|
+
awaitify(tryEach);
|
|
71824
|
+
|
|
71825
|
+
/**
|
|
71826
|
+
* Repeatedly call `iteratee`, while `test` returns `true`. Calls `callback` when
|
|
71827
|
+
* stopped, or an error occurs.
|
|
71828
|
+
*
|
|
71829
|
+
* @name whilst
|
|
71830
|
+
* @static
|
|
71831
|
+
* @memberOf module:ControlFlow
|
|
71832
|
+
* @method
|
|
71833
|
+
* @category Control Flow
|
|
71834
|
+
* @param {AsyncFunction} test - asynchronous truth test to perform before each
|
|
71835
|
+
* execution of `iteratee`. Invoked with (callback).
|
|
71836
|
+
* @param {AsyncFunction} iteratee - An async function which is called each time
|
|
71837
|
+
* `test` passes. Invoked with (callback).
|
|
71838
|
+
* @param {Function} [callback] - A callback which is called after the test
|
|
71839
|
+
* function has failed and repeated execution of `iteratee` has stopped. `callback`
|
|
71840
|
+
* will be passed an error and any arguments passed to the final `iteratee`'s
|
|
71841
|
+
* callback. Invoked with (err, [results]);
|
|
71842
|
+
* @returns {Promise} a promise, if no callback is passed
|
|
71843
|
+
* @example
|
|
71844
|
+
*
|
|
71845
|
+
* var count = 0;
|
|
71846
|
+
* async.whilst(
|
|
71847
|
+
* function test(cb) { cb(null, count < 5); },
|
|
71848
|
+
* function iter(callback) {
|
|
71849
|
+
* count++;
|
|
71850
|
+
* setTimeout(function() {
|
|
71851
|
+
* callback(null, count);
|
|
71852
|
+
* }, 1000);
|
|
71853
|
+
* },
|
|
71854
|
+
* function (err, n) {
|
|
71855
|
+
* // 5 seconds have passed, n = 5
|
|
71856
|
+
* }
|
|
71857
|
+
* );
|
|
71858
|
+
*/
|
|
71859
|
+
function whilst(test, iteratee, callback) {
|
|
71860
|
+
callback = onlyOnce(callback);
|
|
71861
|
+
var _fn = wrapAsync(iteratee);
|
|
71862
|
+
var _test = wrapAsync(test);
|
|
71863
|
+
var results = [];
|
|
71864
|
+
|
|
71865
|
+
function next(err, ...rest) {
|
|
71866
|
+
if (err) return callback(err);
|
|
71867
|
+
results = rest;
|
|
71868
|
+
if (err === false) return;
|
|
71869
|
+
_test(check);
|
|
71870
|
+
}
|
|
71871
|
+
|
|
71872
|
+
function check(err, truth) {
|
|
71873
|
+
if (err) return callback(err);
|
|
71874
|
+
if (err === false) return;
|
|
71875
|
+
if (!truth) return callback(null, ...results);
|
|
71876
|
+
_fn(next);
|
|
71877
|
+
}
|
|
71878
|
+
|
|
71879
|
+
return _test(check);
|
|
71880
|
+
}
|
|
71881
|
+
awaitify(whilst, 3);
|
|
71882
|
+
|
|
71883
|
+
/**
|
|
71884
|
+
* Runs the `tasks` array of functions in series, each passing their results to
|
|
71885
|
+
* the next in the array. However, if any of the `tasks` pass an error to their
|
|
71886
|
+
* own callback, the next function is not executed, and the main `callback` is
|
|
71887
|
+
* immediately called with the error.
|
|
71888
|
+
*
|
|
71889
|
+
* @name waterfall
|
|
71890
|
+
* @static
|
|
71891
|
+
* @memberOf module:ControlFlow
|
|
71892
|
+
* @method
|
|
71893
|
+
* @category Control Flow
|
|
71894
|
+
* @param {Array} tasks - An array of [async functions]{@link AsyncFunction}
|
|
71895
|
+
* to run.
|
|
71896
|
+
* Each function should complete with any number of `result` values.
|
|
71897
|
+
* The `result` values will be passed as arguments, in order, to the next task.
|
|
71898
|
+
* @param {Function} [callback] - An optional callback to run once all the
|
|
71899
|
+
* functions have completed. This will be passed the results of the last task's
|
|
71900
|
+
* callback. Invoked with (err, [results]).
|
|
71901
|
+
* @returns {Promise} a promise, if a callback is omitted
|
|
71902
|
+
* @example
|
|
71903
|
+
*
|
|
71904
|
+
* async.waterfall([
|
|
71905
|
+
* function(callback) {
|
|
71906
|
+
* callback(null, 'one', 'two');
|
|
71907
|
+
* },
|
|
71908
|
+
* function(arg1, arg2, callback) {
|
|
71909
|
+
* // arg1 now equals 'one' and arg2 now equals 'two'
|
|
71910
|
+
* callback(null, 'three');
|
|
71911
|
+
* },
|
|
71912
|
+
* function(arg1, callback) {
|
|
71913
|
+
* // arg1 now equals 'three'
|
|
71914
|
+
* callback(null, 'done');
|
|
71915
|
+
* }
|
|
71916
|
+
* ], function (err, result) {
|
|
71917
|
+
* // result now equals 'done'
|
|
71918
|
+
* });
|
|
71919
|
+
*
|
|
71920
|
+
* // Or, with named functions:
|
|
71921
|
+
* async.waterfall([
|
|
71922
|
+
* myFirstFunction,
|
|
71923
|
+
* mySecondFunction,
|
|
71924
|
+
* myLastFunction,
|
|
71925
|
+
* ], function (err, result) {
|
|
71926
|
+
* // result now equals 'done'
|
|
71927
|
+
* });
|
|
71928
|
+
* function myFirstFunction(callback) {
|
|
71929
|
+
* callback(null, 'one', 'two');
|
|
71930
|
+
* }
|
|
71931
|
+
* function mySecondFunction(arg1, arg2, callback) {
|
|
71932
|
+
* // arg1 now equals 'one' and arg2 now equals 'two'
|
|
71933
|
+
* callback(null, 'three');
|
|
71934
|
+
* }
|
|
71935
|
+
* function myLastFunction(arg1, callback) {
|
|
71936
|
+
* // arg1 now equals 'three'
|
|
71937
|
+
* callback(null, 'done');
|
|
71938
|
+
* }
|
|
71939
|
+
*/
|
|
71940
|
+
function waterfall (tasks, callback) {
|
|
71941
|
+
callback = once(callback);
|
|
71942
|
+
if (!Array.isArray(tasks)) return callback(new Error('First argument to waterfall must be an array of functions'));
|
|
71943
|
+
if (!tasks.length) return callback();
|
|
71944
|
+
var taskIndex = 0;
|
|
71945
|
+
|
|
71946
|
+
function nextTask(args) {
|
|
71947
|
+
var task = wrapAsync(tasks[taskIndex++]);
|
|
71948
|
+
task(...args, onlyOnce(next));
|
|
71949
|
+
}
|
|
71950
|
+
|
|
71951
|
+
function next(err, ...args) {
|
|
71952
|
+
if (err === false) return
|
|
71953
|
+
if (err || taskIndex === tasks.length) {
|
|
71954
|
+
return callback(err, ...args);
|
|
71955
|
+
}
|
|
71956
|
+
nextTask(args);
|
|
71957
|
+
}
|
|
71958
|
+
|
|
71959
|
+
nextTask([]);
|
|
71960
|
+
}
|
|
71961
|
+
|
|
71962
|
+
awaitify(waterfall);
|
|
71963
|
+
|
|
69283
71964
|
var LogLevel;
|
|
69284
71965
|
(function (LogLevel) {
|
|
69285
71966
|
LogLevel[LogLevel["DEBUG"] = 0] = "DEBUG";
|
|
@@ -69365,6 +72046,7 @@ class AccountService {
|
|
|
69365
72046
|
dataService;
|
|
69366
72047
|
account;
|
|
69367
72048
|
logger;
|
|
72049
|
+
poolConcurrency;
|
|
69368
72050
|
/**
|
|
69369
72051
|
* Creates a new AccountService instance.
|
|
69370
72052
|
*
|
|
@@ -69372,12 +72054,14 @@ class AccountService {
|
|
|
69372
72054
|
* @param config - Configuration for the account service (either mnemonic or existing account)
|
|
69373
72055
|
* @param config.mnemonic - Optional mnemonic for deterministic key generation
|
|
69374
72056
|
* @param config.account - Optional existing account to initialize with
|
|
72057
|
+
* @param config.poolConcurrency - Optional maximum number of pools to fetch events for concurrently (default: 2)
|
|
69375
72058
|
*
|
|
69376
72059
|
* @throws {AccountError} If account initialization fails
|
|
69377
72060
|
*/
|
|
69378
72061
|
constructor(dataService, config) {
|
|
69379
72062
|
this.dataService = dataService;
|
|
69380
|
-
this.logger = new Logger({ prefix: "Account"
|
|
72063
|
+
this.logger = new Logger({ prefix: "Account" });
|
|
72064
|
+
this.poolConcurrency = config.poolConcurrency ?? 2;
|
|
69381
72065
|
if ("mnemonic" in config) {
|
|
69382
72066
|
this.account = this._initializeAccount(config.mnemonic);
|
|
69383
72067
|
}
|
|
@@ -69592,12 +72276,6 @@ class AccountService {
|
|
|
69592
72276
|
addPoolAccount(scope, value, nullifier, secret, label, blockNumber, txHash) {
|
|
69593
72277
|
const precommitment = this._hashPrecommitment(nullifier, secret);
|
|
69594
72278
|
const commitment = this._hashCommitment(value, label, precommitment);
|
|
69595
|
-
this.logger.debug(`Commitment calculation details`, {
|
|
69596
|
-
value,
|
|
69597
|
-
label,
|
|
69598
|
-
precommitment,
|
|
69599
|
-
commitment,
|
|
69600
|
-
});
|
|
69601
72279
|
const newAccount = {
|
|
69602
72280
|
label,
|
|
69603
72281
|
deposit: {
|
|
@@ -69663,7 +72341,6 @@ class AccountService {
|
|
|
69663
72341
|
blockNumber,
|
|
69664
72342
|
txHash,
|
|
69665
72343
|
};
|
|
69666
|
-
this.logger.debug(`adding the following commitment`, { precommitment, hash: this._hashCommitment(value, parentCommitment.label, precommitment), });
|
|
69667
72344
|
foundAccount.children.push(newCommitment);
|
|
69668
72345
|
this.logger.info(`Added new commitment with value ${value} to account with label ${parentCommitment.label}`);
|
|
69669
72346
|
return newCommitment;
|
|
@@ -69779,24 +72456,36 @@ class AccountService {
|
|
|
69779
72456
|
*/
|
|
69780
72457
|
async getEvents(pools) {
|
|
69781
72458
|
const events = new Map();
|
|
69782
|
-
|
|
69783
|
-
|
|
69784
|
-
|
|
69785
|
-
|
|
69786
|
-
|
|
69787
|
-
|
|
69788
|
-
|
|
69789
|
-
|
|
69790
|
-
|
|
69791
|
-
|
|
69792
|
-
|
|
69793
|
-
|
|
69794
|
-
|
|
69795
|
-
|
|
69796
|
-
|
|
69797
|
-
|
|
69798
|
-
|
|
69799
|
-
|
|
72459
|
+
// Use mapLimit to control concurrency at pool level
|
|
72460
|
+
const poolEventResults = await mapLimit$1(pools, this.poolConcurrency, async (pool) => {
|
|
72461
|
+
try {
|
|
72462
|
+
this.logger.info(`Fetching events for pool`, {
|
|
72463
|
+
poolAddress: pool.address,
|
|
72464
|
+
poolChainId: pool.chainId,
|
|
72465
|
+
poolDeploymentBlock: pool.deploymentBlock,
|
|
72466
|
+
});
|
|
72467
|
+
const [depositEvents, withdrawalEvents, ragequitEvents] = await Promise.all([
|
|
72468
|
+
this.getDepositEvents(pool),
|
|
72469
|
+
this.getWithdrawalEvents(pool),
|
|
72470
|
+
this.getRagequitEvents(pool),
|
|
72471
|
+
]);
|
|
72472
|
+
return {
|
|
72473
|
+
status: "fulfilled",
|
|
72474
|
+
value: {
|
|
72475
|
+
scope: pool.scope,
|
|
72476
|
+
depositEvents,
|
|
72477
|
+
withdrawalEvents,
|
|
72478
|
+
ragequitEvents,
|
|
72479
|
+
},
|
|
72480
|
+
};
|
|
72481
|
+
}
|
|
72482
|
+
catch (error) {
|
|
72483
|
+
return {
|
|
72484
|
+
status: "rejected",
|
|
72485
|
+
reason: error,
|
|
72486
|
+
};
|
|
72487
|
+
}
|
|
72488
|
+
});
|
|
69800
72489
|
for (const result of poolEventResults) {
|
|
69801
72490
|
if (result.status === "fulfilled") {
|
|
69802
72491
|
const { scope, depositEvents, withdrawalEvents, ragequitEvents } = result.value;
|
|
@@ -70162,15 +72851,24 @@ class DataService {
|
|
|
70162
72851
|
chainConfigs;
|
|
70163
72852
|
clients = new Map();
|
|
70164
72853
|
logger;
|
|
72854
|
+
logFetchConfigs;
|
|
70165
72855
|
/**
|
|
70166
72856
|
* Initialize the data service with chain configurations
|
|
70167
72857
|
*
|
|
70168
72858
|
* @param chainConfigs - Array of chain configurations containing chainId, RPC URL, and API key
|
|
72859
|
+
* @param logFetchConfig - Per-chain configuration for rate-limited log fetching as a Map<chainId, config>.
|
|
72860
|
+
* Each chain can have its own specific settings (e.g., different block chunk sizes).
|
|
70169
72861
|
* @throws {DataError} If client initialization fails for any chain
|
|
70170
72862
|
*/
|
|
70171
|
-
constructor(chainConfigs) {
|
|
72863
|
+
constructor(chainConfigs, logFetchConfig = new Map()) {
|
|
70172
72864
|
this.chainConfigs = chainConfigs;
|
|
70173
|
-
this.logger = new Logger({ prefix: "Data" });
|
|
72865
|
+
this.logger = new Logger({ prefix: "Data", level: LogLevel.DEBUG });
|
|
72866
|
+
// Initialize per-chain configs with defaults merged with chain-specific overrides
|
|
72867
|
+
this.logFetchConfigs = new Map();
|
|
72868
|
+
for (const config of chainConfigs) {
|
|
72869
|
+
const chainSpecificConfig = logFetchConfig.get(config.chainId);
|
|
72870
|
+
this.logFetchConfigs.set(config.chainId, { ...DEFAULT_LOG_FETCH_CONFIG, ...chainSpecificConfig });
|
|
72871
|
+
}
|
|
70174
72872
|
try {
|
|
70175
72873
|
for (const config of chainConfigs) {
|
|
70176
72874
|
if (!config.rpcUrl) {
|
|
@@ -70189,29 +72887,41 @@ class DataService {
|
|
|
70189
72887
|
/**
|
|
70190
72888
|
* Get deposit events for a specific chain
|
|
70191
72889
|
*
|
|
70192
|
-
* @param
|
|
70193
|
-
* @param options - Event filter options including fromBlock, toBlock, and other filters
|
|
72890
|
+
* @param pool - Pool info containing chainId, address, and deployment block
|
|
70194
72891
|
* @returns Array of deposit events with properly typed fields (bigint for numbers, Hash for commitments)
|
|
70195
72892
|
* @throws {DataError} If client is not configured, network error occurs, or event data is invalid
|
|
70196
72893
|
*/
|
|
70197
72894
|
async getDeposits(pool) {
|
|
70198
72895
|
try {
|
|
70199
72896
|
const client = this.getClientForChain(pool.chainId);
|
|
70200
|
-
const
|
|
70201
|
-
const
|
|
70202
|
-
|
|
70203
|
-
|
|
70204
|
-
|
|
70205
|
-
|
|
70206
|
-
|
|
72897
|
+
const chainConfig = this.getConfigForChain(pool.chainId);
|
|
72898
|
+
const logConfig = this.getLogFetchConfigForChain(pool.chainId);
|
|
72899
|
+
const fromBlock = pool.deploymentBlock ?? chainConfig.startBlock;
|
|
72900
|
+
const toBlock = await this.getCurrentBlock(pool.chainId);
|
|
72901
|
+
const ranges = this.generateBlockRanges(fromBlock, toBlock, logConfig.blockChunkSize);
|
|
72902
|
+
this.logger.info(`Fetching deposits in ${ranges.length} chunks for pool ${pool.address}, chunk size is: ${logConfig.blockChunkSize}`);
|
|
72903
|
+
// Use async.mapLimit for controlled concurrency
|
|
72904
|
+
const allLogs = await mapLimit$1(ranges, logConfig.concurrency, async (range) => {
|
|
72905
|
+
if (logConfig.chunkDelayMs > 0) {
|
|
72906
|
+
await this.sleep(logConfig.chunkDelayMs);
|
|
72907
|
+
}
|
|
72908
|
+
return this.fetchLogsWithRetry(client, pool.address, DEPOSIT_EVENT, range, logConfig);
|
|
70207
72909
|
});
|
|
70208
|
-
|
|
72910
|
+
// Flatten and parse results
|
|
72911
|
+
const flatLogs = allLogs.flat();
|
|
72912
|
+
return flatLogs.map((log) => {
|
|
70209
72913
|
try {
|
|
70210
|
-
|
|
72914
|
+
const typedLog = log;
|
|
72915
|
+
if (!typedLog.args) {
|
|
70211
72916
|
throw DataError.invalidLog("deposit", "missing args");
|
|
70212
72917
|
}
|
|
70213
|
-
const { _depositor: depositor, _commitment: commitment, _label: label, _value: value, _merkleRoot: precommitment, } =
|
|
70214
|
-
if (!depositor ||
|
|
72918
|
+
const { _depositor: depositor, _commitment: commitment, _label: label, _value: value, _merkleRoot: precommitment, } = typedLog.args;
|
|
72919
|
+
if (!depositor ||
|
|
72920
|
+
!commitment ||
|
|
72921
|
+
!label ||
|
|
72922
|
+
!precommitment ||
|
|
72923
|
+
!typedLog.blockNumber ||
|
|
72924
|
+
!typedLog.transactionHash) {
|
|
70215
72925
|
throw DataError.invalidLog("deposit", "missing required fields");
|
|
70216
72926
|
}
|
|
70217
72927
|
return {
|
|
@@ -70220,8 +72930,8 @@ class DataService {
|
|
|
70220
72930
|
label: label,
|
|
70221
72931
|
value: value || BigInt(0),
|
|
70222
72932
|
precommitment: precommitment,
|
|
70223
|
-
blockNumber: BigInt(
|
|
70224
|
-
transactionHash:
|
|
72933
|
+
blockNumber: BigInt(typedLog.blockNumber),
|
|
72934
|
+
transactionHash: typedLog.transactionHash,
|
|
70225
72935
|
};
|
|
70226
72936
|
}
|
|
70227
72937
|
catch (error) {
|
|
@@ -70240,37 +72950,49 @@ class DataService {
|
|
|
70240
72950
|
/**
|
|
70241
72951
|
* Get withdrawal events for a specific chain
|
|
70242
72952
|
*
|
|
70243
|
-
* @param
|
|
70244
|
-
* @param
|
|
72953
|
+
* @param pool - Pool info containing chainId, address, and deployment block
|
|
72954
|
+
* @param fromBlock - Optional starting block (defaults to pool deployment block)
|
|
70245
72955
|
* @returns Array of withdrawal events with properly typed fields (bigint for numbers, Hash for commitments)
|
|
70246
72956
|
* @throws {DataError} If client is not configured, network error occurs, or event data is invalid
|
|
70247
72957
|
*/
|
|
70248
72958
|
async getWithdrawals(pool, fromBlock = pool.deploymentBlock) {
|
|
70249
72959
|
try {
|
|
70250
72960
|
const client = this.getClientForChain(pool.chainId);
|
|
70251
|
-
const
|
|
70252
|
-
const
|
|
70253
|
-
|
|
70254
|
-
|
|
70255
|
-
|
|
70256
|
-
|
|
70257
|
-
|
|
72961
|
+
const chainConfig = this.getConfigForChain(pool.chainId);
|
|
72962
|
+
const logConfig = this.getLogFetchConfigForChain(pool.chainId);
|
|
72963
|
+
const startBlock = fromBlock ?? chainConfig.startBlock;
|
|
72964
|
+
const toBlock = await this.getCurrentBlock(pool.chainId);
|
|
72965
|
+
const ranges = this.generateBlockRanges(startBlock, toBlock, logConfig.blockChunkSize);
|
|
72966
|
+
this.logger.debug(`Fetching withdrawals in ${ranges.length} chunks for pool ${pool.address}`);
|
|
72967
|
+
// Use async.mapLimit for controlled concurrency
|
|
72968
|
+
const allLogs = await mapLimit$1(ranges, logConfig.concurrency, async (range) => {
|
|
72969
|
+
if (logConfig.chunkDelayMs > 0) {
|
|
72970
|
+
await this.sleep(logConfig.chunkDelayMs);
|
|
72971
|
+
}
|
|
72972
|
+
return this.fetchLogsWithRetry(client, pool.address, WITHDRAWAL_EVENT, range, logConfig);
|
|
70258
72973
|
});
|
|
70259
|
-
|
|
72974
|
+
// Flatten and parse results
|
|
72975
|
+
const flatLogs = allLogs.flat();
|
|
72976
|
+
return flatLogs.map((log) => {
|
|
70260
72977
|
try {
|
|
70261
|
-
|
|
72978
|
+
const typedLog = log;
|
|
72979
|
+
if (!typedLog.args) {
|
|
70262
72980
|
throw DataError.invalidLog("withdrawal", "missing args");
|
|
70263
72981
|
}
|
|
70264
|
-
const { _value: value, _spentNullifier: spentNullifier, _newCommitment: newCommitment, } =
|
|
70265
|
-
if (!value ||
|
|
72982
|
+
const { _value: value, _spentNullifier: spentNullifier, _newCommitment: newCommitment, } = typedLog.args;
|
|
72983
|
+
if (!value ||
|
|
72984
|
+
!spentNullifier ||
|
|
72985
|
+
!newCommitment ||
|
|
72986
|
+
!typedLog.blockNumber ||
|
|
72987
|
+
!typedLog.transactionHash) {
|
|
70266
72988
|
throw DataError.invalidLog("withdrawal", "missing required fields");
|
|
70267
72989
|
}
|
|
70268
72990
|
return {
|
|
70269
72991
|
withdrawn: value,
|
|
70270
72992
|
spentNullifier: spentNullifier,
|
|
70271
72993
|
newCommitment: newCommitment,
|
|
70272
|
-
blockNumber: BigInt(
|
|
70273
|
-
transactionHash:
|
|
72994
|
+
blockNumber: BigInt(typedLog.blockNumber),
|
|
72995
|
+
transactionHash: typedLog.transactionHash,
|
|
70274
72996
|
};
|
|
70275
72997
|
}
|
|
70276
72998
|
catch (error) {
|
|
@@ -70289,29 +73011,41 @@ class DataService {
|
|
|
70289
73011
|
/**
|
|
70290
73012
|
* Get ragequit events for a specific chain
|
|
70291
73013
|
*
|
|
70292
|
-
* @param
|
|
70293
|
-
* @param
|
|
73014
|
+
* @param pool - Pool info containing chainId, address, and deployment block
|
|
73015
|
+
* @param fromBlock - Optional starting block (defaults to pool deployment block)
|
|
70294
73016
|
* @returns Array of ragequit events with properly typed fields (bigint for numbers, Hash for commitments)
|
|
70295
73017
|
* @throws {DataError} If client is not configured, network error occurs, or event data is invalid
|
|
70296
73018
|
*/
|
|
70297
73019
|
async getRagequits(pool, fromBlock = pool.deploymentBlock) {
|
|
70298
73020
|
try {
|
|
70299
73021
|
const client = this.getClientForChain(pool.chainId);
|
|
70300
|
-
const
|
|
70301
|
-
const
|
|
70302
|
-
|
|
70303
|
-
|
|
70304
|
-
|
|
70305
|
-
|
|
70306
|
-
|
|
73022
|
+
const chainConfig = this.getConfigForChain(pool.chainId);
|
|
73023
|
+
const logConfig = this.getLogFetchConfigForChain(pool.chainId);
|
|
73024
|
+
const startBlock = fromBlock ?? chainConfig.startBlock;
|
|
73025
|
+
const toBlock = await this.getCurrentBlock(pool.chainId);
|
|
73026
|
+
const ranges = this.generateBlockRanges(startBlock, toBlock, logConfig.blockChunkSize);
|
|
73027
|
+
this.logger.debug(`Fetching ragequits in ${ranges.length} chunks for pool ${pool.address}`);
|
|
73028
|
+
// Use async.mapLimit for controlled concurrency
|
|
73029
|
+
const allLogs = await mapLimit$1(ranges, logConfig.concurrency, async (range) => {
|
|
73030
|
+
if (logConfig.chunkDelayMs > 0) {
|
|
73031
|
+
await this.sleep(logConfig.chunkDelayMs);
|
|
73032
|
+
}
|
|
73033
|
+
return this.fetchLogsWithRetry(client, pool.address, RAGEQUIT_EVENT, range, logConfig);
|
|
70307
73034
|
});
|
|
70308
|
-
|
|
73035
|
+
// Flatten and parse results
|
|
73036
|
+
const flatLogs = allLogs.flat();
|
|
73037
|
+
return flatLogs.map((log) => {
|
|
70309
73038
|
try {
|
|
70310
|
-
|
|
73039
|
+
const typedLog = log;
|
|
73040
|
+
if (!typedLog.args) {
|
|
70311
73041
|
throw DataError.invalidLog("ragequit", "missing args");
|
|
70312
73042
|
}
|
|
70313
|
-
const { _ragequitter: ragequitter, _commitment: commitment, _label: label, _value: value, } =
|
|
70314
|
-
if (!ragequitter ||
|
|
73043
|
+
const { _ragequitter: ragequitter, _commitment: commitment, _label: label, _value: value, } = typedLog.args;
|
|
73044
|
+
if (!ragequitter ||
|
|
73045
|
+
!commitment ||
|
|
73046
|
+
!label ||
|
|
73047
|
+
!typedLog.blockNumber ||
|
|
73048
|
+
!typedLog.transactionHash) {
|
|
70315
73049
|
throw DataError.invalidLog("ragequit", "missing required fields");
|
|
70316
73050
|
}
|
|
70317
73051
|
return {
|
|
@@ -70319,8 +73053,8 @@ class DataService {
|
|
|
70319
73053
|
commitment: commitment,
|
|
70320
73054
|
label: label,
|
|
70321
73055
|
value: value || BigInt(0),
|
|
70322
|
-
blockNumber: BigInt(
|
|
70323
|
-
transactionHash:
|
|
73056
|
+
blockNumber: BigInt(typedLog.blockNumber),
|
|
73057
|
+
transactionHash: typedLog.transactionHash,
|
|
70324
73058
|
};
|
|
70325
73059
|
}
|
|
70326
73060
|
catch (error) {
|
|
@@ -70336,6 +73070,66 @@ class DataService {
|
|
|
70336
73070
|
throw DataError.networkError(pool.chainId, error instanceof Error ? error : new Error(String(error)));
|
|
70337
73071
|
}
|
|
70338
73072
|
}
|
|
73073
|
+
/**
|
|
73074
|
+
* Gets the current block number for a chain
|
|
73075
|
+
*/
|
|
73076
|
+
async getCurrentBlock(chainId) {
|
|
73077
|
+
const client = this.getClientForChain(chainId);
|
|
73078
|
+
return client.getBlockNumber();
|
|
73079
|
+
}
|
|
73080
|
+
/**
|
|
73081
|
+
* Generates block ranges for chunked fetching
|
|
73082
|
+
*/
|
|
73083
|
+
generateBlockRanges(fromBlock, toBlock, chunkSize) {
|
|
73084
|
+
const ranges = [];
|
|
73085
|
+
let current = fromBlock;
|
|
73086
|
+
while (current <= toBlock) {
|
|
73087
|
+
const end = current + BigInt(chunkSize) - 1n;
|
|
73088
|
+
ranges.push({
|
|
73089
|
+
fromBlock: current,
|
|
73090
|
+
toBlock: end > toBlock ? toBlock : end,
|
|
73091
|
+
});
|
|
73092
|
+
current = end + 1n;
|
|
73093
|
+
}
|
|
73094
|
+
return ranges;
|
|
73095
|
+
}
|
|
73096
|
+
/**
|
|
73097
|
+
* Fetches logs for a single block range with retry logic
|
|
73098
|
+
*/
|
|
73099
|
+
async fetchLogsWithRetry(client, address,
|
|
73100
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
73101
|
+
event, range, logConfig) {
|
|
73102
|
+
const maxRetries = logConfig.retryOnFailure
|
|
73103
|
+
? logConfig.maxRetries
|
|
73104
|
+
: 0;
|
|
73105
|
+
let lastError;
|
|
73106
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
73107
|
+
try {
|
|
73108
|
+
const logs = await client.getLogs({
|
|
73109
|
+
address: address,
|
|
73110
|
+
event,
|
|
73111
|
+
fromBlock: range.fromBlock,
|
|
73112
|
+
toBlock: range.toBlock,
|
|
73113
|
+
});
|
|
73114
|
+
return logs;
|
|
73115
|
+
}
|
|
73116
|
+
catch (error) {
|
|
73117
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
73118
|
+
if (attempt < maxRetries) {
|
|
73119
|
+
const delay = logConfig.retryBaseDelayMs * Math.pow(2, attempt);
|
|
73120
|
+
this.logger.warn(`Log fetch failed, retrying in ${delay}ms (attempt ${attempt + 1}/${maxRetries})`, { error: lastError.message, range });
|
|
73121
|
+
await this.sleep(delay);
|
|
73122
|
+
}
|
|
73123
|
+
}
|
|
73124
|
+
}
|
|
73125
|
+
throw lastError;
|
|
73126
|
+
}
|
|
73127
|
+
/**
|
|
73128
|
+
* Helper to add delay between requests
|
|
73129
|
+
*/
|
|
73130
|
+
sleep(ms) {
|
|
73131
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
73132
|
+
}
|
|
70339
73133
|
getClientForChain(chainId) {
|
|
70340
73134
|
const client = this.clients.get(chainId);
|
|
70341
73135
|
if (!client) {
|
|
@@ -70344,13 +73138,21 @@ class DataService {
|
|
|
70344
73138
|
return client;
|
|
70345
73139
|
}
|
|
70346
73140
|
getConfigForChain(chainId) {
|
|
70347
|
-
const config = this.chainConfigs.find(c => c.chainId === chainId);
|
|
73141
|
+
const config = this.chainConfigs.find((c) => c.chainId === chainId);
|
|
70348
73142
|
if (!config) {
|
|
70349
73143
|
throw DataError.chainNotConfigured(chainId);
|
|
70350
73144
|
}
|
|
70351
73145
|
return config;
|
|
70352
73146
|
}
|
|
73147
|
+
getLogFetchConfigForChain(chainId) {
|
|
73148
|
+
const config = this.logFetchConfigs.get(chainId);
|
|
73149
|
+
if (!config) {
|
|
73150
|
+
// Fallback to default if not found (shouldn't happen if constructor is correct)
|
|
73151
|
+
return DEFAULT_LOG_FETCH_CONFIG;
|
|
73152
|
+
}
|
|
73153
|
+
return config;
|
|
73154
|
+
}
|
|
70353
73155
|
}
|
|
70354
73156
|
|
|
70355
|
-
export { AccountService as A, BlockchainProvider as B, CommitmentService as C, DataService as D, ErrorCode as E, FetchArtifact as F, InvalidRpcUrl as I, PrivacyPoolSDK as P, SDKError as S, WithdrawalService as W,
|
|
70356
|
-
//# sourceMappingURL=index-
|
|
73157
|
+
export { AccountService as A, BlockchainProvider as B, CommitmentService as C, DataService as D, ErrorCode as E, FetchArtifact as F, InvalidRpcUrl as I, PrivacyPoolSDK as P, SDKError as S, WithdrawalService as W, DEFAULT_LOG_FETCH_CONFIG as a, generateDepositSecrets as b, generateWithdrawalSecrets as c, getCommitment as d, generateMerkleProof as e, bigintToHash as f, generateMasterKeys as g, hashPrecommitment as h, bigintToHex as i, calculateContext as j, Circuits as k, ContractInteractionsService as l, ProofError as m, ContractError as n, AccountError as o, CircuitName as p };
|
|
73158
|
+
//# sourceMappingURL=index-CRtEyHEf.js.map
|