zlib: add ArrayBuffer support
PR-URL: https://github.com/nodejs/node/pull/16042 Refs: https://github.com/nodejs/node/issues/1826 Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
parent
d64b0a85e6
commit
284871862e
@ -286,6 +286,9 @@ Compression strategy.
|
||||
<!-- YAML
|
||||
added: v0.11.1
|
||||
changes:
|
||||
- version: REPLACEME
|
||||
pr-url: https://github.com/nodejs/node/pull/16042
|
||||
description: The `dictionary` option can be an ArrayBuffer.
|
||||
- version: v8.0.0
|
||||
pr-url: https://github.com/nodejs/node/pull/12001
|
||||
description: The `dictionary` option can be an Uint8Array now.
|
||||
@ -308,8 +311,8 @@ ignored by the decompression classes.
|
||||
* `level` {integer} (compression only)
|
||||
* `memLevel` {integer} (compression only)
|
||||
* `strategy` {integer} (compression only)
|
||||
* `dictionary` {Buffer|TypedArray|DataView} (deflate/inflate only, empty dictionary by
|
||||
default)
|
||||
* `dictionary` {Buffer|TypedArray|DataView|ArrayBuffer} (deflate/inflate only,
|
||||
empty dictionary by default)
|
||||
* `info` {boolean} (If `true`, returns an object with `buffer` and `engine`)
|
||||
|
||||
See the description of `deflateInit2` and `inflateInit2` at
|
||||
@ -511,9 +514,10 @@ Creates and returns a new [Unzip][] object with the given [options][].
|
||||
|
||||
<!--type=misc-->
|
||||
|
||||
All of these take a [`Buffer`][], [`TypedArray`][], [`DataView`][], or string as
|
||||
the first argument, an optional second argument to supply options to the `zlib`
|
||||
classes and will call the supplied callback with `callback(error, result)`.
|
||||
All of these take a [`Buffer`][], [`TypedArray`][], [`DataView`][],
|
||||
[`ArrayBuffer`][] or string as the first argument, an optional second argument
|
||||
to supply options to the `zlib` classes and will call the supplied callback
|
||||
with `callback(error, result)`.
|
||||
|
||||
Every method has a `*Sync` counterpart, which accept the same arguments, but
|
||||
without a callback.
|
||||
@ -522,6 +526,9 @@ without a callback.
|
||||
<!-- YAML
|
||||
added: v0.6.0
|
||||
changes:
|
||||
- version: REPLACEME
|
||||
pr-url: https://github.com/nodejs/node/pull/16042
|
||||
description: The `buffer` parameter can be an ArrayBuffer.
|
||||
- version: v8.0.0
|
||||
pr-url: https://github.com/nodejs/node/pull/12223
|
||||
description: The `buffer` parameter can be any TypedArray or DataView now.
|
||||
@ -533,6 +540,9 @@ changes:
|
||||
<!-- YAML
|
||||
added: v0.11.12
|
||||
changes:
|
||||
- version: REPLACEME
|
||||
pr-url: https://github.com/nodejs/node/pull/16042
|
||||
description: The `buffer` parameter can be an ArrayBuffer.
|
||||
- version: v8.0.0
|
||||
pr-url: https://github.com/nodejs/node/pull/12223
|
||||
description: The `buffer` parameter can be any TypedArray or DataView now.
|
||||
@ -541,7 +551,7 @@ changes:
|
||||
description: The `buffer` parameter can be an Uint8Array now.
|
||||
-->
|
||||
|
||||
- `buffer` {Buffer|TypedArray|DataView|string}
|
||||
- `buffer` {Buffer|TypedArray|DataView|ArrayBuffer|string}
|
||||
|
||||
Compress a chunk of data with [Deflate][].
|
||||
|
||||
@ -560,6 +570,9 @@ changes:
|
||||
<!-- YAML
|
||||
added: v0.11.12
|
||||
changes:
|
||||
- version: REPLACEME
|
||||
pr-url: https://github.com/nodejs/node/pull/16042
|
||||
description: The `buffer` parameter can be an ArrayBuffer.
|
||||
- version: v8.0.0
|
||||
pr-url: https://github.com/nodejs/node/pull/12223
|
||||
description: The `buffer` parameter can be any TypedArray or DataView now.
|
||||
@ -568,7 +581,7 @@ changes:
|
||||
description: The `buffer` parameter can be an Uint8Array now.
|
||||
-->
|
||||
|
||||
- `buffer` {Buffer|TypedArray|DataView|string}
|
||||
- `buffer` {Buffer|TypedArray|DataView|ArrayBuffer|string}
|
||||
|
||||
Compress a chunk of data with [DeflateRaw][].
|
||||
|
||||
@ -576,6 +589,9 @@ Compress a chunk of data with [DeflateRaw][].
|
||||
<!-- YAML
|
||||
added: v0.6.0
|
||||
changes:
|
||||
- version: REPLACEME
|
||||
pr-url: https://github.com/nodejs/node/pull/16042
|
||||
description: The `buffer` parameter can be an ArrayBuffer.
|
||||
- version: v8.0.0
|
||||
pr-url: https://github.com/nodejs/node/pull/12223
|
||||
description: The `buffer` parameter can be any TypedArray or DataView now.
|
||||
@ -587,6 +603,9 @@ changes:
|
||||
<!-- YAML
|
||||
added: v0.11.12
|
||||
changes:
|
||||
- version: REPLACEME
|
||||
pr-url: https://github.com/nodejs/node/pull/16042
|
||||
description: The `buffer` parameter can be an ArrayBuffer.
|
||||
- version: v8.0.0
|
||||
pr-url: https://github.com/nodejs/node/pull/12223
|
||||
description: The `buffer` parameter can be any TypedArray or DataView now.
|
||||
@ -595,7 +614,7 @@ changes:
|
||||
description: The `buffer` parameter can be an Uint8Array now.
|
||||
-->
|
||||
|
||||
- `buffer` {Buffer|TypedArray|DataView|string}
|
||||
- `buffer` {Buffer|TypedArray|DataView|ArrayBuffer|string}
|
||||
|
||||
Decompress a chunk of data with [Gunzip][].
|
||||
|
||||
@ -603,6 +622,9 @@ Decompress a chunk of data with [Gunzip][].
|
||||
<!-- YAML
|
||||
added: v0.6.0
|
||||
changes:
|
||||
- version: REPLACEME
|
||||
pr-url: https://github.com/nodejs/node/pull/16042
|
||||
description: The `buffer` parameter can be an ArrayBuffer.
|
||||
- version: v8.0.0
|
||||
pr-url: https://github.com/nodejs/node/pull/12223
|
||||
description: The `buffer` parameter can be any TypedArray or DataView now.
|
||||
@ -614,6 +636,9 @@ changes:
|
||||
<!-- YAML
|
||||
added: v0.11.12
|
||||
changes:
|
||||
- version: REPLACEME
|
||||
pr-url: https://github.com/nodejs/node/pull/16042
|
||||
description: The `buffer` parameter can be an ArrayBuffer.
|
||||
- version: v8.0.0
|
||||
pr-url: https://github.com/nodejs/node/pull/12223
|
||||
description: The `buffer` parameter can be any TypedArray or DataView now.
|
||||
@ -622,7 +647,7 @@ changes:
|
||||
description: The `buffer` parameter can be an Uint8Array now.
|
||||
-->
|
||||
|
||||
- `buffer` {Buffer|TypedArray|DataView|string}
|
||||
- `buffer` {Buffer|TypedArray|DataView|ArrayBuffer|string}
|
||||
|
||||
Compress a chunk of data with [Gzip][].
|
||||
|
||||
@ -630,6 +655,9 @@ Compress a chunk of data with [Gzip][].
|
||||
<!-- YAML
|
||||
added: v0.6.0
|
||||
changes:
|
||||
- version: REPLACEME
|
||||
pr-url: https://github.com/nodejs/node/pull/16042
|
||||
description: The `buffer` parameter can be an ArrayBuffer.
|
||||
- version: v8.0.0
|
||||
pr-url: https://github.com/nodejs/node/pull/12223
|
||||
description: The `buffer` parameter can be any TypedArray or DataView now.
|
||||
@ -641,6 +669,9 @@ changes:
|
||||
<!-- YAML
|
||||
added: v0.11.12
|
||||
changes:
|
||||
- version: REPLACEME
|
||||
pr-url: https://github.com/nodejs/node/pull/16042
|
||||
description: The `buffer` parameter can be an ArrayBuffer.
|
||||
- version: v8.0.0
|
||||
pr-url: https://github.com/nodejs/node/pull/12223
|
||||
description: The `buffer` parameter can be any TypedArray or DataView now.
|
||||
@ -649,7 +680,7 @@ changes:
|
||||
description: The `buffer` parameter can be an Uint8Array now.
|
||||
-->
|
||||
|
||||
- `buffer` {Buffer|TypedArray|DataView|string}
|
||||
- `buffer` {Buffer|TypedArray|DataView|ArrayBuffer|string}
|
||||
|
||||
Decompress a chunk of data with [Inflate][].
|
||||
|
||||
@ -657,6 +688,9 @@ Decompress a chunk of data with [Inflate][].
|
||||
<!-- YAML
|
||||
added: v0.6.0
|
||||
changes:
|
||||
- version: REPLACEME
|
||||
pr-url: https://github.com/nodejs/node/pull/16042
|
||||
description: The `buffer` parameter can be an ArrayBuffer.
|
||||
- version: v8.0.0
|
||||
pr-url: https://github.com/nodejs/node/pull/12223
|
||||
description: The `buffer` parameter can be any TypedArray or DataView now.
|
||||
@ -668,6 +702,9 @@ changes:
|
||||
<!-- YAML
|
||||
added: v0.11.12
|
||||
changes:
|
||||
- version: REPLACEME
|
||||
pr-url: https://github.com/nodejs/node/pull/16042
|
||||
description: The `buffer` parameter can be an ArrayBuffer.
|
||||
- version: v8.0.0
|
||||
pr-url: https://github.com/nodejs/node/pull/12223
|
||||
description: The `buffer` parameter can be any TypedArray or DataView now.
|
||||
@ -676,7 +713,7 @@ changes:
|
||||
description: The `buffer` parameter can be an Uint8Array now.
|
||||
-->
|
||||
|
||||
- `buffer` {Buffer|TypedArray|DataView|string}
|
||||
- `buffer` {Buffer|TypedArray|DataView|ArrayBuffer|string}
|
||||
|
||||
Decompress a chunk of data with [InflateRaw][].
|
||||
|
||||
@ -684,6 +721,9 @@ Decompress a chunk of data with [InflateRaw][].
|
||||
<!-- YAML
|
||||
added: v0.6.0
|
||||
changes:
|
||||
- version: REPLACEME
|
||||
pr-url: https://github.com/nodejs/node/pull/16042
|
||||
description: The `buffer` parameter can be an ArrayBuffer.
|
||||
- version: v8.0.0
|
||||
pr-url: https://github.com/nodejs/node/pull/12223
|
||||
description: The `buffer` parameter can be any TypedArray or DataView now.
|
||||
@ -695,6 +735,9 @@ changes:
|
||||
<!-- YAML
|
||||
added: v0.11.12
|
||||
changes:
|
||||
- version: REPLACEME
|
||||
pr-url: https://github.com/nodejs/node/pull/16042
|
||||
description: The `buffer` parameter can be an ArrayBuffer.
|
||||
- version: v8.0.0
|
||||
pr-url: https://github.com/nodejs/node/pull/12223
|
||||
description: The `buffer` parameter can be any TypedArray or DataView now.
|
||||
@ -703,12 +746,13 @@ changes:
|
||||
description: The `buffer` parameter can be an Uint8Array now.
|
||||
-->
|
||||
|
||||
- `buffer` {Buffer|TypedArray|DataView|string}
|
||||
- `buffer` {Buffer|TypedArray|DataView|ArrayBuffer|string}
|
||||
|
||||
Decompress a chunk of data with [Unzip][].
|
||||
|
||||
[`.flush()`]: #zlib_zlib_flush_kind_callback
|
||||
[`Accept-Encoding`]: https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
|
||||
[`ArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
|
||||
[`Buffer`]: buffer.html#buffer_class_buffer
|
||||
[`Content-Encoding`]: https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.11
|
||||
[`DataView`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView
|
||||
|
24
lib/zlib.js
24
lib/zlib.js
@ -24,6 +24,7 @@
|
||||
const errors = require('internal/errors');
|
||||
const Transform = require('_stream_transform');
|
||||
const { _extend } = require('util');
|
||||
const { isAnyArrayBuffer } = process.binding('util');
|
||||
const { isArrayBufferView } = require('internal/util/types');
|
||||
const binding = process.binding('zlib');
|
||||
const assert = require('assert').ok;
|
||||
@ -67,6 +68,8 @@ function zlibBuffer(engine, buffer, callback) {
|
||||
if (isArrayBufferView(buffer) &&
|
||||
Object.getPrototypeOf(buffer) !== Buffer.prototype) {
|
||||
buffer = Buffer.from(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
||||
} else if (isAnyArrayBuffer(buffer)) {
|
||||
buffer = Buffer.from(buffer);
|
||||
}
|
||||
engine.buffers = null;
|
||||
engine.nread = 0;
|
||||
@ -114,9 +117,14 @@ function zlibBufferSync(engine, buffer) {
|
||||
if (typeof buffer === 'string') {
|
||||
buffer = Buffer.from(buffer);
|
||||
} else if (!isArrayBufferView(buffer)) {
|
||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
|
||||
'buffer',
|
||||
['string', 'Buffer', 'TypedArray', 'DataView']);
|
||||
if (isAnyArrayBuffer(buffer)) {
|
||||
buffer = Buffer.from(buffer);
|
||||
} else {
|
||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
|
||||
'buffer',
|
||||
['string', 'Buffer', 'TypedArray', 'DataView',
|
||||
'ArrayBuffer']);
|
||||
}
|
||||
}
|
||||
buffer = processChunkSync(engine, buffer, engine._finishFlushFlag);
|
||||
if (engine._info)
|
||||
@ -245,9 +253,13 @@ function Zlib(opts, mode) {
|
||||
|
||||
dictionary = opts.dictionary;
|
||||
if (dictionary !== undefined && !isArrayBufferView(dictionary)) {
|
||||
throw new errors.TypeError('ERR_INVALID_OPT_VALUE',
|
||||
'dictionary',
|
||||
dictionary);
|
||||
if (isAnyArrayBuffer(dictionary)) {
|
||||
dictionary = Buffer.from(dictionary);
|
||||
} else {
|
||||
throw new errors.TypeError('ERR_INVALID_OPT_VALUE',
|
||||
'dictionary',
|
||||
dictionary);
|
||||
}
|
||||
}
|
||||
|
||||
if (opts.encoding || opts.objectMode || opts.writableObjectMode) {
|
||||
|
@ -124,6 +124,13 @@ Checks if `pathname` exists
|
||||
|
||||
Returns an instance of all possible `ArrayBufferView`s of the provided Buffer.
|
||||
|
||||
### getBufferSources(buf)
|
||||
* `buf` [<Buffer>]
|
||||
* return [<BufferSource[]>]
|
||||
|
||||
Returns an instance of all possible `BufferSource`s of the provided Buffer,
|
||||
consisting of all `ArrayBufferView` and an `ArrayBuffer`.
|
||||
|
||||
### getCallSite(func)
|
||||
* `func` [<Function>]
|
||||
* return [<String>]
|
||||
|
@ -814,6 +814,10 @@ exports.getArrayBufferViews = function getArrayBufferViews(buf) {
|
||||
return out;
|
||||
};
|
||||
|
||||
exports.getBufferSources = function getBufferSources(buf) {
|
||||
return [...exports.getArrayBufferViews(buf), new Uint8Array(buf).buffer];
|
||||
};
|
||||
|
||||
// Crash the process on unhandled rejections.
|
||||
exports.crashOnUnhandledRejection = function() {
|
||||
process.on('unhandledRejection',
|
||||
|
@ -43,7 +43,7 @@ const optsInfo = {
|
||||
for (const [type, expect] of [
|
||||
['string', expectStr],
|
||||
['Buffer', expectBuf],
|
||||
...common.getArrayBufferViews(expectBuf).map((obj) =>
|
||||
...common.getBufferSources(expectBuf).map((obj) =>
|
||||
[obj[Symbol.toStringTag], obj]
|
||||
)
|
||||
]) {
|
||||
|
@ -167,7 +167,7 @@ function deflateRawResetDictionaryTest(spdyDict) {
|
||||
});
|
||||
}
|
||||
|
||||
for (const dict of [spdyDict, ...common.getArrayBufferViews(spdyDict)]) {
|
||||
for (const dict of [spdyDict, ...common.getBufferSources(spdyDict)]) {
|
||||
basicDictionaryTest(dict);
|
||||
deflateResetDictionaryTest(dict);
|
||||
rawDictionaryTest(dict);
|
||||
|
@ -13,7 +13,7 @@ const zlib = require('zlib');
|
||||
code: 'ERR_INVALID_ARG_TYPE',
|
||||
type: TypeError,
|
||||
message: 'The "buffer" argument must be one of type string, Buffer, ' +
|
||||
'TypedArray, or DataView'
|
||||
'TypedArray, DataView, or ArrayBuffer'
|
||||
}
|
||||
);
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user