summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/minizlib/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'deps/npm/node_modules/minizlib/index.js')
-rw-r--r--deps/npm/node_modules/minizlib/index.js159
1 files changed, 65 insertions, 94 deletions
diff --git a/deps/npm/node_modules/minizlib/index.js b/deps/npm/node_modules/minizlib/index.js
index c91a59c92d..df486965c7 100644
--- a/deps/npm/node_modules/minizlib/index.js
+++ b/deps/npm/node_modules/minizlib/index.js
@@ -2,11 +2,13 @@
const assert = require('assert')
const Buffer = require('buffer').Buffer
-const binding = process.binding('zlib')
+const realZlib = require('zlib')
const constants = exports.constants = require('./constants.js')
const MiniPass = require('minipass')
+const OriginalBufferConcat = Buffer.concat
+
class ZlibError extends Error {
constructor (msg, errno) {
super('zlib: ' + msg)
@@ -54,24 +56,19 @@ const strategies = new Set([
// true or false if there is anything in the queue when
// you call the .write() method.
const _opts = Symbol('opts')
-const _chunkSize = Symbol('chunkSize')
const _flushFlag = Symbol('flushFlag')
const _finishFlush = Symbol('finishFlush')
const _handle = Symbol('handle')
-const _hadError = Symbol('hadError')
-const _buffer = Symbol('buffer')
-const _offset = Symbol('offset')
+const _onError = Symbol('onError')
const _level = Symbol('level')
const _strategy = Symbol('strategy')
const _ended = Symbol('ended')
-const _writeState = Symbol('writeState')
class Zlib extends MiniPass {
constructor (opts, mode) {
super(opts)
this[_ended] = false
this[_opts] = opts = opts || {}
- this[_chunkSize] = opts.chunkSize || constants.Z_DEFAULT_CHUNK
if (opts.flush && !validFlushFlags.has(opts.flush)) {
throw new TypeError('Invalid flush flag: ' + opts.flush)
}
@@ -119,18 +116,17 @@ class Zlib extends MiniPass {
}
}
- this[_handle] = new binding.Zlib(mode)
+ this[_handle] = new realZlib[mode](opts)
- this[_hadError] = false
- this[_handle].onerror = (message, errno) => {
+ this[_onError] = (err) => {
// there is no way to cleanly recover.
// continuing only obscures problems.
this.close()
- this[_hadError] = true
- const error = new ZlibError(message, errno)
+ const error = new ZlibError(err.message, err.errno)
this.emit('error', error)
}
+ this[_handle].on('error', this[_onError])
const level = typeof opts.level === 'number' ? opts.level
: constants.Z_DEFAULT_COMPRESSION
@@ -138,30 +134,9 @@ class Zlib extends MiniPass {
var strategy = typeof opts.strategy === 'number' ? opts.strategy
: constants.Z_DEFAULT_STRATEGY
- this[_writeState] = new Uint32Array(2);
- const window = opts.windowBits || constants.Z_DEFAULT_WINDOWBITS
- const memLevel = opts.memLevel || constants.Z_DEFAULT_MEMLEVEL
-
// API changed in node v9
/* istanbul ignore next */
- if (/^v[0-8]\./.test(process.version)) {
- this[_handle].init(window,
- level,
- memLevel,
- strategy,
- opts.dictionary)
- } else {
- this[_handle].init(window,
- level,
- memLevel,
- strategy,
- this[_writeState],
- () => {},
- opts.dictionary)
- }
- this[_buffer] = Buffer.allocUnsafe(this[_chunkSize])
- this[_offset] = 0
this[_level] = level
this[_strategy] = strategy
@@ -196,9 +171,18 @@ class Zlib extends MiniPass {
if (this[_level] !== level || this[_strategy] !== strategy) {
this.flush(constants.Z_SYNC_FLUSH)
assert(this[_handle], 'zlib binding closed')
+ // .params() calls .flush(), but the latter is always async in the
+ // core zlib. We override .flush() temporarily to intercept that and
+ // flush synchronously.
+ const origFlush = this[_handle].flush
+ this[_handle].flush = (flushFlag, cb) => {
+ this[_handle].flush = origFlush
+ this.flush(flushFlag)
+ cb()
+ }
this[_handle].params(level, strategy)
/* istanbul ignore else */
- if (!this[_hadError]) {
+ if (this[_handle]) {
this[_level] = level
this[_strategy] = strategy
}
@@ -244,64 +228,51 @@ class Zlib extends MiniPass {
if (typeof chunk === 'string')
chunk = Buffer.from(chunk, encoding)
- let availInBefore = chunk && chunk.length
- let availOutBefore = this[_chunkSize] - this[_offset]
- let inOff = 0 // the offset of the input buffer
- const flushFlag = this[_flushFlag]
- let writeReturn = true
-
assert(this[_handle], 'zlib binding closed')
- do {
- let res = this[_handle].writeSync(
- flushFlag,
- chunk, // in
- inOff, // in_off
- availInBefore, // in_len
- this[_buffer], // out
- this[_offset], //out_off
- availOutBefore // out_len
- )
-
- if (this[_hadError])
- break
-
- // API changed in v9
- /* istanbul ignore next */
- let availInAfter = res ? res[0] : this[_writeState][1]
- /* istanbul ignore next */
- let availOutAfter = res ? res[1] : this[_writeState][0]
-
- const have = availOutBefore - availOutAfter
- assert(have >= 0, 'have should not go down')
-
- if (have > 0) {
- const out = this[_buffer].slice(
- this[_offset], this[_offset] + have
- )
-
- this[_offset] += have
- // serve some output to the consumer.
- writeReturn = super.write(out) && writeReturn
- }
- // exhausted the output buffer, or used all the input create a new one.
- if (availOutAfter === 0 || this[_offset] >= this[_chunkSize]) {
- availOutBefore = this[_chunkSize]
- this[_offset] = 0
- this[_buffer] = Buffer.allocUnsafe(this[_chunkSize])
+ // _processChunk tries to .close() the native handle after it's done, so we
+ // intercept that by temporarily making it a no-op.
+ const nativeHandle = this[_handle]._handle
+ const originalNativeClose = nativeHandle.close
+ nativeHandle.close = () => {}
+ const originalClose = this[_handle].close
+ this[_handle].close = () => {}
+ // It also calls `Buffer.concat()` at the end, which may be convenient
+ // for some, but which we are not interested in as it slows us down.
+ Buffer.concat = (args) => args
+ let result
+ try {
+ result = this[_handle]._processChunk(chunk, this[_flushFlag])
+ } catch (err) {
+ this[_onError](err)
+ } finally {
+ Buffer.concat = OriginalBufferConcat
+ if (this[_handle]) {
+ // Core zlib resets `_handle` to null after attempting to close the
+ // native handle. Our no-op handler prevented actual closure, but we
+ // need to restore the `._handle` property.
+ this[_handle]._handle = nativeHandle
+ nativeHandle.close = originalNativeClose
+ this[_handle].close = originalClose
+ // `_processChunk()` adds an 'error' listener. If we don't remove it
+ // after each call, these handlers start piling up.
+ this[_handle].removeAllListeners('error')
}
+ }
- if (availOutAfter === 0) {
- // Not actually done. Need to reprocess.
- // Also, update the availInBefore to the availInAfter value,
- // so that if we have to hit it a third (fourth, etc.) time,
- // it'll have the correct byte counts.
- inOff += (availInBefore - availInAfter)
- availInBefore = availInAfter
- continue
+ let writeReturn
+ if (result) {
+ if (Array.isArray(result) && result.length > 0) {
+ // The first buffer is always `handle._outBuffer`, which would be
+ // re-used for later invocations; so, we always have to copy that one.
+ writeReturn = super.write(Buffer.from(result[0]))
+ for (let i = 1; i < result.length; i++) {
+ writeReturn = super.write(result[i])
+ }
+ } else {
+ writeReturn = super.write(Buffer.from(result))
}
- break
- } while (!this[_hadError])
+ }
if (cb)
cb()
@@ -312,46 +283,46 @@ class Zlib extends MiniPass {
// minimal 2-byte header
class Deflate extends Zlib {
constructor (opts) {
- super(opts, constants.DEFLATE)
+ super(opts, 'Deflate')
}
}
class Inflate extends Zlib {
constructor (opts) {
- super(opts, constants.INFLATE)
+ super(opts, 'Inflate')
}
}
// gzip - bigger header, same deflate compression
class Gzip extends Zlib {
constructor (opts) {
- super(opts, constants.GZIP)
+ super(opts, 'Gzip')
}
}
class Gunzip extends Zlib {
constructor (opts) {
- super(opts, constants.GUNZIP)
+ super(opts, 'Gunzip')
}
}
// raw - no header
class DeflateRaw extends Zlib {
constructor (opts) {
- super(opts, constants.DEFLATERAW)
+ super(opts, 'DeflateRaw')
}
}
class InflateRaw extends Zlib {
constructor (opts) {
- super(opts, constants.INFLATERAW)
+ super(opts, 'InflateRaw')
}
}
// auto-detect header.
class Unzip extends Zlib {
constructor (opts) {
- super(opts, constants.UNZIP)
+ super(opts, 'Unzip')
}
}