@illuma/core 1.4.0 → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/testkit.js CHANGED
@@ -1,5 +1,171 @@
1
1
  var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
2
5
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
+ var __esm = (fn, res) => function __init() {
7
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
8
+ };
9
+ var __export = (target, all) => {
10
+ for (var name in all)
11
+ __defProp(target, name, { get: all[name], enumerable: true });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from))
16
+ if (!__hasOwnProp.call(to, key) && key !== except)
17
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
+ }
19
+ return to;
20
+ };
21
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
22
+
23
+ // src/lib/plugins/core/plugin-container.ts
24
+ var Illuma;
25
+ var init_plugin_container = __esm({
26
+ "src/lib/plugins/core/plugin-container.ts"() {
27
+ "use strict";
28
+ Illuma = class _Illuma {
29
+ static {
30
+ __name(this, "Illuma");
31
+ }
32
+ static _diagnostics = /* @__PURE__ */ new Set();
33
+ static _scanners = [];
34
+ static _middlewares = [];
35
+ /** @internal */
36
+ static get contextScanners() {
37
+ return _Illuma._scanners;
38
+ }
39
+ /**
40
+ * Extends the diagnostics with a new diagnostics module.
41
+ * These will be run on diagnostics reports after container bootstrap.
42
+ *
43
+ * @param m - The diagnostics module instance to add
44
+ */
45
+ static extendDiagnostics(m) {
46
+ _Illuma._diagnostics.add(m);
47
+ }
48
+ /**
49
+ * Extends the context scanners with a new context scanner.
50
+ * These will be run in injection context scans to detect additional injections (alongside `nodeInject` calls).
51
+ *
52
+ * @param scanner - The context scanner instance to add
53
+ */
54
+ static extendContextScanner(scanner) {
55
+ _Illuma._scanners.push(scanner);
56
+ }
57
+ /**
58
+ * Registers a global middleware to be applied during instance creation.
59
+ * Typically used for cross-cutting concerns like logging, profiling, or custom instantiation logic.
60
+ * Function should accept instantiation parameters and a `next` function to proceed with the next middleware or actual instantiation.
61
+ *
62
+ * @param m - The middleware function to register
63
+ */
64
+ static registerGlobalMiddleware(m) {
65
+ _Illuma._middlewares.push(m);
66
+ }
67
+ middlewares = [];
68
+ registerMiddleware(m) {
69
+ this.middlewares.push(m);
70
+ }
71
+ static onReport(report) {
72
+ for (const diag of _Illuma._diagnostics) diag.onReport(report);
73
+ }
74
+ /**
75
+ * @internal
76
+ * Check if diagnostics modules are registered
77
+ */
78
+ static hasDiagnostics() {
79
+ return _Illuma._diagnostics.size > 0;
80
+ }
81
+ /**
82
+ * @internal
83
+ * Reset all plugin registrations
84
+ */
85
+ static __resetPlugins() {
86
+ _Illuma._diagnostics.clear();
87
+ _Illuma._scanners.length = 0;
88
+ _Illuma._middlewares.length = 0;
89
+ }
90
+ };
91
+ }
92
+ });
93
+
94
+ // src/lib/plugins/core/index.ts
95
+ var init_core = __esm({
96
+ "src/lib/plugins/core/index.ts"() {
97
+ "use strict";
98
+ init_plugin_container();
99
+ }
100
+ });
101
+
102
+ // src/lib/plugins/middlewares/diagnostics.middleware.ts
103
+ var performanceDiagnostics;
104
+ var init_diagnostics_middleware = __esm({
105
+ "src/lib/plugins/middlewares/diagnostics.middleware.ts"() {
106
+ "use strict";
107
+ performanceDiagnostics = /* @__PURE__ */ __name((params, next) => {
108
+ if (!params.deps.size) {
109
+ return next(params);
110
+ }
111
+ const start = performance.now();
112
+ const instance = next(params);
113
+ const end = performance.now();
114
+ const duration = end - start;
115
+ console.log(`Instantiated ${params.token.name} in ${duration.toFixed(2)} ms`);
116
+ return instance;
117
+ }, "performanceDiagnostics");
118
+ }
119
+ });
120
+
121
+ // src/lib/plugins/diagnostics/default-impl.ts
122
+ var DiagnosticsDefaultReporter;
123
+ var init_default_impl = __esm({
124
+ "src/lib/plugins/diagnostics/default-impl.ts"() {
125
+ "use strict";
126
+ DiagnosticsDefaultReporter = class {
127
+ static {
128
+ __name(this, "DiagnosticsDefaultReporter");
129
+ }
130
+ onReport(report) {
131
+ console.log("[Illuma] \u{1F9F9} Diagnostics:");
132
+ console.log(` Total: ${report.totalNodes} node(s)`);
133
+ console.log(` ${report.unusedNodes.length} were not used while bootstrap:`);
134
+ for (const node of report.unusedNodes) console.log(` - ${node.toString()}`);
135
+ }
136
+ };
137
+ }
138
+ });
139
+
140
+ // src/lib/plugins/diagnostics/built-in.ts
141
+ var built_in_exports = {};
142
+ __export(built_in_exports, {
143
+ __resetDiagnosticsState: () => __resetDiagnosticsState,
144
+ enableIllumaDiagnostics: () => enableIllumaDiagnostics
145
+ });
146
+ function enableIllumaDiagnostics() {
147
+ if (state.enabled) return;
148
+ state.enabled = true;
149
+ Illuma.extendDiagnostics(new DiagnosticsDefaultReporter());
150
+ Illuma.registerGlobalMiddleware(performanceDiagnostics);
151
+ }
152
+ function __resetDiagnosticsState() {
153
+ state.enabled = false;
154
+ }
155
+ var state;
156
+ var init_built_in = __esm({
157
+ "src/lib/plugins/diagnostics/built-in.ts"() {
158
+ "use strict";
159
+ init_core();
160
+ init_diagnostics_middleware();
161
+ init_default_impl();
162
+ state = {
163
+ enabled: false
164
+ };
165
+ __name(enableIllumaDiagnostics, "enableIllumaDiagnostics");
166
+ __name(__resetDiagnosticsState, "__resetDiagnosticsState");
167
+ }
168
+ });
3
169
 
4
170
  // src/lib/api/token.ts
5
171
  var NodeBase = class {
@@ -195,56 +361,8 @@ function isConstructor(fn) {
195
361
  }
196
362
  __name(isConstructor, "isConstructor");
197
363
 
198
- // src/lib/plugins/core/plugin-container.ts
199
- var Illuma = class _Illuma {
200
- static {
201
- __name(this, "Illuma");
202
- }
203
- static _diagnostics = [];
204
- static _scanners = [];
205
- static _middlewares = [];
206
- /** @internal */
207
- static get contextScanners() {
208
- return _Illuma._scanners;
209
- }
210
- /**
211
- * Extends the diagnostics with a new diagnostics module.
212
- * These will be run on diagnostics reports after container bootstrap.
213
- *
214
- * @param m - The diagnostics module instance to add
215
- */
216
- static extendDiagnostics(m) {
217
- _Illuma._diagnostics.push(m);
218
- }
219
- /**
220
- * Extends the context scanners with a new context scanner.
221
- * These will be run in injection context scans to detect additional injections (alongside `nodeInject` calls).
222
- *
223
- * @param scanner - The context scanner instance to add
224
- */
225
- static extendContextScanner(scanner) {
226
- _Illuma._scanners.push(scanner);
227
- }
228
- /**
229
- * Registers a global middleware to be applied during instance creation.
230
- * Typically used for cross-cutting concerns like logging, profiling, or custom instantiation logic.
231
- * Function should accept instantiation parameters and a `next` function to proceed with the next middleware or actual instantiation.
232
- *
233
- * @param m - The middleware function to register
234
- */
235
- static registerGlobalMiddleware(m) {
236
- _Illuma._middlewares.push(m);
237
- }
238
- middlewares = [];
239
- registerMiddleware(m) {
240
- this.middlewares.push(m);
241
- }
242
- static onReport(report) {
243
- for (const diag of _Illuma._diagnostics) diag.onReport(report);
244
- }
245
- };
246
-
247
364
  // src/lib/context/context.ts
365
+ init_plugin_container();
248
366
  var InjectionContext = class _InjectionContext {
249
367
  static {
250
368
  __name(this, "InjectionContext");
@@ -344,18 +462,19 @@ function nodeInject(provider, options) {
344
462
  }
345
463
  __name(nodeInject, "nodeInject");
346
464
 
347
- // src/lib/plugins/diagnostics/default-impl.ts
348
- var DiagnosticsDefaultReporter = class {
349
- static {
350
- __name(this, "DiagnosticsDefaultReporter");
351
- }
352
- onReport(report) {
353
- console.log("[Illuma] \u{1F9F9} Diagnostics:");
354
- console.log(` Total: ${report.totalNodes} node(s)`);
355
- console.log(` ${report.unusedNodes.length} were not used while bootstrap:`);
356
- for (const node of report.unusedNodes) console.log(` - ${node.toString()}`);
357
- }
358
- };
465
+ // src/lib/container/container.ts
466
+ init_plugin_container();
467
+
468
+ // src/lib/plugins/middlewares/runner.ts
469
+ function runMiddlewares(middlewares, params) {
470
+ const ms = middlewares;
471
+ const next = /* @__PURE__ */ __name((i, current) => {
472
+ if (i >= ms.length) return current.factory();
473
+ return ms[i](current, (nextParams) => next(i + 1, nextParams));
474
+ }, "next");
475
+ return next(0, params);
476
+ }
477
+ __name(runMiddlewares, "runMiddlewares");
359
478
 
360
479
  // src/lib/provider/extractor.ts
361
480
  function extractProvider(provider) {
@@ -462,24 +581,15 @@ var InjectorImpl = class {
462
581
  var Injector = new NodeToken("Injector");
463
582
 
464
583
  // src/lib/provider/tree-node.ts
465
- function runMiddlewares(middlewares, params) {
466
- const ms = middlewares;
467
- const next = /* @__PURE__ */ __name((i, current) => {
468
- if (i >= ms.length) return current.factory();
469
- return ms[i](current, (nextParams) => next(i + 1, nextParams));
470
- }, "next");
471
- return next(0, params);
472
- }
473
- __name(runMiddlewares, "runMiddlewares");
474
584
  function retrieverFactory(node, deps, transparentDeps) {
475
585
  return (token, optional) => {
476
586
  const depNode = deps.get(token);
477
587
  if (!depNode && !optional) {
478
- const transparent = Array.from(transparentDeps).find((n) => "proto" in n && n.proto.parent.token === token);
588
+ const transparent = transparentDeps.get(token);
479
589
  if (transparent) return transparent.instance;
480
590
  throw InjectionError.untracked(token, node);
481
591
  }
482
- return depNode?.instance ?? null;
592
+ return depNode ? depNode.instance : null;
483
593
  };
484
594
  }
485
595
  __name(retrieverFactory, "retrieverFactory");
@@ -490,7 +600,7 @@ var TreeRootNode = class {
490
600
  instant;
491
601
  middlewares;
492
602
  _deps = /* @__PURE__ */ new Set();
493
- _treePool = /* @__PURE__ */ new WeakMap();
603
+ _treePool = /* @__PURE__ */ new Map();
494
604
  constructor(instant = true, middlewares = []) {
495
605
  this.instant = instant;
496
606
  this.middlewares = middlewares;
@@ -555,13 +665,21 @@ var TreeNodeSingle = class {
555
665
  if (this._resolved) return;
556
666
  for (const node of this._deps.values()) node.instantiate(pool, middlewares);
557
667
  for (const dep of this._transparent) dep.instantiate(pool, middlewares);
558
- const retriever = retrieverFactory(this.proto.token, this._deps, this._transparent);
668
+ const transparentMap = /* @__PURE__ */ new Map();
669
+ for (const tNode of this._transparent) {
670
+ transparentMap.set(tNode.proto.parent.token, tNode);
671
+ }
672
+ const retriever = retrieverFactory(this.proto.token, this._deps, transparentMap);
559
673
  const factory = this.proto.factory ?? this.proto.token.opts?.factory;
560
674
  if (!factory) throw InjectionError.notFound(this.proto.token);
561
675
  const contextFactory = /* @__PURE__ */ __name(() => InjectionContext.instantiate(factory, retriever), "contextFactory");
562
676
  this._instance = runMiddlewares(middlewares, {
563
677
  token: this.proto.token,
564
- factory: contextFactory
678
+ factory: contextFactory,
679
+ deps: /* @__PURE__ */ new Set([
680
+ ...this._deps.keys(),
681
+ ...Array.from(this._transparent).map((n) => n.proto.parent.token)
682
+ ])
565
683
  });
566
684
  this._resolved = true;
567
685
  if (pool) pool.set(this.proto.token, this);
@@ -600,11 +718,19 @@ var TreeNodeTransparent = class _TreeNodeTransparent {
600
718
  if (this._resolved) return;
601
719
  for (const dep of this._transparent) dep.instantiate(pool, middlewares);
602
720
  for (const node of this._deps.values()) node.instantiate(pool, middlewares);
603
- const retriever = retrieverFactory(this.proto.parent.token, this._deps, this._transparent);
721
+ const transparentMap = /* @__PURE__ */ new Map();
722
+ for (const tNode of this._transparent) {
723
+ transparentMap.set(tNode.proto.parent.token, tNode);
724
+ }
725
+ const retriever = retrieverFactory(this.proto.parent.token, this._deps, transparentMap);
604
726
  const refFactory = /* @__PURE__ */ __name(() => InjectionContext.instantiate(this.proto.factory, retriever), "refFactory");
605
727
  this._instance = runMiddlewares(middlewares, {
606
728
  token: this.proto.parent.token,
607
- factory: refFactory
729
+ factory: refFactory,
730
+ deps: /* @__PURE__ */ new Set([
731
+ ...this._deps.keys(),
732
+ ...Array.from(this._transparent).map((n) => n.proto.parent.token)
733
+ ])
608
734
  });
609
735
  this._resolved = true;
610
736
  }
@@ -799,10 +925,13 @@ var NodeContainer = class extends Illuma {
799
925
  _multiProtoNodes = /* @__PURE__ */ new Map();
800
926
  constructor(_opts) {
801
927
  super(), this._opts = _opts;
802
- this._parent = _opts?.parent;
803
928
  if (_opts?.diagnostics) {
804
- Illuma.extendDiagnostics(new DiagnosticsDefaultReporter());
929
+ console.warn("[Illuma] Deprecation Warning: The 'diagnostics' option in iContainerOptions is deprecated and will be removed in future versions. Please use the `enableIllumaDiagnostics` from '@illuma/core/plugins` instead.");
930
+ const m = (init_built_in(), __toCommonJS(built_in_exports));
931
+ if (m.enabled) return;
932
+ m.enableIllumaDiagnostics();
805
933
  }
934
+ this._parent = _opts?.parent;
806
935
  }
807
936
  /**
808
937
  * Registers a provider in the container.
@@ -977,7 +1106,7 @@ var NodeContainer = class extends Illuma {
977
1106
  if (this._opts?.measurePerformance) {
978
1107
  console.log(`[Illuma] \u{1F680} Bootstrapped in ${duration.toFixed(2)} ms`);
979
1108
  }
980
- if (this._opts?.diagnostics) {
1109
+ if (this._opts?.diagnostics || Illuma.hasDiagnostics()) {
981
1110
  const allNodes = this._rootNode.dependencies.size;
982
1111
  const unusedNodes = Array.from(this._rootNode.dependencies).filter((node) => node.allocations === 0).filter((node) => {
983
1112
  if (!(node.proto instanceof ProtoNodeSingle)) return true;
@@ -1030,12 +1159,33 @@ var NodeContainer = class extends Illuma {
1030
1159
  if (isConstructor(fn) && !isInjectable(fn)) {
1031
1160
  throw InjectionError.invalidCtor(fn);
1032
1161
  }
1033
- const factory = isInjectable(fn) ? () => new fn() : fn;
1034
- return InjectionContext.instantiate(factory, (t, optional) => {
1035
- if (!this._rootNode) throw InjectionError.notBootstrapped();
1036
- const node = this._rootNode.find(t);
1037
- if (!node && !optional) throw InjectionError.notFound(t);
1162
+ let factory;
1163
+ if (isInjectable(fn)) {
1164
+ const f = getInjectableToken(fn).opts?.factory;
1165
+ if (!f) factory = /* @__PURE__ */ __name(() => new fn(), "factory");
1166
+ else factory = /* @__PURE__ */ __name(() => getInjectableToken(fn).opts?.factory?.(), "factory");
1167
+ } else {
1168
+ factory = fn;
1169
+ }
1170
+ const rootNode = this._rootNode;
1171
+ if (!rootNode) throw InjectionError.notBootstrapped();
1172
+ const retriever = /* @__PURE__ */ __name((token, optional) => {
1173
+ const node = rootNode.find(token);
1174
+ if (!node && !optional) throw InjectionError.notFound(token);
1038
1175
  return node ? node.instance : null;
1176
+ }, "retriever");
1177
+ const deps = InjectionContext.scan(factory);
1178
+ const middlewares = [
1179
+ ...Illuma._middlewares,
1180
+ ...this.collectMiddlewares()
1181
+ ];
1182
+ const contextFactory = /* @__PURE__ */ __name(() => InjectionContext.instantiate(factory, retriever), "contextFactory");
1183
+ return runMiddlewares(middlewares, {
1184
+ token: new NodeToken("ProducedNode"),
1185
+ deps: new Set([
1186
+ ...deps.values()
1187
+ ].map((d) => d.token)),
1188
+ factory: contextFactory
1039
1189
  });
1040
1190
  }
1041
1191
  };