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 +113 -75
- package/dist/light/protobuf.js +119 -55
- package/dist/light/protobuf.js.map +1 -1
- package/dist/light/protobuf.min.js +3 -3
- package/dist/light/protobuf.min.js.map +1 -1
- package/dist/minimal/protobuf.js +61 -18
- package/dist/minimal/protobuf.js.map +1 -1
- package/dist/minimal/protobuf.min.js +3 -3
- package/dist/minimal/protobuf.min.js.map +1 -1
- package/dist/protobuf.js +138 -60
- package/dist/protobuf.js.map +1 -1
- package/dist/protobuf.min.js +3 -3
- package/dist/protobuf.min.js.map +1 -1
- package/ext/descriptor/index.d.ts +2 -1
- package/ext/descriptor.d.ts +11 -234
- package/ext/descriptor.generated.d.ts +409 -0
- package/ext/descriptor.js +96 -66
- package/ext/textformat.d.ts +2 -13
- package/ext/textformat.generated.d.ts +11 -0
- package/ext/textformat.js +28 -42
- package/index.d.ts +304 -275
- package/package.json +4 -7
- package/src/converter.js +3 -3
- package/src/decoder.js +6 -2
- package/src/enum.js +8 -3
- package/src/field.js +2 -0
- package/src/message.js +3 -6
- package/src/method.js +1 -1
- package/src/namespace.js +10 -2
- package/src/object.js +0 -1
- package/src/oneof.js +1 -0
- package/src/parse.js +19 -5
- package/src/root.js +4 -1
- package/src/service.js +6 -1
- package/src/type.js +12 -3
- package/src/typescript.js +19 -0
- package/src/util/minimal.js +8 -1
- package/src/util/utf8.js +39 -13
- package/src/util.js +2 -14
- package/src/writer.js +6 -1
- package/src/writer_buffer.js +6 -1
- package/tsconfig.json +2 -4
- package/src/typescript.jsdoc +0 -15
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
|
-
```
|
|
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
|
-
```
|
|
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
|
-
```
|
|
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
|
-
```
|
|
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
|
-
```
|
|
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
|
-
|
|
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
|
-
```
|
|
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
|
-
|
|
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
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
AwesomeMessage.prototype.customInstanceMethod = function() {
|
|
249
|
-
// ...
|
|
250
|
-
};
|
|
251
|
-
```
|
|
280
|
+
```ts
|
|
281
|
+
class AwesomeMessage extends protobuf.Message<AwesomeMessage> {
|
|
282
|
+
awesomeField = "";
|
|
252
283
|
|
|
253
|
-
|
|
284
|
+
constructor(properties?: protobuf.Properties<AwesomeMessage>) {
|
|
285
|
+
super(properties);
|
|
286
|
+
// ...
|
|
287
|
+
}
|
|
254
288
|
|
|
255
|
-
|
|
256
|
-
|
|
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
|
-
|
|
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
|
|
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
|
|
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.
|
|
334
|
+
One run on an AMD Ryzen 9 9950X3D with Node.js 24.15.0 produced:
|
|
306
335
|
|
|
307
336
|
```
|
|
308
|
-
benchmarking
|
|
309
|
-
|
|
310
|
-
protobuf.js
|
|
311
|
-
protobuf.js
|
|
312
|
-
JSON
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
benchmarking
|
|
323
|
-
|
|
324
|
-
protobuf.js
|
|
325
|
-
protobuf.js
|
|
326
|
-
JSON
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
benchmarking
|
|
337
|
-
|
|
338
|
-
protobuf.js
|
|
339
|
-
protobuf.js
|
|
340
|
-
JSON
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
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
|