Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zlib: avoid converting Uint8Array instances to Buffer #39492

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ const {
ObjectDefineProperties,
ObjectDefineProperty,
ObjectFreeze,
ObjectGetPrototypeOf,
ObjectKeys,
ObjectSetPrototypeOf,
ReflectApply,
StringPrototypeStartsWith,
Symbol,
TypedArrayPrototypeGetBuffer,
TypedArrayPrototypeGetByteLength,
TypedArrayPrototypeGetByteOffset,
TypedArrayPrototypeFill,
Uint32Array,
} = primordials;
Expand All @@ -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');
Expand Down Expand Up @@ -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);
}
Expand Down