@aptre/protobuf-es-lite 0.2.7 → 0.2.8

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 (121) hide show
  1. package/.ignore +1 -0
  2. package/README.md +23 -24
  3. package/bin/protoc-gen-es-lite +3 -3
  4. package/dist/binary-encoding.d.ts +420 -0
  5. package/dist/binary-encoding.js +437 -0
  6. package/dist/binary.d.ts +35 -2
  7. package/dist/binary.js +64 -65
  8. package/dist/codegen-info.d.ts +43 -0
  9. package/dist/codegen-info.js +72 -0
  10. package/dist/create-descriptor-set.d.ts +36 -0
  11. package/dist/create-descriptor-set.js +936 -0
  12. package/dist/descriptor-set.d.ts +672 -0
  13. package/dist/{editions.js → descriptor-set.js} +0 -11
  14. package/dist/enum.d.ts +35 -1
  15. package/dist/feature-set.d.ts +19 -0
  16. package/dist/feature-set.js +103 -0
  17. package/dist/field-wrapper.d.ts +7 -3
  18. package/dist/field-wrapper.js +30 -1
  19. package/dist/field.d.ts +8 -19
  20. package/dist/field.js +15 -117
  21. package/dist/google/index.d.ts +9 -0
  22. package/dist/google/index.js +41 -0
  23. package/dist/google/protobuf/any.pb.d.ts +139 -0
  24. package/dist/google/protobuf/any.pb.js +42 -0
  25. package/dist/google/protobuf/api.pb.d.ts +252 -0
  26. package/dist/google/protobuf/api.pb.js +70 -0
  27. package/dist/google/protobuf/compiler/plugin.pb.d.ts +240 -0
  28. package/dist/google/protobuf/compiler/plugin.pb.js +203 -0
  29. package/dist/google/protobuf/descriptor.pb.d.ts +2132 -0
  30. package/dist/google/protobuf/descriptor.pb.js +1080 -0
  31. package/dist/google/protobuf/duration.pb.d.ts +92 -0
  32. package/dist/google/protobuf/duration.pb.js +42 -0
  33. package/dist/google/protobuf/empty.pb.d.ts +18 -0
  34. package/dist/google/protobuf/empty.pb.js +39 -0
  35. package/dist/google/protobuf/source_context.pb.d.ts +21 -0
  36. package/dist/google/protobuf/source_context.pb.js +41 -0
  37. package/dist/google/protobuf/struct.pb.d.ts +121 -0
  38. package/dist/google/protobuf/struct.pb.js +83 -0
  39. package/dist/google/protobuf/timestamp.pb.d.ts +121 -0
  40. package/dist/google/protobuf/timestamp.pb.js +42 -0
  41. package/dist/google/protobuf/type.pb.d.ts +464 -0
  42. package/dist/google/protobuf/type.pb.js +338 -0
  43. package/dist/google/protobuf/wrappers.pb.d.ts +183 -0
  44. package/dist/google/protobuf/wrappers.pb.js +107 -0
  45. package/dist/index.d.ts +8 -3
  46. package/dist/index.js +38 -8
  47. package/dist/is-message.d.ts +10 -2
  48. package/dist/is-message.js +74 -3
  49. package/dist/json.d.ts +68 -1
  50. package/dist/json.js +57 -81
  51. package/dist/message.d.ts +30 -61
  52. package/dist/message.js +61 -20
  53. package/dist/names.d.ts +43 -0
  54. package/dist/names.js +276 -0
  55. package/dist/partial.js +14 -15
  56. package/dist/proto-base64.d.ts +18 -0
  57. package/dist/proto-base64.js +128 -0
  58. package/dist/proto-double.d.ts +5 -0
  59. package/dist/proto-double.js +29 -0
  60. package/dist/proto-int64.d.ts +95 -0
  61. package/dist/proto-int64.js +116 -0
  62. package/dist/protoc-gen-es-lite/protoc-gen-es-lite-plugin.d.ts +1 -0
  63. package/dist/{protoc-gen-es-lite-plugin.js → protoc-gen-es-lite/protoc-gen-es-lite-plugin.js} +2 -2
  64. package/dist/{typescript.d.ts → protoc-gen-es-lite/typescript.d.ts} +3 -2
  65. package/dist/{typescript.js → protoc-gen-es-lite/typescript.js} +46 -34
  66. package/dist/protoplugin/create-es-plugin.d.ts +83 -0
  67. package/dist/protoplugin/create-es-plugin.js +119 -0
  68. package/dist/protoplugin/ecmascript/file-preamble.d.ts +2 -0
  69. package/dist/protoplugin/ecmascript/file-preamble.js +74 -0
  70. package/dist/protoplugin/ecmascript/generated-file.d.ts +116 -0
  71. package/dist/protoplugin/ecmascript/generated-file.js +407 -0
  72. package/dist/protoplugin/ecmascript/import-path.d.ts +65 -0
  73. package/dist/protoplugin/ecmascript/import-path.js +156 -0
  74. package/dist/protoplugin/ecmascript/import-symbol.d.ts +39 -0
  75. package/dist/protoplugin/ecmascript/import-symbol.js +37 -0
  76. package/dist/protoplugin/ecmascript/index.d.ts +7 -0
  77. package/dist/protoplugin/ecmascript/index.js +20 -0
  78. package/dist/protoplugin/ecmascript/jsdoc.d.ts +14 -0
  79. package/dist/protoplugin/ecmascript/jsdoc.js +93 -0
  80. package/dist/protoplugin/ecmascript/opaque-printables.d.ts +27 -0
  81. package/dist/protoplugin/ecmascript/opaque-printables.js +15 -0
  82. package/dist/protoplugin/ecmascript/parameter.d.ts +13 -0
  83. package/dist/protoplugin/ecmascript/parameter.js +161 -0
  84. package/dist/protoplugin/ecmascript/reify-wkt.d.ts +100 -0
  85. package/dist/protoplugin/ecmascript/reify-wkt.js +170 -0
  86. package/dist/protoplugin/ecmascript/runtime-imports.d.ts +6 -0
  87. package/dist/protoplugin/ecmascript/runtime-imports.js +28 -0
  88. package/dist/protoplugin/ecmascript/schema.d.ts +43 -0
  89. package/dist/protoplugin/ecmascript/schema.js +73 -0
  90. package/dist/protoplugin/ecmascript/target.d.ts +4 -0
  91. package/dist/protoplugin/ecmascript/target.js +15 -0
  92. package/dist/protoplugin/ecmascript/transpile.d.ts +2 -0
  93. package/dist/protoplugin/ecmascript/transpile.js +149 -0
  94. package/dist/protoplugin/error.d.ts +6 -0
  95. package/dist/protoplugin/error.js +43 -0
  96. package/dist/protoplugin/index.d.ts +8 -0
  97. package/dist/protoplugin/index.js +20 -0
  98. package/dist/protoplugin/plugin.d.ts +18 -0
  99. package/dist/protoplugin/plugin.js +15 -0
  100. package/dist/protoplugin/run-node.d.ts +12 -0
  101. package/dist/protoplugin/run-node.js +83 -0
  102. package/dist/scalar.d.ts +53 -1
  103. package/dist/scalar.js +87 -22
  104. package/dist/service-type.d.ts +95 -0
  105. package/dist/service-type.js +53 -0
  106. package/dist/text-format.d.ts +4 -0
  107. package/dist/text-format.js +189 -0
  108. package/dist/type-registry.d.ts +43 -0
  109. package/dist/type-registry.js +15 -0
  110. package/dist/unknown.d.ts +1 -1
  111. package/dist/util.d.ts +2 -2
  112. package/dist/util.js +46 -44
  113. package/dist/varint.d.ts +70 -0
  114. package/dist/varint.js +324 -0
  115. package/example/example.pb.ts +87 -0
  116. package/example/example.proto +1 -0
  117. package/example/tsconfig.json +8 -2
  118. package/package.json +19 -12
  119. package/tsconfig.json +4 -4
  120. package/dist/editions.d.ts +0 -6
  121. package/dist/protoc-gen-es-lite-plugin.d.ts +0 -1
@@ -0,0 +1,407 @@
1
+ "use strict";
2
+ // Copyright 2021-2024 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 import_symbol_js_1 = require("./import-symbol.js");
18
+ const import_path_js_1 = require("./import-path.js");
19
+ const jsdoc_js_1 = require("./jsdoc.js");
20
+ const proto_int64_js_1 = require("../../proto-int64.js");
21
+ const scalar_js_1 = require("../../scalar.js");
22
+ function createGeneratedFile(name, importPath, jsImportStyle, rewriteImportPath, createTypeImport, runtimeImports, createPreamble) {
23
+ let preamble;
24
+ const el = [];
25
+ return {
26
+ preamble(file) {
27
+ preamble = createPreamble(file);
28
+ },
29
+ print(printableOrFragments, ...rest) {
30
+ let printables;
31
+ if (printableOrFragments != null &&
32
+ Object.prototype.hasOwnProperty.call(printableOrFragments, "raw")) {
33
+ // If called with a tagged template literal
34
+ printables = buildPrintablesFromFragments(printableOrFragments, rest);
35
+ }
36
+ else {
37
+ // If called with just an array of Printables
38
+ printables =
39
+ printableOrFragments != null ?
40
+ [printableOrFragments, ...rest]
41
+ : rest;
42
+ }
43
+ printableToEl(printables, el, createTypeImport, runtimeImports);
44
+ el.push("\n");
45
+ },
46
+ export(name) {
47
+ return (0, import_symbol_js_1.createImportSymbol)(name, importPath);
48
+ },
49
+ exportDecl(declaration, name) {
50
+ return {
51
+ kind: "es_export_decl",
52
+ declaration,
53
+ name,
54
+ };
55
+ },
56
+ string(string) {
57
+ // We do not use LiteralString, which was added later, to maintain backwards compatibility
58
+ return escapeString(string);
59
+ },
60
+ jsDoc(textOrDesc, indentation) {
61
+ return (0, jsdoc_js_1.createJsDocBlock)(textOrDesc, indentation);
62
+ },
63
+ import(typeOrName, from) {
64
+ if (typeof typeOrName == "string") {
65
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
66
+ return (0, import_symbol_js_1.createImportSymbol)(typeOrName, from);
67
+ }
68
+ return createTypeImport(typeOrName);
69
+ },
70
+ jsImportStyle,
71
+ getFileInfo() {
72
+ return {
73
+ name,
74
+ content: elToContent(el, importPath, rewriteImportPath, jsImportStyle == "legacy_commonjs"),
75
+ preamble,
76
+ };
77
+ },
78
+ };
79
+ }
80
+ exports.createGeneratedFile = createGeneratedFile;
81
+ function elToContent(el, importerPath, rewriteImportPath, legacyCommonJs) {
82
+ if (el.length == 0) {
83
+ return "";
84
+ }
85
+ const c = [];
86
+ if (legacyCommonJs) {
87
+ c.push(`"use strict";\n`);
88
+ c.push(`Object.defineProperty(exports, "__esModule", { value: true });\n`);
89
+ c.push(`\n`);
90
+ }
91
+ const symbolToIdentifier = processImports(el, importerPath, rewriteImportPath, (typeOnly, from, names) => {
92
+ if (legacyCommonJs) {
93
+ const p = names.map(({ name, alias }) => alias == undefined ? name : `${name}: ${alias}`);
94
+ const what = `{ ${p.join(", ")} }`;
95
+ c.push(`const ${what} = require(${escapeString(from)});\n`);
96
+ }
97
+ else {
98
+ const p = names.map(({ name, alias }) => alias == undefined ? name : `${name} as ${alias}`);
99
+ const what = `{ ${p.join(", ")} }`;
100
+ if (typeOnly) {
101
+ c.push(`import type ${what} from ${escapeString(from)};\n`);
102
+ }
103
+ else {
104
+ c.push(`import ${what} from ${escapeString(from)};\n`);
105
+ }
106
+ }
107
+ });
108
+ if (c.length > 0) {
109
+ c.push("\n");
110
+ }
111
+ const legacyCommonJsExportNames = [];
112
+ for (const e of el) {
113
+ if (typeof e == "string") {
114
+ c.push(e);
115
+ }
116
+ else {
117
+ switch (e.kind) {
118
+ case "es_symbol": {
119
+ const ident = symbolToIdentifier.get(e.id);
120
+ if (ident != undefined) {
121
+ c.push(ident);
122
+ }
123
+ break;
124
+ }
125
+ case "es_export_stmt":
126
+ if (legacyCommonJs) {
127
+ legacyCommonJsExportNames.push(e.name);
128
+ }
129
+ else {
130
+ c.push("export ");
131
+ }
132
+ if (e.declaration !== undefined && e.declaration.length > 0) {
133
+ c.push(e.declaration, " ");
134
+ }
135
+ c.push(e.name);
136
+ break;
137
+ }
138
+ }
139
+ }
140
+ if (legacyCommonJs) {
141
+ if (legacyCommonJsExportNames.length > 0) {
142
+ c.push(`\n`);
143
+ }
144
+ for (const name of legacyCommonJsExportNames) {
145
+ c.push(`exports.`, name, " = ", name, ";\n");
146
+ }
147
+ }
148
+ return c.join("");
149
+ }
150
+ function printableToEl(printables, el, createTypeImport, runtimeImports) {
151
+ for (const p of printables) {
152
+ if (Array.isArray(p)) {
153
+ printableToEl(p, el, createTypeImport, runtimeImports);
154
+ }
155
+ else {
156
+ switch (typeof p) {
157
+ case "string":
158
+ el.push(p);
159
+ break;
160
+ case "number":
161
+ elNumber(el, p, runtimeImports);
162
+ break;
163
+ case "boolean":
164
+ el.push(p.toString());
165
+ break;
166
+ case "bigint":
167
+ elBigint(el, p, runtimeImports);
168
+ break;
169
+ case "object":
170
+ if (p instanceof Uint8Array) {
171
+ elUint8Array(el, p);
172
+ break;
173
+ }
174
+ switch (p.kind) {
175
+ case "es_symbol":
176
+ el.push(p);
177
+ break;
178
+ case "es_jsdoc":
179
+ el.push(p.toString());
180
+ break;
181
+ case "es_string":
182
+ el.push(escapeString(p.value));
183
+ break;
184
+ case "es_proto_int64":
185
+ elProtoInt64(el, p, runtimeImports);
186
+ break;
187
+ case "es_export_decl":
188
+ el.push({
189
+ kind: "es_export_stmt",
190
+ declaration: p.declaration,
191
+ name: typeof p.name == "string" ?
192
+ p.name
193
+ : createTypeImport(p.name).name,
194
+ });
195
+ break;
196
+ case "es_ref_message":
197
+ case "es_ref_enum":
198
+ el.push(p.typeOnly ?
199
+ createTypeImport(p.type).toTypeOnly()
200
+ : createTypeImport(p.type));
201
+ break;
202
+ case "message":
203
+ case "extension":
204
+ case "enum":
205
+ el.push(createTypeImport(p));
206
+ break;
207
+ }
208
+ break;
209
+ default:
210
+ throw `cannot print ${typeof p}`;
211
+ }
212
+ }
213
+ }
214
+ }
215
+ function buildPrintablesFromFragments(fragments, values) {
216
+ const printables = [];
217
+ fragments.forEach((fragment, i) => {
218
+ printables.push(fragment);
219
+ if (fragments.length - 1 !== i) {
220
+ printables.push(values[i]);
221
+ }
222
+ });
223
+ return printables;
224
+ }
225
+ function processImports(el, importerPath, rewriteImportPath, makeImportStatement) {
226
+ // identifiers to use in the output
227
+ const symbolToIdentifier = new Map();
228
+ // symbols that need a value import (as opposed to a type-only import)
229
+ const symbolToIsValue = new Map();
230
+ // taken in this file
231
+ const identifiersTaken = new Set();
232
+ // foreign symbols need an import
233
+ const foreignSymbols = [];
234
+ // Walk through all symbols used and populate the collections above.
235
+ for (const s of el) {
236
+ if (typeof s != "object") {
237
+ continue;
238
+ }
239
+ switch (s.kind) {
240
+ case "es_symbol":
241
+ symbolToIdentifier.set(s.id, s.name);
242
+ if (!s.typeOnly) {
243
+ // a symbol is only type-imported as long as all uses are type-only
244
+ symbolToIsValue.set(s.id, true);
245
+ }
246
+ if (s.from === importerPath) {
247
+ identifiersTaken.add(s.name);
248
+ }
249
+ else {
250
+ foreignSymbols.push(s);
251
+ }
252
+ break;
253
+ case "es_export_stmt":
254
+ identifiersTaken.add(s.name);
255
+ break;
256
+ }
257
+ }
258
+ // Walk through all foreign symbols and make their identifiers unique.
259
+ const handledSymbols = new Set();
260
+ for (const s of foreignSymbols) {
261
+ if (handledSymbols.has(s.id)) {
262
+ continue;
263
+ }
264
+ handledSymbols.add(s.id);
265
+ if (!identifiersTaken.has(s.name)) {
266
+ identifiersTaken.add(s.name);
267
+ continue;
268
+ }
269
+ let i = 1;
270
+ let alias;
271
+ for (;;) {
272
+ // We choose '$' because it is invalid in proto identifiers.
273
+ alias = `${s.name}$${i}`;
274
+ if (!identifiersTaken.has(alias)) {
275
+ break;
276
+ }
277
+ i++;
278
+ }
279
+ identifiersTaken.add(alias);
280
+ symbolToIdentifier.set(s.id, alias);
281
+ }
282
+ const sourceToImport = new Map();
283
+ for (const s of foreignSymbols) {
284
+ let i = sourceToImport.get(s.from);
285
+ if (i == undefined) {
286
+ i = {
287
+ types: new Map(),
288
+ values: new Map(),
289
+ };
290
+ sourceToImport.set(s.from, i);
291
+ }
292
+ let alias = symbolToIdentifier.get(s.id);
293
+ if (alias == s.name) {
294
+ alias = undefined;
295
+ }
296
+ if (symbolToIsValue.get(s.id)) {
297
+ i.values.set(s.name, alias);
298
+ }
299
+ else {
300
+ i.types.set(s.name, alias);
301
+ }
302
+ }
303
+ // Make import statements.
304
+ const handledSource = new Set();
305
+ const buildNames = (map) => {
306
+ const names = [];
307
+ map.forEach((value, key) => names.push({ name: key, alias: value }));
308
+ names.sort((a, b) => a.name.localeCompare(b.name));
309
+ return names;
310
+ };
311
+ for (const s of foreignSymbols) {
312
+ if (handledSource.has(s.from)) {
313
+ continue;
314
+ }
315
+ handledSource.add(s.from);
316
+ const i = sourceToImport.get(s.from);
317
+ if (i == undefined) {
318
+ // should never happen
319
+ continue;
320
+ }
321
+ const from = (0, import_path_js_1.makeImportPathRelative)(importerPath, rewriteImportPath(s.from));
322
+ if (i.types.size > 0) {
323
+ makeImportStatement(true, from, buildNames(i.types));
324
+ }
325
+ if (i.values.size > 0) {
326
+ makeImportStatement(false, from, buildNames(i.values));
327
+ }
328
+ }
329
+ return symbolToIdentifier;
330
+ }
331
+ function elBigint(el, value, runtimeImports) {
332
+ if (value == proto_int64_js_1.protoInt64.zero) {
333
+ // Loose comparison will match between 0n and 0.
334
+ el.push(runtimeImports.protoInt64, ".zero");
335
+ }
336
+ else {
337
+ el.push(runtimeImports.protoInt64, value > 0 ? ".uParse(" : ".parse(", escapeString(value.toString()), ")");
338
+ }
339
+ }
340
+ function elNumber(el, value, runtimeImports) {
341
+ if (Number.isNaN(value)) {
342
+ el.push(runtimeImports.protoDouble, ".NaN");
343
+ }
344
+ else if (value === Number.POSITIVE_INFINITY) {
345
+ el.push(runtimeImports.protoDouble, ".POSITIVE_INFINITY");
346
+ }
347
+ else if (value === Number.NEGATIVE_INFINITY) {
348
+ el.push(runtimeImports.protoDouble, ".NEGATIVE_INFINITY");
349
+ }
350
+ else {
351
+ el.push(value.toString(10));
352
+ }
353
+ }
354
+ function elUint8Array(el, value) {
355
+ if (value.length === 0) {
356
+ el.push("new Uint8Array(0)");
357
+ return;
358
+ }
359
+ el.push("new Uint8Array([");
360
+ const strings = [];
361
+ for (const n of value) {
362
+ strings.push("0x" + n.toString(16).toUpperCase().padStart(2, "0"));
363
+ }
364
+ el.push(strings.join(", "));
365
+ el.push("])");
366
+ }
367
+ function elProtoInt64(el, literal, runtimeImports) {
368
+ switch (literal.longType) {
369
+ case scalar_js_1.LongType.STRING:
370
+ el.push(escapeString(literal.value.toString()));
371
+ break;
372
+ case scalar_js_1.LongType.BIGINT:
373
+ if (literal.value == proto_int64_js_1.protoInt64.zero) {
374
+ // Loose comparison will match between 0n and 0.
375
+ el.push(runtimeImports.protoInt64, ".zero");
376
+ break;
377
+ }
378
+ switch (literal.type) {
379
+ case scalar_js_1.ScalarType.UINT64:
380
+ case scalar_js_1.ScalarType.FIXED64:
381
+ el.push(runtimeImports.protoInt64);
382
+ el.push(".uParse(");
383
+ el.push(escapeString(literal.value.toString()));
384
+ el.push(")");
385
+ break;
386
+ default:
387
+ el.push(runtimeImports.protoInt64);
388
+ el.push(".parse(");
389
+ el.push(escapeString(literal.value.toString()));
390
+ el.push(")");
391
+ break;
392
+ }
393
+ }
394
+ }
395
+ function escapeString(value) {
396
+ return ('"' +
397
+ value
398
+ .split("\\")
399
+ .join("\\\\")
400
+ .split('"')
401
+ .join('\\"')
402
+ .split("\r")
403
+ .join("\\r")
404
+ .split("\n")
405
+ .join("\\n") +
406
+ '"');
407
+ }
@@ -0,0 +1,65 @@
1
+ import { DescFile } from "../../descriptor-set.js";
2
+ /**
3
+ * A configuration for rewriting import paths, a feature mainly used for
4
+ * remote code generation in the BSR npm registry, which makes it possible
5
+ * to serve the output of a BSR module and a plugin in an individual package.
6
+ *
7
+ * All plugins based on @bufbuild/protoplugin support the option
8
+ * "rewrite_imports", which is parsed into this type. The option can be given
9
+ * multiple times, in the form of `rewrite_imports=<pattern>:<target>`.
10
+ *
11
+ * The pattern is a very reduced subset of glob:
12
+ * - `*` matches zero or more characters except `/`.
13
+ * - `**` matches zero or more path elements, where an element is one or more
14
+ * characters with a trailing `/`.
15
+ *
16
+ * The target is typically a npm package name, for example `@scope/pkg`.
17
+ *
18
+ * If any generated file imports from a path matching one of the patterns, the
19
+ * import path is rewritten to the corresponding target, by prepending the
20
+ * target to the import path (after replacing any leading ./ or ../ from the
21
+ * import path with / first).
22
+ *
23
+ * Note that the pattern is matched against the import path before it is made
24
+ * relative to the file importing it. The first matching pattern wins.
25
+ *
26
+ * For example, the pattern `./foo/**\/*.pb.js` (escaped for block comment!)
27
+ * matches:
28
+ * - ./foo/bar.pb.js
29
+ * - ./foo/bar/baz.pb.js
30
+ *
31
+ * But neither of:
32
+ * - ./bar.pb.js
33
+ * - ./foo/bar_xx.js
34
+ *
35
+ * With the target `@scope/pkg`, the import path `./foo/bar.pb.js` is
36
+ * transformed to `@scope/pkg/foo/bar.pb.js`.
37
+ */
38
+ export type RewriteImports = {
39
+ pattern: string;
40
+ target: string;
41
+ }[];
42
+ /**
43
+ * Apply import rewrites to the given import path, and change all .js extensions
44
+ * to the given import extension.
45
+ */
46
+ export declare function rewriteImportPath(importPath: string, rewriteImports: RewriteImports, importExtension: string): string;
47
+ /**
48
+ * Returns the import path for files generated by the base type generator
49
+ * protoc-gen-es.
50
+ */
51
+ export declare function makeImportPath(file: DescFile, bootstrapWkt: boolean, filesToGenerate: DescFile[]): string;
52
+ export declare const relativePathRE: RegExp;
53
+ /**
54
+ * Derives an ECMAScript module import path from a file path. For example,
55
+ * the path `foo/bar.ts` is transformed into `./foo/bar.js`.
56
+ */
57
+ export declare function deriveImportPath(filename: string): string;
58
+ /**
59
+ * Makes an import path relative to the file importing it. For example,
60
+ * consider the following files:
61
+ * - foo/foo.js
62
+ * - baz.js
63
+ * If foo.js wants to import baz.js, we return ../baz.js
64
+ */
65
+ export declare function makeImportPathRelative(importer: string, importPath: string): string;
@@ -0,0 +1,156 @@
1
+ "use strict";
2
+ // Copyright 2021-2024 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.makeImportPathRelative = exports.deriveImportPath = exports.relativePathRE = exports.makeImportPath = exports.rewriteImportPath = void 0;
17
+ const codegen_info_js_1 = require("../../codegen-info.js");
18
+ const cache = new WeakMap();
19
+ /**
20
+ * Apply import rewrites to the given import path, and change all .js extensions
21
+ * to the given import extension.
22
+ */
23
+ function rewriteImportPath(importPath, rewriteImports, importExtension) {
24
+ let ri = cache.get(rewriteImports);
25
+ if (ri === undefined) {
26
+ ri = rewriteImports.map(({ pattern, target }) => {
27
+ return {
28
+ pattern: starToRegExp(pattern),
29
+ target,
30
+ };
31
+ });
32
+ cache.set(rewriteImports, ri);
33
+ }
34
+ for (const { pattern, target } of ri) {
35
+ if (pattern.test(importPath)) {
36
+ if (exports.relativePathRE.test(importPath)) {
37
+ importPath =
38
+ target.replace(/\/$/, "") + importPath.replace(exports.relativePathRE, "/");
39
+ }
40
+ else {
41
+ importPath = target;
42
+ }
43
+ break;
44
+ }
45
+ }
46
+ if (importExtension != ".js" && importPath.endsWith(".js")) {
47
+ importPath =
48
+ importPath.substring(0, importPath.length - 3) + importExtension;
49
+ }
50
+ return importPath;
51
+ }
52
+ exports.rewriteImportPath = rewriteImportPath;
53
+ function starToRegExp(star) {
54
+ const r = ["^"];
55
+ for (let i = 0; i < star.length; i++) {
56
+ switch (star[i]) {
57
+ case "*":
58
+ if (star[i + 1] === "*" && star[i + 2] === "/") {
59
+ i += 2;
60
+ r.push("([^\\/]+\\/)*");
61
+ break;
62
+ }
63
+ r.push("[^\\/]*");
64
+ break;
65
+ case ".":
66
+ case "+":
67
+ case "?":
68
+ case "^":
69
+ case "$":
70
+ case "{":
71
+ case "}":
72
+ case "(":
73
+ case ")":
74
+ case "|":
75
+ case "[":
76
+ case "]":
77
+ case "\\":
78
+ r.push("\\", star[i]);
79
+ break;
80
+ default:
81
+ r.push(star[i]);
82
+ break;
83
+ }
84
+ }
85
+ r.push("$");
86
+ return new RegExp(r.join(""));
87
+ }
88
+ /**
89
+ * Returns the import path for files generated by the base type generator
90
+ * protoc-gen-es.
91
+ */
92
+ function makeImportPath(file, bootstrapWkt, filesToGenerate) {
93
+ // Well-known types are published with the runtime package. We usually want
94
+ // the generated code to import them from the runtime package, with the
95
+ // following exceptions:
96
+ // 1. We are bootstrapping the runtime package via the plugin option
97
+ // "bootstrap_wkt". In that case, we cannot refer to the runtime package
98
+ // itself.
99
+ // 2. We were explicitly asked to generate the well-known type.
100
+ if (!bootstrapWkt &&
101
+ !filesToGenerate.includes(file) &&
102
+ codegen_info_js_1.codegenInfo.wktSourceFiles.includes(file.name + ".proto")) {
103
+ return codegen_info_js_1.codegenInfo.packageName;
104
+ }
105
+ return "./" + file.name + ".pb.js";
106
+ }
107
+ exports.makeImportPath = makeImportPath;
108
+ exports.relativePathRE = /^\.{1,2}\//;
109
+ /**
110
+ * Derives an ECMAScript module import path from a file path. For example,
111
+ * the path `foo/bar.ts` is transformed into `./foo/bar.js`.
112
+ */
113
+ function deriveImportPath(filename) {
114
+ let importPath = filename.replace(/\.(js|ts|d.ts)$/, ".js");
115
+ if (!exports.relativePathRE.test(importPath)) {
116
+ importPath = "./" + importPath;
117
+ }
118
+ return importPath;
119
+ }
120
+ exports.deriveImportPath = deriveImportPath;
121
+ /**
122
+ * Makes an import path relative to the file importing it. For example,
123
+ * consider the following files:
124
+ * - foo/foo.js
125
+ * - baz.js
126
+ * If foo.js wants to import baz.js, we return ../baz.js
127
+ */
128
+ function makeImportPathRelative(importer, importPath) {
129
+ if (!exports.relativePathRE.test(importPath)) {
130
+ // We don't touch absolute imports
131
+ return importPath;
132
+ }
133
+ let a = importer
134
+ .replace(/^\.\//, "")
135
+ .split("/")
136
+ .filter((p) => p.length > 0)
137
+ .slice(0, -1);
138
+ let b = importPath
139
+ .replace(/^\.\//, "")
140
+ .split("/")
141
+ .filter((p) => p.length > 0);
142
+ let matchingPartCount = 0;
143
+ for (let l = Math.min(a.length, b.length); matchingPartCount < l; matchingPartCount++) {
144
+ if (a[matchingPartCount] !== b[matchingPartCount]) {
145
+ break;
146
+ }
147
+ }
148
+ a = a.slice(matchingPartCount);
149
+ b = b.slice(matchingPartCount);
150
+ const c = a
151
+ .map(() => "..")
152
+ .concat(b)
153
+ .join("/");
154
+ return exports.relativePathRE.test(c) ? c : "./" + c;
155
+ }
156
+ exports.makeImportPathRelative = makeImportPathRelative;
@@ -0,0 +1,39 @@
1
+ /**
2
+ * An import symbol represents an ECMAScript import.
3
+ */
4
+ export type ImportSymbol = {
5
+ readonly kind: "es_symbol";
6
+ /**
7
+ * The name to import.
8
+ */
9
+ readonly name: string;
10
+ /**
11
+ * The import path.
12
+ *
13
+ * The path can point to a package, for example `@foo/bar/baz.js`, or
14
+ * to a file, for example `./bar/baz.js`.
15
+ *
16
+ * Note that while paths to a file begin with a `./`, they must be
17
+ * relative to the project root.
18
+ */
19
+ readonly from: string;
20
+ /**
21
+ * Whether this is a type-only import - an import that only exists in
22
+ * TypeScript.
23
+ */
24
+ readonly typeOnly: boolean;
25
+ /**
26
+ * Create a copy of this import, and make it type-only for TypeScript.
27
+ */
28
+ toTypeOnly(): ImportSymbol;
29
+ /**
30
+ * The unique ID based on name and from, disregarding typeOnly.
31
+ */
32
+ readonly id: EsSymbolId;
33
+ };
34
+ /**
35
+ * Create a new import symbol.
36
+ */
37
+ export declare function createImportSymbol(name: string, from: string, typeOnly?: boolean): ImportSymbol;
38
+ type EsSymbolId = string;
39
+ export {};