Projekt

Obecné

Profil

Stáhnout (17.1 KB) Statistiky
| Větev: | Revize:
1 3a515b92 cagy
'use strict';
2
3
const Buffer = require('buffer').Buffer;
4
const Transform = require('stream').Transform;
5
const binding = require('./binding');
6
const util = require('util');
7
const assert = require('assert').ok;
8
const kMaxLength = require('buffer').kMaxLength;
9
const kRangeErrorMessage = 'Cannot create final Buffer. It would be larger ' +
10
                           'than 0x' + kMaxLength.toString(16) + ' bytes';
11
12
// zlib doesn't provide these, so kludge them in following the same
13
// const naming scheme zlib uses.
14
binding.Z_MIN_WINDOWBITS = 8;
15
binding.Z_MAX_WINDOWBITS = 15;
16
binding.Z_DEFAULT_WINDOWBITS = 15;
17
18
// fewer than 64 bytes per chunk is stupid.
19
// technically it could work with as few as 8, but even 64 bytes
20
// is absurdly low.  Usually a MB or more is best.
21
binding.Z_MIN_CHUNK = 64;
22
binding.Z_MAX_CHUNK = Infinity;
23
binding.Z_DEFAULT_CHUNK = (16 * 1024);
24
25
binding.Z_MIN_MEMLEVEL = 1;
26
binding.Z_MAX_MEMLEVEL = 9;
27
binding.Z_DEFAULT_MEMLEVEL = 8;
28
29
binding.Z_MIN_LEVEL = -1;
30
binding.Z_MAX_LEVEL = 9;
31
binding.Z_DEFAULT_LEVEL = binding.Z_DEFAULT_COMPRESSION;
32
33
// expose all the zlib constants
34
const bkeys = Object.keys(binding);
35
for (var bk = 0; bk < bkeys.length; bk++) {
36
  var bkey = bkeys[bk];
37
  if (bkey.match(/^Z/)) {
38
    Object.defineProperty(exports, bkey, {
39
      enumerable: true, value: binding[bkey], writable: false
40
    });
41
  }
42
}
43
44
// translation table for return codes.
45
const codes = {
46
  Z_OK: binding.Z_OK,
47
  Z_STREAM_END: binding.Z_STREAM_END,
48
  Z_NEED_DICT: binding.Z_NEED_DICT,
49
  Z_ERRNO: binding.Z_ERRNO,
50
  Z_STREAM_ERROR: binding.Z_STREAM_ERROR,
51
  Z_DATA_ERROR: binding.Z_DATA_ERROR,
52
  Z_MEM_ERROR: binding.Z_MEM_ERROR,
53
  Z_BUF_ERROR: binding.Z_BUF_ERROR,
54
  Z_VERSION_ERROR: binding.Z_VERSION_ERROR
55
};
56
57
const ckeys = Object.keys(codes);
58
for (var ck = 0; ck < ckeys.length; ck++) {
59
  var ckey = ckeys[ck];
60
  codes[codes[ckey]] = ckey;
61
}
62
63
Object.defineProperty(exports, 'codes', {
64
  enumerable: true, value: Object.freeze(codes), writable: false
65
});
66
67
exports.Deflate = Deflate;
68
exports.Inflate = Inflate;
69
exports.Gzip = Gzip;
70
exports.Gunzip = Gunzip;
71
exports.DeflateRaw = DeflateRaw;
72
exports.InflateRaw = InflateRaw;
73
exports.Unzip = Unzip;
74
75
exports.createDeflate = function(o) {
76
  return new Deflate(o);
77
};
78
79
exports.createInflate = function(o) {
80
  return new Inflate(o);
81
};
82
83
exports.createDeflateRaw = function(o) {
84
  return new DeflateRaw(o);
85
};
86
87
exports.createInflateRaw = function(o) {
88
  return new InflateRaw(o);
89
};
90
91
exports.createGzip = function(o) {
92
  return new Gzip(o);
93
};
94
95
exports.createGunzip = function(o) {
96
  return new Gunzip(o);
97
};
98
99
exports.createUnzip = function(o) {
100
  return new Unzip(o);
101
};
102
103
104
// Convenience methods.
105
// compress/decompress a string or buffer in one step.
106
exports.deflate = function(buffer, opts, callback) {
107
  if (typeof opts === 'function') {
108
    callback = opts;
109
    opts = {};
110
  }
111
  return zlibBuffer(new Deflate(opts), buffer, callback);
112
};
113
114
exports.deflateSync = function(buffer, opts) {
115
  return zlibBufferSync(new Deflate(opts), buffer);
116
};
117
118
exports.gzip = function(buffer, opts, callback) {
119
  if (typeof opts === 'function') {
120
    callback = opts;
121
    opts = {};
122
  }
123
  return zlibBuffer(new Gzip(opts), buffer, callback);
124
};
125
126
exports.gzipSync = function(buffer, opts) {
127
  return zlibBufferSync(new Gzip(opts), buffer);
128
};
129
130
exports.deflateRaw = function(buffer, opts, callback) {
131
  if (typeof opts === 'function') {
132
    callback = opts;
133
    opts = {};
134
  }
135
  return zlibBuffer(new DeflateRaw(opts), buffer, callback);
136
};
137
138
exports.deflateRawSync = function(buffer, opts) {
139
  return zlibBufferSync(new DeflateRaw(opts), buffer);
140
};
141
142
exports.unzip = function(buffer, opts, callback) {
143
  if (typeof opts === 'function') {
144
    callback = opts;
145
    opts = {};
146
  }
147
  return zlibBuffer(new Unzip(opts), buffer, callback);
148
};
149
150
exports.unzipSync = function(buffer, opts) {
151
  return zlibBufferSync(new Unzip(opts), buffer);
152
};
153
154
exports.inflate = function(buffer, opts, callback) {
155
  if (typeof opts === 'function') {
156
    callback = opts;
157
    opts = {};
158
  }
159
  return zlibBuffer(new Inflate(opts), buffer, callback);
160
};
161
162
exports.inflateSync = function(buffer, opts) {
163
  return zlibBufferSync(new Inflate(opts), buffer);
164
};
165
166
exports.gunzip = function(buffer, opts, callback) {
167
  if (typeof opts === 'function') {
168
    callback = opts;
169
    opts = {};
170
  }
171
  return zlibBuffer(new Gunzip(opts), buffer, callback);
172
};
173
174
exports.gunzipSync = function(buffer, opts) {
175
  return zlibBufferSync(new Gunzip(opts), buffer);
176
};
177
178
exports.inflateRaw = function(buffer, opts, callback) {
179
  if (typeof opts === 'function') {
180
    callback = opts;
181
    opts = {};
182
  }
183
  return zlibBuffer(new InflateRaw(opts), buffer, callback);
184
};
185
186
exports.inflateRawSync = function(buffer, opts) {
187
  return zlibBufferSync(new InflateRaw(opts), buffer);
188
};
189
190
function zlibBuffer(engine, buffer, callback) {
191
  var buffers = [];
192
  var nread = 0;
193
194
  engine.on('error', onError);
195
  engine.on('end', onEnd);
196
197
  engine.end(buffer);
198
  flow();
199
200
  function flow() {
201
    var chunk;
202
    while (null !== (chunk = engine.read())) {
203
      buffers.push(chunk);
204
      nread += chunk.length;
205
    }
206
    engine.once('readable', flow);
207
  }
208
209
  function onError(err) {
210
    engine.removeListener('end', onEnd);
211
    engine.removeListener('readable', flow);
212
    callback(err);
213
  }
214
215
  function onEnd() {
216
    var buf;
217
    var err = null;
218
219
    if (nread >= kMaxLength) {
220
      err = new RangeError(kRangeErrorMessage);
221
    } else {
222
      buf = Buffer.concat(buffers, nread);
223
    }
224
225
    buffers = [];
226
    engine.close();
227
    callback(err, buf);
228
  }
229
}
230
231
function zlibBufferSync(engine, buffer) {
232
  if (typeof buffer === 'string')
233
    buffer = Buffer.from(buffer);
234
235
  if (!Buffer.isBuffer(buffer))
236
    throw new TypeError('Not a string or buffer');
237
238
  var flushFlag = engine._finishFlushFlag;
239
240
  return engine._processChunk(buffer, flushFlag);
241
}
242
243
// generic zlib
244
// minimal 2-byte header
245
function Deflate(opts) {
246
  if (!(this instanceof Deflate)) return new Deflate(opts);
247
  Zlib.call(this, opts, binding.DEFLATE);
248
}
249
250
function Inflate(opts) {
251
  if (!(this instanceof Inflate)) return new Inflate(opts);
252
  Zlib.call(this, opts, binding.INFLATE);
253
}
254
255
256
// gzip - bigger header, same deflate compression
257
function Gzip(opts) {
258
  if (!(this instanceof Gzip)) return new Gzip(opts);
259
  Zlib.call(this, opts, binding.GZIP);
260
}
261
262
function Gunzip(opts) {
263
  if (!(this instanceof Gunzip)) return new Gunzip(opts);
264
  Zlib.call(this, opts, binding.GUNZIP);
265
}
266
267
268
// raw - no header
269
function DeflateRaw(opts) {
270
  if (!(this instanceof DeflateRaw)) return new DeflateRaw(opts);
271
  Zlib.call(this, opts, binding.DEFLATERAW);
272
}
273
274
function InflateRaw(opts) {
275
  if (!(this instanceof InflateRaw)) return new InflateRaw(opts);
276
  Zlib.call(this, opts, binding.INFLATERAW);
277
}
278
279
280
// auto-detect header.
281
function Unzip(opts) {
282
  if (!(this instanceof Unzip)) return new Unzip(opts);
283
  Zlib.call(this, opts, binding.UNZIP);
284
}
285
286
function isValidFlushFlag(flag) {
287
  return flag === binding.Z_NO_FLUSH ||
288
         flag === binding.Z_PARTIAL_FLUSH ||
289
         flag === binding.Z_SYNC_FLUSH ||
290
         flag === binding.Z_FULL_FLUSH ||
291
         flag === binding.Z_FINISH ||
292
         flag === binding.Z_BLOCK;
293
}
294
295
// the Zlib class they all inherit from
296
// This thing manages the queue of requests, and returns
297
// true or false if there is anything in the queue when
298
// you call the .write() method.
299
300
function Zlib(opts, mode) {
301
  this._opts = opts = opts || {};
302
  this._chunkSize = opts.chunkSize || exports.Z_DEFAULT_CHUNK;
303
304
  Transform.call(this, opts);
305
306
  if (opts.flush && !isValidFlushFlag(opts.flush)) {
307
    throw new Error('Invalid flush flag: ' + opts.flush);
308
  }
309
  if (opts.finishFlush && !isValidFlushFlag(opts.finishFlush)) {
310
    throw new Error('Invalid flush flag: ' + opts.finishFlush);
311
  }
312
313
  this._flushFlag = opts.flush || binding.Z_NO_FLUSH;
314
  this._finishFlushFlag = typeof opts.finishFlush !== 'undefined' ?
315
    opts.finishFlush : binding.Z_FINISH;
316
317
  if (opts.chunkSize) {
318
    if (opts.chunkSize < exports.Z_MIN_CHUNK ||
319
        opts.chunkSize > exports.Z_MAX_CHUNK) {
320
      throw new Error('Invalid chunk size: ' + opts.chunkSize);
321
    }
322
  }
323
324
  if (opts.windowBits) {
325
    if (opts.windowBits < exports.Z_MIN_WINDOWBITS ||
326
        opts.windowBits > exports.Z_MAX_WINDOWBITS) {
327
      throw new Error('Invalid windowBits: ' + opts.windowBits);
328
    }
329
  }
330
331
  if (opts.level) {
332
    if (opts.level < exports.Z_MIN_LEVEL ||
333
        opts.level > exports.Z_MAX_LEVEL) {
334
      throw new Error('Invalid compression level: ' + opts.level);
335
    }
336
  }
337
338
  if (opts.memLevel) {
339
    if (opts.memLevel < exports.Z_MIN_MEMLEVEL ||
340
        opts.memLevel > exports.Z_MAX_MEMLEVEL) {
341
      throw new Error('Invalid memLevel: ' + opts.memLevel);
342
    }
343
  }
344
345
  if (opts.strategy) {
346
    if (opts.strategy != exports.Z_FILTERED &&
347
        opts.strategy != exports.Z_HUFFMAN_ONLY &&
348
        opts.strategy != exports.Z_RLE &&
349
        opts.strategy != exports.Z_FIXED &&
350
        opts.strategy != exports.Z_DEFAULT_STRATEGY) {
351
      throw new Error('Invalid strategy: ' + opts.strategy);
352
    }
353
  }
354
355
  if (opts.dictionary) {
356
    if (!Buffer.isBuffer(opts.dictionary)) {
357
      throw new Error('Invalid dictionary: it should be a Buffer instance');
358
    }
359
  }
360
361
  this._handle = new binding.Zlib(mode);
362
363
  var self = this;
364
  this._hadError = false;
365
  this._handle.onerror = function(message, errno) {
366
    // there is no way to cleanly recover.
367
    // continuing only obscures problems.
368
    _close(self);
369
    self._hadError = true;
370
371
    var error = new Error(message);
372
    error.errno = errno;
373
    error.code = exports.codes[errno];
374
    self.emit('error', error);
375
  };
376
377
  var level = exports.Z_DEFAULT_COMPRESSION;
378
  if (typeof opts.level === 'number') level = opts.level;
379
380
  var strategy = exports.Z_DEFAULT_STRATEGY;
381
  if (typeof opts.strategy === 'number') strategy = opts.strategy;
382
383
  this._handle.init(opts.windowBits || exports.Z_DEFAULT_WINDOWBITS,
384
                    level,
385
                    opts.memLevel || exports.Z_DEFAULT_MEMLEVEL,
386
                    strategy,
387
                    opts.dictionary);
388
389
  this._buffer = Buffer.allocUnsafe(this._chunkSize);
390
  this._offset = 0;
391
  this._level = level;
392
  this._strategy = strategy;
393
394
  this.once('end', this.close);
395
396
  Object.defineProperty(this, '_closed', {
397
    get: () => { return !this._handle; },
398
    configurable: true,
399
    enumerable: true
400
  });
401
}
402
403
util.inherits(Zlib, Transform);
404
405
Zlib.prototype.params = function(level, strategy, callback) {
406
  if (level < exports.Z_MIN_LEVEL ||
407
      level > exports.Z_MAX_LEVEL) {
408
    throw new RangeError('Invalid compression level: ' + level);
409
  }
410
  if (strategy != exports.Z_FILTERED &&
411
      strategy != exports.Z_HUFFMAN_ONLY &&
412
      strategy != exports.Z_RLE &&
413
      strategy != exports.Z_FIXED &&
414
      strategy != exports.Z_DEFAULT_STRATEGY) {
415
    throw new TypeError('Invalid strategy: ' + strategy);
416
  }
417
418
  if (this._level !== level || this._strategy !== strategy) {
419
    var self = this;
420
    this.flush(binding.Z_SYNC_FLUSH, function() {
421
      assert(self._handle, 'zlib binding closed');
422
      self._handle.params(level, strategy);
423
      if (!self._hadError) {
424
        self._level = level;
425
        self._strategy = strategy;
426
        if (callback) callback();
427
      }
428
    });
429
  } else {
430
    process.nextTick(callback);
431
  }
432
};
433
434
Zlib.prototype.reset = function() {
435
  assert(this._handle, 'zlib binding closed');
436
  return this._handle.reset();
437
};
438
439
// This is the _flush function called by the transform class,
440
// internally, when the last chunk has been written.
441
Zlib.prototype._flush = function(callback) {
442
  this._transform(Buffer.alloc(0), '', callback);
443
};
444
445
Zlib.prototype.flush = function(kind, callback) {
446
  var ws = this._writableState;
447
448
  if (typeof kind === 'function' || (kind === undefined && !callback)) {
449
    callback = kind;
450
    kind = binding.Z_FULL_FLUSH;
451
  }
452
453
  if (ws.ended) {
454
    if (callback)
455
      process.nextTick(callback);
456
  } else if (ws.ending) {
457
    if (callback)
458
      this.once('end', callback);
459
  } else if (ws.needDrain) {
460
    if (callback) {
461
      this.once('drain', () => this.flush(kind, callback));
462
    }
463
  } else {
464
    this._flushFlag = kind;
465
    this.write(Buffer.alloc(0), '', callback);
466
  }
467
};
468
469
Zlib.prototype.close = function(callback) {
470
  _close(this, callback);
471
  process.nextTick(emitCloseNT, this);
472
};
473
474
function _close(engine, callback) {
475
  if (callback)
476
    process.nextTick(callback);
477
478
  // Caller may invoke .close after a zlib error (which will null _handle).
479
  if (!engine._handle)
480
    return;
481
482
  engine._handle.close();
483
  engine._handle = null;
484
}
485
486
function emitCloseNT(self) {
487
  self.emit('close');
488
}
489
490
Zlib.prototype._transform = function(chunk, encoding, cb) {
491
  var flushFlag;
492
  var ws = this._writableState;
493
  var ending = ws.ending || ws.ended;
494
  var last = ending && (!chunk || ws.length === chunk.length);
495
496
  if (chunk !== null && !Buffer.isBuffer(chunk))
497
    return cb(new Error('invalid input'));
498
499
  if (!this._handle)
500
    return cb(new Error('zlib binding closed'));
501
502
  // If it's the last chunk, or a final flush, we use the Z_FINISH flush flag
503
  // (or whatever flag was provided using opts.finishFlush).
504
  // If it's explicitly flushing at some other time, then we use
505
  // Z_FULL_FLUSH. Otherwise, use Z_NO_FLUSH for maximum compression
506
  // goodness.
507
  if (last)
508
    flushFlag = this._finishFlushFlag;
509
  else {
510
    flushFlag = this._flushFlag;
511
    // once we've flushed the last of the queue, stop flushing and
512
    // go back to the normal behavior.
513
    if (chunk.length >= ws.length) {
514
      this._flushFlag = this._opts.flush || binding.Z_NO_FLUSH;
515
    }
516
  }
517
518
  this._processChunk(chunk, flushFlag, cb);
519
};
520
521
Zlib.prototype._processChunk = function(chunk, flushFlag, cb) {
522
  var availInBefore = chunk && chunk.length;
523
  var availOutBefore = this._chunkSize - this._offset;
524
  var inOff = 0;
525
526
  var self = this;
527
528
  var async = typeof cb === 'function';
529
530
  if (!async) {
531
    var buffers = [];
532
    var nread = 0;
533
534
    var error;
535
    this.on('error', function(er) {
536
      error = er;
537
    });
538
539
    assert(this._handle, 'zlib binding closed');
540
    do {
541
      var res = this._handle.writeSync(flushFlag,
542
                                       chunk, // in
543
                                       inOff, // in_off
544
                                       availInBefore, // in_len
545
                                       this._buffer, // out
546
                                       this._offset, //out_off
547
                                       availOutBefore); // out_len
548
    } while (!this._hadError && callback(res[0], res[1]));
549
550
    if (this._hadError) {
551
      throw error;
552
    }
553
554
    if (nread >= kMaxLength) {
555
      _close(this);
556
      throw new RangeError(kRangeErrorMessage);
557
    }
558
559
    var buf = Buffer.concat(buffers, nread);
560
    _close(this);
561
562
    return buf;
563
  }
564
565
  assert(this._handle, 'zlib binding closed');
566
  var req = this._handle.write(flushFlag,
567
                               chunk, // in
568
                               inOff, // in_off
569
                               availInBefore, // in_len
570
                               this._buffer, // out
571
                               this._offset, //out_off
572
                               availOutBefore); // out_len
573
574
  req.buffer = chunk;
575
  req.callback = callback;
576
577
  function callback(availInAfter, availOutAfter) {
578
    // When the callback is used in an async write, the callback's
579
    // context is the `req` object that was created. The req object
580
    // is === this._handle, and that's why it's important to null
581
    // out the values after they are done being used. `this._handle`
582
    // can stay in memory longer than the callback and buffer are needed.
583
    if (this) {
584
      this.buffer = null;
585
      this.callback = null;
586
    }
587
588
    if (self._hadError)
589
      return;
590
591
    var have = availOutBefore - availOutAfter;
592
    assert(have >= 0, 'have should not go down');
593
594
    if (have > 0) {
595
      var out = self._buffer.slice(self._offset, self._offset + have);
596
      self._offset += have;
597
      // serve some output to the consumer.
598
      if (async) {
599
        self.push(out);
600
      } else {
601
        buffers.push(out);
602
        nread += out.length;
603
      }
604
    }
605
606
    // exhausted the output buffer, or used all the input create a new one.
607
    if (availOutAfter === 0 || self._offset >= self._chunkSize) {
608
      availOutBefore = self._chunkSize;
609
      self._offset = 0;
610
      self._buffer = Buffer.allocUnsafe(self._chunkSize);
611
    }
612
613
    if (availOutAfter === 0) {
614
      // Not actually done.  Need to reprocess.
615
      // Also, update the availInBefore to the availInAfter value,
616
      // so that if we have to hit it a third (fourth, etc.) time,
617
      // it'll have the correct byte counts.
618
      inOff += (availInBefore - availInAfter);
619
      availInBefore = availInAfter;
620
621
      if (!async)
622
        return true;
623
624
      var newReq = self._handle.write(flushFlag,
625
                                      chunk,
626
                                      inOff,
627
                                      availInBefore,
628
                                      self._buffer,
629
                                      self._offset,
630
                                      self._chunkSize);
631
      newReq.callback = callback; // this same function
632
      newReq.buffer = chunk;
633
      return;
634
    }
635
636
    if (!async)
637
      return false;
638
639
    // finished with the chunk.
640
    cb();
641
  }
642
};
643
644
util.inherits(Deflate, Zlib);
645
util.inherits(Inflate, Zlib);
646
util.inherits(Gzip, Zlib);
647
util.inherits(Gunzip, Zlib);
648
util.inherits(DeflateRaw, Zlib);
649
util.inherits(InflateRaw, Zlib);
650
util.inherits(Unzip, Zlib);