protobufjs 8.7.1 → 8.7.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 +61 -84
- package/dist/light/protobuf.js +66 -45
- 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 +47 -40
- 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 +92 -68
- 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 +26 -10
- package/index.d.ts +3 -0
- package/package.json +1 -1
- package/src/converter.js +5 -1
- package/src/enum.js +5 -2
- package/src/field.js +6 -1
- package/src/parse.js +26 -23
- package/src/reader.js +36 -9
- package/src/util/codegen.js +3 -1
- package/src/util/utf8.js +2 -28
- 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
|
|
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.
|
|
190
173
|
|
|
191
|
-
Reflection
|
|
174
|
+
### Reflection modules
|
|
192
175
|
|
|
193
|
-
|
|
194
|
-
npx pbjs -t json -o awesome.json awesome1.proto awesome2.proto ...
|
|
195
|
-
```
|
|
196
|
-
|
|
197
|
-
```ts
|
|
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
|
|
|
@@ -367,37 +322,59 @@ protobuf.js is validated against the official Protocol Buffers conformance suite
|
|
|
367
322
|
|
|
368
323
|
<!-- END CONFORMANCE DATA -->
|
|
369
324
|
|
|
370
|
-
[Structured results](https://github.com/protobufjs/protobuf.js/actions/workflows/test.yml?query=branch%3Amaster+event%3Apush) are available as CI artifacts.
|
|
325
|
+
[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
326
|
|
|
372
327
|
## Performance
|
|
373
328
|
|
|
374
|
-
|
|
329
|
+
Both reflection and static modes use specialized encoders and decoders backed by the same hand-tuned reader and writer primitives.
|
|
375
330
|
|
|
376
|
-
The repository includes a [
|
|
331
|
+
The repository includes a [benchmark suite](./bench) you can run yourself. It compares protobuf.js with other general-purpose JavaScript implementations across three substantially different cases: our classic common message shape and two unmodified, structurally distinct fixtures sourced externally. The suite measures each library's recommended serialization and deserialization path using identical schemas and inputs, with JSON included as an additional baseline. Across these cases, protobuf.js is a clear upgrade over using JSON and consistently the fastest Protobuf implementation by a considerable margin.
|
|
377
332
|
|
|
378
333
|
<!-- BEGIN BENCHMARK DATA -->
|
|
379
334
|
|
|
380
|
-
|
|
335
|
+
<picture>
|
|
336
|
+
<source media="(prefers-color-scheme: dark)" srcset="./bench/results/encode-dark.svg">
|
|
337
|
+
<source media="(prefers-color-scheme: light)" srcset="./bench/results/encode-light.svg">
|
|
338
|
+
<img alt="Encode benchmark" src="./bench/results/encode.svg">
|
|
339
|
+
</picture>
|
|
340
|
+
|
|
341
|
+
<details>
|
|
342
|
+
<summary>Show table</summary>
|
|
343
|
+
|
|
344
|
+
| Implementation | Common | Vector tile | Buf perf |
|
|
345
|
+
| --- | ---: | ---: | ---: |
|
|
346
|
+
| protobuf.js static | **5.34M ops/s** <small>1.0x</small> | **3.09K ops/s** <small>1.0x</small> | 43.1K ops/s <small>1.0x</small> |
|
|
347
|
+
| protobuf.js reflect | 4.89M ops/s <small>1.1x</small> | 3.03K ops/s <small>1.0x</small> | **43.3K ops/s** <small>1.0x</small> |
|
|
348
|
+
| JSON | 2.09M ops/s <small>2.6x</small> | 897 ops/s <small>3.4x</small> | 6.70K ops/s <small>6.5x</small> |
|
|
349
|
+
| protoc-gen-js | 1.03M ops/s <small>5.2x</small> | 700 ops/s <small>4.4x</small> | 13.9K ops/s <small>3.1x</small> |
|
|
350
|
+
| protoc-gen-es | 402K ops/s <small>13.3x</small> | 235 ops/s <small>13.2x</small> | 8.22K ops/s <small>5.3x</small> |
|
|
351
|
+
|
|
352
|
+
</details>
|
|
353
|
+
|
|
354
|
+
<picture>
|
|
355
|
+
<source media="(prefers-color-scheme: dark)" srcset="./bench/results/decode-dark.svg">
|
|
356
|
+
<source media="(prefers-color-scheme: light)" srcset="./bench/results/decode-light.svg">
|
|
357
|
+
<img alt="Decode benchmark" src="./bench/results/decode.svg">
|
|
358
|
+
</picture>
|
|
381
359
|
|
|
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 |
|
|
360
|
+
<details>
|
|
361
|
+
<summary>Show table</summary>
|
|
387
362
|
|
|
388
|
-
|
|
363
|
+
| Implementation | Common | Vector tile | Buf perf |
|
|
364
|
+
| --- | ---: | ---: | ---: |
|
|
365
|
+
| protobuf.js static | 6.45M ops/s <small>1.1x</small> | 2.70K ops/s <small>1.1x</small> | **81.7K ops/s** <small>1.0x</small> |
|
|
366
|
+
| protobuf.js reflect | **6.93M ops/s** <small>1.0x</small> | **2.99K ops/s** <small>1.0x</small> | 80.3K ops/s <small>1.0x</small> |
|
|
367
|
+
| JSON | 1.37M ops/s <small>5.0x</small> | 1.06K ops/s <small>2.8x</small> | 19.5K ops/s <small>4.2x</small> |
|
|
368
|
+
| protoc-gen-js | 811K ops/s <small>8.5x</small> | 851 ops/s <small>3.5x</small> | 21.7K ops/s <small>3.8x</small> |
|
|
369
|
+
| protoc-gen-es | 691K ops/s <small>10.0x</small> | 376 ops/s <small>8.0x</small> | 14.3K ops/s <small>5.7x</small> |
|
|
389
370
|
|
|
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 |
|
|
371
|
+
</details>
|
|
395
372
|
|
|
396
373
|
<!-- END BENCHMARK DATA -->
|
|
397
374
|
|
|
398
|
-
[Structured results](./bench/results/latest.json)
|
|
375
|
+
[Structured results](./bench/results/latest.json) and environment details for this run are also available as committed artifacts.
|
|
399
376
|
|
|
400
|
-
To run the benchmark
|
|
377
|
+
To run the benchmark on your own hardware:
|
|
401
378
|
|
|
402
379
|
```sh
|
|
403
380
|
npm --prefix bench install
|
package/dist/light/protobuf.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* protobuf.js v8.7.
|
|
3
|
-
* compiled
|
|
2
|
+
* protobuf.js v8.7.2 (c) 2016, daniel wirtz
|
|
3
|
+
* compiled sat, 08 aug 2026 13:15:56 utc
|
|
4
4
|
* licensed under the bsd-3-clause license
|
|
5
5
|
* see: https://github.com/dcodeio/protobuf.js for details
|
|
6
6
|
*/
|
|
@@ -345,7 +345,11 @@ converter.toObject = function toObject(mtype) {
|
|
|
345
345
|
("d%s=%j", prop, arrayDefault)
|
|
346
346
|
("if(o.bytes!==Array)d%s=util.newBuffer(d%s)", prop, prop)
|
|
347
347
|
("}");
|
|
348
|
-
} else
|
|
348
|
+
} else if ((field.type === "double" || field.type === "float") && typeof field.typeDefault === "number"
|
|
349
|
+
&& (!isFinite(field.typeDefault) || Object.is(field.typeDefault, -0))) gen
|
|
350
|
+
("d%s=%f", prop, field.typeDefault)
|
|
351
|
+
("if(o.json&&!isFinite(d%s))d%s=String(d%s)", prop, prop, prop);
|
|
352
|
+
else gen
|
|
349
353
|
("d%s=%j", prop, field.typeDefault); // also messages (=null)
|
|
350
354
|
} gen
|
|
351
355
|
("}");
|
|
@@ -884,8 +888,11 @@ function Enum(name, values, options, comment, comments, valuesOptions) {
|
|
|
884
888
|
|
|
885
889
|
if (values)
|
|
886
890
|
for (var keys = Object.keys(values), i = 0; i < keys.length; ++i)
|
|
887
|
-
if (keys[i] !== "__proto__" && typeof values[keys[i]] === "number") // use forward entries only
|
|
888
|
-
this.
|
|
891
|
+
if (keys[i] !== "__proto__" && typeof values[keys[i]] === "number") { // use forward entries only
|
|
892
|
+
this.values[keys[i]] = values[keys[i]];
|
|
893
|
+
if (this.valuesById[values[keys[i]]] === undefined)
|
|
894
|
+
this.valuesById[values[keys[i]]] = keys[i];
|
|
895
|
+
}
|
|
889
896
|
}
|
|
890
897
|
|
|
891
898
|
/**
|
|
@@ -1414,12 +1421,17 @@ Field.prototype.resolve = function resolve() {
|
|
|
1414
1421
|
|
|
1415
1422
|
// convert to internal data type if necesssary
|
|
1416
1423
|
if (this.long) {
|
|
1417
|
-
|
|
1424
|
+
var unsigned = this.type === "uint64" || this.type === "fixed64";
|
|
1425
|
+
this.typeDefault = typeof this.typeDefault === "string"
|
|
1426
|
+
? util.Long.fromString(this.typeDefault, unsigned)
|
|
1427
|
+
: util.Long.fromNumber(this.typeDefault, unsigned);
|
|
1418
1428
|
|
|
1419
1429
|
/* istanbul ignore else */
|
|
1420
1430
|
if (Object.freeze)
|
|
1421
1431
|
Object.freeze(this.typeDefault); // long instances are meant to be immutable anyway (i.e. use small int cache that even requires it)
|
|
1422
1432
|
|
|
1433
|
+
} else if (types.long[this.type] !== undefined && typeof this.typeDefault === "string") {
|
|
1434
|
+
this.typeDefault = parseInt(this.typeDefault, 10);
|
|
1423
1435
|
} else if (this.bytes && typeof this.typeDefault === "string") {
|
|
1424
1436
|
var buf;
|
|
1425
1437
|
if (util.base64.test(this.typeDefault))
|
|
@@ -3763,9 +3775,18 @@ Reader.prototype.double = function read_double() {
|
|
|
3763
3775
|
*/
|
|
3764
3776
|
Reader.prototype.uint32s = function read_uint32s(array) {
|
|
3765
3777
|
if (array === undefined) array = [];
|
|
3766
|
-
var end = this.uint32() + this.pos;
|
|
3767
|
-
while (
|
|
3768
|
-
|
|
3778
|
+
var end = this.uint32() + this.pos, buf = this.buf, pos = this.pos, value;
|
|
3779
|
+
while (pos < end) {
|
|
3780
|
+
value = buf[pos++];
|
|
3781
|
+
if (value < 128)
|
|
3782
|
+
array.push(value);
|
|
3783
|
+
else {
|
|
3784
|
+
this.pos = pos - 1;
|
|
3785
|
+
array.push(this.uint32());
|
|
3786
|
+
pos = this.pos;
|
|
3787
|
+
}
|
|
3788
|
+
}
|
|
3789
|
+
this.pos = pos;
|
|
3769
3790
|
return array;
|
|
3770
3791
|
};
|
|
3771
3792
|
|
|
@@ -3776,9 +3797,18 @@ Reader.prototype.uint32s = function read_uint32s(array) {
|
|
|
3776
3797
|
*/
|
|
3777
3798
|
Reader.prototype.int32s = function read_int32s(array) {
|
|
3778
3799
|
if (array === undefined) array = [];
|
|
3779
|
-
var end = this.uint32() + this.pos;
|
|
3780
|
-
while (
|
|
3781
|
-
|
|
3800
|
+
var end = this.uint32() + this.pos, buf = this.buf, pos = this.pos, value;
|
|
3801
|
+
while (pos < end) {
|
|
3802
|
+
value = buf[pos++];
|
|
3803
|
+
if (value < 128)
|
|
3804
|
+
array.push(value);
|
|
3805
|
+
else {
|
|
3806
|
+
this.pos = pos - 1;
|
|
3807
|
+
array.push(this.int32());
|
|
3808
|
+
pos = this.pos;
|
|
3809
|
+
}
|
|
3810
|
+
}
|
|
3811
|
+
this.pos = pos;
|
|
3782
3812
|
return array;
|
|
3783
3813
|
};
|
|
3784
3814
|
|
|
@@ -3802,9 +3832,18 @@ Reader.prototype.sint32s = function read_sint32s(array) {
|
|
|
3802
3832
|
*/
|
|
3803
3833
|
Reader.prototype.bools = function read_bools(array) {
|
|
3804
3834
|
if (array === undefined) array = [];
|
|
3805
|
-
var end = this.uint32() + this.pos;
|
|
3806
|
-
while (
|
|
3807
|
-
|
|
3835
|
+
var end = this.uint32() + this.pos, buf = this.buf, pos = this.pos, value;
|
|
3836
|
+
while (pos < end) {
|
|
3837
|
+
value = buf[pos++];
|
|
3838
|
+
if (value < 128)
|
|
3839
|
+
array.push(value !== 0);
|
|
3840
|
+
else {
|
|
3841
|
+
this.pos = pos - 1;
|
|
3842
|
+
array.push(this.bool());
|
|
3843
|
+
pos = this.pos;
|
|
3844
|
+
}
|
|
3845
|
+
}
|
|
3846
|
+
this.pos = pos;
|
|
3808
3847
|
return array;
|
|
3809
3848
|
};
|
|
3810
3849
|
|
|
@@ -6494,7 +6533,9 @@ function codegen(functionParams, functionName) {
|
|
|
6494
6533
|
formatStringOrScope = formatStringOrScope.replace(/%([%dfijs])/g, function replace($0, $1) {
|
|
6495
6534
|
var value = formatParams[formatOffset++];
|
|
6496
6535
|
switch ($1) {
|
|
6497
|
-
case "d": case "f":
|
|
6536
|
+
case "d": case "f":
|
|
6537
|
+
value = Number(value);
|
|
6538
|
+
return Object.is(value, -0) ? "-0" : String(value);
|
|
6498
6539
|
case "i": return String(Math.floor(value));
|
|
6499
6540
|
case "j": return JSON.stringify(value);
|
|
6500
6541
|
case "s": return String(value);
|
|
@@ -7988,7 +8029,6 @@ function pool(alloc, slice, size) {
|
|
|
7988
8029
|
* @namespace
|
|
7989
8030
|
*/
|
|
7990
8031
|
var utf8 = exports,
|
|
7991
|
-
replacementChar = "\ufffd",
|
|
7992
8032
|
looseDecoder = new TextDecoder("utf-8", { ignoreBOM: true }),
|
|
7993
8033
|
strictDecoder;
|
|
7994
8034
|
var TEXT_DECODER_MIN_LENGTH = 64;
|
|
@@ -8023,31 +8063,6 @@ utf8.length = function utf8_length(string) {
|
|
|
8023
8063
|
return len;
|
|
8024
8064
|
};
|
|
8025
8065
|
|
|
8026
|
-
function utf8_read_js(buffer, start, end, str) {
|
|
8027
|
-
for (var i = start; i < end;) {
|
|
8028
|
-
var t = buffer[i++];
|
|
8029
|
-
if (t <= 0x7F) {
|
|
8030
|
-
str += String.fromCharCode(t);
|
|
8031
|
-
} else if (t >= 0xC0 && t < 0xE0) {
|
|
8032
|
-
var c2 = (t & 0x1F) << 6 | buffer[i++] & 0x3F;
|
|
8033
|
-
str += c2 >= 0x80 ? String.fromCharCode(c2) : replacementChar;
|
|
8034
|
-
} else if (t >= 0xE0 && t < 0xF0) {
|
|
8035
|
-
var c3 = (t & 0xF) << 12 | (buffer[i++] & 0x3F) << 6 | buffer[i++] & 0x3F;
|
|
8036
|
-
str += c3 >= 0x800 ? String.fromCharCode(c3) : replacementChar;
|
|
8037
|
-
} else if (t >= 0xF0) {
|
|
8038
|
-
var t2 = (t & 7) << 18 | (buffer[i++] & 0x3F) << 12 | (buffer[i++] & 0x3F) << 6 | buffer[i++] & 0x3F;
|
|
8039
|
-
if (t2 < 0x10000 || t2 > 0x10FFFF)
|
|
8040
|
-
str += replacementChar;
|
|
8041
|
-
else {
|
|
8042
|
-
t2 -= 0x10000;
|
|
8043
|
-
str += String.fromCharCode(0xD800 + (t2 >> 10));
|
|
8044
|
-
str += String.fromCharCode(0xDC00 + (t2 & 0x3FF));
|
|
8045
|
-
}
|
|
8046
|
-
}
|
|
8047
|
-
}
|
|
8048
|
-
return str;
|
|
8049
|
-
}
|
|
8050
|
-
|
|
8051
8066
|
function utf8_read_decoder(decoder, buffer, start, end) {
|
|
8052
8067
|
var source = start === 0 && end === buffer.length
|
|
8053
8068
|
? buffer
|
|
@@ -8082,14 +8097,14 @@ utf8.read = function utf8_read_loose(buffer, start, end) {
|
|
|
8082
8097
|
c7 = buffer[i + 6];
|
|
8083
8098
|
c8 = buffer[i + 7];
|
|
8084
8099
|
if ((c1 | c2 | c3 | c4 | c5 | c6 | c7 | c8) & 0x80)
|
|
8085
|
-
return
|
|
8100
|
+
return str + utf8_read_decoder(looseDecoder, buffer, i, end);
|
|
8086
8101
|
str += String.fromCharCode(c1, c2, c3, c4, c5, c6, c7, c8);
|
|
8087
8102
|
}
|
|
8088
8103
|
|
|
8089
8104
|
for (; i < end; ++i) {
|
|
8090
8105
|
c1 = buffer[i];
|
|
8091
8106
|
if (c1 & 0x80)
|
|
8092
|
-
return
|
|
8107
|
+
return str + utf8_read_decoder(looseDecoder, buffer, i, end);
|
|
8093
8108
|
str += String.fromCharCode(c1);
|
|
8094
8109
|
}
|
|
8095
8110
|
|
|
@@ -8495,7 +8510,7 @@ function Writer() {
|
|
|
8495
8510
|
* Backing buffer.
|
|
8496
8511
|
* @type {Uint8Array}
|
|
8497
8512
|
*/
|
|
8498
|
-
this.buf = this.constructor.alloc(
|
|
8513
|
+
this.buf = this.constructor.alloc(Writer.initialBufferSize);
|
|
8499
8514
|
|
|
8500
8515
|
/**
|
|
8501
8516
|
* Cached DataView over {@link Writer#buf}.
|
|
@@ -8510,6 +8525,12 @@ function Writer() {
|
|
|
8510
8525
|
this.states = null;
|
|
8511
8526
|
}
|
|
8512
8527
|
|
|
8528
|
+
/**
|
|
8529
|
+
* Initial backing buffer size in bytes. Defaults to 128.
|
|
8530
|
+
* @type {number}
|
|
8531
|
+
*/
|
|
8532
|
+
Writer.initialBufferSize = 128;
|
|
8533
|
+
|
|
8513
8534
|
/**
|
|
8514
8535
|
* Current write position.
|
|
8515
8536
|
* @name Writer#len
|