summaryrefslogtreecommitdiff
path: root/ext/node/polyfills/internal/buffer.mjs
diff options
context:
space:
mode:
authorAapo Alasuutari <aapo.alasuutari@gmail.com>2023-10-08 11:09:50 +0900
committerGitHub <noreply@github.com>2023-10-08 11:09:50 +0900
commiteffb5e1ce417e9b7f22adcdc598e9a5ec593f4a2 (patch)
tree620544d0d4051ad16d78c5df3fbed0441b800ea0 /ext/node/polyfills/internal/buffer.mjs
parentedeccef4990598620bf5595df6fc8c1b1b1a7f73 (diff)
fix(node/buffer): utf8ToBytes should return a Uint8Array (#20769)
Diffstat (limited to 'ext/node/polyfills/internal/buffer.mjs')
-rw-r--r--ext/node/polyfills/internal/buffer.mjs95
1 files changed, 14 insertions, 81 deletions
diff --git a/ext/node/polyfills/internal/buffer.mjs b/ext/node/polyfills/internal/buffer.mjs
index 6f3121233..cd69a199f 100644
--- a/ext/node/polyfills/internal/buffer.mjs
+++ b/ext/node/polyfills/internal/buffer.mjs
@@ -744,12 +744,12 @@ Buffer.prototype.utf8Slice = function utf8Slice(string, offset, length) {
};
Buffer.prototype.utf8Write = function utf8Write(string, offset, length) {
- return blitBuffer(
- utf8ToBytes(string, this.length - offset),
- this,
- offset,
- length,
- );
+ offset = offset || 0;
+ const maxLength = Math.min(length || Infinity, this.length - offset);
+ const buf = offset || maxLength < this.length
+ ? this.subarray(offset, maxLength + offset)
+ : this;
+ return utf8Encoder.encodeInto(string, buf).written;
};
Buffer.prototype.write = function write(string, offset, length, encoding) {
@@ -1687,80 +1687,13 @@ function checkIntBI(value, min, max, buf, offset, byteLength2) {
checkBounds(buf, offset, byteLength2);
}
-function utf8ToBytes(string, units) {
- units = units || Infinity;
- let codePoint;
- const length = string.length;
- let leadSurrogate = null;
- const bytes = [];
- for (let i = 0; i < length; ++i) {
- codePoint = string.charCodeAt(i);
- if (codePoint > 55295 && codePoint < 57344) {
- if (!leadSurrogate) {
- if (codePoint > 56319) {
- if ((units -= 3) > -1) {
- bytes.push(239, 191, 189);
- }
- continue;
- } else if (i + 1 === length) {
- if ((units -= 3) > -1) {
- bytes.push(239, 191, 189);
- }
- continue;
- }
- leadSurrogate = codePoint;
- continue;
- }
- if (codePoint < 56320) {
- if ((units -= 3) > -1) {
- bytes.push(239, 191, 189);
- }
- leadSurrogate = codePoint;
- continue;
- }
- codePoint = (leadSurrogate - 55296 << 10 | codePoint - 56320) + 65536;
- } else if (leadSurrogate) {
- if ((units -= 3) > -1) {
- bytes.push(239, 191, 189);
- }
- }
- leadSurrogate = null;
- if (codePoint < 128) {
- if ((units -= 1) < 0) {
- break;
- }
- bytes.push(codePoint);
- } else if (codePoint < 2048) {
- if ((units -= 2) < 0) {
- break;
- }
- bytes.push(codePoint >> 6 | 192, codePoint & 63 | 128);
- } else if (codePoint < 65536) {
- if ((units -= 3) < 0) {
- break;
- }
- bytes.push(
- codePoint >> 12 | 224,
- codePoint >> 6 & 63 | 128,
- codePoint & 63 | 128,
- );
- } else if (codePoint < 1114112) {
- if ((units -= 4) < 0) {
- break;
- }
- bytes.push(
- codePoint >> 18 | 240,
- codePoint >> 12 & 63 | 128,
- codePoint >> 6 & 63 | 128,
- codePoint & 63 | 128,
- );
- } else {
- throw new Error("Invalid code point");
- }
- }
- return bytes;
-}
-
+/**
+ * @param {Uint8Array} src Source buffer to read from
+ * @param {Buffer} dst Destination buffer to write to
+ * @param {number} [offset] Byte offset to write at in the destination buffer
+ * @param {number} [byteLength] Optional number of bytes to, at most, write into destination buffer.
+ * @returns {number} Number of bytes written to destination buffer
+ */
function blitBuffer(src, dst, offset, byteLength = Infinity) {
const srcLength = src.length;
// Establish the number of bytes to be written
@@ -1771,7 +1704,7 @@ function blitBuffer(src, dst, offset, byteLength = Infinity) {
// The length of the source sets an upper bound being the source of data.
srcLength,
// The length of the destination minus any offset into it sets an upper bound.
- dst.length - offset,
+ dst.length - (offset || 0),
);
if (bytesToWrite < srcLength) {
// Resize the source buffer to the number of bytes we're about to write.