full site update
This commit is contained in:
103
node_modules/restructure/test/Array.js
generated
vendored
Normal file
103
node_modules/restructure/test/Array.js
generated
vendored
Normal file
@@ -0,0 +1,103 @@
|
||||
import assert from 'assert';
|
||||
import {Array as ArrayT, Pointer, uint8, uint16, DecodeStream, EncodeStream} from 'restructure';
|
||||
|
||||
describe('Array', function() {
|
||||
describe('decode', function() {
|
||||
it('should decode fixed length', function() {
|
||||
const buffer = new Uint8Array([1, 2, 3, 4, 5]);
|
||||
const array = new ArrayT(uint8, 4);
|
||||
assert.deepEqual(array.fromBuffer(buffer), [1, 2, 3, 4]);
|
||||
});
|
||||
|
||||
it('should decode fixed amount of bytes', function() {
|
||||
const buffer = new Uint8Array([1, 2, 3, 4, 5]);
|
||||
const array = new ArrayT(uint16, 4, 'bytes');
|
||||
assert.deepEqual(array.fromBuffer(buffer), [258, 772]);
|
||||
});
|
||||
|
||||
it('should decode length from parent key', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([1, 2, 3, 4, 5]));
|
||||
const array = new ArrayT(uint8, 'len');
|
||||
assert.deepEqual(array.decode(stream, {len: 4}), [1, 2, 3, 4]);
|
||||
});
|
||||
|
||||
it('should decode amount of bytes from parent key', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([1, 2, 3, 4, 5]));
|
||||
const array = new ArrayT(uint16, 'len', 'bytes');
|
||||
assert.deepEqual(array.decode(stream, {len: 4}), [258, 772]);
|
||||
});
|
||||
|
||||
it('should decode length as number before array', function() {
|
||||
const buffer = new Uint8Array([4, 1, 2, 3, 4, 5]);
|
||||
const array = new ArrayT(uint8, uint8);
|
||||
assert.deepEqual(array.fromBuffer(buffer), [1, 2, 3, 4]);
|
||||
});
|
||||
|
||||
it('should decode amount of bytes as number before array', function() {
|
||||
const buffer = new Uint8Array([4, 1, 2, 3, 4, 5]);
|
||||
const array = new ArrayT(uint16, uint8, 'bytes');
|
||||
assert.deepEqual(array.fromBuffer(buffer), [258, 772]);
|
||||
});
|
||||
|
||||
it('should decode length from function', function() {
|
||||
const buffer = new Uint8Array([1, 2, 3, 4, 5]);
|
||||
const array = new ArrayT(uint8, function() { return 4; });
|
||||
assert.deepEqual(array.fromBuffer(buffer), [1, 2, 3, 4]);
|
||||
});
|
||||
|
||||
it('should decode amount of bytes from function', function() {
|
||||
const buffer = new Uint8Array([1, 2, 3, 4, 5]);
|
||||
const array = new ArrayT(uint16, (function() { return 4; }), 'bytes');
|
||||
assert.deepEqual(array.fromBuffer(buffer), [258, 772]);
|
||||
});
|
||||
|
||||
it('should decode to the end of the parent if no length is given', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([1, 2, 3, 4, 5]));
|
||||
const array = new ArrayT(uint8);
|
||||
assert.deepEqual(array.decode(stream, {_length: 4, _startOffset: 0}), [1, 2, 3, 4]);
|
||||
});
|
||||
|
||||
it('should decode to the end of the stream if no parent and length is given', function() {
|
||||
const buffer = new Uint8Array([1, 2, 3, 4]);
|
||||
const array = new ArrayT(uint8);
|
||||
assert.deepEqual(array.fromBuffer(buffer), [1, 2, 3, 4]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('size', function() {
|
||||
it('should use array length', function() {
|
||||
const array = new ArrayT(uint8, 10);
|
||||
assert.equal(array.size([1, 2, 3, 4]), 4);
|
||||
});
|
||||
|
||||
it('should add size of length field before string', function() {
|
||||
const array = new ArrayT(uint8, uint8);
|
||||
assert.equal(array.size([1, 2, 3, 4]), 5);
|
||||
});
|
||||
|
||||
it('should use defined length if no value given', function() {
|
||||
const array = new ArrayT(uint8, 10);
|
||||
assert.equal(array.size(), 10);
|
||||
});
|
||||
});
|
||||
|
||||
describe('encode', function() {
|
||||
it('should encode using array length', function() {
|
||||
const array = new ArrayT(uint8, 10);
|
||||
const buffer = array.toBuffer([1, 2, 3, 4]);
|
||||
assert.deepEqual(buffer, new Uint8Array([1, 2, 3, 4]));
|
||||
});
|
||||
|
||||
it('should encode length as number before array', function() {
|
||||
const array = new ArrayT(uint8, uint8);
|
||||
const buffer = array.toBuffer([1, 2, 3, 4]);
|
||||
assert.deepEqual(buffer, new Uint8Array([4, 1, 2, 3, 4]));
|
||||
});
|
||||
|
||||
it('should add pointers after array if length is encoded at start', function() {
|
||||
const array = new ArrayT(new Pointer(uint8, uint8), uint8);
|
||||
const buffer = array.toBuffer([1, 2, 3, 4]);
|
||||
assert.deepEqual(buffer, new Uint8Array([4, 5, 6, 7, 8, 1, 2, 3, 4]));
|
||||
});
|
||||
});
|
||||
});
|
29
node_modules/restructure/test/Bitfield.js
generated
vendored
Normal file
29
node_modules/restructure/test/Bitfield.js
generated
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
import assert from 'assert';
|
||||
import {Bitfield, uint8, DecodeStream, EncodeStream} from 'restructure';
|
||||
|
||||
describe('Bitfield', function() {
|
||||
const bitfield = new Bitfield(uint8, ['Jack', 'Kack', 'Lack', 'Mack', 'Nack', 'Oack', 'Pack', 'Quack']);
|
||||
const JACK = 1 << 0;
|
||||
const KACK = 1 << 1;
|
||||
const LACK = 1 << 2;
|
||||
const MACK = 1 << 3;
|
||||
const NACK = 1 << 4;
|
||||
const OACK = 1 << 5;
|
||||
const PACK = 1 << 6;
|
||||
const QUACK = 1 << 7;
|
||||
|
||||
it('should have the right size', () => assert.equal(bitfield.size(), 1));
|
||||
|
||||
it('should decode', function() {
|
||||
const buffer = new Uint8Array([JACK | MACK | PACK | NACK | QUACK]);
|
||||
assert.deepEqual(
|
||||
bitfield.fromBuffer(buffer),
|
||||
{Jack: true, Kack: false, Lack: false, Mack: true, Nack: true, Oack: false, Pack: true, Quack: true}
|
||||
);
|
||||
});
|
||||
|
||||
it('should encode', function() {
|
||||
let buffer = bitfield.toBuffer({Jack: true, Kack: false, Lack: false, Mack: true, Nack: true, Oack: false, Pack: true, Quack: true});
|
||||
assert.deepEqual(buffer, Buffer.from([JACK | MACK | PACK | NACK | QUACK]));
|
||||
});
|
||||
});
|
39
node_modules/restructure/test/Boolean.js
generated
vendored
Normal file
39
node_modules/restructure/test/Boolean.js
generated
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
import assert from 'assert';
|
||||
import {Boolean, uint8, DecodeStream, EncodeStream} from 'restructure';
|
||||
|
||||
describe('Boolean', function() {
|
||||
describe('decode', function() {
|
||||
it('should decode 0 as false', function() {
|
||||
const buffer = new Uint8Array([0]);
|
||||
const boolean = new Boolean(uint8);
|
||||
assert.deepEqual(boolean.fromBuffer(buffer), false);
|
||||
});
|
||||
|
||||
it('should decode 1 as true', function() {
|
||||
const buffer = new Uint8Array([1]);
|
||||
const boolean = new Boolean(uint8);
|
||||
assert.deepEqual(boolean.fromBuffer(buffer), true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('size', () =>
|
||||
it('should return given type size', function() {
|
||||
const boolean = new Boolean(uint8);
|
||||
assert.deepEqual(boolean.size(), 1);
|
||||
})
|
||||
);
|
||||
|
||||
describe('encode', function() {
|
||||
it('should encode false as 0', function() {
|
||||
const boolean = new Boolean(uint8);
|
||||
const buffer = boolean.toBuffer(false);
|
||||
assert.deepEqual(buffer, Buffer.from([0]));
|
||||
});
|
||||
|
||||
it('should encode true as 1', function() {
|
||||
const boolean = new Boolean(uint8);
|
||||
const buffer = boolean.toBuffer(true);
|
||||
assert.deepEqual(buffer, Buffer.from([1]));
|
||||
});
|
||||
});
|
||||
});
|
45
node_modules/restructure/test/Buffer.js
generated
vendored
Normal file
45
node_modules/restructure/test/Buffer.js
generated
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
import assert from 'assert';
|
||||
import {Buffer as BufferT, uint8, DecodeStream, EncodeStream} from 'restructure';
|
||||
|
||||
describe('Buffer', function() {
|
||||
describe('decode', function() {
|
||||
it('should decode', function() {
|
||||
const buffer = new Uint8Array([0xab, 0xff]);
|
||||
const buf = new BufferT(2);
|
||||
assert.deepEqual(buf.fromBuffer(buffer), new Uint8Array([0xab, 0xff]));
|
||||
});
|
||||
|
||||
it('should decode with parent key length', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([0xab, 0xff, 0x1f, 0xb6]));
|
||||
const buf = new BufferT('len');
|
||||
assert.deepEqual(buf.decode(stream, {len: 3}), new Uint8Array([0xab, 0xff, 0x1f]));
|
||||
assert.deepEqual(buf.decode(stream, {len: 1}), new Uint8Array([0xb6]));
|
||||
});
|
||||
});
|
||||
|
||||
describe('size', function() {
|
||||
it('should return size', function() {
|
||||
const buf = new BufferT(2);
|
||||
assert.equal(buf.size(new Uint8Array([0xab, 0xff])), 2);
|
||||
});
|
||||
|
||||
it('should use defined length if no value given', function() {
|
||||
const array = new BufferT(10);
|
||||
assert.equal(array.size(), 10);
|
||||
});
|
||||
});
|
||||
|
||||
describe('encode', function() {
|
||||
it('should encode', function() {
|
||||
const buf = new BufferT(2);
|
||||
const buffer = buf.toBuffer(new Uint8Array([0xab, 0xff]));
|
||||
assert.deepEqual(buffer, new Uint8Array([0xab, 0xff]));
|
||||
});
|
||||
|
||||
it('should encode length before buffer', function() {
|
||||
const buf = new BufferT(uint8);
|
||||
const buffer = buf.toBuffer(new Uint8Array([0xab, 0xff]));
|
||||
assert.deepEqual(buffer, new Uint8Array([2, 0xab, 0xff]));
|
||||
});
|
||||
});
|
||||
});
|
101
node_modules/restructure/test/DecodeStream.js
generated
vendored
Normal file
101
node_modules/restructure/test/DecodeStream.js
generated
vendored
Normal file
@@ -0,0 +1,101 @@
|
||||
import {DecodeStream} from 'restructure';
|
||||
import assert from 'assert';
|
||||
|
||||
describe('DecodeStream', function() {
|
||||
it('should read a buffer', function() {
|
||||
const buf = new Uint8Array([1,2,3]);
|
||||
const stream = new DecodeStream(buf);
|
||||
assert.deepEqual(stream.readBuffer(buf.length), new Uint8Array([1,2,3]));
|
||||
});
|
||||
|
||||
it('should readUInt16BE', function() {
|
||||
const buf = new Uint8Array([0xab, 0xcd]);
|
||||
const stream = new DecodeStream(buf);
|
||||
assert.deepEqual(stream.readUInt16BE(), 0xabcd);
|
||||
});
|
||||
|
||||
it('should readUInt16LE', function() {
|
||||
const buf = new Uint8Array([0xab, 0xcd]);
|
||||
const stream = new DecodeStream(buf);
|
||||
assert.deepEqual(stream.readUInt16LE(), 0xcdab);
|
||||
});
|
||||
|
||||
it('should readUInt24BE', function() {
|
||||
const buf = new Uint8Array([0xab, 0xcd, 0xef]);
|
||||
const stream = new DecodeStream(buf);
|
||||
assert.deepEqual(stream.readUInt24BE(), 0xabcdef);
|
||||
});
|
||||
|
||||
it('should readUInt24LE', function() {
|
||||
const buf = new Uint8Array([0xab, 0xcd, 0xef]);
|
||||
const stream = new DecodeStream(buf);
|
||||
assert.deepEqual(stream.readUInt24LE(), 0xefcdab);
|
||||
});
|
||||
|
||||
it('should readInt24BE', function() {
|
||||
const buf = new Uint8Array([0xff, 0xab, 0x24]);
|
||||
const stream = new DecodeStream(buf);
|
||||
assert.deepEqual(stream.readInt24BE(), -21724);
|
||||
});
|
||||
|
||||
it('should readInt24LE', function() {
|
||||
const buf = new Uint8Array([0x24, 0xab, 0xff]);
|
||||
const stream = new DecodeStream(buf);
|
||||
assert.deepEqual(stream.readInt24LE(), -21724);
|
||||
});
|
||||
|
||||
describe('readString', function() {
|
||||
it('should decode ascii by default', function() {
|
||||
const buf = Buffer.from('some text', 'ascii');
|
||||
const stream = new DecodeStream(buf);
|
||||
assert.equal(stream.readString(buf.length), 'some text');
|
||||
});
|
||||
|
||||
it('should decode ascii', function() {
|
||||
const buf = Buffer.from('some text', 'ascii');
|
||||
const stream = new DecodeStream(buf);
|
||||
assert.equal(stream.readString(buf.length, 'ascii'), 'some text');
|
||||
});
|
||||
|
||||
it('should decode utf8', function() {
|
||||
const buf = Buffer.from('unicode! 👍', 'utf8');
|
||||
const stream = new DecodeStream(buf);
|
||||
assert.equal(stream.readString(buf.length, 'utf8'), 'unicode! 👍');
|
||||
});
|
||||
|
||||
it('should decode utf16le', function() {
|
||||
const buf = Buffer.from('unicode! 👍', 'utf16le');
|
||||
const stream = new DecodeStream(buf);
|
||||
assert.equal(stream.readString(buf.length, 'utf16le'), 'unicode! 👍');
|
||||
});
|
||||
|
||||
it('should decode ucs2', function() {
|
||||
const buf = Buffer.from('unicode! 👍', 'ucs2');
|
||||
const stream = new DecodeStream(buf);
|
||||
assert.equal(stream.readString(buf.length, 'ucs2'), 'unicode! 👍');
|
||||
});
|
||||
|
||||
it('should decode utf16be', function() {
|
||||
const buf = Buffer.from('unicode! 👍', 'utf16le');
|
||||
for (let i = 0, end = buf.length - 1; i < end; i += 2) {
|
||||
const byte = buf[i];
|
||||
buf[i] = buf[i + 1];
|
||||
buf[i + 1] = byte;
|
||||
}
|
||||
|
||||
const stream = new DecodeStream(buf);
|
||||
assert.equal(stream.readString(buf.length, 'utf16be'), 'unicode! 👍');
|
||||
});
|
||||
|
||||
it('should decode macroman', function() {
|
||||
const buf = new Uint8Array([0x8a, 0x63, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x64, 0x20, 0x63, 0x68, 0x87, 0x72, 0x61, 0x63, 0x74, 0x65, 0x72, 0x73]);
|
||||
const stream = new DecodeStream(buf);
|
||||
assert.equal(stream.readString(buf.length, 'mac'), 'äccented cháracters');
|
||||
});
|
||||
|
||||
it('should return a buffer for unsupported encodings', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([1, 2, 3]));
|
||||
assert.deepEqual(stream.readString(3, 'unsupported'), new Uint8Array([1, 2, 3]));
|
||||
});
|
||||
});
|
||||
});
|
104
node_modules/restructure/test/EncodeStream.js
generated
vendored
Normal file
104
node_modules/restructure/test/EncodeStream.js
generated
vendored
Normal file
@@ -0,0 +1,104 @@
|
||||
import {EncodeStream} from 'restructure';
|
||||
import assert from 'assert';
|
||||
|
||||
describe('EncodeStream', function() {
|
||||
it('should write a buffer', function() {
|
||||
const stream = new EncodeStream(new Uint8Array(3));
|
||||
stream.writeBuffer(new Uint8Array([1,2,3]));
|
||||
assert.deepEqual(stream.buffer, new Uint8Array([1,2,3]));
|
||||
});
|
||||
|
||||
it('should writeUInt16BE', function() {
|
||||
const stream = new EncodeStream(new Uint8Array(2));
|
||||
stream.writeUInt16BE(0xabcd);
|
||||
assert.deepEqual(stream.buffer, new Uint8Array([0xab, 0xcd]));
|
||||
});
|
||||
|
||||
it('should writeUInt16LE', function() {
|
||||
const stream = new EncodeStream(new Uint8Array(2));
|
||||
stream.writeUInt16LE(0xcdab);
|
||||
assert.deepEqual(stream.buffer, new Uint8Array([0xab, 0xcd]));
|
||||
});
|
||||
|
||||
it('should writeUInt24BE', function() {
|
||||
const stream = new EncodeStream(new Uint8Array(3));
|
||||
stream.writeUInt24BE(0xabcdef);
|
||||
assert.deepEqual(stream.buffer, new Uint8Array([0xab, 0xcd, 0xef]));
|
||||
});
|
||||
|
||||
it('should writeUInt24LE', function() {
|
||||
const stream = new EncodeStream(new Uint8Array(3));
|
||||
stream.writeUInt24LE(0xabcdef);
|
||||
assert.deepEqual(stream.buffer, new Uint8Array([0xef, 0xcd, 0xab]));
|
||||
});
|
||||
|
||||
it('should writeInt24BE', function() {
|
||||
const stream = new EncodeStream(new Uint8Array(6));
|
||||
stream.writeInt24BE(-21724);
|
||||
stream.writeInt24BE(0xabcdef);
|
||||
assert.deepEqual(stream.buffer, new Uint8Array([0xff, 0xab, 0x24, 0xab, 0xcd, 0xef]));
|
||||
});
|
||||
|
||||
it('should writeInt24LE', function() {
|
||||
const stream = new EncodeStream(new Uint8Array(6));
|
||||
stream.writeInt24LE(-21724);
|
||||
stream.writeInt24LE(0xabcdef);
|
||||
assert.deepEqual(stream.buffer, new Uint8Array([0x24, 0xab, 0xff, 0xef, 0xcd, 0xab]));
|
||||
});
|
||||
|
||||
it('should fill', function() {
|
||||
const stream = new EncodeStream(new Uint8Array(5));
|
||||
stream.fill(10, 5);
|
||||
assert.deepEqual(stream.buffer, new Uint8Array([10, 10, 10, 10, 10]));
|
||||
});
|
||||
|
||||
describe('writeString', function() {
|
||||
it('should encode ascii by default', function() {
|
||||
const expected = Buffer.from('some text', 'ascii');
|
||||
const stream = new EncodeStream(new Uint8Array(expected.length));
|
||||
stream.writeString('some text');
|
||||
assert.deepEqual(stream.buffer, expected);
|
||||
});
|
||||
|
||||
it('should encode ascii', function() {
|
||||
const expected = Buffer.from('some text', 'ascii');
|
||||
const stream = new EncodeStream(new Uint8Array(expected.length));
|
||||
stream.writeString('some text', 'ascii');
|
||||
assert.deepEqual(stream.buffer, expected);
|
||||
});
|
||||
|
||||
it('should encode utf8', function() {
|
||||
const expected = Buffer.from('unicode! 👍', 'utf8');
|
||||
const stream = new EncodeStream(new Uint8Array(expected.length));
|
||||
stream.writeString('unicode! 👍', 'utf8');
|
||||
assert.deepEqual(stream.buffer, expected);
|
||||
});
|
||||
|
||||
it('should encode utf16le', function() {
|
||||
const expected = Buffer.from('unicode! 👍', 'utf16le');
|
||||
const stream = new EncodeStream(new Uint8Array(expected.length));
|
||||
stream.writeString('unicode! 👍', 'utf16le');
|
||||
assert.deepEqual(stream.buffer, expected);
|
||||
});
|
||||
|
||||
it('should encode ucs2', function() {
|
||||
const expected = Buffer.from('unicode! 👍', 'ucs2');
|
||||
const stream = new EncodeStream(new Uint8Array(expected.length));
|
||||
stream.writeString('unicode! 👍', 'ucs2');
|
||||
assert.deepEqual(stream.buffer, expected);
|
||||
});
|
||||
|
||||
it('should encode utf16be', function() {
|
||||
const expected = Buffer.from('unicode! 👍', 'utf16le');
|
||||
for (let i = 0, end = expected.length - 1; i < end; i += 2) {
|
||||
const byte = expected[i];
|
||||
expected[i] = expected[i + 1];
|
||||
expected[i + 1] = byte;
|
||||
}
|
||||
|
||||
const stream = new EncodeStream(new Uint8Array(expected.length));
|
||||
stream.writeString('unicode! 👍', 'utf16be');
|
||||
assert.deepEqual(stream.buffer, expected);
|
||||
});
|
||||
});
|
||||
});
|
24
node_modules/restructure/test/Enum.js
generated
vendored
Normal file
24
node_modules/restructure/test/Enum.js
generated
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
import assert from 'assert';
|
||||
import {Enum, uint8, DecodeStream, EncodeStream} from 'restructure';
|
||||
|
||||
describe('Enum', function() {
|
||||
const e = new Enum(uint8, ['foo', 'bar', 'baz']);
|
||||
it('should have the right size', () => assert.equal(e.size(), 1));
|
||||
|
||||
it('should decode', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([1, 2, 0]));
|
||||
assert.equal(e.decode(stream), 'bar');
|
||||
assert.equal(e.decode(stream), 'baz');
|
||||
assert.equal(e.decode(stream), 'foo');
|
||||
});
|
||||
|
||||
it('should encode', function() {
|
||||
assert.deepEqual(e.toBuffer('bar'), new Uint8Array([1]));
|
||||
assert.deepEqual(e.toBuffer('baz'), new Uint8Array([2]));
|
||||
assert.deepEqual(e.toBuffer('foo'), new Uint8Array([0]));
|
||||
});
|
||||
|
||||
it('should throw on unknown option', function() {
|
||||
return assert.throws(() => e.toBuffer('unknown'));
|
||||
});
|
||||
});
|
59
node_modules/restructure/test/LazyArray.js
generated
vendored
Normal file
59
node_modules/restructure/test/LazyArray.js
generated
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
import assert from 'assert';
|
||||
import {LazyArray, Pointer, uint8, uint16, DecodeStream, EncodeStream} from 'restructure';
|
||||
|
||||
describe('LazyArray', function() {
|
||||
describe('decode', function() {
|
||||
it('should decode items lazily', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([1, 2, 3, 4, 5]));
|
||||
const array = new LazyArray(uint8, 4);
|
||||
|
||||
const arr = array.decode(stream);
|
||||
assert(!(arr instanceof Array));
|
||||
assert.equal(arr.length, 4);
|
||||
assert.equal(stream.pos, 4);
|
||||
|
||||
assert.equal(arr.get(0), 1);
|
||||
assert.equal(arr.get(1), 2);
|
||||
assert.equal(arr.get(2), 3);
|
||||
assert.equal(arr.get(3), 4);
|
||||
|
||||
assert.equal(arr.get(-1), null);
|
||||
assert.equal(arr.get(5), null);
|
||||
});
|
||||
|
||||
it('should be able to convert to an array', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([1, 2, 3, 4, 5]));
|
||||
const array = new LazyArray(uint8, 4);
|
||||
|
||||
const arr = array.decode(stream);
|
||||
assert.deepEqual(arr.toArray(), [1, 2, 3, 4]);
|
||||
});
|
||||
|
||||
it('should decode length as number before array', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([4, 1, 2, 3, 4, 5]));
|
||||
const array = new LazyArray(uint8, uint8);
|
||||
const arr = array.decode(stream);
|
||||
|
||||
assert.deepEqual(arr.toArray(), [1, 2, 3, 4]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('size', () =>
|
||||
it('should work with LazyArrays', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([1, 2, 3, 4, 5]));
|
||||
const array = new LazyArray(uint8, 4);
|
||||
const arr = array.decode(stream);
|
||||
|
||||
assert.equal(array.size(arr), 4);
|
||||
})
|
||||
);
|
||||
|
||||
describe('encode', () =>
|
||||
it('should work with LazyArrays', function() {
|
||||
const array = new LazyArray(uint8, 4);
|
||||
const arr = array.fromBuffer(new Uint8Array([1, 2, 3, 4, 5]));
|
||||
const buffer = array.toBuffer(arr);
|
||||
assert.deepEqual(buffer, new Uint8Array([1, 2, 3, 4]));
|
||||
})
|
||||
);
|
||||
});
|
356
node_modules/restructure/test/Number.js
generated
vendored
Normal file
356
node_modules/restructure/test/Number.js
generated
vendored
Normal file
@@ -0,0 +1,356 @@
|
||||
import {
|
||||
uint8,
|
||||
uint16, uint16be, uint16le,
|
||||
uint24, uint24be, uint24le,
|
||||
uint32, uint32be, uint32le,
|
||||
int8,
|
||||
int16, int16be, int16le,
|
||||
int24, int24be, int24le,
|
||||
int32, int32be, int32le,
|
||||
float, floatbe, floatle,
|
||||
double, doublebe, doublele,
|
||||
fixed16, fixed16be, fixed16le,
|
||||
fixed32, fixed32be, fixed32le,
|
||||
DecodeStream, EncodeStream
|
||||
} from 'restructure';
|
||||
import assert from 'assert';
|
||||
|
||||
describe('Number', function() {
|
||||
describe('uint8', function() {
|
||||
it('should decode', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([0xab, 0xff]));
|
||||
assert.equal(uint8.decode(stream), 0xab);
|
||||
assert.equal(uint8.decode(stream), 0xff);
|
||||
});
|
||||
|
||||
it('should have a size', () => assert.equal(uint8.size(), 1));
|
||||
|
||||
it('should encode', function() {
|
||||
assert.deepEqual(uint8.toBuffer(0xab), new Uint8Array([0xab]));
|
||||
assert.deepEqual(uint8.toBuffer(0xff), new Uint8Array([0xff]));
|
||||
});
|
||||
});
|
||||
|
||||
describe('uint16', () =>
|
||||
it('is an alias for uint16be', () => assert.deepEqual(uint16, uint16be))
|
||||
);
|
||||
|
||||
describe('uint16be', function() {
|
||||
it('should decode', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([0xab, 0xff]));
|
||||
assert.equal(uint16be.decode(stream), 0xabff);
|
||||
});
|
||||
|
||||
it('should have a size', () => assert.equal(uint16be.size(), 2));
|
||||
|
||||
it('should encode', function() {
|
||||
assert.deepEqual(uint16be.toBuffer(0xabff), new Uint8Array([0xab, 0xff]));
|
||||
});
|
||||
});
|
||||
|
||||
describe('uint16le', function() {
|
||||
it('should decode', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([0xff, 0xab]));
|
||||
assert.equal(uint16le.decode(stream), 0xabff);
|
||||
});
|
||||
|
||||
it('should have a size', () => assert.equal(uint16le.size(), 2));
|
||||
|
||||
it('should encode', function() {
|
||||
assert.deepEqual(uint16le.toBuffer(0xabff), new Uint8Array([0xff, 0xab]));
|
||||
});
|
||||
});
|
||||
|
||||
describe('uint24', () =>
|
||||
it('is an alias for uint24be', () => assert.deepEqual(uint24, uint24be))
|
||||
);
|
||||
|
||||
describe('uint24be', function() {
|
||||
it('should decode', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([0xff, 0xab, 0x24]));
|
||||
assert.equal(uint24be.decode(stream), 0xffab24);
|
||||
});
|
||||
|
||||
it('should have a size', () => assert.equal(uint24be.size(), 3));
|
||||
|
||||
it('should encode', function() {
|
||||
assert.deepEqual(uint24be.toBuffer(0xffab24), new Uint8Array([0xff, 0xab, 0x24]));
|
||||
});
|
||||
});
|
||||
|
||||
describe('uint24le', function() {
|
||||
it('should decode', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([0x24, 0xab, 0xff]));
|
||||
assert.equal(uint24le.decode(stream), 0xffab24);
|
||||
});
|
||||
|
||||
it('should have a size', () => assert.equal(uint24le.size(), 3));
|
||||
|
||||
it('should encode', function() {
|
||||
assert.deepEqual(uint24le.toBuffer(0xffab24), new Uint8Array([0x24, 0xab, 0xff]));
|
||||
});
|
||||
});
|
||||
|
||||
describe('uint32', () =>
|
||||
it('is an alias for uint32be', () => assert.deepEqual(uint32, uint32be))
|
||||
);
|
||||
|
||||
describe('uint32be', function() {
|
||||
it('should decode', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([0xff, 0xab, 0x24, 0xbf]));
|
||||
assert.equal(uint32be.decode(stream), 0xffab24bf);
|
||||
});
|
||||
|
||||
it('should have a size', () => assert.equal(uint32be.size(), 4));
|
||||
|
||||
it('should encode', function() {
|
||||
assert.deepEqual(uint32be.toBuffer(0xffab24bf), new Uint8Array([0xff, 0xab, 0x24, 0xbf]));
|
||||
});
|
||||
});
|
||||
|
||||
describe('uint32le', function() {
|
||||
it('should decode', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([0xbf, 0x24, 0xab, 0xff]));
|
||||
assert.equal(uint32le.decode(stream), 0xffab24bf);
|
||||
});
|
||||
|
||||
it('should have a size', () => assert.equal(uint32le.size(), 4));
|
||||
|
||||
it('should encode', function() {
|
||||
assert.deepEqual(uint32le.toBuffer(0xffab24bf), new Uint8Array([0xbf, 0x24, 0xab, 0xff]));
|
||||
});
|
||||
});
|
||||
|
||||
describe('int8', function() {
|
||||
it('should decode', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([0x7f, 0xff]));
|
||||
assert.equal(int8.decode(stream), 127);
|
||||
assert.equal(int8.decode(stream), -1);
|
||||
});
|
||||
|
||||
it('should have a size', () => assert.equal(int8.size(), 1));
|
||||
|
||||
it('should encode', function() {
|
||||
assert.deepEqual(uint8.toBuffer(127), new Uint8Array([0x7f]));
|
||||
assert.deepEqual(uint8.toBuffer(-1), new Uint8Array([0xff]));
|
||||
});
|
||||
});
|
||||
|
||||
describe('int16', () =>
|
||||
it('is an alias for int16be', () => assert.deepEqual(int16, int16be))
|
||||
);
|
||||
|
||||
describe('int16be', function() {
|
||||
it('should decode', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([0xff, 0xab]));
|
||||
assert.equal(int16be.decode(stream), -85);
|
||||
});
|
||||
|
||||
it('should have a size', () => assert.equal(int16be.size(), 2));
|
||||
|
||||
it('should encode', function() {
|
||||
assert.deepEqual(int16be.toBuffer(-85), new Uint8Array([0xff, 0xab]));
|
||||
});
|
||||
});
|
||||
|
||||
describe('int16le', function() {
|
||||
it('should decode', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([0xab, 0xff]));
|
||||
assert.equal(int16le.decode(stream), -85);
|
||||
});
|
||||
|
||||
it('should have a size', () => assert.equal(int16le.size(), 2));
|
||||
|
||||
it('should encode', function() {
|
||||
assert.deepEqual(int16le.toBuffer(-85), new Uint8Array([0xab, 0xff]));
|
||||
});
|
||||
});
|
||||
|
||||
describe('int24', () =>
|
||||
it('is an alias for int24be', () => assert.deepEqual(int24, int24be))
|
||||
);
|
||||
|
||||
describe('int24be', function() {
|
||||
it('should decode', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([0xff, 0xab, 0x24]));
|
||||
assert.equal(int24be.decode(stream), -21724);
|
||||
});
|
||||
|
||||
it('should have a size', () => assert.equal(int24be.size(), 3));
|
||||
|
||||
it('should encode', function() {
|
||||
assert.deepEqual(int24be.toBuffer(-21724), new Uint8Array([0xff, 0xab, 0x24]));
|
||||
});
|
||||
});
|
||||
|
||||
describe('int24le', function() {
|
||||
it('should decode', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([0x24, 0xab, 0xff]));
|
||||
assert.equal(int24le.decode(stream), -21724);
|
||||
});
|
||||
|
||||
it('should have a size', () => assert.equal(int24le.size(), 3));
|
||||
|
||||
it('should encode', function() {
|
||||
assert.deepEqual(int24le.toBuffer(-21724), new Uint8Array([0x24, 0xab, 0xff]));
|
||||
});
|
||||
});
|
||||
|
||||
describe('int32', () =>
|
||||
it('is an alias for int32be', () => assert.deepEqual(int32, int32be))
|
||||
);
|
||||
|
||||
describe('int32be', function() {
|
||||
it('should decode', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([0xff, 0xab, 0x24, 0xbf]));
|
||||
assert.equal(int32be.decode(stream), -5561153);
|
||||
});
|
||||
|
||||
it('should have a size', () => assert.equal(int32be.size(), 4));
|
||||
|
||||
it('should encode', function() {
|
||||
assert.deepEqual(int32be.toBuffer(-5561153), new Uint8Array([0xff, 0xab, 0x24, 0xbf]));
|
||||
});
|
||||
});
|
||||
|
||||
describe('int32le', function() {
|
||||
it('should decode', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([0xbf, 0x24, 0xab, 0xff]));
|
||||
assert.equal(int32le.decode(stream), -5561153);
|
||||
});
|
||||
|
||||
it('should have a size', () => assert.equal(int32le.size(), 4));
|
||||
|
||||
it('should encode', function() {
|
||||
assert.deepEqual(int32le.toBuffer(-5561153), new Uint8Array([0xbf, 0x24, 0xab, 0xff]));
|
||||
});
|
||||
});
|
||||
|
||||
describe('float', () =>
|
||||
it('is an alias for floatbe', () => assert.deepEqual(float, floatbe))
|
||||
);
|
||||
|
||||
describe('floatbe', function() {
|
||||
it('should decode', function() {
|
||||
const value = floatbe.fromBuffer(new Uint8Array([0x43, 0x7a, 0x8c, 0xcd]));
|
||||
assert(value >= 250.55 - 0.005);
|
||||
assert(value <= 250.55 + 0.005);
|
||||
});
|
||||
|
||||
it('should have a size', () => assert.equal(floatbe.size(), 4));
|
||||
|
||||
it('should encode', function() {
|
||||
assert.deepEqual(floatbe.toBuffer(250.55), new Uint8Array([0x43, 0x7a, 0x8c, 0xcd]));
|
||||
});
|
||||
});
|
||||
|
||||
describe('floatle', function() {
|
||||
it('should decode', function() {
|
||||
const value = floatle.fromBuffer(new Uint8Array([0xcd, 0x8c, 0x7a, 0x43]));
|
||||
assert(value >= 250.55 - 0.005);
|
||||
assert(value <= 250.55 + 0.005);
|
||||
});
|
||||
|
||||
it('should have a size', () => assert.equal(floatle.size(), 4));
|
||||
|
||||
it('should encode', function() {
|
||||
assert.deepEqual(floatle.toBuffer(250.55), new Uint8Array([0xcd, 0x8c, 0x7a, 0x43]));
|
||||
});
|
||||
});
|
||||
|
||||
describe('double', () =>
|
||||
it('is an alias for doublebe', () => assert.deepEqual(double, doublebe))
|
||||
);
|
||||
|
||||
describe('doublebe', function() {
|
||||
it('should decode', function() {
|
||||
const value = doublebe.fromBuffer(new Uint8Array([0x40, 0x93, 0x4a, 0x3d, 0x70, 0xa3, 0xd7, 0x0a]));
|
||||
assert(value >= 1234.56 - 0.005);
|
||||
assert(value <= 1234.56 + 0.005);
|
||||
});
|
||||
|
||||
it('should have a size', () => assert.equal(doublebe.size(), 8));
|
||||
|
||||
it('should encode', function() {
|
||||
assert.deepEqual(doublebe.toBuffer(1234.56), new Uint8Array([0x40, 0x93, 0x4a, 0x3d, 0x70, 0xa3, 0xd7, 0x0a]));
|
||||
});
|
||||
});
|
||||
|
||||
describe('doublele', function() {
|
||||
it('should decode', function() {
|
||||
const value = doublele.fromBuffer(new Uint8Array([0x0a, 0xd7, 0xa3, 0x70, 0x3d, 0x4a, 0x93, 0x40]));
|
||||
assert(value >= 1234.56 - 0.005);
|
||||
assert(value <= 1234.56 + 0.005);
|
||||
});
|
||||
|
||||
it('should have a size', () => assert.equal(doublele.size(), 8));
|
||||
|
||||
it('should encode', function() {
|
||||
assert.deepEqual(doublele.toBuffer(1234.56), new Uint8Array([0x0a, 0xd7, 0xa3, 0x70, 0x3d, 0x4a, 0x93, 0x40]));
|
||||
});
|
||||
});
|
||||
|
||||
describe('fixed16', () =>
|
||||
it('is an alias for fixed16be', () => assert.deepEqual(fixed16, fixed16be))
|
||||
);
|
||||
|
||||
describe('fixed16be', function() {
|
||||
it('should decode', function() {
|
||||
const value = fixed16be.fromBuffer(new Uint8Array([0x19, 0x57]));
|
||||
assert(value >= 25.34 - 0.005);
|
||||
assert(value <= 25.34 + 0.005);
|
||||
});
|
||||
|
||||
it('should have a size', () => assert.equal(fixed16be.size(), 2));
|
||||
|
||||
it('should encode', function() {
|
||||
assert.deepEqual(fixed16be.toBuffer(25.34), new Uint8Array([0x19, 0x57]));
|
||||
});
|
||||
});
|
||||
|
||||
describe('fixed16le', function() {
|
||||
it('should decode', function() {
|
||||
const value = fixed16le.fromBuffer(new Uint8Array([0x57, 0x19]));
|
||||
assert(value >= 25.34 - 0.005);
|
||||
assert(value <= 25.34 + 0.005);
|
||||
});
|
||||
|
||||
it('should have a size', () => assert.equal(fixed16le.size(), 2));
|
||||
|
||||
it('should encode', function() {
|
||||
assert.deepEqual(fixed16le.toBuffer(25.34), new Uint8Array([0x57, 0x19]));
|
||||
});
|
||||
});
|
||||
|
||||
describe('fixed32', () =>
|
||||
it('is an alias for fixed32be', () => assert.deepEqual(fixed32, fixed32be))
|
||||
);
|
||||
|
||||
describe('fixed32be', function() {
|
||||
it('should decode', function() {
|
||||
const value = fixed32be.fromBuffer(new Uint8Array([0x00, 0xfa, 0x8c, 0xcc]));
|
||||
assert(value >= 250.55 - 0.005);
|
||||
assert(value <= 250.55 + 0.005);
|
||||
});
|
||||
|
||||
it('should have a size', () => assert.equal(fixed32be.size(), 4));
|
||||
|
||||
it('should encode', function() {
|
||||
assert.deepEqual(fixed32be.toBuffer(250.55), new Uint8Array([0x00, 0xfa, 0x8c, 0xcc]));
|
||||
});
|
||||
});
|
||||
|
||||
describe('fixed32le', function() {
|
||||
it('should decode', function() {
|
||||
const value = fixed32le.fromBuffer(new Uint8Array([0xcc, 0x8c, 0xfa, 0x00]));
|
||||
assert(value >= 250.55 - 0.005);
|
||||
assert(value <= 250.55 + 0.005);
|
||||
});
|
||||
|
||||
it('should have a size', () => assert.equal(fixed32le.size(), 4));
|
||||
|
||||
it('should encode', function() {
|
||||
assert.deepEqual(fixed32le.toBuffer(250.55), new Uint8Array([0xcc, 0x8c, 0xfa, 0x00]));
|
||||
});
|
||||
});
|
||||
});
|
100
node_modules/restructure/test/Optional.js
generated
vendored
Normal file
100
node_modules/restructure/test/Optional.js
generated
vendored
Normal file
@@ -0,0 +1,100 @@
|
||||
import assert from 'assert';
|
||||
import {Optional, uint8, DecodeStream, EncodeStream} from 'restructure';
|
||||
|
||||
describe('Optional', function() {
|
||||
describe('decode', function() {
|
||||
it('should not decode when condition is falsy', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([0]));
|
||||
const optional = new Optional(uint8, false);
|
||||
assert.equal(optional.decode(stream), null);
|
||||
assert.equal(stream.pos, 0);
|
||||
});
|
||||
|
||||
it('should not decode when condition is a function and falsy', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([0]));
|
||||
const optional = new Optional(uint8, function() { return false; });
|
||||
assert.equal(optional.decode(stream), null);
|
||||
assert.equal(stream.pos, 0);
|
||||
});
|
||||
|
||||
it('should decode when condition is omitted', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([0]));
|
||||
const optional = new Optional(uint8);
|
||||
assert(optional.decode(stream) != null);
|
||||
assert.equal(stream.pos, 1);
|
||||
});
|
||||
|
||||
it('should decode when condition is truthy', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([0]));
|
||||
const optional = new Optional(uint8, true);
|
||||
assert(optional.decode(stream) != null);
|
||||
assert.equal(stream.pos, 1);
|
||||
});
|
||||
|
||||
it('should decode when condition is a function and truthy', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([0]));
|
||||
const optional = new Optional(uint8, function() { return true; });
|
||||
assert(optional.decode(stream) != null);
|
||||
assert.equal(stream.pos, 1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('size', function() {
|
||||
it('should return 0 when condition is falsy', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([0]));
|
||||
const optional = new Optional(uint8, false);
|
||||
assert.equal(optional.size(), 0);
|
||||
});
|
||||
|
||||
it('should return 0 when condition is a function and falsy', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([0]));
|
||||
const optional = new Optional(uint8, function() { return false; });
|
||||
assert.equal(optional.size(), 0);
|
||||
});
|
||||
|
||||
it('should return given type size when condition is omitted', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([0]));
|
||||
const optional = new Optional(uint8);
|
||||
assert.equal(optional.size(), 1);
|
||||
});
|
||||
|
||||
it('should return given type size when condition is truthy', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([0]));
|
||||
const optional = new Optional(uint8, true);
|
||||
assert.equal(optional.size(), 1);
|
||||
});
|
||||
|
||||
it('should return given type size when condition is a function and truthy', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([0]));
|
||||
const optional = new Optional(uint8, function() { return true; });
|
||||
assert.equal(optional.size(), 1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('encode', function() {
|
||||
it('should not encode when condition is falsy', function() {
|
||||
const optional = new Optional(uint8, false);
|
||||
assert.deepEqual(optional.toBuffer(128), new Uint8Array(0));
|
||||
});
|
||||
|
||||
it('should not encode when condition is a function and falsy', function() {
|
||||
const optional = new Optional(uint8, function() { return false; });
|
||||
assert.deepEqual(optional.toBuffer(128), new Uint8Array(0));
|
||||
});
|
||||
|
||||
it('should encode when condition is omitted', function() {
|
||||
const optional = new Optional(uint8);
|
||||
assert.deepEqual(optional.toBuffer(128), new Uint8Array([128]));
|
||||
});
|
||||
|
||||
it('should encode when condition is truthy', function() {
|
||||
const optional = new Optional(uint8, true);
|
||||
assert.deepEqual(optional.toBuffer(128), new Uint8Array([128]));
|
||||
});
|
||||
|
||||
it('should encode when condition is a function and truthy', function() {
|
||||
const optional = new Optional(uint8, function() { return true; });
|
||||
assert.deepEqual(optional.toBuffer(128), new Uint8Array([128]));
|
||||
});
|
||||
});
|
||||
});
|
289
node_modules/restructure/test/Pointer.js
generated
vendored
Normal file
289
node_modules/restructure/test/Pointer.js
generated
vendored
Normal file
@@ -0,0 +1,289 @@
|
||||
import assert from 'assert';
|
||||
import {Pointer, VoidPointer, uint8, DecodeStream, EncodeStream, Struct} from 'restructure';
|
||||
|
||||
describe('Pointer', function() {
|
||||
describe('decode', function() {
|
||||
it('should handle null pointers', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([0]));
|
||||
const pointer = new Pointer(uint8, uint8);
|
||||
return assert.equal(pointer.decode(stream, {_startOffset: 50}), null);
|
||||
});
|
||||
|
||||
it('should use local offsets from start of parent by default', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([1, 53]));
|
||||
const pointer = new Pointer(uint8, uint8);
|
||||
assert.equal(pointer.decode(stream, {_startOffset: 0}), 53);
|
||||
});
|
||||
|
||||
it('should support immediate offsets', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([1, 53]));
|
||||
const pointer = new Pointer(uint8, uint8, {type: 'immediate'});
|
||||
assert.equal(pointer.decode(stream), 53);
|
||||
});
|
||||
|
||||
it('should support offsets relative to the parent', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([0, 0, 1, 53]));
|
||||
stream.pos = 2;
|
||||
const pointer = new Pointer(uint8, uint8, {type: 'parent'});
|
||||
assert.equal(pointer.decode(stream, {parent: {_startOffset: 2}}), 53);
|
||||
});
|
||||
|
||||
it('should support global offsets', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([1, 2, 4, 0, 0, 0, 53]));
|
||||
const pointer = new Pointer(uint8, uint8, {type: 'global'});
|
||||
stream.pos = 2;
|
||||
assert.equal(pointer.decode(stream, {parent: {parent: {_startOffset: 2}}}), 53);
|
||||
});
|
||||
|
||||
it('should support offsets relative to a property on the parent', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([1, 0, 0, 0, 0, 53]));
|
||||
const pointer = new Pointer(uint8, uint8, {relativeTo: ctx => ctx.parent.ptr});
|
||||
assert.equal(pointer.decode(stream, {_startOffset: 0, parent: {ptr: 4}}), 53);
|
||||
});
|
||||
|
||||
it('should throw when passing a non function relativeTo option', function() {
|
||||
return assert.throws(() => new Pointer(uint8, uint8, {relativeTo: 'parent.ptr'}));
|
||||
});
|
||||
|
||||
it('should support returning pointer if there is no decode type', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([4]));
|
||||
const pointer = new Pointer(uint8, 'void');
|
||||
assert.equal(pointer.decode(stream, {_startOffset: 0}), 4);
|
||||
});
|
||||
|
||||
it('should support decoding pointers lazily', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([1, 53]));
|
||||
const struct = new Struct({
|
||||
ptr: new Pointer(uint8, uint8, {lazy: true})});
|
||||
|
||||
const res = struct.decode(stream);
|
||||
assert.equal(typeof Object.getOwnPropertyDescriptor(res, 'ptr').get, 'function');
|
||||
assert.equal(Object.getOwnPropertyDescriptor(res, 'ptr').enumerable, true);
|
||||
assert.equal(res.ptr, 53);
|
||||
});
|
||||
});
|
||||
|
||||
describe('size', function() {
|
||||
it('should add to local pointerSize', function() {
|
||||
const pointer = new Pointer(uint8, uint8);
|
||||
const ctx = {pointerSize: 0};
|
||||
assert.equal(pointer.size(10, ctx), 1);
|
||||
assert.equal(ctx.pointerSize, 1);
|
||||
});
|
||||
|
||||
it('should add to immediate pointerSize', function() {
|
||||
const pointer = new Pointer(uint8, uint8, {type: 'immediate'});
|
||||
const ctx = {pointerSize: 0};
|
||||
assert.equal(pointer.size(10, ctx), 1);
|
||||
assert.equal(ctx.pointerSize, 1);
|
||||
});
|
||||
|
||||
it('should add to parent pointerSize', function() {
|
||||
const pointer = new Pointer(uint8, uint8, {type: 'parent'});
|
||||
const ctx = {parent: {pointerSize: 0}};
|
||||
assert.equal(pointer.size(10, ctx), 1);
|
||||
assert.equal(ctx.parent.pointerSize, 1);
|
||||
});
|
||||
|
||||
it('should add to global pointerSize', function() {
|
||||
const pointer = new Pointer(uint8, uint8, {type: 'global'});
|
||||
const ctx = {parent: {parent: {parent: {pointerSize: 0}}}};
|
||||
assert.equal(pointer.size(10, ctx), 1);
|
||||
assert.equal(ctx.parent.parent.parent.pointerSize, 1);
|
||||
});
|
||||
|
||||
it('should handle void pointers', function() {
|
||||
const pointer = new Pointer(uint8, 'void');
|
||||
const ctx = {pointerSize: 0};
|
||||
assert.equal(pointer.size(new VoidPointer(uint8, 50), ctx), 1);
|
||||
assert.equal(ctx.pointerSize, 1);
|
||||
});
|
||||
|
||||
it('should throw if no type and not a void pointer', function() {
|
||||
const pointer = new Pointer(uint8, 'void');
|
||||
const ctx = {pointerSize: 0};
|
||||
assert.throws(() => pointer.size(30, ctx));
|
||||
});
|
||||
|
||||
it('should return a fixed size without a value', function() {
|
||||
const pointer = new Pointer(uint8, uint8);
|
||||
assert.equal(pointer.size(), 1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('encode', function() {
|
||||
it('should handle null pointers', function() {
|
||||
const ptr = new Pointer(uint8, uint8);
|
||||
const ctx = {
|
||||
pointerSize: 0,
|
||||
startOffset: 0,
|
||||
pointerOffset: 0,
|
||||
pointers: []
|
||||
};
|
||||
|
||||
const stream = new EncodeStream(new Uint8Array(ptr.size(null)));
|
||||
ptr.encode(stream, null, ctx);
|
||||
assert.equal(ctx.pointerSize, 0);
|
||||
|
||||
assert.deepEqual(stream.buffer, new Uint8Array([0]));
|
||||
});
|
||||
|
||||
it('should handle local offsets', function() {
|
||||
const ptr = new Pointer(uint8, uint8);
|
||||
const ctx = {
|
||||
pointerSize: 0,
|
||||
startOffset: 0,
|
||||
pointerOffset: 1,
|
||||
pointers: []
|
||||
};
|
||||
|
||||
const stream = new EncodeStream(new Uint8Array(ptr.size(10)));
|
||||
ptr.encode(stream, 10, ctx);
|
||||
assert.equal(ctx.pointerOffset, 2);
|
||||
assert.deepEqual(ctx.pointers, [
|
||||
{ type: uint8, val: 10, parent: ctx }
|
||||
]);
|
||||
|
||||
assert.deepEqual(stream.buffer, new Uint8Array([1]));
|
||||
});
|
||||
|
||||
it('should handle immediate offsets', function() {
|
||||
const ptr = new Pointer(uint8, uint8, {type: 'immediate'});
|
||||
const ctx = {
|
||||
pointerSize: 0,
|
||||
startOffset: 0,
|
||||
pointerOffset: 1,
|
||||
pointers: []
|
||||
};
|
||||
|
||||
const stream = new EncodeStream(new Uint8Array(ptr.size(10)));
|
||||
ptr.encode(stream, 10, ctx);
|
||||
assert.equal(ctx.pointerOffset, 2);
|
||||
assert.deepEqual(ctx.pointers, [
|
||||
{ type: uint8, val: 10, parent: ctx }
|
||||
]);
|
||||
|
||||
assert.deepEqual(stream.buffer, new Uint8Array([0]));
|
||||
});
|
||||
|
||||
it('should handle immediate offsets', function() {
|
||||
const ptr = new Pointer(uint8, uint8, {type: 'immediate'});
|
||||
const ctx = {
|
||||
pointerSize: 0,
|
||||
startOffset: 0,
|
||||
pointerOffset: 1,
|
||||
pointers: []
|
||||
};
|
||||
|
||||
const stream = new EncodeStream(new Uint8Array(ptr.size(10)));
|
||||
ptr.encode(stream, 10, ctx);
|
||||
assert.equal(ctx.pointerOffset, 2);
|
||||
assert.deepEqual(ctx.pointers, [
|
||||
{ type: uint8, val: 10, parent: ctx }
|
||||
]);
|
||||
|
||||
assert.deepEqual(stream.buffer, new Uint8Array([0]));
|
||||
});
|
||||
|
||||
it('should handle offsets relative to parent', function() {
|
||||
const ptr = new Pointer(uint8, uint8, {type: 'parent'});
|
||||
const ctx = {
|
||||
parent: {
|
||||
pointerSize: 0,
|
||||
startOffset: 3,
|
||||
pointerOffset: 5,
|
||||
pointers: []
|
||||
}
|
||||
};
|
||||
|
||||
const stream = new EncodeStream(new Uint8Array(ptr.size(10, {parent: {...ctx.parent}})));
|
||||
ptr.encode(stream, 10, ctx);
|
||||
assert.equal(ctx.parent.pointerOffset, 6);
|
||||
assert.deepEqual(ctx.parent.pointers, [
|
||||
{ type: uint8, val: 10, parent: ctx }
|
||||
]);
|
||||
|
||||
assert.deepEqual(stream.buffer, new Uint8Array([2]));
|
||||
});
|
||||
|
||||
it('should handle global offsets', function() {
|
||||
const ptr = new Pointer(uint8, uint8, {type: 'global'});
|
||||
const ctx = {
|
||||
parent: {
|
||||
parent: {
|
||||
parent: {
|
||||
pointerSize: 0,
|
||||
startOffset: 3,
|
||||
pointerOffset: 5,
|
||||
pointers: []
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const stream = new EncodeStream(new Uint8Array(ptr.size(10, JSON.parse(JSON.stringify(ctx)))));
|
||||
ptr.encode(stream, 10, ctx);
|
||||
assert.equal(ctx.parent.parent.parent.pointerOffset, 6);
|
||||
assert.deepEqual(ctx.parent.parent.parent.pointers, [
|
||||
{ type: uint8, val: 10, parent: ctx }
|
||||
]);
|
||||
|
||||
assert.deepEqual(stream.buffer, new Uint8Array([5]));
|
||||
});
|
||||
|
||||
it('should support offsets relative to a property on the parent', function() {
|
||||
const ptr = new Pointer(uint8, uint8, {relativeTo: ctx => ctx.ptr});
|
||||
const ctx = {
|
||||
pointerSize: 0,
|
||||
startOffset: 0,
|
||||
pointerOffset: 10,
|
||||
pointers: [],
|
||||
val: {
|
||||
ptr: 4
|
||||
}
|
||||
};
|
||||
|
||||
const stream = new EncodeStream(new Uint8Array(ptr.size(10, {...ctx})));
|
||||
ptr.encode(stream, 10, ctx);
|
||||
assert.equal(ctx.pointerOffset, 11);
|
||||
assert.deepEqual(ctx.pointers, [
|
||||
{ type: uint8, val: 10, parent: ctx }
|
||||
]);
|
||||
|
||||
assert.deepEqual(stream.buffer, new Uint8Array([6]));
|
||||
});
|
||||
|
||||
it('should support void pointers', function() {
|
||||
const ptr = new Pointer(uint8, 'void');
|
||||
const ctx = {
|
||||
pointerSize: 0,
|
||||
startOffset: 0,
|
||||
pointerOffset: 1,
|
||||
pointers: []
|
||||
};
|
||||
|
||||
const val = new VoidPointer(uint8, 55);
|
||||
const stream = new EncodeStream(new Uint8Array(ptr.size(val, {...ctx})));
|
||||
ptr.encode(stream, val, ctx);
|
||||
assert.equal(ctx.pointerOffset, 2);
|
||||
assert.deepEqual(ctx.pointers, [
|
||||
{ type: uint8, val: 55, parent: ctx }
|
||||
]);
|
||||
|
||||
assert.deepEqual(stream.buffer, new Uint8Array([1]));
|
||||
});
|
||||
|
||||
it('should throw if not a void pointer instance', function() {
|
||||
const ptr = new Pointer(uint8, 'void');
|
||||
const ctx = {
|
||||
pointerSize: 0,
|
||||
startOffset: 0,
|
||||
pointerOffset: 1,
|
||||
pointers: []
|
||||
};
|
||||
|
||||
const stream = new EncodeStream(new Uint8Array(0));
|
||||
assert.throws(() => ptr.encode(stream, 44, ctx));
|
||||
});
|
||||
});
|
||||
});
|
26
node_modules/restructure/test/Reserved.js
generated
vendored
Normal file
26
node_modules/restructure/test/Reserved.js
generated
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
import assert from 'assert';
|
||||
import {Reserved, uint8, uint16, DecodeStream, EncodeStream} from 'restructure';
|
||||
|
||||
describe('Reserved', function() {
|
||||
it('should have a default count of 1', function() {
|
||||
const reserved = new Reserved(uint8);
|
||||
assert.equal(reserved.size(), 1);
|
||||
});
|
||||
|
||||
it('should allow custom counts and types', function() {
|
||||
const reserved = new Reserved(uint16, 10);
|
||||
assert.equal(reserved.size(), 20);
|
||||
});
|
||||
|
||||
it('should decode', function() {
|
||||
const stream = new DecodeStream(new Uint8Array([0, 0]));
|
||||
const reserved = new Reserved(uint16);
|
||||
assert.equal(reserved.decode(stream), null);
|
||||
assert.equal(stream.pos, 2);
|
||||
});
|
||||
|
||||
it('should encode', function() {
|
||||
const reserved = new Reserved(uint16);
|
||||
assert.deepEqual(reserved.toBuffer(), new Uint8Array([0, 0]));
|
||||
});
|
||||
});
|
151
node_modules/restructure/test/String.js
generated
vendored
Normal file
151
node_modules/restructure/test/String.js
generated
vendored
Normal file
@@ -0,0 +1,151 @@
|
||||
import assert from 'assert';
|
||||
import {String as StringT, uint16le, uint8, DecodeStream, Struct} from 'restructure';
|
||||
|
||||
describe('String', function() {
|
||||
describe('decode', function() {
|
||||
it('should decode fixed length', function() {
|
||||
const string = new StringT(7);
|
||||
assert.equal(string.fromBuffer(Buffer.from('testing')), 'testing');
|
||||
});
|
||||
|
||||
it('should decode length from parent key', function() {
|
||||
const stream = new DecodeStream(Buffer.from('testing'));
|
||||
const string = new StringT('len');
|
||||
assert.equal(string.decode(stream, {len: 7}), 'testing');
|
||||
});
|
||||
|
||||
it('should decode length as number before string', function() {
|
||||
const string = new StringT(uint8);
|
||||
assert.equal(string.fromBuffer(Buffer.from('\x07testing')), 'testing');
|
||||
});
|
||||
|
||||
it('should decode utf8', function() {
|
||||
const string = new StringT(4, 'utf8');
|
||||
assert.equal(string.fromBuffer(Buffer.from('🍻')), '🍻');
|
||||
});
|
||||
|
||||
it('should decode encoding computed from function', function() {
|
||||
const string = new StringT(4, function() { return 'utf8'; });
|
||||
assert.equal(string.fromBuffer(Buffer.from('🍻')), '🍻');
|
||||
});
|
||||
|
||||
it('should decode null-terminated string and read past terminator', function() {
|
||||
const stream = new DecodeStream(Buffer.from('🍻\x00'));
|
||||
const string = new StringT(null, 'utf8');
|
||||
assert.equal(string.decode(stream), '🍻');
|
||||
assert.equal(stream.pos, 5);
|
||||
});
|
||||
|
||||
it('should decode remainder of buffer when null-byte missing', function() {
|
||||
const string = new StringT(null, 'utf8');
|
||||
assert.equal(string.fromBuffer(Buffer.from('🍻')), '🍻');
|
||||
});
|
||||
|
||||
it('should decode two-byte null-terminated string for utf16le', function() {
|
||||
const stream = new DecodeStream(Buffer.from('🍻\x00', 'utf16le'));
|
||||
const string = new StringT(null, 'utf16le');
|
||||
assert.equal(string.decode(stream), '🍻');
|
||||
assert.equal(stream.pos, 6);
|
||||
});
|
||||
|
||||
it('should decode remainder of buffer when null-byte missing, utf16le', function() {
|
||||
const string = new StringT(null, 'utf16le');
|
||||
assert.equal(string.fromBuffer(Buffer.from('🍻', 'utf16le')), '🍻');
|
||||
});
|
||||
|
||||
it('should decode x-mac-roman', function() {
|
||||
const string = new StringT(null, 'x-mac-roman');
|
||||
const buf = new Uint8Array([0x8a, 0x63, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x64, 0x20, 0x63, 0x68, 0x87, 0x72, 0x61, 0x63, 0x74, 0x65, 0x72, 0x73]);
|
||||
assert.equal(string.fromBuffer(buf), 'äccented cháracters');
|
||||
})
|
||||
});
|
||||
|
||||
describe('size', function() {
|
||||
it('should use string length', function() {
|
||||
const string = new StringT(7);
|
||||
assert.equal(string.size('testing'), 7);
|
||||
});
|
||||
|
||||
it('should use correct encoding', function() {
|
||||
const string = new StringT(10, 'utf8');
|
||||
assert.equal(string.size('🍻'), 4);
|
||||
});
|
||||
|
||||
it('should use encoding from function', function() {
|
||||
const string = new StringT(10, function() { return 'utf8'; });
|
||||
assert.equal(string.size('🍻'), 4);
|
||||
});
|
||||
|
||||
it('should add size of length field before string', function() {
|
||||
const string = new StringT(uint8, 'utf8');
|
||||
assert.equal(string.size('🍻'), 5);
|
||||
});
|
||||
|
||||
it('should work with utf16be encoding', function() {
|
||||
const string = new StringT(10, 'utf16be');
|
||||
assert.equal(string.size('🍻'), 4);
|
||||
});
|
||||
|
||||
it('should take null-byte into account', function() {
|
||||
const string = new StringT(null, 'utf8');
|
||||
assert.equal(string.size('🍻'), 5);
|
||||
});
|
||||
|
||||
it('should take null-byte into account, utf16le', function() {
|
||||
const string = new StringT(null, 'utf16le');
|
||||
assert.equal(string.size('🍻'), 6);
|
||||
});
|
||||
|
||||
it('should use defined length if no value given', function() {
|
||||
const array = new StringT(10);
|
||||
assert.equal(array.size(), 10);
|
||||
});
|
||||
});
|
||||
|
||||
describe('encode', function() {
|
||||
it('should encode using string length', function() {
|
||||
const string = new StringT(7);
|
||||
assert.deepEqual(string.toBuffer('testing'), Buffer.from('testing'));
|
||||
});
|
||||
|
||||
it('should encode length as number before string', function() {
|
||||
const string = new StringT(uint8);
|
||||
assert.deepEqual(string.toBuffer('testing'), Buffer.from('\x07testing'));
|
||||
});
|
||||
|
||||
it('should encode length as number before string utf8', function() {
|
||||
const string = new StringT(uint8, 'utf8');
|
||||
assert.deepEqual(string.toBuffer('testing 😜'), Buffer.from('\x0ctesting 😜', 'utf8'));
|
||||
});
|
||||
|
||||
it('should encode utf8', function() {
|
||||
const string = new StringT(4, 'utf8');
|
||||
assert.deepEqual(string.toBuffer('🍻'), Buffer.from('🍻'));
|
||||
});
|
||||
|
||||
it('should encode encoding computed from function', function() {
|
||||
const string = new StringT(4, function() { return 'utf8'; });
|
||||
assert.deepEqual(string.toBuffer('🍻'), Buffer.from('🍻'));
|
||||
});
|
||||
|
||||
it('should encode null-terminated string', function() {
|
||||
const string = new StringT(null, 'utf8');
|
||||
assert.deepEqual(string.toBuffer('🍻'), Buffer.from('🍻\x00'));
|
||||
});
|
||||
|
||||
it('should encode using string length, utf16le', function() {
|
||||
const string = new StringT(16, 'utf16le');
|
||||
assert.deepEqual(string.toBuffer('testing'), Buffer.from('testing', 'utf16le'));
|
||||
});
|
||||
|
||||
it('should encode length as number before string utf16le', function() {
|
||||
const string = new StringT(uint16le, 'utf16le');
|
||||
assert.deepEqual(string.toBuffer('testing 😜'), Buffer.from('\u0014testing 😜', 'utf16le'));
|
||||
});
|
||||
|
||||
it('should encode two-byte null-terminated string for UTF-16', function() {
|
||||
const string = new StringT(null, 'utf16le');
|
||||
assert.deepEqual(string.toBuffer('🍻'), Buffer.from('🍻\x00', 'utf16le'));
|
||||
});
|
||||
});
|
||||
});
|
146
node_modules/restructure/test/Struct.js
generated
vendored
Normal file
146
node_modules/restructure/test/Struct.js
generated
vendored
Normal file
@@ -0,0 +1,146 @@
|
||||
import assert from 'assert';
|
||||
import {Struct, String as StringT, Pointer, uint8, DecodeStream, EncodeStream} from 'restructure';
|
||||
|
||||
describe('Struct', function() {
|
||||
describe('decode', function() {
|
||||
it('should decode into an object', function() {
|
||||
const struct = new Struct({
|
||||
name: new StringT(uint8),
|
||||
age: uint8
|
||||
});
|
||||
|
||||
assert.deepEqual(struct.fromBuffer(Buffer.from('\x05devon\x15')), {
|
||||
name: 'devon',
|
||||
age: 21
|
||||
});
|
||||
});
|
||||
|
||||
it('should support process hook', function() {
|
||||
const struct = new Struct({
|
||||
name: new StringT(uint8),
|
||||
age: uint8
|
||||
});
|
||||
|
||||
struct.process = function() {
|
||||
return this.canDrink = this.age >= 21;
|
||||
};
|
||||
|
||||
assert.deepEqual(struct.fromBuffer(Buffer.from('\x05devon\x20')), {
|
||||
name: 'devon',
|
||||
age: 32,
|
||||
canDrink: true
|
||||
});
|
||||
});
|
||||
|
||||
it('should support function keys', function() {
|
||||
const struct = new Struct({
|
||||
name: new StringT(uint8),
|
||||
age: uint8,
|
||||
canDrink() { return this.age >= 21; }
|
||||
});
|
||||
|
||||
assert.deepEqual(struct.fromBuffer(Buffer.from('\x05devon\x20')), {
|
||||
name: 'devon',
|
||||
age: 32,
|
||||
canDrink: true
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('size', function() {
|
||||
it('should compute the correct size', function() {
|
||||
const struct = new Struct({
|
||||
name: new StringT(uint8),
|
||||
age: uint8
|
||||
});
|
||||
|
||||
assert.equal(struct.size({name: 'devon', age: 21}), 7);
|
||||
});
|
||||
|
||||
it('should compute the correct size with pointers', function() {
|
||||
const struct = new Struct({
|
||||
name: new StringT(uint8),
|
||||
age: uint8,
|
||||
ptr: new Pointer(uint8, new StringT(uint8))
|
||||
});
|
||||
|
||||
const size = struct.size({
|
||||
name: 'devon',
|
||||
age: 21,
|
||||
ptr: 'hello'
|
||||
});
|
||||
|
||||
assert.equal(size, 14);
|
||||
});
|
||||
|
||||
it('should get the correct size when no value is given', function() {
|
||||
const struct = new Struct({
|
||||
name: new StringT(4),
|
||||
age: uint8
|
||||
});
|
||||
|
||||
assert.equal(struct.size(), 5);
|
||||
});
|
||||
|
||||
it('should throw when getting non-fixed length size and no value is given', function() {
|
||||
const struct = new Struct({
|
||||
name: new StringT(uint8),
|
||||
age: uint8
|
||||
});
|
||||
|
||||
assert.throws(() => struct.size(), /not a fixed size/i);
|
||||
});
|
||||
});
|
||||
|
||||
describe('encode', function() {
|
||||
it('should encode objects to buffers', function() {
|
||||
const struct = new Struct({
|
||||
name: new StringT(uint8),
|
||||
age: uint8
|
||||
});
|
||||
|
||||
const buf = struct.toBuffer({
|
||||
name: 'devon',
|
||||
age: 21
|
||||
});
|
||||
|
||||
assert.deepEqual(buf, Buffer.from('\x05devon\x15'));
|
||||
});
|
||||
|
||||
it('should support preEncode hook', function() {
|
||||
const struct = new Struct({
|
||||
nameLength: uint8,
|
||||
name: new StringT('nameLength'),
|
||||
age: uint8
|
||||
});
|
||||
|
||||
struct.preEncode = function() {
|
||||
return this.nameLength = this.name.length;
|
||||
};
|
||||
|
||||
const buf = struct.toBuffer({
|
||||
name: 'devon',
|
||||
age: 21
|
||||
});
|
||||
|
||||
assert.deepEqual(buf, Buffer.from('\x05devon\x15'));
|
||||
});
|
||||
|
||||
it('should encode pointer data after structure', function() {
|
||||
const struct = new Struct({
|
||||
name: new StringT(uint8),
|
||||
age: uint8,
|
||||
ptr: new Pointer(uint8, new StringT(uint8))
|
||||
});
|
||||
|
||||
const buf = struct.toBuffer({
|
||||
name: 'devon',
|
||||
age: 21,
|
||||
ptr: 'hello'
|
||||
});
|
||||
|
||||
assert.deepEqual(buf, Buffer.from('\x05devon\x15\x08\x05hello'));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
495
node_modules/restructure/test/VersionedStruct.js
generated
vendored
Normal file
495
node_modules/restructure/test/VersionedStruct.js
generated
vendored
Normal file
@@ -0,0 +1,495 @@
|
||||
import assert from 'assert';
|
||||
import {VersionedStruct, String as StringT, Pointer, uint8, DecodeStream, EncodeStream} from 'restructure';
|
||||
|
||||
describe('VersionedStruct', function() {
|
||||
describe('decode', function() {
|
||||
it('should get version from number type', function() {
|
||||
const struct = new VersionedStruct(uint8, {
|
||||
0: {
|
||||
name: new StringT(uint8, 'ascii'),
|
||||
age: uint8
|
||||
},
|
||||
1: {
|
||||
name: new StringT(uint8, 'utf8'),
|
||||
age: uint8,
|
||||
gender: uint8
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
let stream = new DecodeStream(Buffer.from('\x00\x05devon\x15'));
|
||||
assert.deepEqual(struct.decode(stream), {
|
||||
version: 0,
|
||||
name: 'devon',
|
||||
age: 21
|
||||
});
|
||||
|
||||
stream = new DecodeStream(Buffer.from('\x01\x0adevon 👍\x15\x00', 'utf8'));
|
||||
assert.deepEqual(struct.decode(stream), {
|
||||
version: 1,
|
||||
name: 'devon 👍',
|
||||
age: 21,
|
||||
gender: 0
|
||||
});
|
||||
});
|
||||
|
||||
it('should throw for unknown version', function() {
|
||||
const struct = new VersionedStruct(uint8, {
|
||||
0: {
|
||||
name: new StringT(uint8, 'ascii'),
|
||||
age: uint8
|
||||
},
|
||||
1: {
|
||||
name: new StringT(uint8, 'utf8'),
|
||||
age: uint8,
|
||||
gender: uint8
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
const stream = new DecodeStream(Buffer.from('\x05\x05devon\x15'));
|
||||
return assert.throws(() => struct.decode(stream));
|
||||
});
|
||||
|
||||
it('should support common header block', function() {
|
||||
const struct = new VersionedStruct(uint8, {
|
||||
header: {
|
||||
age: uint8,
|
||||
alive: uint8
|
||||
},
|
||||
0: {
|
||||
name: new StringT(uint8, 'ascii')
|
||||
},
|
||||
1: {
|
||||
name: new StringT(uint8, 'utf8'),
|
||||
gender: uint8
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
let stream = new DecodeStream(Buffer.from('\x00\x15\x01\x05devon'));
|
||||
assert.deepEqual(struct.decode(stream), {
|
||||
version: 0,
|
||||
age: 21,
|
||||
alive: 1,
|
||||
name: 'devon'
|
||||
});
|
||||
|
||||
stream = new DecodeStream(Buffer.from('\x01\x15\x01\x0adevon 👍\x00', 'utf8'));
|
||||
assert.deepEqual(struct.decode(stream), {
|
||||
version: 1,
|
||||
age: 21,
|
||||
alive: 1,
|
||||
name: 'devon 👍',
|
||||
gender: 0
|
||||
});
|
||||
});
|
||||
|
||||
it('should support parent version key', function() {
|
||||
const struct = new VersionedStruct('version', {
|
||||
0: {
|
||||
name: new StringT(uint8, 'ascii'),
|
||||
age: uint8
|
||||
},
|
||||
1: {
|
||||
name: new StringT(uint8, 'utf8'),
|
||||
age: uint8,
|
||||
gender: uint8
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
let stream = new DecodeStream(Buffer.from('\x05devon\x15'));
|
||||
assert.deepEqual(struct.decode(stream, {version: 0}), {
|
||||
version: 0,
|
||||
name: 'devon',
|
||||
age: 21
|
||||
});
|
||||
|
||||
stream = new DecodeStream(Buffer.from('\x0adevon 👍\x15\x00', 'utf8'));
|
||||
assert.deepEqual(struct.decode(stream, {version: 1}), {
|
||||
version: 1,
|
||||
name: 'devon 👍',
|
||||
age: 21,
|
||||
gender: 0
|
||||
});
|
||||
});
|
||||
|
||||
it('should support parent version nested key', function() {
|
||||
const struct = new VersionedStruct('obj.version', {
|
||||
0: {
|
||||
name: new StringT(uint8, 'ascii'),
|
||||
age: uint8
|
||||
},
|
||||
1: {
|
||||
name: new StringT(uint8, 'utf8'),
|
||||
age: uint8,
|
||||
gender: uint8
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
let stream = new DecodeStream(Buffer.from('\x05devon\x15'));
|
||||
assert.deepEqual(struct.decode(stream, {obj: {version: 0}}), {
|
||||
version: 0,
|
||||
name: 'devon',
|
||||
age: 21
|
||||
});
|
||||
|
||||
stream = new DecodeStream(Buffer.from('\x0adevon 👍\x15\x00', 'utf8'));
|
||||
assert.deepEqual(struct.decode(stream, {obj: {version: 1}}), {
|
||||
version: 1,
|
||||
name: 'devon 👍',
|
||||
age: 21,
|
||||
gender: 0
|
||||
});
|
||||
});
|
||||
|
||||
it('should support sub versioned structs', function() {
|
||||
const struct = new VersionedStruct(uint8, {
|
||||
0: {
|
||||
name: new StringT(uint8, 'ascii'),
|
||||
age: uint8
|
||||
},
|
||||
1: new VersionedStruct(uint8, {
|
||||
0: {
|
||||
name: new StringT(uint8)
|
||||
},
|
||||
1: {
|
||||
name: new StringT(uint8),
|
||||
isDesert: uint8
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
);
|
||||
|
||||
let stream = new DecodeStream(Buffer.from('\x00\x05devon\x15'));
|
||||
assert.deepEqual(struct.decode(stream, {version: 0}), {
|
||||
version: 0,
|
||||
name: 'devon',
|
||||
age: 21
|
||||
});
|
||||
|
||||
stream = new DecodeStream(Buffer.from('\x01\x00\x05pasta'));
|
||||
assert.deepEqual(struct.decode(stream, {version: 0}), {
|
||||
version: 0,
|
||||
name: 'pasta'
|
||||
});
|
||||
|
||||
stream = new DecodeStream(Buffer.from('\x01\x01\x09ice cream\x01'));
|
||||
assert.deepEqual(struct.decode(stream, {version: 0}), {
|
||||
version: 1,
|
||||
name: 'ice cream',
|
||||
isDesert: 1
|
||||
});
|
||||
});
|
||||
|
||||
it('should support process hook', function() {
|
||||
const struct = new VersionedStruct(uint8, {
|
||||
0: {
|
||||
name: new StringT(uint8, 'ascii'),
|
||||
age: uint8
|
||||
},
|
||||
1: {
|
||||
name: new StringT(uint8, 'utf8'),
|
||||
age: uint8,
|
||||
gender: uint8
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
struct.process = function() {
|
||||
return this.processed = true;
|
||||
};
|
||||
|
||||
const stream = new DecodeStream(Buffer.from('\x00\x05devon\x15'));
|
||||
assert.deepEqual(struct.decode(stream), {
|
||||
version: 0,
|
||||
name: 'devon',
|
||||
age: 21,
|
||||
processed: true
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('size', function() {
|
||||
it('should compute the correct size', function() {
|
||||
const struct = new VersionedStruct(uint8, {
|
||||
0: {
|
||||
name: new StringT(uint8, 'ascii'),
|
||||
age: uint8
|
||||
},
|
||||
1: {
|
||||
name: new StringT(uint8, 'utf8'),
|
||||
age: uint8,
|
||||
gender: uint8
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
let size = struct.size({
|
||||
version: 0,
|
||||
name: 'devon',
|
||||
age: 21
|
||||
});
|
||||
|
||||
assert.equal(size, 8);
|
||||
|
||||
size = struct.size({
|
||||
version: 1,
|
||||
name: 'devon 👍',
|
||||
age: 21,
|
||||
gender: 0
|
||||
});
|
||||
|
||||
assert.equal(size, 14);
|
||||
});
|
||||
|
||||
it('should throw for unknown version', function() {
|
||||
const struct = new VersionedStruct(uint8, {
|
||||
0: {
|
||||
name: new StringT(uint8, 'ascii'),
|
||||
age: uint8
|
||||
},
|
||||
1: {
|
||||
name: new StringT(uint8, 'utf8'),
|
||||
age: uint8,
|
||||
gender: uint8
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
assert.throws(() =>
|
||||
struct.size({
|
||||
version: 5,
|
||||
name: 'devon',
|
||||
age: 21
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('should support common header block', function() {
|
||||
const struct = new VersionedStruct(uint8, {
|
||||
header: {
|
||||
age: uint8,
|
||||
alive: uint8
|
||||
},
|
||||
0: {
|
||||
name: new StringT(uint8, 'ascii')
|
||||
},
|
||||
1: {
|
||||
name: new StringT(uint8, 'utf8'),
|
||||
gender: uint8
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
let size = struct.size({
|
||||
version: 0,
|
||||
age: 21,
|
||||
alive: 1,
|
||||
name: 'devon'
|
||||
});
|
||||
|
||||
assert.equal(size, 9);
|
||||
|
||||
size = struct.size({
|
||||
version: 1,
|
||||
age: 21,
|
||||
alive: 1,
|
||||
name: 'devon 👍',
|
||||
gender: 0
|
||||
});
|
||||
|
||||
assert.equal(size, 15);
|
||||
});
|
||||
|
||||
it('should compute the correct size with pointers', function() {
|
||||
const struct = new VersionedStruct(uint8, {
|
||||
0: {
|
||||
name: new StringT(uint8, 'ascii'),
|
||||
age: uint8
|
||||
},
|
||||
1: {
|
||||
name: new StringT(uint8, 'utf8'),
|
||||
age: uint8,
|
||||
ptr: new Pointer(uint8, new StringT(uint8))
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
const size = struct.size({
|
||||
version: 1,
|
||||
name: 'devon',
|
||||
age: 21,
|
||||
ptr: 'hello'
|
||||
});
|
||||
|
||||
assert.equal(size, 15);
|
||||
});
|
||||
|
||||
it('should throw if no value is given', function() {
|
||||
const struct = new VersionedStruct(uint8, {
|
||||
0: {
|
||||
name: new StringT(4, 'ascii'),
|
||||
age: uint8
|
||||
},
|
||||
1: {
|
||||
name: new StringT(4, 'utf8'),
|
||||
age: uint8,
|
||||
gender: uint8
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
assert.throws(() => struct.size(), /not a fixed size/i);
|
||||
});
|
||||
});
|
||||
|
||||
describe('encode', function() {
|
||||
it('should encode objects to buffers', function() {
|
||||
const struct = new VersionedStruct(uint8, {
|
||||
0: {
|
||||
name: new StringT(uint8, 'ascii'),
|
||||
age: uint8
|
||||
},
|
||||
1: {
|
||||
name: new StringT(uint8, 'utf8'),
|
||||
age: uint8,
|
||||
gender: uint8
|
||||
}
|
||||
});
|
||||
|
||||
const buf1 = struct.toBuffer({
|
||||
version: 0,
|
||||
name: 'devon',
|
||||
age: 21
|
||||
});
|
||||
|
||||
assert.deepEqual(buf1, Buffer.from('\x00\x05devon\x15', 'utf8'));
|
||||
|
||||
const buf2 = struct.toBuffer({
|
||||
version: 1,
|
||||
name: 'devon 👍',
|
||||
age: 21,
|
||||
gender: 0
|
||||
});
|
||||
|
||||
assert.deepEqual(buf2, Buffer.from('\x01\x0adevon 👍\x15\x00', 'utf8'));
|
||||
});
|
||||
|
||||
it('should throw for unknown version', function() {
|
||||
const struct = new VersionedStruct(uint8, {
|
||||
0: {
|
||||
name: new StringT(uint8, 'ascii'),
|
||||
age: uint8
|
||||
},
|
||||
1: {
|
||||
name: new StringT(uint8, 'utf8'),
|
||||
age: uint8,
|
||||
gender: uint8
|
||||
}
|
||||
});
|
||||
|
||||
assert.throws(() =>
|
||||
struct.toBuffer({
|
||||
version: 5,
|
||||
name: 'devon',
|
||||
age: 21
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('should support common header block', function() {
|
||||
const struct = new VersionedStruct(uint8, {
|
||||
header: {
|
||||
age: uint8,
|
||||
alive: uint8
|
||||
},
|
||||
0: {
|
||||
name: new StringT(uint8, 'ascii')
|
||||
},
|
||||
1: {
|
||||
name: new StringT(uint8, 'utf8'),
|
||||
gender: uint8
|
||||
}
|
||||
});
|
||||
|
||||
const buf1 = struct.toBuffer({
|
||||
version: 0,
|
||||
age: 21,
|
||||
alive: 1,
|
||||
name: 'devon'
|
||||
});
|
||||
|
||||
assert.deepEqual(buf1, Buffer.from('\x00\x15\x01\x05devon', 'utf8'));
|
||||
|
||||
const buf2 = struct.toBuffer({
|
||||
version: 1,
|
||||
age: 21,
|
||||
alive: 1,
|
||||
name: 'devon 👍',
|
||||
gender: 0
|
||||
});
|
||||
|
||||
assert.deepEqual(buf2, Buffer.from('\x01\x15\x01\x0adevon 👍\x00', 'utf8'));
|
||||
});
|
||||
|
||||
it('should encode pointer data after structure', function() {
|
||||
const struct = new VersionedStruct(uint8, {
|
||||
0: {
|
||||
name: new StringT(uint8, 'ascii'),
|
||||
age: uint8
|
||||
},
|
||||
1: {
|
||||
name: new StringT(uint8, 'utf8'),
|
||||
age: uint8,
|
||||
ptr: new Pointer(uint8, new StringT(uint8))
|
||||
}
|
||||
});
|
||||
|
||||
const buf = struct.toBuffer({
|
||||
version: 1,
|
||||
name: 'devon',
|
||||
age: 21,
|
||||
ptr: 'hello'
|
||||
});
|
||||
|
||||
assert.deepEqual(buf, Buffer.from('\x01\x05devon\x15\x09\x05hello', 'utf8'));
|
||||
});
|
||||
|
||||
it('should support preEncode hook', function() {
|
||||
const struct = new VersionedStruct(uint8, {
|
||||
0: {
|
||||
name: new StringT(uint8, 'ascii'),
|
||||
age: uint8
|
||||
},
|
||||
1: {
|
||||
name: new StringT(uint8, 'utf8'),
|
||||
age: uint8,
|
||||
gender: uint8
|
||||
}
|
||||
});
|
||||
|
||||
struct.preEncode = function() {
|
||||
return this.version = (this.gender != null) ? 1 : 0;
|
||||
};
|
||||
|
||||
const buf1 = struct.toBuffer({
|
||||
name: 'devon',
|
||||
age: 21
|
||||
});
|
||||
|
||||
assert.deepEqual(buf1, Buffer.from('\x00\x05devon\x15', 'utf8'));
|
||||
|
||||
const buf2 = struct.toBuffer({
|
||||
name: 'devon 👍',
|
||||
age: 21,
|
||||
gender: 0
|
||||
});
|
||||
|
||||
assert.deepEqual(buf2, Buffer.from('\x01\x0adevon 👍\x15\x00', 'utf8'));
|
||||
});
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user