awilix 11.0.3 → 12.0.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/lib/awilix.browser.js +301 -383
- package/lib/awilix.d.ts +6 -5
- package/lib/awilix.js +4 -1
- package/lib/awilix.js.map +1 -1
- package/lib/awilix.module.mjs +2 -1
- package/lib/awilix.umd.js +1465 -1527
- package/lib/utils.d.ts +1 -1
- package/lib/utils.js +1 -0
- package/lib/utils.js.map +1 -1
- package/package.json +22 -19
package/lib/awilix.browser.js
CHANGED
|
@@ -1,147 +1,56 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
5
|
-
purpose with or without fee is hereby granted.
|
|
6
|
-
|
|
7
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
8
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
9
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
10
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
11
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
12
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
13
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
14
|
-
***************************************************************************** */
|
|
15
|
-
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
|
|
16
|
-
|
|
17
|
-
var extendStatics = function(d, b) {
|
|
18
|
-
extendStatics = Object.setPrototypeOf ||
|
|
19
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
20
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
21
|
-
return extendStatics(d, b);
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
function __extends(d, b) {
|
|
25
|
-
if (typeof b !== "function" && b !== null)
|
|
26
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
27
|
-
extendStatics(d, b);
|
|
28
|
-
function __() { this.constructor = d; }
|
|
29
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
var __assign = function() {
|
|
33
|
-
__assign = Object.assign || function __assign(t) {
|
|
34
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
35
|
-
s = arguments[i];
|
|
36
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
37
|
-
}
|
|
38
|
-
return t;
|
|
39
|
-
};
|
|
40
|
-
return __assign.apply(this, arguments);
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
function __generator(thisArg, body) {
|
|
44
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
45
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
46
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
47
|
-
function step(op) {
|
|
48
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
49
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
50
|
-
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;
|
|
51
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
52
|
-
switch (op[0]) {
|
|
53
|
-
case 0: case 1: t = op; break;
|
|
54
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
55
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
56
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
57
|
-
default:
|
|
58
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
59
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
60
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
61
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
62
|
-
if (t[2]) _.ops.pop();
|
|
63
|
-
_.trys.pop(); continue;
|
|
64
|
-
}
|
|
65
|
-
op = body.call(thisArg, _);
|
|
66
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
67
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function __spreadArray(to, from, pack) {
|
|
72
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
73
|
-
if (ar || !(i in from)) {
|
|
74
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
75
|
-
ar[i] = from[i];
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
82
|
-
var e = new Error(message);
|
|
83
|
-
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
84
|
-
};
|
|
1
|
+
import * as glob from 'fast-glob';
|
|
2
|
+
import * as path from 'path';
|
|
85
3
|
|
|
86
4
|
/**
|
|
87
5
|
* Newline.
|
|
88
6
|
*/
|
|
89
|
-
|
|
7
|
+
const EOL = '\n';
|
|
90
8
|
/**
|
|
91
9
|
* An extendable error class.
|
|
92
10
|
* @author https://github.com/bjyoungblood/es6-error/
|
|
93
11
|
*/
|
|
94
|
-
|
|
95
|
-
__extends(ExtendableError, _super);
|
|
12
|
+
class ExtendableError extends Error {
|
|
96
13
|
/**
|
|
97
14
|
* Constructor for the error.
|
|
98
15
|
*
|
|
99
16
|
* @param {String} message
|
|
100
17
|
* The error message.
|
|
101
18
|
*/
|
|
102
|
-
|
|
103
|
-
|
|
19
|
+
constructor(message) {
|
|
20
|
+
super(message);
|
|
104
21
|
// extending Error is weird and does not propagate `message`
|
|
105
|
-
Object.defineProperty(
|
|
22
|
+
Object.defineProperty(this, 'message', {
|
|
106
23
|
enumerable: false,
|
|
107
24
|
value: message,
|
|
108
25
|
});
|
|
109
|
-
Object.defineProperty(
|
|
26
|
+
Object.defineProperty(this, 'name', {
|
|
110
27
|
enumerable: false,
|
|
111
|
-
value:
|
|
28
|
+
value: this.constructor.name,
|
|
112
29
|
});
|
|
113
30
|
// Not all browsers have this function.
|
|
114
31
|
/* istanbul ignore else */
|
|
115
32
|
if ('captureStackTrace' in Error) {
|
|
116
|
-
Error.captureStackTrace(
|
|
33
|
+
Error.captureStackTrace(this, this.constructor);
|
|
117
34
|
}
|
|
118
35
|
else {
|
|
119
|
-
Object.defineProperty(
|
|
36
|
+
Object.defineProperty(this, 'stack', {
|
|
120
37
|
enumerable: false,
|
|
121
38
|
value: Error(message).stack,
|
|
122
39
|
writable: true,
|
|
123
40
|
configurable: true,
|
|
124
41
|
});
|
|
125
42
|
}
|
|
126
|
-
return _this;
|
|
127
43
|
}
|
|
128
|
-
|
|
129
|
-
}(Error));
|
|
44
|
+
}
|
|
130
45
|
/**
|
|
131
46
|
* Base error for all Awilix-specific errors.
|
|
132
47
|
*/
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
function AwilixError() {
|
|
136
|
-
return _super !== null && _super.apply(this, arguments) || this;
|
|
137
|
-
}
|
|
138
|
-
return AwilixError;
|
|
139
|
-
}(ExtendableError));
|
|
48
|
+
class AwilixError extends ExtendableError {
|
|
49
|
+
}
|
|
140
50
|
/**
|
|
141
51
|
* Error thrown to indicate a type mismatch.
|
|
142
52
|
*/
|
|
143
|
-
|
|
144
|
-
__extends(AwilixTypeError, _super);
|
|
53
|
+
class AwilixTypeError extends AwilixError {
|
|
145
54
|
/**
|
|
146
55
|
* Constructor, takes the function name, expected and given
|
|
147
56
|
* type to produce an error.
|
|
@@ -158,8 +67,8 @@ var AwilixTypeError = /** @class */ (function (_super) {
|
|
|
158
67
|
* @param {string} givenType
|
|
159
68
|
* Name of the given type.
|
|
160
69
|
*/
|
|
161
|
-
|
|
162
|
-
|
|
70
|
+
constructor(funcDescription, paramName, expectedType, givenType) {
|
|
71
|
+
super(`${funcDescription}: expected ${paramName} to be ${expectedType}, but got ${givenType}.`);
|
|
163
72
|
}
|
|
164
73
|
/**
|
|
165
74
|
* Asserts the given condition, throws an error otherwise.
|
|
@@ -179,19 +88,17 @@ var AwilixTypeError = /** @class */ (function (_super) {
|
|
|
179
88
|
* @param {string} givenType
|
|
180
89
|
* Name of the given type.
|
|
181
90
|
*/
|
|
182
|
-
|
|
91
|
+
static assert(condition, funcDescription, paramName, expectedType, givenType) {
|
|
183
92
|
if (!condition) {
|
|
184
93
|
throw new AwilixTypeError(funcDescription, paramName, expectedType, givenType);
|
|
185
94
|
}
|
|
186
95
|
return condition;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
}(AwilixError));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
190
98
|
/**
|
|
191
99
|
* A nice error class so we can do an instanceOf check.
|
|
192
100
|
*/
|
|
193
|
-
|
|
194
|
-
__extends(AwilixResolutionError, _super);
|
|
101
|
+
class AwilixResolutionError extends AwilixError {
|
|
195
102
|
/**
|
|
196
103
|
* Constructor, takes the registered modules and unresolved tokens
|
|
197
104
|
* to create a message.
|
|
@@ -202,29 +109,24 @@ var AwilixResolutionError = /** @class */ (function (_super) {
|
|
|
202
109
|
* @param {string[]} resolutionStack
|
|
203
110
|
* The current resolution stack
|
|
204
111
|
*/
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
var val = _a.name;
|
|
209
|
-
return val.toString();
|
|
210
|
-
});
|
|
112
|
+
constructor(name, resolutionStack, message) {
|
|
113
|
+
const stringName = name.toString();
|
|
114
|
+
const nameStack = resolutionStack.map(({ name: val }) => val.toString());
|
|
211
115
|
nameStack.push(stringName);
|
|
212
|
-
|
|
213
|
-
|
|
116
|
+
const resolutionPathString = nameStack.join(' -> ');
|
|
117
|
+
let msg = `Could not resolve '${stringName}'.`;
|
|
214
118
|
if (message) {
|
|
215
|
-
msg +=
|
|
119
|
+
msg += ` ${message}`;
|
|
216
120
|
}
|
|
217
121
|
msg += EOL + EOL;
|
|
218
|
-
msg +=
|
|
219
|
-
|
|
122
|
+
msg += `Resolution path: ${resolutionPathString}`;
|
|
123
|
+
super(msg);
|
|
220
124
|
}
|
|
221
|
-
|
|
222
|
-
}(AwilixError));
|
|
125
|
+
}
|
|
223
126
|
/**
|
|
224
127
|
* A nice error class so we can do an instanceOf check.
|
|
225
128
|
*/
|
|
226
|
-
|
|
227
|
-
__extends(AwilixRegistrationError, _super);
|
|
129
|
+
class AwilixRegistrationError extends AwilixError {
|
|
228
130
|
/**
|
|
229
131
|
* Constructor, takes the registered modules and unresolved tokens
|
|
230
132
|
* to create a message.
|
|
@@ -232,21 +134,20 @@ var AwilixRegistrationError = /** @class */ (function (_super) {
|
|
|
232
134
|
* @param {string|symbol} name
|
|
233
135
|
* The name of the module that could not be registered.
|
|
234
136
|
*/
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
137
|
+
constructor(name, message) {
|
|
138
|
+
const stringName = name.toString();
|
|
139
|
+
let msg = `Could not register '${stringName}'.`;
|
|
238
140
|
if (message) {
|
|
239
|
-
msg +=
|
|
141
|
+
msg += ` ${message}`;
|
|
240
142
|
}
|
|
241
|
-
|
|
143
|
+
super(msg);
|
|
242
144
|
}
|
|
243
|
-
|
|
244
|
-
}(AwilixError));
|
|
145
|
+
}
|
|
245
146
|
|
|
246
147
|
/**
|
|
247
148
|
* Resolution modes.
|
|
248
149
|
*/
|
|
249
|
-
|
|
150
|
+
const InjectionMode = {
|
|
250
151
|
/**
|
|
251
152
|
* The dependencies will be resolved by injecting the cradle proxy.
|
|
252
153
|
*
|
|
@@ -264,7 +165,7 @@ var InjectionMode = {
|
|
|
264
165
|
/**
|
|
265
166
|
* Lifetime types.
|
|
266
167
|
*/
|
|
267
|
-
|
|
168
|
+
const Lifetime = {
|
|
268
169
|
/**
|
|
269
170
|
* The registration will be resolved once and only once.
|
|
270
171
|
* @type {String}
|
|
@@ -295,24 +196,23 @@ function isLifetimeLonger(a, b) {
|
|
|
295
196
|
* @param source
|
|
296
197
|
*/
|
|
297
198
|
function createTokenizer(source) {
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
199
|
+
const end = source.length;
|
|
200
|
+
let pos = 0;
|
|
201
|
+
let type = 'EOF';
|
|
202
|
+
let value = '';
|
|
203
|
+
let flags = 0 /* TokenizerFlags.None */;
|
|
303
204
|
// These are used to greedily skip as much as possible.
|
|
304
205
|
// Whenever we reach a paren, we increment these.
|
|
305
|
-
|
|
306
|
-
|
|
206
|
+
let parenLeft = 0;
|
|
207
|
+
let parenRight = 0;
|
|
307
208
|
return {
|
|
308
|
-
next
|
|
309
|
-
done
|
|
209
|
+
next,
|
|
210
|
+
done,
|
|
310
211
|
};
|
|
311
212
|
/**
|
|
312
213
|
* Advances the tokenizer and returns the next token.
|
|
313
214
|
*/
|
|
314
|
-
function next(nextFlags) {
|
|
315
|
-
if (nextFlags === void 0) { nextFlags = 0 /* TokenizerFlags.None */; }
|
|
215
|
+
function next(nextFlags = 0 /* TokenizerFlags.None */) {
|
|
316
216
|
flags = nextFlags;
|
|
317
217
|
advance();
|
|
318
218
|
return createToken();
|
|
@@ -327,7 +227,7 @@ function createTokenizer(source) {
|
|
|
327
227
|
if (pos >= end) {
|
|
328
228
|
return (type = 'EOF');
|
|
329
229
|
}
|
|
330
|
-
|
|
230
|
+
const ch = source.charAt(pos);
|
|
331
231
|
// Whitespace is irrelevant
|
|
332
232
|
if (isWhiteSpace(ch)) {
|
|
333
233
|
pos++;
|
|
@@ -359,14 +259,14 @@ function createTokenizer(source) {
|
|
|
359
259
|
return (type = ch);
|
|
360
260
|
case '/': {
|
|
361
261
|
pos++;
|
|
362
|
-
|
|
262
|
+
const nextCh = source.charAt(pos);
|
|
363
263
|
if (nextCh === '/') {
|
|
364
|
-
skipUntil(
|
|
264
|
+
skipUntil((c) => c === '\n', true);
|
|
365
265
|
pos++;
|
|
366
266
|
}
|
|
367
267
|
if (nextCh === '*') {
|
|
368
|
-
skipUntil(
|
|
369
|
-
|
|
268
|
+
skipUntil((c) => {
|
|
269
|
+
const closing = source.charAt(pos + 1);
|
|
370
270
|
return c === '*' && closing === '/';
|
|
371
271
|
}, true);
|
|
372
272
|
pos++;
|
|
@@ -389,8 +289,8 @@ function createTokenizer(source) {
|
|
|
389
289
|
* we are ready to do so.
|
|
390
290
|
*/
|
|
391
291
|
function scanIdentifier() {
|
|
392
|
-
|
|
393
|
-
|
|
292
|
+
const identStart = source.charAt(pos);
|
|
293
|
+
const start = ++pos;
|
|
394
294
|
while (isIdentifierPart(source.charAt(pos))) {
|
|
395
295
|
pos++;
|
|
396
296
|
}
|
|
@@ -406,8 +306,8 @@ function createTokenizer(source) {
|
|
|
406
306
|
* Checks the parenthesis balance so we correctly skip function calls.
|
|
407
307
|
*/
|
|
408
308
|
function skipExpression() {
|
|
409
|
-
skipUntil(
|
|
410
|
-
|
|
309
|
+
skipUntil((ch) => {
|
|
310
|
+
const isAtRoot = parenLeft === parenRight + 1;
|
|
411
311
|
if (ch === ',' && isAtRoot) {
|
|
412
312
|
return true;
|
|
413
313
|
}
|
|
@@ -431,10 +331,9 @@ function createTokenizer(source) {
|
|
|
431
331
|
* @param dumb if `true`, does not skip whitespace and strings;
|
|
432
332
|
* it only stops once the callback returns `true`.
|
|
433
333
|
*/
|
|
434
|
-
function skipUntil(callback, dumb) {
|
|
435
|
-
if (dumb === void 0) { dumb = false; }
|
|
334
|
+
function skipUntil(callback, dumb = false) {
|
|
436
335
|
while (pos < source.length) {
|
|
437
|
-
|
|
336
|
+
const ch = source.charAt(pos);
|
|
438
337
|
if (callback(ch)) {
|
|
439
338
|
return;
|
|
440
339
|
}
|
|
@@ -455,11 +354,11 @@ function createTokenizer(source) {
|
|
|
455
354
|
* Given the current position is at a string quote, skips the entire string.
|
|
456
355
|
*/
|
|
457
356
|
function skipString() {
|
|
458
|
-
|
|
357
|
+
const quote = source.charAt(pos);
|
|
459
358
|
pos++;
|
|
460
359
|
while (pos < source.length) {
|
|
461
|
-
|
|
462
|
-
|
|
360
|
+
const ch = source.charAt(pos);
|
|
361
|
+
const prev = source.charAt(pos - 1);
|
|
463
362
|
// Checks if the quote was escaped.
|
|
464
363
|
if (ch === quote && prev !== '\\') {
|
|
465
364
|
pos++;
|
|
@@ -467,15 +366,15 @@ function createTokenizer(source) {
|
|
|
467
366
|
}
|
|
468
367
|
// Template strings are a bit tougher, we want to skip the interpolated values.
|
|
469
368
|
if (quote === '`') {
|
|
470
|
-
|
|
471
|
-
if (
|
|
472
|
-
|
|
369
|
+
const next = source.charAt(pos + 1);
|
|
370
|
+
if (next === '$') {
|
|
371
|
+
const afterDollar = source.charAt(pos + 2);
|
|
473
372
|
if (afterDollar === '{') {
|
|
474
373
|
// This is the start of an interpolation; skip the ${
|
|
475
374
|
pos = pos + 2;
|
|
476
375
|
// Skip strings and whitespace until we reach the ending }.
|
|
477
376
|
// This includes skipping nested interpolated strings. :D
|
|
478
|
-
skipUntil(
|
|
377
|
+
skipUntil((ch) => ch === '}');
|
|
479
378
|
}
|
|
480
379
|
}
|
|
481
380
|
}
|
|
@@ -487,9 +386,9 @@ function createTokenizer(source) {
|
|
|
487
386
|
*/
|
|
488
387
|
function createToken() {
|
|
489
388
|
if (value) {
|
|
490
|
-
return { value
|
|
389
|
+
return { value, type };
|
|
491
390
|
}
|
|
492
|
-
return { type
|
|
391
|
+
return { type };
|
|
493
392
|
}
|
|
494
393
|
/**
|
|
495
394
|
* Determines if we are done parsing.
|
|
@@ -530,8 +429,8 @@ function isStringQuote(ch) {
|
|
|
530
429
|
// NOTE: I've added the `.` character so that member expression paths
|
|
531
430
|
// are seen as identifiers. This is so we don't get a constructor token for
|
|
532
431
|
// stuff like `MyClass.prototype.constructor()`
|
|
533
|
-
|
|
534
|
-
|
|
432
|
+
const IDENT_START_EXPR = /^[_$a-zA-Z\xA0-\uFFFF]$/;
|
|
433
|
+
const IDENT_PART_EXPR = /^[._$a-zA-Z0-9\xA0-\uFFFF]$/;
|
|
535
434
|
/**
|
|
536
435
|
* Determines if the character is a valid JS identifier start character.
|
|
537
436
|
*/
|
|
@@ -545,6 +444,24 @@ function isIdentifierPart(ch) {
|
|
|
545
444
|
return IDENT_PART_EXPR.test(ch);
|
|
546
445
|
}
|
|
547
446
|
|
|
447
|
+
/**
|
|
448
|
+
* Quick flatten utility to flatten a 2-dimensional array.
|
|
449
|
+
*
|
|
450
|
+
* @param {Array<Array<Item>>} array
|
|
451
|
+
* The array to flatten.
|
|
452
|
+
*
|
|
453
|
+
* @return {Array<Item>}
|
|
454
|
+
* The flattened array.
|
|
455
|
+
*/
|
|
456
|
+
function flatten(array) {
|
|
457
|
+
const result = [];
|
|
458
|
+
array.forEach((arr) => {
|
|
459
|
+
arr.forEach((item) => {
|
|
460
|
+
result.push(item);
|
|
461
|
+
});
|
|
462
|
+
});
|
|
463
|
+
return result;
|
|
464
|
+
}
|
|
548
465
|
/**
|
|
549
466
|
* Creates a { name: value } object if the input isn't already in that format.
|
|
550
467
|
*
|
|
@@ -557,10 +474,9 @@ function isIdentifierPart(ch) {
|
|
|
557
474
|
* @return {object}
|
|
558
475
|
*/
|
|
559
476
|
function nameValueToObject(name, value) {
|
|
560
|
-
|
|
561
|
-
var obj = name;
|
|
477
|
+
const obj = name;
|
|
562
478
|
if (typeof obj === 'string' || typeof obj === 'symbol') {
|
|
563
|
-
return
|
|
479
|
+
return { [name]: value };
|
|
564
480
|
}
|
|
565
481
|
return obj;
|
|
566
482
|
}
|
|
@@ -589,12 +505,12 @@ fn) {
|
|
|
589
505
|
return false;
|
|
590
506
|
}
|
|
591
507
|
// Should only need 2 tokens.
|
|
592
|
-
|
|
593
|
-
|
|
508
|
+
const tokenizer = createTokenizer(fn.toString());
|
|
509
|
+
const first = tokenizer.next();
|
|
594
510
|
if (first.type === 'class') {
|
|
595
511
|
return true;
|
|
596
512
|
}
|
|
597
|
-
|
|
513
|
+
const second = tokenizer.next();
|
|
598
514
|
if (first.type === 'function' && second.value) {
|
|
599
515
|
if (second.value[0] === second.value[0].toUpperCase()) {
|
|
600
516
|
return true;
|
|
@@ -611,6 +527,7 @@ fn) {
|
|
|
611
527
|
* @return {boolean}
|
|
612
528
|
* true if the value is a function, false otherwise.
|
|
613
529
|
*/
|
|
530
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
614
531
|
function isFunction(val) {
|
|
615
532
|
return typeof val === 'function';
|
|
616
533
|
}
|
|
@@ -627,6 +544,62 @@ function uniq(arr) {
|
|
|
627
544
|
return Array.from(new Set(arr));
|
|
628
545
|
}
|
|
629
546
|
|
|
547
|
+
// Regex to extract the module name.
|
|
548
|
+
const nameExpr = /(.*)\..*/i;
|
|
549
|
+
/**
|
|
550
|
+
* Internal method for globbing a single pattern.
|
|
551
|
+
*
|
|
552
|
+
* @param {String} globPattern
|
|
553
|
+
* The glob pattern.
|
|
554
|
+
*
|
|
555
|
+
* @param {String} opts.cwd
|
|
556
|
+
* Current working directory, used for resolving filepaths.
|
|
557
|
+
* Defaults to `process.cwd()`.
|
|
558
|
+
*
|
|
559
|
+
* @return {[{name, path, opts}]}
|
|
560
|
+
* The module names and paths.
|
|
561
|
+
*
|
|
562
|
+
* @api private
|
|
563
|
+
*/
|
|
564
|
+
function _listModules(globPattern, opts) {
|
|
565
|
+
opts = { cwd: process.cwd(), glob: glob.sync, ...opts };
|
|
566
|
+
let patternOpts = null;
|
|
567
|
+
if (Array.isArray(globPattern)) {
|
|
568
|
+
patternOpts = globPattern[1];
|
|
569
|
+
globPattern = globPattern[0];
|
|
570
|
+
}
|
|
571
|
+
// Replace Windows path separators with Posix path
|
|
572
|
+
globPattern = globPattern.replace(/\\/g, '/');
|
|
573
|
+
const result = opts.glob(globPattern, { cwd: opts.cwd });
|
|
574
|
+
const mapped = result.map((p) => ({
|
|
575
|
+
name: nameExpr.exec(path.basename(p))[1],
|
|
576
|
+
path: path.resolve(opts.cwd, p),
|
|
577
|
+
opts: patternOpts,
|
|
578
|
+
}));
|
|
579
|
+
return mapped;
|
|
580
|
+
}
|
|
581
|
+
/**
|
|
582
|
+
* Returns a list of {name, path} pairs,
|
|
583
|
+
* where the name is the module name, and path is the actual
|
|
584
|
+
* full path to the module.
|
|
585
|
+
*
|
|
586
|
+
* @param {String|Array<String>} globPatterns
|
|
587
|
+
* The glob pattern as a string or an array of strings.
|
|
588
|
+
*
|
|
589
|
+
* @param {String} opts.cwd
|
|
590
|
+
* Current working directory, used for resolving filepaths.
|
|
591
|
+
* Defaults to `process.cwd()`.
|
|
592
|
+
*
|
|
593
|
+
* @return {[{name, path}]}
|
|
594
|
+
* An array of objects with the module names and paths.
|
|
595
|
+
*/
|
|
596
|
+
function listModules(globPatterns, opts) {
|
|
597
|
+
if (Array.isArray(globPatterns)) {
|
|
598
|
+
return flatten(globPatterns.map((p) => _listModules(p, opts)));
|
|
599
|
+
}
|
|
600
|
+
return _listModules(globPatterns, opts);
|
|
601
|
+
}
|
|
602
|
+
|
|
630
603
|
/*
|
|
631
604
|
* Parses the parameter list of a function string, including ES6 class constructors.
|
|
632
605
|
*
|
|
@@ -638,9 +611,9 @@ function uniq(arr) {
|
|
|
638
611
|
* constructor was found for a class.
|
|
639
612
|
*/
|
|
640
613
|
function parseParameterList(source) {
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
614
|
+
const { next: _next, done } = createTokenizer(source);
|
|
615
|
+
const params = [];
|
|
616
|
+
let t = null;
|
|
644
617
|
nextToken();
|
|
645
618
|
while (!done()) {
|
|
646
619
|
switch (t.type) {
|
|
@@ -655,7 +628,7 @@ function parseParameterList(source) {
|
|
|
655
628
|
nextToken();
|
|
656
629
|
break;
|
|
657
630
|
case 'function': {
|
|
658
|
-
|
|
631
|
+
const next = nextToken();
|
|
659
632
|
if (next.type === 'ident' || next.type === '*') {
|
|
660
633
|
// This is the function name or a generator star. Skip it.
|
|
661
634
|
nextToken();
|
|
@@ -672,12 +645,12 @@ function parseParameterList(source) {
|
|
|
672
645
|
case 'ident': {
|
|
673
646
|
// Likely a paren-less arrow function
|
|
674
647
|
// which can have no default args.
|
|
675
|
-
|
|
648
|
+
const param = { name: t.value, optional: false };
|
|
676
649
|
if (t.value === 'async') {
|
|
677
650
|
// Given it's the very first token, we can assume it's an async function,
|
|
678
651
|
// so skip the async keyword if the next token is not an equals sign, in which
|
|
679
652
|
// case it is a single-arg arrow func.
|
|
680
|
-
|
|
653
|
+
const next = nextToken();
|
|
681
654
|
if (next && next.type !== '=') {
|
|
682
655
|
break;
|
|
683
656
|
}
|
|
@@ -697,7 +670,7 @@ function parseParameterList(source) {
|
|
|
697
670
|
*/
|
|
698
671
|
function parseParams() {
|
|
699
672
|
// Current token is a left-paren
|
|
700
|
-
|
|
673
|
+
let param = { name: '', optional: false };
|
|
701
674
|
while (!done()) {
|
|
702
675
|
nextToken();
|
|
703
676
|
switch (t.type) {
|
|
@@ -740,8 +713,7 @@ function parseParameterList(source) {
|
|
|
740
713
|
/**
|
|
741
714
|
* Advances the tokenizer and stores the previous token in history
|
|
742
715
|
*/
|
|
743
|
-
function nextToken(flags) {
|
|
744
|
-
if (flags === void 0) { flags = 0 /* TokenizerFlags.None */; }
|
|
716
|
+
function nextToken(flags = 0 /* TokenizerFlags.None */) {
|
|
745
717
|
t = _next(flags);
|
|
746
718
|
return t;
|
|
747
719
|
}
|
|
@@ -750,7 +722,7 @@ function parseParameterList(source) {
|
|
|
750
722
|
*/
|
|
751
723
|
/* istanbul ignore next */
|
|
752
724
|
function unexpected() {
|
|
753
|
-
return new SyntaxError(
|
|
725
|
+
return new SyntaxError(`Parsing parameter list, did not expect ${t.type} token${t.value ? ` (${t.value})` : ''}`);
|
|
754
726
|
}
|
|
755
727
|
}
|
|
756
728
|
|
|
@@ -760,7 +732,7 @@ function parseParameterList(source) {
|
|
|
760
732
|
* RESOLVER symbol can be used by modules loaded by
|
|
761
733
|
* `loadModules` to configure their lifetime, injection mode, etc.
|
|
762
734
|
*/
|
|
763
|
-
|
|
735
|
+
const RESOLVER = Symbol('Awilix Resolver Config');
|
|
764
736
|
/**
|
|
765
737
|
* Creates a simple value resolver where the given value will always be resolved. The value is
|
|
766
738
|
* marked as leak-safe since in strict mode, the value will only be resolved when it is not leaking
|
|
@@ -774,7 +746,7 @@ var RESOLVER = Symbol('Awilix Resolver Config');
|
|
|
774
746
|
*/
|
|
775
747
|
function asValue(value) {
|
|
776
748
|
return {
|
|
777
|
-
resolve:
|
|
749
|
+
resolve: () => value,
|
|
778
750
|
isLeakSafe: true,
|
|
779
751
|
};
|
|
780
752
|
}
|
|
@@ -798,12 +770,15 @@ function asFunction(fn, opts) {
|
|
|
798
770
|
if (!isFunction(fn)) {
|
|
799
771
|
throw new AwilixTypeError('asFunction', 'fn', 'function', fn);
|
|
800
772
|
}
|
|
801
|
-
|
|
773
|
+
const defaults = {
|
|
802
774
|
lifetime: Lifetime.TRANSIENT,
|
|
803
775
|
};
|
|
804
776
|
opts = makeOptions(defaults, opts, fn[RESOLVER]);
|
|
805
|
-
|
|
806
|
-
|
|
777
|
+
const resolve = generateResolve(fn);
|
|
778
|
+
const result = {
|
|
779
|
+
resolve,
|
|
780
|
+
...opts,
|
|
781
|
+
};
|
|
807
782
|
return createDisposableResolver(createBuildResolver(result));
|
|
808
783
|
}
|
|
809
784
|
/**
|
|
@@ -825,20 +800,19 @@ function asClass(Type, opts) {
|
|
|
825
800
|
if (!isFunction(Type)) {
|
|
826
801
|
throw new AwilixTypeError('asClass', 'Type', 'class', Type);
|
|
827
802
|
}
|
|
828
|
-
|
|
803
|
+
const defaults = {
|
|
829
804
|
lifetime: Lifetime.TRANSIENT,
|
|
830
805
|
};
|
|
831
806
|
opts = makeOptions(defaults, opts, Type[RESOLVER]);
|
|
832
807
|
// A function to handle object construction for us, as to make the generateResolve more reusable
|
|
833
|
-
|
|
834
|
-
var args = [];
|
|
835
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
836
|
-
args[_i] = arguments[_i];
|
|
837
|
-
}
|
|
808
|
+
const newClass = function newClass(...args) {
|
|
838
809
|
return Reflect.construct(Type, args);
|
|
839
810
|
};
|
|
840
|
-
|
|
841
|
-
return createDisposableResolver(createBuildResolver(
|
|
811
|
+
const resolve = generateResolve(newClass, Type);
|
|
812
|
+
return createDisposableResolver(createBuildResolver({
|
|
813
|
+
...opts,
|
|
814
|
+
resolve,
|
|
815
|
+
}));
|
|
842
816
|
}
|
|
843
817
|
/**
|
|
844
818
|
* Resolves to the specified registration. Marked as leak-safe since the alias target is what should
|
|
@@ -846,7 +820,7 @@ function asClass(Type, opts) {
|
|
|
846
820
|
*/
|
|
847
821
|
function aliasTo(name) {
|
|
848
822
|
return {
|
|
849
|
-
resolve
|
|
823
|
+
resolve(container) {
|
|
850
824
|
return container.resolve(name);
|
|
851
825
|
},
|
|
852
826
|
isLeakSafe: true,
|
|
@@ -864,21 +838,30 @@ function aliasTo(name) {
|
|
|
864
838
|
*/
|
|
865
839
|
function createBuildResolver(obj) {
|
|
866
840
|
function setLifetime(value) {
|
|
867
|
-
return createBuildResolver(
|
|
841
|
+
return createBuildResolver({
|
|
842
|
+
...this,
|
|
843
|
+
lifetime: value,
|
|
844
|
+
});
|
|
868
845
|
}
|
|
869
846
|
function setInjectionMode(value) {
|
|
870
|
-
return createBuildResolver(
|
|
847
|
+
return createBuildResolver({
|
|
848
|
+
...this,
|
|
849
|
+
injectionMode: value,
|
|
850
|
+
});
|
|
871
851
|
}
|
|
872
852
|
function inject(injector) {
|
|
873
|
-
return createBuildResolver(
|
|
853
|
+
return createBuildResolver({
|
|
854
|
+
...this,
|
|
855
|
+
injector,
|
|
856
|
+
});
|
|
874
857
|
}
|
|
875
858
|
return updateResolver(obj, {
|
|
876
|
-
setLifetime
|
|
877
|
-
inject
|
|
859
|
+
setLifetime,
|
|
860
|
+
inject,
|
|
878
861
|
transient: partial(setLifetime, Lifetime.TRANSIENT),
|
|
879
862
|
scoped: partial(setLifetime, Lifetime.SCOPED),
|
|
880
863
|
singleton: partial(setLifetime, Lifetime.SINGLETON),
|
|
881
|
-
setInjectionMode
|
|
864
|
+
setInjectionMode,
|
|
882
865
|
proxy: partial(setInjectionMode, InjectionMode.PROXY),
|
|
883
866
|
classic: partial(setInjectionMode, InjectionMode.CLASSIC),
|
|
884
867
|
});
|
|
@@ -890,10 +873,13 @@ function createBuildResolver(obj) {
|
|
|
890
873
|
*/
|
|
891
874
|
function createDisposableResolver(obj) {
|
|
892
875
|
function disposer(dispose) {
|
|
893
|
-
return createDisposableResolver(
|
|
876
|
+
return createDisposableResolver({
|
|
877
|
+
...this,
|
|
878
|
+
dispose,
|
|
879
|
+
});
|
|
894
880
|
}
|
|
895
881
|
return updateResolver(obj, {
|
|
896
|
-
disposer
|
|
882
|
+
disposer,
|
|
897
883
|
});
|
|
898
884
|
}
|
|
899
885
|
/**
|
|
@@ -915,12 +901,8 @@ function partial(fn, arg1) {
|
|
|
915
901
|
*
|
|
916
902
|
* @return {object}
|
|
917
903
|
*/
|
|
918
|
-
function makeOptions(defaults) {
|
|
919
|
-
|
|
920
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
|
921
|
-
rest[_i - 1] = arguments[_i];
|
|
922
|
-
}
|
|
923
|
-
return Object.assign.apply(Object, __spreadArray([{}, defaults], rest, false));
|
|
904
|
+
function makeOptions(defaults, ...rest) {
|
|
905
|
+
return Object.assign({}, defaults, ...rest);
|
|
924
906
|
}
|
|
925
907
|
/**
|
|
926
908
|
* Creates a new resolver with props merged from both.
|
|
@@ -929,7 +911,10 @@ function makeOptions(defaults) {
|
|
|
929
911
|
* @param target
|
|
930
912
|
*/
|
|
931
913
|
function updateResolver(source, target) {
|
|
932
|
-
|
|
914
|
+
const result = {
|
|
915
|
+
...source,
|
|
916
|
+
...target,
|
|
917
|
+
};
|
|
933
918
|
return result;
|
|
934
919
|
}
|
|
935
920
|
/**
|
|
@@ -957,61 +942,26 @@ function wrapWithLocals(container, locals) {
|
|
|
957
942
|
* @return {Proxy}
|
|
958
943
|
*/
|
|
959
944
|
function createInjectorProxy(container, injector) {
|
|
960
|
-
|
|
961
|
-
|
|
945
|
+
const locals = injector(container);
|
|
946
|
+
const allKeys = uniq([
|
|
947
|
+
...Reflect.ownKeys(container.cradle),
|
|
948
|
+
...Reflect.ownKeys(locals),
|
|
949
|
+
]);
|
|
962
950
|
// TODO: Lots of duplication here from the container proxy.
|
|
963
951
|
// Need to refactor.
|
|
964
|
-
|
|
952
|
+
const proxy = new Proxy({}, {
|
|
965
953
|
/**
|
|
966
954
|
* Resolves the value by first checking the locals, then the container.
|
|
967
955
|
*/
|
|
968
|
-
get
|
|
956
|
+
get(target, name) {
|
|
969
957
|
if (name === Symbol.iterator) {
|
|
970
|
-
return function iterateRegistrationsAndLocals() {
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
for (_c in _a)
|
|
978
|
-
_b.push(_c);
|
|
979
|
-
_i = 0;
|
|
980
|
-
_h.label = 1;
|
|
981
|
-
case 1:
|
|
982
|
-
if (!(_i < _b.length)) return [3 /*break*/, 4];
|
|
983
|
-
_c = _b[_i];
|
|
984
|
-
if (!(_c in _a)) return [3 /*break*/, 3];
|
|
985
|
-
prop = _c;
|
|
986
|
-
return [4 /*yield*/, prop];
|
|
987
|
-
case 2:
|
|
988
|
-
_h.sent();
|
|
989
|
-
_h.label = 3;
|
|
990
|
-
case 3:
|
|
991
|
-
_i++;
|
|
992
|
-
return [3 /*break*/, 1];
|
|
993
|
-
case 4:
|
|
994
|
-
_d = locals;
|
|
995
|
-
_e = [];
|
|
996
|
-
for (_f in _d)
|
|
997
|
-
_e.push(_f);
|
|
998
|
-
_g = 0;
|
|
999
|
-
_h.label = 5;
|
|
1000
|
-
case 5:
|
|
1001
|
-
if (!(_g < _e.length)) return [3 /*break*/, 8];
|
|
1002
|
-
_f = _e[_g];
|
|
1003
|
-
if (!(_f in _d)) return [3 /*break*/, 7];
|
|
1004
|
-
prop = _f;
|
|
1005
|
-
return [4 /*yield*/, prop];
|
|
1006
|
-
case 6:
|
|
1007
|
-
_h.sent();
|
|
1008
|
-
_h.label = 7;
|
|
1009
|
-
case 7:
|
|
1010
|
-
_g++;
|
|
1011
|
-
return [3 /*break*/, 5];
|
|
1012
|
-
case 8: return [2 /*return*/];
|
|
1013
|
-
}
|
|
1014
|
-
});
|
|
958
|
+
return function* iterateRegistrationsAndLocals() {
|
|
959
|
+
for (const prop in container.cradle) {
|
|
960
|
+
yield prop;
|
|
961
|
+
}
|
|
962
|
+
for (const prop in locals) {
|
|
963
|
+
yield prop;
|
|
964
|
+
}
|
|
1015
965
|
};
|
|
1016
966
|
}
|
|
1017
967
|
if (name in locals) {
|
|
@@ -1022,13 +972,13 @@ function createInjectorProxy(container, injector) {
|
|
|
1022
972
|
/**
|
|
1023
973
|
* Used for `Object.keys`.
|
|
1024
974
|
*/
|
|
1025
|
-
ownKeys
|
|
975
|
+
ownKeys() {
|
|
1026
976
|
return allKeys;
|
|
1027
977
|
},
|
|
1028
978
|
/**
|
|
1029
979
|
* Used for `Object.keys`.
|
|
1030
980
|
*/
|
|
1031
|
-
getOwnPropertyDescriptor
|
|
981
|
+
getOwnPropertyDescriptor(target, key) {
|
|
1032
982
|
if (allKeys.indexOf(key) > -1) {
|
|
1033
983
|
return {
|
|
1034
984
|
enumerable: true,
|
|
@@ -1067,17 +1017,17 @@ function generateResolve(fn, dependencyParseTarget) {
|
|
|
1067
1017
|
// NOTE: we do this regardless of whether PROXY is used or not,
|
|
1068
1018
|
// because if this fails, we want it to fail early (at startup) rather
|
|
1069
1019
|
// than at resolution time.
|
|
1070
|
-
|
|
1020
|
+
const dependencies = parseDependencies(dependencyParseTarget);
|
|
1071
1021
|
// Use a regular function instead of an arrow function to facilitate binding to the resolver.
|
|
1072
1022
|
return function resolve(container) {
|
|
1073
1023
|
// Because the container holds a global reolutionMode we need to determine it in the proper order of precedence:
|
|
1074
1024
|
// resolver -> container -> default value
|
|
1075
|
-
|
|
1025
|
+
const injectionMode = this.injectionMode ||
|
|
1076
1026
|
container.options.injectionMode ||
|
|
1077
1027
|
InjectionMode.PROXY;
|
|
1078
1028
|
if (injectionMode !== InjectionMode.CLASSIC) {
|
|
1079
1029
|
// If we have a custom injector, we need to wrap the cradle.
|
|
1080
|
-
|
|
1030
|
+
const cradle = this.injector
|
|
1081
1031
|
? createInjectorProxy(container, this.injector)
|
|
1082
1032
|
: container.cradle;
|
|
1083
1033
|
// Return the target injected with the cradle
|
|
@@ -1085,13 +1035,11 @@ function generateResolve(fn, dependencyParseTarget) {
|
|
|
1085
1035
|
}
|
|
1086
1036
|
// We have dependencies so we need to resolve them manually
|
|
1087
1037
|
if (dependencies.length > 0) {
|
|
1088
|
-
|
|
1038
|
+
const resolve = this.injector
|
|
1089
1039
|
? wrapWithLocals(container, this.injector(container))
|
|
1090
1040
|
: container.resolve;
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
});
|
|
1094
|
-
return fn.apply(void 0, children);
|
|
1041
|
+
const children = dependencies.map((p) => resolve(p.name, { allowUnregistered: p.optional }));
|
|
1042
|
+
return fn(...children);
|
|
1095
1043
|
}
|
|
1096
1044
|
return fn();
|
|
1097
1045
|
};
|
|
@@ -1102,11 +1050,11 @@ function generateResolve(fn, dependencyParseTarget) {
|
|
|
1102
1050
|
* not have a defined constructor, attempt to parse it's super constructor.
|
|
1103
1051
|
*/
|
|
1104
1052
|
function parseDependencies(fn) {
|
|
1105
|
-
|
|
1053
|
+
const result = parseParameterList(fn.toString());
|
|
1106
1054
|
if (!result) {
|
|
1107
1055
|
// No defined constructor for a class, check if there is a parent
|
|
1108
1056
|
// we can parse.
|
|
1109
|
-
|
|
1057
|
+
const parent = Object.getPrototypeOf(fn);
|
|
1110
1058
|
if (typeof parent === 'function' && parent !== Function.prototype) {
|
|
1111
1059
|
// Try to parse the parent
|
|
1112
1060
|
return parseDependencies(parent);
|
|
@@ -1119,15 +1067,15 @@ function parseDependencies(fn) {
|
|
|
1119
1067
|
/**
|
|
1120
1068
|
* Family tree symbol.
|
|
1121
1069
|
*/
|
|
1122
|
-
|
|
1070
|
+
const FAMILY_TREE = Symbol('familyTree');
|
|
1123
1071
|
/**
|
|
1124
1072
|
* Roll Up Registrations symbol.
|
|
1125
1073
|
*/
|
|
1126
|
-
|
|
1074
|
+
const ROLL_UP_REGISTRATIONS = Symbol('rollUpRegistrations');
|
|
1127
1075
|
/**
|
|
1128
1076
|
* The string representation when calling toString.
|
|
1129
1077
|
*/
|
|
1130
|
-
|
|
1078
|
+
const CRADLE_STRING_TAG = 'AwilixContainerCradle';
|
|
1131
1079
|
/**
|
|
1132
1080
|
* Creates an Awilix container instance.
|
|
1133
1081
|
*
|
|
@@ -1141,26 +1089,28 @@ var CRADLE_STRING_TAG = 'AwilixContainerCradle';
|
|
|
1141
1089
|
*
|
|
1142
1090
|
* @return {AwilixContainer<T>} The container.
|
|
1143
1091
|
*/
|
|
1144
|
-
function createContainer(options) {
|
|
1145
|
-
if (options === void 0) { options = {}; }
|
|
1092
|
+
function createContainer(options = {}) {
|
|
1146
1093
|
return createContainerInternal(options);
|
|
1147
1094
|
}
|
|
1148
1095
|
function createContainerInternal(options, parentContainer, parentResolutionStack) {
|
|
1149
|
-
|
|
1150
|
-
|
|
1096
|
+
options = {
|
|
1097
|
+
injectionMode: InjectionMode.PROXY,
|
|
1098
|
+
strict: false,
|
|
1099
|
+
...options,
|
|
1100
|
+
};
|
|
1151
1101
|
/**
|
|
1152
1102
|
* Tracks the names and lifetimes of the modules being resolved. Used to detect circular
|
|
1153
1103
|
* dependencies and, in strict mode, lifetime leakage issues.
|
|
1154
1104
|
*/
|
|
1155
|
-
|
|
1105
|
+
const resolutionStack = parentResolutionStack ?? [];
|
|
1156
1106
|
// Internal registration store for this container.
|
|
1157
|
-
|
|
1107
|
+
const registrations = {};
|
|
1158
1108
|
/**
|
|
1159
1109
|
* The `Proxy` that is passed to functions so they can resolve their dependencies without
|
|
1160
1110
|
* knowing where they come from. I call it the "cradle" because
|
|
1161
1111
|
* it is where registered things come to life at resolution-time.
|
|
1162
1112
|
*/
|
|
1163
|
-
|
|
1113
|
+
const cradle = new Proxy({
|
|
1164
1114
|
/* removed in browser build */
|
|
1165
1115
|
}, {
|
|
1166
1116
|
/**
|
|
@@ -1175,27 +1125,27 @@ function createContainerInternal(options, parentContainer, parentResolutionStack
|
|
|
1175
1125
|
* @return {*}
|
|
1176
1126
|
* Whatever the resolve call returns.
|
|
1177
1127
|
*/
|
|
1178
|
-
get:
|
|
1128
|
+
get: (_target, name) => resolve(name),
|
|
1179
1129
|
/**
|
|
1180
1130
|
* Setting things on the cradle throws an error.
|
|
1181
1131
|
*
|
|
1182
1132
|
* @param {object} target
|
|
1183
1133
|
* @param {string} name
|
|
1184
1134
|
*/
|
|
1185
|
-
set:
|
|
1186
|
-
throw new Error(
|
|
1135
|
+
set: (_target, name) => {
|
|
1136
|
+
throw new Error(`Attempted setting property "${name}" on container cradle - this is not allowed.`);
|
|
1187
1137
|
},
|
|
1188
1138
|
/**
|
|
1189
1139
|
* Used for `Object.keys`.
|
|
1190
1140
|
*/
|
|
1191
|
-
ownKeys
|
|
1141
|
+
ownKeys() {
|
|
1192
1142
|
return Array.from(cradle);
|
|
1193
1143
|
},
|
|
1194
1144
|
/**
|
|
1195
1145
|
* Used for `Object.keys`.
|
|
1196
1146
|
*/
|
|
1197
|
-
getOwnPropertyDescriptor
|
|
1198
|
-
|
|
1147
|
+
getOwnPropertyDescriptor(target, key) {
|
|
1148
|
+
const regs = rollUpRegistrations();
|
|
1199
1149
|
if (Object.getOwnPropertyDescriptor(regs, key)) {
|
|
1200
1150
|
return {
|
|
1201
1151
|
enumerable: true,
|
|
@@ -1206,44 +1156,39 @@ function createContainerInternal(options, parentContainer, parentResolutionStack
|
|
|
1206
1156
|
},
|
|
1207
1157
|
});
|
|
1208
1158
|
// The container being exposed.
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
},
|
|
1159
|
+
const container = {
|
|
1160
|
+
options,
|
|
1161
|
+
cradle,
|
|
1162
|
+
inspect,
|
|
1163
|
+
cache: new Map(),
|
|
1164
|
+
loadModules: () => { throw new Error("loadModules is not supported in the browser."); },
|
|
1165
|
+
createScope,
|
|
1166
|
+
register: register,
|
|
1167
|
+
build,
|
|
1168
|
+
resolve,
|
|
1169
|
+
hasRegistration,
|
|
1170
|
+
dispose,
|
|
1171
|
+
getRegistration,
|
|
1223
1172
|
/* removed in browser build */
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
enumerable: false,
|
|
1230
|
-
configurable: true
|
|
1231
|
-
}),
|
|
1232
|
-
_a);
|
|
1173
|
+
[ROLL_UP_REGISTRATIONS]: rollUpRegistrations,
|
|
1174
|
+
get registrations() {
|
|
1175
|
+
return rollUpRegistrations();
|
|
1176
|
+
},
|
|
1177
|
+
};
|
|
1233
1178
|
// Track the family tree.
|
|
1234
|
-
|
|
1179
|
+
const familyTree = parentContainer
|
|
1235
1180
|
? [container].concat(parentContainer[FAMILY_TREE])
|
|
1236
1181
|
: [container];
|
|
1237
1182
|
container[FAMILY_TREE] = familyTree;
|
|
1238
1183
|
// We need a reference to the root container,
|
|
1239
1184
|
// so we can retrieve and store singletons.
|
|
1240
|
-
|
|
1185
|
+
const rootContainer = last(familyTree);
|
|
1241
1186
|
return container;
|
|
1242
1187
|
/**
|
|
1243
1188
|
* Used by util.inspect (which is used by console.log).
|
|
1244
1189
|
*/
|
|
1245
1190
|
function inspect() {
|
|
1246
|
-
return
|
|
1191
|
+
return `[AwilixContainer (${parentContainer ? 'scoped, ' : ''}registrations: ${Object.keys(container.registrations).length})]`;
|
|
1247
1192
|
}
|
|
1248
1193
|
/**
|
|
1249
1194
|
* Rolls up registrations from the family tree.
|
|
@@ -1259,38 +1204,19 @@ function createContainerInternal(options, parentContainer, parentResolutionStack
|
|
|
1259
1204
|
* The merged registrations object.
|
|
1260
1205
|
*/
|
|
1261
1206
|
function rollUpRegistrations() {
|
|
1262
|
-
return
|
|
1207
|
+
return {
|
|
1208
|
+
...(parentContainer && parentContainer[ROLL_UP_REGISTRATIONS]()),
|
|
1209
|
+
...registrations,
|
|
1210
|
+
};
|
|
1263
1211
|
}
|
|
1264
1212
|
/**
|
|
1265
1213
|
* Used for providing an iterator to the cradle.
|
|
1266
1214
|
*/
|
|
1267
|
-
function cradleIterator() {
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
registrations = rollUpRegistrations();
|
|
1273
|
-
_a = registrations;
|
|
1274
|
-
_b = [];
|
|
1275
|
-
for (_c in _a)
|
|
1276
|
-
_b.push(_c);
|
|
1277
|
-
_i = 0;
|
|
1278
|
-
_d.label = 1;
|
|
1279
|
-
case 1:
|
|
1280
|
-
if (!(_i < _b.length)) return [3 /*break*/, 4];
|
|
1281
|
-
_c = _b[_i];
|
|
1282
|
-
if (!(_c in _a)) return [3 /*break*/, 3];
|
|
1283
|
-
registrationName = _c;
|
|
1284
|
-
return [4 /*yield*/, registrationName];
|
|
1285
|
-
case 2:
|
|
1286
|
-
_d.sent();
|
|
1287
|
-
_d.label = 3;
|
|
1288
|
-
case 3:
|
|
1289
|
-
_i++;
|
|
1290
|
-
return [3 /*break*/, 1];
|
|
1291
|
-
case 4: return [2 /*return*/];
|
|
1292
|
-
}
|
|
1293
|
-
});
|
|
1215
|
+
function* cradleIterator() {
|
|
1216
|
+
const registrations = rollUpRegistrations();
|
|
1217
|
+
for (const registrationName in registrations) {
|
|
1218
|
+
yield registrationName;
|
|
1219
|
+
}
|
|
1294
1220
|
}
|
|
1295
1221
|
/**
|
|
1296
1222
|
* Creates a scoped container.
|
|
@@ -1305,11 +1231,10 @@ function createContainerInternal(options, parentContainer, parentResolutionStack
|
|
|
1305
1231
|
* Adds a registration for a resolver.
|
|
1306
1232
|
*/
|
|
1307
1233
|
function register(arg1, arg2) {
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
for (
|
|
1311
|
-
|
|
1312
|
-
var resolver = obj[key];
|
|
1234
|
+
const obj = nameValueToObject(arg1, arg2);
|
|
1235
|
+
const keys = [...Object.keys(obj), ...Object.getOwnPropertySymbols(obj)];
|
|
1236
|
+
for (const key of keys) {
|
|
1237
|
+
const resolver = obj[key];
|
|
1313
1238
|
// If strict mode is enabled, check to ensure we are not registering a singleton on a non-root
|
|
1314
1239
|
// container.
|
|
1315
1240
|
if (options.strict && resolver.lifetime === Lifetime.SINGLETON) {
|
|
@@ -1335,7 +1260,7 @@ function createContainerInternal(options, parentContainer, parentResolutionStack
|
|
|
1335
1260
|
* @param name {string | symbol} The registration name.
|
|
1336
1261
|
*/
|
|
1337
1262
|
function getRegistration(name) {
|
|
1338
|
-
|
|
1263
|
+
const resolver = registrations[name];
|
|
1339
1264
|
if (resolver) {
|
|
1340
1265
|
return resolver;
|
|
1341
1266
|
}
|
|
@@ -1360,11 +1285,8 @@ function createContainerInternal(options, parentContainer, parentResolutionStack
|
|
|
1360
1285
|
resolveOpts = resolveOpts || {};
|
|
1361
1286
|
try {
|
|
1362
1287
|
// Grab the registration by name.
|
|
1363
|
-
|
|
1364
|
-
if (resolutionStack.some(
|
|
1365
|
-
var parentName = _a.name;
|
|
1366
|
-
return parentName === name;
|
|
1367
|
-
})) {
|
|
1288
|
+
const resolver = getRegistration(name);
|
|
1289
|
+
if (resolutionStack.some(({ name: parentName }) => parentName === name)) {
|
|
1368
1290
|
throw new AwilixResolutionError(name, resolutionStack, 'Cyclic dependencies detected.');
|
|
1369
1291
|
}
|
|
1370
1292
|
// Used in JSON.stringify.
|
|
@@ -1399,24 +1321,21 @@ function createContainerInternal(options, parentContainer, parentResolutionStack
|
|
|
1399
1321
|
}
|
|
1400
1322
|
throw new AwilixResolutionError(name, resolutionStack);
|
|
1401
1323
|
}
|
|
1402
|
-
|
|
1324
|
+
const lifetime = resolver.lifetime || Lifetime.TRANSIENT;
|
|
1403
1325
|
// if we are running in strict mode, this resolver is not explicitly marked leak-safe, and any
|
|
1404
1326
|
// of the parents have a shorter lifetime than the one requested, throw an error.
|
|
1405
1327
|
if (options.strict && !resolver.isLeakSafe) {
|
|
1406
|
-
|
|
1407
|
-
var parentLifetime = _a.lifetime;
|
|
1408
|
-
return isLifetimeLonger(parentLifetime, lifetime_1);
|
|
1409
|
-
});
|
|
1328
|
+
const maybeLongerLifetimeParentIndex = resolutionStack.findIndex(({ lifetime: parentLifetime }) => isLifetimeLonger(parentLifetime, lifetime));
|
|
1410
1329
|
if (maybeLongerLifetimeParentIndex > -1) {
|
|
1411
|
-
throw new AwilixResolutionError(name, resolutionStack,
|
|
1330
|
+
throw new AwilixResolutionError(name, resolutionStack, `Dependency '${name.toString()}' has a shorter lifetime than its ancestor: '${resolutionStack[maybeLongerLifetimeParentIndex].name.toString()}'`);
|
|
1412
1331
|
}
|
|
1413
1332
|
}
|
|
1414
1333
|
// Pushes the currently-resolving module information onto the stack
|
|
1415
|
-
resolutionStack.push({ name
|
|
1334
|
+
resolutionStack.push({ name, lifetime });
|
|
1416
1335
|
// Do the thing
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
switch (
|
|
1336
|
+
let cached;
|
|
1337
|
+
let resolved;
|
|
1338
|
+
switch (lifetime) {
|
|
1420
1339
|
case Lifetime.TRANSIENT:
|
|
1421
1340
|
// Transient lifetime means resolve every time.
|
|
1422
1341
|
resolved = resolver.resolve(container);
|
|
@@ -1428,7 +1347,7 @@ function createContainerInternal(options, parentContainer, parentResolutionStack
|
|
|
1428
1347
|
// if we are running in strict mode, perform singleton resolution using the root
|
|
1429
1348
|
// container only.
|
|
1430
1349
|
resolved = resolver.resolve(options.strict ? rootContainer : container);
|
|
1431
|
-
rootContainer.cache.set(name, { resolver
|
|
1350
|
+
rootContainer.cache.set(name, { resolver, value: resolved });
|
|
1432
1351
|
}
|
|
1433
1352
|
else {
|
|
1434
1353
|
resolved = cached.value;
|
|
@@ -1448,10 +1367,10 @@ function createContainerInternal(options, parentContainer, parentResolutionStack
|
|
|
1448
1367
|
}
|
|
1449
1368
|
// If we still have not found one, we need to resolve and cache it.
|
|
1450
1369
|
resolved = resolver.resolve(container);
|
|
1451
|
-
container.cache.set(name, { resolver
|
|
1370
|
+
container.cache.set(name, { resolver, value: resolved });
|
|
1452
1371
|
break;
|
|
1453
1372
|
default:
|
|
1454
|
-
throw new AwilixResolutionError(name, resolutionStack,
|
|
1373
|
+
throw new AwilixResolutionError(name, resolutionStack, `Unknown lifetime "${resolver.lifetime}"`);
|
|
1455
1374
|
}
|
|
1456
1375
|
// Pop it from the stack again, ready for the next resolution
|
|
1457
1376
|
resolutionStack.pop();
|
|
@@ -1488,11 +1407,11 @@ function createContainerInternal(options, parentContainer, parentResolutionStack
|
|
|
1488
1407
|
if (targetOrResolver && targetOrResolver.resolve) {
|
|
1489
1408
|
return targetOrResolver.resolve(container);
|
|
1490
1409
|
}
|
|
1491
|
-
|
|
1492
|
-
|
|
1410
|
+
const funcName = 'build';
|
|
1411
|
+
const paramName = 'targetOrResolver';
|
|
1493
1412
|
AwilixTypeError.assert(targetOrResolver, funcName, paramName, 'a registration, function or class', targetOrResolver);
|
|
1494
1413
|
AwilixTypeError.assert(typeof targetOrResolver === 'function', funcName, paramName, 'a function or class', targetOrResolver);
|
|
1495
|
-
|
|
1414
|
+
const resolver = isClass(targetOrResolver)
|
|
1496
1415
|
? asClass(targetOrResolver, opts)
|
|
1497
1416
|
: asFunction(targetOrResolver, opts);
|
|
1498
1417
|
return resolver.resolve(container);
|
|
@@ -1502,18 +1421,17 @@ function createContainerInternal(options, parentContainer, parentResolutionStack
|
|
|
1502
1421
|
* on all disposable registrations and clearing the cache.
|
|
1503
1422
|
*/
|
|
1504
1423
|
function dispose() {
|
|
1505
|
-
|
|
1424
|
+
const entries = Array.from(container.cache.entries());
|
|
1506
1425
|
container.cache.clear();
|
|
1507
|
-
return Promise.all(entries.map(
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
var disposable = resolver;
|
|
1426
|
+
return Promise.all(entries.map(([, entry]) => {
|
|
1427
|
+
const { resolver, value } = entry;
|
|
1428
|
+
const disposable = resolver;
|
|
1511
1429
|
if (disposable.dispose) {
|
|
1512
|
-
return Promise.resolve().then(
|
|
1430
|
+
return Promise.resolve().then(() => disposable.dispose(value));
|
|
1513
1431
|
}
|
|
1514
1432
|
return Promise.resolve();
|
|
1515
|
-
})).then(
|
|
1433
|
+
})).then(() => undefined);
|
|
1516
1434
|
}
|
|
1517
1435
|
}
|
|
1518
1436
|
|
|
1519
|
-
export { AwilixError, AwilixRegistrationError, AwilixResolutionError, AwilixTypeError, InjectionMode, Lifetime, RESOLVER, aliasTo, asClass, asFunction, asValue, createBuildResolver, createContainer, createDisposableResolver };
|
|
1437
|
+
export { AwilixError, AwilixRegistrationError, AwilixResolutionError, AwilixTypeError, InjectionMode, Lifetime, RESOLVER, aliasTo, asClass, asFunction, asValue, createBuildResolver, createContainer, createDisposableResolver, isClass, isFunction, listModules };
|