Skip to content

Commit 584e74d

Browse files
Jem BezooyenMylesBorins
Jem Bezooyen
authored andcommitted
zlib: add ArrayBuffer support
PR-URL: #16042 Refs: #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>
1 parent 6cb4cc2 commit 584e74d

7 files changed

+88
-21
lines changed

doc/api/zlib.md

+56-12
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ Compression strategy.
286286
<!-- YAML
287287
added: v0.11.1
288288
changes:
289+
- version: REPLACEME
290+
pr-url: https://github.com/nodejs/node/pull/16042
291+
description: The `dictionary` option can be an ArrayBuffer.
289292
- version: v8.0.0
290293
pr-url: https://github.com/nodejs/node/pull/12001
291294
description: The `dictionary` option can be an Uint8Array now.
@@ -308,8 +311,8 @@ ignored by the decompression classes.
308311
* `level` {integer} (compression only)
309312
* `memLevel` {integer} (compression only)
310313
* `strategy` {integer} (compression only)
311-
* `dictionary` {Buffer|TypedArray|DataView} (deflate/inflate only, empty dictionary by
312-
default)
314+
* `dictionary` {Buffer|TypedArray|DataView|ArrayBuffer} (deflate/inflate only,
315+
empty dictionary by default)
313316
* `info` {boolean} (If `true`, returns an object with `buffer` and `engine`)
314317

315318
See the description of `deflateInit2` and `inflateInit2` at
@@ -511,9 +514,10 @@ Creates and returns a new [Unzip][] object with the given [options][].
511514

512515
<!--type=misc-->
513516

514-
All of these take a [`Buffer`][], [`TypedArray`][], [`DataView`][], or string as
515-
the first argument, an optional second argument to supply options to the `zlib`
516-
classes and will call the supplied callback with `callback(error, result)`.
517+
All of these take a [`Buffer`][], [`TypedArray`][], [`DataView`][],
518+
[`ArrayBuffer`][] or string as the first argument, an optional second argument
519+
to supply options to the `zlib` classes and will call the supplied callback
520+
with `callback(error, result)`.
517521

518522
Every method has a `*Sync` counterpart, which accept the same arguments, but
519523
without a callback.
@@ -522,6 +526,9 @@ without a callback.
522526
<!-- YAML
523527
added: v0.6.0
524528
changes:
529+
- version: REPLACEME
530+
pr-url: https://github.com/nodejs/node/pull/16042
531+
description: The `buffer` parameter can be an ArrayBuffer.
525532
- version: v8.0.0
526533
pr-url: https://github.com/nodejs/node/pull/12223
527534
description: The `buffer` parameter can be any TypedArray or DataView now.
@@ -533,6 +540,9 @@ changes:
533540
<!-- YAML
534541
added: v0.11.12
535542
changes:
543+
- version: REPLACEME
544+
pr-url: https://github.com/nodejs/node/pull/16042
545+
description: The `buffer` parameter can be an ArrayBuffer.
536546
- version: v8.0.0
537547
pr-url: https://github.com/nodejs/node/pull/12223
538548
description: The `buffer` parameter can be any TypedArray or DataView now.
@@ -541,7 +551,7 @@ changes:
541551
description: The `buffer` parameter can be an Uint8Array now.
542552
-->
543553

544-
- `buffer` {Buffer|TypedArray|DataView|string}
554+
- `buffer` {Buffer|TypedArray|DataView|ArrayBuffer|string}
545555

546556
Compress a chunk of data with [Deflate][].
547557

@@ -560,6 +570,9 @@ changes:
560570
<!-- YAML
561571
added: v0.11.12
562572
changes:
573+
- version: REPLACEME
574+
pr-url: https://github.com/nodejs/node/pull/16042
575+
description: The `buffer` parameter can be an ArrayBuffer.
563576
- version: v8.0.0
564577
pr-url: https://github.com/nodejs/node/pull/12223
565578
description: The `buffer` parameter can be any TypedArray or DataView now.
@@ -568,14 +581,17 @@ changes:
568581
description: The `buffer` parameter can be an Uint8Array now.
569582
-->
570583

571-
- `buffer` {Buffer|TypedArray|DataView|string}
584+
- `buffer` {Buffer|TypedArray|DataView|ArrayBuffer|string}
572585

573586
Compress a chunk of data with [DeflateRaw][].
574587

575588
### zlib.gunzip(buffer[, options], callback)
576589
<!-- YAML
577590
added: v0.6.0
578591
changes:
592+
- version: REPLACEME
593+
pr-url: https://github.com/nodejs/node/pull/16042
594+
description: The `buffer` parameter can be an ArrayBuffer.
579595
- version: v8.0.0
580596
pr-url: https://github.com/nodejs/node/pull/12223
581597
description: The `buffer` parameter can be any TypedArray or DataView now.
@@ -587,6 +603,9 @@ changes:
587603
<!-- YAML
588604
added: v0.11.12
589605
changes:
606+
- version: REPLACEME
607+
pr-url: https://github.com/nodejs/node/pull/16042
608+
description: The `buffer` parameter can be an ArrayBuffer.
590609
- version: v8.0.0
591610
pr-url: https://github.com/nodejs/node/pull/12223
592611
description: The `buffer` parameter can be any TypedArray or DataView now.
@@ -595,14 +614,17 @@ changes:
595614
description: The `buffer` parameter can be an Uint8Array now.
596615
-->
597616

598-
- `buffer` {Buffer|TypedArray|DataView|string}
617+
- `buffer` {Buffer|TypedArray|DataView|ArrayBuffer|string}
599618

600619
Decompress a chunk of data with [Gunzip][].
601620

602621
### zlib.gzip(buffer[, options], callback)
603622
<!-- YAML
604623
added: v0.6.0
605624
changes:
625+
- version: REPLACEME
626+
pr-url: https://github.com/nodejs/node/pull/16042
627+
description: The `buffer` parameter can be an ArrayBuffer.
606628
- version: v8.0.0
607629
pr-url: https://github.com/nodejs/node/pull/12223
608630
description: The `buffer` parameter can be any TypedArray or DataView now.
@@ -614,6 +636,9 @@ changes:
614636
<!-- YAML
615637
added: v0.11.12
616638
changes:
639+
- version: REPLACEME
640+
pr-url: https://github.com/nodejs/node/pull/16042
641+
description: The `buffer` parameter can be an ArrayBuffer.
617642
- version: v8.0.0
618643
pr-url: https://github.com/nodejs/node/pull/12223
619644
description: The `buffer` parameter can be any TypedArray or DataView now.
@@ -622,14 +647,17 @@ changes:
622647
description: The `buffer` parameter can be an Uint8Array now.
623648
-->
624649

625-
- `buffer` {Buffer|TypedArray|DataView|string}
650+
- `buffer` {Buffer|TypedArray|DataView|ArrayBuffer|string}
626651

627652
Compress a chunk of data with [Gzip][].
628653

629654
### zlib.inflate(buffer[, options], callback)
630655
<!-- YAML
631656
added: v0.6.0
632657
changes:
658+
- version: REPLACEME
659+
pr-url: https://github.com/nodejs/node/pull/16042
660+
description: The `buffer` parameter can be an ArrayBuffer.
633661
- version: v8.0.0
634662
pr-url: https://github.com/nodejs/node/pull/12223
635663
description: The `buffer` parameter can be any TypedArray or DataView now.
@@ -641,6 +669,9 @@ changes:
641669
<!-- YAML
642670
added: v0.11.12
643671
changes:
672+
- version: REPLACEME
673+
pr-url: https://github.com/nodejs/node/pull/16042
674+
description: The `buffer` parameter can be an ArrayBuffer.
644675
- version: v8.0.0
645676
pr-url: https://github.com/nodejs/node/pull/12223
646677
description: The `buffer` parameter can be any TypedArray or DataView now.
@@ -649,14 +680,17 @@ changes:
649680
description: The `buffer` parameter can be an Uint8Array now.
650681
-->
651682

652-
- `buffer` {Buffer|TypedArray|DataView|string}
683+
- `buffer` {Buffer|TypedArray|DataView|ArrayBuffer|string}
653684

654685
Decompress a chunk of data with [Inflate][].
655686

656687
### zlib.inflateRaw(buffer[, options], callback)
657688
<!-- YAML
658689
added: v0.6.0
659690
changes:
691+
- version: REPLACEME
692+
pr-url: https://github.com/nodejs/node/pull/16042
693+
description: The `buffer` parameter can be an ArrayBuffer.
660694
- version: v8.0.0
661695
pr-url: https://github.com/nodejs/node/pull/12223
662696
description: The `buffer` parameter can be any TypedArray or DataView now.
@@ -668,6 +702,9 @@ changes:
668702
<!-- YAML
669703
added: v0.11.12
670704
changes:
705+
- version: REPLACEME
706+
pr-url: https://github.com/nodejs/node/pull/16042
707+
description: The `buffer` parameter can be an ArrayBuffer.
671708
- version: v8.0.0
672709
pr-url: https://github.com/nodejs/node/pull/12223
673710
description: The `buffer` parameter can be any TypedArray or DataView now.
@@ -676,14 +713,17 @@ changes:
676713
description: The `buffer` parameter can be an Uint8Array now.
677714
-->
678715

679-
- `buffer` {Buffer|TypedArray|DataView|string}
716+
- `buffer` {Buffer|TypedArray|DataView|ArrayBuffer|string}
680717

681718
Decompress a chunk of data with [InflateRaw][].
682719

683720
### zlib.unzip(buffer[, options], callback)
684721
<!-- YAML
685722
added: v0.6.0
686723
changes:
724+
- version: REPLACEME
725+
pr-url: https://github.com/nodejs/node/pull/16042
726+
description: The `buffer` parameter can be an ArrayBuffer.
687727
- version: v8.0.0
688728
pr-url: https://github.com/nodejs/node/pull/12223
689729
description: The `buffer` parameter can be any TypedArray or DataView now.
@@ -695,6 +735,9 @@ changes:
695735
<!-- YAML
696736
added: v0.11.12
697737
changes:
738+
- version: REPLACEME
739+
pr-url: https://github.com/nodejs/node/pull/16042
740+
description: The `buffer` parameter can be an ArrayBuffer.
698741
- version: v8.0.0
699742
pr-url: https://github.com/nodejs/node/pull/12223
700743
description: The `buffer` parameter can be any TypedArray or DataView now.
@@ -703,12 +746,13 @@ changes:
703746
description: The `buffer` parameter can be an Uint8Array now.
704747
-->
705748

706-
- `buffer` {Buffer|TypedArray|DataView|string}
749+
- `buffer` {Buffer|TypedArray|DataView|ArrayBuffer|string}
707750

708751
Decompress a chunk of data with [Unzip][].
709752

710753
[`.flush()`]: #zlib_zlib_flush_kind_callback
711754
[`Accept-Encoding`]: https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
755+
[`ArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
712756
[`Buffer`]: buffer.html#buffer_class_buffer
713757
[`Content-Encoding`]: https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.11
714758
[`DataView`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView

lib/zlib.js

+18-6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
const errors = require('internal/errors');
2525
const Transform = require('_stream_transform');
2626
const { _extend } = require('util');
27+
const { isAnyArrayBuffer } = process.binding('util');
2728
const { isArrayBufferView } = require('internal/util/types');
2829
const binding = process.binding('zlib');
2930
const assert = require('assert').ok;
@@ -67,6 +68,8 @@ function zlibBuffer(engine, buffer, callback) {
6768
if (isArrayBufferView(buffer) &&
6869
Object.getPrototypeOf(buffer) !== Buffer.prototype) {
6970
buffer = Buffer.from(buffer.buffer, buffer.byteOffset, buffer.byteLength);
71+
} else if (isAnyArrayBuffer(buffer)) {
72+
buffer = Buffer.from(buffer);
7073
}
7174
engine.buffers = null;
7275
engine.nread = 0;
@@ -114,9 +117,14 @@ function zlibBufferSync(engine, buffer) {
114117
if (typeof buffer === 'string') {
115118
buffer = Buffer.from(buffer);
116119
} else if (!isArrayBufferView(buffer)) {
117-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
118-
'buffer',
119-
['string', 'Buffer', 'TypedArray', 'DataView']);
120+
if (isAnyArrayBuffer(buffer)) {
121+
buffer = Buffer.from(buffer);
122+
} else {
123+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
124+
'buffer',
125+
['string', 'Buffer', 'TypedArray', 'DataView',
126+
'ArrayBuffer']);
127+
}
120128
}
121129
buffer = processChunkSync(engine, buffer, engine._finishFlushFlag);
122130
if (engine._info)
@@ -245,9 +253,13 @@ function Zlib(opts, mode) {
245253

246254
dictionary = opts.dictionary;
247255
if (dictionary !== undefined && !isArrayBufferView(dictionary)) {
248-
throw new errors.TypeError('ERR_INVALID_OPT_VALUE',
249-
'dictionary',
250-
dictionary);
256+
if (isAnyArrayBuffer(dictionary)) {
257+
dictionary = Buffer.from(dictionary);
258+
} else {
259+
throw new errors.TypeError('ERR_INVALID_OPT_VALUE',
260+
'dictionary',
261+
dictionary);
262+
}
251263
}
252264

253265
if (opts.encoding || opts.objectMode || opts.writableObjectMode) {

test/common/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ a reason otherwise.
133133

134134
Returns an instance of all possible `ArrayBufferView`s of the provided Buffer.
135135

136+
### getBufferSources(buf)
137+
* `buf` [&lt;Buffer>]
138+
* return [&lt;BufferSource&#91;&#93;>]
139+
140+
Returns an instance of all possible `BufferSource`s of the provided Buffer,
141+
consisting of all `ArrayBufferView` and an `ArrayBuffer`.
142+
136143
### getCallSite(func)
137144
* `func` [&lt;Function>]
138145
* return [&lt;String>]

test/common/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,10 @@ exports.getArrayBufferViews = function getArrayBufferViews(buf) {
814814
return out;
815815
};
816816

817+
exports.getBufferSources = function getBufferSources(buf) {
818+
return [...exports.getArrayBufferViews(buf), new Uint8Array(buf).buffer];
819+
};
820+
817821
// Crash the process on unhandled rejections.
818822
exports.crashOnUnhandledRejection = function() {
819823
process.on('unhandledRejection',

test/parallel/test-zlib-convenience-methods.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const optsInfo = {
4343
for (const [type, expect] of [
4444
['string', expectStr],
4545
['Buffer', expectBuf],
46-
...common.getArrayBufferViews(expectBuf).map((obj) =>
46+
...common.getBufferSources(expectBuf).map((obj) =>
4747
[obj[Symbol.toStringTag], obj]
4848
)
4949
]) {

test/parallel/test-zlib-dictionary.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ function deflateRawResetDictionaryTest(spdyDict) {
167167
});
168168
}
169169

170-
for (const dict of [spdyDict, ...common.getArrayBufferViews(spdyDict)]) {
170+
for (const dict of [spdyDict, ...common.getBufferSources(spdyDict)]) {
171171
basicDictionaryTest(dict);
172172
deflateResetDictionaryTest(dict);
173173
rawDictionaryTest(dict);

test/parallel/test-zlib-not-string-or-buffer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const zlib = require('zlib');
1313
code: 'ERR_INVALID_ARG_TYPE',
1414
type: TypeError,
1515
message: 'The "buffer" argument must be one of type string, Buffer, ' +
16-
'TypedArray, or DataView'
16+
'TypedArray, DataView, or ArrayBuffer'
1717
}
1818
);
1919
});

0 commit comments

Comments
 (0)