cbor-x 1.3.3 → 1.4.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/LICENSE +21 -21
- package/README.md +347 -346
- package/SECURITY.md +11 -11
- package/benchmark.md +73 -73
- package/browser.js +11 -11
- package/decode.d.ts +2 -46
- package/decode.js +9 -7
- package/dist/index.js +1122 -1112
- package/dist/index.min.js +86 -76
- package/dist/node.cjs +1190 -1180
- package/dist/test.js +17 -1
- package/encode.d.ts +1 -6
- package/encode.js +1041 -1033
- package/index.d.ts +61 -12
- package/index.js +3 -3
- package/iterators.js +85 -85
- package/node-index.js +24 -24
- package/package.json +6 -1
- package/rollup.config.js +45 -45
- package/stream.js +57 -57
- package/webpack.config.js +19 -19
- package/dist/index.js.map +0 -1
- package/dist/node-index.js +0 -93
package/SECURITY.md
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
# Security Policy
|
|
2
|
-
|
|
3
|
-
## Supported Versions
|
|
4
|
-
|
|
5
|
-
| Version | Supported |
|
|
6
|
-
| ------- | ------------------ |
|
|
7
|
-
| 0.9.x | :white_check_mark: |
|
|
8
|
-
|
|
9
|
-
## Reporting a Vulnerability
|
|
10
|
-
|
|
11
|
-
Please report security vulnerabilities to kriszyp@gmail.com.
|
|
1
|
+
# Security Policy
|
|
2
|
+
|
|
3
|
+
## Supported Versions
|
|
4
|
+
|
|
5
|
+
| Version | Supported |
|
|
6
|
+
| ------- | ------------------ |
|
|
7
|
+
| 0.9.x | :white_check_mark: |
|
|
8
|
+
|
|
9
|
+
## Reporting a Vulnerability
|
|
10
|
+
|
|
11
|
+
Please report security vulnerabilities to kriszyp@gmail.com.
|
package/benchmark.md
CHANGED
|
@@ -1,73 +1,73 @@
|
|
|
1
|
-
Here are more comprehensive benchmarks. This is comparison with the next fastest JS projects using the benchmark tool from `msgpack-lite` (and data is from some clinical research data we use that has a good mix of different value types and structures). It also includes comparison to V8 native JSON functionality, and JavaScript Avro (`avsc`, a very optimized Avro implementation):
|
|
2
|
-
|
|
3
|
-
operation | op | ms | op/s
|
|
4
|
-
---------------------------------------------------------- | ------: | ----: | -----:
|
|
5
|
-
buf = Buffer(JSON.stringify(obj)); | 82000 | 5004 | 16386
|
|
6
|
-
obj = JSON.parse(buf); | 88600 | 5000 | 17720
|
|
7
|
-
require("cbor-x").encode(obj); | 161500 | 5002 | 32287
|
|
8
|
-
require("cbor-x").decode(buf); | 94600 | 5004 | 18904
|
|
9
|
-
cbor-x w/ shared structures: encoder.encode(obj); | 178400 | 5002 | 35665
|
|
10
|
-
cbor-x w/ shared structures: encoder.decode(buf); | 376700 | 5000 | 75340
|
|
11
|
-
buf = require("cbor").encode(obj); | 7700 | 5008 | 1537
|
|
12
|
-
obj = require("cbor").decode(buf); | 3100 | 5119 | 605
|
|
13
|
-
buf = require("cbor-sync").encode(obj); | 18000 | 5025 | 3582
|
|
14
|
-
obj = require("cbor-sync").decode(buf); | 20900 | 5011 | 4170
|
|
15
|
-
buf = require("msgpack-lite").encode(obj); | 30100 | 5012 | 6005
|
|
16
|
-
obj = require("msgpack-lite").decode(buf); | 16200 | 5001 | 3239
|
|
17
|
-
buf = require("@msgpack/msgpack").encode(obj); | 101200 | 5001 | 20235
|
|
18
|
-
obj = require("@msgpack/msgpack").decode(buf); | 71200 | 5004 | 14228
|
|
19
|
-
buf = require("notepack").encode(obj); | 62600 | 5005 | 12507
|
|
20
|
-
obj = require("notepack").decode(buf); | 32400 | 5007 | 6470
|
|
21
|
-
require("what-the-pack")... encoder.encode(obj); | 63500 | 5002 | 12694
|
|
22
|
-
require("what-the-pack")... encoder.decode(buf); | 32000 | 5001 | 6398
|
|
23
|
-
require("avsc")...make schema/type...type.toBuffer(obj); | 84600 | 5003 | 16909
|
|
24
|
-
require("avsc")...make schema/type...type.toBuffer(obj); | 99300 | 5001 | 19856
|
|
25
|
-
|
|
26
|
-
(`avsc` is schema-based and more comparable in style to cbor-x with shared structures).
|
|
27
|
-
|
|
28
|
-
Here is a benchmark of streaming data (again borrowed from `msgpack-lite`'s benchmarking), where cbor-x is able to take advantage of the structured record extension and really pull away from other tools:
|
|
29
|
-
|
|
30
|
-
operation (1000000 x 2) | op | ms | op/s
|
|
31
|
-
------------------------------------------------ | ------: | ----: | -----:
|
|
32
|
-
new EncoderStream().write(obj); | 1000000 | 533 | 1876172
|
|
33
|
-
new DecoderStream().write(buf); | 1000000 | 297 | 3367003
|
|
34
|
-
stream.write(msgpack.encode(obj)); | 1000000 | 3179 | 314564
|
|
35
|
-
stream.write(msgpack.decode(buf)); | 1000000 | 2151 | 464900
|
|
36
|
-
stream.write(notepack.encode(obj)); | 1000000 | 944 | 1059322
|
|
37
|
-
stream.write(notepack.decode(buf)); | 1000000 | 1131 | 884173
|
|
38
|
-
msgpack.Encoder().on("data",ondata).encode(obj); | 1000000 | 1687 | 592768
|
|
39
|
-
msgpack.createDecodeStream().write(buf); | 1000000 | 2084 | 479846
|
|
40
|
-
msgpack.createEncodeStream().write(obj); | 1000000 | 1475 | 677966
|
|
41
|
-
msgpack.Decoder().on("data",ondata).decode(buf); | 1000000 | 2264 | 441696
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
These are the benchmarks from notepack package. The larger test data for these benchmarks is very heavily weighted with large binary/buffer data and objects with extreme numbers of keys (much more than I typically see with real-world data, but YMMV):
|
|
46
|
-
|
|
47
|
-
node ./benchmarks/encode
|
|
48
|
-
|
|
49
|
-
library | tiny | small | medium | large
|
|
50
|
-
---------------- | ----------------: | --------------: | ---------------| -------:
|
|
51
|
-
notepack | 2,171,621 ops/sec | 546,905 ops/sec | 29,578 ops/sec | 265 ops/sec
|
|
52
|
-
msgpack-js | 967,682 ops/sec | 184,455 ops/sec | 20,556 ops/sec | 259 ops/sec
|
|
53
|
-
cbor-x | 2,392,826 ops/sec | 556,915 ops/sec | 70,573 ops/sec | 313 ops/sec
|
|
54
|
-
msgpack-lite | 553,143 ops/sec | 132,318 ops/sec | 11,816 ops/sec | 186 ops/sec
|
|
55
|
-
@msgpack/msgpack | 2,157,655 ops/sec | 573,236 ops/sec | 25,864 ops/sec | 90.26 ops/sec
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
node ./benchmarks/decode
|
|
59
|
-
|
|
60
|
-
library | tiny | small | medium | large
|
|
61
|
-
---------------- | ----------------: | --------------: | --------------- | -------:
|
|
62
|
-
notepack | 2,220,904 ops/sec | 560,630 ops/sec | 28,177 ops/sec | 275 ops/sec
|
|
63
|
-
msgpack-js | 965,719 ops/sec | 222,047 ops/sec | 21,431 ops/sec | 257 ops/sec
|
|
64
|
-
cbor-x | 2,320,046 ops/sec | 589,167 ops/sec | 70,299 ops/sec | 329 ops/sec
|
|
65
|
-
cbor-x records | 3,750,547 ops/sec | 912,419 ops/sec | 136,853 ops/sec | 733 ops/sec
|
|
66
|
-
msgpack-lite | 569,222 ops/sec | 129,008 ops/sec | 12,424 ops/sec | 180 ops/sec
|
|
67
|
-
@msgpack/msgpack | 2,089,697 ops/sec | 557,507 ops/sec | 20,256 ops/sec | 85.03 ops/sec
|
|
68
|
-
|
|
69
|
-
This was run by adding the cbor-x to the benchmarks for notepack.
|
|
70
|
-
|
|
71
|
-
All benchmarks were performed on Node 14.8.0 (Windows i7-4770 3.4Ghz). They can be run with:
|
|
72
|
-
npm install --no-save msgpack msgpack-js @msgpack/msgpack msgpack-lite notepack avsc
|
|
73
|
-
node tests/benchmark
|
|
1
|
+
Here are more comprehensive benchmarks. This is comparison with the next fastest JS projects using the benchmark tool from `msgpack-lite` (and data is from some clinical research data we use that has a good mix of different value types and structures). It also includes comparison to V8 native JSON functionality, and JavaScript Avro (`avsc`, a very optimized Avro implementation):
|
|
2
|
+
|
|
3
|
+
operation | op | ms | op/s
|
|
4
|
+
---------------------------------------------------------- | ------: | ----: | -----:
|
|
5
|
+
buf = Buffer(JSON.stringify(obj)); | 82000 | 5004 | 16386
|
|
6
|
+
obj = JSON.parse(buf); | 88600 | 5000 | 17720
|
|
7
|
+
require("cbor-x").encode(obj); | 161500 | 5002 | 32287
|
|
8
|
+
require("cbor-x").decode(buf); | 94600 | 5004 | 18904
|
|
9
|
+
cbor-x w/ shared structures: encoder.encode(obj); | 178400 | 5002 | 35665
|
|
10
|
+
cbor-x w/ shared structures: encoder.decode(buf); | 376700 | 5000 | 75340
|
|
11
|
+
buf = require("cbor").encode(obj); | 7700 | 5008 | 1537
|
|
12
|
+
obj = require("cbor").decode(buf); | 3100 | 5119 | 605
|
|
13
|
+
buf = require("cbor-sync").encode(obj); | 18000 | 5025 | 3582
|
|
14
|
+
obj = require("cbor-sync").decode(buf); | 20900 | 5011 | 4170
|
|
15
|
+
buf = require("msgpack-lite").encode(obj); | 30100 | 5012 | 6005
|
|
16
|
+
obj = require("msgpack-lite").decode(buf); | 16200 | 5001 | 3239
|
|
17
|
+
buf = require("@msgpack/msgpack").encode(obj); | 101200 | 5001 | 20235
|
|
18
|
+
obj = require("@msgpack/msgpack").decode(buf); | 71200 | 5004 | 14228
|
|
19
|
+
buf = require("notepack").encode(obj); | 62600 | 5005 | 12507
|
|
20
|
+
obj = require("notepack").decode(buf); | 32400 | 5007 | 6470
|
|
21
|
+
require("what-the-pack")... encoder.encode(obj); | 63500 | 5002 | 12694
|
|
22
|
+
require("what-the-pack")... encoder.decode(buf); | 32000 | 5001 | 6398
|
|
23
|
+
require("avsc")...make schema/type...type.toBuffer(obj); | 84600 | 5003 | 16909
|
|
24
|
+
require("avsc")...make schema/type...type.toBuffer(obj); | 99300 | 5001 | 19856
|
|
25
|
+
|
|
26
|
+
(`avsc` is schema-based and more comparable in style to cbor-x with shared structures).
|
|
27
|
+
|
|
28
|
+
Here is a benchmark of streaming data (again borrowed from `msgpack-lite`'s benchmarking), where cbor-x is able to take advantage of the structured record extension and really pull away from other tools:
|
|
29
|
+
|
|
30
|
+
operation (1000000 x 2) | op | ms | op/s
|
|
31
|
+
------------------------------------------------ | ------: | ----: | -----:
|
|
32
|
+
new EncoderStream().write(obj); | 1000000 | 533 | 1876172
|
|
33
|
+
new DecoderStream().write(buf); | 1000000 | 297 | 3367003
|
|
34
|
+
stream.write(msgpack.encode(obj)); | 1000000 | 3179 | 314564
|
|
35
|
+
stream.write(msgpack.decode(buf)); | 1000000 | 2151 | 464900
|
|
36
|
+
stream.write(notepack.encode(obj)); | 1000000 | 944 | 1059322
|
|
37
|
+
stream.write(notepack.decode(buf)); | 1000000 | 1131 | 884173
|
|
38
|
+
msgpack.Encoder().on("data",ondata).encode(obj); | 1000000 | 1687 | 592768
|
|
39
|
+
msgpack.createDecodeStream().write(buf); | 1000000 | 2084 | 479846
|
|
40
|
+
msgpack.createEncodeStream().write(obj); | 1000000 | 1475 | 677966
|
|
41
|
+
msgpack.Decoder().on("data",ondata).decode(buf); | 1000000 | 2264 | 441696
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
These are the benchmarks from notepack package. The larger test data for these benchmarks is very heavily weighted with large binary/buffer data and objects with extreme numbers of keys (much more than I typically see with real-world data, but YMMV):
|
|
46
|
+
|
|
47
|
+
node ./benchmarks/encode
|
|
48
|
+
|
|
49
|
+
library | tiny | small | medium | large
|
|
50
|
+
---------------- | ----------------: | --------------: | ---------------| -------:
|
|
51
|
+
notepack | 2,171,621 ops/sec | 546,905 ops/sec | 29,578 ops/sec | 265 ops/sec
|
|
52
|
+
msgpack-js | 967,682 ops/sec | 184,455 ops/sec | 20,556 ops/sec | 259 ops/sec
|
|
53
|
+
cbor-x | 2,392,826 ops/sec | 556,915 ops/sec | 70,573 ops/sec | 313 ops/sec
|
|
54
|
+
msgpack-lite | 553,143 ops/sec | 132,318 ops/sec | 11,816 ops/sec | 186 ops/sec
|
|
55
|
+
@msgpack/msgpack | 2,157,655 ops/sec | 573,236 ops/sec | 25,864 ops/sec | 90.26 ops/sec
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
node ./benchmarks/decode
|
|
59
|
+
|
|
60
|
+
library | tiny | small | medium | large
|
|
61
|
+
---------------- | ----------------: | --------------: | --------------- | -------:
|
|
62
|
+
notepack | 2,220,904 ops/sec | 560,630 ops/sec | 28,177 ops/sec | 275 ops/sec
|
|
63
|
+
msgpack-js | 965,719 ops/sec | 222,047 ops/sec | 21,431 ops/sec | 257 ops/sec
|
|
64
|
+
cbor-x | 2,320,046 ops/sec | 589,167 ops/sec | 70,299 ops/sec | 329 ops/sec
|
|
65
|
+
cbor-x records | 3,750,547 ops/sec | 912,419 ops/sec | 136,853 ops/sec | 733 ops/sec
|
|
66
|
+
msgpack-lite | 569,222 ops/sec | 129,008 ops/sec | 12,424 ops/sec | 180 ops/sec
|
|
67
|
+
@msgpack/msgpack | 2,089,697 ops/sec | 557,507 ops/sec | 20,256 ops/sec | 85.03 ops/sec
|
|
68
|
+
|
|
69
|
+
This was run by adding the cbor-x to the benchmarks for notepack.
|
|
70
|
+
|
|
71
|
+
All benchmarks were performed on Node 14.8.0 (Windows i7-4770 3.4Ghz). They can be run with:
|
|
72
|
+
npm install --no-save msgpack msgpack-js @msgpack/msgpack msgpack-lite notepack avsc
|
|
73
|
+
node tests/benchmark
|
package/browser.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
exports.Encoder = require('./encode').Encoder
|
|
2
|
-
exports.Decoder = require('./decode').Decoder
|
|
3
|
-
exports.addExtension = require('./encode').addExtension
|
|
4
|
-
let encoder = new exports.Encoder({ useRecords: false })
|
|
5
|
-
exports.decode = encoder.decode
|
|
6
|
-
exports.encode = encoder.encode
|
|
7
|
-
Object.assign(exports, {
|
|
8
|
-
ALWAYS:1,
|
|
9
|
-
DECIMAL_ROUND: 3,
|
|
10
|
-
DECIMAL_FIT: 4
|
|
11
|
-
})
|
|
1
|
+
exports.Encoder = require('./encode').Encoder
|
|
2
|
+
exports.Decoder = require('./decode').Decoder
|
|
3
|
+
exports.addExtension = require('./encode').addExtension
|
|
4
|
+
let encoder = new exports.Encoder({ useRecords: false })
|
|
5
|
+
exports.decode = encoder.decode
|
|
6
|
+
exports.encode = encoder.encode
|
|
7
|
+
Object.assign(exports, {
|
|
8
|
+
ALWAYS:1,
|
|
9
|
+
DECIMAL_ROUND: 3,
|
|
10
|
+
DECIMAL_FIT: 4
|
|
11
|
+
})
|
package/decode.d.ts
CHANGED
|
@@ -1,46 +1,2 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
ALWAYS = 1,
|
|
4
|
-
DECIMAL_ROUND = 3,
|
|
5
|
-
DECIMAL_FIT = 4
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export interface Options {
|
|
9
|
-
useFloat32?: FLOAT32_OPTIONS
|
|
10
|
-
useRecords?: boolean
|
|
11
|
-
structures?: {}[]
|
|
12
|
-
structuredClone?: boolean
|
|
13
|
-
mapsAsObjects?: boolean
|
|
14
|
-
variableMapSize?: boolean
|
|
15
|
-
copyBuffers?: boolean
|
|
16
|
-
bundleStrings?: boolean
|
|
17
|
-
useTimestamp32?: boolean
|
|
18
|
-
largeBigIntToFloat?: boolean
|
|
19
|
-
encodeUndefinedAsNil?: boolean
|
|
20
|
-
maxSharedStructures?: number
|
|
21
|
-
maxOwnStructures?: number
|
|
22
|
-
useSelfDescribedHeader?: boolean
|
|
23
|
-
keyMap?: {}
|
|
24
|
-
shouldShareStructure?: (keys: string[]) => boolean
|
|
25
|
-
getStructures?(): {}[]
|
|
26
|
-
saveStructures?(structures: {}[]): boolean | void
|
|
27
|
-
onInvalidDate?: () => any
|
|
28
|
-
}
|
|
29
|
-
interface Extension {
|
|
30
|
-
Class: Function
|
|
31
|
-
tag: number
|
|
32
|
-
encode(value: any): Buffer | Uint8Array
|
|
33
|
-
decode(messagePack: Buffer | Uint8Array): any
|
|
34
|
-
}
|
|
35
|
-
export class Decoder {
|
|
36
|
-
constructor(options?: Options)
|
|
37
|
-
decode(messagePack: Buffer | Uint8Array): any
|
|
38
|
-
decodeMultiple(messagePack: Buffer | Uint8Array, forEach?: (value: any) => any): [] | void
|
|
39
|
-
}
|
|
40
|
-
export function decode(messagePack: Buffer | Uint8Array): any
|
|
41
|
-
export function decodeMultiple(messagePack: Buffer | Uint8Array, forEach?: (value: any) => any): [] | void
|
|
42
|
-
export function addExtension(extension: Extension): void
|
|
43
|
-
export function clearSource(): void
|
|
44
|
-
export function roundFloat32(float32Number: number): number
|
|
45
|
-
export let isNativeAccelerationEnabled: boolean
|
|
46
|
-
|
|
1
|
+
export { decode, decodeMultiple, Decoder, addExtension, clearSource,roundFloat32, isNativeAccelerationEnabled,
|
|
2
|
+
Extension, Options, FLOAT32_OPTIONS} from '.'
|
package/decode.js
CHANGED
|
@@ -822,8 +822,6 @@ export class Tag {
|
|
|
822
822
|
}
|
|
823
823
|
}
|
|
824
824
|
|
|
825
|
-
let glbl = typeof self == 'object' ? self : global
|
|
826
|
-
|
|
827
825
|
currentExtensions[0] = (dateString) => {
|
|
828
826
|
// string date extension
|
|
829
827
|
return new Date(dateString)
|
|
@@ -890,7 +888,7 @@ currentExtensions[15] = (value) => {
|
|
|
890
888
|
return bundledStrings[1].slice(bundledStrings.position1, bundledStrings.position1 += value)
|
|
891
889
|
return new Tag(value, 15)
|
|
892
890
|
}
|
|
893
|
-
|
|
891
|
+
let glbl = { Error, RegExp }
|
|
894
892
|
currentExtensions[27] = (data) => { // http://cbor.schmorp.de/generic-object
|
|
895
893
|
return (glbl[data[0]] || Error)(data[1], data[2])
|
|
896
894
|
}
|
|
@@ -1015,13 +1013,17 @@ currentExtensionRanges.push((tag, input) => {
|
|
|
1015
1013
|
})
|
|
1016
1014
|
|
|
1017
1015
|
const isLittleEndianMachine = new Uint8Array(new Uint16Array([1]).buffer)[0] == 1
|
|
1018
|
-
export const typedArrays = [
|
|
1016
|
+
export const typedArrays = [Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array,
|
|
1017
|
+
typeof BigUint64Array == 'undefined' ? { name:'BigUint64Array' } : BigUint64Array, Int8Array, Int16Array, Int32Array,
|
|
1018
|
+
typeof BigInt64Array == 'undefined' ? { name:'BigInt64Array' } : BigInt64Array, Float32Array, Float64Array]
|
|
1019
1019
|
const typedArrayTags = [64, 68, 69, 70, 71, 72, 77, 78, 79, 85, 86]
|
|
1020
1020
|
for (let i = 0; i < typedArrays.length; i++) {
|
|
1021
1021
|
registerTypedArray(typedArrays[i], typedArrayTags[i])
|
|
1022
1022
|
}
|
|
1023
|
-
function registerTypedArray(
|
|
1024
|
-
let
|
|
1023
|
+
function registerTypedArray(TypedArray, tag) {
|
|
1024
|
+
let dvMethod = 'get' + TypedArray.name.slice(0, -5)
|
|
1025
|
+
if (typeof TypedArray !== 'function')
|
|
1026
|
+
TypedArray = null;
|
|
1025
1027
|
let bytesPerElement = TypedArray.BYTES_PER_ELEMENT
|
|
1026
1028
|
for (let littleEndian = 0; littleEndian < 2; littleEndian++) {
|
|
1027
1029
|
if (!littleEndian && bytesPerElement == 1)
|
|
@@ -1038,7 +1040,7 @@ function registerTypedArray(typedArrayId, tag, littleEndian) {
|
|
|
1038
1040
|
let dv = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)
|
|
1039
1041
|
let elements = buffer.length >> sizeShift
|
|
1040
1042
|
let ta = new TypedArray(elements)
|
|
1041
|
-
let method = dv[
|
|
1043
|
+
let method = dv[dvMethod]
|
|
1042
1044
|
for (let i = 0; i < elements; i++) {
|
|
1043
1045
|
ta[i] = method.call(dv, i << sizeShift, littleEndian)
|
|
1044
1046
|
}
|