Skip to content

Commit

Permalink
zlib: avoid converting Uint8Array instances to Buffer
Browse files Browse the repository at this point in the history
PR-URL: #39492
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Zeyu Yang <himself65@outlook.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
aduh95 authored and targos committed Sep 4, 2021
1 parent 61261cd commit 61b4a98
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/zlib.js
Expand Up @@ -34,7 +34,6 @@ const {
ObjectDefineProperties,
ObjectDefineProperty,
ObjectFreeze,
ObjectGetPrototypeOf,
ObjectKeys,
ObjectSetPrototypeOf,
ReflectApply,
Expand All @@ -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');
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 61b4a98

Please sign in to comment.