From b28f45aa0142e17f6ab834f12e452ae2b63a948d Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Fri, 16 Feb 2018 00:12:41 -0800 Subject: [PATCH] Support Uint8Array arguments to buf.compare() (test-buffer-compare.js) --- index.js | 11 ++++++-- test/node/test-buffer-compare.js | 48 ++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 test/node/test-buffer-compare.js diff --git a/index.js b/index.js index 4b98dea..2dfbefe 100644 --- a/index.js +++ b/index.js @@ -306,8 +306,12 @@ Buffer.isBuffer = function isBuffer (b) { } Buffer.compare = function compare (a, b) { + if (isInstance(a, Uint8Array)) a = Buffer.from(a, a.offset, a.byteLength) + if (isInstance(b, Uint8Array)) b = Buffer.from(b, b.offset, b.byteLength) if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) { - throw new TypeError('Arguments must be Buffers') + throw new TypeError( + 'The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array' + ) } if (a === b) return 0 @@ -368,7 +372,7 @@ Buffer.concat = function concat (list, length) { var pos = 0 for (i = 0; i < list.length; ++i) { var buf = list[i] - if (ArrayBuffer.isView(buf)) { + if (isInstance(buf, Uint8Array)) { buf = Buffer.from(buf) } if (!Buffer.isBuffer(buf)) { @@ -576,6 +580,9 @@ Buffer.prototype.inspect = function inspect () { } Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) { + if (isInstance(target, Uint8Array)) { + target = Buffer.from(target, target.offset, target.byteLength) + } if (!Buffer.isBuffer(target)) { throw new TypeError( 'The "target" argument must be one of type Buffer or Uint8Array. ' + diff --git a/test/node/test-buffer-compare.js b/test/node/test-buffer-compare.js new file mode 100644 index 0000000..c5d2872 --- /dev/null +++ b/test/node/test-buffer-compare.js @@ -0,0 +1,48 @@ +'use strict'; +var Buffer = require('../../').Buffer; + +const common = require('./common'); +const assert = require('assert'); + +const b = Buffer.alloc(1, 'a'); +const c = Buffer.alloc(1, 'c'); +const d = Buffer.alloc(2, 'aa'); +const e = new Uint8Array([ 0x61, 0x61 ]); // ASCII 'aa', same as d + +assert.strictEqual(b.compare(c), -1); +assert.strictEqual(c.compare(d), 1); +assert.strictEqual(d.compare(b), 1); +assert.strictEqual(d.compare(e), 0); +assert.strictEqual(b.compare(d), -1); +assert.strictEqual(b.compare(b), 0); + +assert.strictEqual(Buffer.compare(b, c), -1); +assert.strictEqual(Buffer.compare(c, d), 1); +assert.strictEqual(Buffer.compare(d, b), 1); +assert.strictEqual(Buffer.compare(b, d), -1); +assert.strictEqual(Buffer.compare(c, c), 0); +assert.strictEqual(Buffer.compare(e, e), 0); +assert.strictEqual(Buffer.compare(d, e), 0); +assert.strictEqual(Buffer.compare(d, b), 1); + +assert.strictEqual(Buffer.compare(Buffer.alloc(0), Buffer.alloc(0)), 0); +assert.strictEqual(Buffer.compare(Buffer.alloc(0), Buffer.alloc(1)), -1); +assert.strictEqual(Buffer.compare(Buffer.alloc(1), Buffer.alloc(0)), 1); + +const errMsg = common.expectsError({ + code: 'ERR_INVALID_ARG_TYPE', + type: TypeError, + message: 'The "buf1", "buf2" arguments must be one of ' + + 'type Buffer or Uint8Array' +}, 2); +assert.throws(() => Buffer.compare(Buffer.alloc(1), 'abc'), errMsg); + +assert.throws(() => Buffer.compare('abc', Buffer.alloc(1)), errMsg); + +common.expectsError(() => Buffer.alloc(1).compare('abc'), { + code: 'ERR_INVALID_ARG_TYPE', + type: TypeError, + message: 'The "target" argument must be one of ' + + 'type Buffer or Uint8Array. Received type string' +}); + -- 2.34.1