@cheep/transport
Advanced tools
Comparing version 1.0.0-beta.15 to 1.0.0-beta.16
{ | ||
"name": "@cheep/transport", | ||
"version": "1.0.0-beta.15", | ||
"version": "1.0.0-beta.16", | ||
"main": "src/index.js", | ||
"typings": "src/index.d.ts" | ||
} |
@@ -12,1 +12,2 @@ export * from './lib/domain/normalizeError'; | ||
export * from './lib/transport.base'; | ||
export * from './lib/constants'; |
@@ -15,2 +15,3 @@ "use strict"; | ||
tslib_1.__exportStar(require("./lib/transport.base"), exports); | ||
tslib_1.__exportStar(require("./lib/constants"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -12,6 +12,4 @@ import { MessageMetadata } from './transport'; | ||
init(): Promise<void>; | ||
start(): Promise<void>; | ||
stop(): Promise<void>; | ||
protected sendMessage(props: SendMessageProps): Promise<void>; | ||
protected sendReplyMessage(props: SendReplyMessageProps): Promise<void>; | ||
} |
@@ -16,10 +16,2 @@ "use strict"; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
start() { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { }); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
stop() { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { }); | ||
} | ||
sendMessage(props) { | ||
@@ -26,0 +18,0 @@ return tslib_1.__awaiter(this, void 0, void 0, function* () { |
import { CallStackMetadata } from '../metadataReducers/callStack.reducer'; | ||
import { TransactionMetadata } from '../metadataReducers/transaction.reducer'; | ||
import { ValidatorMessage } from '../transport'; | ||
export declare function callStackValidator(prefixesToCheck: string[] | 'all'): (msg: ValidatorMessage<CallStackMetadata>) => void; | ||
export declare function callStackValidator(prefixesToCheck: string[] | 'all'): (msg: ValidatorMessage<CallStackMetadata & TransactionMetadata>) => void; |
@@ -8,3 +8,3 @@ "use strict"; | ||
transactionId, transactionDuration, route, callStack) { | ||
super(`Recursion call [${transactionId}, ${callStack | ||
super(`Transaction Duration Error [${transactionId}, ${transactionDuration}ms, ${callStack | ||
.concat([route]) | ||
@@ -11,0 +11,0 @@ .join(' -> ')}]`); |
import { NormalizedError } from './domain/normalizeError'; | ||
import { ExecuteProps, FireAndForgetHandler, MessageMetadata, MetadataReducer, MetadataValidator, PublishProps, RouteHandler, Transport, TransportMessage, TransportState } from './transport'; | ||
import { ExecuteProps, FireAndForgetHandler, MessageMetadata, MetadataReducer, MetadataValidator, PublishProps, RawHandler, RouteHandler, Transport, TransportMessage, TransportState } from './transport'; | ||
import 'reflect-metadata'; | ||
export interface TransportOptions<TMeta extends MessageMetadata = MessageMetadata> { | ||
@@ -19,2 +20,3 @@ defaultRpcTimeout?: number; | ||
private prefixHandlers; | ||
private rawHandlers; | ||
private registeredPrefixes; | ||
@@ -35,3 +37,6 @@ private listenCallbacks; | ||
execute(props: ExecuteProps<MessageMetadata>): Promise<unknown>; | ||
onEvery(prefixes: string[], action: FireAndForgetHandler): void; | ||
/** provide a fire-and-forget handler for an array of prefixes*/ | ||
onEvery(prefixes: string[], action: FireAndForgetHandler, isRawHandler?: false): void; | ||
/** provide raw handler for a specific prefix */ | ||
onEvery(prefix: string, action: RawHandler, isRawHandler: true): void; | ||
dispose(): Promise<void>; | ||
@@ -38,0 +43,0 @@ protected processMessage(msg: TransportMessage): Promise<void>; |
@@ -9,2 +9,5 @@ "use strict"; | ||
const transport_1 = require("./transport"); | ||
require("reflect-metadata"); | ||
const constants_1 = require("./constants"); | ||
const HANDLER_META = Symbol('ROUTE_HANDLER'); | ||
class TransportBase { | ||
@@ -16,2 +19,3 @@ constructor(options, utils) { | ||
this.prefixHandlers = new Map(); | ||
this.rawHandlers = new Map(); | ||
this.registeredPrefixes = new Set(); | ||
@@ -130,10 +134,23 @@ this.listenCallbacks = new Map(); | ||
} | ||
onEvery(prefixes, action) { | ||
prefixes.forEach(x => { | ||
var _a; | ||
this.registeredPrefixes.add(x); | ||
const handlers = (_a = this.prefixHandlers.get(x)) !== null && _a !== void 0 ? _a : new Set(); | ||
onEvery(prefixes, action, isRawHandler) { | ||
var _a; | ||
const safePrefixes = Array.isArray(prefixes) | ||
? prefixes | ||
: [prefixes]; | ||
safePrefixes.forEach(p => this.registeredPrefixes.add(p)); | ||
if (isRawHandler) { | ||
const prefix = prefixes; | ||
Reflect.defineMetadata(HANDLER_META, true, action); | ||
const handlers = (_a = this.rawHandlers.get(prefix)) !== null && _a !== void 0 ? _a : new Set(); | ||
handlers.add(action); | ||
this.prefixHandlers.set(x, handlers); | ||
}); | ||
this.rawHandlers.set(prefix, handlers); | ||
} | ||
else { | ||
safePrefixes.forEach(prefix => { | ||
var _a; | ||
const handlers = (_a = this.prefixHandlers.get(prefix)) !== null && _a !== void 0 ? _a : new Set(); | ||
handlers.add(action); | ||
this.prefixHandlers.set(prefix, handlers); | ||
}); | ||
} | ||
} | ||
@@ -149,2 +166,3 @@ dispose() { | ||
processMessage(msg) { | ||
var _a; | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
@@ -187,4 +205,9 @@ const { metadataValidator } = this.options; | ||
const handlers = registeredPrefixes.flatMap(prefix => { | ||
const handlerSet = this.prefixHandlers.get(prefix); | ||
return [...handlerSet].map(handler => { | ||
var _a; | ||
const handlerSet = (_a = this.prefixHandlers.get(prefix)) !== null && _a !== void 0 ? _a : []; | ||
return [...handlerSet] | ||
.filter(handler => | ||
// only fire for handlers who DO NOT have the metadata | ||
!Reflect.hasMetadata(HANDLER_META, handler)) | ||
.map(handler => { | ||
return new Promise((resolve, reject) => { | ||
@@ -208,3 +231,15 @@ try { | ||
} | ||
const routeHandlers = this.routeHandlers.get(msg.route); | ||
// find any prefix handlers that are declared as route handlers | ||
const rawPrefixHandlers = registeredPrefixes.flatMap(p => { | ||
var _a; | ||
return [ | ||
...((_a = this.rawHandlers.get(p)) !== null && _a !== void 0 ? _a : []), | ||
]; | ||
}); | ||
// .filter(handler => Reflect.hasMetadata(HANDLER_META, handler)) | ||
// put the route prefix handlers last, so if there are more specific handlers provided, they will be the RPC call | ||
const routeHandlers = [ | ||
...((_a = this.routeHandlers.get(msg.route)) !== null && _a !== void 0 ? _a : []), | ||
...rawPrefixHandlers, | ||
]; | ||
if (routeHandlers === null || routeHandlers === void 0 ? void 0 : routeHandlers.length) { | ||
@@ -216,2 +251,3 @@ const [routeHandler, ...additionalHandlers] = routeHandlers; | ||
// Always call first handler | ||
// it may be a raw handler, so include the msg prop just in case | ||
result = yield routeHandler({ | ||
@@ -221,3 +257,3 @@ route: msg.route, | ||
metadata: message.metadata, | ||
}); | ||
}, msg); | ||
} | ||
@@ -252,3 +288,3 @@ catch (err) { | ||
// Process additional handlers | ||
else if (additionalHandlers.length) { | ||
if (additionalHandlers.length) { | ||
const tasks = additionalHandlers.map(handler => handler({ | ||
@@ -258,3 +294,6 @@ route: msg.route, | ||
metadata: message.metadata, | ||
}).catch(err => { | ||
}, msg).catch(err => { | ||
if (err === constants_1.WILL_NOT_HANDLE) { | ||
return; | ||
} | ||
throw { reason: err, handler: handler.name }; | ||
@@ -266,3 +305,3 @@ })); | ||
if (errs.length > 0) { | ||
console.warn('Multiple.RouteHandlers.Error', errs); | ||
console.warn(`Multiple.RouteHandlers.Error @ ${msg.route}`, errs); | ||
} | ||
@@ -269,0 +308,0 @@ }); |
@@ -9,3 +9,7 @@ export interface Transport { | ||
off(route: string): void; | ||
onEvery(prefixes: string[], action: FireAndForgetHandler): void; | ||
/** provide a fire-and-forget handler for an array of prefixes*/ | ||
onEvery(prefixes: string[], action: FireAndForgetHandler, isRawHandler?: false): void; | ||
/** provide raw handler for a specific prefix */ | ||
onEvery(prefix: string, action: RawHandler, isRawHandler: true): void; | ||
onEvery(prefixes: string[] | string, action: RawHandler | FireAndForgetHandler, isRawHandler?: boolean): void; | ||
/** | ||
@@ -42,5 +46,5 @@ * At this point all handlers are registered and we can | ||
} | ||
export interface TransportCompactMessage { | ||
export interface TransportCompactMessage<TPayload = unknown> { | ||
route: string; | ||
payload: unknown; | ||
payload: TPayload; | ||
metadata: MessageMetadata; | ||
@@ -50,2 +54,3 @@ } | ||
export declare type MessageMetadata = Record<string, unknown>; | ||
export declare type RawHandler = (item: TransportCompactMessage, raw: TransportMessage) => Promise<unknown | void>; | ||
export declare type RouteHandler = (item: TransportCompactMessage) => Promise<unknown | void>; | ||
@@ -52,0 +57,0 @@ export declare type FireAndForgetHandler = (item: TransportCompactMessage) => void; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
52640
47
846