@illuma/core 0.1.0 → 0.2.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/CHANGELOG.md CHANGED
@@ -7,4 +7,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## Unreleased
9
9
 
10
+ ## 0.2.0 - 2026-01-10
11
+
10
12
  ## 0.1.0 - 2026-01-07
package/dist/index.cjs CHANGED
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  // src/index.ts
22
22
  var index_exports = {};
23
23
  __export(index_exports, {
24
+ ILLUMA_ERR_CODES: () => ERR_CODES,
24
25
  INJECTION_SYMBOL: () => INJECTION_SYMBOL,
25
26
  InjectionContext: () => InjectionContext,
26
27
  InjectionError: () => InjectionError,
@@ -34,6 +35,7 @@ __export(index_exports, {
34
35
  extractToken: () => extractToken,
35
36
  getInjectableToken: () => getInjectableToken,
36
37
  injectAsync: () => injectAsync,
38
+ injectDefer: () => injectDefer,
37
39
  injectEntryAsync: () => injectEntryAsync,
38
40
  injectGroupAsync: () => injectGroupAsync,
39
41
  isConstructor: () => isConstructor,
@@ -45,6 +47,29 @@ __export(index_exports, {
45
47
  module.exports = __toCommonJS(index_exports);
46
48
 
47
49
  // src/lib/errors.ts
50
+ var ERR_CODES = {
51
+ // Provider errors
52
+ DUPLICATE_PROVIDER: 100,
53
+ DUPLICATE_FACTORY: 101,
54
+ INVALID_CTOR: 102,
55
+ INVALID_PROVIDER: 103,
56
+ // Alias errors
57
+ INVALID_ALIAS: 200,
58
+ LOOP_ALIAS: 201,
59
+ // Bootstrap errors
60
+ NOT_BOOTSTRAPPED: 300,
61
+ BOOTSTRAPPED: 301,
62
+ DOUBLE_BOOTSTRAP: 302,
63
+ // Retrieval errors
64
+ NOT_FOUND: 400,
65
+ CIRCULAR_DEPENDENCY: 401,
66
+ // Instantiation errors
67
+ UNTRACKED: 500,
68
+ OUTSIDE_CONTEXT: 501,
69
+ CALLED_UTILS_OUTSIDE_CONTEXT: 502,
70
+ INSTANCE_ACCESS_FAILED: 503,
71
+ ACCESS_FAILED: 504
72
+ };
48
73
  var InjectionError = class _InjectionError extends Error {
49
74
  static {
50
75
  __name(this, "InjectionError");
@@ -56,66 +81,70 @@ var InjectionError = class _InjectionError extends Error {
56
81
  }
57
82
  // Provider errors
58
83
  static duplicate(token) {
59
- return new _InjectionError(100, `Duplicate provider for token "${token.toString()}" detected.`);
84
+ return new _InjectionError(ERR_CODES.DUPLICATE_PROVIDER, `Duplicate provider for token "${token.toString()}" detected.`);
60
85
  }
61
86
  static duplicateFactory(token) {
62
- return new _InjectionError(101, `Tried to re-provide factory for token "${token.toString()}" detected.`);
87
+ return new _InjectionError(ERR_CODES.DUPLICATE_FACTORY, `Tried to re-provide factory for token "${token.toString()}" detected.`);
63
88
  }
64
89
  static invalidCtor(ctor) {
65
- return new _InjectionError(102, `Cannot use constructor for token "${ctor.name}". Please make sure to use @nodeInjectable() decorator`);
90
+ return new _InjectionError(ERR_CODES.INVALID_CTOR, `Cannot use constructor for token "${ctor.name}". Please make sure to use @nodeInjectable() decorator`);
66
91
  }
67
92
  static invalidProvider(provider) {
68
- return new _InjectionError(103, `Cannot use provider as it is neither a NodeToken nor MultiNodeToken nor a valid constructor.:
93
+ return new _InjectionError(ERR_CODES.INVALID_PROVIDER, `Cannot use provider as it is neither a NodeToken nor MultiNodeToken nor a valid constructor.:
69
94
  ${provider}`);
70
95
  }
71
96
  // Alias errors
72
97
  static invalidAlias(alias) {
73
98
  const aliasStr = typeof alias === "function" ? alias.name || "Unknown" : String(alias);
74
- return new _InjectionError(200, `Invalid alias target "${aliasStr}". Alias must be a NodeToken, MultiNodeToken, or a class decorated with @NodeInjectable().`);
99
+ return new _InjectionError(ERR_CODES.INVALID_ALIAS, `Invalid alias target "${aliasStr}". Alias must be a NodeToken, MultiNodeToken, or a class decorated with @NodeInjectable().`);
75
100
  }
76
101
  static loopAlias(alias) {
77
- return new _InjectionError(201, `Token "${alias.toString()}" cannot alias itself in a loop.`);
102
+ return new _InjectionError(ERR_CODES.LOOP_ALIAS, `Token "${alias.toString()}" cannot alias itself in a loop.`);
78
103
  }
79
104
  // Bootstrap errors
80
105
  static notBootstrapped() {
81
- return new _InjectionError(300, "Cannot retrieve providers before the container has been bootstrapped.");
106
+ return new _InjectionError(ERR_CODES.NOT_BOOTSTRAPPED, "Cannot retrieve providers before the container has been bootstrapped.");
82
107
  }
83
108
  static bootstrapped() {
84
- return new _InjectionError(301, "Cannot modify providers after the container has been bootstrapped.");
109
+ return new _InjectionError(ERR_CODES.BOOTSTRAPPED, "Cannot modify providers after the container has been bootstrapped.");
85
110
  }
86
111
  static doubleBootstrap() {
87
- return new _InjectionError(302, "Container has already been bootstrapped and cannot be bootstrapped again.");
112
+ return new _InjectionError(ERR_CODES.DOUBLE_BOOTSTRAP, "Container has already been bootstrapped and cannot be bootstrapped again.");
88
113
  }
89
114
  // Retrieval errors
90
115
  static notFound(token) {
91
- return new _InjectionError(400, `No provider found for "${token.toString()}".`);
116
+ return new _InjectionError(ERR_CODES.NOT_FOUND, `No provider found for "${token.toString()}".`);
92
117
  }
93
118
  static circularDependency(provider, path) {
94
119
  const providerStr = provider instanceof NodeBase ? provider.toString() : provider.name;
95
120
  const pathStr = path.map((p) => p instanceof NodeBase ? p.toString() : p.name).join(" -> ");
96
- return new _InjectionError(401, `Circular dependency detected while resolving "${providerStr}":
121
+ return new _InjectionError(ERR_CODES.CIRCULAR_DEPENDENCY, `Circular dependency detected while resolving "${providerStr}":
97
122
  ${pathStr}`);
98
123
  }
99
124
  // Instantiation errors
100
125
  static untracked(token, parent) {
101
126
  const tokenStr = token instanceof NodeBase ? token.toString() : token.name;
102
127
  const parentStr = parent instanceof NodeBase ? parent.toString() : parent.name;
103
- return new _InjectionError(500, `Cannot instantiate ${parentStr} because it depends on untracked injection ${tokenStr}. Please make sure all injections are properly tracked.`);
128
+ return new _InjectionError(ERR_CODES.UNTRACKED, `Cannot instantiate ${parentStr} because it depends on untracked injection ${tokenStr}. Please make sure all injections are properly tracked.`);
104
129
  }
105
130
  static outsideContext(token) {
106
131
  const tokenStr = token instanceof NodeBase ? token.toString() : token.name;
107
- return new _InjectionError(501, `Cannot inject "${tokenStr}" outside of an injection context.`);
132
+ return new _InjectionError(ERR_CODES.OUTSIDE_CONTEXT, `Cannot inject "${tokenStr}" outside of an injection context.`);
108
133
  }
109
134
  static calledUtilsOutsideContext() {
110
- return new _InjectionError(502, "Cannot call injection utilities outside of an injection context.");
135
+ return new _InjectionError(ERR_CODES.CALLED_UTILS_OUTSIDE_CONTEXT, "Cannot call injection utilities outside of an injection context.");
111
136
  }
112
137
  static instanceAccessFailed(token) {
113
- return new _InjectionError(503, `Failed to access instance for token "${token.toString()}". It was not properly instantiated.`);
138
+ return new _InjectionError(ERR_CODES.INSTANCE_ACCESS_FAILED, `Failed to access instance for token "${token.toString()}". It was not properly instantiated.`);
114
139
  }
115
140
  static accessFailed() {
116
- return new _InjectionError(504, "Failed to access the requested instance due to an unknown error.");
141
+ return new _InjectionError(ERR_CODES.ACCESS_FAILED, "Failed to access the requested instance due to an unknown error.");
117
142
  }
118
143
  };
144
+ function isNotFoundError(error) {
145
+ return error instanceof InjectionError && error.code === ERR_CODES.NOT_FOUND;
146
+ }
147
+ __name(isNotFoundError, "isNotFoundError");
119
148
 
120
149
  // src/lib/api/token.ts
121
150
  var NodeBase = class {
@@ -478,23 +507,29 @@ var TreeRootNode = class {
478
507
  static {
479
508
  __name(this, "TreeRootNode");
480
509
  }
510
+ instant;
481
511
  _deps = /* @__PURE__ */ new Set();
482
512
  _treePool = /* @__PURE__ */ new Map();
513
+ constructor(instant = true) {
514
+ this.instant = instant;
515
+ }
483
516
  get dependencies() {
484
517
  return this._deps;
485
518
  }
486
519
  addDependency(node) {
487
520
  this._deps.add(node);
488
521
  }
489
- instantiate() {
522
+ build() {
490
523
  for (const dep of this._deps) {
491
- dep.instantiate(this._treePool);
492
524
  if ("token" in dep.proto) this._treePool.set(dep.proto.token, dep);
525
+ if (this.instant) dep.instantiate(this._treePool);
526
+ else dep.collectPool(this._treePool);
493
527
  }
494
528
  }
495
529
  find(token) {
496
530
  const node = this._treePool.get(token);
497
531
  if (!node) return null;
532
+ if (!this.instant) node.instantiate(this._treePool);
498
533
  return node;
499
534
  }
500
535
  toString() {
@@ -525,6 +560,11 @@ var TreeNodeSingle = class {
525
560
  else this._deps.set(node.proto.token, node);
526
561
  node.allocations++;
527
562
  }
563
+ collectPool(pool) {
564
+ for (const node of this._deps.values()) node.collectPool(pool);
565
+ for (const dep of this._transparent) dep.collectPool(pool);
566
+ pool.set(this.proto.token, this);
567
+ }
528
568
  instantiate(pool) {
529
569
  if (this._resolved) return;
530
570
  for (const node of this._deps.values()) node.instantiate(pool);
@@ -562,6 +602,10 @@ var TreeNodeTransparent = class _TreeNodeTransparent {
562
602
  else this._deps.set(node.proto.token, node);
563
603
  node.allocations++;
564
604
  }
605
+ collectPool(pool) {
606
+ for (const node of this._deps.values()) node.collectPool(pool);
607
+ for (const dep of this._transparent) dep.collectPool(pool);
608
+ }
565
609
  instantiate(pool) {
566
610
  if (this._resolved) return;
567
611
  for (const dep of this._transparent) dep.instantiate(pool);
@@ -586,6 +630,10 @@ var TreeNodeMulti = class _TreeNodeMulti {
586
630
  constructor(proto) {
587
631
  this.proto = proto;
588
632
  }
633
+ collectPool(pool) {
634
+ for (const dep of this._deps) dep.collectPool(pool);
635
+ pool.set(this.proto.token, this);
636
+ }
589
637
  instantiate(pool) {
590
638
  if (this._resolved) return;
591
639
  for (const dep of this._deps) {
@@ -762,6 +810,37 @@ var InjectorImpl = class {
762
810
  };
763
811
  var Injector = new NodeToken("Injector");
764
812
 
813
+ // src/lib/utils/defer.ts
814
+ function injectDefer(provider, options) {
815
+ const injector = nodeInject(Injector);
816
+ let token = provider;
817
+ if (isInjectable(provider)) token = getInjectableToken(provider);
818
+ if (!isNodeBase(token)) throw InjectionError.invalidProvider(String(token));
819
+ let resolved = false;
820
+ let instance = SHAPE_SHIFTER;
821
+ return () => {
822
+ if (resolved) return instance;
823
+ if (options?.optional) {
824
+ try {
825
+ instance = injector.get(token);
826
+ resolved = true;
827
+ return instance;
828
+ } catch (e) {
829
+ if (isNotFoundError(e)) {
830
+ resolved = true;
831
+ instance = null;
832
+ return instance;
833
+ }
834
+ throw e;
835
+ }
836
+ }
837
+ instance = injector.get(token);
838
+ resolved = true;
839
+ return instance;
840
+ };
841
+ }
842
+ __name(injectDefer, "injectDefer");
843
+
765
844
  // src/lib/utils/inheritance.ts
766
845
  function injectGroupAsync(fn, opts) {
767
846
  const { container: parent } = nodeInject(Injector);
@@ -956,7 +1035,7 @@ var NodeContainer = class extends Illuma {
956
1035
  return parentNode.findNode(token);
957
1036
  }
958
1037
  _buildInjectionTree() {
959
- const root = new TreeRootNode();
1038
+ const root = new TreeRootNode(this._opts?.instant);
960
1039
  const cache = /* @__PURE__ */ new Map();
961
1040
  const nodes = [
962
1041
  ...this._protoNodes.values(),
@@ -1001,7 +1080,7 @@ var NodeContainer = class extends Illuma {
1001
1080
  value: new InjectorImpl(this)
1002
1081
  });
1003
1082
  this._rootNode = this._buildInjectionTree();
1004
- this._rootNode.instantiate();
1083
+ this._rootNode.build();
1005
1084
  this._bootstrapped = true;
1006
1085
  const end = performance.now();
1007
1086
  const duration = end - start;
@@ -1072,6 +1151,7 @@ var NodeContainer = class extends Illuma {
1072
1151
  };
1073
1152
  // Annotate the CommonJS export names for ESM import in node:
1074
1153
  0 && (module.exports = {
1154
+ ILLUMA_ERR_CODES,
1075
1155
  INJECTION_SYMBOL,
1076
1156
  InjectionContext,
1077
1157
  InjectionError,
@@ -1085,6 +1165,7 @@ var NodeContainer = class extends Illuma {
1085
1165
  extractToken,
1086
1166
  getInjectableToken,
1087
1167
  injectAsync,
1168
+ injectDefer,
1088
1169
  injectEntryAsync,
1089
1170
  injectGroupAsync,
1090
1171
  isConstructor,