protobufjs 8.4.2 → 8.6.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 +65 -58
- package/dist/light/protobuf.js +167 -50
- 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 +26 -8
- 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 +182 -51
- package/dist/protobuf.js.map +1 -1
- package/dist/protobuf.min.js +3 -3
- package/dist/protobuf.min.js.map +1 -1
- package/ext/README.md +38 -49
- package/ext/descriptor.generated.d.ts +2 -2
- package/ext/descriptor.js +18 -3
- package/ext/protojson.LICENSE +201 -0
- package/ext/protojson.d.ts +20 -0
- package/ext/protojson.generated.d.ts +49 -0
- package/ext/protojson.js +903 -0
- package/ext/textformat.d.ts +2 -2
- package/ext/textformat.generated.d.ts +22 -0
- package/ext/textformat.js +40 -19
- package/index.d.ts +48 -9
- package/package.json +4 -7
- package/src/converter.js +7 -5
- package/src/decoder.js +6 -4
- package/src/encoder.js +1 -1
- package/src/field.js +49 -7
- package/src/mapfield.js +16 -7
- package/src/method.js +16 -2
- package/src/namespace.js +4 -4
- package/src/parse.js +15 -1
- package/src/reader.js +12 -0
- package/src/roots.js +1 -1
- package/src/rpc/service.js +10 -4
- package/src/service.js +5 -7
- package/src/type.js +12 -3
- package/src/util/minimal.js +1 -1
- package/src/util.js +23 -0
- package/src/verifier.js +2 -2
- package/scripts/postinstall.js +0 -32
package/README.md
CHANGED
|
@@ -9,13 +9,9 @@
|
|
|
9
9
|
|
|
10
10
|
**Protocol Buffers** are a language-neutral, platform-neutral, extensible way of serializing structured data for use in communications protocols, data storage, and more, originally designed at Google ([see](https://protobuf.dev/)).
|
|
11
11
|
|
|
12
|
-
**protobuf.js** is a standalone JavaScript implementation of Protocol Buffers for Node.js and
|
|
12
|
+
**protobuf.js** is a standalone JavaScript implementation of Protocol Buffers for Node.js and browsers. It is tuned for fast binary I/O, battle-tested at scale, can load `.proto` files directly, and supports runtime reflection as well as static code generation with strong TypeScript declarations.
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
protobuf.js has grown from a personal project into a widely used JavaScript infrastructure library for Protocol Buffers. It is independently maintained, with participation from the upstream Protocol Buffers ecosystem, and is intentionally not tied to any specific vendor platform, commercial service, or schema registry.
|
|
17
|
-
|
|
18
|
-
If protobuf.js is important to your project or organization, especially if you depend on it commercially, [consider supporting](https://github.com/sponsors/dcodeIO) its ongoing maintenance.
|
|
14
|
+
If protobuf.js is important to your project or organization, especially if you depend on it commercially, [consider supporting its ongoing maintenance](https://github.com/sponsors/dcodeIO).
|
|
19
15
|
|
|
20
16
|
## Getting started
|
|
21
17
|
|
|
@@ -31,34 +27,11 @@ The [command line utility](./cli/#readme) for generating reflection bundles, sta
|
|
|
31
27
|
npm install --save-dev protobufjs-cli
|
|
32
28
|
```
|
|
33
29
|
|
|
34
|
-
The CLI is a
|
|
35
|
-
|
|
36
|
-
### Choose a runtime
|
|
37
|
-
|
|
38
|
-
Pick the smallest runtime variant that supports how your application loads schemas and whether it needs reflection at runtime.
|
|
39
|
-
|
|
40
|
-
| Import | Includes | Use when
|
|
41
|
-
| ----------------------- | ------------------ | --------
|
|
42
|
-
| `protobufjs` | Reflection, Parser | You load `.proto` files at runtime
|
|
43
|
-
| `protobufjs/light.js` | Reflection | You load JSON bundles or build schemas programmatically
|
|
44
|
-
| `protobufjs/minimal.js` | Static runtime | You use generated static code
|
|
45
|
-
|
|
46
|
-
The full build includes the light build, and the light build includes the minimal runtime.
|
|
30
|
+
The CLI is a JS-native protobuf.js toolchain that does not require setting up `protoc`. If you prefer a `protoc`-based workflow, it provides `protoc-gen-pbjs` as an option.
|
|
47
31
|
|
|
48
|
-
|
|
32
|
+
#### Browser builds
|
|
49
33
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
```html
|
|
53
|
-
<!-- Full -->
|
|
54
|
-
<script src="https://cdn.jsdelivr.net/npm/protobufjs@8.X.X/dist/protobuf.min.js"></script>
|
|
55
|
-
<!-- Light -->
|
|
56
|
-
<script src="https://cdn.jsdelivr.net/npm/protobufjs@8.X.X/dist/light/protobuf.min.js"></script>
|
|
57
|
-
<!-- Minimal -->
|
|
58
|
-
<script src="https://cdn.jsdelivr.net/npm/protobufjs@8.X.X/dist/minimal/protobuf.min.js"></script>
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
Browser builds support CommonJS and AMD loaders and export globally as `window.protobuf`.
|
|
34
|
+
Canonical browser builds for each runtime variant 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.
|
|
62
35
|
|
|
63
36
|
## Usage
|
|
64
37
|
|
|
@@ -92,10 +65,6 @@ Optionally use `load()` with a callback, or `loadSync()` for synchronous loading
|
|
|
92
65
|
```ts
|
|
93
66
|
const payload = { awesomeField: "hello" };
|
|
94
67
|
|
|
95
|
-
// Optionally verify if the payload is of uncertain shape
|
|
96
|
-
const err = AwesomeMessage.verify(payload);
|
|
97
|
-
if (err) throw Error(err);
|
|
98
|
-
|
|
99
68
|
// Optionally create a message instance from already valid data
|
|
100
69
|
const message = AwesomeMessage.create(payload);
|
|
101
70
|
|
|
@@ -103,11 +72,11 @@ const encoded = AwesomeMessage.encode(message).finish();
|
|
|
103
72
|
const decoded = AwesomeMessage.decode(encoded);
|
|
104
73
|
```
|
|
105
74
|
|
|
106
|
-
`encode` expects a message instance or equivalent plain object and does not verify input implicitly. Use `
|
|
75
|
+
`encode` expects a message instance or equivalent plain object and does not verify input implicitly. Use `create` to create a message instance from already valid data when useful, `verify` for plain objects whose shape is not guaranteed, and `fromObject` when conversion from broader JavaScript objects is needed.
|
|
107
76
|
|
|
108
|
-
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
|
|
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.
|
|
109
78
|
|
|
110
|
-
|
|
79
|
+
Unknown fields present on the wire are preserved by default in `message.$unknowns` and forwarded when the message is re-encoded. Unknown field data can be dropped from a decoded message with `delete message.$unknowns`, discarded during decode with `reader.discardUnknown = true` per reader, or disabled by default for subsequently created readers with `Reader.discardUnknown = true`.
|
|
111
80
|
|
|
112
81
|
### Convert plain objects
|
|
113
82
|
|
|
@@ -164,16 +133,30 @@ Message types expose focused methods for validation, conversion, and binary I/O.
|
|
|
164
133
|
* **toObject**(message: `Message`, options?: `ConversionOptions`): `object`
|
|
165
134
|
Converts a message instance to a configurable plain JavaScript object.
|
|
166
135
|
|
|
167
|
-
* **message
|
|
136
|
+
* **message.toJSON**(): `object`
|
|
168
137
|
Converts a message instance to JSON-compatible output using default conversion options.
|
|
169
138
|
|
|
139
|
+
Message instances provide runtime identity, so they can be tested with `instanceof`. Their `toJSON` method integrates them with `JSON.stringify`.
|
|
140
|
+
|
|
170
141
|
Length-delimited methods read and write a varint byte length before the message, which is useful for streams and framed protocols.
|
|
171
142
|
|
|
172
143
|
If required fields are missing while decoding proto2 data, `decode` throws `protobuf.util.ProtocolError` with the partially decoded message available as `err.instance`.
|
|
173
144
|
|
|
145
|
+
## Runtimes
|
|
146
|
+
|
|
147
|
+
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.
|
|
148
|
+
|
|
149
|
+
| Import | Includes | Use when
|
|
150
|
+
| ----------------------- | ------------------ | --------
|
|
151
|
+
| `protobufjs` | Reflection, Parser | You load `.proto` files at runtime
|
|
152
|
+
| `protobufjs/light.js` | Reflection | You load JSON bundles or build schemas programmatically
|
|
153
|
+
| `protobufjs/minimal.js` | Static runtime | You use generated static code
|
|
154
|
+
|
|
155
|
+
The full build includes the light build, and the light build includes the minimal runtime.
|
|
156
|
+
|
|
174
157
|
## Code generation
|
|
175
158
|
|
|
176
|
-
|
|
159
|
+
Use [`protobufjs-cli`](./cli/#readme) to generate reflection bundles, static JavaScript code, and matching TypeScript declarations, either directly with `pbjs` or through the optional `protoc-gen-pbjs` plugin for `protoc`.
|
|
177
160
|
|
|
178
161
|
Reflection keeps schemas as JSON metadata and generates optimized functions at runtime. Static code emits schema-specific, reflection-free functions ahead of time. The main tradeoffs are how schemas are loaded, how bundle size scales with schema size, and whether reflection metadata should remain available at runtime.
|
|
179
162
|
|
|
@@ -199,9 +182,11 @@ import { awesomepackage } from "./awesome.js";
|
|
|
199
182
|
const message = awesomepackage.AwesomeMessage.create({ awesomeField: "hello" });
|
|
200
183
|
```
|
|
201
184
|
|
|
185
|
+
While static code is verbose by design, its repeated patterns compress well with Brotli or gzip, and it works in [CSP](https://w3c.github.io/webappsec-csp/)-restricted environments that disallow unsafe-eval without sacrificing performance.
|
|
186
|
+
|
|
202
187
|
### Reflection bundles
|
|
203
188
|
|
|
204
|
-
|
|
189
|
+
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.
|
|
205
190
|
|
|
206
191
|
```sh
|
|
207
192
|
npx pbjs -t json -o awesome.json awesome1.proto awesome2.proto ...
|
|
@@ -311,35 +296,57 @@ protobuf.js will populate the constructor with the usual static runtime methods
|
|
|
311
296
|
|
|
312
297
|
### Services
|
|
313
298
|
|
|
314
|
-
protobuf.js supports service clients built from
|
|
299
|
+
protobuf.js supports service clients built from service definitions. The service API is transport-agnostic: provide an `rpcImpl` function to connect it to HTTP, WebSocket, gRPC, or another transport.
|
|
300
|
+
|
|
301
|
+
```js
|
|
302
|
+
function myRpcImpl(method, requestData, callback) {
|
|
303
|
+
// method.name
|
|
304
|
+
// method.path
|
|
305
|
+
// method.requestStream?
|
|
306
|
+
// method.responseStream?
|
|
307
|
+
performRequest(requestData, function(err, responseData) {
|
|
308
|
+
callback(err, responseData);
|
|
309
|
+
});
|
|
310
|
+
}
|
|
315
311
|
|
|
316
|
-
|
|
312
|
+
const myService = MyService.create(myRpcImpl/*, requestDelimited?, responseDelimited? */);
|
|
313
|
+
```
|
|
317
314
|
|
|
318
|
-
|
|
315
|
+
See [examples/streaming-rpc.js](./examples/streaming-rpc.js) for a streaming example.
|
|
319
316
|
|
|
320
|
-
###
|
|
317
|
+
### Extensions
|
|
321
318
|
|
|
322
|
-
|
|
319
|
+
The following extensions provide descriptor conversion and text-based protobuf formats when reflection metadata is available. Most applications only need the binary APIs above.
|
|
323
320
|
|
|
324
|
-
|
|
321
|
+
#### Descriptors
|
|
325
322
|
|
|
326
|
-
|
|
323
|
+
protobuf.js uses a compact JSON-based reflection representation internally that is easy to embed and fast to parse, so schemas can be loaded directly without first decoding binary descriptor blobs or postprocessing their full JSON representation. See [ext/descriptor](./ext/README.md#descriptor) for use cases that need conversion between reflected roots and `protoc` descriptor messages.
|
|
327
324
|
|
|
328
|
-
|
|
325
|
+
#### ProtoJSON
|
|
329
326
|
|
|
330
|
-
|
|
327
|
+
Protocol Buffers support a special [ProtoJSON format](https://protobuf.dev/programming-guides/json/) to share data with systems that do not support the binary wire format, for example when implementing gateways. Spec-compliant ProtoJSON is supported via [ext/protojson](./ext/README.md#protojson).
|
|
328
|
+
|
|
329
|
+
#### Text Format
|
|
330
|
+
|
|
331
|
+
Protocol Buffers [Text Format](https://protobuf.dev/reference/protobuf/textformat-spec/) is a special syntax for representing protobuf data in text form, which can be useful for configurations or tests. Spec-compliant Text Format is supported via [ext/textformat](./ext/README.md#textformat).
|
|
332
|
+
|
|
333
|
+
## Conformance
|
|
331
334
|
|
|
332
|
-
|
|
335
|
+
protobuf.js targets complete binary wire-format conformance for **Proto2**, **Proto3** and **Editions** in both static and reflection modes, plus complete **ProtoJSON** and **Text Format** conformance with reflection metadata present. CI runs the official Protocol Buffers conformance suite for validation, with logs [uploaded as artifacts](https://github.com/protobufjs/protobuf.js/actions/workflows/test.yml?query=branch%3Amaster+event%3Apush).
|
|
333
336
|
|
|
334
|
-
|
|
|
335
|
-
|
|
|
336
|
-
|
|
|
337
|
-
|
|
|
338
|
-
|
|
|
337
|
+
| Category | Total | Required | Recommended |
|
|
338
|
+
| ---------- | ------------------: | ------------------: | ------------------: |
|
|
339
|
+
| Binary | 100.00% (2805/2805) | 100.00% (1939/1939) | 100.00% (866/866) |
|
|
340
|
+
| ↳ Proto2 | 100.00% (703/703) | 100.00% (485/485) | 100.00% (218/218) |
|
|
341
|
+
| ↳ Proto3 | 100.00% (698/698) | 100.00% (482/482) | 100.00% (216/216) |
|
|
342
|
+
| ↳ Editions | 100.00% (1404/1404) | 100.00% (972/972) | 100.00% (432/432) |
|
|
343
|
+
| ProtoJSON | 100.00% (2762/2762) | 100.00% (2328/2328) | 100.00% (434/434) |
|
|
344
|
+
| TextFormat | 100.00% (883/883) | 100.00% (819/819) | 100.00% (64/64) |
|
|
345
|
+
| Overall | 100.00% (6450/6450) | 100.00% (5086/5086) | 100.00% (1364/1364) |
|
|
339
346
|
|
|
340
347
|
## Performance
|
|
341
348
|
|
|
342
|
-
In both reflection and
|
|
349
|
+
In both reflection and reflection-free modes, protobuf.js builds specialized encoders and decoders instead of interpreting descriptors at runtime.
|
|
343
350
|
|
|
344
351
|
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.
|
|
345
352
|
|