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