diff --git a/.gitignore b/.gitignore index 319db68..f56d8a7 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ .airtap.yml *.log node_modules/ +perf/bundle.js +package-lock.json diff --git a/AUTHORS.md b/AUTHORS.md index 3f4918c..2c4acbf 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -69,6 +69,13 @@ - jkkang (jkkang@smartauth.kr) - Deklan Webster (deklanw@gmail.com) - Martin Heidegger (martin.heidegger@gmail.com) -- junderw (junderwood@bitcoinbank.co.jp) +- André Werlang (589286+awerlang@users.noreply.github.com) +- Bradley Odell (btodell@hotmail.com) +- Dominik Moritz (domoritz@gmail.com) +- Rachel Simone Weil (partytimehexcellent@gmail.com) +- Patrick McAndrew (urg@users.noreply.github.com) +- Jonathan Underwood (jonathan.underwood4649@gmail.com) +- Christopher Jeffrey (JJ) (chjjeffrey@gmail.com) +- George MacKerron (george@mackerron.co.uk) #### Generated by bin/update-authors.sh. diff --git a/index.js b/index.js index bdea604..1d4fb9c 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,7 @@ 'use strict' -const base64 = require('base64-js') +const hextreme = require('hextreme') const ieee754 = require('ieee754') const customInspectSymbol = (typeof Symbol === 'function' && typeof Symbol['for'] === 'function') // eslint-disable-line dot-notation @@ -847,20 +847,10 @@ function hexWrite (buf, string, offset, length) { length = strLen >>> 1 } - for (let i = 0; i < length; ++i) { - const a = string.charCodeAt(i * 2 + 0) - const b = string.charCodeAt(i * 2 + 1) - const hi = hexCharValueTable[a & 0x7f] - const lo = hexCharValueTable[b & 0x7f] - - if ((a | b | hi | lo) & ~0x7f) { - return i - } - - buf[offset + i] = (hi << 4) | lo - } - - return length + if (string.length > length << 1) string = string.slice(0, length << 1) + const data = hextreme.fromHex(string, { onInvalidInput: 'truncate' }) + buf.set(data, offset) + return data.length } function utf8Write (buf, string, offset, length) { @@ -957,9 +947,9 @@ Buffer.prototype.toJSON = function toJSON () { function base64Slice (buf, start, end) { if (start === 0 && end === buf.length) { - return base64.fromByteArray(buf) + return hextreme.toBase64(buf) } else { - return base64.fromByteArray(buf.slice(start, end)) + return hextreme.toBase64(buf.slice(start, end)) } } @@ -1088,11 +1078,11 @@ function hexSlice (buf, start, end) { if (!start || start < 0) start = 0 if (!end || end < 0 || end > len) end = len - let out = '' - for (let i = start; i < end; ++i) { - out += hexSliceLookupTable[buf[i]] + if (start === 0 && end === buf.length) { + return hextreme.toHex(buf) + } else { + return hextreme.toHex(buf.slice(start, end)) } - return out } function utf16leSlice (buf, start, end) { @@ -1943,22 +1933,6 @@ function boundsError (value, length, type) { // HELPER FUNCTIONS // ================ -const INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g - -function base64clean (str) { - // Node takes equal signs as end of the Base64 encoding - str = str.split('=')[0] - // Node strips out invalid characters like \n and \t from the string, base64-js does not - str = str.trim().replace(INVALID_BASE64_RE, '') - // Node converts strings with length < 2 to '' - if (str.length < 2) return '' - // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not - while (str.length % 4 !== 0) { - str = str + '=' - } - return str -} - function utf8ToBytes (string, units) { units = units || Infinity let codePoint @@ -2065,7 +2039,7 @@ function utf16leToBytes (str, units) { } function base64ToBytes (str) { - return base64.toByteArray(base64clean(str)) + return hextreme.fromBase64(str, { onInvalidInput: 'skip', alphabet: 'base64any' }) } function blitBuffer (src, dst, offset, length) { @@ -2091,42 +2065,6 @@ function numberIsNaN (obj) { return obj !== obj // eslint-disable-line no-self-compare } -// Create lookup table for `toString('hex')` -// See: https://github.com/feross/buffer/issues/219 -const hexSliceLookupTable = (function () { - const alphabet = '0123456789abcdef' - const table = new Array(256) - for (let i = 0; i < 16; ++i) { - const i16 = i * 16 - for (let j = 0; j < 16; ++j) { - table[i16 + j] = alphabet[i] + alphabet[j] - } - } - return table -})() - -// hex lookup table for Buffer.from(x, 'hex') -/* eslint-disable no-multi-spaces, indent */ -const hexCharValueTable = [ - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - 0, 1, 2, 3, 4, 5, 6, 7, - 8, 9, -1, -1, -1, -1, -1, -1, - -1, 10, 11, 12, 13, 14, 15, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, 10, 11, 12, 13, 14, 15, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1 -] -/* eslint-enable no-multi-spaces, indent */ - // Return not function with Error if BigInt not supported function defineBigIntMethod (fn) { return typeof BigInt === 'undefined' ? BufferBigIntNotDefined : fn diff --git a/package.json b/package.json index 5ee6b8d..4665e16 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "James Halliday " ], "dependencies": { - "base64-js": "^1.3.1", + "hextreme": "^1.0.5", "ieee754": "^1.2.1" }, "devDependencies": { @@ -59,7 +59,7 @@ }, "scripts": { "perf": "browserify --debug perf/bracket-notation.js > perf/bundle.js && open perf/index.html", - "perf-node": "node perf/bracket-notation.js && node perf/concat.js && node perf/copy-big.js && node perf/copy.js && node perf/new-big.js && node perf/new.js && node perf/readDoubleBE.js && node perf/readFloatBE.js && node perf/readUInt32LE.js && node perf/slice.js && node perf/writeFloatBE.js && node perf/write-hex.js", + "perf-node": "node perf/toString-hex.js && node perf/from-hex.js && node perf/toString-base64.js && node perf/from-base64.js && node perf/bracket-notation.js && node perf/concat.js && node perf/copy-big.js && node perf/copy.js && node perf/new-big.js && node perf/new.js && node perf/readDoubleBE.js && node perf/readFloatBE.js && node perf/readUInt32LE.js && node perf/slice.js && node perf/writeFloatBE.js && node perf/write-hex.js", "size": "browserify -r ./ | uglifyjs -c -m | gzip | wc -c", "standard": "standard", "test": "tape test/*.js test/node/*.js", diff --git a/perf/from-base64.js b/perf/from-base64.js new file mode 100644 index 0000000..8e0ad36 --- /dev/null +++ b/perf/from-base64.js @@ -0,0 +1,22 @@ +const BrowserBuffer = require('../').Buffer // (this module) +const util = require('./util') +const suite = util.suite() + +const LENGTH = 4096 +const browserSubject = BrowserBuffer.alloc(LENGTH) + +for (let i = 0; i < LENGTH; i++) { + browserSubject[i] = (Math.random() * 255) << 0 +} + +const base64 = browserSubject.toString('base64') + +suite + .add('BrowserBuffer#from(base64String, "base64")', function () { + BrowserBuffer.from(base64, 'base64') + }) + +if (!process.browser) suite + .add('NodeBuffer#from(base64String, "base64")', function () { + Buffer.from(base64, 'base64') + }) diff --git a/perf/from-hex.js b/perf/from-hex.js new file mode 100644 index 0000000..d48a02d --- /dev/null +++ b/perf/from-hex.js @@ -0,0 +1,22 @@ +const BrowserBuffer = require('../').Buffer // (this module) +const util = require('./util') +const suite = util.suite() + +const LENGTH = 4096 +const browserSubject = BrowserBuffer.alloc(LENGTH) + +for (let i = 0; i < LENGTH; i++) { + browserSubject[i] = (Math.random() * 255) << 0 +} + +const hex = browserSubject.toString('hex') + +suite + .add('BrowserBuffer#from(hexString, "hex")', function () { + BrowserBuffer.from(hex, 'hex') + }) + +if (!process.browser) suite + .add('NodeBuffer#from(hexString, "hex")', function () { + Buffer.from(hex, 'hex') + }) diff --git a/perf/toString-base64.js b/perf/toString-base64.js new file mode 100644 index 0000000..6ecbb01 --- /dev/null +++ b/perf/toString-base64.js @@ -0,0 +1,21 @@ +const BrowserBuffer = require('../').Buffer // (this module) +const util = require('./util') +const suite = util.suite() + +const LENGTH = 4096 +const browserSubject = BrowserBuffer.alloc(LENGTH) +const nodeSubject = Buffer.alloc(LENGTH) + +for (let i = 0; i < LENGTH; i++) { + browserSubject[i] = nodeSubject[i] = (Math.random() * 255) << 0 +} + +suite + .add('BrowserBuffer#toString("base64")', function () { + browserSubject.toString('base64') + }) + +if (!process.browser) suite + .add('NodeBuffer#toString("base64")', function () { + nodeSubject.toString('base64') + }) diff --git a/perf/toString-hex.js b/perf/toString-hex.js new file mode 100644 index 0000000..033ae02 --- /dev/null +++ b/perf/toString-hex.js @@ -0,0 +1,21 @@ +const BrowserBuffer = require('../').Buffer // (this module) +const util = require('./util') +const suite = util.suite() + +const LENGTH = 4096 +const browserSubject = BrowserBuffer.alloc(LENGTH) +const nodeSubject = Buffer.alloc(LENGTH) + +for (let i = 0; i < LENGTH; i++) { + browserSubject[i] = nodeSubject[i] = (Math.random() * 255) << 0 +} + +suite + .add('BrowserBuffer#toString("hex")', function () { + browserSubject.toString('hex') + }) + +if (!process.browser) suite + .add('NodeBuffer#toString("hex")', function () { + nodeSubject.toString('hex') + })