protobufjs 8.1.6-experimental → 8.2.0

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 (76) hide show
  1. package/README.md +219 -565
  2. package/dist/light/protobuf.js +1986 -1483
  3. package/dist/light/protobuf.js.map +1 -1
  4. package/dist/light/protobuf.min.js +3 -3
  5. package/dist/light/protobuf.min.js.map +1 -1
  6. package/dist/minimal/protobuf.js +1122 -861
  7. package/dist/minimal/protobuf.js.map +1 -1
  8. package/dist/minimal/protobuf.min.js +3 -3
  9. package/dist/minimal/protobuf.min.js.map +1 -1
  10. package/dist/protobuf.js +2089 -1513
  11. package/dist/protobuf.js.map +1 -1
  12. package/dist/protobuf.min.js +3 -3
  13. package/dist/protobuf.min.js.map +1 -1
  14. package/ext/README.md +81 -0
  15. package/ext/descriptor/README.md +3 -70
  16. package/ext/descriptor/index.d.ts +1 -191
  17. package/ext/descriptor/index.js +1 -1161
  18. package/ext/descriptor.d.ts +309 -0
  19. package/ext/descriptor.js +1236 -0
  20. package/ext/textformat.d.ts +30 -0
  21. package/ext/textformat.js +1249 -0
  22. package/google/protobuf/compiler/plugin.json +126 -0
  23. package/google/protobuf/compiler/plugin.proto +47 -0
  24. package/google/protobuf/descriptor.json +2 -2
  25. package/google/protobuf/descriptor.proto +2 -1
  26. package/index.d.ts +590 -476
  27. package/package.json +23 -38
  28. package/src/converter.js +60 -24
  29. package/src/decoder.js +122 -49
  30. package/src/encoder.js +10 -2
  31. package/src/enum.js +4 -1
  32. package/src/field.js +10 -7
  33. package/src/mapfield.js +1 -0
  34. package/src/message.js +7 -6
  35. package/src/method.js +4 -3
  36. package/src/namespace.js +23 -12
  37. package/src/object.js +24 -19
  38. package/src/oneof.js +2 -0
  39. package/src/parse.js +114 -46
  40. package/src/reader.js +145 -30
  41. package/src/reader_buffer.js +24 -3
  42. package/src/root.js +7 -4
  43. package/src/service.js +12 -6
  44. package/src/tokenize.js +6 -1
  45. package/src/type.js +48 -25
  46. package/src/types.js +1 -1
  47. package/src/util/aspromise.d.ts +13 -0
  48. package/src/util/aspromise.js +52 -0
  49. package/src/util/base64.d.ts +32 -0
  50. package/src/util/base64.js +146 -0
  51. package/src/util/codegen.d.ts +31 -0
  52. package/src/util/codegen.js +113 -0
  53. package/src/util/eventemitter.d.ts +45 -0
  54. package/src/util/eventemitter.js +84 -0
  55. package/src/util/fetch.d.ts +56 -0
  56. package/src/util/fetch.js +112 -0
  57. package/src/util/float.d.ts +83 -0
  58. package/src/util/float.js +335 -0
  59. package/src/util/fs.js +11 -0
  60. package/src/util/inquire.d.ts +10 -0
  61. package/src/util/inquire.js +38 -0
  62. package/src/util/minimal.js +67 -12
  63. package/src/util/path.d.ts +22 -0
  64. package/src/util/path.js +72 -0
  65. package/src/util/patterns.js +8 -0
  66. package/src/util/pool.d.ts +32 -0
  67. package/src/util/pool.js +48 -0
  68. package/src/util/utf8.d.ts +24 -0
  69. package/src/util/utf8.js +104 -0
  70. package/src/util.js +30 -13
  71. package/src/verifier.js +7 -4
  72. package/src/wrappers.js +4 -3
  73. package/src/writer.js +27 -4
  74. package/src/writer_buffer.js +12 -0
  75. package/tsconfig.json +2 -2
  76. package/ext/descriptor/test.js +0 -54
package/src/mapfield.js CHANGED
@@ -108,6 +108,7 @@ MapField.prototype.resolve = function resolve() {
108
108
  * @param {"double"|"float"|"int32"|"uint32"|"sint32"|"fixed32"|"sfixed32"|"int64"|"uint64"|"sint64"|"fixed64"|"sfixed64"|"bool"|"string"|"bytes"|Object|Constructor<{}>} fieldValueType Field value type
109
109
  * @returns {FieldDecorator} Decorator function
110
110
  * @template T extends { [key: string]: number | Long | string | boolean | Uint8Array | Buffer | number[] | Message<{}> }
111
+ * @deprecated Legacy TypeScript decorator support. Will be removed in a future release.
111
112
  */
112
113
  MapField.d = function decorateMapField(fieldId, fieldKeyType, fieldValueType) {
113
114
 
package/src/message.js CHANGED
@@ -8,13 +8,18 @@ var util = require("./util/minimal");
8
8
  * @classdesc Abstract runtime message.
9
9
  * @constructor
10
10
  * @param {Properties<T>} [properties] Properties to set
11
+ * @property {Array.<Uint8Array>} [$unknowns] Unknown fields preserved while decoding
11
12
  * @template T extends object = object
12
13
  */
13
14
  function Message(properties) {
14
15
  // not used internally
15
16
  if (properties)
16
- for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i)
17
- this[keys[i]] = properties[keys[i]];
17
+ for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i) {
18
+ var key = keys[i];
19
+ if (key === "__proto__")
20
+ continue;
21
+ this[key] = properties[key];
22
+ }
18
23
  }
19
24
 
20
25
  /**
@@ -31,8 +36,6 @@ function Message(properties) {
31
36
  * @readonly
32
37
  */
33
38
 
34
- /*eslint-disable valid-jsdoc*/
35
-
36
39
  /**
37
40
  * Creates a new message of this type using the specified properties.
38
41
  * @param {Object.<string,*>} [properties] Properties to set
@@ -135,5 +138,3 @@ Message.toObject = function toObject(message, options) {
135
138
  Message.prototype.toJSON = function toJSON() {
136
139
  return this.$type.toObject(this, util.toJSONOptions);
137
140
  };
138
-
139
- /*eslint-enable valid-jsdoc*/
package/src/method.js CHANGED
@@ -20,7 +20,7 @@ var util = require("./util");
20
20
  * @param {boolean|Object.<string,*>} [responseStream] Whether the response is streamed
21
21
  * @param {Object.<string,*>} [options] Declared options
22
22
  * @param {string} [comment] The comment for this method
23
- * @param {Object.<string,*>} [parsedOptions] Declared options, properly parsed into an object
23
+ * @param {Array.<Object.<string,*>>} [parsedOptions] Declared options, properly parsed into objects
24
24
  */
25
25
  function Method(name, type, requestType, responseType, requestStream, responseStream, options, comment, parsedOptions) {
26
26
 
@@ -96,7 +96,8 @@ function Method(name, type, requestType, responseType, requestStream, responseSt
96
96
  this.comment = comment;
97
97
 
98
98
  /**
99
- * Options properly parsed into an object
99
+ * Options properly parsed into objects
100
+ * @type {Array.<Object.<string,*>>|undefined}
100
101
  */
101
102
  this.parsedOptions = parsedOptions;
102
103
  }
@@ -111,7 +112,7 @@ function Method(name, type, requestType, responseType, requestStream, responseSt
111
112
  * @property {boolean} [responseStream=false] Whether responses are streamed
112
113
  * @property {Object.<string,*>} [options] Method options
113
114
  * @property {string} comment Method comments
114
- * @property {Object.<string,*>} [parsedOptions] Method options properly parsed into an object
115
+ * @property {Array.<Object.<string,*>>} [parsedOptions] Method options properly parsed into objects
115
116
  */
116
117
 
117
118
  /**
package/src/namespace.js CHANGED
@@ -29,11 +29,13 @@ var Type, // cyclic
29
29
  * @function
30
30
  * @param {string} name Namespace name
31
31
  * @param {Object.<string,*>} json JSON object
32
+ * @param {number} [depth] Current nesting depth, defaults to `0`
32
33
  * @returns {Namespace} Created namespace
33
34
  * @throws {TypeError} If arguments are invalid
34
35
  */
35
- Namespace.fromJSON = function fromJSON(name, json) {
36
- return new Namespace(name, json.options).addJSON(json.nested);
36
+ Namespace.fromJSON = function fromJSON(name, json, depth) {
37
+ depth = util.checkDepth(depth);
38
+ return new Namespace(name, json.options).addJSON(json.nested, depth);
37
39
  };
38
40
 
39
41
  /**
@@ -116,7 +118,7 @@ function Namespace(name, options) {
116
118
  * @type {Object.<string,ReflectionObject|null>}
117
119
  * @private
118
120
  */
119
- this._lookupCache = {};
121
+ this._lookupCache = Object.create(null);
120
122
 
121
123
  /**
122
124
  * Whether or not objects contained in this namespace need feature resolution.
@@ -135,12 +137,12 @@ function Namespace(name, options) {
135
137
 
136
138
  function clearCache(namespace) {
137
139
  namespace._nestedArray = null;
138
- namespace._lookupCache = {};
140
+ namespace._lookupCache = Object.create(null);
139
141
 
140
142
  // Also clear parent caches, since they include nested lookups.
141
143
  var parent = namespace;
142
144
  while(parent = parent.parent) {
143
- parent._lookupCache = {};
145
+ parent._lookupCache = Object.create(null);
144
146
  }
145
147
  return namespace;
146
148
  }
@@ -191,9 +193,11 @@ Namespace.prototype.toJSON = function toJSON(toJSONOptions) {
191
193
  /**
192
194
  * Adds nested objects to this namespace from nested object descriptors.
193
195
  * @param {Object.<string,AnyNestedObject>} nestedJson Any nested object descriptors
196
+ * @param {number} [depth] Current nesting depth, defaults to `0`
194
197
  * @returns {Namespace} `this`
195
198
  */
196
- Namespace.prototype.addJSON = function addJSON(nestedJson) {
199
+ Namespace.prototype.addJSON = function addJSON(nestedJson, depth) {
200
+ depth = util.checkDepth(depth);
197
201
  var ns = this;
198
202
  /* istanbul ignore else */
199
203
  if (nestedJson) {
@@ -208,7 +212,7 @@ Namespace.prototype.addJSON = function addJSON(nestedJson) {
208
212
  ? Service.fromJSON
209
213
  : nested.id !== undefined
210
214
  ? Field.fromJSON
211
- : Namespace.fromJSON )(names[i], nested)
215
+ : Namespace.fromJSON )(names[i], nested, depth + 1)
212
216
  );
213
217
  }
214
218
  }
@@ -221,8 +225,9 @@ Namespace.prototype.addJSON = function addJSON(nestedJson) {
221
225
  * @returns {ReflectionObject|null} The reflection object or `null` if it doesn't exist
222
226
  */
223
227
  Namespace.prototype.get = function get(name) {
224
- return this.nested && this.nested[name]
225
- || null;
228
+ return this.nested && Object.prototype.hasOwnProperty.call(this.nested, name)
229
+ ? this.nested[name]
230
+ : null;
226
231
  };
227
232
 
228
233
  /**
@@ -233,7 +238,7 @@ Namespace.prototype.get = function get(name) {
233
238
  * @throws {Error} If there is no such enum
234
239
  */
235
240
  Namespace.prototype.getEnum = function getEnum(name) {
236
- if (this.nested && this.nested[name] instanceof Enum)
241
+ if (this.nested && Object.prototype.hasOwnProperty.call(this.nested, name) && this.nested[name] instanceof Enum)
237
242
  return this.nested[name].values;
238
243
  throw Error("no such enum: " + name);
239
244
  };
@@ -250,6 +255,9 @@ Namespace.prototype.add = function add(object) {
250
255
  if (!(object instanceof Field && object.extend !== undefined || object instanceof Type || object instanceof OneOf || object instanceof Enum || object instanceof Service || object instanceof Namespace))
251
256
  throw TypeError("object must be a valid nested object");
252
257
 
258
+ if (object.name === "__proto__")
259
+ return this;
260
+
253
261
  if (!this.nested)
254
262
  this.nested = {};
255
263
  else {
@@ -352,7 +360,8 @@ Namespace.prototype.define = function define(path, json) {
352
360
  Namespace.prototype.resolveAll = function resolveAll() {
353
361
  if (!this._needsRecursiveResolve) return this;
354
362
 
355
- this._resolveFeaturesRecursive(this._edition);
363
+ if (this._needsRecursiveFeatureResolution)
364
+ this._resolveFeaturesRecursive(this._edition);
356
365
 
357
366
  var nested = this.nestedArray, i = 0;
358
367
  this.resolve();
@@ -462,8 +471,10 @@ Namespace.prototype._lookupImpl = function lookup(path, flatPath) {
462
471
  // Otherwise try each nested namespace
463
472
  } else {
464
473
  for (var i = 0; i < this.nestedArray.length; ++i)
465
- if (this._nestedArray[i] instanceof Namespace && (found = this._nestedArray[i]._lookupImpl(path, flatPath)))
474
+ if (this._nestedArray[i] instanceof Namespace && (found = this._nestedArray[i]._lookupImpl(path, flatPath))) {
466
475
  exact = found;
476
+ break;
477
+ }
467
478
  }
468
479
 
469
480
  // Set this even when null, so that when we walk up the tree we can quickly bail on repeated checks back down.
package/src/object.js CHANGED
@@ -10,9 +10,10 @@ var Root; // cyclic
10
10
 
11
11
  /* eslint-disable no-warning-comments */
12
12
  // TODO: Replace with embedded proto.
13
- var editions2023Defaults = {enum_type: "OPEN", field_presence: "EXPLICIT", json_format: "ALLOW", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "PACKED", utf8_validation: "VERIFY"};
14
- var proto2Defaults = {enum_type: "CLOSED", field_presence: "EXPLICIT", json_format: "LEGACY_BEST_EFFORT", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "EXPANDED", utf8_validation: "NONE"};
15
- var proto3Defaults = {enum_type: "OPEN", field_presence: "IMPLICIT", json_format: "ALLOW", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "PACKED", utf8_validation: "VERIFY"};
13
+ var editions2024Defaults = {enum_type: "OPEN", field_presence: "EXPLICIT", json_format: "ALLOW", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "PACKED", utf8_validation: "VERIFY", enforce_naming_style: "STYLE2024", default_symbol_visibility: "EXPORT_TOP_LEVEL" };
14
+ var editions2023Defaults = {enum_type: "OPEN", field_presence: "EXPLICIT", json_format: "ALLOW", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "PACKED", utf8_validation: "VERIFY", enforce_naming_style: "STYLE_LEGACY", default_symbol_visibility: "EXPORT_ALL" };
15
+ var proto2Defaults = {enum_type: "CLOSED", field_presence: "EXPLICIT", json_format: "LEGACY_BEST_EFFORT", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "EXPANDED", utf8_validation: "NONE", enforce_naming_style: "STYLE_LEGACY", default_symbol_visibility: "EXPORT_ALL" };
16
+ var proto3Defaults = {enum_type: "OPEN", field_presence: "IMPLICIT", json_format: "ALLOW", message_encoding: "LENGTH_PREFIXED", repeated_field_encoding: "PACKED", utf8_validation: "VERIFY", enforce_naming_style: "STYLE_LEGACY", default_symbol_visibility: "EXPORT_ALL" };
16
17
 
17
18
  /**
18
19
  * Constructs a new reflection object instance.
@@ -225,27 +226,27 @@ ReflectionObject.prototype._resolveFeatures = function _resolveFeatures(edition)
225
226
  defaults = Object.assign({}, proto3Defaults);
226
227
  } else if (edition === "2023") {
227
228
  defaults = Object.assign({}, editions2023Defaults);
229
+ } else if (edition === "2024") {
230
+ defaults = Object.assign({}, editions2024Defaults);
228
231
  } else {
229
232
  throw new Error("Unknown edition: " + edition);
230
233
  }
231
234
  this._features = Object.assign(defaults, protoFeatures || {});
232
- this._featuresResolved = true;
233
- return;
234
- }
235
-
236
- // fields in Oneofs aren't actually children of them, so we have to
237
- // special-case it
238
- /* istanbul ignore else */
239
- if (this.partOf instanceof OneOf) {
240
- var lexicalParentFeaturesCopy = Object.assign({}, this.partOf._features);
241
- this._features = Object.assign(lexicalParentFeaturesCopy, protoFeatures || {});
242
- } else if (this.declaringField) {
243
- // Skip feature resolution of sister fields.
244
- } else if (this.parent) {
245
- var parentFeaturesCopy = Object.assign({}, this.parent._features);
246
- this._features = Object.assign(parentFeaturesCopy, protoFeatures || {});
247
235
  } else {
248
- throw new Error("Unable to find a parent for " + this.fullName);
236
+ // fields in Oneofs aren't actually children of them, so we have to
237
+ // special-case it
238
+ /* istanbul ignore else */
239
+ if (this.partOf instanceof OneOf) {
240
+ var lexicalParentFeaturesCopy = Object.assign({}, this.partOf._features);
241
+ this._features = Object.assign(lexicalParentFeaturesCopy, protoFeatures || {});
242
+ } else if (this.declaringField) {
243
+ // Skip feature resolution of sister fields.
244
+ } else if (this.parent) {
245
+ var parentFeaturesCopy = Object.assign({}, this.parent._features);
246
+ this._features = Object.assign(parentFeaturesCopy, protoFeatures || {});
247
+ } else {
248
+ throw new Error("Unable to find a parent for " + this.fullName);
249
+ }
249
250
  }
250
251
  if (this.extensionField) {
251
252
  // Sister fields should have the same features as their extensions.
@@ -283,6 +284,8 @@ ReflectionObject.prototype.getOption = function getOption(name) {
283
284
  * @returns {ReflectionObject} `this`
284
285
  */
285
286
  ReflectionObject.prototype.setOption = function setOption(name, value, ifNotSet) {
287
+ if (name === "__proto__")
288
+ return this;
286
289
  if (!this.options)
287
290
  this.options = {};
288
291
  if (/^features\./.test(name)) {
@@ -303,6 +306,8 @@ ReflectionObject.prototype.setOption = function setOption(name, value, ifNotSet)
303
306
  * @returns {ReflectionObject} `this`
304
307
  */
305
308
  ReflectionObject.prototype.setParsedOption = function setParsedOption(name, value, propName) {
309
+ if (name === "__proto__")
310
+ return this;
306
311
  if (!this.parsedOptions) {
307
312
  this.parsedOptions = [];
308
313
  }
package/src/oneof.js CHANGED
@@ -197,6 +197,7 @@ Object.defineProperty(OneOf.prototype, "isProto3Optional", {
197
197
  * @param {Object} prototype Target prototype
198
198
  * @param {string} oneofName OneOf name
199
199
  * @returns {undefined}
200
+ * @deprecated Legacy TypeScript decorator support. Will be removed in a future release.
200
201
  */
201
202
 
202
203
  /**
@@ -205,6 +206,7 @@ Object.defineProperty(OneOf.prototype, "isProto3Optional", {
205
206
  * @param {...string} fieldNames Field names
206
207
  * @returns {OneOfDecorator} Decorator function
207
208
  * @template T extends string
209
+ * @deprecated Legacy TypeScript decorator support. Will be removed in a future release.
208
210
  */
209
211
  OneOf.d = function decorateOneOf() {
210
212
  var fieldNames = new Array(arguments.length),