@nodescript/protocomm
Advanced tools
Comparing version 0.5.2 to 0.5.3
@@ -30,2 +30,7 @@ import { ObjectPropsDef } from '@nodescript/schema'; | ||
export type MethodType = 'query' | 'command'; | ||
export interface DomainMethodStat { | ||
domain: string; | ||
method: string; | ||
latency: number; | ||
} | ||
export {}; |
@@ -0,1 +1,3 @@ | ||
import { Event } from '@nodescript/event'; | ||
import { DomainMethodStat } from './domain.js'; | ||
import { ProtocolIndex } from './protocol.js'; | ||
@@ -11,2 +13,3 @@ import { RpcEvent, RpcMethodRequest, RpcMethodResponse } from './rpc-messages.js'; | ||
readonly sendEvent: (evt: RpcEvent) => void; | ||
methodStats: Event<DomainMethodStat>; | ||
constructor(protocolIndex: ProtocolIndex<P>, protocolImpl: P, sendResponse: (res: RpcMethodResponse) => void, sendEvent: (evt: RpcEvent) => void); | ||
@@ -13,0 +16,0 @@ processMessage(msg: unknown): Promise<void>; |
@@ -0,1 +1,2 @@ | ||
import { Event } from '@nodescript/event'; | ||
import { ChannelEvent } from './channel-event.js'; | ||
@@ -13,2 +14,3 @@ import { MethodNotFound } from './protocol.js'; | ||
this.sendEvent = sendEvent; | ||
this.methodStats = new Event(); | ||
this.registerEvents(); | ||
@@ -40,12 +42,22 @@ } | ||
async runMethod(rpcReq) { | ||
const { domain, method, params } = rpcReq; | ||
const { reqSchema, resSchema, } = this.protocolIndex.lookupMethod(domain, method); | ||
const domainImpl = this.protocolImpl[domain]; | ||
const methodImpl = domainImpl?.[method]; | ||
if (!methodImpl) { | ||
throw new MethodNotFound(`${domain}.${method}`); | ||
const startedAt = Date.now(); | ||
try { | ||
const { domain, method, params } = rpcReq; | ||
const { reqSchema, resSchema, } = this.protocolIndex.lookupMethod(domain, method); | ||
const domainImpl = this.protocolImpl[domain]; | ||
const methodImpl = domainImpl?.[method]; | ||
if (!methodImpl) { | ||
throw new MethodNotFound(`${domain}.${method}`); | ||
} | ||
const decodedParams = reqSchema.decode(params, { strictRequired: true }); | ||
const res = await methodImpl.call(domainImpl, decodedParams); | ||
return resSchema.decode(res); | ||
} | ||
const decodedParams = reqSchema.decode(params, { strictRequired: true }); | ||
const res = await methodImpl.call(domainImpl, decodedParams); | ||
return resSchema.decode(res); | ||
finally { | ||
this.methodStats.emit({ | ||
domain: rpcReq.domain, | ||
method: rpcReq.method, | ||
latency: Date.now() - startedAt, | ||
}); | ||
} | ||
} | ||
@@ -52,0 +64,0 @@ registerEvents() { |
{ | ||
"name": "@nodescript/protocomm", | ||
"version": "0.5.2", | ||
"version": "0.5.3", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "description": "Bi-directional transport-agnostic JSON-based messaging", |
Sorry, the diff of this file is not supported yet
37808
575