protobufjs 8.7.1 → 8.8.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 +70 -86
- package/dist/light/protobuf.js +185 -54
- 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 +105 -44
- 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 +220 -81
- 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.generated.d.ts +7 -0
- package/ext/descriptor.js +50 -10
- package/ext/protojson.js +14 -4
- package/ext/textformat.js +8 -4
- package/google/protobuf/descriptor.json +20 -1
- package/google/protobuf/descriptor.proto +14 -1
- package/index.d.ts +15 -0
- package/package.json +1 -1
- package/src/converter.js +5 -1
- package/src/decoder.js +31 -2
- package/src/enum.js +15 -2
- package/src/field.js +6 -1
- package/src/object.js +5 -2
- package/src/parse.js +35 -27
- package/src/reader.js +94 -13
- package/src/type.js +10 -0
- package/src/util/codegen.js +3 -1
- package/src/util/utf8.js +2 -28
- package/src/wrappers.js +5 -1
- package/src/writer.js +7 -1
package/README.md
CHANGED
|
@@ -7,15 +7,13 @@
|
|
|
7
7
|
<a href="https://www.jsdelivr.com/package/npm/protobufjs"><img src="https://img.shields.io/jsdelivr/npm/hm/protobufjs?label=requests&logo=jsdelivr" alt=""></a>
|
|
8
8
|
</p>
|
|
9
9
|
|
|
10
|
-
**
|
|
11
|
-
|
|
12
|
-
**protobuf.js** is a very fast, conformant, and unusually versatile JavaScript implementation of Protocol Buffers for Node.js and browsers. It works with `.proto` files out of the box, does not require protoc, and supports runtime reflection as well as specialized code generation with strong TypeScript declarations.
|
|
10
|
+
**protobuf.js** is a very fast, conformant, and unusually versatile JavaScript implementation of [Protocol Buffers](https://protobuf.dev) for Node.js and browsers. It is independently maintained with contributions from the upstream Protocol Buffers project, works with `.proto` schemas without requiring `protoc`, and supports runtime reflection as well as specialized code generation with matching TypeScript declarations.
|
|
13
11
|
|
|
14
12
|
If protobuf.js is important to your project or organization, or if you depend on it commercially, [consider supporting](https://github.com/sponsors/dcodeIO) its ongoing maintenance. Sponsorship helps make bug fixes, releases, LTS/security handling, and user support more sustainable.
|
|
15
13
|
|
|
16
14
|
## Getting started
|
|
17
15
|
|
|
18
|
-
Getting up and running is simple: Install the package, load a
|
|
16
|
+
Getting up and running is simple: Install the package, load a schema, and you are all set to encode and decode Protobuf messages. From there, protobuf.js grows with your requirements: Add code generation with TypeScript declarations, transport-agnostic services, support for text-based formats, and more as needed.
|
|
19
17
|
|
|
20
18
|
### Install
|
|
21
19
|
|
|
@@ -33,7 +31,7 @@ The CLI is a JS-native protobuf.js toolchain that does not require setting up `p
|
|
|
33
31
|
|
|
34
32
|
#### Browser builds
|
|
35
33
|
|
|
36
|
-
Canonical browser builds
|
|
34
|
+
Canonical browser builds are [provided via the jsDelivr CDN](https://cdn.jsdelivr.net/npm/protobufjs@8.X.X/dist/), supporting CommonJS, AMD and global `window.protobuf`. Make sure to pin an exact version in production.
|
|
37
35
|
|
|
38
36
|
## Usage
|
|
39
37
|
|
|
@@ -78,7 +76,7 @@ const decoded = AwesomeMessage.decode(encoded);
|
|
|
78
76
|
|
|
79
77
|
Plain objects can be encoded directly when they already use protobuf.js runtime types: numbers for 32-bit numeric fields, booleans for `bool`, strings for `string`, `Uint8Array` or `Buffer` for `bytes`, arrays for repeated fields, and plain objects for maps. Map keys are the string representation of the respective value or an 8-character hash string for 64-bit keys.
|
|
80
78
|
|
|
81
|
-
|
|
79
|
+
Note that, as with any structured binary format, decoded structures incur runtime memory overhead beyond their encoded representation. Applications processing untrusted input should therefore apply appropriate input-size and concurrency limits to bound memory use. For this reason, unknown fields present on the wire are intentionally discarded as the safer default, but can be retained by setting `reader.discardUnknown = false` per reader, or by setting `Reader.discardUnknown = false` to make it the default for subsequently created readers. Preserved unknown field data can also be explicitly dropped with `delete message.$unknowns`.
|
|
82
80
|
|
|
83
81
|
### Convert plain objects
|
|
84
82
|
|
|
@@ -138,41 +136,28 @@ Message types expose focused methods for validation, conversion, and binary I/O.
|
|
|
138
136
|
* **message.toJSON**(): `object`
|
|
139
137
|
Converts a message instance to JSON-compatible output using default conversion options.
|
|
140
138
|
|
|
141
|
-
Message instances provide runtime identity
|
|
139
|
+
Message instances provide stable runtime identity for testing with `instanceof`.
|
|
142
140
|
|
|
143
141
|
Length-delimited methods read and write a varint byte length before the message, which is useful for streams and framed protocols.
|
|
144
142
|
|
|
145
143
|
If required fields are missing while decoding proto2 data, `decode` throws `protobuf.util.ProtocolError` with the partially decoded message available as `err.instance`.
|
|
146
144
|
|
|
147
|
-
## Runtimes
|
|
148
|
-
|
|
149
|
-
protobuf.js provides three runtime entry points, keeping parser and reflection support optional: Runtime `.proto` loading needs the parser, JSON/reflection bundles need reflection support, and generated static modules only need the minimal runtime.
|
|
150
|
-
|
|
151
|
-
| Import | Includes | Use when
|
|
152
|
-
| ----------------------- | ------------------ | --------
|
|
153
|
-
| `protobufjs` | Reflection, Parser | You load `.proto` files at runtime
|
|
154
|
-
| `protobufjs/light.js` | Reflection | You load JSON bundles or build schemas programmatically
|
|
155
|
-
| `protobufjs/minimal.js` | Static runtime | You use generated static code
|
|
156
|
-
|
|
157
|
-
The full build includes the light build, and the light build includes the minimal runtime.
|
|
158
|
-
|
|
159
145
|
## Code generation
|
|
160
146
|
|
|
161
|
-
Use [`protobufjs-cli`](./cli/#readme) to generate
|
|
147
|
+
Use [`protobufjs-cli`](./cli/#readme) to generate schema-specific code, either directly with `pbjs` or through the optional `protoc-gen-pbjs` plugin for `protoc`.
|
|
162
148
|
|
|
163
|
-
|
|
149
|
+
protobuf.js offers two code-generation modes, both with matching TypeScript declarations: reflection modules generate optimized code at runtime with reflection metadata retained; static modules generate reflection-free code ahead of time. Both use the same runtime package and automatically import only the runtime capabilities they need.
|
|
164
150
|
|
|
165
|
-
| Target | Output |
|
|
166
|
-
|
|
167
|
-
| `json` |
|
|
168
|
-
| `
|
|
169
|
-
| `static-module` | Static code module | `protobufjs/minimal.js` |
|
|
151
|
+
| Target | Output | Runtime entry |
|
|
152
|
+
|--------|--------|---------------|
|
|
153
|
+
| `json-module` | Reflection module | `protobufjs/light.js` (without parser) |
|
|
154
|
+
| `static-module` | Static code module | `protobufjs/minimal.js` (without reflection) |
|
|
170
155
|
|
|
171
156
|
Module targets support `--wrap default` for CommonJS and AMD, plus `esm`, `commonjs`, `amd`, and `closure`; `--wrap` can also load a custom wrapper module.
|
|
172
157
|
|
|
173
158
|
### Static modules
|
|
174
159
|
|
|
175
|
-
Static modules emit dedicated JavaScript for your schema
|
|
160
|
+
Static modules emit dedicated, reflection-free JavaScript code for your schema.
|
|
176
161
|
|
|
177
162
|
```sh
|
|
178
163
|
npx pbjs -t static-module -w esm -o awesome.js --dts awesome.proto
|
|
@@ -184,22 +169,11 @@ import { awesomepackage } from "./awesome.js";
|
|
|
184
169
|
const message = awesomepackage.AwesomeMessage.create({ awesomeField: "hello" });
|
|
185
170
|
```
|
|
186
171
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
### Reflection bundles
|
|
190
|
-
|
|
191
|
-
Reflection bundles store schemas as compact JSON metadata, avoiding `.proto` parsing at runtime and letting browsers load schema metadata in one request. While they require at least `protobufjs/light.js`, large schemas can produce smaller combined bundles than equivalent static modules because common code is shared through reflection.
|
|
172
|
+
Static code is repetitive by design, but compresses unusually well with Brotli or gzip and works in [CSP](https://w3c.github.io/webappsec-csp/)-restricted environments.
|
|
192
173
|
|
|
193
|
-
|
|
194
|
-
npx pbjs -t json -o awesome.json awesome1.proto awesome2.proto ...
|
|
195
|
-
```
|
|
174
|
+
### Reflection modules
|
|
196
175
|
|
|
197
|
-
|
|
198
|
-
const bundle = require("./awesome.json");
|
|
199
|
-
|
|
200
|
-
const root = protobuf.Root.fromJSON(bundle);
|
|
201
|
-
const AwesomeMessage = root.lookupType("awesomepackage.AwesomeMessage");
|
|
202
|
-
```
|
|
176
|
+
Reflection modules wrap schemas as compact JSON metadata while avoiding `.proto` parsing at runtime.
|
|
203
177
|
|
|
204
178
|
```sh
|
|
205
179
|
npx pbjs -t json-module -w esm -o awesome.js --dts awesome.proto
|
|
@@ -211,13 +185,11 @@ import { awesomepackage } from "./awesome.js";
|
|
|
211
185
|
const AwesomeMessage = awesomepackage.AwesomeMessage;
|
|
212
186
|
```
|
|
213
187
|
|
|
214
|
-
|
|
188
|
+
Declarations for reflection modules mirror `static-module` typings. Because JSON modules export reflection objects, message instances should be created with `MyMessage.create(...)` rather than constructors. Code using `create(...)` works with static modules as well. Separately, `-t json` can serialize reflection metadata as bare JSON bundles for use with `load()` or `Root.fromJSON()`.
|
|
215
189
|
|
|
216
190
|
### TypeScript integration
|
|
217
191
|
|
|
218
|
-
protobuf.js works with TypeScript out of the box:
|
|
219
|
-
|
|
220
|
-
For example, given the oneof:
|
|
192
|
+
protobuf.js works with TypeScript out of the box: its runtime API is typed, and generated code can be paired with matching TypeScript declarations in a single CLI invocation. Declarations are strongly typed, including discriminated unions for oneofs and scoped types for plain-object usage:
|
|
221
193
|
|
|
222
194
|
```proto
|
|
223
195
|
message Profile {
|
|
@@ -228,8 +200,6 @@ message Profile {
|
|
|
228
200
|
}
|
|
229
201
|
```
|
|
230
202
|
|
|
231
|
-
Generated declarations narrow both the `contact` oneof and the concrete values:
|
|
232
|
-
|
|
233
203
|
```ts
|
|
234
204
|
const profile = Profile.create({
|
|
235
205
|
contact: "email",
|
|
@@ -246,7 +216,7 @@ if (decoded.contact === "phone") {
|
|
|
246
216
|
}
|
|
247
217
|
```
|
|
248
218
|
|
|
249
|
-
|
|
219
|
+
The same narrowed shape is available for plain-object inputs:
|
|
250
220
|
|
|
251
221
|
```ts
|
|
252
222
|
const object: Profile.$Shape = {
|
|
@@ -259,7 +229,7 @@ const object: Profile.$Shape = {
|
|
|
259
229
|
|
|
260
230
|
### Programmatic schemas
|
|
261
231
|
|
|
262
|
-
|
|
232
|
+
Schemas can be constructed directly through reflection:
|
|
263
233
|
|
|
264
234
|
```ts
|
|
265
235
|
const AwesomeMessage = new protobuf.Type("AwesomeMessage")
|
|
@@ -316,22 +286,7 @@ const myService = MyService.create(myRpcImpl/*, requestDelimited?, responseDelim
|
|
|
316
286
|
|
|
317
287
|
See [examples/streaming-rpc.js](./examples/streaming-rpc.js) for a streaming example.
|
|
318
288
|
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
```js
|
|
322
|
-
const grpc = require('@grpc/grpc-js');
|
|
323
|
-
|
|
324
|
-
const Client = grpc.makeGenericClientConstructor({});
|
|
325
|
-
const client = new Client(serverAddress, grpc.credentials.createInsecure());
|
|
326
|
-
|
|
327
|
-
const rpcImpl = (method, requestData, callback) =>
|
|
328
|
-
client.makeUnaryRequest(method.path, (data) => data, (data) => data, requestData, callback);
|
|
329
|
-
|
|
330
|
-
const greeter = root.lookupService("example.Greeter").create(rpcImpl);
|
|
331
|
-
const reply = await greeter.sayHello({ name: "world" });
|
|
332
|
-
```
|
|
333
|
-
|
|
334
|
-
See [examples/grpc-service.js](./examples/grpc-service.js) for a complete example.
|
|
289
|
+
See [examples/grpc-service.js](./examples/grpc-service.js) for an integration example with [@grpc/grpc-js](https://www.npmjs.com/package/@grpc/grpc-js).
|
|
335
290
|
|
|
336
291
|
### Extensions
|
|
337
292
|
|
|
@@ -339,7 +294,7 @@ The following extensions provide descriptor conversion and text-based protobuf f
|
|
|
339
294
|
|
|
340
295
|
#### Descriptors
|
|
341
296
|
|
|
342
|
-
protobuf.js uses a compact JSON-based reflection representation internally
|
|
297
|
+
protobuf.js uses a compact JSON-based reflection representation internally. See [ext/descriptor](./ext/README.md#descriptor) for use cases that need conversion between reflected roots and `protoc` descriptor messages.
|
|
343
298
|
|
|
344
299
|
#### ProtoJSON
|
|
345
300
|
|
|
@@ -351,7 +306,7 @@ Protocol Buffers [Text Format](https://protobuf.dev/reference/protobuf/textforma
|
|
|
351
306
|
|
|
352
307
|
## Conformance
|
|
353
308
|
|
|
354
|
-
protobuf.js is validated against the official Protocol Buffers conformance suite
|
|
309
|
+
protobuf.js is validated against the official Protocol Buffers conformance suite. It passes all required and recommended tests for the **Proto2**, **Proto3** and **Editions** binary wire formats, with complete **ProtoJSON** and **Text Format** support available as optional extensions when those formats are needed.
|
|
355
310
|
|
|
356
311
|
<!-- BEGIN CONFORMANCE DATA -->
|
|
357
312
|
|
|
@@ -361,46 +316,75 @@ protobuf.js is validated against the official Protocol Buffers conformance suite
|
|
|
361
316
|
| ↳ Proto2 | 100.00% (707/707) | 100.00% (489/489) | 100.00% (218/218) |
|
|
362
317
|
| ↳ Proto3 | 100.00% (707/707) | 100.00% (486/486) | 100.00% (221/221) |
|
|
363
318
|
| ↳ Editions | 100.00% (1421/1421) | 100.00% (983/983) | 100.00% (438/438) |
|
|
364
|
-
| ProtoJSON | 100.00% (
|
|
319
|
+
| ProtoJSON | 100.00% (2814/2814) | 100.00% (2362/2362) | 100.00% (452/452) |
|
|
365
320
|
| TextFormat | 100.00% (909/909) | 100.00% (845/845) | 100.00% (64/64) |
|
|
366
|
-
| Overall | 100.00% (
|
|
321
|
+
| Overall | 100.00% (6558/6558) | 100.00% (5165/5165) | 100.00% (1393/1393) |
|
|
367
322
|
|
|
368
323
|
<!-- END CONFORMANCE DATA -->
|
|
369
324
|
|
|
370
|
-
|
|
325
|
+
| Edition | Supported since
|
|
326
|
+
|---------|----------------
|
|
327
|
+
| 2023 | v7.5.0
|
|
328
|
+
| 2024 | v8.0.0
|
|
329
|
+
| 2026 | v8.8.0
|
|
330
|
+
|
|
331
|
+
[Structured results](https://github.com/protobufjs/protobuf.js/actions/workflows/test.yml?query=branch%3Amaster+event%3Apush) of the conformance tests are also available as CI artifacts.
|
|
371
332
|
|
|
372
333
|
## Performance
|
|
373
334
|
|
|
374
|
-
|
|
335
|
+
protobuf.js's reflection and static modes share the same code-generation backend and underlying hand-tuned reader and writer primitives. Reflection mode emits specialized encoders and decoders just in time, while static mode emits equivalent code ahead of time.
|
|
375
336
|
|
|
376
|
-
|
|
337
|
+
To see how this architecture compares in practice, the repository includes a reproducible [benchmark suite](./bench) measuring both modes alongside other general-purpose JavaScript implementations across three cases: our classic common message shape and two unmodified, structurally distinct fixtures sourced from other implementations. Each library is tested through its recommended serialization and deserialization path using identical schemas and message contents.
|
|
377
338
|
|
|
378
339
|
<!-- BEGIN BENCHMARK DATA -->
|
|
379
340
|
|
|
380
|
-
|
|
341
|
+
<picture>
|
|
342
|
+
<source media="(prefers-color-scheme: dark)" srcset="./bench/results/encode-dark.svg">
|
|
343
|
+
<source media="(prefers-color-scheme: light)" srcset="./bench/results/encode-light.svg">
|
|
344
|
+
<img alt="Encode Throughput" src="./bench/results/encode.svg">
|
|
345
|
+
</picture>
|
|
346
|
+
|
|
347
|
+
<details>
|
|
348
|
+
<summary>Show table</summary>
|
|
349
|
+
|
|
350
|
+
| Implementation | Common | Vector tile | Buf perf |
|
|
351
|
+
| --- | ---: | ---: | ---: |
|
|
352
|
+
| protobuf.js static | **5.28M ops/s** <small>1.0x</small> | 3.06K ops/s <small>1.0x</small> | **49.2K ops/s** <small>1.0x</small> |
|
|
353
|
+
| protobuf.js reflect | 5.16M ops/s <small>1.0x</small> | **3.08K ops/s** <small>1.0x</small> | 47.5K ops/s <small>1.0x</small> |
|
|
354
|
+
| JSON | 3.44M ops/s <small>1.5x</small> | 1.50K ops/s <small>2.1x</small> | 7.13K ops/s <small>6.9x</small> |
|
|
355
|
+
| protoc-gen-js | 1.05M ops/s <small>5.0x</small> | 691 ops/s <small>4.5x</small> | 15.0K ops/s <small>3.3x</small> |
|
|
356
|
+
| protoc-gen-es | 1.22M ops/s <small>4.3x</small> | 1.14K ops/s <small>2.7x</small> | 35.8K ops/s <small>1.4x</small> |
|
|
357
|
+
|
|
358
|
+
</details>
|
|
359
|
+
|
|
360
|
+
<picture>
|
|
361
|
+
<source media="(prefers-color-scheme: dark)" srcset="./bench/results/decode-dark.svg">
|
|
362
|
+
<source media="(prefers-color-scheme: light)" srcset="./bench/results/decode-light.svg">
|
|
363
|
+
<img alt="Decode Throughput" src="./bench/results/decode.svg">
|
|
364
|
+
</picture>
|
|
381
365
|
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
| Common | 3.18M ops/s | 3.25M ops/s | 2.07M ops/s | 1.01M ops/s | 395K ops/s |
|
|
385
|
-
| Vector tile | 2.82K ops/s | 2.76K ops/s | 859 ops/s | 680 ops/s | 231 ops/s |
|
|
386
|
-
| Buf perf | 41.4K ops/s | 39.9K ops/s | 6.59K ops/s | 13.5K ops/s | 8.00K ops/s |
|
|
366
|
+
<details>
|
|
367
|
+
<summary>Show table</summary>
|
|
387
368
|
|
|
388
|
-
|
|
369
|
+
| Implementation | Common | Vector tile | Buf perf |
|
|
370
|
+
| --- | ---: | ---: | ---: |
|
|
371
|
+
| protobuf.js static | 5.84M ops/s <small>1.1x</small> | 2.45K ops/s <small>1.1x</small> | **78.9K ops/s** <small>1.0x</small> |
|
|
372
|
+
| protobuf.js reflect | **6.54M ops/s** <small>1.0x</small> | **2.66K ops/s** <small>1.0x</small> | 77.0K ops/s <small>1.0x</small> |
|
|
373
|
+
| JSON | 1.58M ops/s <small>4.1x</small> | 1.21K ops/s <small>2.2x</small> | 21.0K ops/s <small>3.8x</small> |
|
|
374
|
+
| protoc-gen-js | 701K ops/s <small>9.3x</small> | 946 ops/s <small>2.8x</small> | 21.5K ops/s <small>3.7x</small> |
|
|
375
|
+
| protoc-gen-es | 1.67M ops/s <small>3.9x</small> | 1.19K ops/s <small>2.2x</small> | 31.0K ops/s <small>2.5x</small> |
|
|
389
376
|
|
|
390
|
-
|
|
391
|
-
| --- | ---: | ---: | ---: | ---: | ---: |
|
|
392
|
-
| Common | 6.11M ops/s | 6.09M ops/s | 1.31M ops/s | 790K ops/s | 710K ops/s |
|
|
393
|
-
| Vector tile | 2.45K ops/s | 2.78K ops/s | 1.04K ops/s | 858 ops/s | 392 ops/s |
|
|
394
|
-
| Buf perf | 72.5K ops/s | 66.4K ops/s | 19.0K ops/s | 21.1K ops/s | 14.2K ops/s |
|
|
377
|
+
</details>
|
|
395
378
|
|
|
396
379
|
<!-- END BENCHMARK DATA -->
|
|
397
380
|
|
|
398
|
-
[Structured results](./bench/results/latest.json)
|
|
381
|
+
[Structured results](./bench/results/latest.json) include environment details for this run and are committed alongside the charts. The suite also runs in CI.
|
|
399
382
|
|
|
400
|
-
To run the benchmark
|
|
383
|
+
To run the benchmark on your own hardware:
|
|
401
384
|
|
|
402
385
|
```sh
|
|
403
386
|
npm --prefix bench install
|
|
387
|
+
npm --prefix bench run generate
|
|
404
388
|
npm run bench
|
|
405
389
|
```
|
|
406
390
|
|