-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRPCClient.ts
More file actions
541 lines (526 loc) · 17.9 KB
/
Copy pathRPCClient.ts
File metadata and controls
541 lines (526 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
import type { ReadableStream, WritableStream } from 'stream/web';
import type { ContextTimedInput } from '@matrixai/contexts';
import type {
ClientManifest,
HandlerType,
IdGen,
JSONObject,
JSONRPCRequest,
JSONRPCRequestMessage,
JSONRPCResponse,
JSONRPCResponseSuccess,
JSONValue,
MapCallers,
MiddlewareFactory,
RPCStream,
StreamFactory,
ToError,
} from './types.js';
import Logger from '@matrixai/logger';
import { Timer } from '@matrixai/timer';
import * as middleware from './middleware.js';
import * as errors from './errors.js';
import * as utils from './utils.js';
const timerCleanupReasonSymbol = Symbol('timerCleanUpReasonSymbol');
class RPCClient<M extends ClientManifest> {
protected idGen: IdGen;
protected logger: Logger;
protected streamFactory: StreamFactory;
protected toError: ToError;
protected middlewareFactory: MiddlewareFactory<
Uint8Array,
JSONRPCRequest,
JSONRPCResponse,
Uint8Array
>;
protected callerTypes: Record<string, HandlerType>;
// Method proxies
public readonly timeoutTime: number;
public readonly graceTime: number;
public readonly methodsProxy = new Proxy(
{},
{
get: (_, method) => {
if (typeof method === 'symbol') return;
switch (this.callerTypes[method]) {
case 'UNARY':
return (params: JSONObject, ctx: Partial<ContextTimedInput>) =>
this.unaryCaller(method, params, ctx);
case 'SERVER':
return (params: JSONObject, ctx: Partial<ContextTimedInput>) =>
this.serverStreamCaller(method, params, ctx);
case 'CLIENT':
return (ctx: Partial<ContextTimedInput>) =>
this.clientStreamCaller(method, ctx);
case 'DUPLEX':
return (ctx: Partial<ContextTimedInput>) =>
this.duplexStreamCaller(method, ctx);
case 'RAW':
return (header: JSONObject, ctx: Partial<ContextTimedInput>) =>
this.rawStreamCaller(method, header, ctx);
default:
return;
}
},
},
);
/**
* @param obj
* @param obj.manifest - Client manifest that defines the types for the rpc
* methods.
* @param obj.streamFactory - An arrow function that when called, creates a
* new stream for each rpc method call.
* @param obj.middlewareFactory - Middleware used to process the rpc messages.
* The middlewareFactory needs to be a function that creates a pair of
* transform streams that convert `JSONRPCRequest` to `Uint8Array` on the forward
* path and `Uint8Array` to `JSONRPCResponse` on the reverse path.
* @param obj.timeoutTime - Timeout time used if no timeout timer was provided when making a call.
* Defaults to 60,000 milliseconds.
* for a client call.
* @param obj.logger
*/
public constructor({
manifest,
streamFactory,
middlewareFactory = middleware.defaultClientMiddlewareWrapper(),
timeoutTime = Infinity,
graceTime = 1000,
logger,
toError = utils.toError,
idGen = () => null,
}: {
manifest: M;
streamFactory: StreamFactory;
middlewareFactory?: MiddlewareFactory<
Uint8Array,
JSONRPCRequest,
JSONRPCResponse,
Uint8Array
>;
timeoutTime?: number;
graceTime?: number;
logger?: Logger;
idGen?: IdGen;
toError?: ToError;
}) {
if (timeoutTime < 0) {
throw new errors.ErrorRPCInvalidTimeout();
}
this.idGen = idGen;
this.callerTypes = utils.getHandlerTypes(manifest);
this.streamFactory = streamFactory;
this.middlewareFactory = middlewareFactory;
this.timeoutTime = timeoutTime;
this.graceTime = graceTime;
this.logger = logger ?? new Logger(this.constructor.name);
this.toError = toError;
}
public get methods(): MapCallers<M> {
return this.methodsProxy as MapCallers<M>;
}
/**
* Generic caller for unary RPC calls.
* This returns the response in the provided type. No validation is done so
* make sure the types match the handler types.
* @param method - Method name of the RPC call
* @param parameters - Parameters to be provided with the RPC message. Matches
* the provided I type.
* @param ctx - ContextTimed used for timeouts and cancellation.
*/
public async unaryCaller<I extends JSONObject, O extends JSONObject>(
method: string,
parameters: I,
ctx: Partial<ContextTimedInput> = {},
): Promise<O> {
const callerInterface = await this.duplexStreamCaller<I, O>(method, ctx);
const reader = callerInterface.readable.getReader();
const writer = callerInterface.writable.getWriter();
try {
await writer.write(parameters);
const output = await reader.read();
if (output.done) {
throw new errors.ErrorMissingCaller('Missing response', {
cause: ctx.signal?.reason,
});
}
await writer.close();
// Release lock of previous reader to ready for flush
reader.releaseLock();
for await (const _ of callerInterface.readable) {
// Noop so that stream can close after flushing
}
return output.value;
} finally {
// Attempt clean up, ignore errors if already cleaned up
await writer.close().catch(() => {});
}
}
/**
* Generic caller for server streaming RPC calls.
* This returns a ReadableStream of the provided type. When finished, the
* readable needs to be cleaned up, otherwise cleanup happens mostly
* automatically.
* @param method - Method name of the RPC call
* @param parameters - Parameters to be provided with the RPC message. Matches
* the provided I type.
* @param ctx - ContextTimed used for timeouts and cancellation.
*/
public async serverStreamCaller<I extends JSONObject, O extends JSONObject>(
method: string,
parameters: I,
ctx: Partial<ContextTimedInput> = {},
): Promise<ReadableStream<O>> {
const callerInterface = await this.duplexStreamCaller<I, O>(method, ctx);
const writer = callerInterface.writable.getWriter();
try {
await writer.write(parameters);
await writer.close();
} catch (e) {
// Clean up if any problems, ignore errors if already closed
await callerInterface.readable.cancel(e);
throw e;
}
return callerInterface.readable;
}
/**
* Generic caller for Client streaming RPC calls.
* This returns a WritableStream for writing the input to and a Promise that
* resolves when the output is received.
* When finished the writable stream must be ended. Failing to do so will
* hold the connection open and result in a resource leak until the
* call times out.
* @param method - Method name of the RPC call
* @param ctx - ContextTimed used for timeouts and cancellation.
*/
public async clientStreamCaller<I extends JSONObject, O extends JSONObject>(
method: string,
ctx: Partial<ContextTimedInput> = {},
): Promise<{
output: Promise<O>;
writable: WritableStream<I>;
}> {
const callerInterface = await this.duplexStreamCaller<I, O>(method, ctx);
const reader = callerInterface.readable.getReader();
const output = reader.read().then(({ value, done }) => {
if (done) {
throw new errors.ErrorMissingCaller('Missing response', {
cause: ctx.signal?.reason,
});
}
return value;
});
return {
output,
writable: callerInterface.writable,
};
}
/**
* Generic caller for duplex RPC calls.
* This returns a `ReadableWritablePair` of the types specified. No validation
* is applied to these types so make sure they match the types of the handler
* you are calling.
* When finished the streams must be ended manually. Failing to do so will
* hold the connection open and result in a resource leak until the
* call times out.
* @param method - Method name of the RPC call
* @param ctx - ContextTimed used for timeouts and cancellation.
*/
public async duplexStreamCaller<I extends JSONObject, O extends JSONObject>(
method: string,
ctx: Partial<ContextTimedInput> = {},
): Promise<RPCStream<O, I>> {
// Setting up abort signal and timer
const abortController = new AbortController();
const signal = abortController.signal;
// A promise that will reject if there is an abort signal or timeout
const abortRaceProm = utils.promise<never>();
// Prevent unhandled rejection when we're done with the promise
abortRaceProm.p.catch(() => {});
const abortRacePromHandler = () => {
abortRaceProm.rejectP(signal.reason);
};
signal.addEventListener('abort', abortRacePromHandler);
let abortHandler: () => void;
if (ctx.signal != null) {
// Propagate signal events
abortHandler = () => {
abortController.abort(ctx.signal?.reason);
};
if (ctx.signal.aborted) abortHandler();
ctx.signal.addEventListener('abort', abortHandler);
}
let timer: Timer;
if (!(ctx.timer instanceof Timer)) {
timer = new Timer({
delay: ctx.timer ?? this.timeoutTime,
});
} else {
timer = ctx.timer;
}
let timerGrace: Timer | undefined;
const cleanUp = () => {
if (timerGrace != null) timerGrace.cancel(timerCleanupReasonSymbol);
// Clean up the timer and signal
if (ctx.timer == null) timer.cancel(timerCleanupReasonSymbol);
if (ctx.signal != null) {
ctx.signal.removeEventListener('abort', abortHandler);
}
signal.addEventListener('abort', abortRacePromHandler);
};
// Setting up abort events for timeout
const timeoutError = new errors.ErrorRPCTimedOut(
'Error RPC has timed out',
{ cause: ctx.signal?.reason },
);
void timer.then(
() => {
abortController.abort(timeoutError);
},
() => {}, // Ignore cancellation error
);
// Hooking up agnostic stream side
let rpcStream: RPCStream<Uint8Array, Uint8Array>;
const streamFactoryProm = this.streamFactory({ signal, timer }).then(
(e) => ({ status: 'fulfilled' as const, value: e }),
(e) => ({ status: 'rejected' as const, reason: e }),
);
try {
const rpcStreamResult = await Promise.race([
streamFactoryProm,
abortRaceProm.p,
]);
if (rpcStreamResult.status === 'rejected') {
throw rpcStreamResult.reason;
}
rpcStream = rpcStreamResult.value;
} catch (e) {
cleanUp();
void streamFactoryProm.then((streamResult) => {
if (streamResult.status === 'fulfilled') {
streamResult.value.cancel(errors.ErrorRPCStreamEnded);
}
});
throw e;
}
void timer.then(
async () => {
timerGrace = new Timer({ delay: this.graceTime });
try {
await timerGrace;
} catch (e) {
if (e === timerCleanupReasonSymbol) return;
throw e;
}
rpcStream.cancel(
new errors.ErrorRPCTimedOut('RPC has timed out', {
cause: ctx.signal?.reason,
}),
);
},
() => {}, // Ignore cancellation error
);
// Deciding if we want to allow cancelling
// We want to cancel timer if none was provided
const cancellingTimer: Timer | undefined = !(ctx.timer instanceof Timer)
? timer
: undefined;
// Composing stream transforms and middleware
const metadata = {
...(rpcStream.meta ?? {}),
command: method,
};
const outputMessageTransformStream = utils.clientOutputTransformStream<O>(
metadata,
this.toError,
cancellingTimer,
);
const inputMessageTransformStream =
utils.clientInputTransformStream<I>(method);
const middleware = this.middlewareFactory(
{ signal, timer },
(...args) => rpcStream.cancel(...args),
metadata,
);
// This `Promise.allSettled` is used to asynchronously track the state
// of the streams. When both have finished we can clean up resources.
void Promise.allSettled([
rpcStream.readable
.pipeThrough(middleware.reverse)
.pipeTo(outputMessageTransformStream.writable)
// Ignore any errors, we only care about stream ending
.catch(() => {}),
inputMessageTransformStream.readable
.pipeThrough(middleware.forward)
.pipeTo(rpcStream.writable)
// Ignore any errors, we only care about stream ending
.catch(() => {}),
]).finally(() => {
cleanUp();
});
// Returning interface
return {
readable: outputMessageTransformStream.readable,
writable: inputMessageTransformStream.writable,
cancel: (...args) => rpcStream.cancel(...args),
meta: metadata,
};
}
/**
* Generic caller for raw RPC calls.
* This returns a `ReadableWritablePair` of the raw RPC stream.
* When finished the streams must be ended manually. Failing to do so will
* hold the connection open and result in a resource leak until the
* call times out.
* Raw streams don't support the keep alive timeout. Timeout will only apply\
* to the creation of the stream.
* @param method - Method name of the RPC call
* @param headerParams - Parameters for the header message. The header is a
* single RPC message that is sent to specify the method for the RPC call.
* Any metadata of extra parameters is provided here.
* @param ctx - ContextTimed used for timeouts and cancellation.
*/
public async rawStreamCaller(
method: string,
headerParams: JSONObject,
ctx: Partial<ContextTimedInput> = {},
): Promise<
RPCStream<
Uint8Array,
Uint8Array,
Record<string, JSONValue> & { result: JSONValue; command: string }
>
> {
// Setting up abort signal and timer
const abortController = new AbortController();
const signal = abortController.signal;
// A promise that will reject if there is an abort signal or timeout
const abortRaceProm = utils.promise<never>();
// Prevent unhandled rejection when we're done with the promise
abortRaceProm.p.catch(() => {});
const abortRacePromHandler = () => {
abortRaceProm.rejectP(signal.reason);
};
signal.addEventListener('abort', abortRacePromHandler);
let abortHandler: () => void;
if (ctx.signal != null) {
// Propagate signal events
abortHandler = () => {
abortController.abort(ctx.signal?.reason);
};
if (ctx.signal.aborted) abortHandler();
ctx.signal.addEventListener('abort', abortHandler);
}
let timer: Timer;
if (!(ctx.timer instanceof Timer)) {
timer = new Timer({
delay: ctx.timer ?? this.timeoutTime,
});
} else {
timer = ctx.timer;
}
const cleanUp = () => {
// Clean up the timer and signal
if (ctx.timer == null) timer.cancel(timerCleanupReasonSymbol);
if (ctx.signal != null) {
ctx.signal.removeEventListener('abort', abortHandler);
}
signal.addEventListener('abort', abortRacePromHandler);
};
// Setting up abort events for timeout
const timeoutError = new errors.ErrorRPCTimedOut('RPC has timed out', {
cause: ctx.signal?.reason,
});
void timer.then(
() => {
abortController.abort(timeoutError);
},
() => {}, // Ignore cancellation error
);
const setupStream = async (): Promise<
[JSONValue, RPCStream<Uint8Array, Uint8Array>]
> => {
if (signal.aborted) throw signal.reason;
const abortProm = utils.promise<never>();
// Ignore error if orphaned
void abortProm.p.catch(() => {});
signal.addEventListener(
'abort',
() => {
abortProm.rejectP(signal.reason);
},
{ once: true },
);
const rpcStream = await Promise.race([
this.streamFactory({ signal, timer }),
abortProm.p,
]);
const tempWriter = rpcStream.writable.getWriter();
const id = await this.idGen();
const header: JSONRPCRequestMessage = {
jsonrpc: '2.0',
method,
params: headerParams,
id,
};
await tempWriter.write(Buffer.from(JSON.stringify(header)));
tempWriter.releaseLock();
const headTransformStream = utils.parseHeadStream(
utils.parseJSONRPCResponse,
);
void rpcStream.readable
// Allow us to re-use the readable after reading the first message
.pipeTo(headTransformStream.writable)
// Ignore any errors here, we only care that it ended
.catch(() => {});
const tempReader = headTransformStream.readable.getReader();
let leadingMessage: JSONRPCResponseSuccess;
try {
const message = await Promise.race([tempReader.read(), abortProm.p]);
const messageValue = message.value as JSONRPCResponse;
if (message.done) {
utils.never('a message was expected, received done instead');
}
if ('error' in messageValue) {
const metadata = {
...(rpcStream.meta ?? {}),
command: method,
};
throw this.toError(messageValue.error.data, metadata);
}
leadingMessage = messageValue;
} catch (e) {
rpcStream.cancel(
new errors.ErrorRPCStreamEnded('RPC Stream Ended', { cause: e }),
);
throw e;
}
tempReader.releaseLock();
const newRpcStream: RPCStream<Uint8Array, Uint8Array> = {
writable: rpcStream.writable,
readable: headTransformStream.readable as ReadableStream<Uint8Array>,
cancel: (...args) => rpcStream.cancel(...args),
meta: rpcStream.meta,
};
return [leadingMessage.result, newRpcStream];
};
let streamCreation: [JSONValue, RPCStream<Uint8Array, Uint8Array>];
try {
streamCreation = await setupStream();
} finally {
cleanUp();
}
const [result, rpcStream] = streamCreation;
const metadata = {
...(rpcStream.meta ?? {}),
result,
command: method,
};
return {
writable: rpcStream.writable,
readable: rpcStream.readable,
cancel: (...args) => rpcStream.cancel(...args),
meta: metadata,
};
}
}
export default RPCClient;