diff --git a/lib/encode.js b/lib/encode.js index 7df9d4a..50c8fe7 100644 --- a/lib/encode.js +++ b/lib/encode.js @@ -168,6 +168,20 @@ function _encode(bytes, defers, value) { return 1; } + // Uint64BE (https://github.com/kawanet/int64-buffer) + if (value._isUint64BE) { + value = value.toArray(); + bytes.push(0xcf, ...value); + return 9; + } + + // Int64BE (https://github.com/kawanet/int64-buffer) + if (value._isInt64BE) { + value = value.toArray(); + bytes.push(0xd3, ...value); + return 9; + } + if (Array.isArray(value)) { length = value.length; diff --git a/package-lock.json b/package-lock.json index f0906d0..2fc0042 100644 --- a/package-lock.json +++ b/package-lock.json @@ -606,9 +606,9 @@ "dev": true }, "int64-buffer": { - "version": "0.1.9", - "resolved": "https://registry.npmjs.org/int64-buffer/-/int64-buffer-0.1.9.tgz", - "integrity": "sha1-ngOdoEOyT3ixlrKD4EZT716ZD2E=", + "version": "0.99.1007", + "resolved": "https://registry.npmjs.org/int64-buffer/-/int64-buffer-0.99.1007.tgz", + "integrity": "sha512-XDBEu44oSTqlvCSiOZ/0FoUkpWu/vwjJLGSKDabNISPQNZ5wub1FodGHBljRsrR0IXRPq7SslshZYMuA55CgTQ==", "dev": true }, "is-buffer": { @@ -917,6 +917,12 @@ "isarray": "^1.0.0" }, "dependencies": { + "int64-buffer": { + "version": "0.1.10", + "resolved": "https://registry.npmjs.org/int64-buffer/-/int64-buffer-0.1.10.tgz", + "integrity": "sha1-J3siiofZWtd30HwTgyAiQGpHNCM=", + "dev": true + }, "isarray": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", diff --git a/package.json b/package.json index 1124bda..fc06cdf 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "benchmark": "^2.1.2", "benchtable": "^0.1.0", "chai": "^3.5.0", + "int64-buffer": "^0.99.1007", "istanbul": "^0.4.5", "jshint": "^2.9.5", "mocha": "^3.2.0", diff --git a/test/test.js b/test/test.js index 6759274..b872c84 100644 --- a/test/test.js +++ b/test/test.js @@ -2,6 +2,7 @@ const notepack = require('../'); const expect = require('chai').expect; +const { Int64BE, Uint64BE } = require('int64-buffer'); function array(length) { const arr = new Array(length); @@ -153,6 +154,14 @@ describe('notepack', function () { check(Math.pow(2, 63) + 1024, 'cf8000000000000000'); }); + it('Uint64BE', function () { + checkEncode(new Uint64BE(4294967296), 'cf0000000100000000'); + checkEncode(new Uint64BE(Math.pow(2, 53) - 1), 'cf001fffffffffffff'); + // unsafe unsigned integer + checkEncode(new Uint64BE('18446744073709551615'), 'cfffffffffffffffff'); + checkEncode(new Uint64BE('18446744073709551614'), 'cffffffffffffffffe'); + }); + // NOTE: We'll always encode a positive number as a uint, but we should be // able to decode a positive int value @@ -206,6 +215,21 @@ describe('notepack', function () { check(-Math.pow(2, 63) - 1024, 'd38000000000000000'); }); + it('Int64BE', function () { + checkEncode(new Int64BE(-2147483649), 'd3ffffffff7fffffff'); + checkEncode(new Int64BE(-4294967297), 'd3fffffffeffffffff'); + checkEncode(new Int64BE(-65437650001231), 'd3ffffc47c1c1de2b1'); + checkEncode(new Int64BE(-1111111111111111), 'd3fffc0d7348ea8e39'); + checkEncode(new Int64BE(-1532678092380345), 'd3fffa8e0992bfa747'); + checkEncode(new Int64BE(-4503599627370496), 'd3fff0000000000000'); + checkEncode(new Int64BE(-7840340234323423), 'd3ffe42540896a3a21'); + // Minimum safe signed integer + checkEncode(new Int64BE(-Math.pow(2, 53) + 1), 'd3ffe0000000000001'); + // unsafe signed integer + checkEncode(new Int64BE('-9223372036854775808'), 'd38000000000000000'); + checkEncode(new Int64BE('-9223372036854775807'), 'd38000000000000001'); + }); + it('fixext 1 / undefined', function () { check(undefined, 'd40000'); checkDecode([127, Buffer.from('a')], 'd4' + '7f' + '61');