From 8464969a296124fc8050ac492f8834cf009587d3 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Fri, 23 Jul 2021 00:06:49 +0200 Subject: [PATCH 1/2] zlib: avoid converting `Uint8Array` instances to `Buffer` --- lib/zlib.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/zlib.js b/lib/zlib.js index 83116fd259c755..22bac42377f42e 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -34,12 +34,14 @@ const { ObjectDefineProperties, ObjectDefineProperty, ObjectFreeze, - ObjectGetPrototypeOf, ObjectKeys, ObjectSetPrototypeOf, ReflectApply, StringPrototypeStartsWith, Symbol, + TypedArrayPrototypeGetBuffer, + TypedArrayPrototypeGetByteLength, + TypedArrayPrototypeGetByteOffset, TypedArrayPrototypeFill, Uint32Array, } = primordials; @@ -60,7 +62,8 @@ const { } = require('internal/util'); const { isArrayBufferView, - isAnyArrayBuffer + isAnyArrayBuffer, + isUint8Array, } = require('internal/util/types'); const binding = internalBinding('zlib'); const assert = require('internal/assert'); @@ -112,11 +115,14 @@ for (const ckey of ObjectKeys(codes)) { function zlibBuffer(engine, buffer, callback) { validateFunction(callback, '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) { - buffer = Buffer.from(buffer.buffer, buffer.byteOffset, buffer.byteLength); + if (isArrayBufferView(buffer) && !isUint8Array(buffer)) { + buffer = Buffer.from( + TypedArrayPrototypeGetBuffer(buffer), + TypedArrayPrototypeGetByteOffset(buffer), + TypedArrayPrototypeGetByteLength(buffer) + ); } else if (isAnyArrayBuffer(buffer)) { buffer = Buffer.from(buffer); } From 22a8376f4a1508acac403891e85602a1b9a19e79 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Fri, 23 Jul 2021 10:04:51 +0200 Subject: [PATCH 2/2] fixup! zlib: avoid converting `Uint8Array` instances to `Buffer` --- lib/zlib.js | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/lib/zlib.js b/lib/zlib.js index 22bac42377f42e..35e16534fb3d52 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -39,9 +39,6 @@ const { ReflectApply, StringPrototypeStartsWith, Symbol, - TypedArrayPrototypeGetBuffer, - TypedArrayPrototypeGetByteLength, - TypedArrayPrototypeGetByteOffset, TypedArrayPrototypeFill, Uint32Array, } = primordials; @@ -118,11 +115,7 @@ function zlibBuffer(engine, buffer, callback) { // Streams do not support non-Uint8Array ArrayBufferViews yet. Convert it to a // Buffer without copying. if (isArrayBufferView(buffer) && !isUint8Array(buffer)) { - buffer = Buffer.from( - TypedArrayPrototypeGetBuffer(buffer), - TypedArrayPrototypeGetByteOffset(buffer), - TypedArrayPrototypeGetByteLength(buffer) - ); + buffer = Buffer.from(buffer.buffer, buffer.byteOffset, buffer.byteLength); } else if (isAnyArrayBuffer(buffer)) { buffer = Buffer.from(buffer); }