@bufbuild/protoplugin 1.9.0 → 2.0.0-alpha.1

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 (77) hide show
  1. package/dist/cjs/create-es-plugin.d.ts +7 -11
  2. package/dist/cjs/create-es-plugin.js +15 -13
  3. package/dist/cjs/ecmascript/file-preamble.js +8 -7
  4. package/dist/cjs/ecmascript/generated-file.d.ts +19 -20
  5. package/dist/cjs/ecmascript/generated-file.js +95 -108
  6. package/dist/cjs/ecmascript/import-path.d.ts +0 -6
  7. package/dist/cjs/ecmascript/import-path.js +1 -22
  8. package/dist/cjs/ecmascript/import-symbol.d.ts +1 -3
  9. package/dist/cjs/ecmascript/import-symbol.js +3 -5
  10. package/dist/cjs/ecmascript/index.d.ts +3 -16
  11. package/dist/cjs/ecmascript/index.js +3 -29
  12. package/dist/cjs/ecmascript/jsdoc.d.ts +2 -13
  13. package/dist/cjs/ecmascript/jsdoc.js +23 -31
  14. package/dist/cjs/ecmascript/names.d.ts +4 -0
  15. package/dist/cjs/ecmascript/names.js +119 -0
  16. package/dist/cjs/ecmascript/parameter.d.ts +1 -1
  17. package/dist/cjs/ecmascript/parameter.js +2 -2
  18. package/dist/cjs/ecmascript/printable.d.ts +36 -0
  19. package/dist/cjs/ecmascript/runtime-imports.d.ts +12 -23
  20. package/dist/cjs/ecmascript/runtime-imports.js +28 -29
  21. package/dist/cjs/ecmascript/safe-identifier.d.ts +6 -0
  22. package/dist/cjs/ecmascript/safe-identifier.js +90 -0
  23. package/dist/cjs/ecmascript/schema.d.ts +9 -8
  24. package/dist/cjs/ecmascript/schema.js +64 -20
  25. package/dist/cjs/index.d.ts +1 -5
  26. package/dist/cjs/index.js +6 -1
  27. package/dist/cjs/plugin.d.ts +1 -1
  28. package/dist/cjs/run-node.js +4 -3
  29. package/dist/cjs/source-code-info.d.ts +18 -0
  30. package/dist/cjs/source-code-info.js +259 -0
  31. package/dist/esm/create-es-plugin.d.ts +7 -11
  32. package/dist/esm/create-es-plugin.js +15 -13
  33. package/dist/esm/ecmascript/file-preamble.js +5 -4
  34. package/dist/esm/ecmascript/generated-file.d.ts +19 -20
  35. package/dist/esm/ecmascript/generated-file.js +97 -110
  36. package/dist/esm/ecmascript/import-path.d.ts +0 -6
  37. package/dist/esm/ecmascript/import-path.js +0 -20
  38. package/dist/esm/ecmascript/import-symbol.d.ts +1 -3
  39. package/dist/esm/ecmascript/import-symbol.js +3 -5
  40. package/dist/esm/ecmascript/index.d.ts +3 -16
  41. package/dist/esm/ecmascript/index.js +1 -18
  42. package/dist/esm/ecmascript/jsdoc.d.ts +2 -13
  43. package/dist/esm/ecmascript/jsdoc.js +20 -29
  44. package/dist/esm/ecmascript/names.d.ts +4 -0
  45. package/dist/esm/ecmascript/names.js +113 -0
  46. package/dist/esm/ecmascript/parameter.d.ts +1 -1
  47. package/dist/esm/ecmascript/parameter.js +2 -2
  48. package/dist/esm/ecmascript/printable.d.ts +36 -0
  49. package/dist/esm/ecmascript/{opaque-printables.js → printable.js} +1 -1
  50. package/dist/esm/ecmascript/runtime-imports.d.ts +12 -23
  51. package/dist/esm/ecmascript/runtime-imports.js +28 -29
  52. package/dist/esm/ecmascript/safe-identifier.d.ts +6 -0
  53. package/dist/esm/ecmascript/safe-identifier.js +86 -0
  54. package/dist/esm/ecmascript/schema.d.ts +9 -8
  55. package/dist/esm/ecmascript/schema.js +66 -22
  56. package/dist/esm/index.d.ts +1 -5
  57. package/dist/esm/index.js +1 -0
  58. package/dist/esm/plugin.d.ts +1 -1
  59. package/dist/esm/run-node.js +4 -3
  60. package/dist/esm/source-code-info.d.ts +18 -0
  61. package/dist/esm/source-code-info.js +252 -0
  62. package/package.json +2 -2
  63. package/dist/cjs/ecmascript/legacy-custom-options.d.ts +0 -40
  64. package/dist/cjs/ecmascript/legacy-custom-options.js +0 -121
  65. package/dist/cjs/ecmascript/legacy-gencommon.d.ts +0 -25
  66. package/dist/cjs/ecmascript/legacy-gencommon.js +0 -237
  67. package/dist/cjs/ecmascript/opaque-printables.d.ts +0 -27
  68. package/dist/cjs/ecmascript/reify-wkt.d.ts +0 -100
  69. package/dist/cjs/ecmascript/reify-wkt.js +0 -170
  70. package/dist/esm/ecmascript/legacy-custom-options.d.ts +0 -40
  71. package/dist/esm/ecmascript/legacy-custom-options.js +0 -115
  72. package/dist/esm/ecmascript/legacy-gencommon.d.ts +0 -25
  73. package/dist/esm/ecmascript/legacy-gencommon.js +0 -230
  74. package/dist/esm/ecmascript/opaque-printables.d.ts +0 -27
  75. package/dist/esm/ecmascript/reify-wkt.d.ts +0 -100
  76. package/dist/esm/ecmascript/reify-wkt.js +0 -166
  77. /package/dist/cjs/ecmascript/{opaque-printables.js → printable.js} +0 -0
@@ -0,0 +1,252 @@
1
+ // Copyright 2021-2024 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 { isFieldSet } from "@bufbuild/protobuf";
15
+ import { ScalarType, protoCamelCase } from "@bufbuild/protobuf/reflect";
16
+ import { Edition, FieldDescriptorProto_Label, FieldDescriptorProto_Type, FieldDescriptorProtoDesc, FieldOptions_JSType, FieldOptionsDesc, SourceCodeInfo_LocationDesc, } from "@bufbuild/protobuf/wkt";
17
+ /**
18
+ * Get comments on the package element in the protobuf source.
19
+ */
20
+ export function getPackageComments(desc) {
21
+ return findComments(desc.proto.sourceCodeInfo, [
22
+ FieldNumber.FileDescriptorProto_Package,
23
+ ]);
24
+ }
25
+ /**
26
+ * Get comments on the syntax element in the protobuf source.
27
+ */
28
+ export function getSyntaxComments(desc) {
29
+ return findComments(desc.proto.sourceCodeInfo, [
30
+ FieldNumber.FileDescriptorProto_Syntax,
31
+ ]);
32
+ }
33
+ /**
34
+ * Get comments on the element in the protobuf source.
35
+ */
36
+ export function getComments(desc) {
37
+ let path = [];
38
+ let file;
39
+ switch (desc.kind) {
40
+ case "enum":
41
+ path = desc.parent
42
+ ? [
43
+ ...getComments(desc.parent).sourcePath,
44
+ FieldNumber.DescriptorProto_EnumType,
45
+ desc.parent.proto.enumType.indexOf(desc.proto),
46
+ ]
47
+ : [
48
+ FieldNumber.FileDescriptorProto_EnumType,
49
+ desc.file.proto.enumType.indexOf(desc.proto),
50
+ ];
51
+ file = desc.file;
52
+ break;
53
+ case "oneof":
54
+ path = [
55
+ ...getComments(desc.parent).sourcePath,
56
+ FieldNumber.DescriptorProto_OneofDecl,
57
+ desc.parent.proto.oneofDecl.indexOf(desc.proto),
58
+ ];
59
+ file = desc.parent.file;
60
+ break;
61
+ case "message":
62
+ path = desc.parent
63
+ ? [
64
+ ...getComments(desc.parent).sourcePath,
65
+ FieldNumber.DescriptorProto_NestedType,
66
+ desc.parent.proto.nestedType.indexOf(desc.proto),
67
+ ]
68
+ : [
69
+ FieldNumber.FileDescriptorProto_MessageType,
70
+ desc.file.proto.messageType.indexOf(desc.proto),
71
+ ];
72
+ file = desc.file;
73
+ break;
74
+ case "enum_value":
75
+ path = [
76
+ ...getComments(desc.parent).sourcePath,
77
+ FieldNumber.EnumDescriptorProto_Value,
78
+ desc.parent.proto.value.indexOf(desc.proto),
79
+ ];
80
+ file = desc.parent.file;
81
+ break;
82
+ case "field":
83
+ path = [
84
+ ...getComments(desc.parent).sourcePath,
85
+ FieldNumber.DescriptorProto_Field,
86
+ desc.parent.proto.field.indexOf(desc.proto),
87
+ ];
88
+ file = desc.parent.file;
89
+ break;
90
+ case "extension":
91
+ path = desc.parent
92
+ ? [
93
+ ...getComments(desc.parent).sourcePath,
94
+ FieldNumber.DescriptorProto_Extension,
95
+ desc.parent.proto.extension.indexOf(desc.proto),
96
+ ]
97
+ : [
98
+ FieldNumber.FileDescriptorProto_Extension,
99
+ desc.file.proto.extension.indexOf(desc.proto),
100
+ ];
101
+ file = desc.file;
102
+ break;
103
+ case "service":
104
+ path = [
105
+ FieldNumber.FileDescriptorProto_Service,
106
+ desc.file.proto.service.indexOf(desc.proto),
107
+ ];
108
+ file = desc.file;
109
+ break;
110
+ case "rpc":
111
+ path = [
112
+ ...getComments(desc.parent).sourcePath,
113
+ FieldNumber.ServiceDescriptorProto_Method,
114
+ desc.parent.proto.method.indexOf(desc.proto),
115
+ ];
116
+ file = desc.parent.file;
117
+ break;
118
+ }
119
+ return findComments(file.proto.sourceCodeInfo, path);
120
+ }
121
+ /**
122
+ * Return a string that matches the definition of a field in the protobuf
123
+ * source. Does not take custom options into account.
124
+ */
125
+ export function getDeclarationString(desc) {
126
+ var _a;
127
+ if (desc.kind === "enum_value") {
128
+ let str = `${desc.name} = ${desc.number}`;
129
+ if (((_a = desc.proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) === true) {
130
+ str += " [deprecated = true]";
131
+ }
132
+ return str;
133
+ }
134
+ const file = desc.kind === "extension" ? desc.file : desc.parent.file;
135
+ const parts = [];
136
+ function typeName(f) {
137
+ if (f.message) {
138
+ return f.message.typeName;
139
+ }
140
+ if (f.enum) {
141
+ return f.enum.typeName;
142
+ }
143
+ return ScalarType[f.scalar].toLowerCase();
144
+ }
145
+ switch (desc.fieldKind) {
146
+ case "scalar":
147
+ case "enum":
148
+ case "message":
149
+ if (file.edition === Edition.EDITION_PROTO2 &&
150
+ isFieldSet(FieldDescriptorProtoDesc, desc.proto, "label") &&
151
+ desc.proto.label == FieldDescriptorProto_Label.REQUIRED) {
152
+ parts.push("required");
153
+ }
154
+ if (desc.optional) {
155
+ parts.push("optional");
156
+ }
157
+ parts.push(typeName(desc));
158
+ break;
159
+ case "list":
160
+ parts.push("repeated", typeName(desc));
161
+ break;
162
+ case "map": {
163
+ const k = ScalarType[desc.mapKey].toLowerCase();
164
+ const v = typeName(desc);
165
+ parts.push(`map<${k}, ${v}>`);
166
+ break;
167
+ }
168
+ }
169
+ parts.push(desc.name, "=", desc.number.toString());
170
+ const options = [];
171
+ const protoOptions = desc.proto.options;
172
+ if (protoOptions !== undefined &&
173
+ isFieldSet(FieldOptionsDesc, protoOptions, "packed")) {
174
+ options.push(`packed = ${protoOptions.packed.toString()}`);
175
+ }
176
+ if (isFieldSet(FieldDescriptorProtoDesc, desc.proto, "defaultValue")) {
177
+ let defaultValue = desc.proto.defaultValue;
178
+ if (desc.proto.type == FieldDescriptorProto_Type.BYTES ||
179
+ desc.proto.type == FieldDescriptorProto_Type.STRING) {
180
+ defaultValue = '"' + defaultValue.replace('"', '\\"') + '"';
181
+ }
182
+ options.push(`default = ${defaultValue}`);
183
+ }
184
+ if (desc.kind == "field" && desc.jsonName !== protoCamelCase(desc.name)) {
185
+ options.push(`json_name = "${desc.jsonName}"`);
186
+ }
187
+ if (protoOptions !== undefined &&
188
+ isFieldSet(FieldOptionsDesc, protoOptions, "jstype")) {
189
+ options.push(`jstype = ${FieldOptions_JSType[protoOptions.jstype]}`);
190
+ }
191
+ if (protoOptions !== undefined &&
192
+ isFieldSet(FieldOptionsDesc, protoOptions, "deprecated")) {
193
+ options.push(`deprecated = true`);
194
+ }
195
+ if (options.length > 0) {
196
+ parts.push("[" + options.join(", ") + "]");
197
+ }
198
+ return parts.join(" ");
199
+ }
200
+ /**
201
+ * Find comments.
202
+ */
203
+ function findComments(sourceCodeInfo, sourcePath) {
204
+ if (!sourceCodeInfo) {
205
+ return {
206
+ leadingDetached: [],
207
+ sourcePath,
208
+ };
209
+ }
210
+ for (const location of sourceCodeInfo.location) {
211
+ if (location.path.length !== sourcePath.length) {
212
+ continue;
213
+ }
214
+ if (location.path.some((value, index) => sourcePath[index] !== value)) {
215
+ continue;
216
+ }
217
+ return {
218
+ leadingDetached: location.leadingDetachedComments,
219
+ leading: isFieldSet(SourceCodeInfo_LocationDesc, location, "leadingComments")
220
+ ? location.leadingComments
221
+ : undefined,
222
+ trailing: isFieldSet(SourceCodeInfo_LocationDesc, location, "trailingComments")
223
+ ? location.trailingComments
224
+ : undefined,
225
+ sourcePath,
226
+ };
227
+ }
228
+ return {
229
+ leadingDetached: [],
230
+ sourcePath,
231
+ };
232
+ }
233
+ /**
234
+ * The following field numbers are used to find comments in
235
+ * google.protobuf.SourceCodeInfo.
236
+ */
237
+ var FieldNumber;
238
+ (function (FieldNumber) {
239
+ FieldNumber[FieldNumber["FileDescriptorProto_Package"] = 2] = "FileDescriptorProto_Package";
240
+ FieldNumber[FieldNumber["FileDescriptorProto_MessageType"] = 4] = "FileDescriptorProto_MessageType";
241
+ FieldNumber[FieldNumber["FileDescriptorProto_EnumType"] = 5] = "FileDescriptorProto_EnumType";
242
+ FieldNumber[FieldNumber["FileDescriptorProto_Service"] = 6] = "FileDescriptorProto_Service";
243
+ FieldNumber[FieldNumber["FileDescriptorProto_Extension"] = 7] = "FileDescriptorProto_Extension";
244
+ FieldNumber[FieldNumber["FileDescriptorProto_Syntax"] = 12] = "FileDescriptorProto_Syntax";
245
+ FieldNumber[FieldNumber["DescriptorProto_Field"] = 2] = "DescriptorProto_Field";
246
+ FieldNumber[FieldNumber["DescriptorProto_NestedType"] = 3] = "DescriptorProto_NestedType";
247
+ FieldNumber[FieldNumber["DescriptorProto_EnumType"] = 4] = "DescriptorProto_EnumType";
248
+ FieldNumber[FieldNumber["DescriptorProto_Extension"] = 6] = "DescriptorProto_Extension";
249
+ FieldNumber[FieldNumber["DescriptorProto_OneofDecl"] = 8] = "DescriptorProto_OneofDecl";
250
+ FieldNumber[FieldNumber["EnumDescriptorProto_Value"] = 2] = "EnumDescriptorProto_Value";
251
+ FieldNumber[FieldNumber["ServiceDescriptorProto_Method"] = 2] = "ServiceDescriptorProto_Method";
252
+ })(FieldNumber || (FieldNumber = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protoplugin",
3
- "version": "1.9.0",
3
+ "version": "2.0.0-alpha.1",
4
4
  "license": "(Apache-2.0 AND BSD-3-Clause)",
5
5
  "description": "Helps to create your own Protocol Buffers code generators.",
6
6
  "repository": {
@@ -36,7 +36,7 @@
36
36
  }
37
37
  },
38
38
  "dependencies": {
39
- "@bufbuild/protobuf": "1.9.0",
39
+ "@bufbuild/protobuf": "2.0.0-alpha.1",
40
40
  "@typescript/vfs": "^1.4.0",
41
41
  "typescript": "4.5.2"
42
42
  },
@@ -1,40 +0,0 @@
1
- import type { AnyDesc, MessageType } from "@bufbuild/protobuf";
2
- import { Message, ScalarType } from "@bufbuild/protobuf";
3
- /**
4
- * @deprecated Please use extensions instead.
5
- *
6
- * Returns the value of a custom option with a scalar type.
7
- *
8
- * If no option is found, returns undefined.
9
- */
10
- export declare function findCustomScalarOption<T extends ScalarType>(desc: AnyDesc, extensionNumber: number, scalarType: T): ScalarValue<T> | undefined;
11
- /**
12
- * @deprecated Please use extensions instead.
13
- *
14
- * Returns the value of a custom message option for the given descriptor and
15
- * extension number.
16
- * The msgType param is then used to deserialize the message for returning to
17
- * the caller.
18
- *
19
- * If no options are found, returns undefined.
20
- *
21
- * If the message option is unable to be read or deserialized, an error will
22
- * be thrown.
23
- */
24
- export declare function findCustomMessageOption<T extends Message<T>>(desc: AnyDesc, extensionNumber: number, msgType: MessageType<T>): T | undefined;
25
- /**
26
- * @deprecated Please use extensions instead.
27
- *
28
- * Returns the value of a custom enum option for the given descriptor and
29
- * extension number.
30
- *
31
- * If no options are found, returns undefined.
32
- */
33
- export declare function findCustomEnumOption(desc: AnyDesc, extensionNumber: number): number | undefined;
34
- /**
35
- * @deprecated Please use extensions instead.
36
- *
37
- * ScalarValue is a conditional type that pairs a ScalarType value with its concrete type.
38
- */
39
- type ScalarValue<T> = T extends ScalarType.STRING ? string : T extends ScalarType.INT32 ? number : T extends ScalarType.UINT32 ? number : T extends ScalarType.UINT32 ? number : T extends ScalarType.SINT32 ? number : T extends ScalarType.FIXED32 ? number : T extends ScalarType.SFIXED32 ? number : T extends ScalarType.FLOAT ? number : T extends ScalarType.DOUBLE ? number : T extends ScalarType.INT64 ? bigint | string : T extends ScalarType.SINT64 ? bigint | string : T extends ScalarType.SFIXED64 ? bigint | string : T extends ScalarType.UINT64 ? bigint | string : T extends ScalarType.FIXED64 ? bigint | string : T extends ScalarType.BOOL ? boolean : T extends ScalarType.BYTES ? Uint8Array : never;
40
- export {};
@@ -1,121 +0,0 @@
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.findCustomEnumOption = exports.findCustomMessageOption = exports.findCustomScalarOption = void 0;
17
- const protobuf_1 = require("@bufbuild/protobuf");
18
- /**
19
- * @deprecated Please use extensions instead.
20
- *
21
- * Returns the value of a custom option with a scalar type.
22
- *
23
- * If no option is found, returns undefined.
24
- */
25
- function findCustomScalarOption(desc, extensionNumber, scalarType) {
26
- const reader = createBinaryReader(desc, extensionNumber);
27
- if (reader) {
28
- switch (scalarType) {
29
- case protobuf_1.ScalarType.INT32:
30
- return reader.int32();
31
- case protobuf_1.ScalarType.UINT32:
32
- return reader.uint32();
33
- case protobuf_1.ScalarType.SINT32:
34
- return reader.sint32();
35
- case protobuf_1.ScalarType.FIXED32:
36
- return reader.fixed32();
37
- case protobuf_1.ScalarType.SFIXED32:
38
- return reader.sfixed32();
39
- case protobuf_1.ScalarType.FLOAT:
40
- return reader.float();
41
- case protobuf_1.ScalarType.DOUBLE:
42
- return reader.double();
43
- case protobuf_1.ScalarType.INT64:
44
- return reader.int64();
45
- case protobuf_1.ScalarType.SINT64:
46
- return reader.sint64();
47
- case protobuf_1.ScalarType.SFIXED64:
48
- return reader.sfixed64();
49
- case protobuf_1.ScalarType.UINT64:
50
- return reader.uint64();
51
- case protobuf_1.ScalarType.FIXED64:
52
- return reader.fixed64();
53
- case protobuf_1.ScalarType.BOOL:
54
- return reader.bool();
55
- case protobuf_1.ScalarType.BYTES:
56
- return reader.bytes();
57
- case protobuf_1.ScalarType.STRING:
58
- return reader.string();
59
- default: {
60
- break;
61
- }
62
- }
63
- }
64
- return undefined;
65
- }
66
- exports.findCustomScalarOption = findCustomScalarOption;
67
- /**
68
- * @deprecated Please use extensions instead.
69
- *
70
- * Returns the value of a custom message option for the given descriptor and
71
- * extension number.
72
- * The msgType param is then used to deserialize the message for returning to
73
- * the caller.
74
- *
75
- * If no options are found, returns undefined.
76
- *
77
- * If the message option is unable to be read or deserialized, an error will
78
- * be thrown.
79
- */
80
- function findCustomMessageOption(desc, extensionNumber, msgType) {
81
- const reader = createBinaryReader(desc, extensionNumber);
82
- if (reader) {
83
- try {
84
- const data = reader.bytes();
85
- return msgType.fromBinary(data);
86
- }
87
- catch (e) {
88
- const innerMessage = e instanceof Error ? e.message : String(e);
89
- throw new Error(`failed to access message option: ${innerMessage}`);
90
- }
91
- }
92
- return undefined;
93
- }
94
- exports.findCustomMessageOption = findCustomMessageOption;
95
- /**
96
- * @deprecated Please use extensions instead.
97
- *
98
- * Returns the value of a custom enum option for the given descriptor and
99
- * extension number.
100
- *
101
- * If no options are found, returns undefined.
102
- */
103
- function findCustomEnumOption(desc, extensionNumber) {
104
- return findCustomScalarOption(desc, extensionNumber, protobuf_1.ScalarType.INT32);
105
- }
106
- exports.findCustomEnumOption = findCustomEnumOption;
107
- /**
108
- * Returns a binary reader for the given descriptor and extension number.
109
- */
110
- function createBinaryReader(desc, extensionNumber) {
111
- const opt = desc.proto.options;
112
- let reader = undefined;
113
- if (opt !== undefined) {
114
- const unknownFields = protobuf_1.proto3.bin.listUnknownFields(opt);
115
- const field = unknownFields.find((f) => f.no === extensionNumber);
116
- if (field) {
117
- reader = new protobuf_1.BinaryReader(field.data);
118
- }
119
- }
120
- return reader;
121
- }
@@ -1,25 +0,0 @@
1
- import type { DescExtension, DescField } from "@bufbuild/protobuf";
2
- import type { GeneratedFile, Printable } from "./generated-file.js";
3
- import type { ImportSymbol } from "./import-symbol.js";
4
- /**
5
- * @deprecated Please use GeneratedFile.string() instead
6
- */
7
- export declare function literalString(value: string): string;
8
- /**
9
- * @deprecated
10
- */
11
- export declare function getFieldTyping(field: DescField | DescExtension, file: GeneratedFile): {
12
- typing: Printable;
13
- optional: boolean;
14
- };
15
- /**
16
- * @deprecated
17
- */
18
- export declare function getFieldExplicitDefaultValue(field: DescField | DescExtension, protoInt64Symbol: ImportSymbol): Printable | undefined;
19
- /**
20
- * @deprecated
21
- */
22
- export declare function getFieldIntrinsicDefaultValue(field: DescField): {
23
- defaultValue: Printable | undefined;
24
- typingInferrable: boolean;
25
- };
@@ -1,237 +0,0 @@
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.getFieldIntrinsicDefaultValue = exports.getFieldExplicitDefaultValue = exports.getFieldTyping = exports.literalString = void 0;
17
- const protobuf_1 = require("@bufbuild/protobuf");
18
- const { localName, getUnwrappedFieldType, scalarZeroValue } = protobuf_1.codegenInfo;
19
- /**
20
- * @deprecated Please use GeneratedFile.string() instead
21
- */
22
- function literalString(value) {
23
- return ('"' +
24
- value
25
- .split("\\")
26
- .join("\\\\")
27
- .split('"')
28
- .join('\\"')
29
- .split("\r")
30
- .join("\\r")
31
- .split("\n")
32
- .join("\\n") +
33
- '"');
34
- }
35
- exports.literalString = literalString;
36
- /**
37
- * @deprecated
38
- */
39
- function getFieldTyping(field, file) {
40
- const typing = [];
41
- let optional = false;
42
- switch (field.fieldKind) {
43
- case "scalar":
44
- typing.push(scalarTypeScriptType(field.scalar, field.longType));
45
- optional = field.optional;
46
- break;
47
- case "message": {
48
- const baseType = getUnwrappedFieldType(field);
49
- if (baseType !== undefined) {
50
- typing.push(scalarTypeScriptType(baseType, protobuf_1.LongType.BIGINT));
51
- }
52
- else {
53
- typing.push(file.import(field.message).toTypeOnly());
54
- }
55
- optional = true;
56
- break;
57
- }
58
- case "enum":
59
- typing.push(file.import(field.enum).toTypeOnly());
60
- optional = field.optional;
61
- break;
62
- case "map": {
63
- let keyType;
64
- switch (field.mapKey) {
65
- case protobuf_1.ScalarType.INT32:
66
- case protobuf_1.ScalarType.FIXED32:
67
- case protobuf_1.ScalarType.UINT32:
68
- case protobuf_1.ScalarType.SFIXED32:
69
- case protobuf_1.ScalarType.SINT32:
70
- keyType = "number";
71
- break;
72
- default:
73
- keyType = "string";
74
- break;
75
- }
76
- let valueType;
77
- switch (field.mapValue.kind) {
78
- case "scalar":
79
- valueType = scalarTypeScriptType(field.mapValue.scalar, protobuf_1.LongType.BIGINT);
80
- break;
81
- case "message":
82
- valueType = file.import(field.mapValue.message).toTypeOnly();
83
- break;
84
- case "enum":
85
- valueType = file.import(field.mapValue.enum).toTypeOnly();
86
- break;
87
- }
88
- typing.push("{ [key: ", keyType, "]: ", valueType, " }");
89
- optional = false;
90
- break;
91
- }
92
- }
93
- if (field.repeated) {
94
- typing.push("[]");
95
- optional = false;
96
- }
97
- return { typing, optional };
98
- }
99
- exports.getFieldTyping = getFieldTyping;
100
- /**
101
- * @deprecated
102
- */
103
- function getFieldExplicitDefaultValue(field, protoInt64Symbol) {
104
- switch (field.fieldKind) {
105
- case "enum": {
106
- const value = field.enum.values.find((v) => v.number === field.getDefaultValue());
107
- if (value !== undefined) {
108
- return [value.parent, ".", localName(value)];
109
- }
110
- break;
111
- }
112
- case "scalar": {
113
- const defaultValue = field.getDefaultValue();
114
- if (defaultValue === undefined) {
115
- break;
116
- }
117
- switch (field.scalar) {
118
- case protobuf_1.ScalarType.FLOAT:
119
- case protobuf_1.ScalarType.DOUBLE: {
120
- return defaultValue;
121
- }
122
- case protobuf_1.ScalarType.INT64:
123
- case protobuf_1.ScalarType.SINT64:
124
- case protobuf_1.ScalarType.SFIXED64:
125
- return [protoInt64Symbol, `.parse("${defaultValue.toString()}")`];
126
- case protobuf_1.ScalarType.UINT64:
127
- case protobuf_1.ScalarType.FIXED64:
128
- return [protoInt64Symbol, `.uParse("${defaultValue.toString()}")`];
129
- case protobuf_1.ScalarType.INT32:
130
- case protobuf_1.ScalarType.FIXED32:
131
- case protobuf_1.ScalarType.UINT32:
132
- case protobuf_1.ScalarType.SFIXED32:
133
- case protobuf_1.ScalarType.SINT32:
134
- return defaultValue;
135
- case protobuf_1.ScalarType.BOOL: {
136
- return defaultValue;
137
- }
138
- case protobuf_1.ScalarType.STRING: {
139
- if (typeof defaultValue == "string") {
140
- return literalString(defaultValue);
141
- }
142
- break;
143
- }
144
- case protobuf_1.ScalarType.BYTES: {
145
- if (defaultValue instanceof Uint8Array) {
146
- return defaultValue;
147
- }
148
- break;
149
- }
150
- }
151
- break;
152
- }
153
- default:
154
- break;
155
- }
156
- return undefined;
157
- }
158
- exports.getFieldExplicitDefaultValue = getFieldExplicitDefaultValue;
159
- /**
160
- * @deprecated
161
- */
162
- function getFieldIntrinsicDefaultValue(field) {
163
- if (field.repeated) {
164
- return {
165
- defaultValue: "[]",
166
- typingInferrable: false,
167
- };
168
- }
169
- if (field.fieldKind == "map") {
170
- return {
171
- defaultValue: "{}",
172
- typingInferrable: false,
173
- };
174
- }
175
- let defaultValue = undefined;
176
- let typingInferrable = false;
177
- if (field.parent.file.syntax == "proto3") {
178
- switch (field.fieldKind) {
179
- case "enum": {
180
- if (!field.optional) {
181
- const zeroValue = field.enum.values.find((v) => v.number === 0);
182
- if (zeroValue === undefined) {
183
- throw new Error("invalid proto3 enum: missing 0 value");
184
- }
185
- defaultValue = [field.enum, ".", localName(zeroValue)];
186
- typingInferrable = true;
187
- }
188
- break;
189
- }
190
- case "scalar":
191
- if (!field.optional) {
192
- typingInferrable = true;
193
- if (field.scalar === protobuf_1.ScalarType.STRING) {
194
- defaultValue = literalString("");
195
- }
196
- else {
197
- defaultValue = scalarZeroValue(field.scalar, field.longType);
198
- if (typeof defaultValue === "string") {
199
- defaultValue = literalString(defaultValue);
200
- }
201
- }
202
- }
203
- break;
204
- default:
205
- break;
206
- }
207
- }
208
- return {
209
- defaultValue,
210
- typingInferrable,
211
- };
212
- }
213
- exports.getFieldIntrinsicDefaultValue = getFieldIntrinsicDefaultValue;
214
- /**
215
- * @deprecated
216
- */
217
- function scalarTypeScriptType(type, longType) {
218
- switch (type) {
219
- case protobuf_1.ScalarType.STRING:
220
- return "string";
221
- case protobuf_1.ScalarType.BOOL:
222
- return "boolean";
223
- case protobuf_1.ScalarType.UINT64:
224
- case protobuf_1.ScalarType.SFIXED64:
225
- case protobuf_1.ScalarType.FIXED64:
226
- case protobuf_1.ScalarType.SINT64:
227
- case protobuf_1.ScalarType.INT64:
228
- if (longType === protobuf_1.LongType.STRING) {
229
- return "string";
230
- }
231
- return "bigint";
232
- case protobuf_1.ScalarType.BYTES:
233
- return "Uint8Array";
234
- default:
235
- return "number";
236
- }
237
- }