From 28eab2cd50439b897abc34ff1a7c16f657ed6635 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 4 Feb 2024 16:24:07 -0500 Subject: [PATCH] Avoid augmenting subarrays post-ES2016 --- index.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index b0b5129..06e1977 100644 --- a/index.js +++ b/index.js @@ -1117,6 +1117,8 @@ function utf16leSlice (buf, start, end) { return res } +let subarrayCallsChild = false + Buffer.prototype.slice = function slice (start, end) { const len = this.length start = ~~start @@ -1139,8 +1141,11 @@ Buffer.prototype.slice = function slice (start, end) { if (end < start) end = start const newBuf = this.subarray(start, end) - // Return an augmented `Uint8Array` instance - Object.setPrototypeOf(newBuf, Buffer.prototype) + + if (!subarrayCallsChild) { + // Return an augmented `Uint8Array` instance + Object.setPrototypeOf(newBuf, Buffer.prototype) + } return newBuf } @@ -2152,3 +2157,7 @@ function defineBigIntMethod (fn) { function BufferBigIntNotDefined () { throw new Error('BigInt not supported') } + +if (Buffer.TYPED_ARRAY_SUPPORT) { + subarrayCallsChild = createBuffer(1).subarray(0, 1) instanceof Buffer +}