diff options
author | Timothy Gu <timothygu99@gmail.com> | 2017-03-22 19:45:03 -0700 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2017-04-03 09:50:09 +0200 |
commit | 91383e47fdbb87ae45365396dd0150dcad8ed967 (patch) | |
tree | b68f4883bf29d10bf78aa097fd3c3c8ca736d855 /lib | |
parent | 2d039ffa29e4c4366a1028555b71cb36b7129fb1 (diff) | |
download | android-node-v8-91383e47fdbb87ae45365396dd0150dcad8ed967.tar.gz android-node-v8-91383e47fdbb87ae45365396dd0150dcad8ed967.tar.bz2 android-node-v8-91383e47fdbb87ae45365396dd0150dcad8ed967.zip |
zlib: support Uint8Array in convenience methods
Also support Uint8Array as a `dictionary` option.
PR-URL: https://github.com/nodejs/node/pull/12001
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/zlib.js | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/lib/zlib.js b/lib/zlib.js index ab06c73689..07040c3ebc 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -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; @@ -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; @@ -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; @@ -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'); } } @@ -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) |