protobufjs 8.2.1 → 8.4.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.
package/README.md CHANGED
@@ -70,7 +70,7 @@ protobuf.js converts `.proto` field names to camelCase by default, so `awesome_f
70
70
 
71
71
  ### Load a schema
72
72
 
73
- ```js
73
+ ```ts
74
74
  const protobuf = require("protobufjs");
75
75
 
76
76
  const root = protobuf.loadSync("awesome.proto");
@@ -81,7 +81,7 @@ Use `protobuf.load()` for the asynchronous variant.
81
81
 
82
82
  ### Encode and decode
83
83
 
84
- ```js
84
+ ```ts
85
85
  const payload = { awesomeField: "hello" };
86
86
 
87
87
  // Optionally verify if the payload is of uncertain shape
@@ -103,7 +103,7 @@ Install [`long`](https://github.com/dcodeIO/long.js) with protobuf.js when exact
103
103
 
104
104
  ### Convert plain objects
105
105
 
106
- ```js
106
+ ```ts
107
107
  const message = AwesomeMessage.fromObject({ awesomeField: 42 });
108
108
  const object = AwesomeMessage.toObject(message, {
109
109
  longs: String,
@@ -116,6 +116,7 @@ Common `ConversionOptions` are:
116
116
 
117
117
  | Option | Effect |
118
118
  |--------|--------|
119
+ | `longs: BigInt` | Converts 64-bit values to bigint values |
119
120
  | `longs: String` | Converts 64-bit values to decimal strings |
120
121
  | `longs: Number` | Converts 64-bit values to JS numbers (may lose precision) |
121
122
  | `enums: String` | Converts enum values to names |
@@ -197,7 +198,7 @@ Bundling schemas avoids reparsing `.proto` files at runtime and can reduce brows
197
198
  npx pbjs -t json -o awesome.json awesome1.proto awesome2.proto ...
198
199
  ```
199
200
 
200
- ```js
201
+ ```ts
201
202
  const bundle = require("./awesome.json");
202
203
 
203
204
  const root = protobuf.Root.fromJSON(bundle);
@@ -208,7 +209,7 @@ const AwesomeMessage = root.lookupType("awesomepackage.AwesomeMessage");
208
209
  npx pbjs -t json-module -w esm -o awesome.js --dts awesome.proto
209
210
  ```
210
211
 
211
- ```js
212
+ ```ts
212
213
  import { awesomepackage } from "./awesome.js";
213
214
 
214
215
  const AwesomeMessage = awesomepackage.AwesomeMessage;
@@ -218,7 +219,45 @@ JSON modules export the reflection root and, with `-w esm`, also provide top-lev
218
219
 
219
220
  ### TypeScript integration
220
221
 
221
- TypeScript is a first-class target in protobuf.js. The runtime API is typed, and with `--dts`, both `json-module` and `static-module` emit ready-to-run JavaScript modules together with matching TypeScript declarations. No separate transpile step is necessary.
222
+ protobuf.js works with TypeScript out of the box: the runtime API is typed, and generated declarations expose idiomatic TypeScript types with type-checked oneofs and JavaScript-friendly plain-object input. No separate `protoc` invocation or TypeScript transpile step is necessary.
223
+
224
+ For example, given the oneof:
225
+
226
+ ```proto
227
+ message Profile {
228
+ oneof contact {
229
+ string email = 1;
230
+ string phone = 2;
231
+ }
232
+ }
233
+ ```
234
+
235
+ Generated declarations narrow both the `contact` oneof and the concrete values:
236
+
237
+ ```ts
238
+ const profile = Profile.create({
239
+ contact: "email",
240
+ email: "hello@example.com"
241
+ });
242
+
243
+ if (profile.contact === "email") {
244
+ profile.email; // string
245
+ }
246
+
247
+ const decoded = Profile.decode(bytes);
248
+ if (decoded.phone != null) {
249
+ decoded.phone; // string
250
+ }
251
+ ```
252
+
253
+ Plain objects can use the same narrowed shape through a collision-free scoped type:
254
+
255
+ ```ts
256
+ const object: Profile.$Shape = {
257
+ contact: "email",
258
+ email: "hello@example.com"
259
+ };
260
+ ```
222
261
 
223
262
  ## Advanced usage
224
263
 
@@ -226,7 +265,7 @@ TypeScript is a first-class target in protobuf.js. The runtime API is typed, and
226
265
 
227
266
  The full and light builds can construct schemas directly through reflection:
228
267
 
229
- ```js
268
+ ```ts
230
269
  const AwesomeMessage = new protobuf.Type("AwesomeMessage")
231
270
  .add(new protobuf.Field("awesomeField", 1, "string"));
232
271
 
@@ -237,30 +276,29 @@ const root = new protobuf.Root()
237
276
 
238
277
  ### Custom message classes
239
278
 
240
- Message classes can be extended by reusing the generated constructor:
241
-
242
- ```js
243
- const AwesomeMessage = root.lookupType("awesomepackage.AwesomeMessage").ctor;
279
+ A reflected type can use a custom class as its runtime constructor:
244
280
 
245
- AwesomeMessage.customStaticMethod = function() {
246
- // ...
247
- };
248
- AwesomeMessage.prototype.customInstanceMethod = function() {
249
- // ...
250
- };
251
- ```
281
+ ```ts
282
+ class AwesomeMessage extends protobuf.Message<AwesomeMessage> {
283
+ awesomeField = "";
252
284
 
253
- Alternatively, a custom constructor can be registered:
285
+ constructor(properties?: protobuf.Properties<AwesomeMessage>) {
286
+ super(properties);
287
+ // ...
288
+ }
254
289
 
255
- ```js
256
- function AwesomeMessage(properties) {
257
- // ...
290
+ customInstanceMethod() {
291
+ return this.awesomeField.toLowerCase();
292
+ }
258
293
  }
259
294
 
260
295
  root.lookupType("awesomepackage.AwesomeMessage").ctor = AwesomeMessage;
296
+
297
+ const decoded = AwesomeMessage.decode(bytes);
298
+ decoded.customInstanceMethod(); // string
261
299
  ```
262
300
 
263
- Custom constructors are populated with static `create`, `encode`, `encodeDelimited`, `decode`, `decodeDelimited`, `verify`, `fromObject`, `toObject`, and the instance method `toJSON`. The reflected type is available as `AwesomeMessage.$type` and `message.$type`.
301
+ protobuf.js will populate the constructor with the usual static runtime methods and use it for decoded messages. In TypeScript, custom members are visible when using the custom class type in consuming code.
264
302
 
265
303
  ### Services
266
304
 
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * protobuf.js v8.2.1 (c) 2016, daniel wirtz
3
- * compiled wed, 13 may 2026 10:17:57 utc
2
+ * protobuf.js v8.4.0 (c) 2016, daniel wirtz
3
+ * compiled mon, 18 may 2026 17:55:10 utc
4
4
  * licensed under the bsd-3-clause license
5
5
  * see: https://github.com/dcodeio/protobuf.js for details
6
6
  */
@@ -251,7 +251,9 @@ function genValuePartial_toObject(gen, field, fieldIndex, dstProp, srcProp) {
251
251
  case "sint64":
252
252
  case "fixed64":
253
253
  case "sfixed64": gen
254
- ("if(typeof m%s===\"number\")", srcProp)
254
+ ("if(typeof BigInt!==\"undefined\"&&o.longs===BigInt)")
255
+ ("d%s=typeof m%s===\"number\"?BigInt(m%s):util.Long.fromBits(m%s.low>>>0,m%s.high>>>0,%j).toBigInt()", dstProp, srcProp, srcProp, srcProp, srcProp, isUnsigned)
256
+ ("else if(typeof m%s===\"number\")", srcProp)
255
257
  ("d%s=o.longs===String?String(m%s):m%s", dstProp, srcProp, srcProp)
256
258
  ("else") // Long-like
257
259
  ("d%s=o.longs===String?util.Long.prototype.toString.call(m%s):o.longs===Number?new util.LongBits(m%s.low>>>0,m%s.high>>>0).toNumber(%s):m%s", dstProp, srcProp, srcProp, srcProp, isUnsigned ? "true": "", srcProp);
@@ -319,9 +321,9 @@ converter.toObject = function toObject(mtype) {
319
321
  else if (field.long) gen
320
322
  ("if(util.Long){")
321
323
  ("var n=new util.Long(%i,%i,%j)", field.typeDefault.low, field.typeDefault.high, field.typeDefault.unsigned)
322
- ("d%s=o.longs===String?n.toString():o.longs===Number?n.toNumber():n", prop)
324
+ ("d%s=o.longs===String?n.toString():o.longs===Number?n.toNumber():typeof BigInt!==\"undefined\"&&o.longs===BigInt?n.toBigInt():n", prop)
323
325
  ("}else")
324
- ("d%s=o.longs===String?%j:%i", prop, field.typeDefault.toString(), field.typeDefault.toNumber());
326
+ ("d%s=o.longs===String?%j:typeof BigInt!==\"undefined\"&&o.longs===BigInt?BigInt(%j):%i", prop, field.typeDefault.toString(), field.typeDefault.toString(), field.typeDefault.toNumber());
325
327
  else if (field.bytes) {
326
328
  var arrayDefault = Array.prototype.slice.call(field.typeDefault);
327
329
  gen
@@ -714,7 +716,7 @@ var Namespace = require(12),
714
716
  * @param {Object.<string,number>} [values] Enum values as an object, by name
715
717
  * @param {Object.<string,*>} [options] Declared options
716
718
  * @param {string} [comment] The comment for this enum
717
- * @param {Object.<string,string>} [comments] The value comments for this enum
719
+ * @param {Object.<string,string|null>} [comments] The value comments for this enum
718
720
  * @param {Object.<string,Object<string,*>>|undefined} [valuesOptions] The value options for this enum
719
721
  */
720
722
  function Enum(name, values, options, comment, comments, valuesOptions) {
@@ -743,7 +745,7 @@ function Enum(name, values, options, comment, comments, valuesOptions) {
743
745
 
744
746
  /**
745
747
  * Value comment texts, if any.
746
- * @type {Object.<string,string>}
748
+ * @type {Object.<string,string|null>}
747
749
  */
748
750
  this.comments = comments || {};
749
751
 
@@ -793,8 +795,13 @@ Enum.prototype._resolveFeatures = function _resolveFeatures(edition) {
793
795
  /**
794
796
  * Enum descriptor.
795
797
  * @interface IEnum
798
+ * @property {string} [edition] Edition
796
799
  * @property {Object.<string,number>} values Enum values
797
800
  * @property {Object.<string,*>} [options] Enum options
801
+ * @property {Object.<string,Object.<string,*>>} [valuesOptions] Enum value options
802
+ * @property {Array.<number[]|string>} [reserved] Reserved ranges
803
+ * @property {string|null} [comment] Enum comment
804
+ * @property {Object.<string,string|null>} [comments] Value comments
798
805
  */
799
806
 
800
807
  /**
@@ -805,7 +812,7 @@ Enum.prototype._resolveFeatures = function _resolveFeatures(edition) {
805
812
  * @throws {TypeError} If arguments are invalid
806
813
  */
807
814
  Enum.fromJSON = function fromJSON(name, json) {
808
- var enm = new Enum(name, json.values, json.options, json.comment, json.comments);
815
+ var enm = new Enum(name, json.values, json.options, json.comment, json.comments, json.valuesOptions);
809
816
  enm.reserved = json.reserved;
810
817
  if (json.edition)
811
818
  enm._edition = json.edition;
@@ -1184,10 +1191,12 @@ Field.prototype.setOption = function setOption(name, value, ifNotSet) {
1184
1191
  /**
1185
1192
  * Field descriptor.
1186
1193
  * @interface IField
1194
+ * @property {string} [edition] Edition
1187
1195
  * @property {string} [rule="optional"] Field rule
1188
1196
  * @property {string} type Field type
1189
1197
  * @property {number} id Field id
1190
1198
  * @property {Object.<string,*>} [options] Field options
1199
+ * @property {string|null} [comment] Field comment
1191
1200
  */
1192
1201
 
1193
1202
  /**
@@ -1670,12 +1679,9 @@ var util = require(34);
1670
1679
  function Message(properties) {
1671
1680
  // not used internally
1672
1681
  if (properties)
1673
- for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i) {
1674
- var key = keys[i];
1675
- if (key === "__proto__")
1676
- continue;
1677
- this[key] = properties[key];
1678
- }
1682
+ for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i)
1683
+ if (properties[keys[i]] != null && keys[i] !== "__proto__")
1684
+ this[keys[i]] = properties[keys[i]];
1679
1685
  }
1680
1686
 
1681
1687
  /**
@@ -1909,7 +1915,7 @@ function Method(name, type, requestType, responseType, requestStream, responseSt
1909
1915
  * @property {boolean} [requestStream=false] Whether requests are streamed
1910
1916
  * @property {boolean} [responseStream=false] Whether responses are streamed
1911
1917
  * @property {Object.<string,*>} [options] Method options
1912
- * @property {string} comment Method comments
1918
+ * @property {string|null} [comment] Method comment
1913
1919
  * @property {Array.<Object.<string,*>>} [parsedOptions] Method options properly parsed into objects
1914
1920
  */
1915
1921
 
@@ -2670,7 +2676,6 @@ Object.defineProperties(ReflectionObject.prototype, {
2670
2676
  /**
2671
2677
  * Converts this reflection object to its descriptor representation.
2672
2678
  * @returns {Object.<string,*>} Descriptor
2673
- * @abstract
2674
2679
  */
2675
2680
  ReflectionObject.prototype.toJSON = /* istanbul ignore next */ function toJSON() {
2676
2681
  throw Error(); // not implemented, shouldn't happen
@@ -2967,6 +2972,7 @@ function OneOf(name, fieldNames, options, comment) {
2967
2972
  * @interface IOneOf
2968
2973
  * @property {Array.<string>} oneof Oneof field names
2969
2974
  * @property {Object.<string,*>} [options] Oneof options
2975
+ * @property {string|null} [comment] Oneof comment
2970
2976
  */
2971
2977
 
2972
2978
  /**
@@ -4401,7 +4407,9 @@ function Service(name, options) {
4401
4407
  * Service descriptor.
4402
4408
  * @interface IService
4403
4409
  * @extends INamespace
4410
+ * @property {string} [edition] Edition
4404
4411
  * @property {Object.<string,IMethod>} methods Method descriptors
4412
+ * @property {string|null} [comment] Service comment
4405
4413
  */
4406
4414
 
4407
4415
  /**
@@ -4786,11 +4794,13 @@ function clearCache(type) {
4786
4794
  * Message type descriptor.
4787
4795
  * @interface IType
4788
4796
  * @extends INamespace
4797
+ * @property {string} [edition] Edition
4789
4798
  * @property {Object.<string,IOneOf>} [oneofs] Oneof descriptors
4790
4799
  * @property {Object.<string,IField>} fields Field descriptors
4791
4800
  * @property {number[][]} [extensions] Extension ranges
4792
4801
  * @property {Array.<number[]|string>} [reserved] Reserved ranges
4793
4802
  * @property {boolean} [group=false] Whether a legacy group or not
4803
+ * @property {string|null} [comment] Message type comment
4794
4804
  */
4795
4805
 
4796
4806
  /**
@@ -5143,7 +5153,7 @@ Type.prototype.fromObject = function fromObject(object) { // eslint-disable-line
5143
5153
  * Conversion options as used by {@link Type#toObject} and {@link Message.toObject}.
5144
5154
  * @interface IConversionOptions
5145
5155
  * @property {Function} [longs] Long conversion type.
5146
- * Valid values are `String` and `Number` (the global types).
5156
+ * Valid values are `BigInt`, `String` and `Number` (the global types).
5147
5157
  * Defaults to copy the present value, which is a possibly unsafe number without and a {@link Long} with a long library.
5148
5158
  * @property {Function} [enums] Enum value conversion type.
5149
5159
  * Only valid value is `String` (the global type).
@@ -7337,7 +7347,7 @@ module.exports = pool;
7337
7347
  * @param {number} start Start offset
7338
7348
  * @param {number} end End offset
7339
7349
  * @returns {Uint8Array} Buffer slice
7340
- * @this {Uint8Array}
7350
+ * @this Uint8Array
7341
7351
  */
7342
7352
 
7343
7353
  /**