@bufbuild/protoplugin 0.0.9
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.md +38 -0
- package/dist/cjs/create-es-plugin.js +117 -0
- package/dist/cjs/ecmascript/gencommon.js +338 -0
- package/dist/cjs/ecmascript/generated-file.js +300 -0
- package/dist/cjs/ecmascript/import-symbol.js +34 -0
- package/dist/cjs/ecmascript/index.js +30 -0
- package/dist/cjs/ecmascript/runtime-imports.js +46 -0
- package/dist/cjs/ecmascript/schema.js +84 -0
- package/dist/cjs/ecmascript/target.js +15 -0
- package/dist/cjs/error.js +34 -0
- package/dist/cjs/index.js +22 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/plugin.js +15 -0
- package/dist/cjs/run-node.js +84 -0
- package/dist/esm/create-es-plugin.js +113 -0
- package/dist/esm/ecmascript/gencommon.js +327 -0
- package/dist/esm/ecmascript/generated-file.js +296 -0
- package/dist/esm/ecmascript/import-symbol.js +30 -0
- package/dist/esm/ecmascript/index.js +21 -0
- package/dist/esm/ecmascript/runtime-imports.js +42 -0
- package/dist/esm/ecmascript/schema.js +80 -0
- package/dist/esm/ecmascript/target.js +14 -0
- package/dist/esm/error.js +29 -0
- package/dist/esm/index.js +17 -0
- package/dist/esm/plugin.js +14 -0
- package/dist/esm/run-node.js +80 -0
- package/dist/types/create-es-plugin.d.ts +21 -0
- package/dist/types/ecmascript/gencommon.d.ts +23 -0
- package/dist/types/ecmascript/generated-file.d.ts +63 -0
- package/dist/types/ecmascript/import-symbol.d.ts +39 -0
- package/dist/types/ecmascript/index.d.ts +7 -0
- package/dist/types/ecmascript/runtime-imports.d.ts +21 -0
- package/dist/types/ecmascript/schema.d.ts +41 -0
- package/dist/types/ecmascript/target.d.ts +4 -0
- package/dist/types/error.d.ts +4 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/plugin.d.ts +18 -0
- package/dist/types/run-node.d.ts +12 -0
- package/package.json +48 -0
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
// Copyright 2021-2022 Buf Technologies, Inc.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
import { CodeGeneratorResponse_File, } from "@bufbuild/protobuf";
|
|
15
|
+
import { createImportSymbol } from "./import-symbol.js";
|
|
16
|
+
import { literalString, makeFilePreamble } from "./gencommon.js";
|
|
17
|
+
export function createGeneratedFile(name, createTypeImport, runtimeImports, preambleSettings) {
|
|
18
|
+
const importPath = deriveImportPath(name);
|
|
19
|
+
let preamble;
|
|
20
|
+
const el = [];
|
|
21
|
+
return {
|
|
22
|
+
preamble(file) {
|
|
23
|
+
preamble = makeFilePreamble(file, preambleSettings.pluginName, preambleSettings.pluginVersion, preambleSettings.parameter, preambleSettings.tsNocheck);
|
|
24
|
+
},
|
|
25
|
+
print(...any) {
|
|
26
|
+
printableToEl(any, el, createTypeImport, runtimeImports);
|
|
27
|
+
el.push("\n");
|
|
28
|
+
},
|
|
29
|
+
export(name) {
|
|
30
|
+
return createImportSymbol(name, importPath);
|
|
31
|
+
},
|
|
32
|
+
import(typeOrName, from) {
|
|
33
|
+
if (typeof typeOrName == "string") {
|
|
34
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
35
|
+
return createImportSymbol(name, from);
|
|
36
|
+
}
|
|
37
|
+
return createTypeImport(typeOrName);
|
|
38
|
+
},
|
|
39
|
+
toResponse(res) {
|
|
40
|
+
let content = elToContent(el, importPath);
|
|
41
|
+
if (content.length === 0) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (preamble !== undefined) {
|
|
45
|
+
content = preamble + "\n" + content;
|
|
46
|
+
}
|
|
47
|
+
res.file.push(new CodeGeneratorResponse_File({
|
|
48
|
+
name: name,
|
|
49
|
+
content,
|
|
50
|
+
}));
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
const importPathExtension = ".js";
|
|
55
|
+
const knownExtensionsRE = /\.(js|ts|d.ts)$/;
|
|
56
|
+
const relativePathRE = /^\.{1,2}\//;
|
|
57
|
+
function deriveImportPath(filename) {
|
|
58
|
+
let importPath = filename.replace(knownExtensionsRE, importPathExtension);
|
|
59
|
+
if (!relativePathRE.test(importPath)) {
|
|
60
|
+
importPath = "./" + importPath;
|
|
61
|
+
}
|
|
62
|
+
return importPath;
|
|
63
|
+
}
|
|
64
|
+
function elToContent(el, importerPath) {
|
|
65
|
+
const c = [];
|
|
66
|
+
const symbolToIdentifier = processImports(el, importerPath, (typeOnly, from, names) => {
|
|
67
|
+
const p = names.map(({ name, alias }) => alias == undefined ? name : `${name} as ${alias}`);
|
|
68
|
+
const what = `{${p.join(", ")}}`;
|
|
69
|
+
if (typeOnly) {
|
|
70
|
+
c.push(`import type ${what} from ${literalString(from)};\n`);
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
c.push(`import ${what} from ${literalString(from)};\n`);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
if (c.length > 0) {
|
|
77
|
+
c.push("\n");
|
|
78
|
+
}
|
|
79
|
+
for (const e of el) {
|
|
80
|
+
if (typeof e == "string") {
|
|
81
|
+
c.push(e);
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
const ident = symbolToIdentifier.get(e.id);
|
|
85
|
+
if (ident != undefined) {
|
|
86
|
+
c.push(ident);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return c.join("");
|
|
90
|
+
}
|
|
91
|
+
function printableToEl(printables, el, createTypeImport, runtimeImports) {
|
|
92
|
+
for (const p of printables) {
|
|
93
|
+
if (Array.isArray(p)) {
|
|
94
|
+
printableToEl(p, el, createTypeImport, runtimeImports);
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
switch (typeof p) {
|
|
98
|
+
case "string":
|
|
99
|
+
el.push(p);
|
|
100
|
+
break;
|
|
101
|
+
case "number":
|
|
102
|
+
el.push(literalNumber(p));
|
|
103
|
+
break;
|
|
104
|
+
case "boolean":
|
|
105
|
+
el.push(p.toString());
|
|
106
|
+
break;
|
|
107
|
+
case "bigint":
|
|
108
|
+
el.push(...literalBigint(p, runtimeImports));
|
|
109
|
+
break;
|
|
110
|
+
case "object":
|
|
111
|
+
if (p instanceof Uint8Array) {
|
|
112
|
+
el.push(literalUint8Array(p));
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
switch (p.kind) {
|
|
116
|
+
case "es_symbol":
|
|
117
|
+
el.push(p);
|
|
118
|
+
break;
|
|
119
|
+
case "message":
|
|
120
|
+
case "enum":
|
|
121
|
+
el.push(createTypeImport(p));
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
break;
|
|
125
|
+
default:
|
|
126
|
+
throw `cannot print ${typeof p}`;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function processImports(el, importerPath, makeImportStatement) {
|
|
132
|
+
// identifiers to use in the output
|
|
133
|
+
const symbolToIdentifier = new Map();
|
|
134
|
+
// symbols that need a value import (as opposed to a type-only import)
|
|
135
|
+
const symbolToIsValue = new Map();
|
|
136
|
+
// taken in this file
|
|
137
|
+
const identifiersTaken = new Set();
|
|
138
|
+
// foreign symbols need an import
|
|
139
|
+
const foreignSymbols = [];
|
|
140
|
+
// Walk through all symbols used and populate the collections above.
|
|
141
|
+
for (const s of el) {
|
|
142
|
+
if (typeof s == "string") {
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
symbolToIdentifier.set(s.id, s.name);
|
|
146
|
+
if (!s.typeOnly) {
|
|
147
|
+
// a symbol is only type-imported as long as all uses are type-only
|
|
148
|
+
symbolToIsValue.set(s.id, true);
|
|
149
|
+
}
|
|
150
|
+
if (s.from === importerPath) {
|
|
151
|
+
identifiersTaken.add(s.name);
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
foreignSymbols.push(s);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
// Walk through all foreign symbols and make their identifiers unique.
|
|
158
|
+
const handledSymbols = new Set();
|
|
159
|
+
for (const s of foreignSymbols) {
|
|
160
|
+
if (handledSymbols.has(s.id)) {
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
handledSymbols.add(s.id);
|
|
164
|
+
if (!identifiersTaken.has(s.name)) {
|
|
165
|
+
identifiersTaken.add(s.name);
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
let i = 1;
|
|
169
|
+
let alias;
|
|
170
|
+
for (;;) {
|
|
171
|
+
// We choose '$' because it is invalid in proto identifiers.
|
|
172
|
+
alias = `${s.name}$${i}`;
|
|
173
|
+
if (!identifiersTaken.has(alias)) {
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
i++;
|
|
177
|
+
}
|
|
178
|
+
identifiersTaken.add(alias);
|
|
179
|
+
symbolToIdentifier.set(s.id, alias);
|
|
180
|
+
}
|
|
181
|
+
const sourceToImport = new Map();
|
|
182
|
+
for (const s of foreignSymbols) {
|
|
183
|
+
let i = sourceToImport.get(s.from);
|
|
184
|
+
if (i == undefined) {
|
|
185
|
+
i = {
|
|
186
|
+
types: new Map(),
|
|
187
|
+
values: new Map(),
|
|
188
|
+
};
|
|
189
|
+
sourceToImport.set(s.from, i);
|
|
190
|
+
}
|
|
191
|
+
let alias = symbolToIdentifier.get(s.id);
|
|
192
|
+
if (alias == s.name) {
|
|
193
|
+
alias = undefined;
|
|
194
|
+
}
|
|
195
|
+
if (symbolToIsValue.get(s.id)) {
|
|
196
|
+
i.values.set(s.name, alias);
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
i.types.set(s.name, alias);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
// Make import statements.
|
|
203
|
+
const handledSource = new Set();
|
|
204
|
+
const buildNames = (map) => {
|
|
205
|
+
const names = [];
|
|
206
|
+
map.forEach((value, key) => names.push({ name: key, alias: value }));
|
|
207
|
+
names.sort((a, b) => a.name.localeCompare(b.name));
|
|
208
|
+
return names;
|
|
209
|
+
};
|
|
210
|
+
for (const s of foreignSymbols) {
|
|
211
|
+
if (handledSource.has(s.from)) {
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
handledSource.add(s.from);
|
|
215
|
+
const i = sourceToImport.get(s.from);
|
|
216
|
+
if (i == undefined) {
|
|
217
|
+
// should never happen
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
const from = makeImportPathRelative(importerPath, s.from);
|
|
221
|
+
if (i.types.size > 0) {
|
|
222
|
+
makeImportStatement(true, from, buildNames(i.types));
|
|
223
|
+
}
|
|
224
|
+
if (i.values.size > 0) {
|
|
225
|
+
makeImportStatement(false, from, buildNames(i.values));
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return symbolToIdentifier;
|
|
229
|
+
}
|
|
230
|
+
// makeImportPathRelative makes an import path relative to the file importing
|
|
231
|
+
// it. For example, consider the following files:
|
|
232
|
+
// - foo/foo.js
|
|
233
|
+
// - baz.js
|
|
234
|
+
// If foo.js wants to import baz.js, we return ../baz.js
|
|
235
|
+
function makeImportPathRelative(importer, importPath) {
|
|
236
|
+
if (!relativePathRE.test(importPath)) {
|
|
237
|
+
// We don't touch absolute imports, like @bufbuild/protobuf
|
|
238
|
+
return importPath;
|
|
239
|
+
}
|
|
240
|
+
let a = importer
|
|
241
|
+
.replace(/^\.\//, "")
|
|
242
|
+
.split("/")
|
|
243
|
+
.filter((p) => p.length > 0)
|
|
244
|
+
.slice(0, -1);
|
|
245
|
+
let b = importPath
|
|
246
|
+
.replace(/^\.\//, "")
|
|
247
|
+
.split("/")
|
|
248
|
+
.filter((p) => p.length > 0);
|
|
249
|
+
let matchingPartCount = 0;
|
|
250
|
+
for (let l = Math.min(a.length, b.length); matchingPartCount < l; matchingPartCount++) {
|
|
251
|
+
if (a[matchingPartCount] !== b[matchingPartCount]) {
|
|
252
|
+
break;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
a = a.slice(matchingPartCount);
|
|
256
|
+
b = b.slice(matchingPartCount);
|
|
257
|
+
const c = a
|
|
258
|
+
.map(() => "..")
|
|
259
|
+
.concat(b)
|
|
260
|
+
.join("/");
|
|
261
|
+
return relativePathRE.test(c) ? c : "./" + c;
|
|
262
|
+
}
|
|
263
|
+
function literalNumber(value) {
|
|
264
|
+
if (Number.isNaN(value)) {
|
|
265
|
+
return "globalThis.Number.NaN";
|
|
266
|
+
}
|
|
267
|
+
if (value === Number.POSITIVE_INFINITY) {
|
|
268
|
+
return "globalThis.Number.POSITIVE_INFINITY";
|
|
269
|
+
}
|
|
270
|
+
if (value === Number.NEGATIVE_INFINITY) {
|
|
271
|
+
return "globalThis.Number.NEGATIVE_INFINITY";
|
|
272
|
+
}
|
|
273
|
+
return value.toString(10);
|
|
274
|
+
}
|
|
275
|
+
function literalBigint(value, runtimeImports) {
|
|
276
|
+
// Loose comparison will match between 0n and 0.
|
|
277
|
+
if (value == 0) {
|
|
278
|
+
return [runtimeImports.protoInt64, ".zero"];
|
|
279
|
+
}
|
|
280
|
+
return [
|
|
281
|
+
runtimeImports.protoInt64,
|
|
282
|
+
".parse(",
|
|
283
|
+
literalString(value.toString()),
|
|
284
|
+
")",
|
|
285
|
+
];
|
|
286
|
+
}
|
|
287
|
+
function literalUint8Array(value) {
|
|
288
|
+
if (value.length === 0) {
|
|
289
|
+
return "new Uint8Array(0)";
|
|
290
|
+
}
|
|
291
|
+
const strings = [];
|
|
292
|
+
for (const n of value) {
|
|
293
|
+
strings.push("0x" + n.toString(16).toUpperCase().padStart(2, "0"));
|
|
294
|
+
}
|
|
295
|
+
return `new Uint8Array([${strings.join(", ")}])`;
|
|
296
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// Copyright 2021-2022 Buf Technologies, Inc.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
/**
|
|
15
|
+
* Create a new import symbol.
|
|
16
|
+
*/
|
|
17
|
+
export function createImportSymbol(name, from, typeOnly) {
|
|
18
|
+
const id = `import("${from}").${name}`;
|
|
19
|
+
const s = {
|
|
20
|
+
kind: "es_symbol",
|
|
21
|
+
name,
|
|
22
|
+
from,
|
|
23
|
+
typeOnly: false,
|
|
24
|
+
id,
|
|
25
|
+
toTypeOnly() {
|
|
26
|
+
return Object.assign(Object.assign({}, this), { typeOnly: true });
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
return typeOnly === true ? s.toTypeOnly() : s;
|
|
30
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Copyright 2021-2022 Buf Technologies, Inc.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
import { codegenInfo } from "@bufbuild/protobuf";
|
|
15
|
+
export {} from "./target.js";
|
|
16
|
+
export {} from "./schema.js";
|
|
17
|
+
export {} from "./runtime-imports.js";
|
|
18
|
+
export {} from "./generated-file.js";
|
|
19
|
+
export {} from "./import-symbol.js";
|
|
20
|
+
export const { localName } = codegenInfo;
|
|
21
|
+
export { createJsDocBlock, getFieldExplicitDefaultValue, getFieldIntrinsicDefaultValue, getFieldTyping, makeJsDoc, literalString, } from "./gencommon.js";
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// Copyright 2021-2022 Buf Technologies, Inc.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
import { createImportSymbol } from "./import-symbol.js";
|
|
15
|
+
import { codegenInfo } from "@bufbuild/protobuf";
|
|
16
|
+
export function createRuntimeImports(bootstrapWkt) {
|
|
17
|
+
// prettier-ignore
|
|
18
|
+
return {
|
|
19
|
+
proto2: infoToSymbol("proto2", bootstrapWkt),
|
|
20
|
+
proto3: infoToSymbol("proto3", bootstrapWkt),
|
|
21
|
+
Message: infoToSymbol("Message", bootstrapWkt),
|
|
22
|
+
PartialMessage: infoToSymbol("PartialMessage", bootstrapWkt),
|
|
23
|
+
PlainMessage: infoToSymbol("PlainMessage", bootstrapWkt),
|
|
24
|
+
FieldList: infoToSymbol("FieldList", bootstrapWkt),
|
|
25
|
+
MessageType: infoToSymbol("MessageType", bootstrapWkt),
|
|
26
|
+
BinaryReadOptions: infoToSymbol("BinaryReadOptions", bootstrapWkt),
|
|
27
|
+
BinaryWriteOptions: infoToSymbol("BinaryWriteOptions", bootstrapWkt),
|
|
28
|
+
JsonReadOptions: infoToSymbol("JsonReadOptions", bootstrapWkt),
|
|
29
|
+
JsonWriteOptions: infoToSymbol("JsonWriteOptions", bootstrapWkt),
|
|
30
|
+
JsonValue: infoToSymbol("JsonValue", bootstrapWkt),
|
|
31
|
+
JsonObject: infoToSymbol("JsonObject", bootstrapWkt),
|
|
32
|
+
protoInt64: infoToSymbol("protoInt64", bootstrapWkt),
|
|
33
|
+
ScalarType: infoToSymbol("ScalarType", bootstrapWkt),
|
|
34
|
+
MethodKind: infoToSymbol("MethodKind", bootstrapWkt),
|
|
35
|
+
MethodIdempotency: infoToSymbol("MethodIdempotency", bootstrapWkt),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function infoToSymbol(name, bootstrapWkt) {
|
|
39
|
+
const info = codegenInfo.symbols[name];
|
|
40
|
+
const symbol = createImportSymbol(name, bootstrapWkt ? info.privateImportPath : info.publicImportPath);
|
|
41
|
+
return info.typeOnly ? symbol.toTypeOnly() : symbol;
|
|
42
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// Copyright 2021-2022 Buf Technologies, Inc.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
import { codegenInfo, CodeGeneratorResponse_Feature, createDescriptorSet, protoInt64, } from "@bufbuild/protobuf";
|
|
15
|
+
import { createGeneratedFile } from "./generated-file.js";
|
|
16
|
+
import { createRuntimeImports } from "./runtime-imports.js";
|
|
17
|
+
import { createImportSymbol } from "./import-symbol.js";
|
|
18
|
+
export function createSchema(request, targets, pluginName, pluginVersion, tsNocheck, bootstrapWkt) {
|
|
19
|
+
const descriptorSet = createDescriptorSet(request.protoFile);
|
|
20
|
+
const filesToGenerate = findFilesToGenerate(descriptorSet, request);
|
|
21
|
+
const runtime = createRuntimeImports(bootstrapWkt);
|
|
22
|
+
const createTypeImport = (desc) => {
|
|
23
|
+
const name = codegenInfo.localName(desc);
|
|
24
|
+
const from = makeImportPath(desc.file, bootstrapWkt, filesToGenerate);
|
|
25
|
+
return createImportSymbol(name, from);
|
|
26
|
+
};
|
|
27
|
+
const generatedFiles = [];
|
|
28
|
+
const schema = {
|
|
29
|
+
targets,
|
|
30
|
+
runtime,
|
|
31
|
+
proto: request,
|
|
32
|
+
files: filesToGenerate,
|
|
33
|
+
allFiles: descriptorSet.files,
|
|
34
|
+
generateFile(name) {
|
|
35
|
+
const genFile = createGeneratedFile(name, createTypeImport, runtime, {
|
|
36
|
+
pluginName,
|
|
37
|
+
pluginVersion,
|
|
38
|
+
parameter: request.parameter,
|
|
39
|
+
tsNocheck,
|
|
40
|
+
});
|
|
41
|
+
generatedFiles.push(genFile);
|
|
42
|
+
return genFile;
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
return {
|
|
46
|
+
schema,
|
|
47
|
+
toResponse(res) {
|
|
48
|
+
res.supportedFeatures = protoInt64.parse(CodeGeneratorResponse_Feature.PROTO3_OPTIONAL);
|
|
49
|
+
for (const genFile of generatedFiles) {
|
|
50
|
+
genFile.toResponse(res);
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function findFilesToGenerate(descriptorSet, request) {
|
|
56
|
+
const missing = request.fileToGenerate.filter((fileToGenerate) => descriptorSet.files.every((file) => fileToGenerate !== file.name + ".proto"));
|
|
57
|
+
if (missing.length) {
|
|
58
|
+
throw `files_to_generate missing in the request: ${missing.join(", ")}`;
|
|
59
|
+
}
|
|
60
|
+
return descriptorSet.files.filter((file) => request.fileToGenerate.includes(file.name + ".proto"));
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Returns the import path for files generated by the base type generator
|
|
64
|
+
* protoc-gen-es.
|
|
65
|
+
*/
|
|
66
|
+
function makeImportPath(file, bootstrapWkt, filesToGenerate) {
|
|
67
|
+
// Well-known types are published with the runtime package. We usually want
|
|
68
|
+
// the generated code to import them from the runtime package, with the
|
|
69
|
+
// following exceptions:
|
|
70
|
+
// 1. We are bootstrapping the runtime package via the plugin option
|
|
71
|
+
// "bootstrap_wkt". In that case, we cannot refer to the runtime package
|
|
72
|
+
// itself.
|
|
73
|
+
// 2. We were explicitly asked to generate the well-known type.
|
|
74
|
+
if (!bootstrapWkt &&
|
|
75
|
+
!filesToGenerate.includes(file) &&
|
|
76
|
+
codegenInfo.wktSourceFiles.includes(file.name + ".proto")) {
|
|
77
|
+
return codegenInfo.packageName;
|
|
78
|
+
}
|
|
79
|
+
return "./" + file.name + "_pb.js";
|
|
80
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Copyright 2021-2022 Buf Technologies, Inc.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Copyright 2021-2022 Buf Technologies, Inc.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
export class PluginOptionError extends Error {
|
|
15
|
+
constructor(option, reason) {
|
|
16
|
+
super(reason === undefined
|
|
17
|
+
? `invalid option "${option}`
|
|
18
|
+
: `invalid option "${option}: ${reasonToString(reason)}`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export function reasonToString(reason) {
|
|
22
|
+
if (reason instanceof Error) {
|
|
23
|
+
return reason.message;
|
|
24
|
+
}
|
|
25
|
+
if (typeof reason === "string") {
|
|
26
|
+
return reason;
|
|
27
|
+
}
|
|
28
|
+
return String(reason);
|
|
29
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Copyright 2021-2022 Buf Technologies, Inc.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
export {} from "./plugin.js";
|
|
15
|
+
export {} from "./ecmascript/schema.js";
|
|
16
|
+
export { runNodeJs } from "./run-node.js";
|
|
17
|
+
export { createEcmaScriptPlugin } from "./create-es-plugin.js";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Copyright 2021-2022 Buf Technologies, Inc.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// Copyright 2021-2022 Buf Technologies, Inc.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
import { CodeGeneratorRequest } from "@bufbuild/protobuf";
|
|
15
|
+
import { PluginOptionError, reasonToString } from "./error.js";
|
|
16
|
+
/**
|
|
17
|
+
* Run a plugin with Node.js.
|
|
18
|
+
*
|
|
19
|
+
* ```
|
|
20
|
+
* #!/usr/bin/env node
|
|
21
|
+
* const {runNodeJs} = require("@bufbuild/protoplugin");
|
|
22
|
+
* const {myPlugin} = require("./protoc-gen-x-plugin.js");
|
|
23
|
+
* runNodeJs(myPlugin);
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export function runNodeJs(plugin) {
|
|
27
|
+
setBlockingStdout();
|
|
28
|
+
const args = process.argv.slice(2);
|
|
29
|
+
if ((args.length === 1 && args[0] === "-v") || args[0] === "--version") {
|
|
30
|
+
process.stdout.write(`${plugin.name} ${plugin.version}\n`);
|
|
31
|
+
process.exit(0);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
if (args.length !== 0) {
|
|
35
|
+
process.stderr.write(`${plugin.name} accepts a google.protobuf.compiler.CodeGeneratorRequest on stdin and writes a CodeGeneratorResponse to stdout\n`);
|
|
36
|
+
process.exit(1);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
readBytes(process.stdin)
|
|
40
|
+
.then((data) => {
|
|
41
|
+
const req = CodeGeneratorRequest.fromBinary(data);
|
|
42
|
+
const res = plugin.run(req);
|
|
43
|
+
process.stdout.write(res.toBinary());
|
|
44
|
+
process.exit(0);
|
|
45
|
+
})
|
|
46
|
+
.catch((reason) => {
|
|
47
|
+
const message = reason instanceof PluginOptionError
|
|
48
|
+
? reason.message
|
|
49
|
+
: reasonToString(reason);
|
|
50
|
+
process.stderr.write(`${plugin.name}: ${message}\n`);
|
|
51
|
+
process.exit(1);
|
|
52
|
+
return;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Read a stream to the end.
|
|
57
|
+
*/
|
|
58
|
+
function readBytes(stream) {
|
|
59
|
+
return new Promise((resolve, reject) => {
|
|
60
|
+
const chunks = [];
|
|
61
|
+
stream.on("data", (chunk) => chunks.push(chunk));
|
|
62
|
+
stream.on("end", () => {
|
|
63
|
+
resolve(Buffer.concat(chunks));
|
|
64
|
+
});
|
|
65
|
+
stream.on("error", (err) => {
|
|
66
|
+
reject(err);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Node.js buffers stdout, and process.exit() will truncate output.
|
|
72
|
+
* As a workaround, we set the stream to blocking via a private API.
|
|
73
|
+
* See https://github.com/timostamm/protobuf-ts/issues/134
|
|
74
|
+
* See https://github.com/nodejs/node/issues/6456
|
|
75
|
+
*/
|
|
76
|
+
function setBlockingStdout() {
|
|
77
|
+
var _a, _b;
|
|
78
|
+
const stdout = process.stdout;
|
|
79
|
+
(_b = (_a = stdout._handle) === null || _a === void 0 ? void 0 : _a.setBlocking) === null || _b === void 0 ? void 0 : _b.call(_a, true);
|
|
80
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Schema } from "./ecmascript";
|
|
2
|
+
import type { Plugin } from "./plugin.js";
|
|
3
|
+
interface PluginInit {
|
|
4
|
+
/**
|
|
5
|
+
* Name of this code generator plugin.
|
|
6
|
+
*/
|
|
7
|
+
name: string;
|
|
8
|
+
/**
|
|
9
|
+
* Version of this code generator plugin.
|
|
10
|
+
*/
|
|
11
|
+
version: string;
|
|
12
|
+
parseOption?: PluginOptionParseFn;
|
|
13
|
+
}
|
|
14
|
+
declare type PluginOptionParseFn = (key: string, value: string | undefined) => void;
|
|
15
|
+
/**
|
|
16
|
+
* Create a new code generator plugin for ECMAScript.
|
|
17
|
+
* The plugin can generate JavaScript, TypeScript, or TypeScript declaration
|
|
18
|
+
* files.
|
|
19
|
+
*/
|
|
20
|
+
export declare function createEcmaScriptPlugin(init: PluginInit, generateFn: (schema: Schema) => void): Plugin;
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { DescEnum, DescEnumValue, DescField, DescFile, DescMessage, DescMethod, DescOneof, DescService, ScalarType } from "@bufbuild/protobuf";
|
|
2
|
+
import type { GeneratedFile } from "./generated-file.js";
|
|
3
|
+
import type { ImportSymbol } from "./import-symbol.js";
|
|
4
|
+
declare type Printable = Parameters<GeneratedFile["print"]>[number];
|
|
5
|
+
export declare function makeFilePreamble(file: DescFile, pluginName: string, pluginVersion: string, parameter: string | undefined, tsNoCheck: boolean): string;
|
|
6
|
+
export declare function createJsDocBlock(text: string, indentation?: string): Printable;
|
|
7
|
+
export declare function makeJsDoc(desc: DescEnum | DescEnumValue | DescMessage | DescOneof | DescField | DescService | DescMethod, indentation?: string): Printable;
|
|
8
|
+
/**
|
|
9
|
+
* Returns an expression for the TypeScript typing of a field,
|
|
10
|
+
* and whether the property should be optional.
|
|
11
|
+
*/
|
|
12
|
+
export declare function getFieldTyping(field: DescField, file: GeneratedFile): {
|
|
13
|
+
typing: Printable;
|
|
14
|
+
optional: boolean;
|
|
15
|
+
};
|
|
16
|
+
export declare function scalarTypeScriptType(type: ScalarType): Printable;
|
|
17
|
+
export declare function literalString(value: string): string;
|
|
18
|
+
export declare function getFieldExplicitDefaultValue(field: DescField, protoInt64Symbol: ImportSymbol): Printable | undefined;
|
|
19
|
+
export declare function getFieldIntrinsicDefaultValue(field: DescField): {
|
|
20
|
+
defaultValue: Printable | undefined;
|
|
21
|
+
typingInferrable: boolean;
|
|
22
|
+
};
|
|
23
|
+
export {};
|