1
0
mirror of https://github.com/S2-/minifyfromhtml.git synced 2025-08-03 04:10:04 +02:00

update node modules

This commit is contained in:
s2
2019-03-29 15:56:41 +01:00
parent f114871153
commit 89c32fb4e6
8347 changed files with 390123 additions and 159877 deletions

View File

@@ -1,23 +1,19 @@
'use strict';
const safeBuffer = require('safe-buffer');
const Limiter = require('async-limiter');
const zlib = require('zlib');
const bufferUtil = require('./buffer-util');
const Buffer = safeBuffer.Buffer;
const { kStatusCode, NOOP } = require('./constants');
const TRAILER = Buffer.from([0x00, 0x00, 0xff, 0xff]);
const EMPTY_BLOCK = Buffer.from([0x00]);
const kWriteInProgress = Symbol('write-in-progress');
const kPendingClose = Symbol('pending-close');
const kPerMessageDeflate = Symbol('permessage-deflate');
const kTotalLength = Symbol('total-length');
const kCallback = Symbol('callback');
const kBuffers = Symbol('buffers');
const kError = Symbol('error');
const kOwner = Symbol('owner');
//
// We limit zlib concurrency, which prevents severe memory fragmentation
@@ -54,12 +50,11 @@ class PerMessageDeflate {
* mode
* @param {Number} maxPayload The maximum allowed message length
*/
constructor (options, isServer, maxPayload) {
constructor(options, isServer, maxPayload) {
this._maxPayload = maxPayload | 0;
this._options = options || {};
this._threshold = this._options.threshold !== undefined
? this._options.threshold
: 1024;
this._threshold =
this._options.threshold !== undefined ? this._options.threshold : 1024;
this._isServer = !!isServer;
this._deflate = null;
this._inflate = null;
@@ -67,9 +62,10 @@ class PerMessageDeflate {
this.params = null;
if (!zlibLimiter) {
const concurrency = this._options.concurrencyLimit !== undefined
? this._options.concurrencyLimit
: 10;
const concurrency =
this._options.concurrencyLimit !== undefined
? this._options.concurrencyLimit
: 10;
zlibLimiter = new Limiter({ concurrency });
}
}
@@ -77,7 +73,7 @@ class PerMessageDeflate {
/**
* @type {String}
*/
static get extensionName () {
static get extensionName() {
return 'permessage-deflate';
}
@@ -87,7 +83,7 @@ class PerMessageDeflate {
* @return {Object} Extension parameters
* @public
*/
offer () {
offer() {
const params = {};
if (this._options.serverNoContextTakeover) {
@@ -115,7 +111,7 @@ class PerMessageDeflate {
* @return {Object} Accepted configuration
* @public
*/
accept (configurations) {
accept(configurations) {
configurations = this.normalizeParams(configurations);
this.params = this._isServer
@@ -130,22 +126,15 @@ class PerMessageDeflate {
*
* @public
*/
cleanup () {
cleanup() {
if (this._inflate) {
if (this._inflate[kWriteInProgress]) {
this._inflate[kPendingClose] = true;
} else {
this._inflate.close();
this._inflate = null;
}
this._inflate.close();
this._inflate = null;
}
if (this._deflate) {
if (this._deflate[kWriteInProgress]) {
this._deflate[kPendingClose] = true;
} else {
this._deflate.close();
this._deflate = null;
}
this._deflate.close();
this._deflate = null;
}
}
@@ -156,7 +145,7 @@ class PerMessageDeflate {
* @return {Object} Accepted configuration
* @private
*/
acceptAsServer (offers) {
acceptAsServer(offers) {
const opts = this._options;
const accepted = offers.find((params) => {
if (
@@ -207,7 +196,7 @@ class PerMessageDeflate {
* @return {Object} Accepted configuration
* @private
*/
acceptAsClient (response) {
acceptAsClient(response) {
const params = response[0];
if (
@@ -241,7 +230,7 @@ class PerMessageDeflate {
* @return {Array} The offers/response with normalized parameters
* @private
*/
normalizeParams (configurations) {
normalizeParams(configurations) {
configurations.forEach((params) => {
Object.keys(params).forEach((key) => {
var value = params[key];
@@ -302,7 +291,7 @@ class PerMessageDeflate {
* @param {Function} callback Callback
* @public
*/
decompress (data, fin, callback) {
decompress(data, fin, callback) {
zlibLimiter.push((done) => {
this._decompress(data, fin, (err, result) => {
done();
@@ -319,7 +308,7 @@ class PerMessageDeflate {
* @param {Function} callback Callback
* @public
*/
compress (data, fin, callback) {
compress(data, fin, callback) {
zlibLimiter.push((done) => {
this._compress(data, fin, (err, result) => {
done();
@@ -336,31 +325,27 @@ class PerMessageDeflate {
* @param {Function} callback Callback
* @private
*/
_decompress (data, fin, callback) {
_decompress(data, fin, callback) {
const endpoint = this._isServer ? 'client' : 'server';
if (!this._inflate) {
const key = `${endpoint}_max_window_bits`;
const windowBits = typeof this.params[key] !== 'number'
? zlib.Z_DEFAULT_WINDOWBITS
: this.params[key];
const windowBits =
typeof this.params[key] !== 'number'
? zlib.Z_DEFAULT_WINDOWBITS
: this.params[key];
this._inflate = zlib.createInflateRaw(
Object.assign(
{},
this._options.zlibInflateOptions,
{ windowBits }
)
Object.assign({}, this._options.zlibInflateOptions, { windowBits })
);
this._inflate[kPerMessageDeflate] = this;
this._inflate[kTotalLength] = 0;
this._inflate[kBuffers] = [];
this._inflate[kOwner] = this;
this._inflate.on('error', inflateOnError);
this._inflate.on('data', inflateOnData);
}
this._inflate[kCallback] = callback;
this._inflate[kWriteInProgress] = true;
this._inflate.write(data);
if (fin) this._inflate.write(TRAILER);
@@ -380,14 +365,10 @@ class PerMessageDeflate {
this._inflate[kTotalLength]
);
if (
(fin && this.params[`${endpoint}_no_context_takeover`]) ||
this._inflate[kPendingClose]
) {
if (fin && this.params[`${endpoint}_no_context_takeover`]) {
this._inflate.close();
this._inflate = null;
} else {
this._inflate[kWriteInProgress] = false;
this._inflate[kTotalLength] = 0;
this._inflate[kBuffers] = [];
}
@@ -404,7 +385,7 @@ class PerMessageDeflate {
* @param {Function} callback Callback
* @private
*/
_compress (data, fin, callback) {
_compress(data, fin, callback) {
if (!data || data.length === 0) {
process.nextTick(callback, null, EMPTY_BLOCK);
return;
@@ -414,37 +395,40 @@ class PerMessageDeflate {
if (!this._deflate) {
const key = `${endpoint}_max_window_bits`;
const windowBits = typeof this.params[key] !== 'number'
? zlib.Z_DEFAULT_WINDOWBITS
: this.params[key];
const windowBits =
typeof this.params[key] !== 'number'
? zlib.Z_DEFAULT_WINDOWBITS
: this.params[key];
this._deflate = zlib.createDeflateRaw(
Object.assign(
// TODO deprecate memLevel/level and recommend zlibDeflateOptions instead
{
memLevel: this._options.memLevel,
level: this._options.level
},
this._options.zlibDeflateOptions,
{ windowBits }
)
Object.assign({}, this._options.zlibDeflateOptions, { windowBits })
);
this._deflate[kTotalLength] = 0;
this._deflate[kBuffers] = [];
//
// `zlib.DeflateRaw` emits an `'error'` event only when an attempt to use
// it is made after it has already been closed. This cannot happen here,
// so we only add a listener for the `'data'` event.
// An `'error'` event is emitted, only on Node.js < 10.0.0, if the
// `zlib.DeflateRaw` instance is closed while data is being processed.
// This can happen if `PerMessageDeflate#cleanup()` is called at the wrong
// time due to an abnormal WebSocket closure.
//
this._deflate.on('error', NOOP);
this._deflate.on('data', deflateOnData);
}
this._deflate[kWriteInProgress] = true;
this._deflate.write(data);
this._deflate.flush(zlib.Z_SYNC_FLUSH, () => {
if (!this._deflate) {
//
// This `if` statement is only needed for Node.js < 10.0.0 because as of
// commit https://github.com/nodejs/node/commit/5e3f5164, the flush
// callback is no longer called if the deflate stream is closed while
// data is being processed.
//
return;
}
var data = bufferUtil.concat(
this._deflate[kBuffers],
this._deflate[kTotalLength]
@@ -452,14 +436,10 @@ class PerMessageDeflate {
if (fin) data = data.slice(0, data.length - 4);
if (
(fin && this.params[`${endpoint}_no_context_takeover`]) ||
this._deflate[kPendingClose]
) {
if (fin && this.params[`${endpoint}_no_context_takeover`]) {
this._deflate.close();
this._deflate = null;
} else {
this._deflate[kWriteInProgress] = false;
this._deflate[kTotalLength] = 0;
this._deflate[kBuffers] = [];
}
@@ -477,7 +457,7 @@ module.exports = PerMessageDeflate;
* @param {Buffer} chunk A chunk of data
* @private
*/
function deflateOnData (chunk) {
function deflateOnData(chunk) {
this[kBuffers].push(chunk);
this[kTotalLength] += chunk.length;
}
@@ -488,19 +468,19 @@ function deflateOnData (chunk) {
* @param {Buffer} chunk A chunk of data
* @private
*/
function inflateOnData (chunk) {
function inflateOnData(chunk) {
this[kTotalLength] += chunk.length;
if (
this[kOwner]._maxPayload < 1 ||
this[kTotalLength] <= this[kOwner]._maxPayload
this[kPerMessageDeflate]._maxPayload < 1 ||
this[kTotalLength] <= this[kPerMessageDeflate]._maxPayload
) {
this[kBuffers].push(chunk);
return;
}
this[kError] = new RangeError('Max payload size exceeded');
this[kError].closeCode = 1009;
this[kError][kStatusCode] = 1009;
this.removeListener('data', inflateOnData);
this.reset();
}
@@ -511,11 +491,12 @@ function inflateOnData (chunk) {
* @param {Error} err The emitted error
* @private
*/
function inflateOnError (err) {
function inflateOnError(err) {
//
// There is no need to call `Zlib#close()` as the handle is automatically
// closed when an error is emitted.
//
this[kOwner]._inflate = null;
this[kPerMessageDeflate]._inflate = null;
err[kStatusCode] = 1007;
this[kCallback](err);
}