ts-proto 1.135.2 → 1.136.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.markdown +2 -0
- package/build/generate-grpc-web.js +7 -3
- package/build/generate-services.js +17 -7
- package/build/main.js +4 -4
- package/build/options.js +1 -0
- package/build/sourceInfo.js +5 -5
- package/build/utils.js +7 -7
- package/package.json +2 -2
package/README.markdown
CHANGED
|
@@ -412,6 +412,8 @@ Generated code will be placed in the Gradle build directory.
|
|
|
412
412
|
|
|
413
413
|
- With `--ts_proto_opt=outputServices=false`, or `=none`, ts-proto will output NO service definitions.
|
|
414
414
|
|
|
415
|
+
- With `--ts_proto_opt=useAbortSignal=true`, the generated services will accept an `AbortSignal` to cancel RPC calls.
|
|
416
|
+
|
|
415
417
|
- With `--ts_proto_opt=useAsyncIterable=true`, the generated services will use `AsyncIterable` instead of `Observable`.
|
|
416
418
|
|
|
417
419
|
- With `--ts_proto_opt=emitImportedFiles=false`, ts-proto will not emit `google/protobuf/*` files unless you explicit add files to `protoc` like this
|
|
@@ -18,7 +18,7 @@ function generateGrpcClientImpl(ctx, _fileDesc, serviceDesc) {
|
|
|
18
18
|
// Create the constructor(rpc: Rpc)
|
|
19
19
|
chunks.push((0, ts_poet_1.code) `
|
|
20
20
|
private readonly rpc: Rpc;
|
|
21
|
-
|
|
21
|
+
|
|
22
22
|
constructor(rpc: Rpc) {
|
|
23
23
|
`);
|
|
24
24
|
chunks.push((0, ts_poet_1.code) `this.rpc = rpc;`);
|
|
@@ -107,7 +107,11 @@ function generateGrpcMethodDesc(ctx, serviceDesc, methodDesc) {
|
|
|
107
107
|
// we want/what grpc-web's runtime needs.
|
|
108
108
|
const responseFn = (0, ts_poet_1.code) `{
|
|
109
109
|
deserializeBinary(data: Uint8Array) {
|
|
110
|
-
|
|
110
|
+
const value = ${outputType}.decode(data);
|
|
111
|
+
return {
|
|
112
|
+
...value,
|
|
113
|
+
toObject() { return value; },
|
|
114
|
+
};
|
|
111
115
|
}
|
|
112
116
|
}`;
|
|
113
117
|
return (0, ts_poet_1.code) `
|
|
@@ -217,7 +221,7 @@ function createPromiseUnaryMethod(ctx) {
|
|
|
217
221
|
debug: this.options.debug,
|
|
218
222
|
onEnd: function (response) {
|
|
219
223
|
if (response.status === grpc.Code.OK) {
|
|
220
|
-
resolve(response.message);
|
|
224
|
+
resolve(response.message!.toObject());
|
|
221
225
|
} else {
|
|
222
226
|
const err = new ${ctx.utils.GrpcWebError}(response.statusMessage, response.status, response.trailers);
|
|
223
227
|
reject(err);
|
|
@@ -39,6 +39,9 @@ function generateService(ctx, fileDesc, sourceInfo, serviceDesc) {
|
|
|
39
39
|
const partialInput = options.outputClientImpl === "grpc-web";
|
|
40
40
|
const inputType = (0, types_1.requestType)(ctx, methodDesc, partialInput);
|
|
41
41
|
params.push((0, ts_poet_1.code) `request: ${inputType}`);
|
|
42
|
+
if (options.useAbortSignal) {
|
|
43
|
+
params.push((0, ts_poet_1.code) `abortSignal?: AbortSignal`);
|
|
44
|
+
}
|
|
42
45
|
// Use metadata as last argument for interface only configuration
|
|
43
46
|
if (options.outputClientImpl === "grpc-web") {
|
|
44
47
|
// We have to use grpc.Metadata where grpc will come from @improbable-eng
|
|
@@ -72,16 +75,20 @@ function generateService(ctx, fileDesc, sourceInfo, serviceDesc) {
|
|
|
72
75
|
return (0, ts_poet_1.joinCode)(chunks, { on: "\n" });
|
|
73
76
|
}
|
|
74
77
|
exports.generateService = generateService;
|
|
75
|
-
function generateRegularRpcMethod(ctx,
|
|
78
|
+
function generateRegularRpcMethod(ctx, methodDesc) {
|
|
76
79
|
(0, utils_1.assertInstanceOf)(methodDesc, utils_1.FormattedMethodDescriptor);
|
|
77
|
-
const { options
|
|
80
|
+
const { options } = ctx;
|
|
78
81
|
const Reader = (0, utils_1.impFile)(ctx.options, "Reader@protobufjs/minimal");
|
|
79
82
|
const rawInputType = (0, types_1.rawRequestType)(ctx, methodDesc, { keepValueType: true });
|
|
80
83
|
const inputType = (0, types_1.requestType)(ctx, methodDesc);
|
|
81
|
-
const outputType = (0, types_1.responseType)(ctx, methodDesc);
|
|
82
84
|
const rawOutputType = (0, types_1.responseType)(ctx, methodDesc, { keepValueType: true });
|
|
83
|
-
const params = [
|
|
85
|
+
const params = [
|
|
86
|
+
...(options.context ? [(0, ts_poet_1.code) `ctx: Context`] : []),
|
|
87
|
+
(0, ts_poet_1.code) `request: ${inputType}`,
|
|
88
|
+
...(options.useAbortSignal ? [(0, ts_poet_1.code) `abortSignal?: AbortSignal`] : []),
|
|
89
|
+
];
|
|
84
90
|
const maybeCtx = options.context ? "ctx," : "";
|
|
91
|
+
const maybeAbortSignal = options.useAbortSignal ? "abortSignal || undefined," : "";
|
|
85
92
|
let encode = (0, ts_poet_1.code) `${rawInputType}.encode(request).finish()`;
|
|
86
93
|
let decode = (0, ts_poet_1.code) `data => ${rawOutputType}.decode(new ${Reader}(data))`;
|
|
87
94
|
// if (options.useDate && rawOutputType.toString().includes("Timestamp")) {
|
|
@@ -131,7 +138,8 @@ function generateRegularRpcMethod(ctx, fileDesc, serviceDesc, methodDesc) {
|
|
|
131
138
|
${maybeCtx}
|
|
132
139
|
this.service,
|
|
133
140
|
"${methodDesc.name}",
|
|
134
|
-
data
|
|
141
|
+
data,
|
|
142
|
+
${maybeAbortSignal}
|
|
135
143
|
);
|
|
136
144
|
return ${decode};
|
|
137
145
|
}
|
|
@@ -172,7 +180,7 @@ function generateServiceClientImpl(ctx, fileDesc, serviceDesc) {
|
|
|
172
180
|
chunks.push(generateCachingRpcMethod(ctx, fileDesc, serviceDesc, methodDesc));
|
|
173
181
|
}
|
|
174
182
|
else {
|
|
175
|
-
chunks.push(generateRegularRpcMethod(ctx,
|
|
183
|
+
chunks.push(generateRegularRpcMethod(ctx, methodDesc));
|
|
176
184
|
}
|
|
177
185
|
}
|
|
178
186
|
chunks.push((0, ts_poet_1.code) `}`);
|
|
@@ -267,6 +275,7 @@ function generateRpcType(ctx, hasStreamingMethods) {
|
|
|
267
275
|
const { options } = ctx;
|
|
268
276
|
const maybeContext = options.context ? "<Context>" : "";
|
|
269
277
|
const maybeContextParam = options.context ? "ctx: Context," : "";
|
|
278
|
+
const maybeAbortSignalParam = options.useAbortSignal ? "abortSignal?: AbortSignal," : "";
|
|
270
279
|
const methods = [[(0, ts_poet_1.code) `request`, (0, ts_poet_1.code) `Uint8Array`, (0, ts_poet_1.code) `Promise<Uint8Array>`]];
|
|
271
280
|
if (hasStreamingMethods) {
|
|
272
281
|
const observable = (0, types_1.observableType)(ctx);
|
|
@@ -286,7 +295,8 @@ function generateRpcType(ctx, hasStreamingMethods) {
|
|
|
286
295
|
${maybeContextParam}
|
|
287
296
|
service: string,
|
|
288
297
|
method: string,
|
|
289
|
-
data: ${method[1]}
|
|
298
|
+
data: ${method[1]},
|
|
299
|
+
${maybeAbortSignalParam}
|
|
290
300
|
): ${method[2]};`);
|
|
291
301
|
});
|
|
292
302
|
chunks.push((0, ts_poet_1.code) ` }`);
|
package/build/main.js
CHANGED
|
@@ -74,7 +74,7 @@ function generateFile(ctx, fileDesc) {
|
|
|
74
74
|
}
|
|
75
75
|
if (options.outputEncodeMethods || options.outputJsonMethods || options.outputTypeRegistry) {
|
|
76
76
|
// then add the encoder/decoder/base instance
|
|
77
|
-
(0, visit_1.visit)(fileDesc, sourceInfo, (fullName, message,
|
|
77
|
+
(0, visit_1.visit)(fileDesc, sourceInfo, (fullName, message, _sInfo, fullProtoTypeName) => {
|
|
78
78
|
const fullTypeName = (0, utils_1.maybePrefixPackage)(fileDesc, fullProtoTypeName);
|
|
79
79
|
chunks.push(generateBaseInstanceFactory(ctx, fullName, message, fullTypeName));
|
|
80
80
|
const staticMembers = [];
|
|
@@ -170,7 +170,7 @@ function generateFile(ctx, fileDesc) {
|
|
|
170
170
|
}
|
|
171
171
|
});
|
|
172
172
|
}
|
|
173
|
-
serviceDesc.method.forEach((methodDesc,
|
|
173
|
+
serviceDesc.method.forEach((methodDesc, _index) => {
|
|
174
174
|
if (methodDesc.serverStreaming || methodDesc.clientStreaming) {
|
|
175
175
|
hasStreamingMethods = true;
|
|
176
176
|
}
|
|
@@ -220,7 +220,7 @@ function makeUtils(options) {
|
|
|
220
220
|
return {
|
|
221
221
|
...bytes,
|
|
222
222
|
...makeDeepPartial(options, longs),
|
|
223
|
-
...makeObjectIdMethods(
|
|
223
|
+
...makeObjectIdMethods(),
|
|
224
224
|
...makeTimestampMethods(options, longs),
|
|
225
225
|
...longs,
|
|
226
226
|
...makeComparisonUtils(),
|
|
@@ -375,7 +375,7 @@ function makeDeepPartial(options, longs) {
|
|
|
375
375
|
`);
|
|
376
376
|
return { Builtin, DeepPartial, Exact };
|
|
377
377
|
}
|
|
378
|
-
function makeObjectIdMethods(
|
|
378
|
+
function makeObjectIdMethods() {
|
|
379
379
|
const mongodb = (0, ts_poet_1.imp)("mongodb*mongodb");
|
|
380
380
|
const fromProtoObjectId = (0, ts_poet_1.conditionalOutput)("fromProtoObjectId", (0, ts_poet_1.code) `
|
|
381
381
|
function fromProtoObjectId(oid: ObjectId): ${mongodb}.ObjectId {
|
package/build/options.js
CHANGED
package/build/sourceInfo.js
CHANGED
|
@@ -45,11 +45,6 @@ class EmptyDescription {
|
|
|
45
45
|
* defined in a protocol buffer.
|
|
46
46
|
*/
|
|
47
47
|
class SourceInfo {
|
|
48
|
-
// Private
|
|
49
|
-
constructor(sourceCode, selfDescription) {
|
|
50
|
-
this.sourceCode = sourceCode;
|
|
51
|
-
this.selfDescription = selfDescription;
|
|
52
|
-
}
|
|
53
48
|
/** Returns an empty SourceInfo */
|
|
54
49
|
static empty() {
|
|
55
50
|
return new SourceInfo({}, new EmptyDescription());
|
|
@@ -68,6 +63,11 @@ class SourceInfo {
|
|
|
68
63
|
}
|
|
69
64
|
return new SourceInfo(map, new EmptyDescription());
|
|
70
65
|
}
|
|
66
|
+
// Private
|
|
67
|
+
constructor(sourceCode, selfDescription) {
|
|
68
|
+
this.sourceCode = sourceCode;
|
|
69
|
+
this.selfDescription = selfDescription;
|
|
70
|
+
}
|
|
71
71
|
/** Returns the code span [start line, start column, end line] */
|
|
72
72
|
get span() {
|
|
73
73
|
return this.selfDescription.span;
|
package/build/utils.js
CHANGED
|
@@ -105,6 +105,13 @@ exports.assertInstanceOf = assertInstanceOf;
|
|
|
105
105
|
* A MethodDescriptorProto subclass that adds formatted properties
|
|
106
106
|
*/
|
|
107
107
|
class FormattedMethodDescriptor {
|
|
108
|
+
/**
|
|
109
|
+
* The name of this method with formatting applied according to the `Options` object passed to the constructor.
|
|
110
|
+
* Automatically updates to any changes to the `Options` or `name` of this object
|
|
111
|
+
*/
|
|
112
|
+
get formattedName() {
|
|
113
|
+
return FormattedMethodDescriptor.formatName(this.name, this.ctxOptions);
|
|
114
|
+
}
|
|
108
115
|
constructor(src, options) {
|
|
109
116
|
this.ctxOptions = options;
|
|
110
117
|
this.original = src;
|
|
@@ -115,13 +122,6 @@ class FormattedMethodDescriptor {
|
|
|
115
122
|
this.clientStreaming = src.clientStreaming;
|
|
116
123
|
this.serverStreaming = src.serverStreaming;
|
|
117
124
|
}
|
|
118
|
-
/**
|
|
119
|
-
* The name of this method with formatting applied according to the `Options` object passed to the constructor.
|
|
120
|
-
* Automatically updates to any changes to the `Options` or `name` of this object
|
|
121
|
-
*/
|
|
122
|
-
get formattedName() {
|
|
123
|
-
return FormattedMethodDescriptor.formatName(this.name, this.ctxOptions);
|
|
124
|
-
}
|
|
125
125
|
/**
|
|
126
126
|
* Retrieve the source `MethodDescriptorProto` used to construct this object
|
|
127
127
|
* @returns The source `MethodDescriptorProto` used to construct this object
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-proto",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.136.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "build/plugin.js",
|
|
6
6
|
"repository": "github:stephenh/ts-proto",
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
"semantic-release": "^19.0.3",
|
|
56
56
|
"ts-jest": "^28.0.5",
|
|
57
57
|
"tsx": "^3.8.2",
|
|
58
|
-
"typescript": "^4.
|
|
58
|
+
"typescript": "^4.9.4",
|
|
59
59
|
"uglify-js": "^3.16.1"
|
|
60
60
|
},
|
|
61
61
|
"dependencies": {
|