|
1 | 1 | import * as grpc from 'grpc';
|
2 | 2 |
|
3 | 3 | import {
|
| 4 | + addServerBuildMethods, |
4 | 5 | ClientFactoryConstructor,
|
5 | 6 | createService,
|
6 | 7 | createServiceClient,
|
7 | 8 | DynamicMethods,
|
| 9 | + GenericServerBuilder, |
8 | 10 | getServiceNames,
|
9 | 11 | grpcLoad,
|
10 | 12 | GrpcService,
|
11 | 13 | lookupPackage,
|
12 | 14 | } from './utils';
|
13 | 15 |
|
14 |
| -export { ClientFactoryConstructor } from './utils'; |
15 |
| - |
16 |
| -export interface GenericServerBuilder<T> { |
17 |
| - start(address: string, credentials?: grpc.ServerCredentials): void; |
18 |
| - forceShutdown(): void; |
19 |
| -} |
| 16 | +export { ClientFactoryConstructor, GenericServerBuilder } from './utils'; |
20 | 17 |
|
21 | 18 | export function serverBuilder<T>(
|
22 | 19 | protoPath: string,
|
23 | 20 | packageName: string,
|
24 | 21 | server = new grpc.Server(),
|
25 | 22 | ): T & GenericServerBuilder<T> {
|
26 |
| - const builder: DynamicMethods = <GenericServerBuilder<T>>{ |
27 |
| - start(address, credentials) { |
28 |
| - server.bind(address, credentials || grpc.ServerCredentials.createInsecure()); |
29 |
| - server.start(); |
30 |
| - }, |
31 |
| - forceShutdown() { |
32 |
| - server.forceShutdown(); |
33 |
| - }, |
34 |
| - }; |
35 |
| - |
| 23 | + const adders: DynamicMethods = {}; |
36 | 24 | const pkg = lookupPackage(grpcLoad(protoPath), packageName);
|
37 | 25 | for (const name of getServiceNames(pkg)) {
|
38 |
| - builder[`add${name}`] = function(rxImpl: DynamicMethods) { |
| 26 | + adders[`add${name}`] = function(rxImpl: DynamicMethods) { |
39 | 27 | const serviceData = (pkg[name] as any) as GrpcService<any>;
|
40 | 28 | server.addService(serviceData.service, createService(serviceData, rxImpl));
|
41 | 29 | return this;
|
42 | 30 | };
|
43 | 31 | }
|
44 |
| - |
45 |
| - return builder as any; |
| 32 | + return addServerBuildMethods(adders, server) as any; |
46 | 33 | }
|
47 | 34 |
|
48 | 35 | export function clientFactory<T>(protoPath: string, packageName: string) {
|
|
0 commit comments