clear-router 2.5.6 → 2.5.8

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.
Files changed (41) hide show
  1. package/dist/{bindings-DIanvIVd.mjs → bindings-DJDdQv1q.mjs} +1 -1
  2. package/dist/{bindings-DvV2DXWi.cjs → bindings-T0FKpWyi.cjs} +6 -0
  3. package/dist/core/index.cjs +2 -2
  4. package/dist/core/index.d.cts +2 -2
  5. package/dist/core/index.d.mts +2 -2
  6. package/dist/core/index.mjs +2 -2
  7. package/dist/decorators/index.cjs +1 -1
  8. package/dist/decorators/index.mjs +1 -1
  9. package/dist/decorators/setup.cjs +2 -2
  10. package/dist/decorators/setup.mjs +2 -2
  11. package/dist/express/index.cjs +3 -3
  12. package/dist/express/index.d.cts +1 -1
  13. package/dist/express/index.d.mts +1 -1
  14. package/dist/express/index.mjs +3 -3
  15. package/dist/fastify/index.cjs +3 -3
  16. package/dist/fastify/index.d.cts +1 -1
  17. package/dist/fastify/index.d.mts +1 -1
  18. package/dist/fastify/index.mjs +3 -3
  19. package/dist/h3/index.cjs +3 -3
  20. package/dist/h3/index.d.cts +1 -1
  21. package/dist/h3/index.d.mts +1 -1
  22. package/dist/h3/index.mjs +3 -3
  23. package/dist/hono/index.cjs +3 -3
  24. package/dist/hono/index.d.cts +1 -1
  25. package/dist/hono/index.d.mts +1 -1
  26. package/dist/hono/index.mjs +3 -3
  27. package/dist/index.cjs +94 -32
  28. package/dist/index.d.cts +23 -4
  29. package/dist/index.d.mts +23 -4
  30. package/dist/index.mjs +94 -32
  31. package/dist/koa/index.cjs +3 -3
  32. package/dist/koa/index.d.cts +1 -1
  33. package/dist/koa/index.d.mts +1 -1
  34. package/dist/koa/index.mjs +3 -3
  35. package/dist/{responses-_II3dOJ5.mjs → responses-B184V5nj.mjs} +1 -1
  36. package/dist/{responses-JzXstGU5.cjs → responses-CfCmLLo9.cjs} +1 -1
  37. package/dist/{router-DCMtQ_Xi.d.mts → router-B8l8jerS.d.mts} +23 -4
  38. package/dist/{router-BYZmNzrZ.d.cts → router-CiGcLUZD.d.cts} +23 -4
  39. package/dist/{router-CU4V1kX0.cjs → router-DbPFGTYG.cjs} +95 -33
  40. package/dist/{router-B3QjblRX.mjs → router-we2Hw9nA.mjs} +95 -33
  41. package/package.json +5 -5
package/dist/index.mjs CHANGED
@@ -218,6 +218,7 @@ var CoreRouter = class {
218
218
  static stateBoundKey = Symbol.for("clear-router:router-state-bound");
219
219
  static defaultConfigKey = Symbol.for("clear-router:default-config");
220
220
  static pluginStoreKey = Symbol.for("clear-router:plugins");
221
+ static pluginPendingKey = Symbol.for("clear-router:plugin-promises");
221
222
  static createBaseConfig() {
222
223
  return {
223
224
  methodOverride: {
@@ -264,6 +265,11 @@ var CoreRouter = class {
264
265
  if (!g[this.pluginStoreKey]) g[this.pluginStoreKey] = /* @__PURE__ */ new Set();
265
266
  return g[this.pluginStoreKey];
266
267
  }
268
+ static getPluginPendingStore() {
269
+ const g = globalThis;
270
+ if (!g[this.pluginPendingKey]) g[this.pluginPendingKey] = /* @__PURE__ */ new Set();
271
+ return g[this.pluginPendingKey];
272
+ }
267
273
  static createDefaultState() {
268
274
  return {
269
275
  config: this.getDefaultConfig(),
@@ -369,28 +375,81 @@ var CoreRouter = class {
369
375
  * @param options
370
376
  * @returns
371
377
  */
372
- static use(plugin, options) {
378
+ static async use(plugin, options) {
373
379
  const name = typeof plugin === "function" ? plugin.name : plugin.name;
374
380
  const store = this.getPluginStore();
375
381
  if (name && store.has(name)) return;
376
- const ctx = {
377
- container: Container,
378
- bind: Container.bind.bind(Container),
379
- configure: this.configure.bind(this),
380
- configureDefaults: this.configureDefaults.bind(this),
381
- options
382
- };
383
- if (typeof plugin === "function") plugin(ctx);
384
- else plugin.setup(ctx);
385
382
  if (name) store.add(name);
383
+ const setup = async () => {
384
+ const ctx = {
385
+ container: Container,
386
+ bind: this.createPluginBind(),
387
+ configure: this.configure.bind(this),
388
+ configureDefaults: this.configureDefaults.bind(this),
389
+ get request() {
390
+ return this.getRequest();
391
+ },
392
+ get response() {
393
+ return this.getResponse();
394
+ },
395
+ getRequest: () => this.getCurrentPluginRequestContext()?.request,
396
+ getResponse: () => this.getCurrentPluginRequestContext()?.response,
397
+ options
398
+ };
399
+ if (typeof plugin === "function") await plugin(ctx);
400
+ else await plugin.setup(ctx);
401
+ };
402
+ const pending = this.getPluginPendingStore();
403
+ const promise = setup();
404
+ pending.add(promise);
405
+ try {
406
+ await promise;
407
+ } catch (error) {
408
+ if (name) store.delete(name);
409
+ throw error;
410
+ } finally {
411
+ pending.delete(promise);
412
+ }
413
+ }
414
+ static async pluginsReady() {
415
+ const pending = Array.from(this.getPluginPendingStore());
416
+ if (!pending.length) return;
417
+ await Promise.all(pending);
386
418
  }
387
419
  static groupContext = new AsyncLocalStorage();
420
+ static pluginRequestContext = new AsyncLocalStorage();
388
421
  static routes = [];
389
422
  static routesByPathMethod = {};
390
423
  static routesByMethod = {};
391
424
  static prefix = "";
392
425
  static groupMiddlewares = [];
393
426
  static globalMiddlewares = [];
427
+ static getCurrentPluginRequestContext() {
428
+ return this.pluginRequestContext.getStore();
429
+ }
430
+ static createPluginRequestContext(ctx) {
431
+ const request = ctx.clearRequest;
432
+ const response = ctx.clearResponse;
433
+ return {
434
+ ...ctx,
435
+ ctx,
436
+ request,
437
+ response,
438
+ clearRequest: request,
439
+ clearResponse: response
440
+ };
441
+ }
442
+ static createPluginBind() {
443
+ const bind = (token, value) => {
444
+ if (typeof value === "function" && !isClass(value)) {
445
+ const factory = value;
446
+ Container.bind(token, (ctx) => factory(this.createPluginRequestContext(ctx)));
447
+ return;
448
+ }
449
+ Container.bind(token, value);
450
+ };
451
+ return bind;
452
+ }
394
453
  static ensureState() {
395
454
  this.bindStateAccessors();
396
455
  if (!this.config) this.config = { methodOverride: {
@@ -705,29 +764,32 @@ var CoreRouter = class {
705
764
  };
706
765
  }
707
766
  static async callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata) {
708
- if (!this.config.container?.enabled) return handlerFunction(ctx, ctx.clearRequest);
709
- const metadata = getBindingMetadataFromTargets([
710
- {
711
- target: bindingTarget,
712
- propertyKey: bindingMethod
713
- },
714
- { target: bindingHandler },
715
- {
716
- target: bindingTarget,
717
- propertyKey: "__class__"
767
+ return this.pluginRequestContext.run(this.createPluginRequestContext(ctx), async () => {
768
+ await this.pluginsReady();
769
+ if (!this.config.container?.enabled) return handlerFunction(ctx, ctx.clearRequest);
770
+ const metadata = getBindingMetadataFromTargets([
771
+ {
772
+ target: bindingTarget,
773
+ propertyKey: bindingMethod
774
+ },
775
+ { target: bindingHandler },
776
+ {
777
+ target: bindingTarget,
778
+ propertyKey: "__class__"
779
+ }
780
+ ]) ?? getStandardMetadata(bindingMetadata, bindingMethod) ?? getStandardMetadata(bindingMetadata, "__class__");
781
+ if (!metadata) return handlerFunction(ctx, ctx.clearRequest);
782
+ const designTokens = [...bindingTarget ? getDesignParamTypes(bindingTarget, bindingMethod) : [], ...bindingHandler ? getDesignParamTypes(bindingHandler) : []];
783
+ const tokens = metadata.tokens?.length ? metadata.tokens : designTokens;
784
+ if (!tokens.length) return handlerFunction(ctx, ctx.clearRequest);
785
+ const args = [];
786
+ for (const token of tokens) {
787
+ const resolved = await Container.resolve(token, ctx, Boolean(this.config.container?.autoDiscover));
788
+ if (typeof resolved === "undefined") return handlerFunction(ctx, ctx.clearRequest);
789
+ args.push(resolved);
718
790
  }
719
- ]) ?? getStandardMetadata(bindingMetadata, bindingMethod) ?? getStandardMetadata(bindingMetadata, "__class__");
720
- if (!metadata) return handlerFunction(ctx, ctx.clearRequest);
721
- const designTokens = [...bindingTarget ? getDesignParamTypes(bindingTarget, bindingMethod) : [], ...bindingHandler ? getDesignParamTypes(bindingHandler) : []];
722
- const tokens = metadata.tokens?.length ? metadata.tokens : designTokens;
723
- if (!tokens.length) return handlerFunction(ctx, ctx.clearRequest);
724
- const args = [];
725
- for (const token of tokens) {
726
- const resolved = await Container.resolve(token, ctx, Boolean(this.config.container?.autoDiscover));
727
- if (typeof resolved === "undefined") return handlerFunction(ctx, ctx.clearRequest);
728
- args.push(resolved);
729
- }
730
- return handlerFunction(...args);
791
+ return handlerFunction(...args);
792
+ });
731
793
  }
732
794
  static bindRequestToInstance(ctx, instance, route, payload) {
733
795
  const clearRequest = ctx.clearRequest instanceof Request ? ctx.clearRequest : new Request({
@@ -1,7 +1,7 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
- require('../bindings-DvV2DXWi.cjs');
3
- const require_router = require('../router-CU4V1kX0.cjs');
4
- const require_responses = require('../responses-JzXstGU5.cjs');
2
+ require('../bindings-T0FKpWyi.cjs');
3
+ const require_router = require('../router-DbPFGTYG.cjs');
4
+ const require_responses = require('../responses-CfCmLLo9.cjs');
5
5
 
6
6
  //#region src/koa/router.ts
7
7
  /**
@@ -1,4 +1,4 @@
1
- import { _ as ApiResourceMiddleware, b as HttpMethod, c as Request, l as Route, t as CoreRouter, v as ControllerAction, x as Response, y as ControllerHandler } from "../router-BYZmNzrZ.cjs";
1
+ import { C as HttpMethod, S as ControllerHandler, b as ApiResourceMiddleware, d as Request, f as Route, t as CoreRouter, w as Response, x as ControllerAction } from "../router-CiGcLUZD.cjs";
2
2
  import Koa from "koa";
3
3
  import Router$1 from "@koa/router";
4
4
 
@@ -1,4 +1,4 @@
1
- import { _ as ApiResourceMiddleware, b as HttpMethod, c as Request, l as Route, t as CoreRouter, v as ControllerAction, x as Response, y as ControllerHandler } from "../router-DCMtQ_Xi.mjs";
1
+ import { C as HttpMethod, S as ControllerHandler, b as ApiResourceMiddleware, d as Request, f as Route, t as CoreRouter, w as Response, x as ControllerAction } from "../router-B8l8jerS.mjs";
2
2
  import Koa from "koa";
3
3
  import Router$1 from "@koa/router";
4
4
 
@@ -1,6 +1,6 @@
1
- import "../bindings-DIanvIVd.mjs";
2
- import { t as CoreRouter } from "../router-B3QjblRX.mjs";
3
- import { n as resolveResponseMeta, t as isFetchResponse } from "../responses-_II3dOJ5.mjs";
1
+ import "../bindings-DJDdQv1q.mjs";
2
+ import { t as CoreRouter } from "../router-we2Hw9nA.mjs";
3
+ import { n as resolveResponseMeta, t as isFetchResponse } from "../responses-B184V5nj.mjs";
4
4
 
5
5
  //#region src/koa/router.ts
6
6
  /**
@@ -1,4 +1,4 @@
1
- import { o as Response } from "./bindings-DIanvIVd.mjs";
1
+ import { s as Response } from "./bindings-DJDdQv1q.mjs";
2
2
 
3
3
  //#region src/core/responses.ts
4
4
  function isFetchResponse(value) {
@@ -1,4 +1,4 @@
1
- const require_bindings = require('./bindings-DvV2DXWi.cjs');
1
+ const require_bindings = require('./bindings-T0FKpWyi.cjs');
2
2
 
3
3
  //#region src/core/responses.ts
4
4
  function isFetchResponse(value) {
@@ -190,13 +190,25 @@ declare class Request$1<X = any, M = any> extends ClearRequest<X, M> {
190
190
  }
191
191
  //#endregion
192
192
  //#region src/core/plugins.d.ts
193
- type PluginSetupResult = void;
194
- type PluginBind = <T>(token: BindToken<T>, value: BindValue<T>) => void;
193
+ type PluginSetupResult = void | Promise<void>;
194
+ interface ClearRouterPluginRequestContext {
195
+ ctx: any;
196
+ request: Request$1;
197
+ response: Response$2;
198
+ [key: string]: any;
199
+ }
200
+ type PluginBindFactory<T = any> = (ctx: ClearRouterPluginRequestContext) => T | Promise<T>;
201
+ type PluginBindValue<T = any> = BindValue<T> | PluginBindFactory<T>;
202
+ type PluginBind = <T>(token: BindToken<T>, value: PluginBindValue<T>) => void;
195
203
  interface ClearRouterPluginContext<Options = any> {
196
204
  container: typeof Container;
197
205
  bind: PluginBind;
198
206
  configure: (options: RouterConfig) => void;
199
207
  configureDefaults: (options: RouterConfig) => void;
208
+ readonly request?: Request$1;
209
+ readonly response?: Response$2;
210
+ getRequest: () => Request$1 | undefined;
211
+ getResponse: () => Response$2 | undefined;
200
212
  options: Options;
201
213
  }
202
214
  interface ClearRouterPlugin<Options = any> {
@@ -229,12 +241,14 @@ declare abstract class CoreRouter {
229
241
  private static readonly stateBoundKey;
230
242
  private static readonly defaultConfigKey;
231
243
  private static readonly pluginStoreKey;
244
+ private static readonly pluginPendingKey;
232
245
  protected static createBaseConfig(): RouterConfig;
233
246
  protected static mergeConfig(target: RouterConfig, source?: RouterConfig): RouterConfig;
234
247
  protected static getDefaultConfig(): RouterConfig;
235
248
  protected static resolveStateNamespace(this: any): string;
236
249
  protected static getStateStore(): Record<string, any>;
237
250
  protected static getPluginStore(): Set<string>;
251
+ protected static getPluginPendingStore(): Set<Promise<void>>;
238
252
  protected static createDefaultState(): {
239
253
  config: RouterConfig;
240
254
  groupContext: AsyncLocalStorage<{
@@ -260,17 +274,22 @@ declare abstract class CoreRouter {
260
274
  * @param options
261
275
  * @returns
262
276
  */
263
- static use<Options = any>(this: any, plugin: ClearRouterPluginInput<Options>, options?: Options): void;
277
+ static use<Options = any>(this: any, plugin: ClearRouterPluginInput<Options>, options?: Options): Promise<void>;
278
+ protected static pluginsReady(this: any): Promise<void>;
264
279
  protected static groupContext: AsyncLocalStorage<{
265
280
  prefix: string;
266
281
  groupMiddlewares: any[];
267
282
  }>;
283
+ protected static pluginRequestContext: AsyncLocalStorage<ClearRouterPluginRequestContext>;
268
284
  static routes: Array<Route<any, any, any>>;
269
285
  static routesByPathMethod: Record<string, Route<any, any, any>>;
270
286
  static routesByMethod: { [method in Uppercase<HttpMethod>]?: Array<Route<any, any, any>> };
271
287
  static prefix: string;
272
288
  static groupMiddlewares: any[];
273
289
  static globalMiddlewares: any[];
290
+ protected static getCurrentPluginRequestContext(): ClearRouterPluginRequestContext | undefined;
291
+ protected static createPluginRequestContext(ctx: any): ClearRouterPluginRequestContext;
292
+ protected static createPluginBind(): PluginBind;
274
293
  protected static ensureState(this: any): void;
275
294
  /**
276
295
  * Normalizes a path by ensuring it starts with a single slash and does not have trailing
@@ -426,4 +445,4 @@ declare abstract class CoreRouter {
426
445
  }): void;
427
446
  }
428
447
  //#endregion
429
- export { ApiResourceMiddleware as _, PluginBind as a, HttpMethod as b, Request$1 as c, Handler as d, HttpContext as f, Middleware$1 as g, HttpContext$1 as h, ClearRouterPluginInput as i, Route as l, Handler$1 as m, ClearRouterPlugin as n, PluginSetupResult as o, Middleware as p, ClearRouterPluginContext as r, definePlugin as s, CoreRouter as t, H3App as u, ControllerAction as v, Response$2 as x, ControllerHandler as y };
448
+ export { HttpMethod as C, ControllerHandler as S, Handler$1 as _, ClearRouterPluginRequestContext as a, ApiResourceMiddleware as b, PluginBindValue as c, Request$1 as d, Route as f, Middleware as g, HttpContext as h, ClearRouterPluginInput as i, PluginSetupResult as l, Handler as m, ClearRouterPlugin as n, PluginBind as o, H3App as p, ClearRouterPluginContext as r, PluginBindFactory as s, CoreRouter as t, definePlugin as u, HttpContext$1 as v, Response$2 as w, ControllerAction as x, Middleware$1 as y };
@@ -190,13 +190,25 @@ declare class Request$1<X = any, M = any> extends ClearRequest<X, M> {
190
190
  }
191
191
  //#endregion
192
192
  //#region src/core/plugins.d.ts
193
- type PluginSetupResult = void;
194
- type PluginBind = <T>(token: BindToken<T>, value: BindValue<T>) => void;
193
+ type PluginSetupResult = void | Promise<void>;
194
+ interface ClearRouterPluginRequestContext {
195
+ ctx: any;
196
+ request: Request$1;
197
+ response: Response$2;
198
+ [key: string]: any;
199
+ }
200
+ type PluginBindFactory<T = any> = (ctx: ClearRouterPluginRequestContext) => T | Promise<T>;
201
+ type PluginBindValue<T = any> = BindValue<T> | PluginBindFactory<T>;
202
+ type PluginBind = <T>(token: BindToken<T>, value: PluginBindValue<T>) => void;
195
203
  interface ClearRouterPluginContext<Options = any> {
196
204
  container: typeof Container;
197
205
  bind: PluginBind;
198
206
  configure: (options: RouterConfig) => void;
199
207
  configureDefaults: (options: RouterConfig) => void;
208
+ readonly request?: Request$1;
209
+ readonly response?: Response$2;
210
+ getRequest: () => Request$1 | undefined;
211
+ getResponse: () => Response$2 | undefined;
200
212
  options: Options;
201
213
  }
202
214
  interface ClearRouterPlugin<Options = any> {
@@ -229,12 +241,14 @@ declare abstract class CoreRouter {
229
241
  private static readonly stateBoundKey;
230
242
  private static readonly defaultConfigKey;
231
243
  private static readonly pluginStoreKey;
244
+ private static readonly pluginPendingKey;
232
245
  protected static createBaseConfig(): RouterConfig;
233
246
  protected static mergeConfig(target: RouterConfig, source?: RouterConfig): RouterConfig;
234
247
  protected static getDefaultConfig(): RouterConfig;
235
248
  protected static resolveStateNamespace(this: any): string;
236
249
  protected static getStateStore(): Record<string, any>;
237
250
  protected static getPluginStore(): Set<string>;
251
+ protected static getPluginPendingStore(): Set<Promise<void>>;
238
252
  protected static createDefaultState(): {
239
253
  config: RouterConfig;
240
254
  groupContext: AsyncLocalStorage<{
@@ -260,17 +274,22 @@ declare abstract class CoreRouter {
260
274
  * @param options
261
275
  * @returns
262
276
  */
263
- static use<Options = any>(this: any, plugin: ClearRouterPluginInput<Options>, options?: Options): void;
277
+ static use<Options = any>(this: any, plugin: ClearRouterPluginInput<Options>, options?: Options): Promise<void>;
278
+ protected static pluginsReady(this: any): Promise<void>;
264
279
  protected static groupContext: AsyncLocalStorage<{
265
280
  prefix: string;
266
281
  groupMiddlewares: any[];
267
282
  }>;
283
+ protected static pluginRequestContext: AsyncLocalStorage<ClearRouterPluginRequestContext>;
268
284
  static routes: Array<Route<any, any, any>>;
269
285
  static routesByPathMethod: Record<string, Route<any, any, any>>;
270
286
  static routesByMethod: { [method in Uppercase<HttpMethod>]?: Array<Route<any, any, any>> };
271
287
  static prefix: string;
272
288
  static groupMiddlewares: any[];
273
289
  static globalMiddlewares: any[];
290
+ protected static getCurrentPluginRequestContext(): ClearRouterPluginRequestContext | undefined;
291
+ protected static createPluginRequestContext(ctx: any): ClearRouterPluginRequestContext;
292
+ protected static createPluginBind(): PluginBind;
274
293
  protected static ensureState(this: any): void;
275
294
  /**
276
295
  * Normalizes a path by ensuring it starts with a single slash and does not have trailing
@@ -426,4 +445,4 @@ declare abstract class CoreRouter {
426
445
  }): void;
427
446
  }
428
447
  //#endregion
429
- export { ApiResourceMiddleware as _, PluginBind as a, HttpMethod as b, Request$1 as c, Handler as d, HttpContext as f, Middleware$1 as g, HttpContext$1 as h, ClearRouterPluginInput as i, Route as l, Handler$1 as m, ClearRouterPlugin as n, PluginSetupResult as o, Middleware as p, ClearRouterPluginContext as r, definePlugin as s, CoreRouter as t, H3App as u, ControllerAction as v, Response$2 as x, ControllerHandler as y };
448
+ export { HttpMethod as C, ControllerHandler as S, Handler$1 as _, ClearRouterPluginRequestContext as a, ApiResourceMiddleware as b, PluginBindValue as c, Request$1 as d, Route as f, Middleware as g, HttpContext as h, ClearRouterPluginInput as i, PluginSetupResult as l, Handler as m, ClearRouterPlugin as n, PluginBind as o, H3App as p, ClearRouterPluginContext as r, PluginBindFactory as s, CoreRouter as t, definePlugin as u, HttpContext$1 as v, Response$2 as w, ControllerAction as x, Middleware$1 as y };
@@ -1,4 +1,4 @@
1
- const require_bindings = require('./bindings-DvV2DXWi.cjs');
1
+ const require_bindings = require('./bindings-T0FKpWyi.cjs');
2
2
  let node_async_hooks = require("node:async_hooks");
3
3
 
4
4
  //#region src/Route.ts
@@ -42,6 +42,7 @@ var CoreRouter = class {
42
42
  static stateBoundKey = Symbol.for("clear-router:router-state-bound");
43
43
  static defaultConfigKey = Symbol.for("clear-router:default-config");
44
44
  static pluginStoreKey = Symbol.for("clear-router:plugins");
45
+ static pluginPendingKey = Symbol.for("clear-router:plugin-promises");
45
46
  static createBaseConfig() {
46
47
  return {
47
48
  methodOverride: {
@@ -88,6 +89,11 @@ var CoreRouter = class {
88
89
  if (!g[this.pluginStoreKey]) g[this.pluginStoreKey] = /* @__PURE__ */ new Set();
89
90
  return g[this.pluginStoreKey];
90
91
  }
92
+ static getPluginPendingStore() {
93
+ const g = globalThis;
94
+ if (!g[this.pluginPendingKey]) g[this.pluginPendingKey] = /* @__PURE__ */ new Set();
95
+ return g[this.pluginPendingKey];
96
+ }
91
97
  static createDefaultState() {
92
98
  return {
93
99
  config: this.getDefaultConfig(),
@@ -193,28 +199,81 @@ var CoreRouter = class {
193
199
  * @param options
194
200
  * @returns
195
201
  */
196
- static use(plugin, options) {
202
+ static async use(plugin, options) {
197
203
  const name = typeof plugin === "function" ? plugin.name : plugin.name;
198
204
  const store = this.getPluginStore();
199
205
  if (name && store.has(name)) return;
200
- const ctx = {
201
- container: require_bindings.Container,
202
- bind: require_bindings.Container.bind.bind(require_bindings.Container),
203
- configure: this.configure.bind(this),
204
- configureDefaults: this.configureDefaults.bind(this),
205
- options
206
- };
207
- if (typeof plugin === "function") plugin(ctx);
208
- else plugin.setup(ctx);
209
206
  if (name) store.add(name);
207
+ const setup = async () => {
208
+ const ctx = {
209
+ container: require_bindings.Container,
210
+ bind: this.createPluginBind(),
211
+ configure: this.configure.bind(this),
212
+ configureDefaults: this.configureDefaults.bind(this),
213
+ get request() {
214
+ return this.getRequest();
215
+ },
216
+ get response() {
217
+ return this.getResponse();
218
+ },
219
+ getRequest: () => this.getCurrentPluginRequestContext()?.request,
220
+ getResponse: () => this.getCurrentPluginRequestContext()?.response,
221
+ options
222
+ };
223
+ if (typeof plugin === "function") await plugin(ctx);
224
+ else await plugin.setup(ctx);
225
+ };
226
+ const pending = this.getPluginPendingStore();
227
+ const promise = setup();
228
+ pending.add(promise);
229
+ try {
230
+ await promise;
231
+ } catch (error) {
232
+ if (name) store.delete(name);
233
+ throw error;
234
+ } finally {
235
+ pending.delete(promise);
236
+ }
237
+ }
238
+ static async pluginsReady() {
239
+ const pending = Array.from(this.getPluginPendingStore());
240
+ if (!pending.length) return;
241
+ await Promise.all(pending);
210
242
  }
211
243
  static groupContext = new node_async_hooks.AsyncLocalStorage();
244
+ static pluginRequestContext = new node_async_hooks.AsyncLocalStorage();
212
245
  static routes = [];
213
246
  static routesByPathMethod = {};
214
247
  static routesByMethod = {};
215
248
  static prefix = "";
216
249
  static groupMiddlewares = [];
217
250
  static globalMiddlewares = [];
251
+ static getCurrentPluginRequestContext() {
252
+ return this.pluginRequestContext.getStore();
253
+ }
254
+ static createPluginRequestContext(ctx) {
255
+ const request = ctx.clearRequest;
256
+ const response = ctx.clearResponse;
257
+ return {
258
+ ...ctx,
259
+ ctx,
260
+ request,
261
+ response,
262
+ clearRequest: request,
263
+ clearResponse: response
264
+ };
265
+ }
266
+ static createPluginBind() {
267
+ const bind = (token, value) => {
268
+ if (typeof value === "function" && !require_bindings.isClass(value)) {
269
+ const factory = value;
270
+ require_bindings.Container.bind(token, (ctx) => factory(this.createPluginRequestContext(ctx)));
271
+ return;
272
+ }
273
+ require_bindings.Container.bind(token, value);
274
+ };
275
+ return bind;
276
+ }
218
277
  static ensureState() {
219
278
  this.bindStateAccessors();
220
279
  if (!this.config) this.config = { methodOverride: {
@@ -529,29 +588,32 @@ var CoreRouter = class {
529
588
  };
530
589
  }
531
590
  static async callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata) {
532
- if (!this.config.container?.enabled) return handlerFunction(ctx, ctx.clearRequest);
533
- const metadata = require_bindings.getBindingMetadataFromTargets([
534
- {
535
- target: bindingTarget,
536
- propertyKey: bindingMethod
537
- },
538
- { target: bindingHandler },
539
- {
540
- target: bindingTarget,
541
- propertyKey: "__class__"
591
+ return this.pluginRequestContext.run(this.createPluginRequestContext(ctx), async () => {
592
+ await this.pluginsReady();
593
+ if (!this.config.container?.enabled) return handlerFunction(ctx, ctx.clearRequest);
594
+ const metadata = require_bindings.getBindingMetadataFromTargets([
595
+ {
596
+ target: bindingTarget,
597
+ propertyKey: bindingMethod
598
+ },
599
+ { target: bindingHandler },
600
+ {
601
+ target: bindingTarget,
602
+ propertyKey: "__class__"
603
+ }
604
+ ]) ?? require_bindings.getStandardMetadata(bindingMetadata, bindingMethod) ?? require_bindings.getStandardMetadata(bindingMetadata, "__class__");
605
+ if (!metadata) return handlerFunction(ctx, ctx.clearRequest);
606
+ const designTokens = [...bindingTarget ? require_bindings.getDesignParamTypes(bindingTarget, bindingMethod) : [], ...bindingHandler ? require_bindings.getDesignParamTypes(bindingHandler) : []];
607
+ const tokens = metadata.tokens?.length ? metadata.tokens : designTokens;
608
+ if (!tokens.length) return handlerFunction(ctx, ctx.clearRequest);
609
+ const args = [];
610
+ for (const token of tokens) {
611
+ const resolved = await require_bindings.Container.resolve(token, ctx, Boolean(this.config.container?.autoDiscover));
612
+ if (typeof resolved === "undefined") return handlerFunction(ctx, ctx.clearRequest);
613
+ args.push(resolved);
542
614
  }
543
- ]) ?? require_bindings.getStandardMetadata(bindingMetadata, bindingMethod) ?? require_bindings.getStandardMetadata(bindingMetadata, "__class__");
544
- if (!metadata) return handlerFunction(ctx, ctx.clearRequest);
545
- const designTokens = [...bindingTarget ? require_bindings.getDesignParamTypes(bindingTarget, bindingMethod) : [], ...bindingHandler ? require_bindings.getDesignParamTypes(bindingHandler) : []];
546
- const tokens = metadata.tokens?.length ? metadata.tokens : designTokens;
547
- if (!tokens.length) return handlerFunction(ctx, ctx.clearRequest);
548
- const args = [];
549
- for (const token of tokens) {
550
- const resolved = await require_bindings.Container.resolve(token, ctx, Boolean(this.config.container?.autoDiscover));
551
- if (typeof resolved === "undefined") return handlerFunction(ctx, ctx.clearRequest);
552
- args.push(resolved);
553
- }
554
- return handlerFunction(...args);
615
+ return handlerFunction(...args);
616
+ });
555
617
  }
556
618
  static bindRequestToInstance(ctx, instance, route, payload) {
557
619
  const clearRequest = ctx.clearRequest instanceof require_bindings.Request ? ctx.clearRequest : new require_bindings.Request({