@h3ravel/core 1.29.0-alpha.16 → 2.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/dist/index.d.ts CHANGED
@@ -2,11 +2,12 @@
2
2
  import { PathLoader } from "@h3ravel/shared";
3
3
  import { H3, H3Event } from "h3";
4
4
  import { Bindings, CallableConstructor, ClassConstructor, ConcreteConstructor, ExtractClassMethods, GenericObject, IApplication, IBinding, IBootstraper, IContainer, IController, IHttpContext, IMiddleware, IPathName, IRegisterer, IRouter, IServiceProvider, IUrl, UseKey } from "@h3ravel/contracts";
5
- import { AppBuilder, Inject, Injectable, MiddlewareHandler, ResponseCodes } from "@h3ravel/foundation";
6
5
  import { ServiceProvider } from "@h3ravel/support";
6
+ import { AppBuilder, Inject, Injectable, MiddlewareHandler, ResponseCodes } from "@h3ravel/foundation";
7
7
 
8
8
  //#region src/Container.d.ts
9
9
  declare class Container extends IContainer {
10
+ private static readonly containerToken;
10
11
  bindings: Map<IBinding, () => unknown>;
11
12
  singletons: Map<IBinding, unknown>;
12
13
  middlewareHandler?: MiddlewareHandler;
@@ -25,23 +26,23 @@ declare class Container extends IContainer {
25
26
  /**
26
27
  * All of the registered rebound callbacks.
27
28
  */
28
- protected reboundCallbacks: Map<string | (new (...args: any[]) => unknown), ((...args: any[]) => any)[]>;
29
+ protected reboundCallbacks: Map<string | symbol | (new (...args: any[]) => unknown), ((...args: any[]) => any)[]>;
29
30
  /**
30
31
  * The container's shared instances.
31
32
  */
32
- protected instances: Map<string | (new (...args: any[]) => unknown), new (...args: any[]) => any>;
33
+ protected instances: Map<string | symbol | (new (...args: any[]) => unknown), new (...args: any[]) => any>;
33
34
  /**
34
35
  * The container's resolved instances.
35
36
  */
36
- protected resolvedInstances: Map<string | (new (...args: any[]) => unknown), any>;
37
+ protected resolvedInstances: Map<string | symbol | (new (...args: any[]) => unknown), any>;
37
38
  /**
38
39
  * The registered type alias.
39
40
  */
40
- protected aliases: Map<string | ClassConstructor, any>;
41
+ protected aliases: Map<IBinding, any>;
41
42
  /**
42
43
  * The registered aliases keyed by the abstract name.
43
44
  */
44
- protected abstractAliases: Map<string | ClassConstructor, any[]>;
45
+ protected abstractAliases: Map<IBinding, any[]>;
45
46
  /**
46
47
  * The registered aliases keyed by the abstract name.
47
48
  */
@@ -49,7 +50,7 @@ declare class Container extends IContainer {
49
50
  /**
50
51
  * The extension closures for services.
51
52
  */
52
- protected extenders: Map<string | (new (...args: any[]) => unknown), CallableConstructor[]>;
53
+ protected extenders: Map<string | symbol | (new (...args: any[]) => unknown), CallableConstructor[]>;
53
54
  /**
54
55
  * Check if the target has any decorators
55
56
  *
@@ -179,6 +180,13 @@ declare class Container extends IContainer {
179
180
  * @param abstract
180
181
  */
181
182
  getAlias(abstract: any): any;
183
+ /**
184
+ * Convert contract constructors to stable keys shared across module loaders.
185
+ *
186
+ * @param key
187
+ * @returns
188
+ */
189
+ private normalizeKey;
182
190
  /**
183
191
  * Get the extender callbacks for a given type.
184
192
  *
package/dist/index.js CHANGED
@@ -1,12 +1,11 @@
1
- import { createRequire } from "node:module";
2
1
  import "reflect-metadata";
3
- import { FileSystem, Logger, PathLoader, Resolver } from "@h3ravel/shared";
2
+ import { FileSystem, Logger, PathLoader, Resolver, importFile } from "@h3ravel/shared";
4
3
  import { H3, serve } from "h3";
5
4
  import { CKernel, IApplication, IContainer, IController, IKernel, IRegisterer } from "@h3ravel/contracts";
6
5
  import { Arr, InvalidArgumentException, Obj, RuntimeException, ServiceProvider, Str, data_get, dd, dump, str } from "@h3ravel/support";
7
6
  import { AppBuilder, ConfigException, Helpers, HttpException, Inject, Injectable, MiddlewareHandler, NotFoundHttpException } from "@h3ravel/foundation";
8
- import { createRequire as createRequire$1 } from "module";
9
7
  import fg from "fast-glob";
8
+ import { readFileSync } from "node:fs";
10
9
  import path from "node:path";
11
10
  import { detect } from "detect-port";
12
11
  import dotenv from "dotenv";
@@ -53,6 +52,7 @@ var ContainerResolver = class ContainerResolver {
53
52
  //#endregion
54
53
  //#region src/Container.ts
55
54
  var Container = class Container extends IContainer {
55
+ static containerToken = Symbol.for("@h3ravel/contracts/container-token");
56
56
  bindings = /* @__PURE__ */ new Map();
57
57
  singletons = /* @__PURE__ */ new Map();
58
58
  middlewareHandler;
@@ -144,6 +144,7 @@ var Container = class Container extends IContainer {
144
144
  }
145
145
  }
146
146
  singleton(key, factory) {
147
+ key = this.getAlias(key);
147
148
  this.bindings.set(key, () => {
148
149
  if (!this.singletons.has(key)) this.singletons.set(key, this.call(factory));
149
150
  return this.singletons.get(key);
@@ -230,11 +231,13 @@ var Container = class Container extends IContainer {
230
231
  return resolved;
231
232
  }
232
233
  afterResolving(key, callback) {
234
+ key = this.getAlias(key);
233
235
  const existing = this.afterResolvingCallbacks.get(key) || [];
234
236
  existing.push(callback);
235
237
  this.afterResolvingCallbacks.set(key, existing);
236
238
  }
237
239
  beforeResolving(key, callback) {
240
+ key = this.getAlias(key);
238
241
  const existing = this.beforeResolvingCallbacks.get(key) || [];
239
242
  existing.push(callback);
240
243
  this.beforeResolvingCallbacks.set(key, existing);
@@ -280,6 +283,7 @@ var Container = class Container extends IContainer {
280
283
  * @param name
281
284
  */
282
285
  isAlias(name) {
286
+ name = this.normalizeKey(name);
283
287
  return this.aliases.has(name) && typeof this.aliases.get(name) !== "undefined";
284
288
  }
285
289
  /**
@@ -288,11 +292,22 @@ var Container = class Container extends IContainer {
288
292
  * @param abstract
289
293
  */
290
294
  getAlias(abstract) {
291
- if (typeof abstract === "string" && this.aliases.has(abstract)) return this.getAlias(this.aliases.get(abstract));
295
+ abstract = this.normalizeKey(abstract);
296
+ if (this.aliases.has(abstract)) return this.getAlias(this.aliases.get(abstract));
292
297
  if (abstract == null) return abstract;
293
298
  return this.aliases.get(abstract) ?? abstract;
294
299
  }
295
300
  /**
301
+ * Convert contract constructors to stable keys shared across module loaders.
302
+ *
303
+ * @param key
304
+ * @returns
305
+ */
306
+ normalizeKey(key) {
307
+ if (key != null && Object.prototype.hasOwnProperty.call(key, Container.containerToken)) return key[Container.containerToken];
308
+ return key;
309
+ }
310
+ /**
296
311
  * Get the extender callbacks for a given type.
297
312
  *
298
313
  * @param abstract
@@ -309,8 +324,8 @@ var Container = class Container extends IContainer {
309
324
  this.extenders.delete(this.getAlias(abstract));
310
325
  }
311
326
  alias(key, target) {
312
- if (Array.isArray(key)) for (const [tokn, targ] of key) this.aliases.set(tokn, targ);
313
- else this.aliases.set(key, target);
327
+ if (Array.isArray(key)) for (const [tokn, targ] of key) this.aliases.set(this.normalizeKey(tokn), this.normalizeKey(targ));
328
+ else this.aliases.set(this.normalizeKey(key), this.normalizeKey(target));
314
329
  return this;
315
330
  }
316
331
  rebinding(abstract, callback) {
@@ -319,6 +334,7 @@ var Container = class Container extends IContainer {
319
334
  if (this.bound(abstract)) return this.make(abstract);
320
335
  }
321
336
  bound(abstract) {
337
+ abstract = this.getAlias(abstract);
322
338
  return this.bindings.has(abstract) || !!this.instances.get(abstract) || this.isAlias(abstract);
323
339
  }
324
340
  has(key) {
@@ -330,7 +346,7 @@ var Container = class Container extends IContainer {
330
346
  * @param abstract
331
347
  */
332
348
  resolved(abstract) {
333
- if (this.isAlias(abstract)) abstract = this.getAlias(abstract);
349
+ abstract = this.getAlias(abstract);
334
350
  return this.resolvedInstances.has(abstract) || this.instances.has(abstract);
335
351
  }
336
352
  extend(abstract, closure) {
@@ -570,7 +586,7 @@ var ProviderRegistry = class {
570
586
  if (autoRegister) {
571
587
  for (const manifestPath of manifests) {
572
588
  const pkg = this.getManifest(path.resolve(manifestPath));
573
- if (pkg.h3ravel?.providers) providers.push(...await Promise.all(pkg.h3ravel.providers.map(async (name) => (await import(path.resolve(path.dirname(manifestPath), "dist/index.js")))[name])));
589
+ if (pkg.h3ravel?.providers) providers.push(...await Promise.all(pkg.h3ravel.providers.map(async (name) => (await importFile(path.resolve(path.dirname(manifestPath), "dist/index.js")))[name])));
574
590
  }
575
591
  for (const provider of providers.filter((e) => typeof e !== "undefined")) {
576
592
  const key = this.getKey(provider);
@@ -586,7 +602,7 @@ var ProviderRegistry = class {
586
602
  * @returns
587
603
  */
588
604
  static getManifest(manifestPath) {
589
- return createRequire$1(import.meta.url)(manifestPath);
605
+ return JSON.parse(readFileSync(manifestPath, "utf8"));
590
606
  }
591
607
  };
592
608
  //#endregion
@@ -1153,7 +1169,7 @@ var Application = class Application extends Container {
1153
1169
  */
1154
1170
  getNamespace() {
1155
1171
  if (this.namespace != null) return this.namespace;
1156
- const pkg = createRequire(import.meta.url)(path.join(process.cwd(), "package.json"));
1172
+ const pkg = JSON.parse(readFileSync(path.join(this.basePath, "package.json"), "utf8"));
1157
1173
  for (const [namespace, pathChoice] of Object.entries(data_get(pkg, "autoload.namespaces"))) if (this.getPath("app", "/") === this.getPath("src", pathChoice)) return this.namespace = namespace;
1158
1174
  throw new RuntimeException("Unable to detect application namespace.");
1159
1175
  }
package/package.json CHANGED
@@ -1,11 +1,9 @@
1
1
  {
2
2
  "name": "@h3ravel/core",
3
- "version": "1.29.0-alpha.16",
3
+ "version": "2.0.0",
4
4
  "description": "Core application container, lifecycle management and service providers for H3ravel.",
5
5
  "type": "module",
6
- "main": "./dist/index.cjs",
7
6
  "types": "./dist/index.d.ts",
8
- "module": "./dist/index.js",
9
7
  "exports": {
10
8
  ".": {
11
9
  "import": "./dist/index.js",
@@ -43,9 +41,9 @@
43
41
  "laravel"
44
42
  ],
45
43
  "dependencies": {
46
- "@h3ravel/shared": "^1.29.0-alpha.16",
47
- "@h3ravel/support": "^1.29.0-alpha.16",
48
- "@h3ravel/foundation": "^1.29.0-alpha.16",
44
+ "@h3ravel/shared": "^2.0.0",
45
+ "@h3ravel/support": "^2.0.0",
46
+ "@h3ravel/foundation": "^2.0.0",
49
47
  "chalk": "^5.6.2",
50
48
  "commander": "^14.0.1",
51
49
  "detect-port": "^2.1.0",
@@ -59,13 +57,13 @@
59
57
  "tslib": "^2.8.1"
60
58
  },
61
59
  "devDependencies": {
62
- "@h3ravel/contracts": "^1.29.0-alpha.16",
60
+ "@h3ravel/contracts": "^2.0.0",
63
61
  "@types/semver": "^7.7.1",
64
62
  "typescript": "^6.0.0"
65
63
  },
66
64
  "scripts": {
67
65
  "build": "tsdown --config-loader unrun",
68
- "dev": "tsx watch src/index.ts",
66
+ "dev": "tsdown --watch --config-loader unrun",
69
67
  "start": "node dist/index.js",
70
68
  "lint": "eslint . --ext .ts",
71
69
  "test": "jest --passWithNoTests",