@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/index.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 {
@@ -221,56 +387,8 @@ function registerClassAsInjectable(ctor, token) {
221
387
  }
222
388
  __name(registerClassAsInjectable, "registerClassAsInjectable");
223
389
 
224
- // src/lib/plugins/core/plugin-container.ts
225
- var Illuma = class _Illuma {
226
- static {
227
- __name(this, "Illuma");
228
- }
229
- static _diagnostics = [];
230
- static _scanners = [];
231
- static _middlewares = [];
232
- /** @internal */
233
- static get contextScanners() {
234
- return _Illuma._scanners;
235
- }
236
- /**
237
- * Extends the diagnostics with a new diagnostics module.
238
- * These will be run on diagnostics reports after container bootstrap.
239
- *
240
- * @param m - The diagnostics module instance to add
241
- */
242
- static extendDiagnostics(m) {
243
- _Illuma._diagnostics.push(m);
244
- }
245
- /**
246
- * Extends the context scanners with a new context scanner.
247
- * These will be run in injection context scans to detect additional injections (alongside `nodeInject` calls).
248
- *
249
- * @param scanner - The context scanner instance to add
250
- */
251
- static extendContextScanner(scanner) {
252
- _Illuma._scanners.push(scanner);
253
- }
254
- /**
255
- * Registers a global middleware to be applied during instance creation.
256
- * Typically used for cross-cutting concerns like logging, profiling, or custom instantiation logic.
257
- * Function should accept instantiation parameters and a `next` function to proceed with the next middleware or actual instantiation.
258
- *
259
- * @param m - The middleware function to register
260
- */
261
- static registerGlobalMiddleware(m) {
262
- _Illuma._middlewares.push(m);
263
- }
264
- middlewares = [];
265
- registerMiddleware(m) {
266
- this.middlewares.push(m);
267
- }
268
- static onReport(report) {
269
- for (const diag of _Illuma._diagnostics) diag.onReport(report);
270
- }
271
- };
272
-
273
390
  // src/lib/context/context.ts
391
+ init_plugin_container();
274
392
  var InjectionContext = class _InjectionContext {
275
393
  static {
276
394
  __name(this, "InjectionContext");
@@ -370,18 +488,19 @@ function nodeInject(provider, options) {
370
488
  }
371
489
  __name(nodeInject, "nodeInject");
372
490
 
373
- // src/lib/plugins/diagnostics/default-impl.ts
374
- var DiagnosticsDefaultReporter = class {
375
- static {
376
- __name(this, "DiagnosticsDefaultReporter");
377
- }
378
- onReport(report) {
379
- console.log("[Illuma] \u{1F9F9} Diagnostics:");
380
- console.log(` Total: ${report.totalNodes} node(s)`);
381
- console.log(` ${report.unusedNodes.length} were not used while bootstrap:`);
382
- for (const node of report.unusedNodes) console.log(` - ${node.toString()}`);
383
- }
384
- };
491
+ // src/lib/container/container.ts
492
+ init_plugin_container();
493
+
494
+ // src/lib/plugins/middlewares/runner.ts
495
+ function runMiddlewares(middlewares, params) {
496
+ const ms = middlewares;
497
+ const next = /* @__PURE__ */ __name((i, current) => {
498
+ if (i >= ms.length) return current.factory();
499
+ return ms[i](current, (nextParams) => next(i + 1, nextParams));
500
+ }, "next");
501
+ return next(0, params);
502
+ }
503
+ __name(runMiddlewares, "runMiddlewares");
385
504
 
386
505
  // src/lib/provider/extractor.ts
387
506
  function extractProvider(provider) {
@@ -488,24 +607,15 @@ var InjectorImpl = class {
488
607
  var Injector = new NodeToken("Injector");
489
608
 
490
609
  // src/lib/provider/tree-node.ts
491
- function runMiddlewares(middlewares, params) {
492
- const ms = middlewares;
493
- const next = /* @__PURE__ */ __name((i, current) => {
494
- if (i >= ms.length) return current.factory();
495
- return ms[i](current, (nextParams) => next(i + 1, nextParams));
496
- }, "next");
497
- return next(0, params);
498
- }
499
- __name(runMiddlewares, "runMiddlewares");
500
610
  function retrieverFactory(node, deps, transparentDeps) {
501
611
  return (token, optional) => {
502
612
  const depNode = deps.get(token);
503
613
  if (!depNode && !optional) {
504
- const transparent = Array.from(transparentDeps).find((n) => "proto" in n && n.proto.parent.token === token);
614
+ const transparent = transparentDeps.get(token);
505
615
  if (transparent) return transparent.instance;
506
616
  throw InjectionError.untracked(token, node);
507
617
  }
508
- return depNode?.instance ?? null;
618
+ return depNode ? depNode.instance : null;
509
619
  };
510
620
  }
511
621
  __name(retrieverFactory, "retrieverFactory");
@@ -516,7 +626,7 @@ var TreeRootNode = class {
516
626
  instant;
517
627
  middlewares;
518
628
  _deps = /* @__PURE__ */ new Set();
519
- _treePool = /* @__PURE__ */ new WeakMap();
629
+ _treePool = /* @__PURE__ */ new Map();
520
630
  constructor(instant = true, middlewares = []) {
521
631
  this.instant = instant;
522
632
  this.middlewares = middlewares;
@@ -581,13 +691,21 @@ var TreeNodeSingle = class {
581
691
  if (this._resolved) return;
582
692
  for (const node of this._deps.values()) node.instantiate(pool, middlewares);
583
693
  for (const dep of this._transparent) dep.instantiate(pool, middlewares);
584
- const retriever = retrieverFactory(this.proto.token, this._deps, this._transparent);
694
+ const transparentMap = /* @__PURE__ */ new Map();
695
+ for (const tNode of this._transparent) {
696
+ transparentMap.set(tNode.proto.parent.token, tNode);
697
+ }
698
+ const retriever = retrieverFactory(this.proto.token, this._deps, transparentMap);
585
699
  const factory = this.proto.factory ?? this.proto.token.opts?.factory;
586
700
  if (!factory) throw InjectionError.notFound(this.proto.token);
587
701
  const contextFactory = /* @__PURE__ */ __name(() => InjectionContext.instantiate(factory, retriever), "contextFactory");
588
702
  this._instance = runMiddlewares(middlewares, {
589
703
  token: this.proto.token,
590
- factory: contextFactory
704
+ factory: contextFactory,
705
+ deps: /* @__PURE__ */ new Set([
706
+ ...this._deps.keys(),
707
+ ...Array.from(this._transparent).map((n) => n.proto.parent.token)
708
+ ])
591
709
  });
592
710
  this._resolved = true;
593
711
  if (pool) pool.set(this.proto.token, this);
@@ -626,11 +744,19 @@ var TreeNodeTransparent = class _TreeNodeTransparent {
626
744
  if (this._resolved) return;
627
745
  for (const dep of this._transparent) dep.instantiate(pool, middlewares);
628
746
  for (const node of this._deps.values()) node.instantiate(pool, middlewares);
629
- const retriever = retrieverFactory(this.proto.parent.token, this._deps, this._transparent);
747
+ const transparentMap = /* @__PURE__ */ new Map();
748
+ for (const tNode of this._transparent) {
749
+ transparentMap.set(tNode.proto.parent.token, tNode);
750
+ }
751
+ const retriever = retrieverFactory(this.proto.parent.token, this._deps, transparentMap);
630
752
  const refFactory = /* @__PURE__ */ __name(() => InjectionContext.instantiate(this.proto.factory, retriever), "refFactory");
631
753
  this._instance = runMiddlewares(middlewares, {
632
754
  token: this.proto.parent.token,
633
- factory: refFactory
755
+ factory: refFactory,
756
+ deps: /* @__PURE__ */ new Set([
757
+ ...this._deps.keys(),
758
+ ...Array.from(this._transparent).map((n) => n.proto.parent.token)
759
+ ])
634
760
  });
635
761
  this._resolved = true;
636
762
  }
@@ -921,10 +1047,13 @@ var NodeContainer = class extends Illuma {
921
1047
  _multiProtoNodes = /* @__PURE__ */ new Map();
922
1048
  constructor(_opts) {
923
1049
  super(), this._opts = _opts;
924
- this._parent = _opts?.parent;
925
1050
  if (_opts?.diagnostics) {
926
- Illuma.extendDiagnostics(new DiagnosticsDefaultReporter());
1051
+ 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.");
1052
+ const m = (init_built_in(), __toCommonJS(built_in_exports));
1053
+ if (m.enabled) return;
1054
+ m.enableIllumaDiagnostics();
927
1055
  }
1056
+ this._parent = _opts?.parent;
928
1057
  }
929
1058
  /**
930
1059
  * Registers a provider in the container.
@@ -1099,7 +1228,7 @@ var NodeContainer = class extends Illuma {
1099
1228
  if (this._opts?.measurePerformance) {
1100
1229
  console.log(`[Illuma] \u{1F680} Bootstrapped in ${duration.toFixed(2)} ms`);
1101
1230
  }
1102
- if (this._opts?.diagnostics) {
1231
+ if (this._opts?.diagnostics || Illuma.hasDiagnostics()) {
1103
1232
  const allNodes = this._rootNode.dependencies.size;
1104
1233
  const unusedNodes = Array.from(this._rootNode.dependencies).filter((node) => node.allocations === 0).filter((node) => {
1105
1234
  if (!(node.proto instanceof ProtoNodeSingle)) return true;
@@ -1152,12 +1281,33 @@ var NodeContainer = class extends Illuma {
1152
1281
  if (isConstructor(fn) && !isInjectable(fn)) {
1153
1282
  throw InjectionError.invalidCtor(fn);
1154
1283
  }
1155
- const factory = isInjectable(fn) ? () => new fn() : fn;
1156
- return InjectionContext.instantiate(factory, (t, optional) => {
1157
- if (!this._rootNode) throw InjectionError.notBootstrapped();
1158
- const node = this._rootNode.find(t);
1159
- if (!node && !optional) throw InjectionError.notFound(t);
1284
+ let factory;
1285
+ if (isInjectable(fn)) {
1286
+ const f = getInjectableToken(fn).opts?.factory;
1287
+ if (!f) factory = /* @__PURE__ */ __name(() => new fn(), "factory");
1288
+ else factory = /* @__PURE__ */ __name(() => getInjectableToken(fn).opts?.factory?.(), "factory");
1289
+ } else {
1290
+ factory = fn;
1291
+ }
1292
+ const rootNode = this._rootNode;
1293
+ if (!rootNode) throw InjectionError.notBootstrapped();
1294
+ const retriever = /* @__PURE__ */ __name((token, optional) => {
1295
+ const node = rootNode.find(token);
1296
+ if (!node && !optional) throw InjectionError.notFound(token);
1160
1297
  return node ? node.instance : null;
1298
+ }, "retriever");
1299
+ const deps = InjectionContext.scan(factory);
1300
+ const middlewares = [
1301
+ ...Illuma._middlewares,
1302
+ ...this.collectMiddlewares()
1303
+ ];
1304
+ const contextFactory = /* @__PURE__ */ __name(() => InjectionContext.instantiate(factory, retriever), "contextFactory");
1305
+ return runMiddlewares(middlewares, {
1306
+ token: new NodeToken("ProducedNode"),
1307
+ deps: new Set([
1308
+ ...deps.values()
1309
+ ].map((d) => d.token)),
1310
+ factory: contextFactory
1161
1311
  });
1162
1312
  }
1163
1313
  };