| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent ea46879 commit 92c517c
8 files changed
| 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`. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -73,6 +73,17 @@ const { | |||
| 73 | 73 | kStringMaxLength, | |
| 74 | 74 | atob: _atob, | |
| 75 | 75 | btoa: _btoa, | |
| 76 | + asciiSlice, | ||
| 77 | + base64Slice, | ||
| 78 | + base64urlSlice, | ||
| 79 | + latin1Slice, | ||
| 80 | + hexSlice, | ||
| 81 | + ucs2Slice, | ||
| 82 | + utf8Slice, | ||
| 83 | + base64Write, | ||
| 84 | + base64urlWrite, | ||
| 85 | + hexWrite, | ||
| 86 | + ucs2Write, | ||
| 76 | 87 | } = internalBinding('buffer'); | |
| 77 | 88 | const { | |
| 78 | 89 | constants: { | |
@@ -129,6 +140,9 @@ const { | |||
| 129 | 140 | markAsUntransferable, | |
| 130 | 141 | addBufferPrototypeMethods, | |
| 131 | 142 | createUnsafeBuffer, | |
| 143 | + asciiWrite, | ||
| 144 | + latin1Write, | ||
| 145 | + utf8Write, | ||
| 132 | 146 | } = require('internal/buffer'); | |
| 133 | 147 | ||
| 134 | 148 | FastBuffer.prototype.constructor = Buffer; | |
@@ -646,44 +660,44 @@ const encodingOps = { | |||
| 646 | 660 | encoding: 'utf8', | |
| 647 | 661 | encodingVal: encodingsMap.utf8, | |
| 648 | 662 | byteLength: byteLengthUtf8, | |
| 649 | - write: (buf, string, offset, len) => buf.utf8Write(string, offset, len), | ||
| 650 | - slice: (buf, start, end) => buf.utf8Slice(start, end), | ||
| 663 | + write: utf8Write, | ||
| 664 | + slice: utf8Slice, | ||
| 651 | 665 | indexOf: (buf, val, byteOffset, dir) => | |
| 652 | 666 | indexOfString(buf, val, byteOffset, encodingsMap.utf8, dir), | |
| 653 | 667 | }, | |
| 654 | 668 | ucs2: { | |
| 655 | 669 | encoding: 'ucs2', | |
| 656 | 670 | encodingVal: encodingsMap.utf16le, | |
| 657 | 671 | byteLength: (string) => string.length * 2, | |
| 658 | - write: (buf, string, offset, len) => buf.ucs2Write(string, offset, len), | ||
| 659 | - slice: (buf, start, end) => buf.ucs2Slice(start, end), | ||
| 672 | + write: ucs2Write, | ||
| 673 | + slice: ucs2Slice, | ||
| 660 | 674 | indexOf: (buf, val, byteOffset, dir) => | |
| 661 | 675 | indexOfString(buf, val, byteOffset, encodingsMap.utf16le, dir), | |
| 662 | 676 | }, | |
| 663 | 677 | utf16le: { | |
| 664 | 678 | encoding: 'utf16le', | |
| 665 | 679 | encodingVal: encodingsMap.utf16le, | |
| 666 | 680 | byteLength: (string) => string.length * 2, | |
| 667 | - write: (buf, string, offset, len) => buf.ucs2Write(string, offset, len), | ||
| 668 | - slice: (buf, start, end) => buf.ucs2Slice(start, end), | ||
| 681 | + write: ucs2Write, | ||
| 682 | + slice: ucs2Slice, | ||
| 669 | 683 | indexOf: (buf, val, byteOffset, dir) => | |
| 670 | 684 | indexOfString(buf, val, byteOffset, encodingsMap.utf16le, dir), | |
| 671 | 685 | }, | |
| 672 | 686 | latin1: { | |
| 673 | 687 | encoding: 'latin1', | |
| 674 | 688 | encodingVal: encodingsMap.latin1, | |
| 675 | 689 | byteLength: (string) => string.length, | |
| 676 | - write: (buf, string, offset, len) => buf.latin1Write(string, offset, len), | ||
| 677 | - slice: (buf, start, end) => buf.latin1Slice(start, end), | ||
| 690 | + write: latin1Write, | ||
| 691 | + slice: latin1Slice, | ||
| 678 | 692 | indexOf: (buf, val, byteOffset, dir) => | |
| 679 | 693 | indexOfString(buf, val, byteOffset, encodingsMap.latin1, dir), | |
| 680 | 694 | }, | |
| 681 | 695 | ascii: { | |
| 682 | 696 | encoding: 'ascii', | |
| 683 | 697 | encodingVal: encodingsMap.ascii, | |
| 684 | 698 | byteLength: (string) => string.length, | |
| 685 | - write: (buf, string, offset, len) => buf.asciiWrite(string, offset, len), | ||
| 686 | - slice: (buf, start, end) => buf.asciiSlice(start, end), | ||
| 699 | + write: asciiWrite, | ||
| 700 | + slice: asciiSlice, | ||
| 687 | 701 | indexOf: (buf, val, byteOffset, dir) => | |
| 688 | 702 | indexOfBuffer(buf, | |
| 689 | 703 | fromStringFast(val, encodingOps.ascii), | |
@@ -695,8 +709,8 @@ const encodingOps = { | |||
| 695 | 709 | encoding: 'base64', | |
| 696 | 710 | encodingVal: encodingsMap.base64, | |
| 697 | 711 | byteLength: (string) => base64ByteLength(string, string.length), | |
| 698 | - write: (buf, string, offset, len) => buf.base64Write(string, offset, len), | ||
| 699 | - slice: (buf, start, end) => buf.base64Slice(start, end), | ||
| 712 | + write: base64Write, | ||
| 713 | + slice: base64Slice, | ||
| 700 | 714 | indexOf: (buf, val, byteOffset, dir) => | |
| 701 | 715 | indexOfBuffer(buf, | |
| 702 | 716 | fromStringFast(val, encodingOps.base64), | |
@@ -708,9 +722,8 @@ const encodingOps = { | |||
| 708 | 722 | encoding: 'base64url', | |
| 709 | 723 | encodingVal: encodingsMap.base64url, | |
| 710 | 724 | byteLength: (string) => base64ByteLength(string, string.length), | |
| 711 | - write: (buf, string, offset, len) => | ||
| 712 | - buf.base64urlWrite(string, offset, len), | ||
| 713 | - slice: (buf, start, end) => buf.base64urlSlice(start, end), | ||
| 725 | + write: base64urlWrite, | ||
| 726 | + slice: base64urlSlice, | ||
| 714 | 727 | indexOf: (buf, val, byteOffset, dir) => | |
| 715 | 728 | indexOfBuffer(buf, | |
| 716 | 729 | fromStringFast(val, encodingOps.base64url), | |
@@ -722,8 +735,8 @@ const encodingOps = { | |||
| 722 | 735 | encoding: 'hex', | |
| 723 | 736 | encodingVal: encodingsMap.hex, | |
| 724 | 737 | byteLength: (string) => string.length >>> 1, | |
| 725 | - write: (buf, string, offset, len) => buf.hexWrite(string, offset, len), | ||
| 726 | - slice: (buf, start, end) => buf.hexSlice(start, end), | ||
| 738 | + write: hexWrite, | ||
| 739 | + slice: hexSlice, | ||
| 727 | 740 | indexOf: (buf, val, byteOffset, dir) => | |
| 728 | 741 | indexOfBuffer(buf, | |
| 729 | 742 | fromStringFast(val, encodingOps.hex), | |
@@ -848,7 +861,7 @@ Buffer.prototype.copy = | |||
| 848 | 861 | // to their upper/lower bounds if the value passed is out of range. | |
| 849 | 862 | Buffer.prototype.toString = function toString(encoding, start, end) { | |
| 850 | 863 | if (arguments.length === 0) { | |
| 851 | - return this.utf8Slice(0, this.length); | ||
| 864 | + return utf8Slice(this, 0, this.length); | ||
| 852 | 865 | } | |
| 853 | 866 | ||
| 854 | 867 | const len = this.length; | |
@@ -869,7 +882,7 @@ Buffer.prototype.toString = function toString(encoding, start, end) { | |||
| 869 | 882 | return ''; | |
| 870 | 883 | ||
| 871 | 884 | if (encoding === undefined) | |
| 872 | - return this.utf8Slice(start, end); | ||
| 885 | + return utf8Slice(this, start, end); | ||
| 873 | 886 | ||
| 874 | 887 | const ops = getEncodingOps(encoding); | |
| 875 | 888 | if (ops === undefined) | |
@@ -900,7 +913,7 @@ Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) { | |||
| 900 | 913 | const actualMax = MathMin(max, this.length); | |
| 901 | 914 | const remaining = this.length - max; | |
| 902 | 915 | let str = StringPrototypeTrim(RegExpPrototypeSymbolReplace( | |
| 903 | - /(.{2})/g, this.hexSlice(0, actualMax), '$1 ')); | ||
| 916 | + /(.{2})/g, hexSlice(this, 0, actualMax), '$1 ')); | ||
| 904 | 917 | if (remaining > 0) | |
| 905 | 918 | str += ` ... ${remaining} more byte${remaining > 1 ? 's' : ''}`; | |
| 906 | 919 | // Inspect special properties as well, if possible. | |
@@ -1039,7 +1052,7 @@ Buffer.prototype.lastIndexOf = function lastIndexOf(val, byteOffset, encoding) { | |||
| 1039 | 1052 | }; | |
| 1040 | 1053 | ||
| 1041 | 1054 | Buffer.prototype.includes = function includes(val, byteOffset, encoding) { | |
| 1042 | - return this.indexOf(val, byteOffset, encoding) !== -1; | ||
| 1055 | + return bidirectionalIndexOf(this, val, byteOffset, encoding, true) !== -1; | ||
| 1043 | 1056 | }; | |
| 1044 | 1057 | ||
| 1045 | 1058 | // Usage: | |
@@ -1124,7 +1137,7 @@ function _fill(buf, value, offset, end, encoding) { | |||
| 1124 | 1137 | Buffer.prototype.write = function write(string, offset, length, encoding) { | |
| 1125 | 1138 | // Buffer#write(string); | |
| 1126 | 1139 | if (offset === undefined) { | |
| 1127 | - return this.utf8Write(string, 0, this.length); | ||
| 1140 | + return utf8Write(this, string, 0, this.length); | ||
| 1128 | 1141 | } | |
| 1129 | 1142 | // Buffer#write(string, encoding) | |
| 1130 | 1143 | if (length === undefined && typeof offset === 'string') { | |
@@ -1151,9 +1164,9 @@ Buffer.prototype.write = function write(string, offset, length, encoding) { | |||
| 1151 | 1164 | } | |
| 1152 | 1165 | ||
| 1153 | 1166 | if (!encoding || encoding === 'utf8') | |
| 1154 | - return this.utf8Write(string, offset, length); | ||
| 1167 | + return utf8Write(this, string, offset, length); | ||
| 1155 | 1168 | if (encoding === 'ascii') | |
| 1156 | - return this.asciiWrite(string, offset, length); | ||
| 1169 | + return asciiWrite(this, string, offset, length); | ||
| 1157 | 1170 | ||
| 1158 | 1171 | const ops = getEncodingOps(encoding); | |
| 1159 | 1172 | if (ops === undefined) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments