@zlink-systems/nestjs 0.30.0 → 0.30.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/LICENSE +150 -68
- package/dist/.tsbuildinfo +1 -1
- package/dist/framework-loader.d.ts +11 -4
- package/dist/handler-adapters.d.ts +8 -7
- package/dist/handler-adapters.js +29 -30
- package/dist/module.js +2 -2
- package/dist/providers.d.ts +7 -2
- package/dist/providers.js +194 -98
- package/dist/registration-composer.d.ts +3 -1
- package/dist/registration-composer.js +21 -13
- package/package.json +3 -3
|
@@ -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;
|
|
@@ -60,9 +69,7 @@ interface FrameworkIntegrationModule {
|
|
|
60
69
|
registerRelocationStore(options: {
|
|
61
70
|
relocationStoreInstance?: import('@zlink-systems/framework').ZLinkRelocationStore;
|
|
62
71
|
}, 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;
|
|
72
|
+
registerIntegrationHandlerFilterScope(resolver: ZLinkProviderResolver, runner: (context: ZLinkHandlerFilterContext, callback: (scope: HandlerResolver) => Promise<unknown>) => Promise<unknown>): void;
|
|
66
73
|
}
|
|
67
74
|
export declare const framework: FrameworkIntegrationModule;
|
|
68
75
|
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
|
-
|
|
6
|
-
export declare function
|
|
7
|
-
export declare function
|
|
8
|
-
export declare function
|
|
9
|
-
export declare function
|
|
10
|
-
export declare function
|
|
11
|
-
export declare function
|
|
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']>;
|
package/dist/handler-adapters.js
CHANGED
|
@@ -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.
|
|
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,
|
|
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,
|
|
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,
|
|
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 (
|
|
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
|
|
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) =>
|
|
161
|
+
useFactory: (discovery, moduleRef) => (0, registration_composer_1.createDiscoveredRegistration)(options, discovery, moduleRef)
|
|
162
162
|
};
|
|
163
163
|
return {
|
|
164
164
|
module: ZLinkModule,
|
package/dist/providers.d.ts
CHANGED
|
@@ -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
|
|
14
|
-
export declare function
|
|
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 {};
|
package/dist/providers.js
CHANGED
|
@@ -6,9 +6,11 @@ exports.conditionalClientProviders = conditionalClientProviders;
|
|
|
6
6
|
exports.conditionalClientProvidersForFactory = conditionalClientProvidersForFactory;
|
|
7
7
|
exports.conditionalClientTokens = conditionalClientTokens;
|
|
8
8
|
exports.createRuntimeHost = createRuntimeHost;
|
|
9
|
-
exports.
|
|
10
|
-
exports.
|
|
9
|
+
exports.nestHandlerFactories = nestHandlerFactories;
|
|
10
|
+
exports.registeredNestHandlerFactory = registeredNestHandlerFactory;
|
|
11
|
+
exports.prepareNestHandlerFactory = prepareNestHandlerFactory;
|
|
11
12
|
exports.providerToken = providerToken;
|
|
13
|
+
const common_1 = require("@nestjs/common");
|
|
12
14
|
const constants_1 = require("@nestjs/common/constants");
|
|
13
15
|
const core_1 = require("@nestjs/core");
|
|
14
16
|
const unknown_element_exception_1 = require("@nestjs/core/errors/exceptions/unknown-element.exception");
|
|
@@ -126,7 +128,9 @@ const CONDITIONAL_CLIENT_PROVIDER_SPECS = [
|
|
|
126
128
|
isEnabled: (registration) => framework_loader_1.framework.hasActorManager(registration),
|
|
127
129
|
create: async (registration, runtime, moduleRef, discovery) => {
|
|
128
130
|
const host = requireRuntime(runtime);
|
|
129
|
-
return framework_loader_1.framework.createIntegrationActorManager(registration, host, moduleRef === undefined
|
|
131
|
+
return framework_loader_1.framework.createIntegrationActorManager(registration, host, moduleRef === undefined
|
|
132
|
+
? undefined
|
|
133
|
+
: createProviderResolver(moduleRef, discovery, registration));
|
|
130
134
|
}
|
|
131
135
|
},
|
|
132
136
|
{
|
|
@@ -225,6 +229,18 @@ function hasLocationStores(registration) {
|
|
|
225
229
|
return (registration.locations.useInMemoryStores || registration.locations.storeInstance !== undefined);
|
|
226
230
|
}
|
|
227
231
|
function createRuntimeHost(registration, moduleRef, discovery) {
|
|
232
|
+
const runtime = framework_loader_1.framework.createIntegrationRuntimeHost(registration, createProviderResolver(moduleRef, discovery, registration));
|
|
233
|
+
runtime.onModuleInit = async () => {
|
|
234
|
+
await runtime.start();
|
|
235
|
+
};
|
|
236
|
+
runtime.onModuleDestroy = async () => {
|
|
237
|
+
await runtime.shutdown();
|
|
238
|
+
};
|
|
239
|
+
runtime.onApplicationBootstrap = async () => { };
|
|
240
|
+
return runtime;
|
|
241
|
+
}
|
|
242
|
+
function createProviderResolver(moduleRef, discovery, registration) {
|
|
243
|
+
const factories = nestHandlerFactories(registration);
|
|
228
244
|
const types = new Set([...registration.spotFactories, ...registration.filterTypes]);
|
|
229
245
|
for (const node of registration.spotNodes.values()) {
|
|
230
246
|
if (node.entrySpotType !== undefined)
|
|
@@ -238,6 +254,8 @@ function createRuntimeHost(registration, moduleRef, discovery) {
|
|
|
238
254
|
for (const value of Object.values(node.instanceSpotFactoryRegistrations ?? {}))
|
|
239
255
|
types.add(value.implementation);
|
|
240
256
|
for (const handler of [
|
|
257
|
+
...(node.routeSendHandlers ?? []),
|
|
258
|
+
...(node.routeRequestHandlers ?? []),
|
|
241
259
|
...(node.entrySpotTimerHandlers ?? []),
|
|
242
260
|
...(node.entrySpotPacketHandlers ?? []),
|
|
243
261
|
...(node.entrySpotSubscriptionHandlers ?? []),
|
|
@@ -248,27 +266,34 @@ function createRuntimeHost(registration, moduleRef, discovery) {
|
|
|
248
266
|
...(node.spotSubscriptionHandlers ?? []),
|
|
249
267
|
...(node.spotActorSendHandlers ?? []),
|
|
250
268
|
...(node.spotActorRequestHandlers ?? [])
|
|
251
|
-
])
|
|
252
|
-
|
|
269
|
+
]) {
|
|
270
|
+
const handlerType = handler.handlerType;
|
|
271
|
+
if (handlerType !== undefined)
|
|
272
|
+
types.add(handlerType);
|
|
273
|
+
}
|
|
274
|
+
for (const channel of Object.values(node.meshChannels ?? {})) {
|
|
275
|
+
for (const handler of [...(channel.sendHandlers ?? []), ...(channel.requestHandlers ?? [])]) {
|
|
276
|
+
const handlerType = handler.handlerType;
|
|
277
|
+
if (handlerType !== undefined)
|
|
278
|
+
types.add(handlerType);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
function prepareHandler(type) {
|
|
283
|
+
return registeredNestHandlerFactory(factories, type, moduleRef).scopeFree;
|
|
253
284
|
}
|
|
254
285
|
for (const type of types)
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
};
|
|
263
|
-
runtime.onApplicationBootstrap = async () => { };
|
|
264
|
-
return runtime;
|
|
265
|
-
}
|
|
266
|
-
function createProviderResolver(moduleRef, discovery) {
|
|
286
|
+
prepareHandler(type);
|
|
287
|
+
function createHandler(contextId, dependencies, type) {
|
|
288
|
+
const factory = factories.get(type);
|
|
289
|
+
if (factory === undefined)
|
|
290
|
+
throw new Error(`Handler creation was not prepared: ${type.name}`);
|
|
291
|
+
return factory(moduleRef, contextId, dependencies);
|
|
292
|
+
}
|
|
267
293
|
const resolver = {
|
|
268
294
|
get(type) {
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
return discovered;
|
|
295
|
+
if (discovery !== undefined) {
|
|
296
|
+
return findDiscoveredProvider(discovery, type)?.instance;
|
|
272
297
|
}
|
|
273
298
|
try {
|
|
274
299
|
return moduleRef.get(type, { strict: false });
|
|
@@ -283,56 +308,68 @@ function createProviderResolver(moduleRef, discovery) {
|
|
|
283
308
|
return await creationModuleRef.create(type);
|
|
284
309
|
}
|
|
285
310
|
};
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
311
|
+
let freeScopeStorage;
|
|
312
|
+
framework_loader_1.framework.registerIntegrationHandlerInstanceScopeFactory(resolver, {
|
|
313
|
+
prepare: prepareHandler,
|
|
314
|
+
create(context) {
|
|
315
|
+
const currentContextId = (0, dispatch_scope_1.currentNestDispatchContext)();
|
|
316
|
+
const contextId = currentContextId ?? core_1.ContextIdFactory.create();
|
|
317
|
+
if (context !== undefined && currentContextId === undefined) {
|
|
318
|
+
moduleRef.registerRequestByContextId({ zlinkContext: context }, contextId);
|
|
319
|
+
}
|
|
320
|
+
const storage = freeScopeStorage ?? new NestHandlerScopeStorage();
|
|
321
|
+
freeScopeStorage = storage.next;
|
|
322
|
+
storage.next = undefined;
|
|
323
|
+
const { instances, dependencies, owned } = storage;
|
|
324
|
+
let disposed = false;
|
|
325
|
+
return {
|
|
326
|
+
resolve(type) {
|
|
327
|
+
if (disposed) {
|
|
328
|
+
return Promise.reject(new Error('Handler instance scope is already disposed.'));
|
|
329
|
+
}
|
|
330
|
+
let instance = instances.get(type);
|
|
331
|
+
if (instance === undefined) {
|
|
332
|
+
try {
|
|
333
|
+
const finish = (created) => {
|
|
306
334
|
if (disposed) {
|
|
307
|
-
|
|
308
|
-
|
|
335
|
+
return framework_loader_1.framework.disposeIntegrationHandler(created).then(() => {
|
|
336
|
+
throw new Error('Handler instance scope was disposed during activation.');
|
|
337
|
+
});
|
|
309
338
|
}
|
|
310
339
|
owned.push(created);
|
|
311
340
|
return created;
|
|
312
|
-
}
|
|
341
|
+
};
|
|
342
|
+
const created = createHandler(contextId, dependencies, type);
|
|
343
|
+
instance = isNestPromiseLike(created)
|
|
344
|
+
? Promise.resolve(created).then(finish)
|
|
345
|
+
: Promise.resolve(finish(created));
|
|
313
346
|
instances.set(type, instance);
|
|
314
347
|
}
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
async dispose() {
|
|
318
|
-
if (disposed)
|
|
319
|
-
return;
|
|
320
|
-
disposed = true;
|
|
321
|
-
const pending = [...instances.values()];
|
|
322
|
-
await Promise.allSettled(pending);
|
|
323
|
-
try {
|
|
324
|
-
await framework_loader_1.framework.disposeIntegrationHandlers(owned);
|
|
325
|
-
}
|
|
326
|
-
finally {
|
|
327
|
-
owned.length = 0;
|
|
328
|
-
instances.clear();
|
|
329
|
-
dependencies.clear();
|
|
348
|
+
catch (error) {
|
|
349
|
+
return Promise.reject(error);
|
|
330
350
|
}
|
|
331
351
|
}
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
352
|
+
return instance;
|
|
353
|
+
},
|
|
354
|
+
async dispose() {
|
|
355
|
+
if (disposed)
|
|
356
|
+
return;
|
|
357
|
+
disposed = true;
|
|
358
|
+
if (owned.length !== instances.size)
|
|
359
|
+
await Promise.allSettled(instances.values());
|
|
360
|
+
try {
|
|
361
|
+
await framework_loader_1.framework.disposeIntegrationHandlers(owned);
|
|
362
|
+
}
|
|
363
|
+
finally {
|
|
364
|
+
owned.length = 0;
|
|
365
|
+
instances.clear();
|
|
366
|
+
dependencies.clear();
|
|
367
|
+
storage.next = freeScopeStorage;
|
|
368
|
+
freeScopeStorage = storage;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
}
|
|
336
373
|
});
|
|
337
374
|
framework_loader_1.framework.registerIntegrationHandlerFilterScope(resolver, (context, callback) => (0, dispatch_scope_1.runInNestDispatchScope)(moduleRef, context, async () => {
|
|
338
375
|
const contextId = (0, dispatch_scope_1.currentNestDispatchContext)();
|
|
@@ -341,7 +378,7 @@ function createProviderResolver(moduleRef, discovery) {
|
|
|
341
378
|
try {
|
|
342
379
|
return await callback({
|
|
343
380
|
async resolve(type) {
|
|
344
|
-
const instance = await
|
|
381
|
+
const instance = await createHandler(contextId, dependencies, type);
|
|
345
382
|
owned.push(instance);
|
|
346
383
|
return instance;
|
|
347
384
|
}
|
|
@@ -353,20 +390,86 @@ function createProviderResolver(moduleRef, discovery) {
|
|
|
353
390
|
}));
|
|
354
391
|
return resolver;
|
|
355
392
|
}
|
|
356
|
-
|
|
393
|
+
const NEST_HANDLER_FACTORIES = Symbol('Nest handler factories');
|
|
394
|
+
function nestHandlerFactories(registration, prepared) {
|
|
395
|
+
const owner = registration;
|
|
396
|
+
return (owner[NEST_HANDLER_FACTORIES] ??= prepared ?? new Map());
|
|
397
|
+
}
|
|
398
|
+
function registeredNestHandlerFactory(factories, type, moduleRef) {
|
|
399
|
+
let factory = factories.get(type);
|
|
400
|
+
if (factory === undefined) {
|
|
401
|
+
factory = prepareNestHandlerFactory(type, moduleRef);
|
|
402
|
+
factories.set(type, factory);
|
|
403
|
+
}
|
|
404
|
+
return factory;
|
|
405
|
+
}
|
|
406
|
+
class NestHandlerScopeStorage {
|
|
407
|
+
instances = new Map();
|
|
408
|
+
dependencies = new Map();
|
|
409
|
+
owned = [];
|
|
410
|
+
next;
|
|
411
|
+
}
|
|
412
|
+
function prepareNestHandlerFactory(type, moduleRef) {
|
|
357
413
|
const { reflected, optional } = nestConstructorDependencies(type);
|
|
358
|
-
const parameters =
|
|
359
|
-
const instance = new type(...parameters);
|
|
360
|
-
const properties = Reflect.getMetadata(constants_1.PROPERTY_DEPS_METADATA, type);
|
|
414
|
+
const parameters = reflected.map((token, index) => prepareNestDependency(nestDependencyToken(token), optional.has(index), moduleRef));
|
|
361
415
|
const optionalProperties = new Set(Reflect.getMetadata(constants_1.OPTIONAL_PROPERTY_DEPS_METADATA, type) ?? []);
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
416
|
+
const properties = (Reflect.getMetadata(constants_1.PROPERTY_DEPS_METADATA, type) ?? []).map((property) => ({
|
|
417
|
+
key: property.key,
|
|
418
|
+
resolve: prepareNestDependency(nestDependencyToken(property.type), optionalProperties.has(property.key), moduleRef)
|
|
419
|
+
}));
|
|
420
|
+
const Constructor = type;
|
|
421
|
+
if (parameters.length === 0 && properties.length === 0) {
|
|
422
|
+
const activate = () => new Constructor();
|
|
423
|
+
return Object.assign(activate, { scopeFree: activate });
|
|
424
|
+
}
|
|
425
|
+
return (moduleRef, contextId, dependencies) => {
|
|
426
|
+
const create = (arguments_) => injectNestHandlerProperties(new Constructor(...arguments_), properties, 0, moduleRef, contextId, dependencies);
|
|
427
|
+
const arguments_ = parameters.map((resolve) => resolve(moduleRef, contextId, dependencies));
|
|
428
|
+
return arguments_.some(isNestPromiseLike)
|
|
429
|
+
? Promise.all(arguments_).then(create)
|
|
430
|
+
: create(arguments_);
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
function prepareNestDependency(token, optional, moduleRef) {
|
|
434
|
+
let lookup = (ref, contextId) => ref.resolve(token, contextId, { strict: false });
|
|
435
|
+
if (moduleRef !== undefined) {
|
|
436
|
+
try {
|
|
437
|
+
if (moduleRef.introspect(token).scope ===
|
|
438
|
+
common_1.Scope.DEFAULT) {
|
|
439
|
+
lookup = (ref) => ref.get(token, { strict: false });
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
catch (error) {
|
|
443
|
+
if (!(error instanceof unknown_element_exception_1.UnknownElementException) || !optional)
|
|
444
|
+
throw error;
|
|
366
445
|
}
|
|
367
446
|
}
|
|
447
|
+
return (ref, contextId, dependencies) => resolveNestHandlerDependency(dependencies, token, optional, lookup, ref, contextId);
|
|
448
|
+
}
|
|
449
|
+
function injectNestHandlerProperties(instance, properties, index, moduleRef, contextId, dependencies) {
|
|
450
|
+
for (; index < properties.length; index++) {
|
|
451
|
+
const property = properties[index];
|
|
452
|
+
const dependency = property.resolve(moduleRef, contextId, dependencies);
|
|
453
|
+
if (isNestPromiseLike(dependency)) {
|
|
454
|
+
const next = index + 1;
|
|
455
|
+
return Promise.resolve(dependency).then((value) => {
|
|
456
|
+
assignNestHandlerProperty(instance, property.key, value);
|
|
457
|
+
return injectNestHandlerProperties(instance, properties, next, moduleRef, contextId, dependencies);
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
assignNestHandlerProperty(instance, property.key, dependency);
|
|
461
|
+
}
|
|
368
462
|
return instance;
|
|
369
463
|
}
|
|
464
|
+
function assignNestHandlerProperty(instance, key, dependency) {
|
|
465
|
+
if (dependency !== undefined)
|
|
466
|
+
instance[key] = dependency;
|
|
467
|
+
}
|
|
468
|
+
function isNestPromiseLike(value) {
|
|
469
|
+
return (value !== null &&
|
|
470
|
+
(typeof value === 'object' || typeof value === 'function') &&
|
|
471
|
+
typeof value.then === 'function');
|
|
472
|
+
}
|
|
370
473
|
function nestConstructorDependencies(type) {
|
|
371
474
|
const reflected = [
|
|
372
475
|
...(Reflect.getMetadata(constants_1.PARAMTYPES_METADATA, type) ??
|
|
@@ -379,18 +482,6 @@ function nestConstructorDependencies(type) {
|
|
|
379
482
|
const optional = new Set(Reflect.getMetadata(constants_1.OPTIONAL_DEPS_METADATA, type) ?? []);
|
|
380
483
|
return { reflected, optional };
|
|
381
484
|
}
|
|
382
|
-
function validateNestConstructorDependencies(moduleRef, type) {
|
|
383
|
-
const { reflected, optional } = nestConstructorDependencies(type);
|
|
384
|
-
for (const [index, token] of reflected.entries()) {
|
|
385
|
-
try {
|
|
386
|
-
moduleRef.introspect(nestDependencyToken(token));
|
|
387
|
-
}
|
|
388
|
-
catch (error) {
|
|
389
|
-
if (!(error instanceof unknown_element_exception_1.UnknownElementException) || !optional.has(index))
|
|
390
|
-
throw error;
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
485
|
function nestDependencyToken(token) {
|
|
395
486
|
const forwardReference = typeof token === 'object' && token !== null && 'forwardRef' in token
|
|
396
487
|
? token
|
|
@@ -399,25 +490,30 @@ function nestDependencyToken(token) {
|
|
|
399
490
|
? forwardReference.forwardRef()
|
|
400
491
|
: token;
|
|
401
492
|
}
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
if (dependency === undefined) {
|
|
406
|
-
dependency = moduleRef.resolve(resolvedToken, contextId, {
|
|
407
|
-
strict: false
|
|
408
|
-
});
|
|
409
|
-
dependencies.set(resolvedToken, dependency);
|
|
410
|
-
}
|
|
493
|
+
function resolveNestHandlerDependency(dependencies, token, optional, lookup, moduleRef, contextId) {
|
|
494
|
+
if (dependencies.has(token))
|
|
495
|
+
return dependencies.get(token);
|
|
411
496
|
try {
|
|
412
|
-
|
|
497
|
+
const dependency = lookup(moduleRef, contextId);
|
|
498
|
+
const result = isNestPromiseLike(dependency)
|
|
499
|
+
? Promise.resolve(dependency).then((value) => {
|
|
500
|
+
dependencies.set(token, value);
|
|
501
|
+
return value;
|
|
502
|
+
}, (error) => nestDependencyFailure(dependencies, token, optional, error))
|
|
503
|
+
: dependency;
|
|
504
|
+
dependencies.set(token, result);
|
|
505
|
+
return result;
|
|
413
506
|
}
|
|
414
507
|
catch (error) {
|
|
415
|
-
dependencies
|
|
416
|
-
if (optional)
|
|
417
|
-
return undefined;
|
|
418
|
-
throw error;
|
|
508
|
+
return nestDependencyFailure(dependencies, token, optional, error);
|
|
419
509
|
}
|
|
420
510
|
}
|
|
511
|
+
function nestDependencyFailure(dependencies, token, optional, error) {
|
|
512
|
+
dependencies.delete(token);
|
|
513
|
+
if (optional)
|
|
514
|
+
return undefined;
|
|
515
|
+
throw error;
|
|
516
|
+
}
|
|
421
517
|
function findDiscoveredProvider(discovery, type) {
|
|
422
518
|
for (const wrapper of discovery?.getProviders() ?? []) {
|
|
423
519
|
if (wrapper.token === type ||
|
|
@@ -432,7 +528,7 @@ function providerToken(provider) {
|
|
|
432
528
|
return typeof provider === 'function' ? provider : provider.provide;
|
|
433
529
|
}
|
|
434
530
|
async function createSpotManager(registration, runtime, moduleRef, discovery) {
|
|
435
|
-
return framework_loader_1.framework.createIntegrationSpotManager(registration, runtime, moduleRef === undefined ? undefined : createProviderResolver(moduleRef, discovery));
|
|
531
|
+
return framework_loader_1.framework.createIntegrationSpotManager(registration, runtime, moduleRef === undefined ? undefined : createProviderResolver(moduleRef, discovery, registration));
|
|
436
532
|
}
|
|
437
533
|
async function createSpotOutbound(_registration, runtime, _moduleRef, _discovery) {
|
|
438
534
|
return framework_loader_1.framework.createIntegrationSpotOutbound(runtime);
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { DiscoveryService, ModuleRef } from '@nestjs/core';
|
|
2
2
|
import type { ZLinkFrameworkRegistrationOptions } from './framework-integration-contracts';
|
|
3
3
|
import { type ZLinkModuleOptions, type ZLinkNestModuleRegistrationOptions } from './contracts';
|
|
4
|
-
|
|
4
|
+
import { type NestHandlerFactories } from './providers';
|
|
5
|
+
export declare function createDiscoveredRegistration(options: ZLinkNestModuleRegistrationOptions, discovery: DiscoveryService, moduleRef: ModuleRef): import("./framework-integration-contracts").ZLinkFrameworkRegistration;
|
|
6
|
+
export declare function createDiscoveredOptions(options: ZLinkNestModuleRegistrationOptions, discovery: DiscoveryService, moduleRef: ModuleRef, factories?: NestHandlerFactories): ZLinkFrameworkRegistrationOptions;
|
|
5
7
|
export declare function createRegistrationOptions(options: ZLinkNestModuleRegistrationOptions): ZLinkFrameworkRegistrationOptions;
|
|
6
8
|
export declare function assertBuiltModuleOptions(options: ZLinkModuleOptions): ZLinkNestModuleRegistrationOptions;
|