From 6bbb59fc34d3d1a8ec03f424bb8e0d03b43065f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Fri, 24 Oct 2025 23:26:21 +0200 Subject: [PATCH 1/2] buffer: inline copyImpl --- lib/buffer.js | 75 ++++++++++++++++++++++++--------------------------- 1 file changed, 35 insertions(+), 40 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index c9f45d333886d3..a59b0942d88b18 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -204,42 +204,6 @@ function toInteger(n, defaultVal) { return defaultVal; } -function copyImpl(source, target, targetStart, sourceStart, sourceEnd) { - if (!ArrayBufferIsView(source)) - throw new ERR_INVALID_ARG_TYPE('source', ['Buffer', 'Uint8Array'], source); - if (!ArrayBufferIsView(target)) - throw new ERR_INVALID_ARG_TYPE('target', ['Buffer', 'Uint8Array'], target); - - if (targetStart === undefined) { - targetStart = 0; - } else { - targetStart = NumberIsInteger(targetStart) ? targetStart : toInteger(targetStart, 0); - if (targetStart < 0) - throw new ERR_OUT_OF_RANGE('targetStart', '>= 0', targetStart); - } - - if (sourceStart === undefined) { - sourceStart = 0; - } else { - sourceStart = NumberIsInteger(sourceStart) ? sourceStart : toInteger(sourceStart, 0); - if (sourceStart < 0 || sourceStart > source.byteLength) - throw new ERR_OUT_OF_RANGE('sourceStart', `>= 0 && <= ${source.byteLength}`, sourceStart); - } - - if (sourceEnd === undefined) { - sourceEnd = source.byteLength; - } else { - sourceEnd = NumberIsInteger(sourceEnd) ? sourceEnd : toInteger(sourceEnd, 0); - if (sourceEnd < 0) - throw new ERR_OUT_OF_RANGE('sourceEnd', '>= 0', sourceEnd); - } - - if (targetStart >= target.byteLength || sourceStart >= sourceEnd) - return 0; - - return _copyActual(source, target, targetStart, sourceStart, sourceEnd); -} - function _copyActual(source, target, targetStart, sourceStart, sourceEnd) { if (sourceEnd - sourceStart > target.byteLength - targetStart) sourceEnd = sourceStart + target.byteLength - targetStart; @@ -821,10 +785,41 @@ ObjectDefineProperty(Buffer.prototype, 'offset', { }, }); -Buffer.prototype.copy = - function copy(target, targetStart, sourceStart, sourceEnd) { - return copyImpl(this, target, targetStart, sourceStart, sourceEnd); - }; +Buffer.prototype.copy = function copy(target, targetStart, sourceStart, sourceEnd) { + if (!ArrayBufferIsView(this)) + throw new ERR_INVALID_ARG_TYPE('source', ['Buffer', 'Uint8Array'], this); + if (!ArrayBufferIsView(target)) + throw new ERR_INVALID_ARG_TYPE('target', ['Buffer', 'Uint8Array'], target); + + if (targetStart === undefined) { + targetStart = 0; + } else { + targetStart = NumberIsInteger(targetStart) ? targetStart : toInteger(targetStart, 0); + if (targetStart < 0) + throw new ERR_OUT_OF_RANGE('targetStart', '>= 0', targetStart); + } + + if (sourceStart === undefined) { + sourceStart = 0; + } else { + sourceStart = NumberIsInteger(sourceStart) ? sourceStart : toInteger(sourceStart, 0); + if (sourceStart < 0 || sourceStart > this.byteLength) + throw new ERR_OUT_OF_RANGE('sourceStart', `>= 0 && <= ${this.byteLength}`, sourceStart); + } + + if (sourceEnd === undefined) { + sourceEnd = this.byteLength; + } else { + sourceEnd = NumberIsInteger(sourceEnd) ? sourceEnd : toInteger(sourceEnd, 0); + if (sourceEnd < 0) + throw new ERR_OUT_OF_RANGE('sourceEnd', '>= 0', sourceEnd); + } + + if (targetStart >= target.byteLength || sourceStart >= sourceEnd) + return 0; + + return _copyActual(this, target, targetStart, sourceStart, sourceEnd); +}; // No need to verify that "buf.length <= MAX_UINT32" since it's a read-only // property of a typed array. From 1f9abbe342fb312344867f88e0f6561c1c2a7f24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Sat, 25 Oct 2025 00:47:55 +0200 Subject: [PATCH 2/2] remove unnecessary check --- lib/buffer.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index a59b0942d88b18..4a26a5db9d83d0 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -786,8 +786,6 @@ ObjectDefineProperty(Buffer.prototype, 'offset', { }); Buffer.prototype.copy = function copy(target, targetStart, sourceStart, sourceEnd) { - if (!ArrayBufferIsView(this)) - throw new ERR_INVALID_ARG_TYPE('source', ['Buffer', 'Uint8Array'], this); if (!ArrayBufferIsView(target)) throw new ERR_INVALID_ARG_TYPE('target', ['Buffer', 'Uint8Array'], target);