protobufjs 8.2.0 → 8.3.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,
@@ -197,7 +197,7 @@ Bundling schemas avoids reparsing `.proto` files at runtime and can reduce brows
197
197
  npx pbjs -t json -o awesome.json awesome1.proto awesome2.proto ...
198
198
  ```
199
199
 
200
- ```js
200
+ ```ts
201
201
  const bundle = require("./awesome.json");
202
202
 
203
203
  const root = protobuf.Root.fromJSON(bundle);
@@ -208,7 +208,7 @@ const AwesomeMessage = root.lookupType("awesomepackage.AwesomeMessage");
208
208
  npx pbjs -t json-module -w esm -o awesome.js --dts awesome.proto
209
209
  ```
210
210
 
211
- ```js
211
+ ```ts
212
212
  import { awesomepackage } from "./awesome.js";
213
213
 
214
214
  const AwesomeMessage = awesomepackage.AwesomeMessage;
@@ -218,7 +218,45 @@ JSON modules export the reflection root and, with `-w esm`, also provide top-lev
218
218
 
219
219
  ### TypeScript integration
220
220
 
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.
221
+ 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.
222
+
223
+ For example, given the oneof:
224
+
225
+ ```proto
226
+ message Profile {
227
+ oneof contact {
228
+ string email = 1;
229
+ string phone = 2;
230
+ }
231
+ }
232
+ ```
233
+
234
+ Generated declarations narrow both the `contact` oneof and the concrete values:
235
+
236
+ ```ts
237
+ const profile = Profile.create({
238
+ contact: "email",
239
+ email: "hello@example.com"
240
+ });
241
+
242
+ if (profile.contact === "email") {
243
+ profile.email; // string
244
+ }
245
+
246
+ const decoded = Profile.decode(bytes);
247
+ if (decoded.phone != null) {
248
+ decoded.phone; // string
249
+ }
250
+ ```
251
+
252
+ Plain objects can use the same narrowed shape through a collision-free scoped type:
253
+
254
+ ```ts
255
+ const object: Profile.$Shape = {
256
+ contact: "email",
257
+ email: "hello@example.com"
258
+ };
259
+ ```
222
260
 
223
261
  ## Advanced usage
224
262
 
@@ -226,7 +264,7 @@ TypeScript is a first-class target in protobuf.js. The runtime API is typed, and
226
264
 
227
265
  The full and light builds can construct schemas directly through reflection:
228
266
 
229
- ```js
267
+ ```ts
230
268
  const AwesomeMessage = new protobuf.Type("AwesomeMessage")
231
269
  .add(new protobuf.Field("awesomeField", 1, "string"));
232
270
 
@@ -237,30 +275,29 @@ const root = new protobuf.Root()
237
275
 
238
276
  ### Custom message classes
239
277
 
240
- Message classes can be extended by reusing the generated constructor:
241
-
242
- ```js
243
- const AwesomeMessage = root.lookupType("awesomepackage.AwesomeMessage").ctor;
278
+ A reflected type can use a custom class as its runtime constructor:
244
279
 
245
- AwesomeMessage.customStaticMethod = function() {
246
- // ...
247
- };
248
- AwesomeMessage.prototype.customInstanceMethod = function() {
249
- // ...
250
- };
251
- ```
280
+ ```ts
281
+ class AwesomeMessage extends protobuf.Message<AwesomeMessage> {
282
+ awesomeField = "";
252
283
 
253
- Alternatively, a custom constructor can be registered:
284
+ constructor(properties?: protobuf.Properties<AwesomeMessage>) {
285
+ super(properties);
286
+ // ...
287
+ }
254
288
 
255
- ```js
256
- function AwesomeMessage(properties) {
257
- // ...
289
+ customInstanceMethod() {
290
+ return this.awesomeField.toLowerCase();
291
+ }
258
292
  }
259
293
 
260
294
  root.lookupType("awesomepackage.AwesomeMessage").ctor = AwesomeMessage;
295
+
296
+ const decoded = AwesomeMessage.decode(bytes);
297
+ decoded.customInstanceMethod(); // string
261
298
  ```
262
299
 
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`.
300
+ 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
301
 
265
302
  ### Services
266
303
 
@@ -278,14 +315,6 @@ Protocol Buffers [Text Format](https://protobuf.dev/reference/protobuf/textforma
278
315
 
279
316
  In [CSP](https://w3c.github.io/webappsec-csp/)-restricted environments that disallow unsafe-eval, use generated static code instead of runtime code generation.
280
317
 
281
- ## Compatibility
282
-
283
- Supported runtimes are browsers, Node.js v12+, Deno (`deno add npm:protobufjs`) and Bun (`bun add protobufjs`). When using the CLI with Bun, Node.js must also be installed.
284
-
285
- ## Security
286
-
287
- protobuf.js favors transparent disclosure. Security-impacting reports are handled through coordinated GitHub Security Advisories where appropriate. See [SECURITY.md](./SECURITY.md) for supported release lines, reporting instructions, and notes on untrusted schema input.
288
-
289
318
  ## Conformance
290
319
 
291
320
  protobuf.js targets full binary wire-format conformance for **Proto2**, **Proto3** and **Editions**. CI runs the official Protocol Buffers conformance suite, with logs uploaded as artifacts.
@@ -298,62 +327,71 @@ protobuf.js targets full binary wire-format conformance for **Proto2**, **Proto3
298
327
 
299
328
  ## Performance
300
329
 
301
- In both reflection and static modes, protobuf.js builds specialized encoders and decoders for each message type instead of interpreting descriptors at runtime.
330
+ In both reflection and static modes, protobuf.js builds specialized encoders and decoders instead of interpreting descriptors at runtime.
302
331
 
303
- The repository includes a small benchmark for the bundled fixture in [`bench/`](./bench/). It compares protobuf.js reflection and static code against native `JSON.stringify`/`JSON.parse` and [google-protobuf](https://www.npmjs.com/package/google-protobuf) (`protoc-gen-js`). Results depend on hardware, Node.js version, and the message shape, so they should be treated as indicative rather than absolute.
332
+ The repository includes a [small benchmark](./bench). It compares protobuf.js reflection and static code against JSON encode/decode, protoc-gen-js, and protoc-gen-es. Results depend on hardware, Node.js version, and message shape, so they should be treated as indicative rather than absolute.
304
333
 
305
- One run on an AMD Ryzen 9 9950X3D with Node.js 24.13.0 and google-protobuf 4.0.2 produced:
334
+ One run on an AMD Ryzen 9 9950X3D with Node.js 24.15.0 produced:
306
335
 
307
336
  ```
308
- benchmarking encoding performance ...
309
-
310
- protobuf.js (reflect) x 2,492,315 ops/sec ±0.56% (94 runs sampled)
311
- protobuf.js (static) x 2,316,421 ops/sec ±0.49% (96 runs sampled)
312
- JSON (string) x 2,581,565 ops/sec ±0.22% (99 runs sampled)
313
- JSON (buffer) x 2,086,216 ops/sec ±0.68% (92 runs sampled)
314
- google-protobuf x 1,052,145 ops/sec ±0.24% (101 runs sampled)
315
-
316
- JSON (string) was fastest
317
- protobuf.js (reflect) was 3.8% ops/sec slower (factor 1.0)
318
- protobuf.js (static) was 10.5% ops/sec slower (factor 1.1)
319
- JSON (buffer) was 19.6% ops/sec slower (factor 1.2)
320
- google-protobuf was 59.3% ops/sec slower (factor 2.5)
321
-
322
- benchmarking decoding performance ...
323
-
324
- protobuf.js (reflect) x 6,053,370 ops/sec ±0.25% (98 runs sampled)
325
- protobuf.js (static) x 6,081,190 ops/sec ±0.35% (97 runs sampled)
326
- JSON (string) x 1,579,677 ops/sec ±0.11% (100 runs sampled)
327
- JSON (buffer) x 1,383,484 ops/sec ±0.15% (98 runs sampled)
328
- google-protobuf x 931,575 ops/sec ±0.25% (97 runs sampled)
329
-
330
- protobuf.js (static) was fastest
331
- protobuf.js (reflect) was 0.4% ops/sec slower (factor 1.0)
332
- JSON (string) was 74.0% ops/sec slower (factor 3.8)
333
- JSON (buffer) was 77.2% ops/sec slower (factor 4.4)
334
- google-protobuf was 84.7% ops/sec slower (factor 6.5)
335
-
336
- benchmarking combined performance ...
337
-
338
- protobuf.js (reflect) x 1,259,417 ops/sec ±0.33% (101 runs sampled)
339
- protobuf.js (static) x 1,296,628 ops/sec ±0.20% (98 runs sampled)
340
- JSON (string) x 848,422 ops/sec ±0.10% (100 runs sampled)
341
- JSON (buffer) x 727,866 ops/sec ±0.30% (100 runs sampled)
342
- google-protobuf x 477,041 ops/sec ±0.22% (101 runs sampled)
343
-
344
- protobuf.js (static) was fastest
345
- protobuf.js (reflect) was 3.0% ops/sec slower (factor 1.0)
346
- JSON (string) was 34.5% ops/sec slower (factor 1.5)
347
- JSON (buffer) was 43.9% ops/sec slower (factor 1.8)
348
- google-protobuf was 63.2% ops/sec slower (factor 2.7)
337
+ benchmarking encode performance ...
338
+
339
+ protobuf.js reflect x 2,430,103 ops/sec ±0.62% (95 runs sampled)
340
+ protobuf.js static x 2,390,407 ops/sec ±0.42% (96 runs sampled)
341
+ JSON encode x 2,155,918 ops/sec ±0.63% (92 runs sampled)
342
+ protoc-gen-js x 995,429 ops/sec ±0.18% (98 runs sampled)
343
+ protoc-gen-es x 403,334 ops/sec ±0.14% (96 runs sampled)
344
+
345
+ protobuf.js reflect was fastest
346
+ protobuf.js static was 1.4% ops/sec slower (factor 1.0)
347
+ JSON encode was 11.3% ops/sec slower (factor 1.1)
348
+ protoc-gen-js was 58.9% ops/sec slower (factor 2.4)
349
+ protoc-gen-es was 83.3% ops/sec slower (factor 6.0)
350
+
351
+ benchmarking decode performance ...
352
+
353
+ protobuf.js reflect x 6,440,387 ops/sec ±0.25% (97 runs sampled)
354
+ protobuf.js static x 6,463,283 ops/sec ±0.27% (101 runs sampled)
355
+ JSON decode x 1,409,923 ops/sec ±0.11% (97 runs sampled)
356
+ protoc-gen-js x 947,647 ops/sec ±0.15% (99 runs sampled)
357
+ protoc-gen-es x 731,819 ops/sec ±0.28% (98 runs sampled)
358
+
359
+ protobuf.js static was fastest
360
+ protobuf.js reflect was 0.3% ops/sec slower (factor 1.0)
361
+ JSON decode was 78.2% ops/sec slower (factor 4.6)
362
+ protoc-gen-js was 85.3% ops/sec slower (factor 6.8)
363
+ protoc-gen-es was 88.7% ops/sec slower (factor 8.8)
364
+
365
+ benchmarking round-trip performance ...
366
+
367
+ protobuf.js reflect x 1,310,677 ops/sec ±0.21% (97 runs sampled)
368
+ protobuf.js static x 1,310,926 ops/sec ±0.26% (101 runs sampled)
369
+ JSON encode/decode x 741,714 ops/sec ±0.24% (99 runs sampled)
370
+ protoc-gen-js x 472,844 ops/sec ±0.09% (96 runs sampled)
371
+ protoc-gen-es x 254,044 ops/sec ±0.05% (101 runs sampled)
372
+
373
+ protobuf.js reflect was fastest
374
+ protobuf.js static was 0.0% ops/sec slower (factor 1.0)
375
+ JSON encode/decode was 43.4% ops/sec slower (factor 1.8)
376
+ protoc-gen-js was 63.9% ops/sec slower (factor 2.8)
377
+ protoc-gen-es was 80.6% ops/sec slower (factor 5.2)
349
378
  ```
350
379
 
351
380
  Run it locally with:
352
381
 
353
382
  ```sh
383
+ npm --prefix bench install
354
384
  npm run bench
355
385
  ```
356
386
 
387
+ ## Compatibility
388
+
389
+ Supported runtimes are browsers, Node.js v12+, Deno and Bun. When using the CLI with Bun, Node.js must also be installed.
390
+
391
+ ## Security
392
+
393
+ protobuf.js favors transparent disclosure. Security-impacting reports are handled through coordinated GitHub Security Advisories where appropriate. See [SECURITY.md](./SECURITY.md) for supported release lines, reporting instructions, and notes on untrusted schema input.
394
+
357
395
  ## Development
358
396
 
359
397
  ```sh