zlib: support Uint8Array in convenience methods by TimothyGu · Pull Request #12001 · nodejs/node · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
139 changes: 107 additions & 32 deletions doc/api/zlib.md
19 changes: 14 additions & 5 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
const Buffer = require('buffer').Buffer;
const internalUtil = require('internal/util');
const Transform = require('_stream_transform');
const { isUint8Array } = process.binding('util');
const binding = process.binding('zlib');
const assert = require('assert').ok;
const kMaxLength = require('buffer').kMaxLength;
Expand Down Expand Up @@ -78,6 +79,13 @@ function isInvalidStrategy(strategy) {
}

function zlibBuffer(engine, buffer, callback) {
// Streams do not support non-Buffer Uint8Arrays yet. Convert it to a
// Buffer without copying.
if (isUint8Array(buffer) &&
Object.getPrototypeOf(buffer) !== Buffer.prototype) {
buffer = Buffer.from(buffer.buffer, buffer.byteOffset, buffer.byteLength);
}

var buffers = [];
var nread = 0;

Expand Down Expand Up @@ -121,8 +129,9 @@ function zlibBuffer(engine, buffer, callback) {
function zlibBufferSync(engine, buffer) {
if (typeof buffer === 'string')
buffer = Buffer.from(buffer);
if (!(buffer instanceof Buffer))
throw new TypeError('Not a string or buffer');
else if (!isUint8Array(buffer))
throw new TypeError('"buffer" argument must be a string, Buffer, or ' +
'Uint8Array');

var flushFlag = engine._finishFlushFlag;

Expand Down Expand Up @@ -205,9 +214,9 @@ class Zlib extends Transform {
throw new TypeError('Invalid strategy: ' + opts.strategy);

if (opts.dictionary) {
if (!(opts.dictionary instanceof Buffer)) {
if (!isUint8Array(opts.dictionary)) {
throw new TypeError(
'Invalid dictionary: it should be a Buffer instance');
'Invalid dictionary: it should be a Buffer or an Uint8Array');
}
}

Expand Down Expand Up @@ -302,7 +311,7 @@ class Zlib extends Transform {
var ending = ws.ending || ws.ended;
var last = ending && (!chunk || ws.length === chunk.length);

if (chunk !== null && !(chunk instanceof Buffer))
if (chunk !== null && !isUint8Array(chunk))
return cb(new TypeError('invalid input'));

if (!this._handle)
Expand Down
81 changes: 41 additions & 40 deletions test/parallel/test-zlib-convenience-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,59 +22,60 @@
'use strict';
// test convenience methods with and without options supplied

require('../common');
const common = require('../common');
const assert = require('assert');
const zlib = require('zlib');

let hadRun = 0;

const expect = 'blahblahblahblahblahblah';
const expectStr = 'blahblahblahblahblahblah';
const expectBuf = Buffer.from(expectStr);
const expectUint8Array = new Uint8Array(expectBuf);
const opts = {
level: 9,
chunkSize: 1024,
};

[
for (const method of [
['gzip', 'gunzip'],
['gzip', 'unzip'],
['deflate', 'inflate'],
['deflateRaw', 'inflateRaw'],
].forEach(function(method) {

zlib[method[0]](expect, opts, function(err, result) {
zlib[method[1]](result, opts, function(err, result) {
assert.strictEqual(result.toString(), expect,
'Should get original string after ' +
method[0] + '/' + method[1] + ' with options.');
hadRun++;
});
});

zlib[method[0]](expect, function(err, result) {
zlib[method[1]](result, function(err, result) {
assert.strictEqual(result.toString(), expect,
'Should get original string after ' +
method[0] + '/' + method[1] + ' without options.');
hadRun++;
});
});
]) {
for (const [type, expect] of [
['string', expectStr],
['Buffer', expectBuf],
['Uint8Array', expectUint8Array]
]) {
zlib[method[0]](expect, opts, common.mustCall((err, result) => {
zlib[method[1]](result, opts, common.mustCall((err, result) => {
assert.strictEqual(result.toString(), expectStr,
`Should get original string after ${method[0]}/` +
`${method[1]} ${type} with options.`);
}));
}));

let result = zlib[method[0] + 'Sync'](expect, opts);
result = zlib[method[1] + 'Sync'](result, opts);
assert.strictEqual(result.toString(), expect,
'Should get original string after ' +
method[0] + '/' + method[1] + ' with options.');
hadRun++;
zlib[method[0]](expect, common.mustCall((err, result) => {
zlib[method[1]](result, common.mustCall((err, result) => {
assert.strictEqual(result.toString(), expectStr,
`Should get original string after ${method[0]}/` +
`${method[1]} ${type} without options.`);
}));
}));

result = zlib[method[0] + 'Sync'](expect);
result = zlib[method[1] + 'Sync'](result);
assert.strictEqual(result.toString(), expect,
'Should get original string after ' +
method[0] + '/' + method[1] + ' without options.');
hadRun++;
{
const compressed = zlib[method[0] + 'Sync'](expect, opts);
const decompressed = zlib[method[1] + 'Sync'](compressed, opts);
assert.strictEqual(decompressed.toString(), expectStr,
`Should get original string after ${method[0]}Sync/` +
`${method[1]}Sync ${type} with options.`);
}

});

process.on('exit', function() {
assert.strictEqual(hadRun, 16, 'expect 16 compressions');
});
{
const compressed = zlib[method[0] + 'Sync'](expect);
const decompressed = zlib[method[1] + 'Sync'](compressed);
assert.strictEqual(decompressed.toString(), expectStr,
`Should get original string after ${method[0]}Sync/` +
`${method[1]}Sync ${type} without options.`);
}
}
}
2 changes: 1 addition & 1 deletion test/parallel/test-zlib-deflate-constructors.js
Loading