@matrixai/rpc
Advanced tools
Comparing version 0.4.4 to 0.5.0
@@ -7,3 +7,2 @@ /// <reference types="node" /> | ||
declare class RPCClient<M extends ClientManifest> { | ||
protected onTimeoutCallback?: () => void; | ||
protected idGen: IdGen; | ||
@@ -15,4 +14,4 @@ protected logger: Logger; | ||
protected callerTypes: Record<string, HandlerType>; | ||
registerOnTimeoutCallback(callback: () => void): void; | ||
readonly timeoutTime: number; | ||
readonly graceTime: number; | ||
readonly methodsProxy: {}; | ||
@@ -34,3 +33,3 @@ /** | ||
*/ | ||
constructor({ manifest, streamFactory, middlewareFactory, timeoutTime, logger, toError, idGen, }: { | ||
constructor({ manifest, streamFactory, middlewareFactory, timeoutTime, graceTime, logger, toError, idGen, }: { | ||
manifest: M; | ||
@@ -40,2 +39,3 @@ streamFactory: StreamFactory; | ||
timeoutTime?: number; | ||
graceTime?: number; | ||
logger?: Logger; | ||
@@ -106,3 +106,2 @@ idGen?: IdGen; | ||
* @param ctx - ContextTimed used for timeouts and cancellation. | ||
* @param id - Id is generated only once, and used throughout the stream for the rest of the communication | ||
*/ | ||
@@ -109,0 +108,0 @@ rawStreamCaller(method: string, headerParams: JSONObject, ctx?: Partial<ContextTimedInput>): Promise<RPCStream<Uint8Array, Uint8Array, Record<string, JSONValue> & { |
@@ -36,3 +36,2 @@ "use strict"; | ||
class RPCClient { | ||
onTimeoutCallback; | ||
idGen; | ||
@@ -44,7 +43,5 @@ logger; | ||
callerTypes; | ||
registerOnTimeoutCallback(callback) { | ||
this.onTimeoutCallback = callback; | ||
} | ||
// Method proxies | ||
timeoutTime; | ||
graceTime; | ||
methodsProxy = new Proxy({}, { | ||
@@ -85,3 +82,3 @@ get: (_, method) => { | ||
*/ | ||
constructor({ manifest, streamFactory, middlewareFactory = middleware.defaultClientMiddlewareWrapper(), timeoutTime = Infinity, logger, toError = utils.toError, idGen = () => null, }) { | ||
constructor({ manifest, streamFactory, middlewareFactory = middleware.defaultClientMiddlewareWrapper(), timeoutTime = Infinity, graceTime = 1000, logger, toError = utils.toError, idGen = () => null, }) { | ||
if (timeoutTime < 0) { | ||
@@ -95,2 +92,3 @@ throw new errors.ErrorRPCInvalidTimeout(); | ||
this.timeoutTime = timeoutTime; | ||
this.graceTime = graceTime; | ||
this.logger = logger ?? new logger_1.default(this.constructor.name); | ||
@@ -228,3 +226,6 @@ this.toError = toError; | ||
} | ||
let timerGrace; | ||
const cleanUp = () => { | ||
if (timerGrace != null) | ||
timerGrace.cancel(timerCleanupReasonSymbol); | ||
// Clean up the timer and signal | ||
@@ -242,5 +243,2 @@ if (ctx.timer == null) | ||
abortController.abort(timeoutError); | ||
if (this.onTimeoutCallback) { | ||
this.onTimeoutCallback(); | ||
} | ||
}, () => { }); | ||
@@ -258,3 +256,12 @@ // Hooking up agnostic stream side | ||
} | ||
void timer.then(() => { | ||
void timer.then(async () => { | ||
timerGrace = new timer_1.Timer({ delay: this.graceTime }); | ||
try { | ||
await timerGrace; | ||
} | ||
catch (e) { | ||
if (e === timerCleanupReasonSymbol) | ||
return; | ||
throw e; | ||
} | ||
rpcStream.cancel(new errors.ErrorRPCTimedOut('RPC has timed out', { | ||
@@ -314,3 +321,2 @@ cause: ctx.signal?.reason, | ||
* @param ctx - ContextTimed used for timeouts and cancellation. | ||
* @param id - Id is generated only once, and used throughout the stream for the rest of the communication | ||
*/ | ||
@@ -317,0 +323,0 @@ async rawStreamCaller(method, headerParams, ctx = {}) { |
@@ -15,3 +15,2 @@ import type { IdGen, ClientHandlerImplementation, DuplexHandlerImplementation, JSONRPCRequest, JSONRPCResponseSuccess, ServerManifest, RawHandlerImplementation, ServerHandlerImplementation, UnaryHandlerImplementation, RPCStream, MiddlewareFactory, FromError, JSONObject } from './types'; | ||
declare class RPCServer { | ||
protected onTimeoutCallback?: () => void; | ||
protected idGen: IdGen; | ||
@@ -26,6 +25,6 @@ protected logger: Logger; | ||
protected middlewareFactory: MiddlewareFactory<JSONRPCRequest, Uint8Array, Uint8Array, JSONRPCResponseSuccess>; | ||
registerOnTimeoutCallback(callback: () => void): void; | ||
/** | ||
* RPCServer Constructor | ||
* | ||
* @param obj | ||
* @param obj.middlewareFactory - Middleware used to process the rpc messages. | ||
@@ -32,0 +31,0 @@ * The middlewareFactory needs to be a function that creates a pair of |
@@ -49,3 +49,2 @@ "use strict"; | ||
let RPCServer = class RPCServer { | ||
onTimeoutCallback; | ||
idGen; | ||
@@ -60,9 +59,6 @@ logger; | ||
middlewareFactory; | ||
// Function to register a callback for timeout | ||
registerOnTimeoutCallback(callback) { | ||
this.onTimeoutCallback = callback; | ||
} | ||
/** | ||
* RPCServer Constructor | ||
* | ||
* @param obj | ||
* @param obj.middlewareFactory - Middleware used to process the rpc messages. | ||
@@ -341,5 +337,2 @@ * The middlewareFactory needs to be a function that creates a pair of | ||
abortController.abort(new errors.ErrorRPCTimedOut()); | ||
if (this.onTimeoutCallback) { | ||
this.onTimeoutCallback(); | ||
} | ||
}, | ||
@@ -346,0 +339,0 @@ }); |
@@ -112,3 +112,3 @@ /// <reference types="node" /> | ||
/** | ||
* `T` is the the params you want to specify. | ||
* `T` is the params you want to specify. | ||
* | ||
@@ -126,3 +126,3 @@ * `M` is the metadata you want to specify. | ||
/** | ||
* `T` is the the result you want to specify. | ||
* `T` is the result you want to specify. | ||
* | ||
@@ -192,3 +192,3 @@ * `M` is the metadata you want to specify. | ||
/** | ||
* This is a factory for creating a `RPCStream` when making a RPC call. | ||
* This is a factory for creating a `RPCStream` when making an RPC call. | ||
* The transport mechanism is a black box to the RPC system. So long as it is | ||
@@ -195,0 +195,0 @@ * provided as a RPCStream the RPC system should function. It is assumed that |
@@ -33,3 +33,3 @@ "use strict"; | ||
exports.timeoutCancelledReason = timeoutCancelledReason; | ||
// Importing PK funcs and utils which are essential for RPC | ||
// Importing PK functions and utils which are essential for RPC | ||
function isObject(o) { | ||
@@ -211,3 +211,3 @@ return o !== null && typeof o === 'object'; | ||
function fromError(error) { | ||
// TODO: Linked-List traversal must be done iteractively rather than recusively to prevent stack overflow. | ||
// TODO: Linked-List traversal must be done interactively rather than recursively to prevent stack overflow. | ||
switch (typeof error) { | ||
@@ -352,6 +352,5 @@ case 'symbol': | ||
if (top) { | ||
const err = new errors.ErrorRPCRemote(clientMetadata, undefined, { | ||
return new errors.ErrorRPCRemote(clientMetadata, undefined, { | ||
cause: e, | ||
}); | ||
return err; | ||
} | ||
@@ -358,0 +357,0 @@ else { |
{ | ||
"name": "@matrixai/rpc", | ||
"version": "0.4.4", | ||
"version": "0.5.0", | ||
"author": "Matrix AI", | ||
@@ -5,0 +5,0 @@ "contributors": [ |
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
261032
3051