GeezerVison is showing github.com — buffer: make methods work on Uint8Array instances · nodejs/node@e9a3426
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 +41Lines changed: 41 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -415,6 +415,21 @@ function: | 415 | 415 | * [`Buffer.from(arrayBuffer[, byteOffset[, length]])`][`Buffer.from(arrayBuf)`] | 416 | 416 | * [`Buffer.from(string[, encoding])`][`Buffer.from(string)`] | 417 | 417 |
| | 418 | +### Buffer methods are callable with `Uint8Array` instances | | 419 | + | | 420 | +All methods on the Buffer prototype are callable with a `Uint8Array` instance. | | 421 | + | | 422 | +```js | | 423 | +const { toString, write } = Buffer.prototype; | | 424 | + | | 425 | +const uint8array = new Uint8Array(5); | | 426 | + | | 427 | +write.call(uint8array, 'hello', 0, 5, 'utf8'); // 5 | | 428 | +// <Uint8Array 68 65 6c 6c 6f> | | 429 | + | | 430 | +toString.call(uint8array, 'utf8'); // 'hello' | | 431 | +``` | | 432 | + | 418 | 433 | ## Buffers and iteration | 419 | 434 |
| 420 | 435 | `Buffer` instances can be iterated over using `for..of` syntax: | @@ -2058,6 +2073,10 @@ console.log(buf.fill('zz', 'hex')); | 2058 | 2073 |
| 2059 | 2074 | <!-- YAML | 2060 | 2075 | added: v5.3.0 | | 2076 | +changes: | | 2077 | + - version: REPLACEME | | 2078 | + pr-url: https://github.com/nodejs/node/pull/56578 | | 2079 | + description: supports Uint8Array as `this` value. | 2061 | 2080 | --> | 2062 | 2081 |
| 2063 | 2082 | * `value` {string|Buffer|Uint8Array|integer} What to search for. | @@ -2945,10 +2964,14 @@ console.log(buf.readInt32LE(1)); | 2945 | 2964 | <!-- YAML | 2946 | 2965 | added: v0.11.15 | 2947 | 2966 | changes: | | 2967 | + - version: REPLACEME | | 2968 | + pr-url: https://github.com/nodejs/node/pull/56578 | | 2969 | + description: supports Uint8Array as `this` value. | 2948 | 2970 | - version: v10.0.0 | 2949 | 2971 | pr-url: https://github.com/nodejs/node/pull/18395 | 2950 | 2972 | description: Removed `noAssert` and no implicit coercion of the offset | 2951 | 2973 | and `byteLength` to `uint32` anymore. | | 2974 | +
| 2952 | 2975 | --> | 2953 | 2976 |
| 2954 | 2977 | * `offset` {integer} Number of bytes to skip before starting to read. Must | @@ -2992,10 +3015,14 @@ console.log(buf.readIntBE(1, 0).toString(16)); | 2992 | 3015 | <!-- YAML | 2993 | 3016 | added: v0.11.15 | 2994 | 3017 | changes: | | 3018 | + - version: REPLACEME | | 3019 | + pr-url: https://github.com/nodejs/node/pull/56578 | | 3020 | + description: supports Uint8Array as `this` value. | 2995 | 3021 | - version: v10.0.0 | 2996 | 3022 | pr-url: https://github.com/nodejs/node/pull/18395 | 2997 | 3023 | description: Removed `noAssert` and no implicit coercion of the offset | 2998 | 3024 | and `byteLength` to `uint32` anymore. | | 3025 | +
| 2999 | 3026 | --> | 3000 | 3027 |
| 3001 | 3028 | * `offset` {integer} Number of bytes to skip before starting to read. Must | @@ -3269,6 +3296,9 @@ console.log(buf.readUInt32LE(1).toString(16)); | 3269 | 3296 | <!-- YAML | 3270 | 3297 | added: v0.11.15 | 3271 | 3298 | changes: | | 3299 | + - version: REPLACEME | | 3300 | + pr-url: https://github.com/nodejs/node/pull/56578 | | 3301 | + description: supports Uint8Array as `this` value. | 3272 | 3302 | - version: | 3273 | 3303 | - v14.9.0 | 3274 | 3304 | - v12.19.0 | @@ -3319,6 +3349,9 @@ console.log(buf.readUIntBE(1, 6).toString(16)); | 3319 | 3349 | <!-- YAML | 3320 | 3350 | added: v0.11.15 | 3321 | 3351 | changes: | | 3352 | + - version: REPLACEME | | 3353 | + pr-url: https://github.com/nodejs/node/pull/56578 | | 3354 | + description: supports Uint8Array as `this` value. | 3322 | 3355 | - version: | 3323 | 3356 | - v14.9.0 | 3324 | 3357 | - v12.19.0 | @@ -3771,6 +3804,10 @@ console.log(copy); | 3771 | 3804 |
| 3772 | 3805 | <!-- YAML | 3773 | 3806 | added: v0.1.90 | | 3807 | +changes: | | 3808 | + - version: REPLACEME | | 3809 | + pr-url: https://github.com/nodejs/node/pull/56578 | | 3810 | + description: supports Uint8Array as `this` value. | 3774 | 3811 | --> | 3775 | 3812 |
| 3776 | 3813 | * `encoding` {string} The character encoding to use. **Default:** `'utf8'`. | @@ -3909,6 +3946,10 @@ for (const value of buf) { | 3909 | 3946 |
| 3910 | 3947 | <!-- YAML | 3911 | 3948 | added: v0.1.90 | | 3949 | +changes: | | 3950 | + - version: REPLACEME | | 3951 | + pr-url: https://github.com/nodejs/node/pull/56578 | | 3952 | + description: supports Uint8Array as `this` value. | 3912 | 3953 | --> | 3913 | 3954 |
| 3914 | 3955 | * `string` {string} String to write to `buf`. | |
Collapse file Copy file name to clipboardExpand all lines: lib/buffer.js +37-24Lines changed: 37 additions & 24 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -72,6 +72,17 @@ const { | 72 | 72 | kStringMaxLength, | 73 | 73 | atob: _atob, | 74 | 74 | btoa: _btoa, | | 75 | + asciiSlice, | | 76 | + base64Slice, | | 77 | + base64urlSlice, | | 78 | + latin1Slice, | | 79 | + hexSlice, | | 80 | + ucs2Slice, | | 81 | + utf8Slice, | | 82 | + base64Write, | | 83 | + base64urlWrite, | | 84 | + hexWrite, | | 85 | + ucs2Write, | 75 | 86 | } = internalBinding('buffer'); | 76 | 87 | const { | 77 | 88 | constants: { | @@ -127,6 +138,9 @@ const { | 127 | 138 | markAsUntransferable, | 128 | 139 | addBufferPrototypeMethods, | 129 | 140 | createUnsafeBuffer, | | 141 | + asciiWrite, | | 142 | + latin1Write, | | 143 | + utf8Write, | 130 | 144 | } = require('internal/buffer'); | 131 | 145 |
| 132 | 146 | FastBuffer.prototype.constructor = Buffer; | @@ -634,44 +648,44 @@ const encodingOps = { | 634 | 648 | encoding: 'utf8', | 635 | 649 | encodingVal: encodingsMap.utf8, | 636 | 650 | byteLength: byteLengthUtf8, | 637 | | - write: (buf, string, offset, len) => buf.utf8Write(string, offset, len), | 638 | | - slice: (buf, start, end) => buf.utf8Slice(start, end), | | 651 | + write: utf8Write, | | 652 | + slice: utf8Slice, | 639 | 653 | indexOf: (buf, val, byteOffset, dir) => | 640 | 654 | indexOfString(buf, val, byteOffset, encodingsMap.utf8, dir), | 641 | 655 | }, | 642 | 656 | ucs2: { | 643 | 657 | encoding: 'ucs2', | 644 | 658 | encodingVal: encodingsMap.utf16le, | 645 | 659 | byteLength: (string) => string.length * 2, | 646 | | - write: (buf, string, offset, len) => buf.ucs2Write(string, offset, len), | 647 | | - slice: (buf, start, end) => buf.ucs2Slice(start, end), | | 660 | + write: ucs2Write, | | 661 | + slice: ucs2Slice, | 648 | 662 | indexOf: (buf, val, byteOffset, dir) => | 649 | 663 | indexOfString(buf, val, byteOffset, encodingsMap.utf16le, dir), | 650 | 664 | }, | 651 | 665 | utf16le: { | 652 | 666 | encoding: 'utf16le', | 653 | 667 | encodingVal: encodingsMap.utf16le, | 654 | 668 | byteLength: (string) => string.length * 2, | 655 | | - write: (buf, string, offset, len) => buf.ucs2Write(string, offset, len), | 656 | | - slice: (buf, start, end) => buf.ucs2Slice(start, end), | | 669 | + write: ucs2Write, | | 670 | + slice: ucs2Slice, | 657 | 671 | indexOf: (buf, val, byteOffset, dir) => | 658 | 672 | indexOfString(buf, val, byteOffset, encodingsMap.utf16le, dir), | 659 | 673 | }, | 660 | 674 | latin1: { | 661 | 675 | encoding: 'latin1', | 662 | 676 | encodingVal: encodingsMap.latin1, | 663 | 677 | byteLength: (string) => string.length, | 664 | | - write: (buf, string, offset, len) => buf.latin1Write(string, offset, len), | 665 | | - slice: (buf, start, end) => buf.latin1Slice(start, end), | | 678 | + write: latin1Write, | | 679 | + slice: latin1Slice, | 666 | 680 | indexOf: (buf, val, byteOffset, dir) => | 667 | 681 | indexOfString(buf, val, byteOffset, encodingsMap.latin1, dir), | 668 | 682 | }, | 669 | 683 | ascii: { | 670 | 684 | encoding: 'ascii', | 671 | 685 | encodingVal: encodingsMap.ascii, | 672 | 686 | byteLength: (string) => string.length, | 673 | | - write: (buf, string, offset, len) => buf.asciiWrite(string, offset, len), | 674 | | - slice: (buf, start, end) => buf.asciiSlice(start, end), | | 687 | + write: asciiWrite, | | 688 | + slice: asciiSlice, | 675 | 689 | indexOf: (buf, val, byteOffset, dir) => | 676 | 690 | indexOfBuffer(buf, | 677 | 691 | fromStringFast(val, encodingOps.ascii), | @@ -683,8 +697,8 @@ const encodingOps = { | 683 | 697 | encoding: 'base64', | 684 | 698 | encodingVal: encodingsMap.base64, | 685 | 699 | byteLength: (string) => base64ByteLength(string, string.length), | 686 | | - write: (buf, string, offset, len) => buf.base64Write(string, offset, len), | 687 | | - slice: (buf, start, end) => buf.base64Slice(start, end), | | 700 | + write: base64Write, | | 701 | + slice: base64Slice, | 688 | 702 | indexOf: (buf, val, byteOffset, dir) => | 689 | 703 | indexOfBuffer(buf, | 690 | 704 | fromStringFast(val, encodingOps.base64), | @@ -696,9 +710,8 @@ const encodingOps = { | 696 | 710 | encoding: 'base64url', | 697 | 711 | encodingVal: encodingsMap.base64url, | 698 | 712 | byteLength: (string) => base64ByteLength(string, string.length), | 699 | | - write: (buf, string, offset, len) => | 700 | | - buf.base64urlWrite(string, offset, len), | 701 | | - slice: (buf, start, end) => buf.base64urlSlice(start, end), | | 713 | + write: base64urlWrite, | | 714 | + slice: base64urlSlice, | 702 | 715 | indexOf: (buf, val, byteOffset, dir) => | 703 | 716 | indexOfBuffer(buf, | 704 | 717 | fromStringFast(val, encodingOps.base64url), | @@ -710,8 +723,8 @@ const encodingOps = { | 710 | 723 | encoding: 'hex', | 711 | 724 | encodingVal: encodingsMap.hex, | 712 | 725 | byteLength: (string) => string.length >>> 1, | 713 | | - write: (buf, string, offset, len) => buf.hexWrite(string, offset, len), | 714 | | - slice: (buf, start, end) => buf.hexSlice(start, end), | | 726 | + write: hexWrite, | | 727 | + slice: hexSlice, | 715 | 728 | indexOf: (buf, val, byteOffset, dir) => | 716 | 729 | indexOfBuffer(buf, | 717 | 730 | fromStringFast(val, encodingOps.hex), | @@ -836,7 +849,7 @@ Buffer.prototype.copy = | 836 | 849 | // to their upper/lower bounds if the value passed is out of range. | 837 | 850 | Buffer.prototype.toString = function toString(encoding, start, end) { | 838 | 851 | if (arguments.length === 0) { | 839 | | - return this.utf8Slice(0, this.length); | | 852 | + return utf8Slice(this, 0, this.length); | 840 | 853 | } | 841 | 854 |
| 842 | 855 | const len = this.length; | @@ -857,7 +870,7 @@ Buffer.prototype.toString = function toString(encoding, start, end) { | 857 | 870 | return ''; | 858 | 871 |
| 859 | 872 | if (encoding === undefined) | 860 | | - return this.utf8Slice(start, end); | | 873 | + return utf8Slice(this, start, end); | 861 | 874 |
| 862 | 875 | const ops = getEncodingOps(encoding); | 863 | 876 | if (ops === undefined) | @@ -888,7 +901,7 @@ Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) { | 888 | 901 | const actualMax = MathMin(max, this.length); | 889 | 902 | const remaining = this.length - max; | 890 | 903 | let str = StringPrototypeTrim(RegExpPrototypeSymbolReplace( | 891 | | - /(.{2})/g, this.hexSlice(0, actualMax), '$1 ')); | | 904 | + /(.{2})/g, hexSlice(this, 0, actualMax), '$1 ')); | 892 | 905 | if (remaining > 0) | 893 | 906 | str += ` ... ${remaining} more byte${remaining > 1 ? 's' : ''}`; | 894 | 907 | // Inspect special properties as well, if possible. | @@ -1027,7 +1040,7 @@ Buffer.prototype.lastIndexOf = function lastIndexOf(val, byteOffset, encoding) { | 1027 | 1040 | }; | 1028 | 1041 |
| 1029 | 1042 | Buffer.prototype.includes = function includes(val, byteOffset, encoding) { | 1030 | | - return this.indexOf(val, byteOffset, encoding) !== -1; | | 1043 | + return bidirectionalIndexOf(this, val, byteOffset, encoding, true) !== -1; | 1031 | 1044 | }; | 1032 | 1045 |
| 1033 | 1046 | // Usage: | @@ -1112,7 +1125,7 @@ function _fill(buf, value, offset, end, encoding) { | 1112 | 1125 | Buffer.prototype.write = function write(string, offset, length, encoding) { | 1113 | 1126 | // Buffer#write(string); | 1114 | 1127 | if (offset === undefined) { | 1115 | | - return this.utf8Write(string, 0, this.length); | | 1128 | + return utf8Write(this, string, 0, this.length); | 1116 | 1129 | } | 1117 | 1130 | // Buffer#write(string, encoding) | 1118 | 1131 | if (length === undefined && typeof offset === 'string') { | @@ -1139,9 +1152,9 @@ Buffer.prototype.write = function write(string, offset, length, encoding) { | 1139 | 1152 | } | 1140 | 1153 |
| 1141 | 1154 | if (!encoding || encoding === 'utf8') | 1142 | | - return this.utf8Write(string, offset, length); | | 1155 | + return utf8Write(this, string, offset, length); | 1143 | 1156 | if (encoding === 'ascii') | 1144 | | - return this.asciiWrite(string, offset, length); | | 1157 | + return asciiWrite(this, string, offset, length); | 1145 | 1158 |
| 1146 | 1159 | const ops = getEncodingOps(encoding); | 1147 | 1160 | if (ops === undefined) | |
|