const enc = require('./') const test = require('brittle') const b4a = require('b4a') const MAX_SAFE_INT = 2 ** 52 - 1 const MIN_SAFE_INT = -(2 ** 52) const FIRST_UNSAFE_INTEGER = BigInt(Number.MAX_SAFE_INTEGER) + 1n test('uint', function (t) { const state = enc.state() enc.uint.preencode(state, 42) t.alike(state, enc.state(0, 1)) enc.uint.preencode(state, 4200) t.alike(state, enc.state(0, 4)) enc.uint.preencode(state, Number.MAX_SAFE_INTEGER) t.alike(state, enc.state(0, 13)) state.buffer = b4a.alloc(state.end) enc.uint.encode(state, 42) t.alike(state, enc.state(1, 13, b4a.from([42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]))) enc.uint.encode(state, 4200) t.alike(state, enc.state(4, 13, b4a.from([42, 0xfd, 104, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0]))) enc.uint.encode(state, Number.MAX_SAFE_INTEGER) t.alike( state, enc.state(13, 13, b4a.from([42, 0xfd, 104, 16, 0xff, 255, 255, 255, 255, 255, 255, 31, 0])) ) state.start = 0 t.is(enc.uint.decode(state), 42) t.is(enc.uint.decode(state), 4200) t.is(enc.uint.decode(state), Number.MAX_SAFE_INTEGER) t.is(state.start, state.end) t.exception(() => enc.uint.decode(state)) }) test('uint32 & uint32be', function (t) { const state = enc.state() enc.uint32.preencode(state, 42) t.alike(state, enc.state(0, 4)) enc.uint32.preencode(state, 4200) t.alike(state, enc.state(0, 8)) state.buffer = b4a.alloc(state.end) enc.uint32.encode(state, 42) t.alike(state, enc.state(4, 8, b4a.from([42, 0, 0, 0, 0, 0, 0, 0]))) enc.uint32.encode(state, 4200) t.alike(state, enc.state(8, 8, b4a.from([42, 0, 0, 0, 104, 16, 0, 0]))) state.start = 0 t.is(enc.uint32.decode(state), 42) t.is(enc.uint32.decode(state), 4200) t.is(state.start, state.end) t.exception(() => enc.uint32.decode(state)) const state2 = enc.state() enc.uint32be.preencode(state2, 42) t.alike(state2, enc.state(0, 4)) enc.uint32be.preencode(state2, 4200) t.alike(state2, enc.state(0, 8)) state2.buffer = b4a.alloc(state2.end) enc.uint32be.encode(state2, 42) t.alike(state2, enc.state(4, 8, b4a.from([0, 0, 0, 42, 0, 0, 0, 0]))) enc.uint32be.encode(state2, 4200) t.alike(state2, enc.state(8, 8, b4a.from([0, 0, 0, 42, 0, 0, 16, 104]))) state2.start = 0 t.is(enc.uint32be.decode(state2), 42) t.is(enc.uint32be.decode(state2), 4200) t.is(state2.start, state2.end) t.exception(() => enc.uint32be.decode(state2)) }) test('uint64 & uint64be', function (t) { const state = enc.state() enc.uint64.preencode(state, 42) t.alike(state, enc.state(0, 8)) enc.uint64.preencode(state, 4200) t.alike(state, enc.state(0, 16)) enc.uint64.preencode(state, 0x200000000) t.alike(state, enc.state(0, 24)) state.buffer = b4a.alloc(state.end) enc.uint64.encode(state, 42) t.alike( state, enc.state( 8, 24, b4a.from([42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) ) ) enc.uint64.encode(state, 4200) t.alike( state, enc.state( 16, 24, b4a.from([42, 0, 0, 0, 0, 0, 0, 0, 104, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) ) ) enc.uint64.encode(state, 0x200000000) t.alike( state, enc.state( 24, 24, b4a.from([42, 0, 0, 0, 0, 0, 0, 0, 104, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0]) ) ) state.start = 0 t.is(enc.uint64.decode(state), 42) t.is(enc.uint64.decode(state), 4200) t.is(enc.uint64.decode(state), 0x200000000) t.is(state.start, state.end) t.exception(() => enc.uint64.decode(state)) const state2 = enc.state() enc.uint64be.preencode(state2, 42) t.alike(state2, enc.state(0, 8)) enc.uint64be.preencode(state2, 4200) t.alike(state2, enc.state(0, 16)) enc.uint64be.preencode(state2, 0x200000000) t.alike(state2, enc.state(0, 24)) state2.buffer = b4a.alloc(state2.end) enc.uint64be.encode(state2, 42) t.alike( state2, enc.state( 8, 24, b4a.from([0, 0, 0, 0, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) ) ) enc.uint64be.encode(state2, 4200) t.alike( state2, enc.state( 16, 24, b4a.from([0, 0, 0, 0, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 16, 104, 0, 0, 0, 0, 0, 0, 0, 0]) ) ) enc.uint64be.encode(state2, 0x200000000) t.alike( state2, enc.state( 24, 24, b4a.from([0, 0, 0, 0, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 16, 104, 0, 0, 0, 2, 0, 0, 0, 0]) ) ) state2.start = 0 t.is(enc.uint64be.decode(state2), 42) t.is(enc.uint64be.decode(state2), 4200) t.is(enc.uint64be.decode(state2), 0x200000000) t.is(state2.start, state2.end) t.exception(() => enc.uint64be.decode(state2)) }) test('int', function (t) { const state = enc.state() enc.int.preencode(state, 42) t.alike(state, enc.state(0, 1)) enc.int.preencode(state, -4200) t.alike(state, enc.state(0, 4)) state.buffer = b4a.alloc(state.end) enc.int.encode(state, 42) t.alike(state, enc.state(1, 4, b4a.from([84, 0, 0, 0]))) enc.int.encode(state, -4200) t.alike(state, enc.state(4, 4, b4a.from([84, 0xfd, 207, 32]))) state.start = 0 t.is(enc.int.decode(state), 42) t.is(enc.int.decode(state), -4200) t.is(state.start, state.end) t.exception(() => enc.int.decode(state)) }) test('a fixed-width uint refuses a value its width cannot hold', function (t) { const widths = { uint8: 8, uint16: 16, uint24: 24, uint32: 32, uint32be: 32, uint40: 40, uint48: 48 } for (const [name, bits] of Object.entries(widths)) { const max = 2 ** bits - 1 t.is( enc.decode(enc[name], enc.encode(enc[name], max)), max, name + ' carries its largest value' ) t.exception.all(() => enc.encode(enc[name], max + 1), { name: 'RangeError' }, name) } }) test('a fixed-width int refuses a value its width cannot hold', function (t) { const widths = { int8: 8, int16: 16, int24: 24, int32: 32, int40: 40, int48: 48 } for (const [name, bits] of Object.entries(widths)) { const max = 2 ** (bits - 1) - 1 const min = -(2 ** (bits - 1)) t.is( enc.decode(enc[name], enc.encode(enc[name], max)), max, name + ' carries its largest value' ) t.is( enc.decode(enc[name], enc.encode(enc[name], min)), min, name + ' carries its smallest value' ) t.exception.all(() => enc.encode(enc[name], max + 1), { name: 'RangeError' }, name) t.exception.all(() => enc.encode(enc[name], min - 1), { name: 'RangeError' }, name) } }) test('a wide fixed-width codec is bounded by its own width alone', function (t) { const values = { uint40: 256, uint48: 65536, uint56: 16777216, uint64: 4294967296, uint64be: 4294967296, uint: 4294967296 } for (const [name, n] of Object.entries(values)) { t.execution(() => enc.encode(enc[name], n), name) } }) test('uint refuses a negative value against its own range', function (t) { t.exception.all(() => enc.encode(enc.uint, -1), /use biguint/) }) test('integers beyond the safe range throw instead of silently corrupting', function (t) { // The safe-integer boundary still encodes and round-trips. const state = enc.state() const unsafe = FIRST_UNSAFE_INTEGER enc.uint64.preencode(state, Number.MAX_SAFE_INTEGER) state.buffer = b4a.alloc(state.end) enc.uint64.encode(state, Number.MAX_SAFE_INTEGER) state.start = 0 t.is(enc.uint64.decode(state), Number.MAX_SAFE_INTEGER) // Beyond it, encoding uint and zig-zag int codecs rejects rather than misrounds. const big = enc.state(0, 64, b4a.alloc(64)) t.exception.all(() => enc.uint64.encode(big, Number.MAX_SAFE_INTEGER + 1), { name: 'RangeError' }) t.exception.all(() => enc.int56.encode(big, -(2 ** 53 - 1)), { name: 'RangeError' }) t.exception.all(() => enc.int.encode(big, 2 ** 53), { name: 'RangeError' }) // Decoding the same first unsafe integer rejects instead of returning an unsafe Number. t.exception.all( () => enc.uint.decode(enc.state(0, 9, b4a.concat([b4a.from([0xff]), uint64(unsafe, true)]))), { name: 'RangeError' } ) t.exception.all( () => enc.int.decode(enc.state(0, 9, b4a.concat([b4a.from([0xff]), uint64(unsafe, true)]))), { name: 'RangeError' } ) t.exception.all(() => enc.uint56.decode(enc.state(0, 7, uint64(unsafe, true).subarray(0, 7))), { name: 'RangeError' }) t.exception.all(() => enc.uint64.decode(enc.state(0, 8, uint64(unsafe, true))), { name: 'RangeError' }) t.exception.all(() => enc.uint64be.decode(enc.state(0, 8, uint64(unsafe, false))), { name: 'RangeError' }) }) test('uint rejects a negative value', function (t) { const state = enc.state(0, 64, b4a.alloc(64)) t.exception.all(() => enc.uint.encode(state, -1), { name: 'RangeError' }) t.exception.all(() => enc.uint8.encode(state, -1), { name: 'RangeError' }) t.exception.all(() => enc.uint64.encode(state, -1), { name: 'RangeError' }) }) test('int rejects values outside the range it can carry', function (t) { const state = enc.state(0, 64, b4a.alloc(64)) // Zig-zag doubles the magnitude before writing it as a uint, so an int only // reaches half as far as a uint of the same width does. t.is(enc.decode(enc.int, enc.encode(enc.int, MAX_SAFE_INT)), MAX_SAFE_INT) t.is(enc.decode(enc.int, enc.encode(enc.int, MIN_SAFE_INT)), MIN_SAFE_INT) t.exception.all(() => enc.int.encode(state, MAX_SAFE_INT + 1), { name: 'RangeError', message: /use bigint/ }) t.exception.all(() => enc.int.encode(state, MIN_SAFE_INT - 1), { name: 'RangeError', message: /use bigint/ }) t.exception.all(() => enc.int.encode(state, Number.MAX_SAFE_INTEGER), { name: 'RangeError', message: /use bigint/ }) t.exception.all(() => enc.int.encode(state, Number.MIN_SAFE_INTEGER), { name: 'RangeError', message: /use bigint/ }) // The rejection lands before any space is reserved for the value. const empty = enc.state() t.exception.all(() => enc.int.preencode(empty, MAX_SAFE_INT + 1), { name: 'RangeError' }) t.is(empty.end, 0) }) test('non-integers are refused', function (t) { const state = enc.state(0, 64, b4a.alloc(64)) t.exception.all(() => enc.uint.encode(state, 1.5), { name: 'TypeError', message: /uint must be an integer/ }) t.exception.all(() => enc.uint.encode(state, 252.5), { name: 'TypeError', message: /uint must be an integer/ }) t.exception.all(() => enc.uint.encode(state, 65535.5), { name: 'TypeError', message: /uint must be an integer/ }) t.exception.all(() => enc.uint.encode(state, 4294967295.5), { name: 'TypeError', message: /uint must be an integer/ }) t.exception.all(() => enc.uint.encode(state, -1.5), { name: 'TypeError', message: /uint must be an integer/ }) t.exception.all(() => enc.uint.encode(state, NaN), { name: 'TypeError', message: /uint must be an integer/ }) t.exception.all(() => enc.uint.encode(state, Infinity), { name: 'TypeError', message: /uint must be an integer/ }) t.exception.all(() => enc.uint32.encode(state, 1.5), { name: 'TypeError', message: /uint must be an integer/ }) t.exception.all(() => enc.uint8.encode(state, 1.5), { name: 'TypeError', message: /uint must be an integer/ }) t.exception.all(() => enc.uint48.encode(state, 1.5), { name: 'TypeError', message: /uint must be an integer/ }) t.exception.all(() => enc.int.encode(state, 1.5), { name: 'TypeError', message: /int must be an integer/ }) t.exception.all(() => enc.int.encode(state, 0.5), { name: 'TypeError', message: /int must be an integer/ }) t.exception.all(() => enc.int.encode(state, -1.5), { name: 'TypeError', message: /int must be an integer/ }) t.exception.all(() => enc.int.encode(state, NaN), { name: 'TypeError', message: /int must be an integer/ }) t.exception.all(() => enc.int.encode(state, Infinity), { name: 'TypeError', message: /int must be an integer/ }) t.exception.all(() => enc.int.encode(state, -Infinity), { name: 'TypeError', message: /int must be an integer/ }) const empty = enc.state() t.exception.all(() => enc.int.preencode(empty, 1.5), { name: 'TypeError', message: /int must be an integer/ }) t.is(empty.end, 0) t.is(enc.decode(enc.uint, enc.encode(enc.uint, -0)), 0) t.is(enc.decode(enc.int, enc.encode(enc.int, -0)), 0) }) test('float64', function (t) { const state = enc.state() enc.float64.preencode(state, 162.2377294) t.alike(state, enc.state(0, 8)) state.buffer = b4a.alloc(state.end) t.alike(state, enc.state(0, 8, b4a.from([0, 0, 0, 0, 0, 0, 0, 0]))) enc.float64.encode(state, 162.2377294) t.alike(state, enc.state(8, 8, b4a.from([0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40]))) state.start = 0 t.is(enc.float64.decode(state), 162.2377294) t.is(state.start, state.end) t.exception(() => enc.float64.decode(state)) // alignement state.start = 0 state.end = 0 state.buffer = null enc.int.preencode(state, 0) enc.float64.preencode(state, 162.2377294) t.alike(state, enc.state(0, 9)) state.buffer = b4a.alloc(state.end) t.alike(state, enc.state(0, 9, b4a.from([0, 0, 0, 0, 0, 0, 0, 0, 0]))) enc.int.encode(state, 0) enc.float64.encode(state, 162.2377294) t.alike(state, enc.state(9, 9, b4a.from([0, 0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40]))) state.start = 0 t.is(enc.int.decode(state), 0) t.is(enc.float64.decode(state), 162.2377294) t.is(state.start, state.end) // subarray const buf = b4a.alloc(10) state.start = 0 state.buffer = buf.subarray(1) t.alike(state, enc.state(0, 9, b4a.from([0, 0, 0, 0, 0, 0, 0, 0, 0]))) enc.int.encode(state, 0) enc.float64.encode(state, 162.2377294) t.alike(state, enc.state(9, 9, b4a.from([0, 0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40]))) t.alike(buf, b4a.from([0, 0, 0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40])) state.start = 0 t.is(enc.int.decode(state), 0) t.is(enc.float64.decode(state), 162.2377294) t.is(state.start, state.end) // 0 state.start = 0 state.end = 0 state.buffer = null enc.float64.preencode(state, 162.2377294) state.buffer = b4a.alloc(state.end) enc.float64.encode(state, 0) t.alike(state, enc.state(8, 8, b4a.from([0, 0, 0, 0, 0, 0, 0, 0]))) state.start = 0 t.is(enc.float64.decode(state), 0) t.is(state.start, state.end) // Infinity state.start = 0 state.end = 0 state.buffer = null enc.float64.preencode(state, Infinity) state.buffer = b4a.alloc(state.end) enc.float64.encode(state, Infinity) t.alike(state, enc.state(8, 8, b4a.from([0, 0, 0, 0, 0, 0, 0xf0, 0x7f]))) state.start = 0 t.is(enc.float64.decode(state), Infinity) t.is(state.start, state.end) // Edge cases state.start = 0 state.end = 0 state.buffer = null enc.float64.preencode(state, 0.1 + 0.2) state.buffer = b4a.alloc(state.end) enc.float64.encode(state, 0.1 + 0.2) t.alike(state, enc.state(8, 8, b4a.from([0x34, 0x33, 0x33, 0x33, 0x33, 0x33, 0xd3, 0x3f]))) state.start = 0 t.is(enc.float64.decode(state), 0.1 + 0.2) t.is(state.start, state.end) }) test('floats refuse a non-number', function (t) { const state = enc.state(0, 64, b4a.alloc(64)) t.exception.all(() => enc.float64.encode(state, '5'), { name: 'TypeError', message: /float must be a number/ }) t.exception.all(() => enc.float32.encode(state, '5'), { name: 'TypeError', message: /float must be a number/ }) // NaN is a value the format carries, so a value that coerces to one has to be // refused for a reader to tell a typo from a NaN the caller meant. t.exception.all(() => enc.float64.encode(state, 'x'), { name: 'TypeError', message: /float must be a number/ }) t.exception.all(() => enc.float64.encode(state, {}), { name: 'TypeError', message: /float must be a number/ }) t.exception.all(() => enc.float64.encode(state, 1n), { name: 'TypeError', message: /float must be a number/ }) t.exception.all(() => enc.float64.encode(state, Symbol()), { name: 'TypeError', message: /float must be a number/ }) t.exception.all(() => enc.float32.encode(state, 1n), { name: 'TypeError', message: /float must be a number/ }) const roundtrip = (v) => enc.decode(enc.float64, enc.encode(enc.float64, v)) t.ok(Number.isNaN(roundtrip(NaN))) t.is(roundtrip(Infinity), Infinity) t.is(roundtrip(-Infinity), -Infinity) }) test('float preencode does not check the value', function (t) { const state = enc.state() t.execution(() => enc.float64.preencode(state, '5')) t.is(state.end, 8) t.execution(() => enc.float32.preencode(state, {})) t.is(state.end, 12) }) test('biguint64', function (t) { const state = enc.state() const n = 0x0102030405060708n enc.biguint64.preencode(state, n) t.alike(state, enc.state(0, 8)) state.buffer = b4a.alloc(state.end) enc.biguint64.encode(state, n) t.alike(state, enc.state(8, 8, b4a.from([0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1]))) state.start = 0 t.is(enc.biguint64.decode(state), n) t.is(state.start, state.end) t.exception(() => enc.biguint64.decode(state)) }) test('bigint64', function (t) { const state = enc.state() const n = -0x0102030405060708n enc.bigint64.preencode(state, n) t.alike(state, enc.state(0, 8)) state.buffer = b4a.alloc(state.end) enc.bigint64.encode(state, n) t.alike(state, enc.state(8, 8, b4a.from([0xf, 0xe, 0xc, 0xa, 0x8, 0x6, 0x4, 0x2]))) state.start = 0 t.is(enc.bigint64.decode(state), n) t.is(state.start, state.end) t.exception(() => enc.bigint64.decode(state)) }) test('biguint64 refuses a value its width cannot hold', function (t) { const max = 2n ** 64n - 1n t.is(enc.decode(enc.biguint64, enc.encode(enc.biguint64, max)), max) t.is(enc.decode(enc.biguint64, enc.encode(enc.biguint64, 0n)), 0n) t.exception.all(() => enc.encode(enc.biguint64, max + 1n), { name: 'RangeError' }) t.exception.all(() => enc.encode(enc.biguint64, -1n), { name: 'RangeError' }) }) test('bigint64 refuses a value its width cannot hold', function (t) { const max = 2n ** 63n - 1n const min = -(2n ** 63n) t.is(enc.decode(enc.bigint64, enc.encode(enc.bigint64, max)), max) t.is(enc.decode(enc.bigint64, enc.encode(enc.bigint64, min)), min) t.exception.all(() => enc.encode(enc.bigint64, max + 1n), { name: 'RangeError' }) t.exception.all(() => enc.encode(enc.bigint64, min - 1n), { name: 'RangeError' }) }) test('biguint', function (t) { const state = enc.state() const n = 0x0102030405060708090a0b0cn enc.biguint.preencode(state, n) t.alike(state, enc.state(0, 17)) state.buffer = b4a.alloc(state.end) enc.biguint.encode(state, n) t.alike( state, enc.state( 17, 17, b4a.from([2, 0xc, 0xb, 0xa, 0x9, 0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1, 0x0, 0x0, 0x0, 0x0]) ) ) state.start = 0 t.is(enc.biguint.decode(state), n) t.is(state.start, state.end) t.exception(() => enc.biguint.decode(state)) }) test('biguint refuses a negative value', function (t) { t.exception.all(() => enc.encode(enc.biguint, -1n), { name: 'RangeError' }) t.exception.all(() => enc.biguint.preencode(enc.state(), -1n), { name: 'RangeError' }) t.exception.all(() => enc.biguint.encode(enc.state(0, 64, b4a.alloc(64)), -1n), { name: 'RangeError' }) }) test('bigint', function (t) { const state = enc.state() const n = -0x0102030405060708090a0b0cn enc.bigint.preencode(state, n) t.alike(state, enc.state(0, 17)) state.buffer = b4a.alloc(state.end) enc.bigint.encode(state, n) t.alike( state, enc.state( 17, 17, b4a.from([ 2, 0x17, 0x16, 0x14, 0x12, 0x10, 0xe, 0xc, 0xa, 0x8, 0x6, 0x4, 0x2, 0x0, 0x0, 0x0, 0x0 ]) ) ) state.start = 0 t.is(enc.bigint.decode(state), n) t.is(state.start, state.end) t.exception(() => enc.bigint.decode(state)) }) test('buffer', function (t) { const state = enc.state() enc.buffer.preencode(state, b4a.from('hi')) t.alike(state, enc.state(0, 3)) enc.buffer.preencode(state, b4a.from('hello')) t.alike(state, enc.state(0, 9)) enc.buffer.preencode(state, b4a.alloc(0)) t.alike(state, enc.state(0, 10)) state.buffer = b4a.alloc(state.end) enc.buffer.encode(state, b4a.from('hi')) t.alike(state, enc.state(3, 10, b4a.from('\x02hi\x00\x00\x00\x00\x00\x00\x00'))) enc.buffer.encode(state, b4a.from('hello')) t.alike(state, enc.state(9, 10, b4a.from('\x02hi\x05hello\x00'))) enc.buffer.encode(state, b4a.alloc(0)) t.alike(state, enc.state(10, 10, b4a.from('\x02hi\x05hello\x00'))) state.start = 0 t.alike(enc.buffer.decode(state), b4a.from('hi')) t.alike(enc.buffer.decode(state), b4a.from('hello')) t.alike(enc.buffer.decode(state), b4a.alloc(0)) t.is(state.start, state.end) t.exception(() => enc.buffer.decode(state)) }) test('optionalBuffer', function (t) { const state = enc.state() enc.optionalBuffer.preencode(state, b4a.from('hi')) t.alike(state, enc.state(0, 3)) enc.optionalBuffer.preencode(state, b4a.from('hello')) t.alike(state, enc.state(0, 9)) enc.optionalBuffer.preencode(state, null) enc.optionalBuffer.preencode(state, b4a.alloc(0)) t.alike(state, enc.state(0, 11)) state.buffer = b4a.alloc(state.end) enc.optionalBuffer.encode(state, b4a.from('hi')) t.alike(state, enc.state(3, 11, b4a.from('\x02hi\x00\x00\x00\x00\x00\x00\x00\x00'))) enc.optionalBuffer.encode(state, b4a.from('hello')) t.alike(state, enc.state(9, 11, b4a.from('\x02hi\x05hello\x00\x00'))) enc.optionalBuffer.encode(state, null) t.alike(state, enc.state(10, 11, b4a.from('\x02hi\x05hello\x00\x00'))) enc.optionalBuffer.encode(state, b4a.alloc(0)) t.alike(state, enc.state(11, 11, b4a.from('\x02hi\x05hello\x00\x00'))) state.start = 0 t.alike(enc.optionalBuffer.decode(state), b4a.from('hi')) t.alike(enc.optionalBuffer.decode(state), b4a.from('hello')) t.is(enc.optionalBuffer.decode(state), null) t.is(enc.optionalBuffer.decode(state), null, 'empty buffer decodes as null') t.is(state.start, state.end) t.exception(() => enc.optionalBuffer.decode(state)) }) test('arraybuffer', function (t) { const state = enc.state() const b1 = new ArrayBuffer(4) b4a.from(b1).fill('a') const b2 = new ArrayBuffer(8) b4a.from(b2).fill('b') enc.arraybuffer.preencode(state, b1) t.alike(state, enc.state(0, 5)) enc.arraybuffer.preencode(state, b2) t.alike(state, enc.state(0, 14)) state.buffer = b4a.alloc(state.end) enc.arraybuffer.encode(state, b1) t.alike(state, enc.state(5, 14, b4a.from('\x04aaaa\x00\x00\x00\x00\x00\x00\x00\x00\x00'))) enc.arraybuffer.encode(state, b2) t.alike(state, enc.state(14, 14, b4a.from('\x04aaaa\x08bbbbbbbb'))) state.start = 0 t.alike(enc.arraybuffer.decode(state), b1) t.alike(enc.arraybuffer.decode(state), b2) t.is(state.start, state.end) t.exception(() => enc.arraybuffer.decode(state)) }) test('arraybuffer - throws when length exceeds buffer', function (t) { const buf = b4a.from([0x05, 0x00, 0x00, 0x00, 0x40]) const state = enc.state(0, buf.byteLength, buf) t.exception(() => enc.arraybuffer.decode(state), /Out of bounds/) }) test('arraybuffer and typed arrays stop at state end', function (t) { const buffer = b4a.from([2, 1, 0, 2, 0]) t.exception(() => enc.arraybuffer.decode(enc.state(0, 2, buffer)), /Out of bounds/) t.exception(() => enc.uint8array.decode(enc.state(0, 2, buffer)), /Out of bounds/) t.exception(() => enc.uint16array.decode(enc.state(0, 4, buffer)), /Out of bounds/) }) test('typed arrays decode from unaligned offsets', function (t) { const encoded = enc.encode(enc.uint32array, new Uint32Array([1, 0xffffffff])) for (let offset = 0; offset < 4; offset++) { const buffer = b4a.alloc(encoded.byteLength + offset) buffer.set(encoded, offset) const state = enc.state(offset, buffer.byteLength, buffer) t.alike(enc.uint32array.decode(state), new Uint32Array([1, 0xffffffff])) t.is(state.start, state.end) } }) test('raw', function (t) { const state = enc.state() enc.raw.preencode(state, b4a.from('hi')) t.alike(state, enc.state(0, 2)) state.buffer = b4a.alloc(state.end) enc.raw.encode(state, b4a.from('hi')) t.alike(state, enc.state(2, 2, b4a.from('hi'))) state.start = 0 t.alike(enc.raw.decode(state), b4a.from('hi')) t.is(state.start, state.end) }) test('raw uint8array', function (t) { const state = enc.state() enc.raw.uint8array.preencode(state, Uint8Array.of(1, 2)) t.alike(state, enc.state(0, 2)) enc.raw.uint8array.preencode(state, Uint8Array.of(3, 4)) t.alike(state, enc.state(0, 4)) state.buffer = b4a.alloc(state.end) enc.raw.uint8array.encode(state, Uint8Array.of(1, 2)) t.alike(state, enc.state(2, 4, b4a.from([1, 2, 0, 0]))) enc.raw.uint8array.encode(state, Uint8Array.of(3, 4)) t.alike(state, enc.state(4, 4, b4a.from([1, 2, 3, 4]))) state.start = 0 t.alike(enc.raw.uint8array.decode(state), Uint8Array.of(1, 2, 3, 4)) t.alike(enc.raw.uint8array.decode(state), Uint8Array.of()) }) test('framed raw buffer', function (t) { const state = enc.state(0, 5, b4a.from([3, 1, 2, 3, 42])) t.alike(enc.frame(enc.raw.buffer).decode(state), b4a.from([1, 2, 3])) t.is(enc.uint.decode(state), 42) }) test('framed raw string', function (t) { const state = enc.state(0, 4, b4a.from([2, 104, 105, 42])) t.is(enc.frame(enc.raw.string).decode(state), 'hi') t.is(enc.uint.decode(state), 42) }) test('framed raw arraybuffer', function (t) { const state = enc.state(0, 5, b4a.from([3, 1, 2, 3, 42])) t.alike(enc.frame(enc.raw.arraybuffer).decode(state), Uint8Array.of(1, 2, 3).buffer) t.is(enc.uint.decode(state), 42) }) test('framed raw uint16array from unaligned offset', function (t) { const state = enc.state(0, 7, Uint8Array.of(4, 1, 0, 255, 255, 42, 43)) t.alike(enc.frame(enc.raw.uint16array).decode(state), Uint16Array.of(1, 65535)) t.is(enc.uint.decode(state), 42) t.is(enc.uint.decode(state), 43) }) test('uint16array', function (t) { const state = enc.state() enc.uint16array.preencode(state, new Uint16Array([1, 2, 3])) t.alike(state, enc.state(0, 7)) state.buffer = b4a.alloc(state.end) enc.uint16array.encode(state, new Uint16Array([1, 2, 3])) t.alike(state, enc.state(7, 7, b4a.from([3, 1, 0, 2, 0, 3, 0]))) state.start = 0 t.alike(enc.uint16array.decode(state), new Uint16Array([1, 2, 3])) t.is(state.start, state.end) t.exception(() => enc.uint16array.decode(state)) }) test('uint32array', function (t) { const state = enc.state() enc.uint32array.preencode(state, new Uint32Array([1])) t.alike(state, enc.state(0, 5)) enc.uint32array.preencode(state, new Uint32Array([42, 43])) t.alike(state, enc.state(0, 14)) state.buffer = b4a.alloc(state.end) enc.uint32array.encode(state, new Uint32Array([1])) t.alike(state, enc.state(5, 14, b4a.from([1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]))) enc.uint32array.encode(state, new Uint32Array([42, 43])) t.alike(state, enc.state(14, 14, b4a.from([1, 1, 0, 0, 0, 2, 42, 0, 0, 0, 43, 0, 0, 0]))) state.start = 0 t.alike(enc.uint32array.decode(state), new Uint32Array([1])) t.alike(enc.uint32array.decode(state), new Uint32Array([42, 43])) t.is(state.start, state.end) t.exception(() => enc.uint32array.decode(state)) }) test('int16array', function (t) { const state = enc.state() enc.int16array.preencode(state, new Int16Array([1, -2, 3])) t.alike(state, enc.state(0, 7)) state.buffer = b4a.alloc(state.end) enc.int16array.encode(state, new Int16Array([1, -2, 3])) t.alike(state, enc.state(7, 7, b4a.from([3, 1, 0, 0xfe, 0xff, 3, 0]))) state.start = 0 t.alike(enc.int16array.decode(state), new Int16Array([1, -2, 3])) t.is(state.start, state.end) t.exception(() => enc.int16array.decode(state)) }) test('int32array', function (t) { const state = enc.state() enc.int32array.preencode(state, new Int32Array([1, -2, 3])) t.alike(state, enc.state(0, 13)) state.buffer = b4a.alloc(state.end) enc.int32array.encode(state, new Int32Array([1, -2, 3])) t.alike(state, enc.state(13, 13, b4a.from([3, 1, 0, 0, 0, 0xfe, 0xff, 0xff, 0xff, 3, 0, 0, 0]))) state.start = 0 t.alike(enc.int32array.decode(state), new Int32Array([1, -2, 3])) t.is(state.start, state.end) t.exception(() => enc.int32array.decode(state)) }) test('biguint64array', function (t) { const state = enc.state() const arr = new BigUint64Array([0x01020304n, 0x05060708n, 0x090a0b0cn]) enc.biguint64array.preencode(state, arr) t.alike(state, enc.state(0, 25)) state.buffer = b4a.alloc(state.end) enc.biguint64array.encode(state, arr) t.alike( state, enc.state( 25, 25, b4a.from([ 3, 0x4, 0x3, 0x2, 0x1, 0x0, 0x0, 0x0, 0x0, 0x8, 0x7, 0x6, 0x5, 0x0, 0x0, 0x0, 0x0, 0xc, 0xb, 0xa, 0x9, 0x0, 0x0, 0x0, 0x0 ]) ) ) state.start = 0 t.alike(enc.biguint64array.decode(state), arr) t.is(state.start, state.end) t.exception(() => enc.biguint64array.decode(state)) }) test('bigint64array', function (t) { const state = enc.state() const arr = new BigInt64Array([-0x01020304n, 0x05060708n, -0x090a0b0cn]) enc.bigint64array.preencode(state, arr) t.alike(state, enc.state(0, 25)) state.buffer = b4a.alloc(state.end) enc.bigint64array.encode(state, arr) t.alike( state, enc.state( 25, 25, b4a.from([ 3, 0xfc, 0xfc, 0xfd, 0xfe, 0xff, 0xff, 0xff, 0xff, 0x8, 0x7, 0x6, 0x5, 0x0, 0x0, 0x0, 0x0, 0xf4, 0xf4, 0xf5, 0xf6, 0xff, 0xff, 0xff, 0xff ]) ) ) state.start = 0 t.alike(enc.bigint64array.decode(state), arr) t.is(state.start, state.end) t.exception(() => enc.bigint64array.decode(state)) }) test('float32array', function (t) { const state = enc.state() enc.float32array.preencode(state, new Float32Array([1.1, -2.2, 3.3])) t.alike(state, enc.state(0, 13)) state.buffer = b4a.alloc(state.end) enc.float32array.encode(state, new Float32Array([1.1, -2.2, 3.3])) t.alike( state, enc.state( 13, 13, b4a.from([3, 0xcd, 0xcc, 0x8c, 0x3f, 0xcd, 0xcc, 0x0c, 0xc0, 0x33, 0x33, 0x53, 0x40]) ) ) state.start = 0 t.alike(enc.float32array.decode(state), new Float32Array([1.1, -2.2, 3.3])) t.is(state.start, state.end) t.exception(() => enc.float32array.decode(state)) }) test('float64array', function (t) { const state = enc.state() enc.float64array.preencode(state, new Float64Array([1.1, -2.2, 3.3])) t.alike(state, enc.state(0, 25)) state.buffer = b4a.alloc(state.end) enc.float64array.encode(state, new Float64Array([1.1, -2.2, 3.3])) t.alike( state, enc.state( 25, 25, b4a.from([ 3, 0x9a, 0x99, 0x99, 0x99, 0x99, 0x99, 0xf1, 0x3f, 0x9a, 0x99, 0x99, 0x99, 0x99, 0x99, 0x01, 0xc0, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x0a, 0x40 ]) ) ) state.start = 0 t.alike(enc.float64array.decode(state), new Float64Array([1.1, -2.2, 3.3])) t.is(state.start, state.end) t.exception(() => enc.float64array.decode(state)) }) test('string', function (t) { const state = enc.state() enc.string.preencode(state, '🌾') t.alike(state, enc.state(0, 5)) enc.string.preencode(state, 'høsten er fin') t.alike(state, enc.state(0, 20)) state.buffer = b4a.alloc(state.end) enc.string.encode(state, '🌾') t.alike( state, enc.state(5, 20, b4a.from('\x04🌾\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')) ) enc.string.encode(state, 'høsten er fin') t.alike(state, enc.state(20, 20, b4a.from('\x04🌾\x0ehøsten er fin'))) state.start = 0 t.is(enc.string.decode(state), '🌾') t.is(enc.string.decode(state), 'høsten er fin') t.is(state.start, state.end) t.exception(() => enc.string.decode(state)) }) test('raw string', function (t) { const state = enc.state() enc.raw.string.preencode(state, 'hello') t.alike(state, enc.state(0, 5)) enc.raw.string.preencode(state, ' world') t.alike(state, enc.state(0, 11)) state.buffer = b4a.alloc(state.end) enc.raw.string.encode(state, 'hello') enc.raw.string.encode(state, ' world') t.alike(state, enc.state(11, 11, b4a.from('hello world'))) state.start = 0 t.is(enc.raw.string.decode(state), 'hello world') t.is(enc.raw.string.decode(state), '') }) test('raw buffer', function (t) { const state = enc.state() enc.raw.string.preencode(state, b4a.from('hello')) t.alike(state, enc.state(0, 5)) enc.raw.string.preencode(state, b4a.from(' world')) t.alike(state, enc.state(0, 11)) state.buffer = b4a.alloc(state.end) enc.raw.buffer.encode(state, b4a.from('hello')) enc.raw.buffer.encode(state, b4a.from(' world')) t.alike(state, enc.state(11, 11, b4a.from('hello world'))) state.start = 0 t.alike(enc.raw.buffer.decode(state), b4a.from('hello world')) t.alike(enc.raw.buffer.decode(state), b4a.alloc(0)) }) test('fixed8', function (t) { const state = enc.state() enc.fixed8.preencode(state, b4a.alloc(8).fill('a')) t.alike(state, enc.state(0, 8)) enc.fixed8.preencode(state, b4a.alloc(8).fill('b')) t.alike(state, enc.state(0, 16)) state.buffer = b4a.alloc(state.end) enc.fixed8.encode(state, b4a.alloc(8).fill('a')) t.alike(state, enc.state(8, 16, b4a.alloc(16).fill('a', 0, 8))) enc.fixed8.encode(state, b4a.alloc(8).fill('b')) t.alike(state, enc.state(16, 16, b4a.alloc(16).fill('a', 0, 8).fill('b', 8, 16))) state.start = 0 t.alike(enc.fixed8.decode(state), b4a.alloc(8).fill('a')) t.alike(enc.fixed8.decode(state), b4a.alloc(8).fill('b')) t.is(state.start, state.end) t.exception(() => enc.fixed8.decode(state)) }) test('fixed16', function (t) { const state = enc.state() enc.fixed16.preencode(state, b4a.alloc(16).fill('a')) t.alike(state, enc.state(0, 16)) enc.fixed16.preencode(state, b4a.alloc(16).fill('b')) t.alike(state, enc.state(0, 32)) state.buffer = b4a.alloc(state.end) enc.fixed16.encode(state, b4a.alloc(16).fill('a')) t.alike(state, enc.state(16, 32, b4a.alloc(32).fill('a', 0, 16))) enc.fixed16.encode(state, b4a.alloc(16).fill('b')) t.alike(state, enc.state(32, 32, b4a.alloc(32).fill('a', 0, 16).fill('b', 16, 32))) state.start = 0 t.alike(enc.fixed16.decode(state), b4a.alloc(16).fill('a')) t.alike(enc.fixed16.decode(state), b4a.alloc(16).fill('b')) t.is(state.start, state.end) t.exception(() => enc.fixed16.decode(state)) }) test('fixed24', function (t) { const state = enc.state() enc.fixed24.preencode(state, b4a.alloc(24).fill('a')) t.alike(state, enc.state(0, 24)) enc.fixed24.preencode(state, b4a.alloc(24).fill('b')) t.alike(state, enc.state(0, 48)) state.buffer = b4a.alloc(state.end) enc.fixed24.encode(state, b4a.alloc(24).fill('a')) t.alike(state, enc.state(24, 48, b4a.alloc(48).fill('a', 0, 24))) enc.fixed24.encode(state, b4a.alloc(24).fill('b')) t.alike(state, enc.state(48, 48, b4a.alloc(48).fill('a', 0, 24).fill('b', 24, 48))) state.start = 0 t.alike(enc.fixed24.decode(state), b4a.alloc(24).fill('a')) t.alike(enc.fixed24.decode(state), b4a.alloc(24).fill('b')) t.is(state.start, state.end) t.exception(() => enc.fixed24.decode(state)) }) test('fixed32', function (t) { const state = enc.state() enc.fixed32.preencode(state, b4a.alloc(32).fill('a')) t.alike(state, enc.state(0, 32)) enc.fixed32.preencode(state, b4a.alloc(32).fill('b')) t.alike(state, enc.state(0, 64)) state.buffer = b4a.alloc(state.end) enc.fixed32.encode(state, b4a.alloc(32).fill('a')) t.alike(state, enc.state(32, 64, b4a.alloc(64).fill('a', 0, 32))) enc.fixed32.encode(state, b4a.alloc(32).fill('b')) t.alike(state, enc.state(64, 64, b4a.alloc(64).fill('a', 0, 32).fill('b', 32, 64))) state.start = 0 t.alike(enc.fixed32.decode(state), b4a.alloc(32).fill('a')) t.alike(enc.fixed32.decode(state), b4a.alloc(32).fill('b')) t.is(state.start, state.end) t.exception(() => enc.fixed32.decode(state)) }) test('fixed64', function (t) { const state = enc.state() enc.fixed64.preencode(state, b4a.alloc(64).fill('a')) t.alike(state, enc.state(0, 64)) enc.fixed64.preencode(state, b4a.alloc(64).fill('b')) t.alike(state, enc.state(0, 128)) state.buffer = b4a.alloc(state.end) enc.fixed64.encode(state, b4a.alloc(64).fill('a')) t.alike(state, enc.state(64, 128, b4a.alloc(128).fill('a', 0, 64))) enc.fixed64.encode(state, b4a.alloc(64).fill('b')) t.alike(state, enc.state(128, 128, b4a.alloc(128).fill('a', 0, 64).fill('b', 64, 128))) state.start = 0 t.alike(enc.fixed64.decode(state), b4a.alloc(64).fill('a')) t.alike(enc.fixed64.decode(state), b4a.alloc(64).fill('b')) t.is(state.start, state.end) t.exception(() => enc.fixed64.decode(state)) }) test('fixed n', function (t) { const state = enc.state() const fixed = enc.fixed(3) fixed.preencode(state, b4a.alloc(3).fill('a')) t.alike(state, enc.state(0, 3)) fixed.preencode(state, b4a.alloc(3).fill('b')) t.alike(state, enc.state(0, 6)) state.buffer = b4a.alloc(state.end) fixed.encode(state, b4a.alloc(3).fill('a')) t.alike(state, enc.state(3, 6, b4a.alloc(6).fill('a', 0, 3))) fixed.encode(state, b4a.alloc(3).fill('b')) t.alike(state, enc.state(6, 6, b4a.alloc(6).fill('a', 0, 3).fill('b', 3, 6))) state.start = 0 t.alike(fixed.decode(state), b4a.alloc(3).fill('a')) t.alike(fixed.decode(state), b4a.alloc(3).fill('b')) t.is(state.start, state.end) t.exception(() => fixed.decode(state)) state.start = 4 t.exception(() => fixed.decode(state)) }) test('error for incorrect buffer sizes when encoding fixed-length buffers', function (t) { const smallbuf = b4a.from('aa', 'hex') const bigBuf = b4a.from('aa'.repeat(500), 'hex') t.exception(() => enc.encode(enc.fixed8, smallbuf), /Incorrect buffer size/) t.exception(() => enc.encode(enc.fixed16, smallbuf), /Incorrect buffer size/) t.exception(() => enc.encode(enc.fixed24, smallbuf), /Incorrect buffer size/) t.exception(() => enc.encode(enc.fixed32, smallbuf), /Incorrect buffer size/) t.exception(() => enc.encode(enc.fixed64, smallbuf), /Incorrect buffer size/) t.exception(() => enc.encode(enc.fixed(100), smallbuf), /Incorrect buffer size/) t.exception(() => enc.encode(enc.fixed8, bigBuf), /Incorrect buffer size/) t.exception(() => enc.encode(enc.fixed16, bigBuf), /Incorrect buffer size/) t.exception(() => enc.encode(enc.fixed24, bigBuf), /Incorrect buffer size/) t.exception(() => enc.encode(enc.fixed32, bigBuf), /Incorrect buffer size/) t.exception(() => enc.encode(enc.fixed64, bigBuf), /Incorrect buffer size/) t.exception(() => enc.encode(enc.fixed(100), bigBuf), /Incorrect buffer size/) }) test('array', function (t) { const state = enc.state() const arr = enc.array(enc.bool) arr.preencode(state, [true, false, true]) t.alike(state, enc.state(0, 4)) arr.preencode(state, [false, false, true, true]) t.alike(state, enc.state(0, 9)) state.buffer = b4a.alloc(state.end) arr.encode(state, [true, false, true]) t.alike(state, enc.state(4, 9, b4a.from([3, 1, 0, 1, 0, 0, 0, 0, 0]))) arr.encode(state, [false, false, true, true]) t.alike(state, enc.state(9, 9, b4a.from([3, 1, 0, 1, 4, 0, 0, 1, 1]))) state.start = 0 t.alike(arr.decode(state), [true, false, true]) t.alike(arr.decode(state), [false, false, true, true]) t.is(state.start, state.end) t.exception(() => arr.decode(state)) }) test('raw array', function (t) { const state = enc.state() const arr = enc.raw.array(enc.bool) arr.preencode(state, [true]) t.alike(state, enc.state(0, 1)) arr.preencode(state, [true]) t.alike(state, enc.state(0, 2)) state.buffer = b4a.alloc(state.end) arr.encode(state, [true]) t.alike(state, enc.state(1, 2, b4a.from([1, 0]))) arr.encode(state, [true]) t.alike(state, enc.state(2, 2, b4a.from([1, 1]))) state.start = 0 t.alike(arr.decode(state), [true, true]) t.alike(arr.decode(state), []) }) test('json', function (t) { const state = enc.state() enc.json.preencode(state, { a: 1, b: 2 }) t.alike(state, enc.state(0, 14)) state.buffer = b4a.alloc(state.end) enc.json.encode(state, { a: 1, b: 2 }) t.alike(state, enc.state(14, 14, b4a.concat([b4a.from([13]), b4a.from('{"a":1,"b":2}')]))) state.start = 0 t.alike(enc.json.decode(state), { a: 1, b: 2 }) t.exception(() => enc.json.decode(state)) }) test('raw json', function (t) { const state = enc.state() enc.raw.json.preencode(state, { a: 1, b: 2 }) t.alike(state, enc.state(0, 13)) state.buffer = b4a.alloc(state.end) enc.raw.json.encode(state, { a: 1, b: 2 }) t.alike(state, enc.state(13, 13, b4a.from('{"a":1,"b":2}'))) state.start = 0 t.alike(enc.raw.json.decode(state), { a: 1, b: 2 }) t.exception(() => enc.json.decode(state)) }) test('lexint: big numbers', function (t) { t.plan(1) let prev = enc.encode(enc.lexint, 0) let n let skip = 1 for (n = 1; n < Number.MAX_VALUE; n += skip) { const cur = enc.encode(enc.lexint, n) if (b4a.compare(cur, prev) < 1) break prev = cur skip = 1 + Math.pow(245, Math.ceil(Math.log(n) / Math.log(256))) } t.is(n, Infinity) }) test('lexint: range precision', function (t) { t.plan(2) const a = 1e55 const b = 1.0000000000001e55 const ha = enc.encode(enc.lexint, a).toString('hex') const hb = enc.encode(enc.lexint, b).toString('hex') t.not(a, b) t.not(ha, hb) }) test('lexint: range precision', function (t) { let prev = enc.encode(enc.lexint, 0) const skip = 0.000000001e55 for (let i = 0, n = 1e55; i < 1000; n = 1e55 + skip * ++i) { const cur = enc.encode(enc.lexint, n) if (b4a.compare(cur, prev) < 1) t.fail('cur <= prev') prev = cur } t.ok(true) t.end() }) test('lexint: small numbers', function (t) { let prev = enc.encode(enc.lexint, 0) for (let n = 1; n < 256 * 256 * 16; n++) { const cur = enc.encode(enc.lexint, n) if (b4a.compare(cur, prev) < 1) t.fail('cur <= prev') prev = cur } t.ok(true) t.end() }) test('lexint: throws', function (t) { t.exception(() => { enc.decode(enc.lexint, b4a.alloc(1, 251)) }) let num = 252 const state = enc.state() enc.lexint.preencode(state, num) state.buffer = b4a.alloc(state.end - state.start) enc.lexint.encode(state, num) t.exception(() => { enc.decode(enc.lexint, state.buffer.subarray(0, state.buffer.byteLength - 2)) }) num <<= 8 state.start = 0 state.end = 0 state.buffer = null enc.lexint.preencode(state, num) state.buffer = b4a.alloc(state.end - state.start) enc.lexint.encode(state, num) t.exception(() => { enc.decode(enc.lexint, state.buffer.subarray(0, state.buffer.byteLength - 2)) }) num <<= 8 state.start = 0 state.end = 0 state.buffer = null enc.lexint.preencode(state, num) state.buffer = b4a.alloc(state.end - state.start) enc.lexint.encode(state, num) t.exception(() => { enc.decode(enc.lexint, state.buffer.subarray(0, state.buffer.byteLength - 2)) }) num *= 256 state.start = 0 state.end = 0 state.buffer = null enc.lexint.preencode(state, num) state.buffer = b4a.alloc(state.end - state.start) enc.lexint.encode(state, num) t.exception(() => { enc.decode(enc.lexint, state.buffer.subarray(0, state.buffer.byteLength - 2)) }) num *= 256 * 256 state.start = 0 state.end = 0 state.buffer = null enc.lexint.preencode(state, num) state.buffer = b4a.alloc(state.end - state.start) enc.lexint.encode(state, num) t.exception(() => { enc.decode(enc.lexint, state.buffer.subarray(0, state.buffer.byteLength - 2)) }) t.end() }) test('lexint: refuses a value outside its range', function (t) { t.is(enc.decode(enc.lexint, enc.encode(enc.lexint, 0)), 0) t.ok(enc.decode(enc.lexint, enc.encode(enc.lexint, Number.MAX_VALUE)) > 0) for (const n of [-1, -0.5, -1e10, -Infinity, Infinity, NaN]) { t.exception.all( () => enc.encode(enc.lexint, n), { name: 'RangeError', message: /lexint/ }, String(n) ) t.exception.all( () => enc.lexint.encode(enc.state(0, 16, b4a.alloc(16)), n), { name: 'RangeError', message: /lexint/ }, String(n) ) } }) test('lexint: unpack', function (t) { let n let skip = 1 for (n = 1; n < Number.MAX_VALUE; n += skip) { const cur = enc.encode(enc.lexint, n) compare(n, enc.decode(enc.lexint, cur)) skip = 1 + Math.pow(245, Math.ceil(Math.log(n) / Math.log(256))) } t.is(n, Infinity) t.end() function compare(a, b) { const desc = a + ' !=~ ' + b if (/e\+\d+$/.test(a) || /e\+\d+$/.test(b)) { if ( String(a).slice(0, 8) !== String(b).slice(0, 8) || /e\+(\d+)$/.exec(a)[1] !== /e\+(\d+)$/.exec(b)[1] ) { t.fail(desc) } } else { if ( String(a).slice(0, 8) !== String(b).slice(0, 8) || String(a).length !== String(b).length ) { t.fail(desc) } } } }) test('date', function (t) { const d = new Date() t.alike(enc.decode(enc.date, enc.encode(enc.date, d)), d) }) test('any', function (t) { const o = { hello: 'world', num: 42, neg: -42, arr: [{ yes: 1 }, { no: false }], nest: {}, today: new Date(), float: 0.54 } t.alike(enc.decode(enc.any, enc.encode(enc.any, o)), o) const arr = new Array(3) t.alike(enc.decode(enc.any, enc.encode(enc.any, arr)), [null, null, null]) }) test('framed', function (t) { const e = enc.frame(enc.uint) t.alike(enc.encode(e, 42), b4a.from([0x01, 0x2a])) t.alike(enc.decode(e, b4a.from([0x01, 0x2a])), 42) t.alike(enc.encode(e, 4200), b4a.from([0x03, 0xfd, 0x68, 0x10])) t.alike(enc.decode(e, b4a.from([0x03, 0xfd, 0x68, 0x10])), 4200) }) test('framed - throws instead when length exceeds buffer', function (t) { // frame length 0x0A bytes but the buffer ends immediately after const buf = b4a.from([0x0a, 0x00]) const e = enc.frame(enc.raw.buffer) t.exception(() => enc.decode(e, buf), /Out of bounds/) }) test('port', function (t) { const p = 0x1234 const buf = Buffer.from([0x34, 0x12]) t.alike(enc.encode(enc.port, p), buf) t.alike(enc.decode(enc.port, buf), p) }) test('ipv4', function (t) { const ip = '1.2.3.4' const buf = Buffer.from([1, 2, 3, 4]) t.alike(enc.encode(enc.ipv4, ip), buf) t.alike(enc.decode(enc.ipv4, buf), ip) }) test('ipv4 + port', function (t) { const host = '1.2.3.4' const port = 1234 t.alike(enc.decode(enc.ipv4Address, enc.encode(enc.ipv4Address, { host, port })), { host, family: 4, port }) }) test('ipv6', function (t) { const ip = '1:2:3:4:5:6:7:8' const buf = Buffer.from([0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8]) t.alike(enc.encode(enc.ipv6, ip), buf) t.alike(enc.decode(enc.ipv6, buf), ip) t.test('abbreviated', function (t) { const buf = Buffer.from([0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 8]) t.alike(enc.encode(enc.ipv6, '1:2::7:8'), buf) t.alike(enc.decode(enc.ipv6, buf), '1:2:0:0:0:0:7:8') }) t.test('prefix abbreviated', function (t) { const buf = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 6, 0, 7, 0, 8]) t.alike(enc.encode(enc.ipv6, '::5:6:7:8'), buf) t.alike(enc.decode(enc.ipv6, buf), '0:0:0:0:5:6:7:8') }) t.test('suffix abbreviated', function (t) { const buf = Buffer.from([0, 1, 0, 2, 0, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0]) t.alike(enc.encode(enc.ipv6, '1:2:3:4::'), buf) t.alike(enc.decode(enc.ipv6, buf), '1:2:3:4:0:0:0:0') }) t.test('any', function (t) { const buf = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) t.alike(enc.encode(enc.ipv6, '::'), buf) t.alike(enc.decode(enc.ipv6, buf), '0:0:0:0:0:0:0:0') }) t.test('lowercase hex', function (t) { const buf = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xab, 0xcd]) t.alike(enc.encode(enc.ipv6, '::abcd'), buf) t.alike(enc.decode(enc.ipv6, buf), '0:0:0:0:0:0:0:abcd') }) t.test('uppercase hex', function (t) { const buf = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xab, 0xcd]) t.alike(enc.encode(enc.ipv6, '::ABCD'), buf) t.alike(enc.decode(enc.ipv6, buf), '0:0:0:0:0:0:0:abcd') }) }) test('ipv4 refuses a part its byte cannot hold', function (t) { t.is(enc.decode(enc.ipv4, enc.encode(enc.ipv4, '255.255.255.255')), '255.255.255.255') for (const ip of ['256.1.1.1', '1.2.3.300']) { t.exception.all(() => enc.encode(enc.ipv4, ip), { name: 'RangeError' }, ip) } }) test('ipv4 refuses an address without exactly 4 parts', function (t) { for (const ip of ['', '1.2', '1.2.3', '1.2.3.4.5']) { t.exception.all(() => enc.encode(enc.ipv4, ip), { name: 'RangeError' }, ip) } }) test('ipv6 refuses a group its 16 bits cannot hold', function (t) { t.is( enc.decode(enc.ipv6, enc.encode(enc.ipv6, 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff')), 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff' ) for (const ip of ['1::fffff', '10000:2:3:4:5:6:7:8']) { t.exception.all(() => enc.encode(enc.ipv6, ip), { name: 'RangeError' }, ip) } }) test('ipv6 refuses an address with the wrong number of groups', function (t) { for (const ip of ['1:2:3', '1:2:3:4:5:6:7:8:9', '::1:2:3:4:5:6:7:8']) { t.exception.all(() => enc.encode(enc.ipv6, ip), { name: 'RangeError' }, ip) } }) test('ipv6 + port', function (t) { const host = '1:2:3:4:5:6:7:8' const port = 1234 t.alike(enc.decode(enc.ipv6Address, enc.encode(enc.ipv6Address, { host, port })), { host, family: 6, port }) }) test('dual ip', function (t) { { const host = '1.2.3.4' t.alike(enc.decode(enc.ip, enc.encode(enc.ip, host)), host, 'ipv4') } { const host = '1:2:3:4:5:6:7:8' t.alike(enc.decode(enc.ip, enc.encode(enc.ip, host)), host, 'ipv6') } }) test('dual ip + port', function (t) { const port = 1234 { const host = '1.2.3.4' t.alike( enc.decode(enc.ipAddress, enc.encode(enc.ipAddress, { host, port })), { host, family: 4, port }, 'ipv4' ) } { const host = '1:2:3:4:5:6:7:8' t.alike( enc.decode(enc.ipAddress, enc.encode(enc.ipAddress, { host, port })), { host, family: 6, port }, 'ipv6' ) } }) test('record', function (t) { const encoding = enc.record(enc.string, enc.string) t.alike( enc.decode(encoding, enc.encode(encoding, { a: 'hello', b: 'world' })), Object.assign(Object.create(null), { a: 'hello', b: 'world' }) ) }) test('record - nested', function (t) { const encoding = enc.record(enc.string, enc.record(enc.string, enc.string)) t.alike( enc.decode( encoding, enc.encode(encoding, { a: { b: 'record' }, c: { d: 'nested', e: 'test' } }) ), Object.assign(Object.create(null), { a: Object.assign(Object.create(null), { b: 'record' }), c: Object.assign(Object.create(null), { d: 'nested', e: 'test' }) }) ) }) test('stringRecord', function (t) { t.alike( enc.decode(enc.stringRecord, enc.encode(enc.stringRecord, { a: 'hello', b: 'world' })), Object.assign(Object.create(null), { a: 'hello', b: 'world' }) ) }) test('orderedRecord', function (t) { const encoding = enc.orderedRecord(enc.string, enc.string) t.alike( enc.encode(encoding, { b: 'world', a: 'hello' }), enc.encode(encoding, { a: 'hello', b: 'world' }), 'the order the keys were set in does not reach the bytes' ) t.alike( enc.decode(encoding, enc.encode(encoding, { b: 'world', a: 'hello' })), Object.assign(Object.create(null), { a: 'hello', b: 'world' }) ) }) test('orderedRecord - keys sort by the bytes they encode to', function (t) { const encoding = enc.orderedRecord(enc.string, enc.uint) const astral = '\u{10000}' const replacement = '\uFFFD' t.alike( enc.encode(encoding, { [astral]: 1, [replacement]: 2 }), b4a.from('0203efbfbd0204f090808001', 'hex'), 'the replacement character sorts first, as its utf8 bytes do' ) t.alike( enc.encode(encoding, { ab: 1, b: 2 }), b4a.from('0202616201016202', 'hex'), 'a shorter key does not sort first for being shorter' ) }) test('orderedRecord - an unpaired surrogate sorts as the bytes replacing it', function (t) { const encoding = enc.orderedRecord(enc.string, enc.uint) const unpaired = '\uD800' const private_ = '\uE000' t.alike( enc.encode(encoding, { [unpaired]: 1, [private_]: 2 }), b4a.from('0203ee80800203efbfbd01', 'hex'), 'the unpaired surrogate is written as U+FFFD, so it sorts after U+E000' ) }) test('bitarray', function (t) { t.alike( enc.decode(enc.bitarray, enc.encode(enc.bitarray, [false, true])), [false, true], 'supports booleans as input' ) const big = [] while (big.length < 641) { big.push(Math.random() < 0.5) } t.alike(enc.decode(enc.bitarray, enc.encode(enc.bitarray, big)), big, 'fuzz big array') // Wire assertions t.alike( enc.encode(enc.bitarray, [0, 1, 0, 1]), b4a.from([4, 0b1010]), 'outputs size then bit packed bytes' ) t.alike( enc.encode(enc.bitarray, [0, 1, 0, 1, 1, 1, 1, 1, 1]), b4a.from([9, 0b11111010, 0b1]), 'encodes 9bits' ) t.alike( enc.decode(enc.bitarray, b4a.from([17, 0b11110000, 0b10101010, 0b1])), [ false, false, false, false, true, true, true, true, false, true, false, true, false, true, false, true, true ], 'decodes 17bits as booleans' ) t.alike(enc.decode(enc.bitarray, b4a.from([1, 0b0])), [false], 'decodes 1 bit as booleans') // OOB error t.exception( () => enc.decode(enc.bitarray, b4a.from([9, 0b10101010])), /Out of bounds/, 'throws error when passed buffer thats too small' ) }) test('out of range names the codec that carries the value', function (t) { const state = enc.state(0, 64, b4a.alloc(64)) t.exception.all(() => enc.uint.encode(state, 2 ** 60), { name: 'RangeError', message: /use biguint$/ }) t.exception.all(() => enc.int.encode(state, MAX_SAFE_INT + 1), { name: 'RangeError', message: /use bigint$/ }) t.exception.all(() => enc.uint64.encode(state, 2 ** 60), { name: 'RangeError', message: /use biguint64$/ }) t.exception.all(() => enc.int64.encode(state, MAX_SAFE_INT + 1), { name: 'RangeError', message: /use bigint64$/ }) }) test('out of range names nothing when no codec carries the value', function (t) { const state = enc.state(0, 64, b4a.alloc(64)) t.exception.all(() => enc.uint56.encode(state, 2 ** 53), { name: 'RangeError', message: /^uint must be between 0 and 9007199254740991$/ }) t.exception.all(() => enc.uint64be.encode(state, 2 ** 60), { name: 'RangeError', message: /^uint must be between 0 and 9007199254740991$/ }) t.exception.all(() => enc.int8.encode(state, 2 ** 60), { name: 'RangeError', message: /^int must be between -4503599627370496 and 4503599627370495$/ }) t.exception.all(() => enc.int56.encode(state, MAX_SAFE_INT + 1), { name: 'RangeError', message: /^int must be between -4503599627370496 and 4503599627370495$/ }) }) test('out of range on decode names the codec that carries the value', function (t) { const ones = (n) => b4a.from('ff'.repeat(n), 'hex') t.exception.all(() => enc.decode(enc.uint, b4a.concat([b4a.from([0xff]), ones(8)])), { name: 'RangeError', message: /use biguint$/ }) t.exception.all(() => enc.decode(enc.uint64, ones(8)), { name: 'RangeError', message: /use biguint64$/ }) t.exception.all(() => enc.decode(enc.int64, ones(8)), { name: 'RangeError', message: /^int must be between -4503599627370496 and 4503599627370495, use bigint64$/ }) }) test('out of range on decode names nothing when no codec carries the value', function (t) { const ones = (n) => b4a.from('ff'.repeat(n), 'hex') t.exception.all(() => enc.decode(enc.uint56, ones(7)), { name: 'RangeError', message: /^uint must be between 0 and 9007199254740991$/ }) t.exception.all(() => enc.decode(enc.uint64be, ones(8)), { name: 'RangeError', message: /^uint must be between 0 and 9007199254740991$/ }) t.exception.all(() => enc.decode(enc.int56, ones(7)), { name: 'RangeError', message: /^int must be between -4503599627370496 and 4503599627370495$/ }) }) function uint64(n, le) { const buf = b4a.alloc(8) const view = new DataView(buf.buffer, buf.byteOffset, 8) view.setBigUint64(0, n, le) return buf } test('the big-endian family writes big-endian bodies', function (t) { t.alike(enc.encode(enc.uint16be, 4200), b4a.from([0x10, 0x68])) t.alike(enc.encode(enc.int16be, -3), b4a.from([0x00, 0x05])) t.alike(enc.encode(enc.int32be, -3), b4a.from([0x00, 0x00, 0x00, 0x05])) t.alike(enc.encode(enc.int64be, -3), b4a.from([0, 0, 0, 0, 0, 0, 0, 5])) t.alike(enc.encode(enc.uintbe, 42), b4a.from([42])) t.alike(enc.encode(enc.uintbe, 4200), b4a.from([0xfd, 0x10, 0x68])) t.alike(enc.encode(enc.uintbe, 0x10000), b4a.from([0xfe, 0x00, 0x01, 0x00, 0x00])) t.alike(enc.encode(enc.uintbe, 0x200000000), b4a.from([0xff, 0, 0, 0, 2, 0, 0, 0, 0])) t.alike(enc.encode(enc.intbe, -3), b4a.from([5])) }) test('the big-endian family round-trips', function (t) { const cases = [ ['uint16be', 0], ['uint16be', 65535], ['int16be', -32768], ['int16be', 32767], ['int32be', -2147483648], ['int32be', 2147483647], ['int64be', MIN_SAFE_INT], ['int64be', MAX_SAFE_INT], ['uintbe', 0], ['uintbe', 0xfc], ['uintbe', 0xfd], ['uintbe', 0xffff], ['uintbe', 0x10000], ['uintbe', 0xffffffff], ['uintbe', 0x100000000], ['intbe', MIN_SAFE_INT], ['intbe', MAX_SAFE_INT], ['intbe', -1] ] for (const [name, n] of cases) { t.is(enc.decode(enc[name], enc.encode(enc[name], n)), n, name + ' ' + n) } }) test('the fixed-width big-endian codecs refuse a value their width cannot hold', function (t) { t.exception.all(() => enc.encode(enc.uint16be, 65536), { name: 'RangeError' }) t.exception.all(() => enc.encode(enc.int16be, 32768), { name: 'RangeError' }) t.exception.all(() => enc.encode(enc.int16be, -32769), { name: 'RangeError' }) t.exception.all(() => enc.encode(enc.int32be, 2147483648), { name: 'RangeError' }) }) test('the big-endian family refuses a truncated value', function (t) { t.exception(() => enc.decode(enc.uint16be, b4a.alloc(1))) t.exception(() => enc.decode(enc.int64be, b4a.alloc(7))) // each framing prefix promises a body the buffer does not carry t.exception(() => enc.decode(enc.uintbe, b4a.from([0xfd, 0x00]))) t.exception(() => enc.decode(enc.uintbe, b4a.from([0xfe, 0x00, 0x00]))) t.exception(() => enc.decode(enc.uintbe, b4a.from([0xff, 0x00, 0x00, 0x00]))) t.exception(() => enc.decode(enc.intbe, b4a.from([0xfd, 0x00]))) }) test('the big-endian family names nothing out of range', function (t) { const state = enc.state(0, 64, b4a.alloc(64)) const intRange = /^int must be between -4503599627370496 and 4503599627370495$/ const uintRange = /^uint must be between 0 and 9007199254740991$/ t.exception.all(() => enc.int64be.encode(state, MAX_SAFE_INT + 1), { name: 'RangeError', message: intRange }) t.exception.all(() => enc.uintbe.encode(state, 2 ** 60), { name: 'RangeError', message: uintRange }) t.exception.all(() => enc.intbe.encode(state, MAX_SAFE_INT + 1), { name: 'RangeError', message: intRange }) const ones = b4a.from('ff'.repeat(8), 'hex') t.exception.all(() => enc.decode(enc.int64be, ones), { name: 'RangeError', message: intRange }) t.exception.all(() => enc.decode(enc.uintbe, b4a.concat([b4a.from([0xff]), ones])), { name: 'RangeError', message: uintRange }) })