GeezerVison is showing github.com — buffer: allow Uint8Array input to methods · nodejs/node@beca324
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/buffer.md +12-12Lines changed: 12 additions & 12 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -635,8 +635,8 @@ actual byte length is returned. | 635 | 635 | added: v0.11.13 | 636 | 636 | --> | 637 | 637 |
| 638 | | -* `buf1` {Buffer} | 639 | | -* `buf2` {Buffer} | | 638 | +* `buf1` {Buffer|Uint8Array} | | 639 | +* `buf2` {Buffer|Uint8Array} | 640 | 640 | * Returns: {Integer} | 641 | 641 |
| 642 | 642 | Compares `buf1` to `buf2` typically for the purpose of sorting arrays of | @@ -660,7 +660,7 @@ console.log(arr.sort(Buffer.compare)); | 660 | 660 | added: v0.7.11 | 661 | 661 | --> | 662 | 662 |
| 663 | | -* `list` {Array} List of `Buffer` instances to concat | | 663 | +* `list` {Array} List of `Buffer` or [`Uint8Array`] instances to concat | 664 | 664 | * `totalLength` {Integer} Total length of the `Buffer` instances in `list` | 665 | 665 | when concatenated | 666 | 666 | * Returns: {Buffer} | @@ -882,7 +882,7 @@ console.log(buf.toString('ascii')); | 882 | 882 | added: v0.11.13 | 883 | 883 | --> | 884 | 884 |
| 885 | | -* `target` {Buffer} A `Buffer` to compare to | | 885 | +* `target` {Buffer|Uint8Array} A `Buffer` or [`Uint8Array`] to compare to | 886 | 886 | * `targetStart` {Integer} The offset within `target` at which to begin | 887 | 887 | comparison. **Default:** `0` | 888 | 888 | * `targetEnd` {Integer} The offset with `target` at which to end comparison | @@ -1037,7 +1037,7 @@ for (const pair of buf.entries()) { | 1037 | 1037 | added: v0.11.13 | 1038 | 1038 | --> | 1039 | 1039 |
| 1040 | | -* `otherBuffer` {Buffer} A `Buffer` to compare to | | 1040 | +* `otherBuffer` {Buffer} A `Buffer` or [`Uint8Array`] to compare to | 1041 | 1041 | * Returns: {Boolean} | 1042 | 1042 |
| 1043 | 1043 | Returns `true` if both `buf` and `otherBuffer` have exactly the same bytes, | @@ -1099,7 +1099,7 @@ console.log(Buffer.allocUnsafe(3).fill('\u0222')); | 1099 | 1099 | added: v1.5.0 | 1100 | 1100 | --> | 1101 | 1101 |
| 1102 | | -* `value` {String | Buffer | Integer} What to search for | | 1102 | +* `value` {String | Buffer | Uint8Array | Integer} What to search for | 1103 | 1103 | * `byteOffset` {Integer} Where to begin searching in `buf`. **Default:** `0` | 1104 | 1104 | * `encoding` {String} If `value` is a string, this is its encoding. | 1105 | 1105 | **Default:** `'utf8'` | @@ -1110,8 +1110,8 @@ If `value` is: | 1110 | 1110 |
| 1111 | 1111 | * a string, `value` is interpreted according to the character encoding in | 1112 | 1112 | `encoding`. | 1113 | | - * a `Buffer`, `value` will be used in its entirety. To compare a partial | 1114 | | - `Buffer` use [`buf.slice()`]. | | 1113 | + * a `Buffer` or [`Uint8Array`], `value` will be used in its entirety. | | 1114 | + To compare a partial `Buffer`, use [`buf.slice()`]. | 1115 | 1115 | * a number, `value` will be interpreted as an unsigned 8-bit integer | 1116 | 1116 | value between `0` and `255`. | 1117 | 1117 |
| @@ -1221,7 +1221,7 @@ for (const key of buf.keys()) { | 1221 | 1221 | added: v6.0.0 | 1222 | 1222 | --> | 1223 | 1223 |
| 1224 | | -* `value` {String | Buffer | Integer} What to search for | | 1224 | +* `value` {String | Buffer | Uint8Array | Integer} What to search for | 1225 | 1225 | * `byteOffset` {Integer} Where to begin searching in `buf`. | 1226 | 1226 | **Default:** [`buf.length`]` - 1` | 1227 | 1227 | * `encoding` {String} If `value` is a string, this is its encoding. | @@ -2313,12 +2313,12 @@ Note that this is a property on the `buffer` module returned by | 2313 | 2313 | added: v7.1.0 | 2314 | 2314 | --> | 2315 | 2315 |
| 2316 | | -* `source` {Buffer} A `Buffer` instance | | 2316 | +* `source` {Buffer|Uint8Array} A `Buffer` or `Uint8Array` instance | 2317 | 2317 | * `fromEnc` {String} The current encoding | 2318 | 2318 | * `toEnc` {String} To target encoding | 2319 | 2319 |
| 2320 | | -Re-encodes the given `Buffer` instance from one character encoding to another. | 2321 | | -Returns a new `Buffer` instance. | | 2320 | +Re-encodes the given `Buffer` or `Uint8Array` instance from one character | | 2321 | +encoding to another. Returns a new `Buffer` instance. | 2322 | 2322 |
| 2323 | 2323 | Throws if the `fromEnc` or `toEnc` specify invalid character encodings or if | 2324 | 2324 | conversion from `fromEnc` to `toEnc` is not permitted. | |
Collapse file Copy file name to clipboardExpand all lines: lib/buffer.js +25-18Lines changed: 25 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 | 'use strict'; | 3 | 3 |
| 4 | 4 | const binding = process.binding('buffer'); | 5 | | -const { isArrayBuffer, isSharedArrayBuffer } = process.binding('util'); | | 5 | +const { isArrayBuffer, isSharedArrayBuffer, isUint8Array } = | | 6 | + process.binding('util'); | 6 | 7 | const bindingObj = {}; | 7 | 8 | const internalUtil = require('internal/util'); | 8 | 9 |
| @@ -251,13 +252,13 @@ function fromArrayBuffer(obj, byteOffset, length) { | 251 | 252 | } | 252 | 253 |
| 253 | 254 | function fromObject(obj) { | 254 | | - if (obj instanceof Buffer) { | | 255 | + if (isUint8Array(obj)) { | 255 | 256 | const b = allocate(obj.length); | 256 | 257 |
| 257 | 258 | if (b.length === 0) | 258 | 259 | return b; | 259 | 260 |
| 260 | | - obj.copy(b, 0, 0, obj.length); | | 261 | + binding.copy(obj, b, 0, 0, obj.length); | 261 | 262 | return b; | 262 | 263 | } | 263 | 264 |
| @@ -287,9 +288,8 @@ Buffer.isBuffer = function isBuffer(b) { | 287 | 288 |
| 288 | 289 |
| 289 | 290 | Buffer.compare = function compare(a, b) { | 290 | | - if (!(a instanceof Buffer) || | 291 | | - !(b instanceof Buffer)) { | 292 | | - throw new TypeError('Arguments must be Buffers'); | | 291 | + if (!isUint8Array(a) || !isUint8Array(b)) { | | 292 | + throw new TypeError('Arguments must be Buffers or Uint8Arrays'); | 293 | 293 | } | 294 | 294 |
| 295 | 295 | if (a === b) { | @@ -306,10 +306,13 @@ Buffer.isEncoding = function(encoding) { | 306 | 306 | }; | 307 | 307 | Buffer[internalUtil.kIsEncodingSymbol] = Buffer.isEncoding; | 308 | 308 |
| | 309 | +const kConcatErrMsg = '"list" argument must be an Array ' + | | 310 | + 'of Buffer or Uint8Array instances'; | | 311 | + | 309 | 312 | Buffer.concat = function(list, length) { | 310 | 313 | var i; | 311 | 314 | if (!Array.isArray(list)) | 312 | | - throw new TypeError('"list" argument must be an Array of Buffers'); | | 315 | + throw new TypeError(kConcatErrMsg); | 313 | 316 |
| 314 | 317 | if (list.length === 0) | 315 | 318 | return new FastBuffer(); | @@ -326,9 +329,9 @@ Buffer.concat = function(list, length) { | 326 | 329 | var pos = 0; | 327 | 330 | for (i = 0; i < list.length; i++) { | 328 | 331 | var buf = list[i]; | 329 | | - if (!Buffer.isBuffer(buf)) | 330 | | - throw new TypeError('"list" argument must be an Array of Buffers'); | 331 | | - buf.copy(buffer, pos); | | 332 | + if (!isUint8Array(buf)) | | 333 | + throw new TypeError(kConcatErrMsg); | | 334 | + binding.copy(buf, buffer, pos); | 332 | 335 | pos += buf.length; | 333 | 336 | } | 334 | 337 |
| @@ -495,6 +498,9 @@ function slowToString(encoding, start, end) { | 495 | 498 | } | 496 | 499 | } | 497 | 500 |
| | 501 | +Buffer.prototype.copy = function(target, targetStart, sourceStart, sourceEnd) { | | 502 | + return binding.copy(this, target, targetStart, sourceStart, sourceEnd); | | 503 | +}; | 498 | 504 |
| 499 | 505 | Buffer.prototype.toString = function() { | 500 | 506 | let result; | @@ -510,8 +516,8 @@ Buffer.prototype.toString = function() { | 510 | 516 |
| 511 | 517 |
| 512 | 518 | Buffer.prototype.equals = function equals(b) { | 513 | | - if (!(b instanceof Buffer)) | 514 | | - throw new TypeError('Argument must be a Buffer'); | | 519 | + if (!isUint8Array(b)) | | 520 | + throw new TypeError('Argument must be a Buffer or Uint8Array'); | 515 | 521 |
| 516 | 522 | if (this === b) | 517 | 523 | return true; | @@ -539,8 +545,8 @@ Buffer.prototype.compare = function compare(target, | 539 | 545 | thisStart, | 540 | 546 | thisEnd) { | 541 | 547 |
| 542 | | - if (!(target instanceof Buffer)) | 543 | | - throw new TypeError('Argument must be a Buffer'); | | 548 | + if (!isUint8Array(target)) | | 549 | + throw new TypeError('Argument must be a Buffer or Uint8Array'); | 544 | 550 |
| 545 | 551 | if (start === undefined) | 546 | 552 | start = 0; | @@ -604,13 +610,14 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) { | 604 | 610 | return binding.indexOfString(buffer, val, byteOffset, encoding, dir); | 605 | 611 | } | 606 | 612 | return slowIndexOf(buffer, val, byteOffset, encoding, dir); | 607 | | - } else if (val instanceof Buffer) { | | 613 | + } else if (isUint8Array(val)) { | 608 | 614 | return binding.indexOfBuffer(buffer, val, byteOffset, encoding, dir); | 609 | 615 | } else if (typeof val === 'number') { | 610 | 616 | return binding.indexOfNumber(buffer, val, byteOffset, dir); | 611 | 617 | } | 612 | 618 |
| 613 | | - throw new TypeError('"val" argument must be string, number or Buffer'); | | 619 | + throw new TypeError('"val" argument must be string, number, Buffer ' + | | 620 | + 'or Uint8Array'); | 614 | 621 | } | 615 | 622 |
| 616 | 623 |
| @@ -1037,8 +1044,8 @@ Buffer.prototype.readDoubleBE = function readDoubleBE(offset, noAssert) { | 1037 | 1044 |
| 1038 | 1045 |
| 1039 | 1046 | function checkInt(buffer, value, offset, ext, max, min) { | 1040 | | - if (!(buffer instanceof Buffer)) | 1041 | | - throw new TypeError('"buffer" argument must be a Buffer instance'); | | 1047 | + if (!isUint8Array(buffer)) | | 1048 | + throw new TypeError('"buffer" argument must be a Buffer or Uint8Array'); | 1042 | 1049 | if (value > max || value < min) | 1043 | 1050 | throw new TypeError('"value" argument is out of bounds'); | 1044 | 1051 | if (offset + ext > buffer.length) | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/buffer.js +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -8,18 +8,19 @@ const normalizeEncoding = require('internal/util').normalizeEncoding; | 8 | 8 | const Buffer = require('buffer').Buffer; | 9 | 9 |
| 10 | 10 | const icu = process.binding('icu'); | | 11 | +const { isUint8Array } = process.binding('util'); | 11 | 12 |
| 12 | 13 | // Transcodes the Buffer from one encoding to another, returning a new | 13 | 14 | // Buffer instance. | 14 | 15 | exports.transcode = function transcode(source, fromEncoding, toEncoding) { | 15 | | - if (!Buffer.isBuffer(source)) | 16 | | - throw new TypeError('"source" argument must be a Buffer'); | | 16 | + if (!isUint8Array(source)) | | 17 | + throw new TypeError('"source" argument must be a Buffer or Uint8Array'); | 17 | 18 | if (source.length === 0) return Buffer.alloc(0); | 18 | 19 |
| 19 | 20 | fromEncoding = normalizeEncoding(fromEncoding) || fromEncoding; | 20 | 21 | toEncoding = normalizeEncoding(toEncoding) || toEncoding; | 21 | 22 | const result = icu.transcode(source, fromEncoding, toEncoding); | 22 | | - if (Buffer.isBuffer(result)) | | 23 | + if (typeof result !== 'number') | 23 | 24 | return result; | 24 | 25 |
| 25 | 26 | const code = icu.icuErrName(result); | |
Collapse file Copy file name to clipboardExpand all lines: src/node_buffer.cc +9-9Lines changed: 9 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -519,23 +519,24 @@ void Base64Slice(const FunctionCallbackInfo<Value>& args) { | 519 | 519 | } | 520 | 520 |
| 521 | 521 |
| 522 | | -// bytesCopied = buffer.copy(target[, targetStart][, sourceStart][, sourceEnd]); | | 522 | +// bytesCopied = copy(buffer, target[, targetStart][, sourceStart][, sourceEnd]) | 523 | 523 | void Copy(const FunctionCallbackInfo<Value> &args) { | 524 | 524 | Environment* env = Environment::GetCurrent(args); | 525 | 525 |
| 526 | | - THROW_AND_RETURN_UNLESS_BUFFER(env, args.This()); | 527 | 526 | THROW_AND_RETURN_UNLESS_BUFFER(env, args[0]); | 528 | | - Local<Object> target_obj = args[0].As<Object>(); | 529 | | - SPREAD_BUFFER_ARG(args.This(), ts_obj); | | 527 | + THROW_AND_RETURN_UNLESS_BUFFER(env, args[1]); | | 528 | + Local<Object> buffer_obj = args[0].As<Object>(); | | 529 | + Local<Object> target_obj = args[1].As<Object>(); | | 530 | + SPREAD_BUFFER_ARG(buffer_obj, ts_obj); | 530 | 531 | SPREAD_BUFFER_ARG(target_obj, target); | 531 | 532 |
| 532 | 533 | size_t target_start; | 533 | 534 | size_t source_start; | 534 | 535 | size_t source_end; | 535 | 536 |
| 536 | | - THROW_AND_RETURN_IF_OOB(ParseArrayIndex(args[1], 0, &target_start)); | 537 | | - THROW_AND_RETURN_IF_OOB(ParseArrayIndex(args[2], 0, &source_start)); | 538 | | - THROW_AND_RETURN_IF_OOB(ParseArrayIndex(args[3], ts_obj_length, &source_end)); | | 537 | + THROW_AND_RETURN_IF_OOB(ParseArrayIndex(args[2], 0, &target_start)); | | 538 | + THROW_AND_RETURN_IF_OOB(ParseArrayIndex(args[3], 0, &source_start)); | | 539 | + THROW_AND_RETURN_IF_OOB(ParseArrayIndex(args[4], ts_obj_length, &source_end)); | 539 | 540 |
| 540 | 541 | // Copy 0 bytes; we're done | 541 | 542 | if (target_start >= target_length || source_start >= source_end) | @@ -1203,8 +1204,6 @@ void SetupBufferJS(const FunctionCallbackInfo<Value>& args) { | 1203 | 1204 | env->SetMethod(proto, "ucs2Write", Ucs2Write); | 1204 | 1205 | env->SetMethod(proto, "utf8Write", Utf8Write); | 1205 | 1206 |
| 1206 | | - env->SetMethod(proto, "copy", Copy); | 1207 | | - | 1208 | 1207 | if (auto zero_fill_field = env->isolate_data()->zero_fill_field()) { | 1209 | 1208 | CHECK(args[1]->IsObject()); | 1210 | 1209 | auto binding_object = args[1].As<Object>(); | @@ -1227,6 +1226,7 @@ void Initialize(Local<Object> target, | 1227 | 1226 | env->SetMethod(target, "createFromString", CreateFromString); | 1228 | 1227 |
| 1229 | 1228 | env->SetMethod(target, "byteLengthUtf8", ByteLengthUtf8); | | 1229 | + env->SetMethod(target, "copy", Copy); | 1230 | 1230 | env->SetMethod(target, "compare", Compare); | 1231 | 1231 | env->SetMethod(target, "compareOffset", CompareOffset); | 1232 | 1232 | env->SetMethod(target, "fill", Fill); | |
Collapse file Copy file name to clipboardExpand all lines: src/node_util.cc +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,7 +29,8 @@ using v8::Value; | 29 | 29 | V(isSet, IsSet) \ | 30 | 30 | V(isSetIterator, IsSetIterator) \ | 31 | 31 | V(isSharedArrayBuffer, IsSharedArrayBuffer) \ | 32 | | - V(isTypedArray, IsTypedArray) | | 32 | + V(isTypedArray, IsTypedArray) \ | | 33 | + V(isUint8Array, IsUint8Array) | 33 | 34 |
| 34 | 35 |
| 35 | 36 | #define V(_, ucname) \ | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-buffer-compare.js +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,10 +6,12 @@ const assert = require('assert'); | 6 | 6 | const b = Buffer.alloc(1, 'a'); | 7 | 7 | const c = Buffer.alloc(1, 'c'); | 8 | 8 | const d = Buffer.alloc(2, 'aa'); | | 9 | +const e = new Uint8Array([ 0x61, 0x61 ]); // ASCII 'aa', same as d | 9 | 10 |
| 10 | 11 | assert.strictEqual(b.compare(c), -1); | 11 | 12 | assert.strictEqual(c.compare(d), 1); | 12 | 13 | assert.strictEqual(d.compare(b), 1); | | 14 | +assert.strictEqual(d.compare(e), 0); | 13 | 15 | assert.strictEqual(b.compare(d), -1); | 14 | 16 | assert.strictEqual(b.compare(b), 0); | 15 | 17 |
| @@ -18,6 +20,9 @@ assert.strictEqual(Buffer.compare(c, d), 1); | 18 | 20 | assert.strictEqual(Buffer.compare(d, b), 1); | 19 | 21 | assert.strictEqual(Buffer.compare(b, d), -1); | 20 | 22 | assert.strictEqual(Buffer.compare(c, c), 0); | | 23 | +assert.strictEqual(Buffer.compare(e, e), 0); | | 24 | +assert.strictEqual(Buffer.compare(d, e), 0); | | 25 | +assert.strictEqual(Buffer.compare(d, b), 1); | 21 | 26 |
| 22 | 27 | assert.strictEqual(Buffer.compare(Buffer.alloc(0), Buffer.alloc(0)), 0); | 23 | 28 | assert.strictEqual(Buffer.compare(Buffer.alloc(0), Buffer.alloc(1)), -1); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-buffer-concat.js +6-1Lines changed: 6 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -35,7 +35,8 @@ function assertWrongList(value) { | 35 | 35 | Buffer.concat(value); | 36 | 36 | }, function(err) { | 37 | 37 | return err instanceof TypeError && | 38 | | - err.message === '"list" argument must be an Array of Buffers'; | | 38 | + err.message === '"list" argument must be an Array of Buffer ' + | | 39 | + 'or Uint8Array instances'; | 39 | 40 | }); | 40 | 41 | } | 41 | 42 |
| @@ -60,3 +61,7 @@ assert.deepStrictEqual(Buffer.concat([empty], 4096), Buffer.alloc(4096)); | 60 | 61 | assert.deepStrictEqual( | 61 | 62 | Buffer.concat([random10], 40), | 62 | 63 | Buffer.concat([random10, Buffer.alloc(30)])); | | 64 | + | | 65 | +assert.deepStrictEqual(Buffer.concat([new Uint8Array([0x41, 0x42]), | | 66 | + new Uint8Array([0x43, 0x44])]), | | 67 | + Buffer.from('ABCD')); |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-buffer-equals.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,5 +12,6 @@ assert.ok(b.equals(c)); | 12 | 12 | assert.ok(!c.equals(d)); | 13 | 13 | assert.ok(!d.equals(e)); | 14 | 14 | assert.ok(d.equals(d)); | | 15 | +assert.ok(d.equals(new Uint8Array([0x61, 0x62, 0x63, 0x64, 0x65]))); | 15 | 16 |
| 16 | 17 | assert.throws(() => Buffer.alloc(1).equals('abc')); |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-buffer-indexof.js +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -524,3 +524,11 @@ assert.equal(0, reallyLong.lastIndexOf(pattern)); | 524 | 524 | assert.strictEqual(buf.indexOf(0xff), -1); | 525 | 525 | assert.strictEqual(buf.indexOf(0xffff), -1); | 526 | 526 | } | | 527 | + | | 528 | +// Test that Uint8Array arguments are okay. | | 529 | +{ | | 530 | + const needle = new Uint8Array([ 0x66, 0x6f, 0x6f ]); | | 531 | + const haystack = Buffer.from('a foo b foo'); | | 532 | + assert.strictEqual(haystack.indexOf(needle), 2); | | 533 | + assert.strictEqual(haystack.lastIndexOf(needle), haystack.length - 3); | | 534 | +} |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-icu-transcode.js +9-1Lines changed: 9 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -40,7 +40,7 @@ for (const test in tests) { | 40 | 40 |
| 41 | 41 | assert.throws( | 42 | 42 | () => buffer.transcode(null, 'utf8', 'ascii'), | 43 | | - /^TypeError: "source" argument must be a Buffer$/ | | 43 | + /^TypeError: "source" argument must be a Buffer or Uint8Array$/ | 44 | 44 | ); | 45 | 45 |
| 46 | 46 | assert.throws( | @@ -62,3 +62,11 @@ assert.deepStrictEqual( | 62 | 62 | assert.deepStrictEqual( | 63 | 63 | buffer.transcode(Buffer.from('hä', 'latin1'), 'latin1', 'utf16le'), | 64 | 64 | Buffer.from('hä', 'utf16le')); | | 65 | + | | 66 | +// Test that Uint8Array arguments are okay. | | 67 | +{ | | 68 | + const uint8array = new Uint8Array([...Buffer.from('hä', 'latin1')]); | | 69 | + assert.deepStrictEqual( | | 70 | + buffer.transcode(uint8array, 'latin1', 'utf16le'), | | 71 | + Buffer.from('hä', 'utf16le')); | | 72 | +} |
|