@bufbuild/protobuf 1.1.1 → 1.2.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 (52) hide show
  1. package/dist/cjs/codegen-info.js +2 -0
  2. package/dist/cjs/google/protobuf/any_pb.js +4 -4
  3. package/dist/cjs/google/protobuf/api_pb.js +4 -4
  4. package/dist/cjs/google/protobuf/compiler/plugin_pb.js +4 -4
  5. package/dist/cjs/google/protobuf/descriptor_pb.js +106 -28
  6. package/dist/cjs/google/protobuf/duration_pb.js +1 -1
  7. package/dist/cjs/google/protobuf/empty_pb.js +1 -1
  8. package/dist/cjs/google/protobuf/field_mask_pb.js +2 -2
  9. package/dist/cjs/google/protobuf/source_context_pb.js +1 -1
  10. package/dist/cjs/google/protobuf/struct_pb.js +5 -5
  11. package/dist/cjs/google/protobuf/timestamp_pb.js +2 -2
  12. package/dist/cjs/google/protobuf/type_pb.js +27 -6
  13. package/dist/cjs/google/protobuf/wrappers_pb.js +9 -9
  14. package/dist/cjs/index.js +0 -3
  15. package/dist/cjs/private/binary-format-common.js +44 -6
  16. package/dist/cjs/private/names.js +128 -97
  17. package/dist/cjs/proto-base64.js +2 -10
  18. package/dist/esm/codegen-info.js +3 -1
  19. package/dist/esm/create-descriptor-set.js +1 -1
  20. package/dist/esm/google/protobuf/any_pb.js +5 -4
  21. package/dist/esm/google/protobuf/api_pb.js +7 -4
  22. package/dist/esm/google/protobuf/compiler/plugin_pb.js +8 -4
  23. package/dist/esm/google/protobuf/descriptor_pb.js +132 -27
  24. package/dist/esm/google/protobuf/duration_pb.js +2 -1
  25. package/dist/esm/google/protobuf/empty_pb.js +2 -1
  26. package/dist/esm/google/protobuf/field_mask_pb.js +3 -2
  27. package/dist/esm/google/protobuf/source_context_pb.js +2 -1
  28. package/dist/esm/google/protobuf/struct_pb.js +8 -5
  29. package/dist/esm/google/protobuf/timestamp_pb.js +3 -2
  30. package/dist/esm/google/protobuf/type_pb.js +32 -6
  31. package/dist/esm/google/protobuf/wrappers_pb.js +18 -9
  32. package/dist/esm/index.js +1 -4
  33. package/dist/esm/private/binary-format-common.js +45 -7
  34. package/dist/esm/private/message-type.js +1 -1
  35. package/dist/esm/private/names.js +125 -96
  36. package/dist/esm/private/util-common.js +1 -1
  37. package/dist/esm/proto-base64.js +2 -10
  38. package/dist/types/codegen-info.d.ts +3 -1
  39. package/dist/types/google/protobuf/any_pb.d.ts +3 -3
  40. package/dist/types/google/protobuf/api_pb.d.ts +1 -1
  41. package/dist/types/google/protobuf/descriptor_pb.d.ts +107 -3
  42. package/dist/types/google/protobuf/struct_pb.d.ts +1 -1
  43. package/dist/types/google/protobuf/timestamp_pb.d.ts +1 -1
  44. package/dist/types/google/protobuf/type_pb.d.ts +19 -1
  45. package/dist/types/index.d.ts +5 -4
  46. package/dist/types/private/binary-format-common.d.ts +3 -2
  47. package/dist/types/private/json-format-common.d.ts +2 -1
  48. package/dist/types/private/message-type.d.ts +2 -1
  49. package/dist/types/private/names.d.ts +9 -0
  50. package/dist/types/private/scalars.d.ts +2 -1
  51. package/dist/types/proto-base64.d.ts +1 -9
  52. package/package.json +2 -2
@@ -13,7 +13,7 @@
13
13
  // See the License for the specific language governing permissions and
14
14
  // limitations under the License.
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.findEnumSharedPrefix = exports.fieldJsonName = exports.localOneofName = exports.localFieldName = exports.localName = void 0;
16
+ exports.safeIdentifier = exports.safeObjectProperty = exports.findEnumSharedPrefix = exports.fieldJsonName = exports.localOneofName = exports.localFieldName = exports.localName = void 0;
17
17
  /**
18
18
  * Returns the name of a protobuf element in generated code.
19
19
  *
@@ -34,10 +34,12 @@ function localName(desc) {
34
34
  const pkg = desc.file.proto.package;
35
35
  const offset = pkg === undefined ? 0 : pkg.length + 1;
36
36
  const name = desc.typeName.substring(offset).replace(/\./g, "_");
37
- if (reservedIdent[name]) {
38
- return name + "$";
39
- }
40
- return name;
37
+ // For services, we only care about safe identifiers, not safe object properties,
38
+ // but we have shipped v1 with a bug that respected object properties, and we
39
+ // do not want to introduce a breaking change, so we continue to escape for
40
+ // safe object properties.
41
+ // See https://github.com/bufbuild/protobuf-es/pull/391
42
+ return (0, exports.safeObjectProperty)((0, exports.safeIdentifier)(name));
41
43
  }
42
44
  case "enum_value": {
43
45
  const sharedPrefix = desc.parent.sharedPrefix;
@@ -45,10 +47,7 @@ function localName(desc) {
45
47
  return desc.name;
46
48
  }
47
49
  const name = desc.name.substring(sharedPrefix.length);
48
- if (reservedObjectProperties[name]) {
49
- return name + "$";
50
- }
51
- return name;
50
+ return (0, exports.safeObjectProperty)(name);
52
51
  }
53
52
  case "rpc": {
54
53
  let name = desc.name;
@@ -56,10 +55,7 @@ function localName(desc) {
56
55
  return name;
57
56
  }
58
57
  name = name[0].toLowerCase() + name.substring(1);
59
- if (reservedObjectProperties[name]) {
60
- return name + "$";
61
- }
62
- return name;
58
+ return (0, exports.safeObjectProperty)(name);
63
59
  }
64
60
  }
65
61
  }
@@ -68,15 +64,12 @@ exports.localName = localName;
68
64
  * Returns the name of a field in generated code.
69
65
  */
70
66
  function localFieldName(protoName, inOneof) {
71
- let name = protoCamelCase(protoName);
67
+ const name = protoCamelCase(protoName);
72
68
  if (inOneof) {
73
69
  // oneof member names are not properties, but values of the `case` property.
74
70
  return name;
75
71
  }
76
- if (reservedObjectProperties[name] || reservedMessageProperties[name]) {
77
- name = name + "$";
78
- }
79
- return name;
72
+ return (0, exports.safeObjectProperty)(safeMessageProperty(name));
80
73
  }
81
74
  exports.localFieldName = localFieldName;
82
75
  /**
@@ -156,91 +149,129 @@ function protoCamelCase(snakeCase) {
156
149
  }
157
150
  return b.join("");
158
151
  }
159
- // Names that cannot be used for identifiers, such as class names,
160
- // but _can_ be used for object properties.
161
- const reservedIdent = {
152
+ /**
153
+ * Names that cannot be used for identifiers, such as class names,
154
+ * but _can_ be used for object properties.
155
+ */
156
+ const reservedIdentifiers = new Set([
162
157
  // ECMAScript 2015 keywords
163
- break: true,
164
- case: true,
165
- catch: true,
166
- class: true,
167
- const: true,
168
- continue: true,
169
- debugger: true,
170
- default: true,
171
- delete: true,
172
- do: true,
173
- else: true,
174
- export: true,
175
- extends: true,
176
- false: true,
177
- finally: true,
178
- for: true,
179
- function: true,
180
- if: true,
181
- import: true,
182
- in: true,
183
- instanceof: true,
184
- new: true,
185
- null: true,
186
- return: true,
187
- super: true,
188
- switch: true,
189
- this: true,
190
- throw: true,
191
- true: true,
192
- try: true,
193
- typeof: true,
194
- var: true,
195
- void: true,
196
- while: true,
197
- with: true,
198
- yield: true,
158
+ "break",
159
+ "case",
160
+ "catch",
161
+ "class",
162
+ "const",
163
+ "continue",
164
+ "debugger",
165
+ "default",
166
+ "delete",
167
+ "do",
168
+ "else",
169
+ "export",
170
+ "extends",
171
+ "false",
172
+ "finally",
173
+ "for",
174
+ "function",
175
+ "if",
176
+ "import",
177
+ "in",
178
+ "instanceof",
179
+ "new",
180
+ "null",
181
+ "return",
182
+ "super",
183
+ "switch",
184
+ "this",
185
+ "throw",
186
+ "true",
187
+ "try",
188
+ "typeof",
189
+ "var",
190
+ "void",
191
+ "while",
192
+ "with",
193
+ "yield",
199
194
  // ECMAScript 2015 future reserved keywords
200
- enum: true,
201
- implements: true,
202
- interface: true,
203
- let: true,
204
- package: true,
205
- private: true,
206
- protected: true,
207
- public: true,
208
- static: true,
195
+ "enum",
196
+ "implements",
197
+ "interface",
198
+ "let",
199
+ "package",
200
+ "private",
201
+ "protected",
202
+ "public",
203
+ "static",
209
204
  // Class name cannot be 'Object' when targeting ES5 with module CommonJS
210
- Object: true,
205
+ "Object",
211
206
  // TypeScript keywords that cannot be used for types (as opposed to variables)
212
- bigint: true,
213
- number: true,
214
- boolean: true,
215
- string: true,
216
- object: true,
207
+ "bigint",
208
+ "number",
209
+ "boolean",
210
+ "string",
211
+ "object",
217
212
  // Identifiers reserved for the runtime, so we can generate legible code
218
- globalThis: true,
219
- Uint8Array: true,
220
- Partial: true,
221
- };
222
- // Names that cannot be used for object properties because they are reserved
223
- // by built-in JavaScript properties.
224
- const reservedObjectProperties = {
213
+ "globalThis",
214
+ "Uint8Array",
215
+ "Partial",
216
+ ]);
217
+ /**
218
+ * Names that cannot be used for object properties because they are reserved
219
+ * by built-in JavaScript properties.
220
+ */
221
+ const reservedObjectProperties = new Set([
225
222
  // names reserved by JavaScript
226
- constructor: true,
227
- toString: true,
228
- toJSON: true,
229
- valueOf: true,
230
- };
231
- // Names that cannot be used for object properties because they are reserved
232
- // by the runtime.
233
- const reservedMessageProperties = {
223
+ "constructor",
224
+ "toString",
225
+ "toJSON",
226
+ "valueOf",
227
+ ]);
228
+ /**
229
+ * Names that cannot be used for object properties because they are reserved
230
+ * by the runtime.
231
+ */
232
+ const reservedMessageProperties = new Set([
234
233
  // names reserved by the runtime
235
- getType: true,
236
- clone: true,
237
- equals: true,
238
- fromBinary: true,
239
- fromJson: true,
240
- fromJsonString: true,
241
- toBinary: true,
242
- toJson: true,
243
- toJsonString: true,
234
+ "getType",
235
+ "clone",
236
+ "equals",
237
+ "fromBinary",
238
+ "fromJson",
239
+ "fromJsonString",
240
+ "toBinary",
241
+ "toJson",
242
+ "toJsonString",
244
243
  // names reserved by the runtime for the future
245
- toObject: true,
244
+ "toObject",
245
+ ]);
246
+ const fallback = (name) => `${name}$`;
247
+ /**
248
+ * Will wrap names that are Object prototype properties or names reserved
249
+ * for `Message`s.
250
+ */
251
+ const safeMessageProperty = (name) => {
252
+ if (reservedMessageProperties.has(name)) {
253
+ return fallback(name);
254
+ }
255
+ return name;
256
+ };
257
+ /**
258
+ * Names that cannot be used for object properties because they are reserved
259
+ * by built-in JavaScript properties.
260
+ */
261
+ const safeObjectProperty = (name) => {
262
+ if (reservedObjectProperties.has(name)) {
263
+ return fallback(name);
264
+ }
265
+ return name;
266
+ };
267
+ exports.safeObjectProperty = safeObjectProperty;
268
+ /**
269
+ * Names that can be used for identifiers or class properties
270
+ */
271
+ const safeIdentifier = (name) => {
272
+ if (reservedIdentifiers.has(name)) {
273
+ return fallback(name);
274
+ }
275
+ return name;
246
276
  };
277
+ exports.safeIdentifier = safeIdentifier;
@@ -90,15 +90,7 @@ exports.protoBase64 = {
90
90
  return bytes.subarray(0, bytePos);
91
91
  },
92
92
  /**
93
- * Decodes a base64 string to a byte array.
94
- *
95
- * - ignores white-space, including line breaks and tabs
96
- * - allows inner padding (can decode concatenated base64 strings)
97
- * - does not require padding
98
- * - understands base64url encoding:
99
- * "-" instead of "+",
100
- * "_" instead of "/",
101
- * no padding
93
+ * Encode a byte array to a base64 string.
102
94
  */
103
95
  enc(bytes) {
104
96
  let base64 = "", groupPos = 0, // position in base64 group
@@ -124,7 +116,7 @@ exports.protoBase64 = {
124
116
  break;
125
117
  }
126
118
  }
127
- // padding required?
119
+ // add output padding
128
120
  if (groupPos) {
129
121
  base64 += encTable[p];
130
122
  base64 += "=";
@@ -11,7 +11,7 @@
11
11
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
- import { localName } from "./private/names.js";
14
+ import { localName, safeIdentifier, safeObjectProperty, } from "./private/names.js";
15
15
  import { getUnwrappedFieldType } from "./private/field-wrapper.js";
16
16
  import { scalarDefaultValue } from "./private/scalars.js";
17
17
  import { reifyWkt } from "./private/reify-wkt.js";
@@ -22,6 +22,8 @@ export const codegenInfo = {
22
22
  reifyWkt,
23
23
  getUnwrappedFieldType,
24
24
  scalarDefaultValue,
25
+ safeIdentifier,
26
+ safeObjectProperty,
25
27
  // prettier-ignore
26
28
  symbols: {
27
29
  proto2: { typeOnly: false, privateImportPath: "./proto2.js", publicImportPath: packageName },
@@ -11,7 +11,7 @@
11
11
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
- import { FieldDescriptorProto_Label, FieldDescriptorProto_Type, FileDescriptorSet, MethodOptions_IdempotencyLevel, } from "./google/protobuf/descriptor_pb.js";
14
+ import { FieldDescriptorProto_Label, FieldDescriptorProto_Type, FileDescriptorProto, FileDescriptorSet, MethodDescriptorProto, MethodOptions_IdempotencyLevel, ServiceDescriptorProto, SourceCodeInfo, } from "./google/protobuf/descriptor_pb.js";
15
15
  import { assert } from "./private/assert.js";
16
16
  import { ScalarType } from "./field.js";
17
17
  import { MethodIdempotency, MethodKind } from "./service-type.js";
@@ -43,7 +43,7 @@ import { proto3 } from "../../proto3.js";
43
43
  * foo = any.unpack(Foo.getDefaultInstance());
44
44
  * }
45
45
  *
46
- * Example 3: Pack and unpack a message in Python.
46
+ * Example 3: Pack and unpack a message in Python.
47
47
  *
48
48
  * foo = Foo(...)
49
49
  * any = Any()
@@ -53,7 +53,7 @@ import { proto3 } from "../../proto3.js";
53
53
  * any.Unpack(foo)
54
54
  * ...
55
55
  *
56
- * Example 4: Pack and unpack a message in Go
56
+ * Example 4: Pack and unpack a message in Go
57
57
  *
58
58
  * foo := &pb.Foo{...}
59
59
  * any, err := anypb.New(foo)
@@ -73,7 +73,7 @@ import { proto3 } from "../../proto3.js";
73
73
  * name "y.z".
74
74
  *
75
75
  * JSON
76
- *
76
+ * ====
77
77
  * The JSON representation of an `Any` value uses the regular
78
78
  * representation of the deserialized, embedded message, with an
79
79
  * additional field `@type` which contains the type URL. Example:
@@ -103,7 +103,7 @@ import { proto3 } from "../../proto3.js";
103
103
  *
104
104
  * @generated from message google.protobuf.Any
105
105
  */
106
- export class Any extends Message {
106
+ class Any extends Message {
107
107
  constructor(data) {
108
108
  super();
109
109
  /**
@@ -266,3 +266,4 @@ Any.fields = proto3.util.newFieldList(() => [
266
266
  { no: 1, name: "type_url", kind: "scalar", T: 9 /* ScalarType.STRING */ },
267
267
  { no: 2, name: "value", kind: "scalar", T: 12 /* ScalarType.BYTES */ },
268
268
  ]);
269
+ export { Any };
@@ -28,7 +28,7 @@ import { proto3 } from "../../proto3.js";
28
28
  *
29
29
  * @generated from message google.protobuf.Api
30
30
  */
31
- export class Api extends Message {
31
+ class Api extends Message {
32
32
  constructor(data) {
33
33
  super();
34
34
  /**
@@ -113,12 +113,13 @@ Api.fields = proto3.util.newFieldList(() => [
113
113
  { no: 6, name: "mixins", kind: "message", T: Mixin, repeated: true },
114
114
  { no: 7, name: "syntax", kind: "enum", T: proto3.getEnumType(Syntax) },
115
115
  ]);
116
+ export { Api };
116
117
  /**
117
118
  * Method represents a method of an API interface.
118
119
  *
119
120
  * @generated from message google.protobuf.Method
120
121
  */
121
- export class Method extends Message {
122
+ class Method extends Message {
122
123
  constructor(data) {
123
124
  super();
124
125
  /**
@@ -189,6 +190,7 @@ Method.fields = proto3.util.newFieldList(() => [
189
190
  { no: 6, name: "options", kind: "message", T: Option, repeated: true },
190
191
  { no: 7, name: "syntax", kind: "enum", T: proto3.getEnumType(Syntax) },
191
192
  ]);
193
+ export { Method };
192
194
  /**
193
195
  * Declares an API Interface to be included in this interface. The including
194
196
  * interface must redeclare all the methods from the included interface, but
@@ -237,7 +239,7 @@ Method.fields = proto3.util.newFieldList(() => [
237
239
  * The mixin construct implies that all methods in `AccessControl` are
238
240
  * also declared with same name and request/response types in
239
241
  * `Storage`. A documentation generator or annotation processor will
240
- * see the effective `Storage.GetAcl` method after inheriting
242
+ * see the effective `Storage.GetAcl` method after inherting
241
243
  * documentation and annotations as follows:
242
244
  *
243
245
  * service Storage {
@@ -271,7 +273,7 @@ Method.fields = proto3.util.newFieldList(() => [
271
273
  *
272
274
  * @generated from message google.protobuf.Mixin
273
275
  */
274
- export class Mixin extends Message {
276
+ class Mixin extends Message {
275
277
  constructor(data) {
276
278
  super();
277
279
  /**
@@ -308,3 +310,4 @@ Mixin.fields = proto3.util.newFieldList(() => [
308
310
  { no: 1, name: "name", kind: "scalar", T: 9 /* ScalarType.STRING */ },
309
311
  { no: 2, name: "root", kind: "scalar", T: 9 /* ScalarType.STRING */ },
310
312
  ]);
313
+ export { Mixin };
@@ -19,7 +19,7 @@ import { FileDescriptorProto, GeneratedCodeInfo } from "../descriptor_pb.js";
19
19
  *
20
20
  * @generated from message google.protobuf.compiler.Version
21
21
  */
22
- export class Version extends Message {
22
+ class Version extends Message {
23
23
  constructor(data) {
24
24
  super();
25
25
  proto2.util.initPartial(data, this);
@@ -45,12 +45,13 @@ Version.fields = proto2.util.newFieldList(() => [
45
45
  { no: 3, name: "patch", kind: "scalar", T: 5 /* ScalarType.INT32 */, opt: true },
46
46
  { no: 4, name: "suffix", kind: "scalar", T: 9 /* ScalarType.STRING */, opt: true },
47
47
  ]);
48
+ export { Version };
48
49
  /**
49
50
  * An encoded CodeGeneratorRequest is written to the plugin's stdin.
50
51
  *
51
52
  * @generated from message google.protobuf.compiler.CodeGeneratorRequest
52
53
  */
53
- export class CodeGeneratorRequest extends Message {
54
+ class CodeGeneratorRequest extends Message {
54
55
  constructor(data) {
55
56
  super();
56
57
  /**
@@ -103,12 +104,13 @@ CodeGeneratorRequest.fields = proto2.util.newFieldList(() => [
103
104
  { no: 15, name: "proto_file", kind: "message", T: FileDescriptorProto, repeated: true },
104
105
  { no: 3, name: "compiler_version", kind: "message", T: Version, opt: true },
105
106
  ]);
107
+ export { CodeGeneratorRequest };
106
108
  /**
107
109
  * The plugin writes an encoded CodeGeneratorResponse to stdout.
108
110
  *
109
111
  * @generated from message google.protobuf.compiler.CodeGeneratorResponse
110
112
  */
111
- export class CodeGeneratorResponse extends Message {
113
+ class CodeGeneratorResponse extends Message {
112
114
  constructor(data) {
113
115
  super();
114
116
  /**
@@ -137,6 +139,7 @@ CodeGeneratorResponse.fields = proto2.util.newFieldList(() => [
137
139
  { no: 2, name: "supported_features", kind: "scalar", T: 4 /* ScalarType.UINT64 */, opt: true },
138
140
  { no: 15, name: "file", kind: "message", T: CodeGeneratorResponse_File, repeated: true },
139
141
  ]);
142
+ export { CodeGeneratorResponse };
140
143
  /**
141
144
  * Sync with code_generator.h.
142
145
  *
@@ -163,7 +166,7 @@ proto2.util.setEnumType(CodeGeneratorResponse_Feature, "google.protobuf.compiler
163
166
  *
164
167
  * @generated from message google.protobuf.compiler.CodeGeneratorResponse.File
165
168
  */
166
- export class CodeGeneratorResponse_File extends Message {
169
+ class CodeGeneratorResponse_File extends Message {
167
170
  constructor(data) {
168
171
  super();
169
172
  proto2.util.initPartial(data, this);
@@ -189,3 +192,4 @@ CodeGeneratorResponse_File.fields = proto2.util.newFieldList(() => [
189
192
  { no: 15, name: "content", kind: "scalar", T: 9 /* ScalarType.STRING */, opt: true },
190
193
  { no: 16, name: "generated_code_info", kind: "message", T: GeneratedCodeInfo, opt: true },
191
194
  ]);
195
+ export { CodeGeneratorResponse_File };