cbor-x 0.8.0 → 0.9.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/.vscode/launch.json +22 -0
- package/README.md +49 -45
- package/SECURITY.md +11 -0
- package/{unpack.d.ts → decode.d.ts} +12 -11
- package/decode.js +86 -33
- package/dist/index.js +194 -49
- package/dist/index.min.js +65 -51
- package/dist/node.cjs +228 -81
- package/dist/test.js +33 -1
- package/encode.d.ts +6 -0
- package/encode.js +31 -18
- package/index.d.ts +7 -6
- package/index.js +2 -2
- package/iterators.js +85 -0
- package/node-index.js +24 -0
- package/package.json +16 -17
- package/rollup.config.js +5 -5
- package/stream.js +20 -22
- package/tests/benchmark.cjs +13 -7
- package/tests/{test-compatibility.js → test-compatibility.cjs} +7 -12
- package/tests/test-incomplete.js +41 -0
- package/tests/test-node-iterators.js +72 -0
- package/tests/test-node-stream.js +17 -5
- package/tests/test.js +33 -2
- package/index.mjs +0 -35
- package/node.js +0 -25
- package/pack.d.ts +0 -9
- package/tests/test.mjs +0 -31
|
@@ -1,11 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { EncoderStream, DecoderStream } from '../node-index.js'
|
|
2
2
|
import stream from 'stream'
|
|
3
|
+
import chai from 'chai'
|
|
4
|
+
import util from 'util'
|
|
5
|
+
import fs from 'fs'
|
|
6
|
+
const finished = util.promisify(stream.finished)
|
|
7
|
+
var assert = chai.assert
|
|
3
8
|
|
|
4
|
-
suite('
|
|
9
|
+
suite('cbor-x node stream tests', function(){
|
|
5
10
|
test('serialize/parse stream', () => {
|
|
6
|
-
const serializeStream = new
|
|
11
|
+
const serializeStream = new EncoderStream({
|
|
7
12
|
})
|
|
8
|
-
const parseStream = new
|
|
13
|
+
const parseStream = new DecoderStream()
|
|
9
14
|
serializeStream.pipe(parseStream)
|
|
10
15
|
const received = []
|
|
11
16
|
parseStream.on('data', data => {
|
|
@@ -31,7 +36,7 @@ suite('msgpackr node stream tests', function(){
|
|
|
31
36
|
})
|
|
32
37
|
})
|
|
33
38
|
test('stream from buffer', () => new Promise(async resolve => {
|
|
34
|
-
const parseStream = new
|
|
39
|
+
const parseStream = new DecoderStream()
|
|
35
40
|
let values = []
|
|
36
41
|
parseStream.on('data', (value) => {
|
|
37
42
|
values.push(value)
|
|
@@ -45,4 +50,11 @@ suite('msgpackr node stream tests', function(){
|
|
|
45
50
|
bufferStream.push(new Uint8Array([1, 2]))
|
|
46
51
|
bufferStream.push(null)
|
|
47
52
|
}))
|
|
53
|
+
|
|
54
|
+
teardown(function() {
|
|
55
|
+
try {
|
|
56
|
+
fs.unlinkSync('test-output.msgpack')
|
|
57
|
+
}catch(error){}
|
|
58
|
+
})
|
|
48
59
|
})
|
|
60
|
+
|
package/tests/test.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import * as CBOR from '../index.js'
|
|
2
2
|
import chai from 'chai'
|
|
3
|
-
//import('./test.mjs')
|
|
4
3
|
import sampleData from './example4.json'
|
|
5
|
-
//import inspector from 'inspector'; inspector.open(
|
|
4
|
+
//import inspector from 'inspector'; inspector.open(9229, null, true); debugger
|
|
6
5
|
function tryRequire(module) {
|
|
7
6
|
try {
|
|
8
7
|
return require(module)
|
|
@@ -85,6 +84,13 @@ suite('CBOR basic tests', function(){
|
|
|
85
84
|
assert.deepEqual(deserialized, data)
|
|
86
85
|
})
|
|
87
86
|
|
|
87
|
+
test('255 chars', function() {
|
|
88
|
+
const data = 'RRZG9A6I7xupPeOZhxcOcioFsuhszGOdyDUcbRf4Zef2kdPIfC9RaLO4jTM5JhuZvTsF09fbRHMGtqk7YAgu3vespeTe9l61ziZ6VrMnYu2CamK96wCkmz0VUXyqaiUoTPgzk414LS9yYrd5uh7w18ksJF5SlC2e91rukWvNqAZJjYN3jpkqHNOFchCwFrhbxq2Lrv1kSJPYCx9blRg2hGmYqTbElLTZHv20iNqwZeQbRMgSBPT6vnbCBPnOh1W'
|
|
89
|
+
var serialized = CBOR.encode(data)
|
|
90
|
+
var deserialized = CBOR.decode(serialized)
|
|
91
|
+
assert.equal(deserialized, data)
|
|
92
|
+
})
|
|
93
|
+
|
|
88
94
|
test('encode/decode sample data', function(){
|
|
89
95
|
var data = sampleData
|
|
90
96
|
var serialized = CBOR.encode(data)
|
|
@@ -215,10 +221,13 @@ suite('CBOR basic tests', function(){
|
|
|
215
221
|
let fa = new Float32Array(b.buffer, 8, 2)
|
|
216
222
|
fa[0] = 2.25
|
|
217
223
|
fa[1] = 6
|
|
224
|
+
let map = new Map()
|
|
225
|
+
map.set('key', 'value')
|
|
218
226
|
let object = {
|
|
219
227
|
error: new Error('test'),
|
|
220
228
|
set: new Set(['a', 'b']),
|
|
221
229
|
regexp: /test/gi,
|
|
230
|
+
map,
|
|
222
231
|
float32Array: fa,
|
|
223
232
|
uint16Array: new Uint16Array([3,4])
|
|
224
233
|
}
|
|
@@ -228,6 +237,7 @@ suite('CBOR basic tests', function(){
|
|
|
228
237
|
var serialized = encoder.encode(object)
|
|
229
238
|
var deserialized = encoder.decode(serialized)
|
|
230
239
|
assert.deepEqual(Array.from(deserialized.set), Array.from(object.set))
|
|
240
|
+
assert.equal(deserialized.map.get('key'), 'value')
|
|
231
241
|
assert.equal(deserialized.error.message, object.error.message)
|
|
232
242
|
assert.equal(deserialized.regexp.test('TEST'), true)
|
|
233
243
|
assert.equal(deserialized.float32Array.constructor.name, 'Float32Array')
|
|
@@ -238,6 +248,19 @@ suite('CBOR basic tests', function(){
|
|
|
238
248
|
assert.equal(deserialized.uint16Array[1], 4)
|
|
239
249
|
})
|
|
240
250
|
|
|
251
|
+
test('explicit maps and sets', function () {
|
|
252
|
+
let map = new Map()
|
|
253
|
+
map.set('key', { inside: 'value'})
|
|
254
|
+
let object = {
|
|
255
|
+
set: new Set(['a', 'b']),
|
|
256
|
+
map,
|
|
257
|
+
}
|
|
258
|
+
var serialized = encode(object) // default encoder
|
|
259
|
+
var deserialized = decode(serialized)
|
|
260
|
+
assert.deepEqual(Array.from(deserialized.set), Array.from(object.set))
|
|
261
|
+
assert.equal(deserialized.map.get('key').inside, 'value')
|
|
262
|
+
})
|
|
263
|
+
|
|
241
264
|
test('object without prototype', function(){
|
|
242
265
|
var data = Object.create(null)
|
|
243
266
|
data.test = 3
|
|
@@ -253,6 +276,13 @@ suite('CBOR basic tests', function(){
|
|
|
253
276
|
var decoded = decode(encoded)
|
|
254
277
|
assert.equal(decoded.length, size)
|
|
255
278
|
})
|
|
279
|
+
test('little buffer', function() {
|
|
280
|
+
var data = typeof Buffer == 'undefined' ? new Uint8Array(0) : Buffer.alloc(0)
|
|
281
|
+
var encoded = encode(data)
|
|
282
|
+
assert.equal(encoded.length, 1) // make sure to use canonical form
|
|
283
|
+
var decoded = decode(encoded)
|
|
284
|
+
assert.equal(decoded.length, 0)
|
|
285
|
+
})
|
|
256
286
|
|
|
257
287
|
test('random strings', function(){
|
|
258
288
|
var data = []
|
|
@@ -300,6 +330,7 @@ suite('CBOR basic tests', function(){
|
|
|
300
330
|
let encoder = new Encoder({
|
|
301
331
|
mapsAsObjects: true,
|
|
302
332
|
useTimestamp32: true,
|
|
333
|
+
useTag259ForMaps: false,
|
|
303
334
|
})
|
|
304
335
|
var serialized = encoder.encode(data)
|
|
305
336
|
var deserialized = encoder.decode(serialized)
|
package/index.mjs
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import encodeModule from './encode.js'
|
|
2
|
-
import decodeModule from './decode.js'
|
|
3
|
-
import { createRequire } from 'module'
|
|
4
|
-
|
|
5
|
-
export const Encoder = encodeModule.Encoder
|
|
6
|
-
export const addExtension = encodeModule.addExtension
|
|
7
|
-
let extractor = tryRequire('cbor-extract')
|
|
8
|
-
if (extractor)
|
|
9
|
-
decodeModule.setExtractor(extractor.extractStrings)
|
|
10
|
-
export const Decoder = decodeModule.Decoder
|
|
11
|
-
export const Tag = decodeModule.Tag
|
|
12
|
-
import stream from './stream.js'
|
|
13
|
-
export const EncoderStream = stream.EncoderStream
|
|
14
|
-
export const DecoderStream = stream.DecoderStream
|
|
15
|
-
let encoder = new encodeModule.Encoder({ useRecords: false })
|
|
16
|
-
export const decode = encoder.decode
|
|
17
|
-
export const encode = encoder.encode
|
|
18
|
-
export const useRecords = false
|
|
19
|
-
export const mapsAsObjects = true
|
|
20
|
-
export const ALWAYS = 1
|
|
21
|
-
export const DECIMAL_ROUND = 3
|
|
22
|
-
export const DECIMAL_FIT = 4
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
function tryRequire(moduleId) {
|
|
26
|
-
try {
|
|
27
|
-
let require = createRequire(import.meta.url)
|
|
28
|
-
return require(moduleId)
|
|
29
|
-
} catch (error) {
|
|
30
|
-
if (typeof window == 'undefined')
|
|
31
|
-
console.warn('Native extraction module not loaded, cbor-x will still run, but with decreased performance. ' + error.message.split('\n')[0])
|
|
32
|
-
else
|
|
33
|
-
console.warn('For browser usage, directly use encode/decode modules. ' + error.message.split('\n')[0])
|
|
34
|
-
}
|
|
35
|
-
}
|
package/node.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
export { Encoder, addExtension, encode, NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT } from './encode.js'
|
|
2
|
-
export { Tag, Decoder, decodeMultiple, decode, FLOAT32_OPTIONS } from './decode.js'
|
|
3
|
-
export { EncoderStream, DecoderStream } from './stream.js'
|
|
4
|
-
export const useRecords = false
|
|
5
|
-
export const mapsAsObjects = true
|
|
6
|
-
import { setExtractor } from './decode.js'
|
|
7
|
-
import { createRequire } from 'module'
|
|
8
|
-
|
|
9
|
-
const extractor = tryRequire('msgpackr-extract')
|
|
10
|
-
if (extractor)
|
|
11
|
-
setExtractor(extractor.extractStrings)
|
|
12
|
-
/*
|
|
13
|
-
Object.assign(exports, unpackModule.FLOAT32_OPTIONS)
|
|
14
|
-
*/
|
|
15
|
-
function tryRequire(moduleId) {
|
|
16
|
-
try {
|
|
17
|
-
let require = createRequire(import.meta.url)
|
|
18
|
-
return require(moduleId)
|
|
19
|
-
} catch (error) {
|
|
20
|
-
if (typeof window == 'undefined')
|
|
21
|
-
console.warn('Native extraction module not loaded, msgpackr will still run, but with decreased performance. ' + error.message.split('\n')[0])
|
|
22
|
-
else
|
|
23
|
-
console.warn('For browser usage, directly use msgpackr/unpack or msgpackr/pack modules. ' + error.message.split('\n')[0])
|
|
24
|
-
}
|
|
25
|
-
}
|
package/pack.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { Unpackr } from './unpack'
|
|
2
|
-
export { addExtension, FLOAT32_OPTIONS } from './unpack'
|
|
3
|
-
export class Packr extends Unpackr {
|
|
4
|
-
pack(value: any): Buffer
|
|
5
|
-
encode(value: any): Buffer
|
|
6
|
-
}
|
|
7
|
-
export class Encoder extends Packr {}
|
|
8
|
-
export function pack(value: any): Buffer
|
|
9
|
-
export function encode(value: any): Buffer
|
package/tests/test.mjs
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { pack, unpack } from '../index.mjs'
|
|
2
|
-
var ITERATIONS = 10000
|
|
3
|
-
|
|
4
|
-
suite('msgpackr basic tests from module', function(){
|
|
5
|
-
test('pack/unpack data', function(){
|
|
6
|
-
var data = {
|
|
7
|
-
data: [
|
|
8
|
-
{ a: 1, name: 'one', type: 'odd', isOdd: true },
|
|
9
|
-
{ a: 2, name: 'two', type: 'even'},
|
|
10
|
-
{ a: 3, name: 'three', type: 'odd', isOdd: true },
|
|
11
|
-
{ a: 4, name: 'four', type: 'even'},
|
|
12
|
-
{ a: 5, name: 'five', type: 'odd', isOdd: true },
|
|
13
|
-
{ a: 6, name: 'six', type: 'even', isOdd: null }
|
|
14
|
-
],
|
|
15
|
-
description: 'some names',
|
|
16
|
-
types: ['odd', 'even'],
|
|
17
|
-
convertEnumToNum: [
|
|
18
|
-
{ prop: 'test' },
|
|
19
|
-
{ prop: 'test' },
|
|
20
|
-
{ prop: 'test' },
|
|
21
|
-
{ prop: 1 },
|
|
22
|
-
{ prop: 2 },
|
|
23
|
-
{ prop: [undefined] },
|
|
24
|
-
{ prop: null }
|
|
25
|
-
]
|
|
26
|
-
}
|
|
27
|
-
var serialized = pack(data)
|
|
28
|
-
var deserialized = unpack(serialized)
|
|
29
|
-
assert.deepEqual(deserialized, data)
|
|
30
|
-
})
|
|
31
|
-
})
|