| 618 | // Base class for all streams actually backed by zlib and using zlib-specific |
| 619 | // parameters. |
| 620 | function Zlib(opts, mode) { |
| 621 | let windowBits = Z_DEFAULT_WINDOWBITS; |
| 622 | let level = Z_DEFAULT_COMPRESSION; |
| 623 | let memLevel = Z_DEFAULT_MEMLEVEL; |
| 624 | let strategy = Z_DEFAULT_STRATEGY; |
| 625 | let dictionary; |
| 626 | |
| 627 | if (opts) { |
| 628 | // windowBits is special. On the compression side, 0 is an invalid value. |
| 629 | // But on the decompression side, a value of 0 for windowBits tells zlib |
| 630 | // to use the window size in the zlib header of the compressed stream. |
| 631 | if ((opts.windowBits == null || opts.windowBits === 0) && |
| 632 | (mode === INFLATE || |
| 633 | mode === GUNZIP || |
| 634 | mode === UNZIP)) { |
| 635 | windowBits = 0; |
| 636 | } else { |
| 637 | // `{ windowBits: 8 }` is valid for deflate but not gzip. |
| 638 | const min = Z_MIN_WINDOWBITS + (mode === GZIP ? 1 : 0); |
| 639 | windowBits = checkRangesOrGetDefault( |
| 640 | opts.windowBits, 'options.windowBits', |
| 641 | min, Z_MAX_WINDOWBITS, Z_DEFAULT_WINDOWBITS); |
| 642 | } |
| 643 | |
| 644 | level = checkRangesOrGetDefault( |
| 645 | opts.level, 'options.level', |
| 646 | Z_MIN_LEVEL, Z_MAX_LEVEL, Z_DEFAULT_COMPRESSION); |
| 647 | |
| 648 | memLevel = checkRangesOrGetDefault( |
| 649 | opts.memLevel, 'options.memLevel', |
| 650 | Z_MIN_MEMLEVEL, Z_MAX_MEMLEVEL, Z_DEFAULT_MEMLEVEL); |
| 651 | |
| 652 | strategy = checkRangesOrGetDefault( |
| 653 | opts.strategy, 'options.strategy', |
| 654 | Z_DEFAULT_STRATEGY, Z_FIXED, Z_DEFAULT_STRATEGY); |
| 655 | |
| 656 | dictionary = opts.dictionary; |
| 657 | if (dictionary !== undefined && !isArrayBufferView(dictionary)) { |
| 658 | if (isAnyArrayBuffer(dictionary)) { |
| 659 | dictionary = Buffer.from(dictionary); |
| 660 | } else { |
| 661 | throw new ERR_INVALID_ARG_TYPE( |
| 662 | 'options.dictionary', |
| 663 | ['Buffer', 'TypedArray', 'DataView', 'ArrayBuffer'], |
| 664 | dictionary, |
| 665 | ); |
| 666 | } |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | const handle = new binding.Zlib(mode); |
| 671 | // Ideally, we could let ZlibBase() set up _writeState. I haven't been able |
| 672 | // to come up with a good solution that doesn't break our internal API, |
| 673 | // and with it all supported npm versions at the time of writing. |
| 674 | this._writeState = new Uint32Array(2); |
| 675 | handle.init(windowBits, |
| 676 | level, |
| 677 | memLevel, |