cbor-x 1.3.3 → 1.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/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 +47 -29
- package/dist/index.js +1160 -1134
- package/dist/index.min.js +100 -85
- package/dist/node.cjs +1228 -1202
- package/dist/test.js +26 -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
|
@@ -84,7 +84,7 @@ export class Decoder {
|
|
|
84
84
|
}
|
|
85
85
|
let res = {}
|
|
86
86
|
//map.forEach((v,k) => res[Object.keys(this._keyMap)[Object.values(this._keyMap).indexOf(k)] || k] = v)
|
|
87
|
-
map.forEach((v,k) => res[this._mapKey.has(k) ? this._mapKey.get(k) : k] = v)
|
|
87
|
+
map.forEach((v,k) => res[safeKey(this._mapKey.has(k) ? this._mapKey.get(k) : k)] = v)
|
|
88
88
|
return res
|
|
89
89
|
}
|
|
90
90
|
|
|
@@ -289,8 +289,8 @@ export function read() {
|
|
|
289
289
|
let key
|
|
290
290
|
if (currentDecoder.mapsAsObjects) {
|
|
291
291
|
let object = {}
|
|
292
|
-
if (currentDecoder.keyMap) while((key = read()) != STOP_CODE) object[currentDecoder.decodeKey(key)] = read()
|
|
293
|
-
else while ((key = read()) != STOP_CODE) object[key] = read()
|
|
292
|
+
if (currentDecoder.keyMap) while((key = read()) != STOP_CODE) object[safeKey(currentDecoder.decodeKey(key))] = read()
|
|
293
|
+
else while ((key = read()) != STOP_CODE) object[safeKey(key)] = read()
|
|
294
294
|
return object
|
|
295
295
|
} else {
|
|
296
296
|
if (restoreMapsAsObject) {
|
|
@@ -338,8 +338,8 @@ export function read() {
|
|
|
338
338
|
case 5: // map
|
|
339
339
|
if (currentDecoder.mapsAsObjects) {
|
|
340
340
|
let object = {}
|
|
341
|
-
if (currentDecoder.keyMap) for (let i = 0; i < token; i++) object[currentDecoder.decodeKey(read())] = read()
|
|
342
|
-
else for (let i = 0; i < token; i++) object[read()] = read()
|
|
341
|
+
if (currentDecoder.keyMap) for (let i = 0; i < token; i++) object[safeKey(currentDecoder.decodeKey(read()))] = read()
|
|
342
|
+
else for (let i = 0; i < token; i++) object[safeKey(read())] = read()
|
|
343
343
|
return object
|
|
344
344
|
} else {
|
|
345
345
|
if (restoreMapsAsObject) {
|
|
@@ -455,8 +455,8 @@ function createStructureReader(structure) {
|
|
|
455
455
|
if (this.slowReads++ >= 3) { // create a fast compiled reader
|
|
456
456
|
let array = this.length == length ? this : this.slice(0, length)
|
|
457
457
|
compiledReader = currentDecoder.keyMap
|
|
458
|
-
? new Function('r', 'return {' + array.map(k => currentDecoder.decodeKey(k)).map(k => validName.test(k) ? k + ':r()' : ('[' + JSON.stringify(k) + ']:r()')).join(',') + '}')
|
|
459
|
-
: new Function('r', 'return {' + array.map(key => validName.test(key) ? key + ':r()' : ('[' + JSON.stringify(key) + ']:r()')).join(',') + '}')
|
|
458
|
+
? new Function('r', 'return {' + array.map(k => currentDecoder.decodeKey(k)).map(k => validName.test(k) ? safeKey(k) + ':r()' : ('[' + JSON.stringify(k) + ']:r()')).join(',') + '}')
|
|
459
|
+
: new Function('r', 'return {' + array.map(key => validName.test(key) ? safeKey(key) + ':r()' : ('[' + JSON.stringify(key) + ']:r()')).join(',') + '}')
|
|
460
460
|
if (this.compiledReader)
|
|
461
461
|
compiledReader.next = this.compiledReader // if there is an existing one, we store multiple readers as a linked list because it is usually pretty rare to have multiple readers (of different length) for the same structure
|
|
462
462
|
compiledReader.propertyCount = length
|
|
@@ -464,14 +464,20 @@ function createStructureReader(structure) {
|
|
|
464
464
|
return compiledReader(read)
|
|
465
465
|
}
|
|
466
466
|
let object = {}
|
|
467
|
-
if (currentDecoder.keyMap) for (let i = 0; i < length; i++) object[currentDecoder.decodeKey(this[i])] = read()
|
|
468
|
-
else for (let i = 0; i < length; i++)
|
|
467
|
+
if (currentDecoder.keyMap) for (let i = 0; i < length; i++) object[safeKey(currentDecoder.decodeKey(this[i]))] = read()
|
|
468
|
+
else for (let i = 0; i < length; i++) {
|
|
469
|
+
object[safeKey(this[i])] = read();
|
|
470
|
+
}
|
|
469
471
|
return object
|
|
470
472
|
}
|
|
471
473
|
structure.slowReads = 0
|
|
472
474
|
return readObject
|
|
473
475
|
}
|
|
474
476
|
|
|
477
|
+
function safeKey(key) {
|
|
478
|
+
return key === '__proto__' ? '__proto_' : key
|
|
479
|
+
}
|
|
480
|
+
|
|
475
481
|
let readFixedString = readStringJS
|
|
476
482
|
let readString8 = readStringJS
|
|
477
483
|
let readString16 = readStringJS
|
|
@@ -737,18 +743,30 @@ function readExt(length) {
|
|
|
737
743
|
else
|
|
738
744
|
throw new Error('Unknown extension type ' + type)
|
|
739
745
|
}
|
|
740
|
-
|
|
746
|
+
let f32Array = new Float32Array(1)
|
|
747
|
+
let u8Array = new Uint8Array(f32Array.buffer, 0, 4)
|
|
741
748
|
function getFloat16() {
|
|
742
749
|
let byte0 = src[position++]
|
|
743
750
|
let byte1 = src[position++]
|
|
744
|
-
let
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
751
|
+
let exponent = (byte0 & 0x7f) >> 2;
|
|
752
|
+
if (exponent === 0x1f) { // specials
|
|
753
|
+
if (byte1 || (byte0 & 3))
|
|
754
|
+
return NaN;
|
|
755
|
+
return (byte0 & 0x80) ? -Infinity : Infinity;
|
|
756
|
+
}
|
|
757
|
+
if (exponent === 0) { // sub-normals
|
|
758
|
+
// significand with 10 fractional bits and divided by 2^14
|
|
759
|
+
let abs = (((byte0 & 3) << 8) | byte1) / (1 << 24)
|
|
760
|
+
return (byte0 & 0x80) ? -abs : abs
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
u8Array[3] = (byte0 & 0x80) | // sign bit
|
|
764
|
+
((exponent >> 1) + 56) // 4 of 5 of the exponent bits, re-offset-ed
|
|
765
|
+
u8Array[2] = ((byte0 & 7) << 5) | // last exponent bit and first two mantissa bits
|
|
766
|
+
(byte1 >> 3) // next 5 bits of mantissa
|
|
767
|
+
u8Array[1] = byte1 << 5; // last three bits of mantissa
|
|
768
|
+
u8Array[0] = 0;
|
|
769
|
+
return f32Array[0];
|
|
752
770
|
}
|
|
753
771
|
|
|
754
772
|
let keyCache = new Array(4096)
|
|
@@ -822,8 +840,6 @@ export class Tag {
|
|
|
822
840
|
}
|
|
823
841
|
}
|
|
824
842
|
|
|
825
|
-
let glbl = typeof self == 'object' ? self : global
|
|
826
|
-
|
|
827
843
|
currentExtensions[0] = (dateString) => {
|
|
828
844
|
// string date extension
|
|
829
845
|
return new Date(dateString)
|
|
@@ -871,11 +887,11 @@ const recordDefinition = (definition) => {
|
|
|
871
887
|
let object = {}
|
|
872
888
|
if (currentDecoder.keyMap) for (let i = 2,l = definition.length; i < l; i++) {
|
|
873
889
|
let key = currentDecoder.decodeKey(structure[i - 2])
|
|
874
|
-
object[key] = definition[i]
|
|
890
|
+
object[safeKey(key)] = definition[i]
|
|
875
891
|
}
|
|
876
892
|
else for (let i = 2,l = definition.length; i < l; i++) {
|
|
877
893
|
let key = structure[i - 2]
|
|
878
|
-
object[key] = definition[i]
|
|
894
|
+
object[safeKey(key)] = definition[i]
|
|
879
895
|
}
|
|
880
896
|
return object
|
|
881
897
|
}
|
|
@@ -890,7 +906,7 @@ currentExtensions[15] = (value) => {
|
|
|
890
906
|
return bundledStrings[1].slice(bundledStrings.position1, bundledStrings.position1 += value)
|
|
891
907
|
return new Tag(value, 15)
|
|
892
908
|
}
|
|
893
|
-
|
|
909
|
+
let glbl = { Error, RegExp }
|
|
894
910
|
currentExtensions[27] = (data) => { // http://cbor.schmorp.de/generic-object
|
|
895
911
|
return (glbl[data[0]] || Error)(data[1], data[2])
|
|
896
912
|
}
|
|
@@ -1015,13 +1031,17 @@ currentExtensionRanges.push((tag, input) => {
|
|
|
1015
1031
|
})
|
|
1016
1032
|
|
|
1017
1033
|
const isLittleEndianMachine = new Uint8Array(new Uint16Array([1]).buffer)[0] == 1
|
|
1018
|
-
export const typedArrays = [
|
|
1034
|
+
export const typedArrays = [Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array,
|
|
1035
|
+
typeof BigUint64Array == 'undefined' ? { name:'BigUint64Array' } : BigUint64Array, Int8Array, Int16Array, Int32Array,
|
|
1036
|
+
typeof BigInt64Array == 'undefined' ? { name:'BigInt64Array' } : BigInt64Array, Float32Array, Float64Array]
|
|
1019
1037
|
const typedArrayTags = [64, 68, 69, 70, 71, 72, 77, 78, 79, 85, 86]
|
|
1020
1038
|
for (let i = 0; i < typedArrays.length; i++) {
|
|
1021
1039
|
registerTypedArray(typedArrays[i], typedArrayTags[i])
|
|
1022
1040
|
}
|
|
1023
|
-
function registerTypedArray(
|
|
1024
|
-
let
|
|
1041
|
+
function registerTypedArray(TypedArray, tag) {
|
|
1042
|
+
let dvMethod = 'get' + TypedArray.name.slice(0, -5)
|
|
1043
|
+
if (typeof TypedArray !== 'function')
|
|
1044
|
+
TypedArray = null;
|
|
1025
1045
|
let bytesPerElement = TypedArray.BYTES_PER_ELEMENT
|
|
1026
1046
|
for (let littleEndian = 0; littleEndian < 2; littleEndian++) {
|
|
1027
1047
|
if (!littleEndian && bytesPerElement == 1)
|
|
@@ -1038,7 +1058,7 @@ function registerTypedArray(typedArrayId, tag, littleEndian) {
|
|
|
1038
1058
|
let dv = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)
|
|
1039
1059
|
let elements = buffer.length >> sizeShift
|
|
1040
1060
|
let ta = new TypedArray(elements)
|
|
1041
|
-
let method = dv[
|
|
1061
|
+
let method = dv[dvMethod]
|
|
1042
1062
|
for (let i = 0; i < elements; i++) {
|
|
1043
1063
|
ta[i] = method.call(dv, i << sizeShift, littleEndian)
|
|
1044
1064
|
}
|
|
@@ -1158,8 +1178,6 @@ export const FLOAT32_OPTIONS = {
|
|
|
1158
1178
|
DECIMAL_ROUND: 3,
|
|
1159
1179
|
DECIMAL_FIT: 4
|
|
1160
1180
|
}
|
|
1161
|
-
let f32Array = new Float32Array(1)
|
|
1162
|
-
let u8Array = new Uint8Array(f32Array.buffer, 0, 4)
|
|
1163
1181
|
export function roundFloat32(float32Number) {
|
|
1164
1182
|
f32Array[0] = float32Number
|
|
1165
1183
|
let multiplier = mult10[((u8Array[3] & 0x7f) << 1) | (u8Array[2] >> 7)]
|