@orioro/util 0.10.0 → 0.11.2
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/CHANGELOG.md +13 -0
- package/dist/array/dataJoin.d.ts +9 -0
- package/dist/array/index.d.ts +1 -0
- package/dist/array/join.d.ts +9 -0
- package/dist/debug/debugFn/index.d.ts +3 -2
- package/dist/deprecate.d.ts +3 -0
- package/dist/index.d.ts +5 -2
- package/dist/index.js +1514 -0
- package/dist/index.mjs +536 -794
- package/dist/interpolate/index.d.ts +1 -1
- package/dist/pathResolve/index.d.ts +7 -0
- package/dist/promise/index.d.ts +4 -0
- package/dist/promise/isPromise.d.ts +1 -0
- package/dist/promise/maybeAsyncFn.d.ts +20 -0
- package/dist/promise/maybeReturnPromise.d.ts +1 -0
- package/dist/promise/untilConditionIsSatisfiedReducer.d.ts +1 -0
- package/dist/slugify/index.d.ts +2 -0
- package/dist/strAutoCast/index.d.ts +1 -0
- package/dist/strAutoCast/strAutoCast.d.ts +1 -0
- package/dist/strExpr/index.d.ts +2 -0
- package/dist/strExpr/strExpr.d.ts +14 -0
- package/dist/strExpr/syntheticJson.d.ts +1 -0
- package/dist/typeOf.d.ts +23 -1
- package/dist/url/index.d.ts +2 -0
- package/dist/url/isValidUrl.d.ts +1 -0
- package/dist/url/url.d.ts +18 -0
- package/dist/validate/common/validators/type.d.ts +2 -2
- package/package.json +7 -3
package/dist/index.js
ADDED
|
@@ -0,0 +1,1514 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var lodashEs = require('lodash-es');
|
|
4
|
+
var EventEmitter = require('eventemitter3');
|
|
5
|
+
var copy = require('fast-copy');
|
|
6
|
+
var traverse = require('traverse');
|
|
7
|
+
var exponentialBackoff = require('exponential-backoff');
|
|
8
|
+
var isPlainObject = require('is-plain-object');
|
|
9
|
+
var memoizeOne = require('memoize-one');
|
|
10
|
+
var queryString = require('query-string');
|
|
11
|
+
|
|
12
|
+
function arrayChunk(array, chunkSize) {
|
|
13
|
+
var chunks = [];
|
|
14
|
+
for (var i = 0; i < array.length; i += chunkSize) {
|
|
15
|
+
chunks.push(array.slice(i, i + chunkSize));
|
|
16
|
+
}
|
|
17
|
+
return chunks;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function _typeof(o) {
|
|
21
|
+
"@babel/helpers - typeof";
|
|
22
|
+
|
|
23
|
+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
|
|
24
|
+
return typeof o;
|
|
25
|
+
} : function (o) {
|
|
26
|
+
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
|
|
27
|
+
}, _typeof(o);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/******************************************************************************
|
|
31
|
+
Copyright (c) Microsoft Corporation.
|
|
32
|
+
|
|
33
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
34
|
+
purpose with or without fee is hereby granted.
|
|
35
|
+
|
|
36
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
37
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
38
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
39
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
40
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
41
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
42
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
43
|
+
***************************************************************************** */
|
|
44
|
+
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
|
|
45
|
+
|
|
46
|
+
var _extendStatics = function extendStatics(d, b) {
|
|
47
|
+
_extendStatics = Object.setPrototypeOf || {
|
|
48
|
+
__proto__: []
|
|
49
|
+
} instanceof Array && function (d, b) {
|
|
50
|
+
d.__proto__ = b;
|
|
51
|
+
} || function (d, b) {
|
|
52
|
+
for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
|
|
53
|
+
};
|
|
54
|
+
return _extendStatics(d, b);
|
|
55
|
+
};
|
|
56
|
+
function __extends(d, b) {
|
|
57
|
+
if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
58
|
+
_extendStatics(d, b);
|
|
59
|
+
function __() {
|
|
60
|
+
this.constructor = d;
|
|
61
|
+
}
|
|
62
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
63
|
+
}
|
|
64
|
+
var _assign = function __assign() {
|
|
65
|
+
_assign = Object.assign || function __assign(t) {
|
|
66
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
67
|
+
s = arguments[i];
|
|
68
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
69
|
+
}
|
|
70
|
+
return t;
|
|
71
|
+
};
|
|
72
|
+
return _assign.apply(this, arguments);
|
|
73
|
+
};
|
|
74
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
75
|
+
function adopt(value) {
|
|
76
|
+
return value instanceof P ? value : new P(function (resolve) {
|
|
77
|
+
resolve(value);
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
81
|
+
function fulfilled(value) {
|
|
82
|
+
try {
|
|
83
|
+
step(generator.next(value));
|
|
84
|
+
} catch (e) {
|
|
85
|
+
reject(e);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function rejected(value) {
|
|
89
|
+
try {
|
|
90
|
+
step(generator["throw"](value));
|
|
91
|
+
} catch (e) {
|
|
92
|
+
reject(e);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
function step(result) {
|
|
96
|
+
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
|
|
97
|
+
}
|
|
98
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
function __generator(thisArg, body) {
|
|
102
|
+
var _ = {
|
|
103
|
+
label: 0,
|
|
104
|
+
sent: function sent() {
|
|
105
|
+
if (t[0] & 1) throw t[1];
|
|
106
|
+
return t[1];
|
|
107
|
+
},
|
|
108
|
+
trys: [],
|
|
109
|
+
ops: []
|
|
110
|
+
},
|
|
111
|
+
f,
|
|
112
|
+
y,
|
|
113
|
+
t,
|
|
114
|
+
g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
115
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function () {
|
|
116
|
+
return this;
|
|
117
|
+
}), g;
|
|
118
|
+
function verb(n) {
|
|
119
|
+
return function (v) {
|
|
120
|
+
return step([n, v]);
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
function step(op) {
|
|
124
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
125
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
126
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
127
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
128
|
+
switch (op[0]) {
|
|
129
|
+
case 0:
|
|
130
|
+
case 1:
|
|
131
|
+
t = op;
|
|
132
|
+
break;
|
|
133
|
+
case 4:
|
|
134
|
+
_.label++;
|
|
135
|
+
return {
|
|
136
|
+
value: op[1],
|
|
137
|
+
done: false
|
|
138
|
+
};
|
|
139
|
+
case 5:
|
|
140
|
+
_.label++;
|
|
141
|
+
y = op[1];
|
|
142
|
+
op = [0];
|
|
143
|
+
continue;
|
|
144
|
+
case 7:
|
|
145
|
+
op = _.ops.pop();
|
|
146
|
+
_.trys.pop();
|
|
147
|
+
continue;
|
|
148
|
+
default:
|
|
149
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
150
|
+
_ = 0;
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
154
|
+
_.label = op[1];
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
158
|
+
_.label = t[1];
|
|
159
|
+
t = op;
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
if (t && _.label < t[2]) {
|
|
163
|
+
_.label = t[2];
|
|
164
|
+
_.ops.push(op);
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
if (t[2]) _.ops.pop();
|
|
168
|
+
_.trys.pop();
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
op = body.call(thisArg, _);
|
|
172
|
+
} catch (e) {
|
|
173
|
+
op = [6, e];
|
|
174
|
+
y = 0;
|
|
175
|
+
} finally {
|
|
176
|
+
f = t = 0;
|
|
177
|
+
}
|
|
178
|
+
if (op[0] & 5) throw op[1];
|
|
179
|
+
return {
|
|
180
|
+
value: op[0] ? op[1] : void 0,
|
|
181
|
+
done: true
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
function __spreadArray(to, from, pack) {
|
|
186
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
187
|
+
if (ar || !(i in from)) {
|
|
188
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
189
|
+
ar[i] = from[i];
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
193
|
+
}
|
|
194
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
195
|
+
var e = new Error(message);
|
|
196
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
function _isTupleDatasetInput(input) {
|
|
200
|
+
return Array.isArray(input) && typeof input[0] === 'string' && Array.isArray(input[1]);
|
|
201
|
+
}
|
|
202
|
+
function _parseDatasetInput(defaultOnKey, datasetInput) {
|
|
203
|
+
var _a;
|
|
204
|
+
if (_isTupleDatasetInput(datasetInput)) {
|
|
205
|
+
var keyInput = datasetInput[0];
|
|
206
|
+
var key = keyInput;
|
|
207
|
+
var srcKey = null;
|
|
208
|
+
if (typeof keyInput === 'string') {
|
|
209
|
+
var keySplit = keyInput.split(':');
|
|
210
|
+
if (keySplit.length === 1) {
|
|
211
|
+
srcKey = null;
|
|
212
|
+
key = keyInput;
|
|
213
|
+
} else {
|
|
214
|
+
srcKey = keySplit[0];
|
|
215
|
+
key = keySplit[1];
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return {
|
|
219
|
+
srcKey: srcKey,
|
|
220
|
+
key: key,
|
|
221
|
+
entries: datasetInput[1],
|
|
222
|
+
path: (_a = datasetInput[2]) !== null && _a !== void 0 ? _a : null
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
return {
|
|
226
|
+
srcKey: null,
|
|
227
|
+
key: defaultOnKey,
|
|
228
|
+
entries: datasetInput,
|
|
229
|
+
path: null
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
function dataJoin(datasets, _a) {
|
|
233
|
+
var _b = _a === void 0 ? {} : _a,
|
|
234
|
+
_c = _b.key,
|
|
235
|
+
defaultOnKey = _c === void 0 ? 'id' : _c,
|
|
236
|
+
_d = _b.mode,
|
|
237
|
+
mode = _d === void 0 ? 'left' : _d;
|
|
238
|
+
var base = datasets[0],
|
|
239
|
+
others = datasets.slice(1);
|
|
240
|
+
//
|
|
241
|
+
// Base dataset requires no byKey dict
|
|
242
|
+
//
|
|
243
|
+
var baseDataset = _parseDatasetInput(defaultOnKey, base);
|
|
244
|
+
//
|
|
245
|
+
// Other datasets require byKey dict
|
|
246
|
+
//
|
|
247
|
+
var otherSets = others.map(function (setInput) {
|
|
248
|
+
var parsed = _parseDatasetInput(defaultOnKey, setInput);
|
|
249
|
+
return _assign(_assign({}, parsed), {
|
|
250
|
+
byKey: lodashEs.keyBy(parsed.entries, parsed.key)
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
//
|
|
254
|
+
// Prepare a set of functions that
|
|
255
|
+
// retrieve a given entry's key relative
|
|
256
|
+
// to another dataset
|
|
257
|
+
//
|
|
258
|
+
function _entryKey(entry, otherSet) {
|
|
259
|
+
var _a;
|
|
260
|
+
return ((_a = otherSet.srcKey ? lodashEs.get(entry, otherSet.srcKey) : lodashEs.get(entry, baseDataset.key)) !== null && _a !== void 0 ? _a : '').toString();
|
|
261
|
+
}
|
|
262
|
+
//
|
|
263
|
+
// If mode === inner, filter out missing data
|
|
264
|
+
//
|
|
265
|
+
var baseEntries = mode === 'inner' ? baseDataset.entries.filter(function (entry) {
|
|
266
|
+
return otherSets.every(function (set) {
|
|
267
|
+
return set.byKey[_entryKey(entry, set)];
|
|
268
|
+
});
|
|
269
|
+
}) : baseDataset.entries;
|
|
270
|
+
//
|
|
271
|
+
// Loop over baseDataset.entries joining it with other data
|
|
272
|
+
//
|
|
273
|
+
return baseEntries.map(function (entry) {
|
|
274
|
+
var _a;
|
|
275
|
+
//
|
|
276
|
+
// Setup a base entry over which data will be set
|
|
277
|
+
//
|
|
278
|
+
var baseEntry = baseDataset.path ? (_a = {}, _a[baseDataset.path] = entry, _a) : Object.assign({}, entry);
|
|
279
|
+
//
|
|
280
|
+
// Loop over all other sets to pick up data
|
|
281
|
+
//
|
|
282
|
+
return otherSets.reduce(function (acc, otherSet) {
|
|
283
|
+
var entryKey = _entryKey(entry, otherSet);
|
|
284
|
+
var otherEntry = otherSet.byKey[entryKey];
|
|
285
|
+
if (otherEntry) {
|
|
286
|
+
if (otherSet.path) {
|
|
287
|
+
lodashEs.set(acc, otherSet.path, otherEntry);
|
|
288
|
+
} else {
|
|
289
|
+
Object.assign(acc, otherEntry);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
return acc;
|
|
293
|
+
}, baseEntry);
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
var PromiseLikeEventEmitter = /** @class */function (_super) {
|
|
298
|
+
__extends(PromiseLikeEventEmitter, _super);
|
|
299
|
+
function PromiseLikeEventEmitter() {
|
|
300
|
+
var _this = _super.call(this) || this;
|
|
301
|
+
_this.promise = new Promise(function (resolve, reject) {
|
|
302
|
+
_this.resolve = resolve;
|
|
303
|
+
_this.reject = reject;
|
|
304
|
+
});
|
|
305
|
+
return _this;
|
|
306
|
+
}
|
|
307
|
+
PromiseLikeEventEmitter.prototype.then = function (onFulfilled, onRejected) {
|
|
308
|
+
return this.promise.then(onFulfilled, onRejected);
|
|
309
|
+
};
|
|
310
|
+
PromiseLikeEventEmitter.prototype["catch"] = function (onRejected) {
|
|
311
|
+
return this.promise["catch"](onRejected);
|
|
312
|
+
};
|
|
313
|
+
PromiseLikeEventEmitter.prototype["finally"] = function (onFinally) {
|
|
314
|
+
return this.promise["finally"](onFinally);
|
|
315
|
+
};
|
|
316
|
+
return PromiseLikeEventEmitter;
|
|
317
|
+
}(EventEmitter);
|
|
318
|
+
|
|
319
|
+
function wait(ms, result) {
|
|
320
|
+
if (ms === void 0) {
|
|
321
|
+
ms = 1000;
|
|
322
|
+
}
|
|
323
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
324
|
+
return __generator(this, function (_a) {
|
|
325
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
326
|
+
setTimeout(function () {
|
|
327
|
+
if (result instanceof Error) {
|
|
328
|
+
reject(result);
|
|
329
|
+
} else {
|
|
330
|
+
resolve(result);
|
|
331
|
+
}
|
|
332
|
+
}, ms);
|
|
333
|
+
})];
|
|
334
|
+
});
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function deepFreeze(object) {
|
|
339
|
+
Object.freeze(object);
|
|
340
|
+
if (Array.isArray(object)) {
|
|
341
|
+
// Handle the array case
|
|
342
|
+
object.forEach(function (item) {
|
|
343
|
+
if (item && _typeof(item) === 'object' && !Object.isFrozen(item)) {
|
|
344
|
+
deepFreeze(item);
|
|
345
|
+
}
|
|
346
|
+
});
|
|
347
|
+
} else {
|
|
348
|
+
// Handle the object case
|
|
349
|
+
Object.keys(object).forEach(function (key) {
|
|
350
|
+
var value = object[key];
|
|
351
|
+
if (value && _typeof(value) === 'object' && !Object.isFrozen(value)) {
|
|
352
|
+
deepFreeze(value);
|
|
353
|
+
}
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
return object;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
function stringifyReplacer(key, value) {
|
|
360
|
+
if (typeof value === 'function') {
|
|
361
|
+
// Convert functions to a consistent string representation
|
|
362
|
+
return "function:".concat(value.toString());
|
|
363
|
+
}
|
|
364
|
+
return value;
|
|
365
|
+
}
|
|
366
|
+
function simpleHash(str) {
|
|
367
|
+
var hash = 0;
|
|
368
|
+
for (var i = 0; i < str.length; i++) {
|
|
369
|
+
var _char = str.charCodeAt(i);
|
|
370
|
+
hash = (hash << 5) - hash + _char;
|
|
371
|
+
hash |= 0; // Convert to 32bit integer
|
|
372
|
+
}
|
|
373
|
+
// Ensure the hash is positive to avoid '-' signals
|
|
374
|
+
hash = Math.abs(hash);
|
|
375
|
+
return hash.toString(36); // Convert to base36 to ensure the result is alphanumeric
|
|
376
|
+
}
|
|
377
|
+
function generateDeterministicId(args) {
|
|
378
|
+
// Convert arguments to a JSON string using the custom replacer
|
|
379
|
+
var argsString = JSON.stringify(args, stringifyReplacer);
|
|
380
|
+
// Generate a simple hash of the string
|
|
381
|
+
return simpleHash(argsString);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
function sequentialCallIdGenerator() {
|
|
385
|
+
var count = 0;
|
|
386
|
+
return function () {
|
|
387
|
+
return count += 1;
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
function debugFn(fnName, fn, logCall, logResult, generateCallId) {
|
|
391
|
+
if (generateCallId === void 0) {
|
|
392
|
+
generateCallId = generateDeterministicId;
|
|
393
|
+
}
|
|
394
|
+
return function () {
|
|
395
|
+
var args = [];
|
|
396
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
397
|
+
args[_i] = arguments[_i];
|
|
398
|
+
}
|
|
399
|
+
var callId = generateCallId(__spreadArray([fnName, fn], args, true));
|
|
400
|
+
if (typeof logCall === 'function') {
|
|
401
|
+
// Deep clone args, so that we may detect mutations later on
|
|
402
|
+
logCall({
|
|
403
|
+
type: 'call',
|
|
404
|
+
callId: callId,
|
|
405
|
+
args: copy(args),
|
|
406
|
+
fnName: fnName
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
var result = fn.apply(void 0, args);
|
|
410
|
+
if (typeof logResult === 'function') {
|
|
411
|
+
logResult({
|
|
412
|
+
type: 'result',
|
|
413
|
+
callId: callId,
|
|
414
|
+
args: args,
|
|
415
|
+
result: result,
|
|
416
|
+
fnName: fnName
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
return result;
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* /\$\{\s*([\w$.]+)\s*\}/g
|
|
425
|
+
* 
|
|
426
|
+
*
|
|
427
|
+
* RegExp used for matching interpolation expressions.
|
|
428
|
+
* Allows a non-interrupted sequence of alphanumeric chars ([A-Za-z0-9_]),
|
|
429
|
+
* dollar signs ($) and dots (.) wrapped in curly braces ({})
|
|
430
|
+
* with or without any number of whitespace chars (' ') between braces and the
|
|
431
|
+
* value identifier.
|
|
432
|
+
*
|
|
433
|
+
* Some resources on RegExp safety concerning RegExp Denial of Service (ReDOS)
|
|
434
|
+
* through Catastrophic backtracking, for future study and reference:
|
|
435
|
+
*
|
|
436
|
+
* - [Catastrophic backtracking](https://www.regular-expressions.info/catastrophic.html)
|
|
437
|
+
* - [Regular expression visualizer](https://github.com/CJex/regulex)
|
|
438
|
+
* - [Validator.js](https://github.com/validatorjs/validator.js)
|
|
439
|
+
* - [Stack Overflow interesting question](https://stackoverflow.com/questions/63127145/safe-regex-patterns-from-redos-attack)
|
|
440
|
+
* - [Catastrophic backtracking - JavaScript Info](https://javascript.info/regexp-catastrophic-backtracking#preventing-backtracking)
|
|
441
|
+
* - [Google re2 library](https://github.com/google/re2)
|
|
442
|
+
* - [Google re2 for Node.js - re2](https://github.com/uhop/node-re2/)
|
|
443
|
+
*
|
|
444
|
+
* @const {RegExp} INTERPOLATION_REGEXP
|
|
445
|
+
*/
|
|
446
|
+
var INTERPOLATION_REGEXP = /\$\{\s*([\w$.=\s]+)\s*\}/g;
|
|
447
|
+
var DEFAULT_SIGN = '=';
|
|
448
|
+
function _parsePathExpr(pathExpr) {
|
|
449
|
+
var splitRes = pathExpr.split(DEFAULT_SIGN);
|
|
450
|
+
return splitRes.length > 1 ? [splitRes[0].trim(), splitRes.slice(1).join(DEFAULT_SIGN).trim()] : [splitRes[0].trim()];
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* @function $stringInterpolate
|
|
454
|
+
* @param {String} template Basic JS template string like `${value.path}` value
|
|
455
|
+
* interpolation. It is possible to access nested properties
|
|
456
|
+
* through dot `.` notation. Keywords between braces are
|
|
457
|
+
* only interpreted as paths to the value. No logic
|
|
458
|
+
* supported: loops, conditionals, etc.
|
|
459
|
+
* @param {Object | Array} data Data context to be used for interpolation
|
|
460
|
+
*/
|
|
461
|
+
function interpolate(template, data, _a) {
|
|
462
|
+
var _b = _a === void 0 ? {} : _a,
|
|
463
|
+
_c = _b.maxLength,
|
|
464
|
+
maxLength = _c === void 0 ? 10000 : _c;
|
|
465
|
+
if (template.length > maxLength) {
|
|
466
|
+
throw new Error("Template exceeds maxLength ".concat(maxLength));
|
|
467
|
+
}
|
|
468
|
+
data = _typeof(data) === 'object' ? data : [data];
|
|
469
|
+
return template.replace(INTERPOLATION_REGEXP, function (_, pathExpr) {
|
|
470
|
+
var _a = _parsePathExpr(pathExpr),
|
|
471
|
+
path = _a[0],
|
|
472
|
+
defaultValue = _a[1];
|
|
473
|
+
var value = lodashEs.get(data, path);
|
|
474
|
+
switch (_typeof(value)) {
|
|
475
|
+
case 'number':
|
|
476
|
+
{
|
|
477
|
+
return value + '';
|
|
478
|
+
}
|
|
479
|
+
case 'string':
|
|
480
|
+
{
|
|
481
|
+
return value;
|
|
482
|
+
}
|
|
483
|
+
default:
|
|
484
|
+
{
|
|
485
|
+
if (typeof value === 'undefined' || value === null) {
|
|
486
|
+
return typeof defaultValue === 'string' ? defaultValue : '';
|
|
487
|
+
} else {
|
|
488
|
+
console.warn("Attempting to use non interpolatable value in interpolate ".concat(value));
|
|
489
|
+
return '';
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
function maybeFn(input) {
|
|
497
|
+
var args = [];
|
|
498
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
499
|
+
args[_i - 1] = arguments[_i];
|
|
500
|
+
}
|
|
501
|
+
return typeof input === 'function' ? input.apply(void 0, args) : input;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
function pickPaths(sourceObj, paths) {
|
|
505
|
+
return paths.reduce(function (acc, path) {
|
|
506
|
+
var _a = Array.isArray(path) ? path : [path, path],
|
|
507
|
+
targetPath = _a[0],
|
|
508
|
+
resolver = _a[1],
|
|
509
|
+
defaultValue = _a[2];
|
|
510
|
+
var value = typeof resolver === 'string' ? lodashEs.get(sourceObj, resolver) : resolver(sourceObj);
|
|
511
|
+
var valueAfterDefault = typeof value === 'undefined' ? defaultValue : value;
|
|
512
|
+
//
|
|
513
|
+
// Undefined values are skipped in order to avoid nested
|
|
514
|
+
// setting undefined values
|
|
515
|
+
//
|
|
516
|
+
return typeof valueAfterDefault !== 'undefined' ? lodashEs.set(acc, targetPath, valueAfterDefault) : acc;
|
|
517
|
+
}, {});
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
function promiseReduce(inputArr, reducerFn, initial) {
|
|
521
|
+
return inputArr.reduce(function (prevPromise, input, index) {
|
|
522
|
+
return prevPromise.then(function (acc) {
|
|
523
|
+
return reducerFn(acc, input, index);
|
|
524
|
+
});
|
|
525
|
+
}, Promise.resolve(initial));
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
var DEFAULT_OPTIONS = {
|
|
529
|
+
mode: 'eager',
|
|
530
|
+
maxDepth: 30,
|
|
531
|
+
clone: copy
|
|
532
|
+
};
|
|
533
|
+
function _echo(input) {
|
|
534
|
+
return input;
|
|
535
|
+
}
|
|
536
|
+
function resolveNestedPromises(node, options, ctx) {
|
|
537
|
+
if (options === void 0) {
|
|
538
|
+
options = {};
|
|
539
|
+
}
|
|
540
|
+
if (ctx === void 0) {
|
|
541
|
+
ctx = {
|
|
542
|
+
root: node,
|
|
543
|
+
depth: 0
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
547
|
+
var opts, promises, _cloneFn, clonedNode, resolved;
|
|
548
|
+
return __generator(this, function (_a) {
|
|
549
|
+
opts = _assign(_assign({}, DEFAULT_OPTIONS), options);
|
|
550
|
+
promises = [];
|
|
551
|
+
_cloneFn = opts.clone || _echo;
|
|
552
|
+
clonedNode = _cloneFn(node);
|
|
553
|
+
resolved = traverse(clonedNode).forEach(function (value) {
|
|
554
|
+
var _this = this;
|
|
555
|
+
var depth = ctx.depth + this.path.length;
|
|
556
|
+
if (depth > opts.maxDepth) {
|
|
557
|
+
throw new Error("Max depth exceeded: { depth: ".concat(depth, ", maxDepth: ").concat(opts.maxDepth, " }"));
|
|
558
|
+
}
|
|
559
|
+
if (value instanceof Promise) {
|
|
560
|
+
var updatePromise = value.then(function (resolvedValue) {
|
|
561
|
+
return __awaiter(_this, void 0, void 0, function () {
|
|
562
|
+
var _a, _b;
|
|
563
|
+
return __generator(this, function (_c) {
|
|
564
|
+
switch (_c.label) {
|
|
565
|
+
case 0:
|
|
566
|
+
// Arrow function;
|
|
567
|
+
// this refers to outside this ctx
|
|
568
|
+
_a = this.update;
|
|
569
|
+
if (!(opts.mode === 'eager')) return [3 /*break*/, 2];
|
|
570
|
+
return [4 /*yield*/, resolveNestedPromises(resolvedValue, opts, _assign(_assign({}, ctx), {
|
|
571
|
+
// pass the base depth of the current value
|
|
572
|
+
depth: depth
|
|
573
|
+
}))];
|
|
574
|
+
case 1:
|
|
575
|
+
_b = _c.sent();
|
|
576
|
+
return [3 /*break*/, 3];
|
|
577
|
+
case 2:
|
|
578
|
+
_b = resolvedValue;
|
|
579
|
+
_c.label = 3;
|
|
580
|
+
case 3:
|
|
581
|
+
// Arrow function;
|
|
582
|
+
// this refers to outside this ctx
|
|
583
|
+
_a.apply(this, [_b]);
|
|
584
|
+
return [2 /*return*/];
|
|
585
|
+
}
|
|
586
|
+
});
|
|
587
|
+
});
|
|
588
|
+
});
|
|
589
|
+
promises = __spreadArray(__spreadArray([], promises, true), [updatePromise], false);
|
|
590
|
+
}
|
|
591
|
+
});
|
|
592
|
+
return [2 /*return*/, Promise.all(promises).then(function () {
|
|
593
|
+
return resolved;
|
|
594
|
+
})];
|
|
595
|
+
});
|
|
596
|
+
});
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
var TimeoutError = /** @class */function (_super) {
|
|
600
|
+
__extends(TimeoutError, _super);
|
|
601
|
+
function TimeoutError(message) {
|
|
602
|
+
var _this = _super.call(this, message) || this;
|
|
603
|
+
_this.name = 'TimeoutError';
|
|
604
|
+
_this.code = 'ETIMEDOUT';
|
|
605
|
+
_this.timestamp = new Date();
|
|
606
|
+
// Maintain proper stack trace (only available in V8 engines, e.g., Chrome and Node.js)
|
|
607
|
+
if (Error.captureStackTrace) {
|
|
608
|
+
Error.captureStackTrace(_this, TimeoutError);
|
|
609
|
+
}
|
|
610
|
+
return _this;
|
|
611
|
+
}
|
|
612
|
+
return TimeoutError;
|
|
613
|
+
}(Error);
|
|
614
|
+
function withTimeout(fn, timeout) {
|
|
615
|
+
return function () {
|
|
616
|
+
var args = [];
|
|
617
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
618
|
+
args[_i] = arguments[_i];
|
|
619
|
+
}
|
|
620
|
+
return new Promise(function (resolve, reject) {
|
|
621
|
+
var timer = setTimeout(function () {
|
|
622
|
+
reject(new TimeoutError("Function timed out (max: ".concat(timeout, "ms)")));
|
|
623
|
+
}, timeout);
|
|
624
|
+
fn.apply(void 0, args).then(resolve)["catch"](reject)["finally"](function () {
|
|
625
|
+
return clearTimeout(timer);
|
|
626
|
+
});
|
|
627
|
+
});
|
|
628
|
+
};
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
var SKIPPED = Symbol();
|
|
632
|
+
function batchFn(itemFn, _a) {
|
|
633
|
+
var _b = _a === void 0 ? {} : _a,
|
|
634
|
+
_c = _b.batchSize,
|
|
635
|
+
batchSize = _c === void 0 ? 10 : _c,
|
|
636
|
+
skip = _b.skip,
|
|
637
|
+
_d = _b.retry,
|
|
638
|
+
retry = _d === void 0 ? false : _d,
|
|
639
|
+
timeout = _b.timeout,
|
|
640
|
+
rollback = _b.rollback;
|
|
641
|
+
//
|
|
642
|
+
// Optionally wrap itemFn into timeout
|
|
643
|
+
//
|
|
644
|
+
itemFn = timeout ? withTimeout(itemFn, timeout) : itemFn;
|
|
645
|
+
return function batchExec(items) {
|
|
646
|
+
var _this = this;
|
|
647
|
+
var batches = arrayChunk(items, batchSize);
|
|
648
|
+
var promise = new PromiseLikeEventEmitter();
|
|
649
|
+
// const events = new EventEmitter<EventTypes>()
|
|
650
|
+
var progressCount = 0;
|
|
651
|
+
var resultsPromise = promiseReduce(batches, function (acc, batchItems, index) {
|
|
652
|
+
return __awaiter(_this, void 0, void 0, function () {
|
|
653
|
+
var batch, batchResults;
|
|
654
|
+
var _this = this;
|
|
655
|
+
return __generator(this, function (_a) {
|
|
656
|
+
switch (_a.label) {
|
|
657
|
+
case 0:
|
|
658
|
+
batch = {
|
|
659
|
+
index: index,
|
|
660
|
+
items: batchItems
|
|
661
|
+
};
|
|
662
|
+
promise.emit('batchStart', {
|
|
663
|
+
batch: batch
|
|
664
|
+
});
|
|
665
|
+
return [4 /*yield*/, Promise.all(batchItems.map(function (item) {
|
|
666
|
+
return __awaiter(_this, void 0, void 0, function () {
|
|
667
|
+
var itemSkip, itemResult, _a, err_1, rollbackErr_1;
|
|
668
|
+
return __generator(this, function (_b) {
|
|
669
|
+
switch (_b.label) {
|
|
670
|
+
case 0:
|
|
671
|
+
_b.trys.push([0, 5,, 10]);
|
|
672
|
+
promise.emit('itemStart', {
|
|
673
|
+
batch: batch,
|
|
674
|
+
item: item
|
|
675
|
+
});
|
|
676
|
+
return [4 /*yield*/, typeof skip === 'function' && skip(item)];
|
|
677
|
+
case 1:
|
|
678
|
+
itemSkip = _b.sent();
|
|
679
|
+
if (itemSkip) {
|
|
680
|
+
promise.emit('itemSkip', {
|
|
681
|
+
batch: batch,
|
|
682
|
+
item: item
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
if (!itemSkip) return [3 /*break*/, 2];
|
|
686
|
+
_a = SKIPPED;
|
|
687
|
+
return [3 /*break*/, 4];
|
|
688
|
+
case 2:
|
|
689
|
+
return [4 /*yield*/, retry === true ? exponentialBackoff.backOff(function () {
|
|
690
|
+
return itemFn(item);
|
|
691
|
+
}) : _typeof(retry) === 'object' && retry !== null ? exponentialBackoff.backOff(function () {
|
|
692
|
+
return itemFn(item);
|
|
693
|
+
}, retry) : itemFn(item)];
|
|
694
|
+
case 3:
|
|
695
|
+
_a = _b.sent();
|
|
696
|
+
_b.label = 4;
|
|
697
|
+
case 4:
|
|
698
|
+
itemResult = _a;
|
|
699
|
+
progressCount += 1;
|
|
700
|
+
promise.emit('progress', {
|
|
701
|
+
type: itemSkip ? 'skip' : 'data',
|
|
702
|
+
batch: batch,
|
|
703
|
+
progress: progressCount / items.length,
|
|
704
|
+
item: item,
|
|
705
|
+
result: itemResult
|
|
706
|
+
});
|
|
707
|
+
return [2 /*return*/, itemResult];
|
|
708
|
+
case 5:
|
|
709
|
+
err_1 = _b.sent();
|
|
710
|
+
progressCount += 1;
|
|
711
|
+
promise.emit('progress', {
|
|
712
|
+
type: 'error',
|
|
713
|
+
batch: batch,
|
|
714
|
+
progress: progressCount / items.length,
|
|
715
|
+
item: item,
|
|
716
|
+
result: err_1
|
|
717
|
+
});
|
|
718
|
+
if (!(typeof rollback === 'function')) return [3 /*break*/, 9];
|
|
719
|
+
_b.label = 6;
|
|
720
|
+
case 6:
|
|
721
|
+
_b.trys.push([6, 8,, 9]);
|
|
722
|
+
return [4 /*yield*/, rollback(item, err_1)];
|
|
723
|
+
case 7:
|
|
724
|
+
_b.sent();
|
|
725
|
+
return [3 /*break*/, 9];
|
|
726
|
+
case 8:
|
|
727
|
+
rollbackErr_1 = _b.sent();
|
|
728
|
+
console.error('Failure rolling back', rollbackErr_1);
|
|
729
|
+
return [3 /*break*/, 9];
|
|
730
|
+
case 9:
|
|
731
|
+
return [2 /*return*/, err_1];
|
|
732
|
+
case 10:
|
|
733
|
+
return [2 /*return*/];
|
|
734
|
+
}
|
|
735
|
+
});
|
|
736
|
+
});
|
|
737
|
+
}))];
|
|
738
|
+
case 1:
|
|
739
|
+
batchResults = _a.sent();
|
|
740
|
+
promise.emit('batchProgress', {
|
|
741
|
+
batch: batch,
|
|
742
|
+
progress: batch.index + 1 / batches.length,
|
|
743
|
+
results: batchResults
|
|
744
|
+
});
|
|
745
|
+
return [2 /*return*/, __spreadArray(__spreadArray([], acc, true), batchResults, true)];
|
|
746
|
+
}
|
|
747
|
+
});
|
|
748
|
+
});
|
|
749
|
+
}, []);
|
|
750
|
+
resultsPromise.then(function (results) {
|
|
751
|
+
promise.resolve(results);
|
|
752
|
+
promise.emit('results', {
|
|
753
|
+
results: results
|
|
754
|
+
});
|
|
755
|
+
}, function (err) {
|
|
756
|
+
promise.reject(err);
|
|
757
|
+
promise.emit('error', err);
|
|
758
|
+
});
|
|
759
|
+
return promise;
|
|
760
|
+
};
|
|
761
|
+
}
|
|
762
|
+
function parseBatchedResults(inputs, results) {
|
|
763
|
+
return results.reduce(function (acc, result, index) {
|
|
764
|
+
if (result === SKIPPED) {
|
|
765
|
+
return _assign(_assign({}, acc), {
|
|
766
|
+
skipped: __spreadArray(__spreadArray([], acc.skipped, true), [inputs[index]], false)
|
|
767
|
+
});
|
|
768
|
+
} else if (result instanceof Error) {
|
|
769
|
+
return _assign(_assign({}, acc), {
|
|
770
|
+
errors: __spreadArray(__spreadArray([], acc.errors, true), [result], false)
|
|
771
|
+
});
|
|
772
|
+
} else {
|
|
773
|
+
return _assign(_assign({}, acc), {
|
|
774
|
+
results: __spreadArray(__spreadArray([], acc.results, true), [result], false)
|
|
775
|
+
});
|
|
776
|
+
}
|
|
777
|
+
}, {
|
|
778
|
+
results: [],
|
|
779
|
+
errors: [],
|
|
780
|
+
skipped: []
|
|
781
|
+
});
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
function makeDeferred() {
|
|
785
|
+
var resolve = function resolve() {};
|
|
786
|
+
var reject = function reject() {};
|
|
787
|
+
var settled = false;
|
|
788
|
+
var promise = new Promise(function (_resolve, _reject) {
|
|
789
|
+
resolve = function resolve(value) {
|
|
790
|
+
if (!settled) {
|
|
791
|
+
settled = true;
|
|
792
|
+
_resolve(value);
|
|
793
|
+
}
|
|
794
|
+
};
|
|
795
|
+
reject = function reject(reason) {
|
|
796
|
+
if (!settled) {
|
|
797
|
+
settled = true;
|
|
798
|
+
_reject(reason);
|
|
799
|
+
}
|
|
800
|
+
};
|
|
801
|
+
});
|
|
802
|
+
return {
|
|
803
|
+
promise: promise,
|
|
804
|
+
resolve: resolve,
|
|
805
|
+
reject: reject
|
|
806
|
+
};
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
function isPromise(value) {
|
|
810
|
+
return value !== null && _typeof(value) === 'object' && typeof value.then === 'function';
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
//
|
|
814
|
+
// Checks whether the input itself is a promise
|
|
815
|
+
// or has any nested promises
|
|
816
|
+
//
|
|
817
|
+
// TODO: implement maxDepth
|
|
818
|
+
//
|
|
819
|
+
function hasNestedPromises(input) {
|
|
820
|
+
if (isPromise(input)) {
|
|
821
|
+
return true;
|
|
822
|
+
} else if (Array.isArray(input) || isPlainObject.isPlainObject(input)) {
|
|
823
|
+
return traverse(input).reduce(function (acc, value) {
|
|
824
|
+
return acc === true ? true : isPromise(value);
|
|
825
|
+
// if (acc === true) {
|
|
826
|
+
// //
|
|
827
|
+
// // Stop is not well documented
|
|
828
|
+
// //
|
|
829
|
+
// this.stop()
|
|
830
|
+
// return true
|
|
831
|
+
// } else {
|
|
832
|
+
// return isPromise(value)
|
|
833
|
+
// }
|
|
834
|
+
}, false);
|
|
835
|
+
} else {
|
|
836
|
+
return false;
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
function _makeDefaultAsyncExpFn(syncFn) {
|
|
840
|
+
return function defaultAsyncExpFn() {
|
|
841
|
+
var args = [];
|
|
842
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
843
|
+
args[_i] = arguments[_i];
|
|
844
|
+
}
|
|
845
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
846
|
+
var resolvedArgs;
|
|
847
|
+
return __generator(this, function (_a) {
|
|
848
|
+
switch (_a.label) {
|
|
849
|
+
case 0:
|
|
850
|
+
return [4 /*yield*/, Promise.all(args.map(function (arg) {
|
|
851
|
+
return resolveNestedPromises(arg);
|
|
852
|
+
}))];
|
|
853
|
+
case 1:
|
|
854
|
+
resolvedArgs = _a.sent();
|
|
855
|
+
return [2 /*return*/, syncFn.apply(void 0, resolvedArgs)];
|
|
856
|
+
}
|
|
857
|
+
});
|
|
858
|
+
});
|
|
859
|
+
};
|
|
860
|
+
}
|
|
861
|
+
function _defaultArgsContainPromises(args) {
|
|
862
|
+
return args.some(function (arg) {
|
|
863
|
+
return hasNestedPromises(arg);
|
|
864
|
+
});
|
|
865
|
+
}
|
|
866
|
+
/**
|
|
867
|
+
* Takes in a function and optionally takes in
|
|
868
|
+
* an async version of that same function.
|
|
869
|
+
* If no async version is defined, a default async
|
|
870
|
+
* version is prepared.
|
|
871
|
+
*
|
|
872
|
+
* Returns a new function that, before executing the wrapped
|
|
873
|
+
* functions verifies if there are any promises
|
|
874
|
+
* in the inputs and executes sync or async versions
|
|
875
|
+
* accordingly
|
|
876
|
+
*/
|
|
877
|
+
function maybeAsyncFn(_a) {
|
|
878
|
+
var syncFn = _a.syncFn,
|
|
879
|
+
_b = _a.asyncFn,
|
|
880
|
+
asyncFn = _b === void 0 ? _makeDefaultAsyncExpFn(syncFn) : _b,
|
|
881
|
+
//
|
|
882
|
+
// By default, argsContainPromises checks
|
|
883
|
+
// if the any of the arguments provided are
|
|
884
|
+
// themselves a promise or if they have a nested promise
|
|
885
|
+
//
|
|
886
|
+
// Allow the argsContainPromises to be configurable.
|
|
887
|
+
// This allows for more specialized checks that
|
|
888
|
+
// would perform better, some examples:
|
|
889
|
+
// - If the first argument includes a promise
|
|
890
|
+
// - If some deeply nested argument is set
|
|
891
|
+
// to a value that will incur in the need
|
|
892
|
+
// for async resolution, even if the input arg
|
|
893
|
+
// itself is not a promise
|
|
894
|
+
//
|
|
895
|
+
_c = _a.argsContainPromises,
|
|
896
|
+
//
|
|
897
|
+
// By default, argsContainPromises checks
|
|
898
|
+
// if the any of the arguments provided are
|
|
899
|
+
// themselves a promise or if they have a nested promise
|
|
900
|
+
//
|
|
901
|
+
// Allow the argsContainPromises to be configurable.
|
|
902
|
+
// This allows for more specialized checks that
|
|
903
|
+
// would perform better, some examples:
|
|
904
|
+
// - If the first argument includes a promise
|
|
905
|
+
// - If some deeply nested argument is set
|
|
906
|
+
// to a value that will incur in the need
|
|
907
|
+
// for async resolution, even if the input arg
|
|
908
|
+
// itself is not a promise
|
|
909
|
+
//
|
|
910
|
+
argsContainPromises = _c === void 0 ? _defaultArgsContainPromises : _c;
|
|
911
|
+
return function () {
|
|
912
|
+
var args = [];
|
|
913
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
914
|
+
args[_i] = arguments[_i];
|
|
915
|
+
}
|
|
916
|
+
return argsContainPromises(args) ? asyncFn.apply(void 0, args) : syncFn.apply(void 0, args);
|
|
917
|
+
};
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
function untilConditionIsSatisfiedReducer(condition, reduce) {
|
|
921
|
+
return function reducer(acc, value, index, allValues) {
|
|
922
|
+
if (isPromise(acc) || condition(acc)) {
|
|
923
|
+
//
|
|
924
|
+
// Has already resolved, as any false value
|
|
925
|
+
// should render the whole $and expression to false
|
|
926
|
+
//
|
|
927
|
+
//
|
|
928
|
+
// The previous condition returned a promise,
|
|
929
|
+
// thus assume it will properly handle resolution
|
|
930
|
+
//
|
|
931
|
+
return acc;
|
|
932
|
+
} else {
|
|
933
|
+
//
|
|
934
|
+
// Resolve currentValue
|
|
935
|
+
//
|
|
936
|
+
var syncResult = reduce(acc, value, index, allValues);
|
|
937
|
+
if (isPromise(syncResult)) {
|
|
938
|
+
var remainingValues_1 = allValues.slice(index + 1);
|
|
939
|
+
return syncResult.then(function (asyncResult) {
|
|
940
|
+
return condition(asyncResult) ? asyncResult : remainingValues_1.reduce(reducer, asyncResult);
|
|
941
|
+
});
|
|
942
|
+
} else {
|
|
943
|
+
return syncResult;
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
};
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
function maybeReturnPromise(result, parseResult) {
|
|
950
|
+
return isPromise(result) ? result.then(parseResult) : parseResult(result);
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
function syntheticJson(str) {
|
|
954
|
+
// Step 1: Replace single quotes with double quotes
|
|
955
|
+
var normalized = str.replace(/'/g, '"');
|
|
956
|
+
// Step 2: Add quotes around unquoted keys (letters, digits, underscores)
|
|
957
|
+
normalized = normalized.replace(/([a-zA-Z0-9_]+)\s*:/g, '"$1":');
|
|
958
|
+
// Step 3: Parse as JSON
|
|
959
|
+
try {
|
|
960
|
+
return JSON.parse(normalized);
|
|
961
|
+
} catch (error) {
|
|
962
|
+
throw new Error("Invalid synthetic JSON: ".concat(str));
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
function _jsonLikeParseArgs(argsStr) {
|
|
967
|
+
try {
|
|
968
|
+
argsStr = "[".concat(argsStr, "]");
|
|
969
|
+
return syntheticJson(argsStr);
|
|
970
|
+
// // Replace single quotes with double quotes
|
|
971
|
+
// argsStr = argsStr.replace(/'/g, '"')
|
|
972
|
+
// // Add double quotes around unquoted keys
|
|
973
|
+
// argsStr = argsStr.replace(/(\w+)\s*:/g, '"$1":')
|
|
974
|
+
// return JSON.parse(argsStr)
|
|
975
|
+
} catch (err) {
|
|
976
|
+
throw new Error("Failed to parse arguments: ".concat(err.message));
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
// Utility to escape regex special characters
|
|
980
|
+
function escapeRegExp(str) {
|
|
981
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
982
|
+
}
|
|
983
|
+
function strExpr(_a) {
|
|
984
|
+
var expressions = _a.expressions,
|
|
985
|
+
_b = _a.pipe,
|
|
986
|
+
pipe = _b === void 0 ? '|' : _b,
|
|
987
|
+
_c = _a.parseArgs,
|
|
988
|
+
parseArgs = _c === void 0 ? _jsonLikeParseArgs : _c;
|
|
989
|
+
var _d = Array.isArray(expressions) ? [expressions, null] : [Object.keys(expressions), expressions],
|
|
990
|
+
expressionIds = _d[0],
|
|
991
|
+
expressionFns = _d[1];
|
|
992
|
+
if (expressionIds.length === 0) {
|
|
993
|
+
throw new Error('No expressions provided');
|
|
994
|
+
}
|
|
995
|
+
//
|
|
996
|
+
// Used to split piping expressions
|
|
997
|
+
//
|
|
998
|
+
var PIPE_RE = new RegExp('\\s*' + escapeRegExp(pipe) + '\\s*', 'g');
|
|
999
|
+
//
|
|
1000
|
+
// Matches and captures the name of the expression
|
|
1001
|
+
//
|
|
1002
|
+
var EXP_NAME_PART = "(".concat(expressionIds.map(escapeRegExp).join('|'), ")");
|
|
1003
|
+
//
|
|
1004
|
+
// Optionally matches and captures arguments ("value1", "value2")
|
|
1005
|
+
//
|
|
1006
|
+
var PARAMS_PART = "(?:\\(([^)]*)\\))?";
|
|
1007
|
+
var EXPRESSIONS_RE = new RegExp("^".concat(EXP_NAME_PART).concat(PARAMS_PART, "$"));
|
|
1008
|
+
//
|
|
1009
|
+
// Takes a string in the format:
|
|
1010
|
+
// 'expr1("param", "param2") | expr2() | expr3()'
|
|
1011
|
+
//
|
|
1012
|
+
// and returns an array denoting expression name and arguments
|
|
1013
|
+
//
|
|
1014
|
+
var parse = memoizeOne(function parse(expr) {
|
|
1015
|
+
var parts = expr.split(PIPE_RE).map(function (expr) {
|
|
1016
|
+
return expr.trim();
|
|
1017
|
+
});
|
|
1018
|
+
return parts.map(function (part) {
|
|
1019
|
+
var exprMatch = part.match(EXPRESSIONS_RE);
|
|
1020
|
+
if (!exprMatch) {
|
|
1021
|
+
throw new Error("Invalid expression part '".concat(part, "'"));
|
|
1022
|
+
}
|
|
1023
|
+
var exprName = exprMatch[1];
|
|
1024
|
+
var exprArgsStr = exprMatch[2] ? exprMatch[2].trim() : '';
|
|
1025
|
+
return [exprName, exprArgsStr ? parseArgs(exprArgsStr) : []];
|
|
1026
|
+
});
|
|
1027
|
+
});
|
|
1028
|
+
//
|
|
1029
|
+
// Applies the expression against a provided input
|
|
1030
|
+
//
|
|
1031
|
+
var apply = expressionFns ? function apply(exprParts, input) {
|
|
1032
|
+
exprParts = typeof exprParts === 'string' ? parse(exprParts) : exprParts;
|
|
1033
|
+
return exprParts.reduce(function (acc, part) {
|
|
1034
|
+
var exprName = part[0];
|
|
1035
|
+
var exprArgs = part[1];
|
|
1036
|
+
var exprFn = expressionFns[exprName];
|
|
1037
|
+
if (!exprFn) {
|
|
1038
|
+
throw new Error("Expression '".concat(exprName, "' not found"));
|
|
1039
|
+
}
|
|
1040
|
+
//
|
|
1041
|
+
// It seems fn.apply is significantly faster than spread operator
|
|
1042
|
+
// Some references:
|
|
1043
|
+
// - https://jonlinnell.co.uk/articles/spread-operator-performance
|
|
1044
|
+
//
|
|
1045
|
+
var _withExprArgs = exprFn.apply(null, exprArgs);
|
|
1046
|
+
// const _withExprArgs = exprFn(...exprArgs)
|
|
1047
|
+
return maybeReturnPromise(acc, function (accRes) {
|
|
1048
|
+
return _withExprArgs(accRes);
|
|
1049
|
+
});
|
|
1050
|
+
}, input);
|
|
1051
|
+
} : function () {
|
|
1052
|
+
throw new Error('Cannot apply expression: No expression functions provided');
|
|
1053
|
+
};
|
|
1054
|
+
//
|
|
1055
|
+
// Compiles an expression into an executable function
|
|
1056
|
+
//
|
|
1057
|
+
function compile(expr) {
|
|
1058
|
+
var parsed = parse(expr);
|
|
1059
|
+
return function (input) {
|
|
1060
|
+
return apply(parsed, input);
|
|
1061
|
+
};
|
|
1062
|
+
}
|
|
1063
|
+
return {
|
|
1064
|
+
parse: parse,
|
|
1065
|
+
apply: apply,
|
|
1066
|
+
compile: compile
|
|
1067
|
+
};
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
function testCriteria(criteria, input) {
|
|
1071
|
+
switch (_typeof(criteria)) {
|
|
1072
|
+
case 'function':
|
|
1073
|
+
{
|
|
1074
|
+
return criteria(input);
|
|
1075
|
+
}
|
|
1076
|
+
case 'object':
|
|
1077
|
+
{
|
|
1078
|
+
if (criteria instanceof RegExp) {
|
|
1079
|
+
return criteria.test(input);
|
|
1080
|
+
} else {
|
|
1081
|
+
throw new Error('Unsupported criteria');
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
case 'string':
|
|
1085
|
+
case 'symbol':
|
|
1086
|
+
{
|
|
1087
|
+
return criteria === input;
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
function switchValue(input, cases) {
|
|
1092
|
+
if (Array.isArray(cases)) {
|
|
1093
|
+
var matching = cases.find(function (case_) {
|
|
1094
|
+
return case_.length === 1 || testCriteria(case_[0], input);
|
|
1095
|
+
});
|
|
1096
|
+
return matching ? matching.length === 1 ? matching[0] : matching[1] : undefined;
|
|
1097
|
+
} else {
|
|
1098
|
+
return cases[input] || cases["default"];
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
1101
|
+
function switchExec(input, cases) {
|
|
1102
|
+
var args = [];
|
|
1103
|
+
for (var _i = 2; _i < arguments.length; _i++) {
|
|
1104
|
+
args[_i - 2] = arguments[_i];
|
|
1105
|
+
}
|
|
1106
|
+
var fn = switchValue(input, cases);
|
|
1107
|
+
return fn.apply(void 0, args);
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
//
|
|
1111
|
+
// The only role of this function is to let typescript
|
|
1112
|
+
// be aware of the available types
|
|
1113
|
+
//
|
|
1114
|
+
function typeMap(typeMap) {
|
|
1115
|
+
return typeMap;
|
|
1116
|
+
}
|
|
1117
|
+
function makeTypeOf(typeMap) {
|
|
1118
|
+
return function typeOf(value) {
|
|
1119
|
+
for (var typeName in typeMap) {
|
|
1120
|
+
if (typeMap[typeName](value)) {
|
|
1121
|
+
return typeName;
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
return null;
|
|
1125
|
+
};
|
|
1126
|
+
}
|
|
1127
|
+
function basicType(typeName) {
|
|
1128
|
+
return function (value) {
|
|
1129
|
+
return _typeof(value) === typeName;
|
|
1130
|
+
};
|
|
1131
|
+
}
|
|
1132
|
+
var DEFAULT_TYPES = typeMap({
|
|
1133
|
+
//
|
|
1134
|
+
// Primitives
|
|
1135
|
+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#primitive_values
|
|
1136
|
+
//
|
|
1137
|
+
"null": function _null(value) {
|
|
1138
|
+
return value === null;
|
|
1139
|
+
},
|
|
1140
|
+
undefined: basicType('undefined'),
|
|
1141
|
+
"boolean": basicType('boolean'),
|
|
1142
|
+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN
|
|
1143
|
+
number: function number(value) {
|
|
1144
|
+
return typeof value === 'number' && !Number.isNaN(value);
|
|
1145
|
+
},
|
|
1146
|
+
bigint: basicType('bigint'),
|
|
1147
|
+
string: basicType('string'),
|
|
1148
|
+
symbol: basicType('symbol'),
|
|
1149
|
+
"function": basicType('function'),
|
|
1150
|
+
object: isPlainObject.isPlainObject,
|
|
1151
|
+
array: function array(value) {
|
|
1152
|
+
return Array.isArray(value);
|
|
1153
|
+
},
|
|
1154
|
+
regexp: function regexp(value) {
|
|
1155
|
+
return value instanceof RegExp;
|
|
1156
|
+
},
|
|
1157
|
+
nan: function nan(value) {
|
|
1158
|
+
return Number.isNaN(value);
|
|
1159
|
+
},
|
|
1160
|
+
date: function date(value) {
|
|
1161
|
+
return value instanceof Date;
|
|
1162
|
+
},
|
|
1163
|
+
map: function map(value) {
|
|
1164
|
+
return value instanceof Map;
|
|
1165
|
+
},
|
|
1166
|
+
weakmap: function weakmap(value) {
|
|
1167
|
+
return value instanceof WeakMap;
|
|
1168
|
+
},
|
|
1169
|
+
set: function set(value) {
|
|
1170
|
+
return value instanceof Set;
|
|
1171
|
+
},
|
|
1172
|
+
weakset: function weakset(value) {
|
|
1173
|
+
return value instanceof WeakSet;
|
|
1174
|
+
},
|
|
1175
|
+
promise: function promise(value) {
|
|
1176
|
+
return value instanceof Promise;
|
|
1177
|
+
},
|
|
1178
|
+
error: function error(value) {
|
|
1179
|
+
return value instanceof Error;
|
|
1180
|
+
}
|
|
1181
|
+
});
|
|
1182
|
+
var typeOf = makeTypeOf(DEFAULT_TYPES);
|
|
1183
|
+
|
|
1184
|
+
function defaultParseResponse(response) {
|
|
1185
|
+
return response.json();
|
|
1186
|
+
}
|
|
1187
|
+
function paginatedHttpFetch(_a) {
|
|
1188
|
+
var pageUrl = _a.pageUrl,
|
|
1189
|
+
_b = _a.parseResponse,
|
|
1190
|
+
parseResponse = _b === void 0 ? defaultParseResponse : _b;
|
|
1191
|
+
return function (params) {
|
|
1192
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1193
|
+
var response, data;
|
|
1194
|
+
return __generator(this, function (_a) {
|
|
1195
|
+
switch (_a.label) {
|
|
1196
|
+
case 0:
|
|
1197
|
+
return [4 /*yield*/, fetch(pageUrl(params))];
|
|
1198
|
+
case 1:
|
|
1199
|
+
response = _a.sent();
|
|
1200
|
+
return [4 /*yield*/, parseResponse(response)];
|
|
1201
|
+
case 2:
|
|
1202
|
+
data = _a.sent();
|
|
1203
|
+
return [2 /*return*/, data];
|
|
1204
|
+
}
|
|
1205
|
+
});
|
|
1206
|
+
});
|
|
1207
|
+
};
|
|
1208
|
+
}
|
|
1209
|
+
function inMemoryDataStore() {
|
|
1210
|
+
var data = [];
|
|
1211
|
+
return {
|
|
1212
|
+
getSize: function getSize() {
|
|
1213
|
+
return data.length;
|
|
1214
|
+
},
|
|
1215
|
+
storePage: function storePage(pageData) {
|
|
1216
|
+
data = __spreadArray(__spreadArray([], data, true), pageData, true);
|
|
1217
|
+
},
|
|
1218
|
+
getResult: function getResult() {
|
|
1219
|
+
return data;
|
|
1220
|
+
}
|
|
1221
|
+
};
|
|
1222
|
+
}
|
|
1223
|
+
function fetchAllPages(_a) {
|
|
1224
|
+
var fetchPage = _a.fetchPage,
|
|
1225
|
+
_b = _a.pageSize,
|
|
1226
|
+
pageSize = _b === void 0 ? 50 : _b,
|
|
1227
|
+
_c = _a.dataStore,
|
|
1228
|
+
dataStore = _c === void 0 ? inMemoryDataStore : _c;
|
|
1229
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1230
|
+
var store, currentPageData, currentOffset, result;
|
|
1231
|
+
return __generator(this, function (_d) {
|
|
1232
|
+
switch (_d.label) {
|
|
1233
|
+
case 0:
|
|
1234
|
+
store = dataStore();
|
|
1235
|
+
currentPageData = null;
|
|
1236
|
+
_d.label = 1;
|
|
1237
|
+
case 1:
|
|
1238
|
+
return [4 /*yield*/, store.getSize()];
|
|
1239
|
+
case 2:
|
|
1240
|
+
currentOffset = _d.sent();
|
|
1241
|
+
return [4 /*yield*/, fetchPage({
|
|
1242
|
+
offset: currentOffset,
|
|
1243
|
+
pageSize: pageSize
|
|
1244
|
+
})];
|
|
1245
|
+
case 3:
|
|
1246
|
+
currentPageData = _d.sent();
|
|
1247
|
+
return [4 /*yield*/, store.storePage(currentPageData, {
|
|
1248
|
+
offset: currentOffset,
|
|
1249
|
+
pageSize: pageSize
|
|
1250
|
+
})];
|
|
1251
|
+
case 4:
|
|
1252
|
+
_d.sent();
|
|
1253
|
+
_d.label = 5;
|
|
1254
|
+
case 5:
|
|
1255
|
+
if (currentPageData.length === pageSize) return [3 /*break*/, 1];
|
|
1256
|
+
_d.label = 6;
|
|
1257
|
+
case 6:
|
|
1258
|
+
return [4 /*yield*/, store.getResult()];
|
|
1259
|
+
case 7:
|
|
1260
|
+
result = _d.sent();
|
|
1261
|
+
return [2 /*return*/, result];
|
|
1262
|
+
}
|
|
1263
|
+
});
|
|
1264
|
+
});
|
|
1265
|
+
}
|
|
1266
|
+
|
|
1267
|
+
function deprecateFn(fn, message) {
|
|
1268
|
+
// Creating a new function that wraps the original function
|
|
1269
|
+
var wrappedFunction = function wrappedFunction() {
|
|
1270
|
+
var args = [];
|
|
1271
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
1272
|
+
args[_i] = arguments[_i];
|
|
1273
|
+
}
|
|
1274
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
1275
|
+
console.warn(["[deprecated][fn] ".concat(fn.name, " is deprecated"), message].filter(Boolean).join(': '));
|
|
1276
|
+
}
|
|
1277
|
+
return fn.apply(void 0, args);
|
|
1278
|
+
};
|
|
1279
|
+
// Copying the original function's properties to the new function
|
|
1280
|
+
Object.assign(wrappedFunction, fn);
|
|
1281
|
+
// Setting the name property explicitly to preserve the original function's name
|
|
1282
|
+
Object.defineProperty(wrappedFunction, 'name', {
|
|
1283
|
+
value: "".concat(fn.name, "_DEPRECATED"),
|
|
1284
|
+
configurable: true
|
|
1285
|
+
});
|
|
1286
|
+
return wrappedFunction;
|
|
1287
|
+
}
|
|
1288
|
+
function deprecateProperty(obj, key, value, message) {
|
|
1289
|
+
// Create a new object that includes all properties of the original object
|
|
1290
|
+
var newObj = Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));
|
|
1291
|
+
Object.defineProperty(newObj, key, {
|
|
1292
|
+
get: function get() {
|
|
1293
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
1294
|
+
console.warn(["[deprecated][property] ".concat(key, " is deprecated"), message].filter(Boolean).join(': '));
|
|
1295
|
+
}
|
|
1296
|
+
return value;
|
|
1297
|
+
}
|
|
1298
|
+
});
|
|
1299
|
+
return newObj;
|
|
1300
|
+
}
|
|
1301
|
+
function deprecateInput(inputValue, message, convert) {
|
|
1302
|
+
if (typeof inputValue === 'undefined') {
|
|
1303
|
+
return undefined;
|
|
1304
|
+
} else {
|
|
1305
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
1306
|
+
console.warn("[deprecated][input] ".concat(message, " | received: ").concat(inputValue));
|
|
1307
|
+
}
|
|
1308
|
+
return typeof convert === 'function' ? convert(inputValue) : inputValue;
|
|
1309
|
+
}
|
|
1310
|
+
}
|
|
1311
|
+
|
|
1312
|
+
function slugify(str, delimiter) {
|
|
1313
|
+
if (delimiter === void 0) {
|
|
1314
|
+
delimiter = '-';
|
|
1315
|
+
}
|
|
1316
|
+
// Escape the delimiter for safe use in regex
|
|
1317
|
+
var safeDelimiter = delimiter.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
|
|
1318
|
+
var CONSECUTIVE_DELIMITER_RE = new RegExp("".concat(safeDelimiter, "+"), 'g');
|
|
1319
|
+
var LEADING_TRAILING_DELIMITER_RE = new RegExp("^".concat(safeDelimiter, "+|").concat(safeDelimiter, "+$"), 'g');
|
|
1320
|
+
return str.trim() // Trim leading and trailing spaces
|
|
1321
|
+
.toLowerCase() // Convert to lowercase
|
|
1322
|
+
.normalize('NFD') // Decompose accented characters
|
|
1323
|
+
.replace(/[\u0300-\u036f]/g, '') // Remove diacritics
|
|
1324
|
+
.replace(/[^a-z0-9]+/g, delimiter) // Replace non-alphanumeric with hyphens
|
|
1325
|
+
.replace(CONSECUTIVE_DELIMITER_RE, delimiter) // Collapse consecutive hyphens
|
|
1326
|
+
.replace(LEADING_TRAILING_DELIMITER_RE, ''); // Remove leading/trailing hyphens
|
|
1327
|
+
}
|
|
1328
|
+
var normalizeString = deprecateFn(slugify, 'normalizeString is depreacted, use @orioro/util -> slugify instead');
|
|
1329
|
+
|
|
1330
|
+
//
|
|
1331
|
+
// Hook module
|
|
1332
|
+
//
|
|
1333
|
+
function hookFn(fn, _a) {
|
|
1334
|
+
var input = _a.input,
|
|
1335
|
+
validate = _a.validate,
|
|
1336
|
+
before = _a.before,
|
|
1337
|
+
output = _a.output,
|
|
1338
|
+
after = _a.after;
|
|
1339
|
+
return function () {
|
|
1340
|
+
var args = [];
|
|
1341
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
1342
|
+
args[_i] = arguments[_i];
|
|
1343
|
+
}
|
|
1344
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1345
|
+
var _input, _a, validationResult, error, rawResult, result, _b;
|
|
1346
|
+
return __generator(this, function (_c) {
|
|
1347
|
+
switch (_c.label) {
|
|
1348
|
+
case 0:
|
|
1349
|
+
if (!(typeof input === 'function')) return [3 /*break*/, 2];
|
|
1350
|
+
return [4 /*yield*/, input.apply(void 0, args)];
|
|
1351
|
+
case 1:
|
|
1352
|
+
_a = _c.sent();
|
|
1353
|
+
return [3 /*break*/, 3];
|
|
1354
|
+
case 2:
|
|
1355
|
+
_a = args;
|
|
1356
|
+
_c.label = 3;
|
|
1357
|
+
case 3:
|
|
1358
|
+
_input = _a;
|
|
1359
|
+
if (!Array.isArray(_input)) {
|
|
1360
|
+
throw new TypeError('Input hook must resolve an array of args');
|
|
1361
|
+
}
|
|
1362
|
+
if (!(typeof validate === 'function')) return [3 /*break*/, 5];
|
|
1363
|
+
return [4 /*yield*/, validate.apply(void 0, _input)];
|
|
1364
|
+
case 4:
|
|
1365
|
+
validationResult = _c.sent();
|
|
1366
|
+
if (validationResult !== true) {
|
|
1367
|
+
error = typeof validationResult === 'string' ? new Error(validationResult) : validationResult instanceof Error ? validationResult : new Error('Invalid input');
|
|
1368
|
+
throw error;
|
|
1369
|
+
}
|
|
1370
|
+
_c.label = 5;
|
|
1371
|
+
case 5:
|
|
1372
|
+
if (!(typeof before === 'function')) return [3 /*break*/, 7];
|
|
1373
|
+
return [4 /*yield*/, before.apply(void 0, _input)];
|
|
1374
|
+
case 6:
|
|
1375
|
+
_c.sent();
|
|
1376
|
+
_c.label = 7;
|
|
1377
|
+
case 7:
|
|
1378
|
+
return [4 /*yield*/, fn.apply(void 0, _input)];
|
|
1379
|
+
case 8:
|
|
1380
|
+
rawResult = _c.sent();
|
|
1381
|
+
if (!(typeof output === 'function')) return [3 /*break*/, 10];
|
|
1382
|
+
return [4 /*yield*/, output(rawResult)];
|
|
1383
|
+
case 9:
|
|
1384
|
+
_b = _c.sent();
|
|
1385
|
+
return [3 /*break*/, 11];
|
|
1386
|
+
case 10:
|
|
1387
|
+
_b = rawResult;
|
|
1388
|
+
_c.label = 11;
|
|
1389
|
+
case 11:
|
|
1390
|
+
result = _b;
|
|
1391
|
+
if (!(typeof after === 'function')) return [3 /*break*/, 13];
|
|
1392
|
+
return [4 /*yield*/, after.apply(void 0, __spreadArray([result], _input, false))];
|
|
1393
|
+
case 12:
|
|
1394
|
+
_c.sent();
|
|
1395
|
+
_c.label = 13;
|
|
1396
|
+
case 13:
|
|
1397
|
+
return [2 /*return*/, result];
|
|
1398
|
+
}
|
|
1399
|
+
});
|
|
1400
|
+
});
|
|
1401
|
+
};
|
|
1402
|
+
}
|
|
1403
|
+
|
|
1404
|
+
function isValidUrl(url) {
|
|
1405
|
+
try {
|
|
1406
|
+
new URL(url);
|
|
1407
|
+
return true;
|
|
1408
|
+
} catch (_a) {
|
|
1409
|
+
return false;
|
|
1410
|
+
}
|
|
1411
|
+
}
|
|
1412
|
+
|
|
1413
|
+
//
|
|
1414
|
+
// https://nodejs.org/api/url.html#url-strings-and-url-objects
|
|
1415
|
+
//
|
|
1416
|
+
// ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
|
|
1417
|
+
// │ href │
|
|
1418
|
+
// ├──────────┬──┬─────────────────────┬────────────────────────┬───────────────────────────┬───────┤
|
|
1419
|
+
// │ protocol │ │ auth │ host │ path │ hash │
|
|
1420
|
+
// │ │ │ ├─────────────────┬──────┼──────────┬────────────────┤ │
|
|
1421
|
+
// │ │ │ │ hostname │ port │ pathname │ search │ │
|
|
1422
|
+
// │ │ │ │ │ │ ├─┬──────────────┤ │
|
|
1423
|
+
// │ │ │ │ │ │ │ │ query │ │
|
|
1424
|
+
// " https: // user : pass @ sub.example.com : 8080 /p/a/t/h ? query=string #hash "
|
|
1425
|
+
// │ │ │ │ │ hostname │ port │ │ │ │
|
|
1426
|
+
// │ │ │ │ ├─────────────────┴──────┤ │ │ │
|
|
1427
|
+
// │ protocol │ │ username │ password │ host │ │ │ │
|
|
1428
|
+
// ├──────────┴──┼──────────┴──────────┼────────────────────────┤ │ │ │
|
|
1429
|
+
// │ origin │ │ origin │ pathname │ search │ hash │
|
|
1430
|
+
// ├─────────────┴─────────────────────┴────────────────────────┴──────────┴────────────────┴───────┤
|
|
1431
|
+
// │ href │
|
|
1432
|
+
// └────────────────────────────────────────────────────────────────────────────────────────────────┘
|
|
1433
|
+
//
|
|
1434
|
+
var DEFAULT_PROTOCOL = 'https:';
|
|
1435
|
+
function _protocol(protocol) {
|
|
1436
|
+
return !protocol ? DEFAULT_PROTOCOL : protocol.endsWith(':') ? protocol : "".concat(protocol, ":");
|
|
1437
|
+
}
|
|
1438
|
+
function _baseHref(spec) {
|
|
1439
|
+
if (spec.href) {
|
|
1440
|
+
return spec.href;
|
|
1441
|
+
} else {
|
|
1442
|
+
var protocol = _protocol(spec.protocol);
|
|
1443
|
+
var host = spec.host || spec.hostname;
|
|
1444
|
+
if (!host) {
|
|
1445
|
+
throw new Error("Invalid URL spec: either 'href', 'host' or 'hostname' must be defined.");
|
|
1446
|
+
}
|
|
1447
|
+
return "".concat(protocol, "//").concat(host);
|
|
1448
|
+
}
|
|
1449
|
+
}
|
|
1450
|
+
function url(spec) {
|
|
1451
|
+
if (typeof spec === 'string') {
|
|
1452
|
+
return new URL(spec).href;
|
|
1453
|
+
} else {
|
|
1454
|
+
var url_1 = new URL(_baseHref(spec));
|
|
1455
|
+
if (spec.protocol) url_1.protocol = spec.protocol;
|
|
1456
|
+
if (spec.username) url_1.username = spec.username;
|
|
1457
|
+
if (spec.password) url_1.password = spec.password;
|
|
1458
|
+
if (spec.host) url_1.host = spec.host;
|
|
1459
|
+
if (spec.hostname) url_1.hostname = spec.hostname;
|
|
1460
|
+
if (spec.port) url_1.port = spec.port;
|
|
1461
|
+
if (spec.pathname) url_1.pathname = spec.pathname;
|
|
1462
|
+
if (spec.search) url_1.search = spec.search;
|
|
1463
|
+
// Handle searchParams
|
|
1464
|
+
if (spec.searchParams) {
|
|
1465
|
+
var searchString = Array.isArray(spec.searchParams) ? queryString.stringify(spec.searchParams[0], spec.searchParams[1]) : queryString.stringify(spec.searchParams);
|
|
1466
|
+
url_1.search = searchString;
|
|
1467
|
+
}
|
|
1468
|
+
if (spec.hash) url_1.hash = spec.hash;
|
|
1469
|
+
return url_1.href;
|
|
1470
|
+
}
|
|
1471
|
+
}
|
|
1472
|
+
|
|
1473
|
+
exports.DEFAULT_TYPES = DEFAULT_TYPES;
|
|
1474
|
+
exports.PromiseLikeEventEmitter = PromiseLikeEventEmitter;
|
|
1475
|
+
exports.SKIPPED = SKIPPED;
|
|
1476
|
+
exports.TimeoutError = TimeoutError;
|
|
1477
|
+
exports.arrayChunk = arrayChunk;
|
|
1478
|
+
exports.batchFn = batchFn;
|
|
1479
|
+
exports.dataJoin = dataJoin;
|
|
1480
|
+
exports.debugFn = debugFn;
|
|
1481
|
+
exports.deepFreeze = deepFreeze;
|
|
1482
|
+
exports.deprecateFn = deprecateFn;
|
|
1483
|
+
exports.deprecateInput = deprecateInput;
|
|
1484
|
+
exports.deprecateProperty = deprecateProperty;
|
|
1485
|
+
exports.fetchAllPages = fetchAllPages;
|
|
1486
|
+
exports.hasNestedPromises = hasNestedPromises;
|
|
1487
|
+
exports.hookFn = hookFn;
|
|
1488
|
+
exports.inMemoryDataStore = inMemoryDataStore;
|
|
1489
|
+
exports.interpolate = interpolate;
|
|
1490
|
+
exports.isPromise = isPromise;
|
|
1491
|
+
exports.isValidUrl = isValidUrl;
|
|
1492
|
+
exports.makeDeferred = makeDeferred;
|
|
1493
|
+
exports.makeTypeOf = makeTypeOf;
|
|
1494
|
+
exports.maybeAsyncFn = maybeAsyncFn;
|
|
1495
|
+
exports.maybeFn = maybeFn;
|
|
1496
|
+
exports.maybeReturnPromise = maybeReturnPromise;
|
|
1497
|
+
exports.normalizeString = normalizeString;
|
|
1498
|
+
exports.paginatedHttpFetch = paginatedHttpFetch;
|
|
1499
|
+
exports.parseBatchedResults = parseBatchedResults;
|
|
1500
|
+
exports.pickPaths = pickPaths;
|
|
1501
|
+
exports.promiseReduce = promiseReduce;
|
|
1502
|
+
exports.resolveNestedPromises = resolveNestedPromises;
|
|
1503
|
+
exports.sequentialCallIdGenerator = sequentialCallIdGenerator;
|
|
1504
|
+
exports.slugify = slugify;
|
|
1505
|
+
exports.strExpr = strExpr;
|
|
1506
|
+
exports.switchExec = switchExec;
|
|
1507
|
+
exports.switchValue = switchValue;
|
|
1508
|
+
exports.syntheticJson = syntheticJson;
|
|
1509
|
+
exports.typeMap = typeMap;
|
|
1510
|
+
exports.typeOf = typeOf;
|
|
1511
|
+
exports.untilConditionIsSatisfiedReducer = untilConditionIsSatisfiedReducer;
|
|
1512
|
+
exports.url = url;
|
|
1513
|
+
exports.wait = wait;
|
|
1514
|
+
exports.withTimeout = withTimeout;
|