From 61b4a984809ca078215902e3c5efda6384228ffd Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Fri, 23 Jul 2021 00:06:49 +0200 Subject: [PATCH] zlib: avoid converting `Uint8Array` instances to `Buffer` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/39492 Reviewed-By: James M Snell Reviewed-By: Zeyu Yang Reviewed-By: Michaƫl Zasso Reviewed-By: Luigi Pinca --- lib/zlib.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/zlib.js b/lib/zlib.js index daabf7b5ca7489..6fcf0b6e46ce9b 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -34,7 +34,6 @@ const { ObjectDefineProperties, ObjectDefineProperty, ObjectFreeze, - ObjectGetPrototypeOf, ObjectKeys, ObjectSetPrototypeOf, ReflectApply, @@ -60,7 +59,8 @@ const { } = require('internal/util'); const { isArrayBufferView, - isAnyArrayBuffer + isAnyArrayBuffer, + isUint8Array, } = require('internal/util/types'); const binding = internalBinding('zlib'); const assert = require('internal/assert'); @@ -109,10 +109,9 @@ for (const ckey of ObjectKeys(codes)) { function zlibBuffer(engine, buffer, callback) { if (typeof callback !== 'function') throw new ERR_INVALID_ARG_TYPE('callback', 'function', callback); - // Streams do not support non-Buffer ArrayBufferViews yet. Convert it to a + // Streams do not support non-Uint8Array ArrayBufferViews yet. Convert it to a // Buffer without copying. - if (isArrayBufferView(buffer) && - ObjectGetPrototypeOf(buffer) !== Buffer.prototype) { + if (isArrayBufferView(buffer) && !isUint8Array(buffer)) { buffer = Buffer.from(buffer.buffer, buffer.byteOffset, buffer.byteLength); } else if (isAnyArrayBuffer(buffer)) { buffer = Buffer.from(buffer);