protobufjs 8.4.0 → 8.4.2
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 +45 -31
- package/dist/light/protobuf.js +112 -78
- 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 +41 -19
- 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 +121 -84
- 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.js +31 -9
- package/index.d.ts +20 -8
- package/package.json +1 -1
- package/src/converter.js +9 -6
- 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/namespace.js +1 -1
- package/src/object.js +6 -6
- package/src/parse.js +9 -6
- package/src/root.js +14 -8
- package/src/type.js +8 -8
- 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.js +2 -3
- package/src/wrappers.js +11 -8
- package/src/writer.js +11 -9
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, {
|
|
@@ -130,18 +140,6 @@ Common `ConversionOptions` are:
|
|
|
130
140
|
|
|
131
141
|
Message types expose focused methods for validation, conversion, and binary I/O.
|
|
132
142
|
|
|
133
|
-
* **verify**(object: `object`): `null | string`
|
|
134
|
-
Checks whether a plain object can be encoded as-is. Returns `null` if valid, otherwise an error message.
|
|
135
|
-
|
|
136
|
-
* **create**(properties?: `object`): `Message`
|
|
137
|
-
Creates a message instance from already valid data.
|
|
138
|
-
|
|
139
|
-
* **fromObject**(object: `object`): `Message`
|
|
140
|
-
Converts broader JavaScript input into a message instance.
|
|
141
|
-
|
|
142
|
-
* **toObject**(message: `Message`, options?: `ConversionOptions`): `object`
|
|
143
|
-
Converts a message instance to a plain object for JSON or interoperability.
|
|
144
|
-
|
|
145
143
|
* **encode**(message: `Message | object`, writer?: `Writer`): `Writer`
|
|
146
144
|
Encodes a message or equivalent plain object. Call `.finish()` on the returned writer to obtain a buffer.
|
|
147
145
|
|
|
@@ -154,6 +152,18 @@ Message types expose focused methods for validation, conversion, and binary I/O.
|
|
|
154
152
|
* **decodeDelimited**(reader: `Reader | Uint8Array`): `Message`
|
|
155
153
|
Decodes a length-delimited message.
|
|
156
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
|
+
|
|
157
167
|
* **message#toJSON**(): `object`
|
|
158
168
|
Converts a message instance to JSON-compatible output using default conversion options.
|
|
159
169
|
|
|
@@ -163,22 +173,21 @@ If required fields are missing while decoding proto2 data, `decode` throws `prot
|
|
|
163
173
|
|
|
164
174
|
## Code generation
|
|
165
175
|
|
|
166
|
-
|
|
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`.
|
|
167
177
|
|
|
168
|
-
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.
|
|
169
179
|
|
|
170
180
|
| Target | Output | Minimum Runtime |
|
|
171
181
|
|--------|--------|-----------------|
|
|
172
182
|
| `json` | JSON bundle | `protobufjs/light.js` |
|
|
173
183
|
| `json-module` | JSON bundle module | `protobufjs/light.js` |
|
|
174
|
-
| `static` | Static code | custom wrapper/integration, not standalone |
|
|
175
184
|
| `static-module` | Static code module | `protobufjs/minimal.js` |
|
|
176
185
|
|
|
177
|
-
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.
|
|
178
187
|
|
|
179
188
|
### Static modules
|
|
180
189
|
|
|
181
|
-
Static modules
|
|
190
|
+
Static modules emit dedicated JavaScript for your schema, so they only need `protobufjs/minimal.js` at runtime.
|
|
182
191
|
|
|
183
192
|
```sh
|
|
184
193
|
npx pbjs -t static-module -w esm -o awesome.js --dts awesome.proto
|
|
@@ -219,7 +228,7 @@ JSON modules export the reflection root and, with `-w esm`, also provide top-lev
|
|
|
219
228
|
|
|
220
229
|
### TypeScript integration
|
|
221
230
|
|
|
222
|
-
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.
|
|
223
232
|
|
|
224
233
|
For example, given the oneof:
|
|
225
234
|
|
|
@@ -245,7 +254,7 @@ if (profile.contact === "email") {
|
|
|
245
254
|
}
|
|
246
255
|
|
|
247
256
|
const decoded = Profile.decode(bytes);
|
|
248
|
-
if (decoded.
|
|
257
|
+
if (decoded.contact === "phone") {
|
|
249
258
|
decoded.phone; // string
|
|
250
259
|
}
|
|
251
260
|
```
|
|
@@ -310,7 +319,7 @@ For `google/protobuf/descriptor.proto` interoperability, see [ext/descriptor](./
|
|
|
310
319
|
|
|
311
320
|
### Text format
|
|
312
321
|
|
|
313
|
-
Protocol Buffers
|
|
322
|
+
Protocol Buffers Text Format is supported via [ext/textformat](./ext/README.md#textformat) and exercised by the conformance suite.
|
|
314
323
|
|
|
315
324
|
### Content Security Policy
|
|
316
325
|
|
|
@@ -318,7 +327,9 @@ In [CSP](https://w3c.github.io/webappsec-csp/)-restricted environments that disa
|
|
|
318
327
|
|
|
319
328
|
## Conformance
|
|
320
329
|
|
|
321
|
-
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:
|
|
322
333
|
|
|
323
334
|
| Syntax | Total | Required | Recommended |
|
|
324
335
|
| -------- | ------------------: | ----------------: | ----------------: |
|
|
@@ -332,7 +343,8 @@ In both reflection and static modes, protobuf.js builds specialized encoders and
|
|
|
332
343
|
|
|
333
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.
|
|
334
345
|
|
|
335
|
-
|
|
346
|
+
<details>
|
|
347
|
+
<summary>Benchmark run on AMD Ryzen 9 9950X3D with Node.js 24.15.0</summary>
|
|
336
348
|
|
|
337
349
|
```
|
|
338
350
|
benchmarking encode performance ...
|
|
@@ -377,6 +389,7 @@ protoc-gen-es x 254,044 ops/sec ±0.05% (101 runs sampled)
|
|
|
377
389
|
protoc-gen-js was 63.9% ops/sec slower (factor 2.8)
|
|
378
390
|
protoc-gen-es was 80.6% ops/sec slower (factor 5.2)
|
|
379
391
|
```
|
|
392
|
+
</details>
|
|
380
393
|
|
|
381
394
|
Run it locally with:
|
|
382
395
|
|
|
@@ -391,7 +404,7 @@ Supported runtimes are browsers, Node.js v12+, Deno and Bun. When using the CLI
|
|
|
391
404
|
|
|
392
405
|
## Security
|
|
393
406
|
|
|
394
|
-
|
|
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.
|
|
395
408
|
|
|
396
409
|
## Development
|
|
397
410
|
|
|
@@ -399,6 +412,7 @@ protobuf.js favors transparent disclosure. Security-impacting reports are handle
|
|
|
399
412
|
git clone https://github.com/protobufjs/protobuf.js
|
|
400
413
|
cd protobuf.js
|
|
401
414
|
npm install
|
|
415
|
+
npm --prefix cli install
|
|
402
416
|
```
|
|
403
417
|
|
|
404
418
|
Running the tests:
|