-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
/
Copy pathserver-grpc.ts
734 lines (672 loc) · 22.8 KB
/
server-grpc.ts
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
import {
isObject,
isString,
isUndefined,
} from '@nestjs/common/utils/shared.utils';
import {
EMPTY,
Observable,
ReplaySubject,
Subject,
Subscription,
defaultIfEmpty,
fromEvent,
lastValueFrom,
} from 'rxjs';
import { catchError, takeUntil } from 'rxjs/operators';
import { GRPC_DEFAULT_PROTO_LOADER, GRPC_DEFAULT_URL } from '../constants';
import { GrpcMethodStreamingType } from '../decorators';
import { Transport } from '../enums';
import { InvalidGrpcPackageException } from '../errors/invalid-grpc-package.exception';
import { InvalidProtoDefinitionException } from '../errors/invalid-proto-definition.exception';
import { ChannelOptions } from '../external/grpc-options.interface';
import { getGrpcPackageDefinition } from '../helpers';
import { MessageHandler } from '../interfaces';
import {
BuildServerSettings,
GrpcOptions,
TransportId,
} from '../interfaces/microservice-configuration.interface';
import { Server } from './server';
const CANCELLED_EVENT = 'cancelled';
// To enable type safety for gRPC. This cant be uncommented by default
// because it would require the user to install the @grpc/grpc-js package even if they dont use gRPC
// Otherwise, TypeScript would fail to compile the code.
//
// type GrpcServer = import('@grpc/grpc-js').Server;
// let grpcPackage = {} as typeof import('@grpc/grpc-js');
// let grpcProtoLoaderPackage = {} as typeof import('@grpc/proto-loader');
type GrpcServer = any;
let grpcPackage = {} as any;
let grpcProtoLoaderPackage = {} as any;
interface GrpcCall<TRequest = any, TMetadata = any> {
request: TRequest;
metadata: TMetadata;
sendMetadata: Function;
end: Function;
write: Function;
on: Function;
off: Function;
emit: Function;
}
/**
* @publicApi
*/
export class ServerGrpc extends Server<never, never> {
public readonly transportId: TransportId = Transport.GRPC;
protected readonly url: string;
protected grpcClient: GrpcServer;
get status(): never {
throw new Error(
'The "status" attribute is not supported by the gRPC transport',
);
}
constructor(
private readonly options: Readonly<GrpcOptions>['options'],
buildServerSettings?: BuildServerSettings,
) {
super();
this.transportId = buildServerSettings?.transportId ?? Transport.GRPC;
this.url = this.getOptionsProp(options, 'url') || GRPC_DEFAULT_URL;
const protoLoader =
this.getOptionsProp(options, 'protoLoader') || GRPC_DEFAULT_PROTO_LOADER;
grpcPackage = this.loadPackage('@grpc/grpc-js', ServerGrpc.name, () =>
require('@grpc/grpc-js'),
);
grpcProtoLoaderPackage = this.loadPackage(
protoLoader,
ServerGrpc.name,
() =>
protoLoader === GRPC_DEFAULT_PROTO_LOADER
? require('@grpc/proto-loader')
: require(protoLoader),
);
}
public async listen(
callback: (err?: unknown, ...optionalParams: unknown[]) => void,
) {
try {
this.grpcClient = await this.createClient();
await this.start(callback);
} catch (err) {
callback(err);
}
}
public async start(callback?: () => void) {
await this.bindEvents();
callback?.();
}
public async bindEvents() {
const grpcContext = this.loadProto();
const packageOption = this.getOptionsProp(this.options, 'package');
const packageNames = Array.isArray(packageOption)
? packageOption
: [packageOption];
for (const packageName of packageNames) {
const grpcPkg = this.lookupPackage(grpcContext, packageName);
await this.createServices(grpcPkg, packageName);
}
}
/**
* Will return all of the services along with their fully namespaced
* names as an array of objects.
* This method initiates recursive scan of grpcPkg object
*/
public getServiceNames(grpcPkg: any): { name: string; service: any }[] {
// Define accumulator to collect all of the services available to load
const services: { name: string; service: any }[] = [];
// Initiate recursive services collector starting with empty name
this.collectDeepServices('', grpcPkg, services);
return services;
}
/**
* Will create service mapping from gRPC generated Object to handlers
* defined with @GrpcMethod or @GrpcStreamMethod annotations
*
* @param grpcService
* @param name
*/
public async createService(grpcService: any, name: string) {
const service = {};
for (const methodName in grpcService.prototype) {
let methodHandler: MessageHandler | null = null;
let streamingType = GrpcMethodStreamingType.NO_STREAMING;
const methodFunction = grpcService.prototype[methodName];
const methodReqStreaming = methodFunction.requestStream;
if (!isUndefined(methodReqStreaming) && methodReqStreaming) {
// Try first pattern to be presented, RX streaming pattern would be
// a preferable pattern to select among a few defined
methodHandler = this.getMessageHandler(
name,
methodName,
GrpcMethodStreamingType.RX_STREAMING,
methodFunction,
);
streamingType = GrpcMethodStreamingType.RX_STREAMING;
// If first pattern didn't match to any of handlers then try
// pass-through handler to be presented
if (!methodHandler) {
methodHandler = this.getMessageHandler(
name,
methodName,
GrpcMethodStreamingType.PT_STREAMING,
methodFunction,
);
streamingType = GrpcMethodStreamingType.PT_STREAMING;
}
} else {
// Select handler if any presented for No-Streaming pattern
methodHandler = this.getMessageHandler(
name,
methodName,
GrpcMethodStreamingType.NO_STREAMING,
methodFunction,
);
streamingType = GrpcMethodStreamingType.NO_STREAMING;
}
if (!methodHandler) {
continue;
}
service[methodName] = this.createServiceMethod(
methodHandler,
grpcService.prototype[methodName],
streamingType,
);
}
return service;
}
public getMessageHandler(
serviceName: string,
methodName: string,
streaming: GrpcMethodStreamingType,
grpcMethod: { path?: string },
): MessageHandler {
let pattern = this.createPattern(serviceName, methodName, streaming);
let methodHandler = this.messageHandlers.get(pattern)!;
if (!methodHandler) {
const packageServiceName = grpcMethod.path?.split?.('/')[1];
pattern = this.createPattern(packageServiceName!, methodName, streaming);
methodHandler = this.messageHandlers.get(pattern)!;
}
return methodHandler;
}
/**
* Will create a string of a JSON serialized format
*
* @param service name of the service which should be a match to gRPC service definition name
* @param methodName name of the method which is coming after rpc keyword
* @param streaming GrpcMethodStreamingType parameter which should correspond to
* stream keyword in gRPC service request part
*/
public createPattern(
service: string,
methodName: string,
streaming: GrpcMethodStreamingType,
): string {
return JSON.stringify({
service,
rpc: methodName,
streaming,
});
}
/**
* Will return async function which will handle gRPC call
* with Rx streams or as a direct call passthrough
*
* @param methodHandler
* @param protoNativeHandler
* @param streamType
*/
public createServiceMethod(
methodHandler: Function,
protoNativeHandler: any,
streamType: GrpcMethodStreamingType,
): Function {
// If proto handler has request stream as "true" then we expect it to have
// streaming from the side of requester
if (protoNativeHandler.requestStream) {
// If any handlers were defined with GrpcStreamMethod annotation use RX
if (streamType === GrpcMethodStreamingType.RX_STREAMING) {
return this.createRequestStreamMethod(
methodHandler,
protoNativeHandler.responseStream,
);
}
// If any handlers were defined with GrpcStreamCall annotation
else if (streamType === GrpcMethodStreamingType.PT_STREAMING) {
return this.createStreamCallMethod(
methodHandler,
protoNativeHandler.responseStream,
);
}
}
return protoNativeHandler.responseStream
? this.createStreamServiceMethod(methodHandler)
: this.createUnaryServiceMethod(methodHandler);
}
public createUnaryServiceMethod(methodHandler: Function): Function {
return async (call: GrpcCall, callback: Function) => {
const handler = methodHandler(call.request, call.metadata, call);
this.transformToObservable(await handler).subscribe({
next: async data => callback(null, await data),
error: (err: any) => callback(err),
});
};
}
public createStreamServiceMethod(methodHandler: Function): Function {
return async (call: GrpcCall, callback: Function) => {
const handler = methodHandler(call.request, call.metadata, call);
const result$ = this.transformToObservable(await handler);
await this.writeObservableToGrpc(result$, call);
};
}
public unwrap<T>(): T {
throw new Error('Method is not supported for gRPC transport');
}
public on<
EventKey extends string | number | symbol = string | number | symbol,
EventCallback = any,
>(event: EventKey, callback: EventCallback) {
throw new Error('Method is not supported in gRPC mode.');
}
/**
* Writes an observable to a GRPC call.
*
* This function will ensure that backpressure is managed while writing values
* that come from an observable to a GRPC call.
*
* @param source The observable we want to write out to the GRPC call.
* @param call The GRPC call we want to write to.
* @returns A promise that resolves when we're done writing to the call.
*/
private writeObservableToGrpc<T>(
source: Observable<T>,
call: GrpcCall<T>,
): Promise<void> {
// This promise should **not** reject, as we're handling errors in the observable for the Call
// the promise is only needed to signal when writing/draining has been completed
return new Promise((resolve, _doNotUse) => {
const valuesWaitingToBeDrained: T[] = [];
let shouldErrorAfterDraining = false;
let error: any;
let shouldResolveAfterDraining = false;
let writing = true;
// Used to manage finalization
const subscription = new Subscription();
// If the call is cancelled, unsubscribe from the source
const cancelHandler = () => {
subscription.unsubscribe();
// Calls that are cancelled by the client should be successfully resolved here
resolve();
};
call.on(CANCELLED_EVENT, cancelHandler);
subscription.add(() => call.off(CANCELLED_EVENT, cancelHandler));
// In all cases, when we finalize, end the writable stream
// being careful that errors and writes must be emitted _before_ this call is ended
subscription.add(() => call.end());
const drain = () => {
writing = true;
while (valuesWaitingToBeDrained.length > 0) {
const value = valuesWaitingToBeDrained.shift();
if (writing) {
// The first time `call.write` returns false, we need to stop.
// It wrote the value, but it won't write anything else.
writing = call.write(value);
if (!writing) {
// We can't write anymore so we need to wait for the drain event
return;
}
}
}
if (shouldResolveAfterDraining) {
subscription.unsubscribe();
resolve();
} else if (shouldErrorAfterDraining) {
call.emit('error', error);
subscription.unsubscribe();
resolve();
}
};
call.on('drain', drain);
subscription.add(() => call.off('drain', drain));
subscription.add(
source.subscribe({
next(value) {
if (writing) {
writing = call.write(value);
} else {
// If we can't write, that's because we need to
// wait for the drain event before we can write again
// buffer the value and wait for the drain event
valuesWaitingToBeDrained.push(value);
}
},
error(err) {
if (valuesWaitingToBeDrained.length === 0) {
// We're not waiting for a drain event, so we can just
// reject and teardown.
call.emit('error', err);
subscription.unsubscribe();
resolve();
} else {
// We're waiting for a drain event, record the
// error so it can be handled after everything is drained.
shouldErrorAfterDraining = true;
error = err;
}
},
complete() {
if (valuesWaitingToBeDrained.length === 0) {
// We're not waiting for a drain event, so we can just
// resolve and teardown.
subscription.unsubscribe();
resolve();
} else {
shouldResolveAfterDraining = true;
}
},
}),
);
});
}
public createRequestStreamMethod(
methodHandler: Function,
isResponseStream: boolean,
) {
return async (
call: GrpcCall,
callback: (err: unknown, value: unknown) => void,
) => {
// Needs to be a Proxy in order to buffer messages that come before handler is executed
// This could happen if handler has any async guards or interceptors registered that would delay
// the execution.
const { subject, next, error, complete, cleanup } =
this.bufferUntilDrained();
call.on('data', (m: any) => next(m));
call.on('error', (e: any) => {
// Check if error means that stream ended on other end
const isCancelledError = String(e).toLowerCase().indexOf('cancelled');
if (isCancelledError) {
call.end();
return;
}
// If another error then just pass it along
error(e);
});
call.on('end', () => {
complete();
cleanup();
});
const handler = methodHandler(
subject.asObservable(),
call.metadata,
call,
);
const res = this.transformToObservable(await handler);
if (isResponseStream) {
await this.writeObservableToGrpc(res, call);
} else {
const response = await lastValueFrom(
res.pipe(
takeUntil(fromEvent(call as any, CANCELLED_EVENT)),
catchError(err => {
callback(err, null);
return EMPTY;
}),
defaultIfEmpty(undefined),
),
);
if (!isUndefined(response)) {
callback(null, response);
}
}
};
}
public createStreamCallMethod(
methodHandler: Function,
isResponseStream: boolean,
) {
return async (
call: GrpcCall,
callback: (err: unknown, value: unknown) => void,
) => {
let handlerStream: Observable<any>;
if (isResponseStream) {
handlerStream = this.transformToObservable(await methodHandler(call));
} else {
handlerStream = this.transformToObservable(
await methodHandler(call, callback),
);
}
await lastValueFrom(handlerStream);
};
}
public async close(): Promise<void> {
if (this.grpcClient) {
const graceful = this.getOptionsProp(this.options, 'gracefulShutdown');
if (graceful) {
await new Promise<void>((resolve, reject) => {
this.grpcClient.tryShutdown((error: Error) => {
if (error) reject(error);
else resolve();
});
});
} else {
this.grpcClient.forceShutdown();
}
}
this.grpcClient = null;
}
public deserialize(obj: any): any {
try {
return JSON.parse(obj);
} catch (e) {
return obj;
}
}
public addHandler(
pattern: unknown,
callback: MessageHandler,
isEventHandler = false,
) {
const route = isString(pattern) ? pattern : JSON.stringify(pattern);
callback.isEventHandler = isEventHandler;
this.messageHandlers.set(route, callback);
}
public async createClient() {
const channelOptions: ChannelOptions =
this.options && this.options.channelOptions
? this.options.channelOptions
: {};
if (this.options && this.options.maxSendMessageLength) {
channelOptions['grpc.max_send_message_length'] =
this.options.maxSendMessageLength;
}
if (this.options && this.options.maxReceiveMessageLength) {
channelOptions['grpc.max_receive_message_length'] =
this.options.maxReceiveMessageLength;
}
if (this.options && this.options.maxMetadataSize) {
channelOptions['grpc.max_metadata_size'] = this.options.maxMetadataSize;
}
const server = new grpcPackage.Server(channelOptions);
const credentials = this.getOptionsProp(this.options, 'credentials');
await new Promise((resolve, reject) => {
server.bindAsync(
this.url,
credentials || grpcPackage.ServerCredentials.createInsecure(),
(error: Error | null, port: number) =>
error ? reject(error) : resolve(port),
);
});
return server;
}
public lookupPackage(root: any, packageName: string) {
/** Reference: https://github.com/kondi/rxjs-grpc */
let pkg = root;
for (const name of packageName.split(/\./)) {
pkg = pkg[name];
}
return pkg;
}
public loadProto(): any {
try {
const packageDefinition = getGrpcPackageDefinition(
this.options,
grpcProtoLoaderPackage,
);
if (this.options.onLoadPackageDefinition) {
this.options.onLoadPackageDefinition(
packageDefinition,
this.grpcClient,
);
}
return grpcPackage.loadPackageDefinition(packageDefinition);
} catch (err) {
const invalidProtoError = new InvalidProtoDefinitionException(err.path);
const message =
err && err.message ? err.message : invalidProtoError.message;
this.logger.error(message, invalidProtoError.stack);
throw invalidProtoError;
}
}
/**
* Recursively fetch all of the service methods available on loaded
* protobuf descriptor object, and collect those as an objects with
* dot-syntax full-path names.
*
* Example:
* for proto package Bundle.FirstService with service Events { rpc...
* will be resolved to object of (while loaded for Bundle package):
* {
* name: "FirstService.Events",
* service: {Object}
* }
*/
private collectDeepServices(
name: string,
grpcDefinition: any,
accumulator: { name: string; service: any }[],
) {
if (!isObject(grpcDefinition)) {
return;
}
const keysToTraverse = Object.keys(grpcDefinition);
// Traverse definitions or namespace extensions
for (const key of keysToTraverse) {
const nameExtended = this.parseDeepServiceName(name, key);
const deepDefinition = grpcDefinition[key];
const isServiceDefined =
deepDefinition && !isUndefined(deepDefinition.service);
const isServiceBoolean = isServiceDefined
? deepDefinition.service !== false
: false;
// grpc namespace object does not have 'format' or 'service' properties defined
const isFormatDefined =
deepDefinition && !isUndefined(deepDefinition.format);
if (isServiceDefined && isServiceBoolean) {
accumulator.push({
name: nameExtended,
service: deepDefinition,
});
} else if (isFormatDefined) {
// Do nothing
} else {
// Continue recursion for namespace object until objects end or service definition found
this.collectDeepServices(nameExtended, deepDefinition, accumulator);
}
}
}
private parseDeepServiceName(name: string, key: string): string {
// If depth is zero then just return key
if (name.length === 0) {
return key;
}
// Otherwise add next through dot syntax
return name + '.' + key;
}
private async createServices(grpcPkg: any, packageName: string) {
if (!grpcPkg) {
const invalidPackageError = new InvalidGrpcPackageException(packageName);
this.logger.error(invalidPackageError);
throw invalidPackageError;
}
// Take all of the services defined in grpcPkg and assign them to
// method handlers defined in Controllers
for (const definition of this.getServiceNames(grpcPkg)) {
this.grpcClient.addService(
// First parameter requires exact service definition from proto
definition.service.service,
// Here full proto definition required along with namespaced pattern name
await this.createService(definition.service, definition.name),
);
}
}
private bufferUntilDrained<T>() {
type DrainableSubject<T> = Subject<T> & { drainBuffer: () => void };
const subject = new Subject<T>();
let replayBuffer: ReplaySubject<T> | null = new ReplaySubject<T>();
let hasDrained = false;
function drainBuffer(this: DrainableSubject<T>) {
if (hasDrained || !replayBuffer) {
return;
}
hasDrained = true;
// Replay buffered values to the new subscriber
setImmediate(() => {
const subcription = replayBuffer!.subscribe(subject);
subcription.unsubscribe();
replayBuffer = null;
});
}
return {
subject: new Proxy<DrainableSubject<T>>(subject as DrainableSubject<T>, {
get(target, prop, receiver) {
if (prop === 'asObservable') {
return () => {
const stream = subject.asObservable();
// "drainBuffer" will be called before the evaluation of the handler
// but after any enhancers have been applied (e.g., `interceptors`)
Object.defineProperty(stream, drainBuffer.name, {
value: drainBuffer,
});
return stream;
};
}
if (hasDrained) {
return Reflect.get(target, prop, receiver);
}
return Reflect.get(replayBuffer!, prop, receiver);
},
}),
next: (value: T) => {
if (!hasDrained) {
replayBuffer!.next(value);
}
subject.next(value);
},
error: (err: any) => {
if (!hasDrained) {
replayBuffer!.error(err);
}
subject.error(err);
},
complete: () => {
if (!hasDrained) {
replayBuffer!.complete();
// Replay buffer is no longer needed
// Return early to allow subject to complete later, after the replay buffer
// has been drained
return;
}
subject.complete();
},
cleanup: () => {
if (hasDrained) {
return;
}
replayBuffer = null;
},
};
}
}