msgpackr 1.7.2 → 1.8.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 +23 -5
- package/dist/index-no-eval.cjs +2181 -0
- package/dist/index-no-eval.cjs.map +1 -0
- package/dist/index-no-eval.min.js +2 -0
- package/dist/index-no-eval.min.js.map +1 -0
- package/dist/index.js +416 -380
- package/dist/index.js.map +1 -1
- package/dist/index.min.js +1 -84
- package/dist/index.min.js.map +1 -1
- package/dist/node.cjs +447 -422
- package/dist/node.cjs.map +1 -1
- package/dist/test.js +841 -508
- package/dist/test.js.map +1 -1
- package/dist/unpack-no-eval.cjs +1158 -0
- package/dist/unpack-no-eval.cjs.map +1 -0
- package/index.d.ts +2 -0
- package/pack.js +38 -21
- package/package.json +10 -7
- package/rollup.config.js +44 -5
- package/struct.js +3 -3
- package/unpack.js +15 -4
package/README.md
CHANGED
|
@@ -69,6 +69,9 @@ For module-based development, it is recommended that you directly import the mod
|
|
|
69
69
|
import { unpack } from 'msgpackr/unpack' // if you only need to unpack
|
|
70
70
|
```
|
|
71
71
|
|
|
72
|
+
The package also includes a minified bundle in index.min.js.
|
|
73
|
+
Additionally, the package includes a version that excludes dynamic code evaluation called index-no-eval.js, for situations where Content Security Policy (CSP) forbids eval/Function in code. The dynamic evaluation provides important performance optimizations (for records), so is not recommended unless required by CSP policy.
|
|
74
|
+
|
|
72
75
|
## Structured Cloning
|
|
73
76
|
You can also use msgpackr for [structured cloning](https://html.spec.whatwg.org/multipage/structured-data.html). By enabling the `structuredClone` option, you can include references to other objects or cyclic references, and object identity will be preserved. Structured cloning also enables preserving certain typed objects like `Error`, `Set`, `RegExp` and TypedArray instances. For example:
|
|
74
77
|
```js
|
|
@@ -170,7 +173,7 @@ The following options properties can be provided to the Packr or Unpackr constru
|
|
|
170
173
|
* `sequential` - Encode structures in serialized data, and reference previously encoded structures with expectation that decoder will read the encoded structures in the same order as encoded, with `unpackMultiple`.
|
|
171
174
|
* `largeBigIntToFloat` - If a bigint needs to be encoded that is larger than will fit in 64-bit integers, it will be encoded as a float-64 (otherwise will throw a RangeError).
|
|
172
175
|
* `encodeUndefinedAsNil` - Encodes a value of `undefined` as a MessagePack `nil`, the same as a `null`.
|
|
173
|
-
* `
|
|
176
|
+
* `int64AsType` - This will decode uint64 and int64 numbers as the specified type. The type can be `bigint` (default), `number`, or `string`.
|
|
174
177
|
* `onInvalidDate` - This can be provided as function that will be called when an invalid date is provided. The function can throw an error, or return a value that will be encoded in place of the invalid date. If not provided, an invalid date will be encoded as an invalid timestamp (which decodes with msgpackr back to an invalid date).
|
|
175
178
|
|
|
176
179
|
### 32-bit Float Options
|
|
@@ -261,7 +264,8 @@ addExtension({
|
|
|
261
264
|
}
|
|
262
265
|
});
|
|
263
266
|
```
|
|
264
|
-
If you want to use msgpackr to encode and decode the data within your extensions, you can use the `read` and `write` functions and read and write data/objects that will be encoded and decoded by msgpackr, which can be easier and faster than creating and receiving separate buffers
|
|
267
|
+
If you want to use msgpackr to encode and decode the data within your extensions, you can use the `read` and `write` functions and read and write data/objects that will be encoded and decoded by msgpackr, which can be easier and faster than creating and receiving separate buffers:
|
|
268
|
+
|
|
265
269
|
```js
|
|
266
270
|
import { addExtension, Packr } from 'msgpackr';
|
|
267
271
|
|
|
@@ -284,6 +288,20 @@ addExtension({
|
|
|
284
288
|
}
|
|
285
289
|
});
|
|
286
290
|
```
|
|
291
|
+
Note that you can just return the same object from `write`, and in this case msgpackr will encode it using the default object/array encoding:
|
|
292
|
+
```js
|
|
293
|
+
addExtension({
|
|
294
|
+
Class: MyCustomClass,
|
|
295
|
+
type: 12,
|
|
296
|
+
read: function(data) {
|
|
297
|
+
Object.setPrototypeOf(data, MyCustomClass.prototype)
|
|
298
|
+
return data
|
|
299
|
+
},
|
|
300
|
+
write: function(data) {
|
|
301
|
+
return data
|
|
302
|
+
}
|
|
303
|
+
})
|
|
304
|
+
```
|
|
287
305
|
You can also create an extension with `Class` and `write` methods, but no `type` (or `read`), if you just want to customize how a class is serialized without using MessagePack extension encoding.
|
|
288
306
|
|
|
289
307
|
### Additional Performance Optimizations
|
|
@@ -300,9 +318,9 @@ The record struction extension uses extension id 0x72 ("r") to declare the use o
|
|
|
300
318
|
|
|
301
319
|
Once a record identifier and record field names have been defined, the parser/decoder should proceed to read the next value. Any subsequent use of the record identifier as a value in the block or stream should parsed as a record instance, and the next n values, where is n is the number of fields (as defined in the array of field names), should be read as the values of the fields. For example, here we have defined a structure with fields "foo" and "bar", with the record identifier 0x40, and then read a record instance that defines the field values of 4 and 2, respectively:
|
|
302
320
|
```
|
|
303
|
-
|
|
304
|
-
| 0xd4 | 0x72 | 0x40 | array: [ "foo", "bar" ] |
|
|
305
|
-
|
|
321
|
+
+--------+--------+--------+~~~~~~~~~~~~~~~~~~~~~~~~~+--------+--------+
|
|
322
|
+
| 0xd4 | 0x72 | 0x40 | array: [ "foo", "bar" ] | 0x04 | 0x02 |
|
|
323
|
+
+--------+--------+--------+~~~~~~~~~~~~~~~~~~~~~~~~~+--------+--------+
|
|
306
324
|
```
|
|
307
325
|
Which should generate an object that would correspond to JSON:
|
|
308
326
|
```js
|