msgpackr 1.5.1 → 1.5.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.
@@ -1,64 +0,0 @@
1
- const data = require('./example4.json');
2
- const { pack, unpack, Packr } = require('msgpackr/pack');
3
- const chai = require('chai');
4
-
5
- function tryRequire(module) {
6
- try {
7
- return require(module)
8
- } catch(error) {
9
- console.log(error)
10
- }
11
- }
12
- //if (typeof chai === 'undefined') { chai = require('chai') }
13
- const assert = chai.assert
14
- //if (typeof msgpackr === 'undefined') { msgpackr = require('..') }
15
- var msgpack_msgpack = tryRequire('@msgpack/msgpack');
16
- var msgpack_lite = tryRequire('msgpack-lite');
17
- var msgpack = tryRequire('msgpack');
18
-
19
- const addCompatibilitySuite = (data) => () => {
20
- if (msgpack_msgpack) {
21
- test('from @msgpack/msgpack', function(){
22
- var serialized = msgpack_msgpack.encode(data)
23
- var deserialized = unpack(serialized)
24
- assert.deepEqual(deserialized, data)
25
- })
26
-
27
- test('to @msgpack/msgpack', function(){
28
- var serialized = pack(data)
29
- var deserialized = msgpack_msgpack.decode(serialized)
30
- assert.deepEqual(deserialized, data)
31
- })
32
- }
33
- if (msgpack_lite) {
34
- test('from msgpack-lite', function(){
35
- var serialized = msgpack_lite.encode(data)
36
- var deserialized = unpack(serialized)
37
- assert.deepEqual(deserialized, data)
38
- })
39
-
40
- test('to msgpack-lite', function(){
41
- var serialized = pack(data)
42
- var deserialized = msgpack_lite.decode(serialized)
43
- assert.deepEqual(deserialized, data)
44
- })
45
- }
46
- if (msgpack) {
47
- test.skip('from msgpack', function(){
48
- var serialized = msgpack.pack(data)
49
- var deserialized = unpack(serialized)
50
- assert.deepEqual(deserialized, data)
51
- })
52
-
53
- test('to msgpack', function(){
54
- var serialized = pack(data)
55
- var deserialized = msgpack.unpack(serialized)
56
- assert.deepEqual(deserialized, data)
57
- })
58
- }
59
- }
60
-
61
- suite('msgpackr compatibility tests (example)', addCompatibilitySuite(require('./example.json')))
62
- suite('msgpackr compatibility tests (example4)', addCompatibilitySuite(require('./example4.json')))
63
- suite('msgpackr compatibility tests (example5)', addCompatibilitySuite(require('./example5.json')))
64
- suite.skip('msgpackr compatibility tests with dates', addCompatibilitySuite({ date: new Date() }))
@@ -1,41 +0,0 @@
1
- import { encode } from '../index.js'
2
- import { assert } from 'chai'
3
- import { Encoder } from '../pack.js'
4
-
5
- const tests = {
6
- string: 'interesting string',
7
- number: 12345,
8
- buffer: Buffer.from('hello world'),
9
- bigint: 12345678910n,
10
- array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
11
- 'many-strings': [],
12
- set: new Set('abcdefghijklmnopqrstuvwxyz'.split('')),
13
- object: { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6 }
14
- }
15
- for (let i = 0; i < 100; i++) {
16
- tests['many-strings'].push('test-data-' + i)
17
- }
18
-
19
- suite('encode and decode tests with partial values', function () {
20
- const encoder = new Encoder({ objectMode: true, structures: [], structuredClone: true })
21
-
22
- for (const [label, testData] of Object.entries(tests)) {
23
- test(label, () => {
24
- const encoded = encoder.encode(testData)
25
- assert.isTrue(Buffer.isBuffer(encoded), 'encode returns a Buffer')
26
- assert.deepStrictEqual(encoder.decode(encoded, encoded.length, true), testData, 'full buffer decodes well')
27
- const firstHalf = encoded.slice(0, Math.floor(encoded.length / 2))
28
- let value
29
- try {
30
- value = encoder.decode(firstHalf, firstHalf.length, true)
31
- } catch (err) {
32
- if (err.incomplete !== true) {
33
- assert.fail(`Should throw an error with .incomplete set to true, instead threw error <${err}>`)
34
- } else {
35
- return; // victory! correct outcome!
36
- }
37
- }
38
- assert.fail(`Should throw an error with .incomplete set to true, instead returned value ${JSON.stringify(value)}`)
39
- })
40
- }
41
- })
@@ -1,72 +0,0 @@
1
- import { encodeIter, decodeIter } from '../index.js'
2
- import { decode } from '../index.js'
3
- import { assert } from 'chai'
4
-
5
- const tests = [
6
- null,
7
- false,
8
- true,
9
- 'interesting string',
10
- 12345,
11
- 123456789n,
12
- 123.456,
13
- Buffer.from('Hello World'),
14
- 'abcdefghijklmnopqrstuvwxyz'.split('')
15
- ]
16
-
17
- suite('msgpackr iterators interface tests', function () {
18
- test('sync encode iterator', () => {
19
- const encodings = [...encodeIter(tests)]
20
- const decodings = encodings.map(x => decode(x))
21
- assert.deepStrictEqual(decodings, tests)
22
- })
23
-
24
- test('async encode iterator', async () => {
25
- async function * generate () {
26
- for (const test of tests) {
27
- await new Promise((resolve, reject) => setImmediate(resolve))
28
- yield test
29
- }
30
- }
31
-
32
- const chunks = []
33
- for await (const chunk of encodeIter(generate())) {
34
- chunks.push(chunk)
35
- }
36
-
37
- const decodings = chunks.map(x => decode(x))
38
- assert.deepStrictEqual(decodings, tests)
39
- })
40
-
41
- test('sync encode and decode iterator', () => {
42
- const encodings = [...encodeIter(tests)]
43
- assert.isTrue(encodings.every(v => Buffer.isBuffer(v)))
44
- const decodings = [...decodeIter(encodings)]
45
- assert.deepStrictEqual(decodings, tests)
46
-
47
- // also test decodings work with buffers multiple values in a buffer
48
- const concatEncoding = Buffer.concat([...encodings])
49
- const decodings2 = [...decodeIter([concatEncoding])]
50
- assert.deepStrictEqual(decodings2, tests)
51
-
52
- // also test decodings work with partial buffers that don't align to values perfectly
53
- const half1 = concatEncoding.slice(0, Math.floor(concatEncoding.length / 2))
54
- const half2 = concatEncoding.slice(Math.floor(concatEncoding.length / 2))
55
- const decodings3 = [...decodeIter([half1, half2])]
56
- assert.deepStrictEqual(decodings3, tests)
57
- })
58
-
59
- test('async encode and decode iterator', async () => {
60
- async function * generator () {
61
- for (const obj of tests) {
62
- await new Promise((resolve, reject) => setImmediate(resolve))
63
- yield obj
64
- }
65
- }
66
- const yields = []
67
- for await (const value of decodeIter(encodeIter(generator()))) {
68
- yields.push(value)
69
- }
70
- assert.deepStrictEqual(yields, tests)
71
- })
72
- })
@@ -1,76 +0,0 @@
1
- import { PackrStream, UnpackrStream } from '../node-index.js'
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
8
-
9
- suite('msgpackr node stream tests', function(){
10
- test('serialize/parse stream', () => {
11
- const serializeStream = new PackrStream({
12
- })
13
- const parseStream = new UnpackrStream()
14
- serializeStream.pipe(parseStream)
15
- const received = []
16
- parseStream.on('data', data => {
17
- received.push(data)
18
- })
19
- const messages = [{
20
- name: 'first'
21
- }, {
22
- name: 'second'
23
- }, {
24
- name: 'third'
25
- }, {
26
- name: 'third',
27
- extra: [1, 3, { foo: 'hi'}, 'bye']
28
- }]
29
- for (const message of messages)
30
- serializeStream.write(message)
31
- return new Promise((resolve, reject) => {
32
- setTimeout(() => {
33
- assert.deepEqual(received, messages)
34
- resolve()
35
- }, 10)
36
- })
37
- })
38
- test('stream from buffer', () => new Promise(async resolve => {
39
- const parseStream = new UnpackrStream()
40
- let values = []
41
- parseStream.on('data', (value) => {
42
- values.push(value)
43
- })
44
- parseStream.on('end', () => {
45
- assert.deepEqual(values, [1, 2])
46
- resolve()
47
- })
48
- let bufferStream = new stream.Duplex()
49
- bufferStream.pipe(parseStream)
50
- bufferStream.push(new Uint8Array([1, 2]))
51
- bufferStream.push(null)
52
- }))
53
- test('serialize stream to file', async function() {
54
- const serializeStream = new PackrStream({
55
- })
56
- const fileStream = fs.createWriteStream('test-output.msgpack');
57
- serializeStream.pipe(fileStream);
58
- const messages = [{
59
- name: 'first'
60
- }, {
61
- name: 'second',
62
- extra: [1, 3, { foo: 'hi'}, 'bye']
63
- }]
64
- for (const message of messages)
65
- serializeStream.write(message)
66
- setTimeout(() => serializeStream.end(), 10)
67
-
68
- await finished(serializeStream)
69
- })
70
- teardown(function() {
71
- try {
72
- fs.unlinkSync('test-output.msgpack')
73
- }catch(error){}
74
- })
75
- })
76
-