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