ts-proto 1.147.2 → 1.148.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 +5 -1
- package/build/generate-grpc-web.js +9 -13
- package/build/main.js +4 -4
- package/build/options.js +4 -0
- package/build/plugin.js +6 -0
- package/build/utils.js +36 -1
- package/package.json +1 -1
package/README.markdown
CHANGED
|
@@ -78,7 +78,7 @@ It will also generate client implementations of `PingService`; currently [Twirp]
|
|
|
78
78
|
- `npm install ts-proto`
|
|
79
79
|
- `protoc --plugin=./node_modules/.bin/protoc-gen-ts_proto --ts_proto_out=. ./simple.proto`
|
|
80
80
|
- (Note that the output parameter name, `ts_proto_out`, is named based on the suffix of the plugin's name, i.e. "ts_proto" suffix in the `--plugin=./node_modules/.bin/protoc-gen-ts_proto` parameter becomes the `_out` prefix, per `protoc`'s CLI conventions.)
|
|
81
|
-
- On Windows, use `protoc --plugin=protoc-gen-ts_proto
|
|
81
|
+
- On Windows, use `protoc --plugin=protoc-gen-ts_proto=".\\node_modules\\.bin\\protoc-gen-ts_proto.cmd" --ts_proto_out=. ./simple.proto` (see [#93](https://github.com/stephenh/ts-proto/issues/93))
|
|
82
82
|
- Ensure you're using a modern `protoc`, i.e. the original `protoc` `3.0.0` doesn't support the `_opt` flag
|
|
83
83
|
|
|
84
84
|
This will generate `*.ts` source files for the given `*.proto` types.
|
|
@@ -509,6 +509,10 @@ Generated code will be placed in the Gradle build directory.
|
|
|
509
509
|
Extension encode/decode methods are compliant with the `outputEncodeMethods` option, and if `unknownFields=true`,
|
|
510
510
|
the `setExtension` and `getExtension` methods will be created for extendable messages, also compliant with `outputEncodeMethods` (setExtension = encode, getExtension = decode).
|
|
511
511
|
|
|
512
|
+
- With `--ts_proto_opt=outputIndex=true`, index files will be generated based on the proto package namespaces.
|
|
513
|
+
|
|
514
|
+
This will disable `exportCommonSymbols` to avoid name collisions on the common symbols.
|
|
515
|
+
|
|
512
516
|
### NestJS Support
|
|
513
517
|
|
|
514
518
|
We have a great way of working together with [nestjs](https://docs.nestjs.com/microservices/grpc). `ts-proto` generates `interfaces` and `decorators` for you controller, client. For more information see the [nestjs readme](NESTJS.markdown).
|
|
@@ -214,12 +214,10 @@ function createPromiseUnaryMethod(ctx) {
|
|
|
214
214
|
const { useAbortSignal } = options;
|
|
215
215
|
const maybeAbortSignal = useAbortSignal
|
|
216
216
|
? `
|
|
217
|
-
|
|
217
|
+
if (abortSignal) abortSignal.addEventListener("abort", () => {
|
|
218
218
|
client.close();
|
|
219
|
-
reject(
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
if (abortSignal) abortSignal.addEventListener("abort", abortHandler);`
|
|
219
|
+
reject(abortSignal.reason);
|
|
220
|
+
});`
|
|
223
221
|
: "";
|
|
224
222
|
return (0, ts_poet_1.code) `
|
|
225
223
|
unary<T extends UnaryMethodDefinitionish>(
|
|
@@ -260,11 +258,10 @@ function createObservableUnaryMethod(ctx) {
|
|
|
260
258
|
const { useAbortSignal } = options;
|
|
261
259
|
const maybeAbortSignal = useAbortSignal
|
|
262
260
|
? `
|
|
263
|
-
|
|
264
|
-
observer.error(
|
|
261
|
+
if (abortSignal) abortSignal.addEventListener("abort", () => {
|
|
262
|
+
observer.error(abortSignal.reason);
|
|
265
263
|
client.close();
|
|
266
|
-
}
|
|
267
|
-
if (abortSignal) abortSignal.addEventListener("abort", abortHandler);`
|
|
264
|
+
});`
|
|
268
265
|
: "";
|
|
269
266
|
return (0, ts_poet_1.code) `
|
|
270
267
|
unary<T extends UnaryMethodDefinitionish>(
|
|
@@ -308,11 +305,10 @@ function createInvokeMethod(ctx) {
|
|
|
308
305
|
const { useAbortSignal } = options;
|
|
309
306
|
const maybeAbortSignal = useAbortSignal
|
|
310
307
|
? `
|
|
311
|
-
|
|
312
|
-
observer.error(
|
|
308
|
+
if (abortSignal) abortSignal.addEventListener("abort", () => {
|
|
309
|
+
observer.error(abortSignal.reason);
|
|
313
310
|
client.close();
|
|
314
|
-
}
|
|
315
|
-
if (abortSignal) abortSignal.addEventListener("abort", abortHandler);`
|
|
311
|
+
});`
|
|
316
312
|
: "";
|
|
317
313
|
return (0, ts_poet_1.code) `
|
|
318
314
|
invoke<T extends UnaryMethodDefinitionish>(
|
package/build/main.js
CHANGED
|
@@ -507,15 +507,15 @@ function makeTimestampMethods(options, longs) {
|
|
|
507
507
|
const fromTimestamp = (0, ts_poet_1.conditionalOutput)("fromTimestamp", options.useDate === options_1.DateOption.STRING
|
|
508
508
|
? (0, ts_poet_1.code) `
|
|
509
509
|
function fromTimestamp(t: ${Timestamp}): string {
|
|
510
|
-
let millis = ${toNumberCode} * 1_000;
|
|
511
|
-
millis += t.nanos / 1_000_000;
|
|
510
|
+
let millis = (${toNumberCode} || 0) * 1_000;
|
|
511
|
+
millis += (t.nanos || 0) / 1_000_000;
|
|
512
512
|
return new Date(millis).toISOString();
|
|
513
513
|
}
|
|
514
514
|
`
|
|
515
515
|
: (0, ts_poet_1.code) `
|
|
516
516
|
function fromTimestamp(t: ${Timestamp}): Date {
|
|
517
|
-
let millis = ${toNumberCode} * 1_000;
|
|
518
|
-
millis += t.nanos / 1_000_000;
|
|
517
|
+
let millis = (${toNumberCode} || 0) * 1_000;
|
|
518
|
+
millis += (t.nanos || 0) / 1_000_000;
|
|
519
519
|
return new Date(millis);
|
|
520
520
|
}
|
|
521
521
|
`);
|
package/build/options.js
CHANGED
|
@@ -80,6 +80,7 @@ function defaultOptions() {
|
|
|
80
80
|
useReadonlyTypes: false,
|
|
81
81
|
useSnakeTypeName: true,
|
|
82
82
|
outputExtensions: false,
|
|
83
|
+
outputIndex: false,
|
|
83
84
|
M: {},
|
|
84
85
|
};
|
|
85
86
|
}
|
|
@@ -160,6 +161,9 @@ function optionsFromParameter(parameter) {
|
|
|
160
161
|
if (options.nestJs) {
|
|
161
162
|
options.initializeFieldsAsUndefined = false;
|
|
162
163
|
}
|
|
164
|
+
if (options.outputIndex) {
|
|
165
|
+
options.exportCommonSymbols = false;
|
|
166
|
+
}
|
|
163
167
|
return options;
|
|
164
168
|
}
|
|
165
169
|
exports.optionsFromParameter = optionsFromParameter;
|
package/build/plugin.js
CHANGED
|
@@ -33,6 +33,12 @@ async function main() {
|
|
|
33
33
|
const content = code.toString({ ...(0, options_1.getTsPoetOpts)(options), path });
|
|
34
34
|
files.push({ name: path, content });
|
|
35
35
|
}
|
|
36
|
+
if (options.outputIndex) {
|
|
37
|
+
for (const [path, code] of (0, utils_1.generateIndexFiles)(filesToGenerate, options)) {
|
|
38
|
+
const content = code.toString({ ...(0, options_1.getTsPoetOpts)(options), path });
|
|
39
|
+
files.push({ name: path, content });
|
|
40
|
+
}
|
|
41
|
+
}
|
|
36
42
|
const response = ts_proto_descriptors_1.CodeGeneratorResponse.fromPartial({
|
|
37
43
|
file: files,
|
|
38
44
|
supportedFeatures: ts_proto_descriptors_1.CodeGeneratorResponse_Feature.FEATURE_PROTO3_OPTIONAL,
|
package/build/utils.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.impProto = exports.impFile = exports.getPropertyAccessor = exports.getFieldJsonName = exports.FormattedMethodDescriptor = exports.assertInstanceOf = exports.maybePrefixPackage = exports.prefixDisableLinter = exports.maybeAddComment = exports.upperFirst = exports.lowerFirst = exports.singular = exports.fail = exports.readToBuffer = exports.protoFilesToGenerate = void 0;
|
|
3
|
+
exports.impProto = exports.impFile = exports.getPropertyAccessor = exports.getFieldJsonName = exports.FormattedMethodDescriptor = exports.assertInstanceOf = exports.maybePrefixPackage = exports.prefixDisableLinter = exports.maybeAddComment = exports.upperFirst = exports.lowerFirst = exports.singular = exports.fail = exports.readToBuffer = exports.generateIndexFiles = exports.protoFilesToGenerate = void 0;
|
|
4
|
+
const path = require("path");
|
|
4
5
|
const ts_poet_1 = require("ts-poet");
|
|
5
6
|
const options_1 = require("./options");
|
|
6
7
|
const case_1 = require("./case");
|
|
@@ -8,6 +9,40 @@ function protoFilesToGenerate(request) {
|
|
|
8
9
|
return request.protoFile.filter((f) => request.fileToGenerate.includes(f.name));
|
|
9
10
|
}
|
|
10
11
|
exports.protoFilesToGenerate = protoFilesToGenerate;
|
|
12
|
+
function generateIndexFiles(files, options) {
|
|
13
|
+
const packageTree = {
|
|
14
|
+
index: "index.ts",
|
|
15
|
+
leaves: {},
|
|
16
|
+
chunks: [],
|
|
17
|
+
};
|
|
18
|
+
for (const { name, package: pkg } of files) {
|
|
19
|
+
const moduleName = name.replace(".proto", options.fileSuffix);
|
|
20
|
+
const pkgParts = pkg.length > 0 ? pkg.split(".") : [];
|
|
21
|
+
const branch = pkgParts.reduce((branch, part, i) => {
|
|
22
|
+
if (!(part in branch.leaves)) {
|
|
23
|
+
const prePkgParts = pkgParts.slice(0, i + 1);
|
|
24
|
+
const index = `index.${prePkgParts.join(".")}.ts`;
|
|
25
|
+
branch.chunks.push((0, ts_poet_1.code) `export * as ${part} from "./${path.basename(index, ".ts")}";`);
|
|
26
|
+
branch.leaves[part] = {
|
|
27
|
+
index,
|
|
28
|
+
leaves: {},
|
|
29
|
+
chunks: [],
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
return branch.leaves[part];
|
|
33
|
+
}, packageTree);
|
|
34
|
+
branch.chunks.push((0, ts_poet_1.code) `export * from "./${moduleName}";`);
|
|
35
|
+
}
|
|
36
|
+
const indexFiles = [];
|
|
37
|
+
let branches = [packageTree];
|
|
38
|
+
let currentBranch;
|
|
39
|
+
while ((currentBranch = branches.pop())) {
|
|
40
|
+
indexFiles.push([currentBranch.index, (0, ts_poet_1.joinCode)(currentBranch.chunks)]);
|
|
41
|
+
branches.push(...Object.values(currentBranch.leaves));
|
|
42
|
+
}
|
|
43
|
+
return indexFiles;
|
|
44
|
+
}
|
|
45
|
+
exports.generateIndexFiles = generateIndexFiles;
|
|
11
46
|
function readToBuffer(stream) {
|
|
12
47
|
return new Promise((resolve) => {
|
|
13
48
|
const ret = [];
|