protobufjs 8.3.0 → 8.4.1
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 +46 -31
- package/dist/light/protobuf.js +105 -71
- 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 +31 -11
- 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 +106 -72
- package/dist/protobuf.js.map +1 -1
- package/dist/protobuf.min.js +3 -3
- package/dist/protobuf.min.js.map +1 -1
- package/index.d.ts +21 -9
- package/package.json +2 -2
- package/src/converter.js +14 -9
- package/src/decoder.js +3 -1
- package/src/encoder.js +8 -5
- package/src/enum.js +2 -2
- package/src/field.js +2 -5
- package/src/index-light.js +1 -1
- package/src/message.js +1 -1
- package/src/object.js +6 -6
- package/src/parse.js +1 -1
- package/src/root.js +14 -8
- package/src/type.js +9 -9
- package/src/typescript.js +6 -0
- package/src/util/eventemitter.js +4 -2
- package/src/util/minimal.js +24 -6
- package/src/util/patterns.js +0 -1
- package/src/util/pool.js +1 -1
- package/src/util.js +2 -3
- package/src/wrappers.js +11 -8
package/README.md
CHANGED
|
@@ -9,7 +9,13 @@
|
|
|
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
|
|
12
|
+
**protobuf.js** is a standalone JavaScript implementation of Protocol Buffers for Node.js and the browser. It works with `.proto` files out of the box, is optimized for fast binary I/O, and supports runtime reflection as well as reflection-free static code generation with strong TypeScript declarations.
|
|
13
|
+
|
|
14
|
+
## About
|
|
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.
|
|
13
19
|
|
|
14
20
|
## Getting started
|
|
15
21
|
|
|
@@ -19,21 +25,23 @@
|
|
|
19
25
|
npm install protobufjs
|
|
20
26
|
```
|
|
21
27
|
|
|
22
|
-
The [command line utility](./cli
|
|
28
|
+
The [command line utility](./cli/#readme) for generating reflection bundles, static code and TypeScript declarations is published as an add-on package:
|
|
23
29
|
|
|
24
30
|
```sh
|
|
25
31
|
npm install --save-dev protobufjs-cli
|
|
26
32
|
```
|
|
27
33
|
|
|
28
|
-
The CLI is a small but capable standalone protobuf.js toolchain. It does not require `protoc`
|
|
34
|
+
The CLI is a small but capable standalone protobuf.js toolchain. It does not require `protoc`, but also provides `protoc-gen-pbjs` for standard `protoc` plugin workflows.
|
|
29
35
|
|
|
30
36
|
### Choose a runtime
|
|
31
37
|
|
|
38
|
+
Pick the smallest runtime variant that supports how your application loads schemas and whether it needs reflection at runtime.
|
|
39
|
+
|
|
32
40
|
| Import | Includes | Use when
|
|
33
41
|
| ----------------------- | ------------------ | --------
|
|
34
42
|
| `protobufjs` | Reflection, Parser | You load `.proto` files at runtime
|
|
35
43
|
| `protobufjs/light.js` | Reflection | You load JSON bundles or build schemas programmatically
|
|
36
|
-
| `protobufjs/minimal.js` | Static runtime | You
|
|
44
|
+
| `protobufjs/minimal.js` | Static runtime | You use generated static code
|
|
37
45
|
|
|
38
46
|
The full build includes the light build, and the light build includes the minimal runtime.
|
|
39
47
|
|
|
@@ -73,11 +81,11 @@ protobuf.js converts `.proto` field names to camelCase by default, so `awesome_f
|
|
|
73
81
|
```ts
|
|
74
82
|
const protobuf = require("protobufjs");
|
|
75
83
|
|
|
76
|
-
const root = protobuf.
|
|
84
|
+
const root = await protobuf.load("awesome.proto");
|
|
77
85
|
const AwesomeMessage = root.lookupType("awesomepackage.AwesomeMessage");
|
|
78
86
|
```
|
|
79
87
|
|
|
80
|
-
|
|
88
|
+
Optionally use `load()` with a callback, or `loadSync()` for synchronous loading on Node.js.
|
|
81
89
|
|
|
82
90
|
### Encode and decode
|
|
83
91
|
|
|
@@ -95,14 +103,16 @@ const encoded = AwesomeMessage.encode(message).finish();
|
|
|
95
103
|
const decoded = AwesomeMessage.decode(encoded);
|
|
96
104
|
```
|
|
97
105
|
|
|
98
|
-
`encode` expects a message instance or equivalent plain object and does not verify input implicitly. Use `verify` for plain objects whose shape is not guaranteed, `create` to create a message instance from already valid data when useful, and `fromObject` when conversion from broader JavaScript
|
|
106
|
+
`encode` expects a message instance or equivalent plain object and does not verify input implicitly. Use `verify` for plain objects whose shape is not guaranteed, `create` to create a message instance from already valid data when useful, and `fromObject` when conversion from broader JavaScript objects is needed.
|
|
99
107
|
|
|
100
|
-
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/`Long` keys.
|
|
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/`Long` keys.
|
|
101
109
|
|
|
102
110
|
Install [`long`](https://github.com/dcodeIO/long.js) with protobuf.js when exact 64-bit integer support is required.
|
|
103
111
|
|
|
104
112
|
### Convert plain objects
|
|
105
113
|
|
|
114
|
+
Conversion is an explicit interoperability boundary. `fromObject` accepts common JavaScript inputs such as enum values by name, base64 bytes, decimal 64-bit strings, `Long`, and `BigInt`; `toObject` lets callers choose the output expected by their application or transport.
|
|
115
|
+
|
|
106
116
|
```ts
|
|
107
117
|
const message = AwesomeMessage.fromObject({ awesomeField: 42 });
|
|
108
118
|
const object = AwesomeMessage.toObject(message, {
|
|
@@ -116,6 +126,7 @@ Common `ConversionOptions` are:
|
|
|
116
126
|
|
|
117
127
|
| Option | Effect |
|
|
118
128
|
|--------|--------|
|
|
129
|
+
| `longs: BigInt` | Converts 64-bit values to bigint values |
|
|
119
130
|
| `longs: String` | Converts 64-bit values to decimal strings |
|
|
120
131
|
| `longs: Number` | Converts 64-bit values to JS numbers (may lose precision) |
|
|
121
132
|
| `enums: String` | Converts enum values to names |
|
|
@@ -129,18 +140,6 @@ Common `ConversionOptions` are:
|
|
|
129
140
|
|
|
130
141
|
Message types expose focused methods for validation, conversion, and binary I/O.
|
|
131
142
|
|
|
132
|
-
* **verify**(object: `object`): `null | string`
|
|
133
|
-
Checks whether a plain object can be encoded as-is. Returns `null` if valid, otherwise an error message.
|
|
134
|
-
|
|
135
|
-
* **create**(properties?: `object`): `Message`
|
|
136
|
-
Creates a message instance from already valid data.
|
|
137
|
-
|
|
138
|
-
* **fromObject**(object: `object`): `Message`
|
|
139
|
-
Converts broader JavaScript input into a message instance.
|
|
140
|
-
|
|
141
|
-
* **toObject**(message: `Message`, options?: `ConversionOptions`): `object`
|
|
142
|
-
Converts a message instance to a plain object for JSON or interoperability.
|
|
143
|
-
|
|
144
143
|
* **encode**(message: `Message | object`, writer?: `Writer`): `Writer`
|
|
145
144
|
Encodes a message or equivalent plain object. Call `.finish()` on the returned writer to obtain a buffer.
|
|
146
145
|
|
|
@@ -153,6 +152,18 @@ Message types expose focused methods for validation, conversion, and binary I/O.
|
|
|
153
152
|
* **decodeDelimited**(reader: `Reader | Uint8Array`): `Message`
|
|
154
153
|
Decodes a length-delimited message.
|
|
155
154
|
|
|
155
|
+
* **create**(properties?: `object`): `Message`
|
|
156
|
+
Creates a message instance from already valid data.
|
|
157
|
+
|
|
158
|
+
* **verify**(object: `object`): `null | string`
|
|
159
|
+
Checks whether a plain object can be encoded as-is. Returns `null` if valid, otherwise an error message.
|
|
160
|
+
|
|
161
|
+
* **fromObject**(object: `object`): `Message`
|
|
162
|
+
Converts broader JavaScript input into a message instance.
|
|
163
|
+
|
|
164
|
+
* **toObject**(message: `Message`, options?: `ConversionOptions`): `object`
|
|
165
|
+
Converts a message instance to a configurable plain JavaScript object.
|
|
166
|
+
|
|
156
167
|
* **message#toJSON**(): `object`
|
|
157
168
|
Converts a message instance to JSON-compatible output using default conversion options.
|
|
158
169
|
|
|
@@ -162,22 +173,21 @@ If required fields are missing while decoding proto2 data, `decode` throws `prot
|
|
|
162
173
|
|
|
163
174
|
## Code generation
|
|
164
175
|
|
|
165
|
-
|
|
176
|
+
Choose the integration style that fits your workflow and use [`protobufjs-cli`](./cli/#readme) to generate reflection bundles, static JavaScript code, and matching TypeScript declarations, either standalone with `pbjs` or through its `protoc-gen-pbjs` plugin for `protoc`.
|
|
166
177
|
|
|
167
|
-
Reflection keeps schemas as
|
|
178
|
+
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.
|
|
168
179
|
|
|
169
180
|
| Target | Output | Minimum Runtime |
|
|
170
181
|
|--------|--------|-----------------|
|
|
171
182
|
| `json` | JSON bundle | `protobufjs/light.js` |
|
|
172
183
|
| `json-module` | JSON bundle module | `protobufjs/light.js` |
|
|
173
|
-
| `static` | Static code | custom wrapper/integration, not standalone |
|
|
174
184
|
| `static-module` | Static code module | `protobufjs/minimal.js` |
|
|
175
185
|
|
|
176
|
-
Module targets support `--wrap default` for CommonJS and AMD, plus `
|
|
186
|
+
Module targets support `--wrap default` for CommonJS and AMD, plus `esm`, `commonjs`, `amd`, and `closure`; `--wrap` can also load a custom wrapper module.
|
|
177
187
|
|
|
178
188
|
### Static modules
|
|
179
189
|
|
|
180
|
-
Static modules
|
|
190
|
+
Static modules emit dedicated JavaScript for your schema, so they only need `protobufjs/minimal.js` at runtime.
|
|
181
191
|
|
|
182
192
|
```sh
|
|
183
193
|
npx pbjs -t static-module -w esm -o awesome.js --dts awesome.proto
|
|
@@ -218,7 +228,7 @@ JSON modules export the reflection root and, with `-w esm`, also provide top-lev
|
|
|
218
228
|
|
|
219
229
|
### TypeScript integration
|
|
220
230
|
|
|
221
|
-
protobuf.js works with TypeScript out of the box: the runtime API is typed, and generated declarations
|
|
231
|
+
protobuf.js works with TypeScript out of the box: the runtime API is typed, and generated JavaScript can be paired with strong TypeScript declarations in the same CLI invocation. Generated output is directly usable from JavaScript without a transpile step, and strongly typed in TypeScript projects, with type-checked oneofs and JavaScript-friendly plain-object input.
|
|
222
232
|
|
|
223
233
|
For example, given the oneof:
|
|
224
234
|
|
|
@@ -244,7 +254,7 @@ if (profile.contact === "email") {
|
|
|
244
254
|
}
|
|
245
255
|
|
|
246
256
|
const decoded = Profile.decode(bytes);
|
|
247
|
-
if (decoded.
|
|
257
|
+
if (decoded.contact === "phone") {
|
|
248
258
|
decoded.phone; // string
|
|
249
259
|
}
|
|
250
260
|
```
|
|
@@ -309,7 +319,7 @@ For `google/protobuf/descriptor.proto` interoperability, see [ext/descriptor](./
|
|
|
309
319
|
|
|
310
320
|
### Text format
|
|
311
321
|
|
|
312
|
-
Protocol Buffers
|
|
322
|
+
Protocol Buffers Text Format is supported via [ext/textformat](./ext/README.md#textformat) and exercised by the conformance suite.
|
|
313
323
|
|
|
314
324
|
### Content Security Policy
|
|
315
325
|
|
|
@@ -317,7 +327,9 @@ In [CSP](https://w3c.github.io/webappsec-csp/)-restricted environments that disa
|
|
|
317
327
|
|
|
318
328
|
## Conformance
|
|
319
329
|
|
|
320
|
-
protobuf.js targets
|
|
330
|
+
protobuf.js targets complete binary wire-format conformance for **Proto2**, **Proto3** and **Editions**. CI runs the official Protocol Buffers conformance suite, with logs [uploaded as artifacts](https://github.com/protobufjs/protobuf.js/actions/workflows/test.yml?query=branch%3Amaster+event%3Apush).
|
|
331
|
+
|
|
332
|
+
Wire format by syntax:
|
|
321
333
|
|
|
322
334
|
| Syntax | Total | Required | Recommended |
|
|
323
335
|
| -------- | ------------------: | ----------------: | ----------------: |
|
|
@@ -331,7 +343,8 @@ In both reflection and static modes, protobuf.js builds specialized encoders and
|
|
|
331
343
|
|
|
332
344
|
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.
|
|
333
345
|
|
|
334
|
-
|
|
346
|
+
<details>
|
|
347
|
+
<summary>Benchmark run on AMD Ryzen 9 9950X3D with Node.js 24.15.0</summary>
|
|
335
348
|
|
|
336
349
|
```
|
|
337
350
|
benchmarking encode performance ...
|
|
@@ -376,6 +389,7 @@ protoc-gen-es x 254,044 ops/sec ±0.05% (101 runs sampled)
|
|
|
376
389
|
protoc-gen-js was 63.9% ops/sec slower (factor 2.8)
|
|
377
390
|
protoc-gen-es was 80.6% ops/sec slower (factor 5.2)
|
|
378
391
|
```
|
|
392
|
+
</details>
|
|
379
393
|
|
|
380
394
|
Run it locally with:
|
|
381
395
|
|
|
@@ -390,7 +404,7 @@ Supported runtimes are browsers, Node.js v12+, Deno and Bun. When using the CLI
|
|
|
390
404
|
|
|
391
405
|
## Security
|
|
392
406
|
|
|
393
|
-
|
|
407
|
+
Security-impacting reports are handled through coordinated GitHub Security Advisories where appropriate. See [SECURITY.md](./SECURITY.md) for supported release lines and reporting instructions.
|
|
394
408
|
|
|
395
409
|
## Development
|
|
396
410
|
|
|
@@ -398,6 +412,7 @@ protobuf.js favors transparent disclosure. Security-impacting reports are handle
|
|
|
398
412
|
git clone https://github.com/protobufjs/protobuf.js
|
|
399
413
|
cd protobuf.js
|
|
400
414
|
npm install
|
|
415
|
+
npm --prefix cli install
|
|
401
416
|
```
|
|
402
417
|
|
|
403
418
|
Running the tests:
|