forked from stephenh/ts-proto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-services.ts
492 lines (445 loc) · 17 KB
/
generate-services.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
import { MethodDescriptorProto, FileDescriptorProto, ServiceDescriptorProto } from "ts-proto-descriptors";
import { Code, code, def, imp, joinCode } from "ts-poet";
import {
BatchMethod,
detectBatchMethod,
requestType,
rawRequestType,
responsePromiseOrObservable,
responseType,
observableType,
} from "./types";
import {
arrowFunction,
assertInstanceOf,
FormattedMethodDescriptor,
impFile,
maybeAddComment,
maybePrefixPackage,
singular,
tryCatchBlock,
} from "./utils";
import SourceInfo, { Fields } from "./sourceInfo";
import { contextTypeVar } from "./main";
import { Context } from "./context";
/**
* Generates an interface for `serviceDesc`.
*
* Some RPC frameworks (i.e. Twirp) can use the same interface, i.e.
* `getFoo(req): Promise<res>` for the client-side and server-side,
* which is the intent for this interface.
*
* Other RPC frameworks (i.e. NestJS) that need different client-side
* vs. server-side code/interfaces are handled separately.
*/
export function generateService(
ctx: Context,
fileDesc: FileDescriptorProto,
sourceInfo: SourceInfo,
serviceDesc: ServiceDescriptorProto,
): Code {
const { options } = ctx;
const chunks: Code[] = [];
maybeAddComment(options, sourceInfo, chunks, serviceDesc.options?.deprecated);
const maybeTypeVar = options.context ? `<${contextTypeVar}>` : "";
chunks.push(code`export interface ${def(serviceDesc.name)}${maybeTypeVar} {`);
serviceDesc.method.forEach((methodDesc, index) => {
assertInstanceOf(methodDesc, FormattedMethodDescriptor);
const info = sourceInfo.lookup(Fields.service.method, index);
maybeAddComment(options, info, chunks, methodDesc.options?.deprecated);
const params: Code[] = [];
if (options.context) {
params.push(code`ctx: Context`);
}
// the grpc-web clients auto-`fromPartial` the input before handing off to grpc-web's
// serde runtime, so it's okay to accept partial results from the client
const partialInput = options.outputClientImpl === "grpc-web";
const inputType = requestType(ctx, methodDesc, partialInput);
params.push(code`request: ${inputType}`);
// Use metadata as last argument for interface only configuration
if (options.outputClientImpl === "grpc-web") {
// We have to use grpc.Metadata where grpc will come from @improbable-eng
params.push(code`metadata?: grpc.Metadata`);
} else if (options.metadataType) {
// custom `metadataType` has precedence over `addGrpcMetadata` that injects Metadata from grpc-js
const Metadata = imp(options.metadataType);
params.push(code`metadata?: ${Metadata}`);
} else if (options.addGrpcMetadata) {
const Metadata = imp("Metadata@@grpc/grpc-js");
params.push(code`metadata?: ${Metadata}`);
}
if (options.useAbortSignal) {
params.push(code`abortSignal?: AbortSignal`);
}
if (options.addNestjsRestParameter) {
params.push(code`...rest: any`);
}
chunks.push(
code`${methodDesc.formattedName}(${joinCode(params, { on: "," })}): ${responsePromiseOrObservable(
ctx,
methodDesc,
)};`,
);
// If this is a batch method, auto-generate the singular version of it
if (options.context) {
const batchMethod = detectBatchMethod(ctx, fileDesc, serviceDesc, methodDesc);
if (batchMethod) {
chunks.push(code`${batchMethod.singleMethodName}(
ctx: Context,
${singular(batchMethod.inputFieldName)}: ${batchMethod.inputType},
): Promise<${batchMethod.outputType}>;`);
}
}
});
chunks.push(code`}`);
return joinCode(chunks, { on: "\n" });
}
function generateRegularRpcMethod(ctx: Context, methodDesc: MethodDescriptorProto): Code {
assertInstanceOf(methodDesc, FormattedMethodDescriptor);
const { options } = ctx;
const Reader = impFile(ctx.options, "Reader@protobufjs/minimal");
const rawInputType = rawRequestType(ctx, methodDesc, { keepValueType: true });
const inputType = requestType(ctx, methodDesc);
const rawOutputType = responseType(ctx, methodDesc, { keepValueType: true });
const metadataType = options.metadataType ? imp(options.metadataType) : imp("Metadata@@grpc/grpc-js");
const params = [
...(options.context ? [code`ctx: Context`] : []),
code`request: ${inputType}`,
...(options.metadataType || options.addGrpcMetadata ? [code`metadata?: ${metadataType}`] : []),
...(options.useAbortSignal ? [code`abortSignal?: AbortSignal`] : []),
];
const maybeCtx = options.context ? "ctx," : "";
const maybeMetadata = options.addGrpcMetadata ? "metadata," : "";
const maybeAbortSignal = options.useAbortSignal ? "abortSignal || undefined," : "";
let errorHandler;
if (options.rpcErrorHandler) {
errorHandler = code`
if (this.rpc.handleError) {
return Promise.reject(this.rpc.handleError(this.service, "${methodDesc.name}", error));
}
return Promise.reject(error);
`;
}
let encode = code`${rawInputType}.encode(request).finish()`;
let beforeRequest;
if (options.rpcBeforeRequest && !methodDesc.clientStreaming) {
beforeRequest = generateBeforeRequest(methodDesc.name);
} else if (methodDesc.clientStreaming && options.rpcBeforeRequest) {
encode = code`{const encodedRequest = ${encode}; ${generateBeforeRequest(
methodDesc.name,
"encodedRequest",
)}; return encodedRequest}`;
}
let decode = code`${rawOutputType}.decode(${Reader}.create(data))`;
if (options.rpcAfterResponse) {
decode = code`
const response = ${rawOutputType}.decode(${Reader}.create(data));
if (this.rpc.afterResponse) {
this.rpc.afterResponse(this.service, "${methodDesc.name}", response);
}
return response;
`;
}
// if (options.useDate && rawOutputType.toString().includes("Timestamp")) {
// decode = code`data => ${utils.fromTimestamp}(${rawOutputType}.decode(${Reader}.create(data)))`;
// }
if (methodDesc.clientStreaming) {
if (options.useAsyncIterable) {
encode = code`${rawInputType}.encodeTransform(request)`;
} else {
encode = code`request.pipe(${imp("map@rxjs/operators")}(request => ${encode}))`;
}
}
const returnStatement = createDefaultServiceReturn(ctx, methodDesc, decode, errorHandler);
let returnVariable: string;
if (options.returnObservable || methodDesc.serverStreaming) {
returnVariable = "result";
} else {
returnVariable = "promise";
}
let rpcMethod: string;
if (methodDesc.clientStreaming && methodDesc.serverStreaming) {
rpcMethod = "bidirectionalStreamingRequest";
} else if (methodDesc.serverStreaming) {
rpcMethod = "serverStreamingRequest";
} else if (methodDesc.clientStreaming) {
rpcMethod = "clientStreamingRequest";
} else {
rpcMethod = "request";
}
return code`
${methodDesc.formattedName}(
${joinCode(params, { on: "," })}
): ${responsePromiseOrObservable(ctx, methodDesc)} {
const data = ${encode}; ${beforeRequest ? beforeRequest : ""}
const ${returnVariable} = this.rpc.${rpcMethod}(
${maybeCtx}
this.service,
"${methodDesc.name}",
data,
${maybeMetadata}
${maybeAbortSignal}
);
return ${returnStatement};
}
`;
}
function generateBeforeRequest(methodName: string, requestVariableName: string = "request") {
return code`
if (this.rpc.beforeRequest) {
this.rpc.beforeRequest(this.service, "${methodName}", ${requestVariableName});
}`;
}
function createDefaultServiceReturn(
ctx: Context,
methodDesc: MethodDescriptorProto,
decode: Code,
errorHandler?: Code,
): Code {
const { options } = ctx;
const rawOutputType = responseType(ctx, methodDesc, { keepValueType: true });
const returnStatement = arrowFunction("data", decode, !options.rpcAfterResponse);
if (options.returnObservable || methodDesc.serverStreaming) {
if (options.useAsyncIterable) {
return code`${rawOutputType}.decodeTransform(result)`;
} else {
if (errorHandler) {
const tc = arrowFunction("data", tryCatchBlock(decode, code`throw error`), !options.rpcAfterResponse);
return code`result.pipe(${imp("map@rxjs/operators")}(${tc}))`;
}
return code`result.pipe(${imp("map@rxjs/operators")}(${returnStatement}))`;
}
}
if (errorHandler) {
if (!options.rpcAfterResponse) {
decode = code`return ${decode}`;
}
return code`promise.then(${arrowFunction(
"data",
tryCatchBlock(decode, code`return Promise.reject(error);`),
false,
)}).catch(${arrowFunction("error", errorHandler, false)})`;
}
return code`promise.then(${returnStatement})`;
}
export function generateServiceClientImpl(
ctx: Context,
fileDesc: FileDescriptorProto,
serviceDesc: ServiceDescriptorProto,
): Code {
const { options } = ctx;
const chunks: Code[] = [];
// Determine information about the service.
const { name } = serviceDesc;
const serviceName = maybePrefixPackage(fileDesc, serviceDesc.name);
// Define the service name constant.
const serviceNameConst = `${name}ServiceName`;
chunks.push(code`export const ${serviceNameConst} = "${serviceName}";`);
// Define the FooServiceImpl class
const i = options.context ? `${name}<Context>` : name;
const t = options.context ? `<${contextTypeVar}>` : "";
chunks.push(code`export class ${name}ClientImpl${t} implements ${def(i)} {`);
// Create the constructor(rpc: Rpc)
const rpcType = options.context ? "Rpc<Context>" : "Rpc";
chunks.push(code`private readonly rpc: ${rpcType};`);
chunks.push(code`private readonly service: string;`);
chunks.push(code`constructor(rpc: ${rpcType}, opts?: {service?: string}) {`);
chunks.push(code`this.service = opts?.service || ${serviceNameConst};`);
chunks.push(code`this.rpc = rpc;`);
// Bind each FooService method to the FooServiceImpl class
for (const methodDesc of serviceDesc.method) {
assertInstanceOf(methodDesc, FormattedMethodDescriptor);
chunks.push(code`this.${methodDesc.formattedName} = this.${methodDesc.formattedName}.bind(this);`);
}
chunks.push(code`}`);
// Create a method for each FooService method
for (const methodDesc of serviceDesc.method) {
// See if this this fuzzy matches to a batchable method
if (options.context) {
const batchMethod = detectBatchMethod(ctx, fileDesc, serviceDesc, methodDesc);
if (batchMethod) {
chunks.push(generateBatchingRpcMethod(ctx, batchMethod));
}
}
if (options.context && methodDesc.name.match(/^Get[A-Z]/)) {
chunks.push(generateCachingRpcMethod(ctx, fileDesc, serviceDesc, methodDesc));
} else {
chunks.push(generateRegularRpcMethod(ctx, methodDesc));
}
}
chunks.push(code`}`);
return code`${chunks}`;
}
/** We've found a BatchXxx method, create a synthetic GetXxx method that calls it. */
function generateBatchingRpcMethod(ctx: Context, batchMethod: BatchMethod): Code {
const {
methodDesc,
singleMethodName,
inputFieldName,
inputType,
outputFieldName,
outputType,
mapType,
uniqueIdentifier,
} = batchMethod;
assertInstanceOf(methodDesc, FormattedMethodDescriptor);
const { options } = ctx;
const hash = options.esModuleInterop ? imp("hash=object-hash") : imp("hash*object-hash");
const dataloader = options.esModuleInterop ? imp("DataLoader=dataloader") : imp("DataLoader*dataloader");
// Create the `(keys) => ...` lambda we'll pass to the DataLoader constructor
const lambda: Code[] = [];
lambda.push(code`
(${inputFieldName}) => {
const request = { ${inputFieldName} };
`);
if (mapType) {
// If the return type is a map, lookup each key in the result
lambda.push(code`
return this.${methodDesc.formattedName}(ctx, request as any).then(res => {
return ${inputFieldName}.map(key => res.${outputFieldName}[key] ?? ${ctx.utils.fail}())
});
`);
} else {
// Otherwise assume they come back in order
lambda.push(code`
return this.${methodDesc.formattedName}(ctx, request as any).then(res => res.${outputFieldName})
`);
}
lambda.push(code`}`);
return code`
${singleMethodName}(
ctx: Context,
${singular(inputFieldName)}: ${inputType}
): Promise<${outputType}> {
const dl = ctx.getDataLoader("${uniqueIdentifier}", () => {
return new ${dataloader}<${inputType}, ${outputType}, string>(
${joinCode(lambda)},
{ cacheKeyFn: ${hash}, ...ctx.rpcDataLoaderOptions }
);
});
return dl.load(${singular(inputFieldName)});
}
`;
}
/** We're not going to batch, but use DataLoader for per-request caching. */
function generateCachingRpcMethod(
ctx: Context,
fileDesc: FileDescriptorProto,
serviceDesc: ServiceDescriptorProto,
methodDesc: MethodDescriptorProto,
): Code {
assertInstanceOf(methodDesc, FormattedMethodDescriptor);
const { options } = ctx;
const hash = options.esModuleInterop ? imp("hash=object-hash") : imp("hash*object-hash");
const dataloader = options.esModuleInterop ? imp("DataLoader=dataloader") : imp("DataLoader*dataloader");
const inputType = requestType(ctx, methodDesc);
const outputType = responseType(ctx, methodDesc);
const uniqueIdentifier = `${maybePrefixPackage(fileDesc, serviceDesc.name)}.${methodDesc.name}`;
const Reader = impFile(ctx.options, "Reader@protobufjs/minimal");
const lambda = code`
(requests) => {
const responses = requests.map(async request => {
const data = ${inputType}.encode(request).finish()
const response = await this.rpc.request(ctx, "${maybePrefixPackage(fileDesc, serviceDesc.name)}", "${
methodDesc.name
}", data);
return ${outputType}.decode(${Reader}.create(response));
});
return Promise.all(responses);
}
`;
return code`
${methodDesc.formattedName}(
ctx: Context,
request: ${inputType},
): Promise<${outputType}> {
const dl = ctx.getDataLoader("${uniqueIdentifier}", () => {
return new ${dataloader}<${inputType}, ${outputType}, string>(
${lambda},
{ cacheKeyFn: ${hash}, ...ctx.rpcDataLoaderOptions },
);
});
return dl.load(request);
}
`;
}
/**
* Creates an `Rpc.request(service, method, data)` abstraction.
*
* This lets clients pass in their own request-promise-ish client.
*
* This also requires clientStreamingRequest, serverStreamingRequest and
* bidirectionalStreamingRequest methods if any of the RPCs is streaming.
*
* We don't export this because if a project uses multiple `*.proto` files,
* we don't want our the barrel imports in `index.ts` to have multiple `Rpc`
* types.
*/
export function generateRpcType(ctx: Context, hasStreamingMethods: boolean): Code {
const { options } = ctx;
const metadata = options.metadataType ? imp(options.metadataType) : imp("Metadata@@grpc/grpc-js");
const metadataType = metadata.symbol;
const maybeContext = options.context ? "<Context>" : "";
const maybeContextParam = options.context ? "ctx: Context," : "";
const maybeMetadataParam = options.metadataType || options.addGrpcMetadata ? `metadata?: ${metadataType},` : "";
const maybeAbortSignalParam = options.useAbortSignal ? "abortSignal?: AbortSignal," : "";
const methods = [[code`request`, code`Uint8Array`, code`Promise<Uint8Array>`]];
const additionalMethods = [];
if (options.rpcBeforeRequest) {
additionalMethods.push(
code`beforeRequest?<T extends { [k in keyof T]: unknown }>(service: string, method: string, request: T): void;`,
);
}
if (options.rpcAfterResponse) {
additionalMethods.push(
code`afterResponse?<T extends { [k in keyof T]: unknown }>(service: string, method: string, response: T): void;`,
);
}
if (options.rpcErrorHandler) {
additionalMethods.push(
code`handleError?(service: string, method: string, error: globalThis.Error): globalThis.Error;`,
);
}
if (hasStreamingMethods) {
const observable = observableType(ctx, true);
methods.push([code`clientStreamingRequest`, code`${observable}<Uint8Array>`, code`Promise<Uint8Array>`]);
methods.push([code`serverStreamingRequest`, code`Uint8Array`, code`${observable}<Uint8Array>`]);
methods.push([
code`bidirectionalStreamingRequest`,
code`${observable}<Uint8Array>`,
code`${observable}<Uint8Array>`,
]);
}
const chunks: Code[] = [];
chunks.push(code` interface Rpc${maybeContext} {`);
methods.forEach((method) => {
chunks.push(code`
${method[0]}(
${maybeContextParam}
service: string,
method: string,
data: ${method[1]},
${maybeMetadataParam}
${maybeAbortSignalParam}
): ${method[2]};`);
});
additionalMethods.forEach((method) => chunks.push(method));
chunks.push(code` }`);
return joinCode(chunks, { on: "\n" });
}
export function generateDataLoadersType(): Code {
// TODO Maybe should be a generic `Context.get<T>(id, () => T): T` method
return code`
export interface DataLoaders {
rpcDataLoaderOptions?: DataLoaderOptions;
getDataLoader<T>(identifier: string, constructorFn: () => T): T;
}
`;
}
export function generateDataLoaderOptionsType(): Code {
return code`
export interface DataLoaderOptions {
cache?: boolean;
}
`;
}