@zlink-systems/nestjs 0.30.0 → 0.30.2

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.
@@ -217,7 +217,7 @@ export interface ZLinkNestMeshObjectRoleBuilder extends ZLinkNestFrameworkOption
217
217
  export interface ZLinkNestMeshObjectClientBuilder extends ZLinkNestFrameworkOptionsBuilder {
218
218
  }
219
219
  export interface ZLinkNestMeshObjectServerBuilder extends ZLinkNestFrameworkOptionsBuilder {
220
- addEntrySpot<TEntrySpot extends ZLinkEntrySpot>(entrySpotType: Type<TEntrySpot>): this;
220
+ addEntrySpot<TEntrySpot extends ZLinkEntrySpot>(entrySpotType: Type<TEntrySpot>, actorType: string): this;
221
221
  addSpotFactory<TSpot extends ZLinkSpot>(spotType: string, implementation: Type<TSpot>, configure: (builder: ZLinkUserSpotFactoryBuilder<TSpot>) => void): this;
222
222
  addInstanceSpotFactory<TSpot extends ZLinkInstanceSpot>(instanceSpotType: string, implementation: Type<TSpot>, configure: (builder: ZLinkInstanceSpotFactoryBuilder<TSpot>) => void): this;
223
223
  addActorFactory<TActor extends ZLinkActor>(actorType: string, implementation: Type<ZLinkActorFactory<TActor>>, configure: (builder: ZLinkActorFactoryBuilder<TActor>) => void): this;
@@ -194,6 +194,7 @@ export interface ZLinkSpotNodeOptions {
194
194
  readonly router?: ZLinkSpotRouterCapabilityOptions;
195
195
  readonly pubSub?: ZLinkSpotPubSubCapabilityOptions;
196
196
  readonly entrySpotType?: Type<ZLinkEntrySpot>;
197
+ readonly entrySpotActorType?: string;
197
198
  readonly spotFactories?: readonly Type<ZLinkSpot>[];
198
199
  readonly spotFactoryRegistrations?: Readonly<Record<string, ZLinkObjectFactoryRegistration<ZLinkSpot, ZLinkSpot, ZLinkUserSpotFactoryConfiguration>>>;
199
200
  readonly instanceSpotFactories?: Readonly<Record<string, Type<ZLinkInstanceSpot>>>;
@@ -1,12 +1,21 @@
1
- import type { Type, ZLinkActorClient, ZLinkActorManager, ZLinkChannelClient, ZLinkCodecRegistrar, ZLinkCodecRegistryBuilder, ZLinkDispatchOptions, ZLinkDispatchOptionsBuilder, ZLinkInboundDispatchOptions, ZLinkLocationOptionValues, ZLinkLocationOptions, ZLinkEntrySpot, ZLinkFanoutClient, ZLinkHandlerFilterContext, ZLinkRouteClient, ZLinkSpot, ZLinkSpotManager, ZLinkSpotOutbound, ZLinkSpotPublisherClient, ZLinkStreamCompressionBuilder, ZLinkStreamCompressionCodec } from '@zlink-systems/framework';
1
+ import type { Type, ZLinkActorClient, ZLinkActorManager, ZLinkChannelClient, ZLinkCodecRegistrar, ZLinkCodecRegistryBuilder, ZLinkDispatchOptions, ZLinkDispatchOptionsBuilder, ZLinkInboundDispatchOptions, ZLinkLocationOptionValues, ZLinkLocationOptions, ZLinkMessageContext, ZLinkEntrySpot, ZLinkFanoutClient, ZLinkHandlerFilterContext, ZLinkRouteClient, ZLinkSpot, ZLinkSpotManager, ZLinkSpotOutbound, ZLinkSpotPublisherClient, ZLinkStreamCompressionBuilder, ZLinkStreamCompressionCodec } from '@zlink-systems/framework';
2
2
  import type { ZLinkHttpExecutionScheduler } from '@zlink-systems/http-client';
3
3
  import type { ZLinkCodecSerializerRegistration, ZLinkFrameworkRegistration, ZLinkFrameworkRegistrationOptions, ZLinkNestIntegrationRuntimeHost, ZLinkProviderResolver, ZLinkStreamCodecRegistration } from './framework-integration-contracts';
4
4
  export type FrameworkRuntimeHost = ZLinkNestIntegrationRuntimeHost;
5
+ interface HandlerResolver {
6
+ resolve<T>(type: Type<T>): Promise<T>;
7
+ }
5
8
  interface FrameworkIntegrationModule {
6
9
  ZLinkSpotActorSend(packetName?: string): MethodDecorator;
7
10
  ZLinkSpotActorRequest(packetName?: string): MethodDecorator;
8
11
  disposeIntegrationHandler(instance: unknown): Promise<void>;
9
12
  disposeIntegrationHandlers(instances: readonly unknown[]): Promise<void>;
13
+ registerIntegrationHandlerInstanceScopeFactory(resolver: ZLinkProviderResolver, factory: {
14
+ prepare?(type: Type): void;
15
+ create(context?: ZLinkMessageContext): HandlerResolver & {
16
+ dispose(): Promise<void>;
17
+ };
18
+ }): void;
10
19
  readonly MAX_LISTENER_PORT: number;
11
20
  readonly ZLINK_MAX_PUBLIC_WEIGHT: number;
12
21
  readonly ZLINK_MAX_CAPACITY: number;
@@ -50,7 +59,8 @@ interface FrameworkIntegrationModule {
50
59
  isValidCapacity(value: number): boolean;
51
60
  registerEntrySpot(options: {
52
61
  entrySpotType?: Type<ZLinkEntrySpot>;
53
- }, entrySpotType: Type<ZLinkEntrySpot>): void;
62
+ entrySpotActorType?: string;
63
+ }, entrySpotType: Type<ZLinkEntrySpot>, actorType: string): void;
54
64
  registerSpotFactory(options: {
55
65
  spotFactories?: Type<ZLinkSpot>[];
56
66
  }, spotType: Type<ZLinkSpot>): void;
@@ -60,9 +70,7 @@ interface FrameworkIntegrationModule {
60
70
  registerRelocationStore(options: {
61
71
  relocationStoreInstance?: import('@zlink-systems/framework').ZLinkRelocationStore;
62
72
  }, store: import('@zlink-systems/framework').ZLinkRelocationStore): void;
63
- registerIntegrationHandlerFilterScope(resolver: ZLinkProviderResolver, runner: (context: ZLinkHandlerFilterContext, callback: (scope: {
64
- resolve<T>(type: Type<T>): Promise<T>;
65
- }) => Promise<unknown>) => Promise<unknown>): void;
73
+ registerIntegrationHandlerFilterScope(resolver: ZLinkProviderResolver, runner: (context: ZLinkHandlerFilterContext, callback: (scope: HandlerResolver) => Promise<unknown>) => Promise<unknown>): void;
66
74
  }
67
75
  export declare const framework: FrameworkIntegrationModule;
68
76
  export {};
@@ -2,10 +2,11 @@ import { ModuleRef } from '@nestjs/core';
2
2
  import type { ZLinkChannelOptions } from './framework-integration-contracts';
3
3
  import type { ZLinkNestManualHandlerOptions } from './contracts';
4
4
  import type { DiscoveredNestProvider } from './provider-discovery';
5
- export declare function createDiscoveredRequestHandlers(providerRefs: readonly DiscoveredNestProvider[], handlerGroups: readonly string[] | undefined, moduleRef: ModuleRef): NonNullable<ZLinkChannelOptions['requestHandlers']>;
6
- export declare function createDiscoveredSendHandlers(providerRefs: readonly DiscoveredNestProvider[], handlerGroups: readonly string[] | undefined, moduleRef: ModuleRef): NonNullable<NonNullable<ZLinkChannelOptions['routeMesh']>['sendHandlers']>;
7
- export declare function createDiscoveredChannelSendHandlers(providerRefs: readonly DiscoveredNestProvider[], handlerGroups: readonly string[] | undefined, moduleRef: ModuleRef): NonNullable<ZLinkChannelOptions['sendHandlers']>;
8
- export declare function createDiscoveredPublishHandlers(providerRefs: readonly DiscoveredNestProvider[], handlerGroups: readonly string[] | undefined, moduleRef: ModuleRef): NonNullable<ZLinkChannelOptions['publishHandlers']>;
9
- export declare function createManualRequestHandlers(handlerTypes: readonly ZLinkNestManualHandlerOptions[] | undefined, moduleRef: ModuleRef): NonNullable<ZLinkChannelOptions['requestHandlers']>;
10
- export declare function createManualPublishHandlers(handlerTypes: readonly ZLinkNestManualHandlerOptions[] | undefined, moduleRef: ModuleRef): NonNullable<ZLinkChannelOptions['publishHandlers']>;
11
- export declare function createManualSendHandlers(handlerTypes: readonly ZLinkNestManualHandlerOptions[] | undefined, moduleRef: ModuleRef): NonNullable<ZLinkChannelOptions['sendHandlers']>;
5
+ import { type NestHandlerFactories } from './providers';
6
+ export declare function createDiscoveredRequestHandlers(providerRefs: readonly DiscoveredNestProvider[], handlerGroups: readonly string[] | undefined, moduleRef: ModuleRef, factories?: NestHandlerFactories): NonNullable<ZLinkChannelOptions['requestHandlers']>;
7
+ export declare function createDiscoveredSendHandlers(providerRefs: readonly DiscoveredNestProvider[], handlerGroups: readonly string[] | undefined, moduleRef: ModuleRef, factories?: NestHandlerFactories): NonNullable<NonNullable<ZLinkChannelOptions['routeMesh']>['sendHandlers']>;
8
+ export declare function createDiscoveredChannelSendHandlers(providerRefs: readonly DiscoveredNestProvider[], handlerGroups: readonly string[] | undefined, moduleRef: ModuleRef, factories?: NestHandlerFactories): NonNullable<ZLinkChannelOptions['sendHandlers']>;
9
+ export declare function createDiscoveredPublishHandlers(providerRefs: readonly DiscoveredNestProvider[], handlerGroups: readonly string[] | undefined, moduleRef: ModuleRef, factories?: NestHandlerFactories): NonNullable<ZLinkChannelOptions['publishHandlers']>;
10
+ export declare function createManualRequestHandlers(handlerTypes: readonly ZLinkNestManualHandlerOptions[] | undefined, moduleRef: ModuleRef, factories?: NestHandlerFactories): NonNullable<ZLinkChannelOptions['requestHandlers']>;
11
+ export declare function createManualPublishHandlers(handlerTypes: readonly ZLinkNestManualHandlerOptions[] | undefined, moduleRef: ModuleRef, factories?: NestHandlerFactories): NonNullable<ZLinkChannelOptions['publishHandlers']>;
12
+ export declare function createManualSendHandlers(handlerTypes: readonly ZLinkNestManualHandlerOptions[] | undefined, moduleRef: ModuleRef, factories?: NestHandlerFactories): NonNullable<ZLinkChannelOptions['sendHandlers']>;
@@ -11,66 +11,65 @@ const core_1 = require("@nestjs/core");
11
11
  const framework_loader_1 = require("./framework-loader");
12
12
  const dispatch_scope_1 = require("./dispatch-scope");
13
13
  const providers_1 = require("./providers");
14
- function createDiscoveredRequestHandlers(providerRefs, handlerGroups, moduleRef) {
15
- return createDiscoveredHandlerRegistrations(providerRefs, handlerGroups, 'request', moduleRef, (ref, metadata) => ({
14
+ function createDiscoveredRequestHandlers(providerRefs, handlerGroups, moduleRef, factories = new Map()) {
15
+ return createDiscoveredHandlerRegistrations(providerRefs, handlerGroups, 'request', moduleRef, factories, (ref, metadata, factory) => ({
16
16
  async handle(payload, context) {
17
- const result = await invokeDiscoveredHandler(moduleRef, ref, metadata, payload, context);
17
+ const result = await invokeDiscoveredHandler(moduleRef, ref, metadata, factory, payload, context);
18
18
  return metadata.encodeResult === undefined
19
19
  ? result
20
20
  : metadata.encodeResult(result, context);
21
21
  }
22
22
  }));
23
23
  }
24
- function createDiscoveredSendHandlers(providerRefs, handlerGroups, moduleRef) {
25
- return createDiscoveredHandlerRegistrations(providerRefs, handlerGroups, 'send', moduleRef, (ref, metadata) => ({
24
+ function createDiscoveredSendHandlers(providerRefs, handlerGroups, moduleRef, factories = new Map()) {
25
+ return createDiscoveredHandlerRegistrations(providerRefs, handlerGroups, 'send', moduleRef, factories, (ref, metadata, factory) => ({
26
26
  async handle(payload, context) {
27
- await invokeDiscoveredHandler(moduleRef, ref, metadata, payload, context);
27
+ await invokeDiscoveredHandler(moduleRef, ref, metadata, factory, payload, context);
28
28
  }
29
29
  }));
30
30
  }
31
- function createDiscoveredChannelSendHandlers(providerRefs, handlerGroups, moduleRef) {
32
- return createDiscoveredHandlerRegistrations(providerRefs, handlerGroups, 'send', moduleRef, (ref, metadata) => ({
31
+ function createDiscoveredChannelSendHandlers(providerRefs, handlerGroups, moduleRef, factories = new Map()) {
32
+ return createDiscoveredHandlerRegistrations(providerRefs, handlerGroups, 'send', moduleRef, factories, (ref, metadata, factory) => ({
33
33
  async handle(payload, context) {
34
- await invokeDiscoveredHandler(moduleRef, ref, metadata, payload, context);
34
+ await invokeDiscoveredHandler(moduleRef, ref, metadata, factory, payload, context);
35
35
  }
36
36
  }));
37
37
  }
38
- function createDiscoveredPublishHandlers(providerRefs, handlerGroups, moduleRef) {
39
- return createDiscoveredHandlerRegistrations(providerRefs, handlerGroups, 'publish', moduleRef, (ref, metadata) => ({
38
+ function createDiscoveredPublishHandlers(providerRefs, handlerGroups, moduleRef, factories = new Map()) {
39
+ return createDiscoveredHandlerRegistrations(providerRefs, handlerGroups, 'publish', moduleRef, factories, (ref, metadata, factory) => ({
40
40
  async handle(payload, context) {
41
- await invokeDiscoveredHandler(moduleRef, ref, metadata, payload, context);
41
+ await invokeDiscoveredHandler(moduleRef, ref, metadata, factory, payload, context);
42
42
  }
43
43
  }));
44
44
  }
45
- function createManualRequestHandlers(handlerTypes, moduleRef) {
46
- return createManualHandlerRegistrations(handlerTypes, moduleRef, (result) => result);
45
+ function createManualRequestHandlers(handlerTypes, moduleRef, factories = new Map()) {
46
+ return createManualHandlerRegistrations(handlerTypes, moduleRef, factories, (result) => result);
47
47
  }
48
- function createManualPublishHandlers(handlerTypes, moduleRef) {
49
- return createManualHandlerRegistrations(handlerTypes, moduleRef, () => undefined);
48
+ function createManualPublishHandlers(handlerTypes, moduleRef, factories = new Map()) {
49
+ return createManualHandlerRegistrations(handlerTypes, moduleRef, factories, () => undefined);
50
50
  }
51
- function createManualSendHandlers(handlerTypes, moduleRef) {
52
- return createManualHandlerRegistrations(handlerTypes, moduleRef, () => undefined);
51
+ function createManualSendHandlers(handlerTypes, moduleRef, factories = new Map()) {
52
+ return createManualHandlerRegistrations(handlerTypes, moduleRef, factories, () => undefined);
53
53
  }
54
- function createManualHandlerRegistrations(handlerTypes, moduleRef, result) {
54
+ function createManualHandlerRegistrations(handlerTypes, moduleRef, factories, result) {
55
55
  return (handlerTypes ?? []).map((registration) => {
56
- (0, providers_1.validateNestConstructorDependencies)(moduleRef, registration.handlerType);
56
+ const factory = (0, providers_1.registeredNestHandlerFactory)(factories, registration.handlerType, moduleRef);
57
57
  return {
58
58
  packetName: registration.packetName,
59
59
  handler: {
60
60
  async handle(payload, context) {
61
- return result(await invokeManualHandler(moduleRef, registration.handlerType, payload, context));
61
+ return result(await invokeManualHandler(moduleRef, registration.handlerType, factory, payload, context));
62
62
  }
63
63
  }
64
64
  };
65
65
  });
66
66
  }
67
- function createDiscoveredHandlerRegistrations(providerRefs, handlerGroups, kind, moduleRef, createHandler) {
67
+ function createDiscoveredHandlerRegistrations(providerRefs, handlerGroups, kind, moduleRef, factories, createHandler) {
68
68
  const descriptors = createDiscoveredHandlerDescriptors(providerRefs, handlerGroups, kind);
69
69
  return descriptors.map(({ ref, metadata }) => {
70
- (0, providers_1.validateNestConstructorDependencies)(moduleRef, ref.handlerKey);
71
70
  return {
72
71
  packetName: metadata.packetName,
73
- handler: createHandler(ref, metadata)
72
+ handler: createHandler(ref, metadata, (0, providers_1.registeredNestHandlerFactory)(factories, ref.handlerKey, moduleRef))
74
73
  };
75
74
  });
76
75
  }
@@ -99,8 +98,8 @@ function createDiscoveredHandlerDescriptors(providerRefs, handlerGroups, kind) {
99
98
  }
100
99
  return selected;
101
100
  }
102
- async function invokeDiscoveredHandler(moduleRef, ref, metadata, payload, context) {
103
- const instance = await resolveHandlerInstance(moduleRef, ref.handlerKey, context);
101
+ async function invokeDiscoveredHandler(moduleRef, ref, metadata, factory, payload, context) {
102
+ const instance = await resolveHandlerInstance(moduleRef, factory, context);
104
103
  try {
105
104
  const method = instance[metadata.methodName];
106
105
  if (typeof method !== 'function') {
@@ -112,8 +111,8 @@ async function invokeDiscoveredHandler(moduleRef, ref, metadata, payload, contex
112
111
  await framework_loader_1.framework.disposeIntegrationHandler(instance);
113
112
  }
114
113
  }
115
- async function invokeManualHandler(moduleRef, handlerType, payload, context) {
116
- const instance = await resolveHandlerInstance(moduleRef, handlerType, context);
114
+ async function invokeManualHandler(moduleRef, handlerType, factory, payload, context) {
115
+ const instance = await resolveHandlerInstance(moduleRef, factory, context);
117
116
  try {
118
117
  const method = instance.handle;
119
118
  if (typeof method !== 'function') {
@@ -125,13 +124,13 @@ async function invokeManualHandler(moduleRef, handlerType, payload, context) {
125
124
  await framework_loader_1.framework.disposeIntegrationHandler(instance);
126
125
  }
127
126
  }
128
- async function resolveHandlerInstance(moduleRef, handlerType, context) {
127
+ async function resolveHandlerInstance(moduleRef, factory, context) {
129
128
  const currentContextId = (0, dispatch_scope_1.currentNestDispatchContext)();
130
129
  const contextId = currentContextId ?? core_1.ContextIdFactory.create();
131
130
  if (currentContextId === undefined) {
132
131
  moduleRef.registerRequestByContextId({ zlinkContext: context }, contextId);
133
132
  }
134
- return (await (0, providers_1.createNestHandlerInstance)(moduleRef, contextId, new Map(), handlerType));
133
+ return (await factory(moduleRef, contextId, new Map()));
135
134
  }
136
135
  function decodePayload(metadata, payload, context) {
137
136
  if (Buffer.isBuffer(payload) || payload instanceof Uint8Array) {
package/dist/module.js CHANGED
@@ -107,7 +107,7 @@ let ZLinkModule = ZLinkModule_1 = class ZLinkModule {
107
107
  const moduleRef = args[args.length - 1];
108
108
  const factoryArgs = args.slice(0, -2);
109
109
  const resolvedOptions = (0, registration_composer_1.assertBuiltModuleOptions)(await options.useFactory(...factoryArgs));
110
- return framework_loader_1.framework.createFrameworkRegistration((0, registration_composer_1.createDiscoveredOptions)(resolvedOptions, discovery, moduleRef));
110
+ return (0, registration_composer_1.createDiscoveredRegistration)(resolvedOptions, discovery, moduleRef);
111
111
  }
112
112
  };
113
113
  return {
@@ -158,7 +158,7 @@ function createDiscoveringZLinkDynamicModule(options) {
158
158
  const registrationProvider = {
159
159
  provide: tokens_1.ZLINK_FRAMEWORK_REGISTRATION,
160
160
  inject: [core_1.DiscoveryService, core_1.ModuleRef],
161
- useFactory: (discovery, moduleRef) => framework_loader_1.framework.createFrameworkRegistration((0, registration_composer_1.createDiscoveredOptions)(options, discovery, moduleRef))
161
+ useFactory: (discovery, moduleRef) => (0, registration_composer_1.createDiscoveredRegistration)(options, discovery, moduleRef)
162
162
  };
163
163
  return {
164
164
  module: ZLinkModule,
@@ -757,8 +757,8 @@ class DefaultZLinkNestMeshObjectServerBuilder extends ZLinkNestOptionsBuilder {
757
757
  this.meshName = meshName;
758
758
  this.node = node;
759
759
  }
760
- addEntrySpot(entrySpotType) {
761
- framework_loader_1.framework.registerEntrySpot(this.node, entrySpotType);
760
+ addEntrySpot(entrySpotType, actorType) {
761
+ framework_loader_1.framework.registerEntrySpot(this.node, entrySpotType, actorType);
762
762
  return this;
763
763
  }
764
764
  addSpotFactory(spotType, implementation, configure) {
@@ -10,7 +10,12 @@ export declare function conditionalClientProviders(registration: ZLinkFrameworkR
10
10
  export declare function conditionalClientProvidersForFactory(registration?: ZLinkFrameworkRegistration): Provider[];
11
11
  export declare function conditionalClientTokens(): InjectionToken[];
12
12
  export declare function createRuntimeHost(registration: ZLinkFrameworkRegistration, moduleRef: ModuleRef, discovery: DiscoveryService): RuntimeHostWithNestLifecycle;
13
- export declare function createNestHandlerInstance<T>(moduleRef: ModuleRef, contextId: import('@nestjs/core').ContextId, dependencies: Map<unknown, Promise<unknown>>, type: Type<T>): Promise<T>;
14
- export declare function validateNestConstructorDependencies(moduleRef: ModuleRef, type: Type): void;
13
+ export type NestHandlerFactories = Map<Type, NestHandlerFactory>;
14
+ export declare function nestHandlerFactories(registration: ZLinkFrameworkRegistration, prepared?: NestHandlerFactories): NestHandlerFactories;
15
+ export declare function registeredNestHandlerFactory<T>(factories: NestHandlerFactories, type: Type<T>, moduleRef: ModuleRef): NestHandlerFactory<T>;
16
+ export type NestHandlerFactory<T = unknown> = ((moduleRef: ModuleRef, contextId: import('@nestjs/core').ContextId, dependencies: Map<unknown, unknown>) => T | Promise<T>) & {
17
+ readonly scopeFree?: () => T;
18
+ };
19
+ export declare function prepareNestHandlerFactory<T>(type: Type<T>, moduleRef?: ModuleRef): NestHandlerFactory<T>;
15
20
  export declare function providerToken(provider: Provider): InjectionToken;
16
21
  export {};