1 | 'use strict';
|
2 |
|
3 | const Limiter = require('async-limiter');
|
4 | const zlib = require('zlib');
|
5 |
|
6 | const bufferUtil = require('./buffer-util');
|
7 | const constants = require('./constants');
|
8 |
|
9 | const TRAILER = Buffer.from([0x00, 0x00, 0xff, 0xff]);
|
10 | const EMPTY_BLOCK = Buffer.from([0x00]);
|
11 |
|
12 | const kPerMessageDeflate = Symbol('permessage-deflate');
|
13 | const kWriteInProgress = Symbol('write-in-progress');
|
14 | const kPendingClose = Symbol('pending-close');
|
15 | const kTotalLength = Symbol('total-length');
|
16 | const kCallback = Symbol('callback');
|
17 | const kBuffers = Symbol('buffers');
|
18 | const kError = Symbol('error');
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | let zlibLimiter;
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | class PerMessageDeflate {
|
33 | |
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | constructor (options, isServer, maxPayload) {
|
56 | this._maxPayload = maxPayload | 0;
|
57 | this._options = options || {};
|
58 | this._threshold = this._options.threshold !== undefined
|
59 | ? this._options.threshold
|
60 | : 1024;
|
61 | this._isServer = !!isServer;
|
62 | this._deflate = null;
|
63 | this._inflate = null;
|
64 |
|
65 | this.params = null;
|
66 |
|
67 | if (!zlibLimiter) {
|
68 | const concurrency = this._options.concurrencyLimit !== undefined
|
69 | ? this._options.concurrencyLimit
|
70 | : 10;
|
71 | zlibLimiter = new Limiter({ concurrency });
|
72 | }
|
73 | }
|
74 |
|
75 | |
76 |
|
77 |
|
78 | static get extensionName () {
|
79 | return 'permessage-deflate';
|
80 | }
|
81 |
|
82 | |
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | offer () {
|
89 | const params = {};
|
90 |
|
91 | if (this._options.serverNoContextTakeover) {
|
92 | params.server_no_context_takeover = true;
|
93 | }
|
94 | if (this._options.clientNoContextTakeover) {
|
95 | params.client_no_context_takeover = true;
|
96 | }
|
97 | if (this._options.serverMaxWindowBits) {
|
98 | params.server_max_window_bits = this._options.serverMaxWindowBits;
|
99 | }
|
100 | if (this._options.clientMaxWindowBits) {
|
101 | params.client_max_window_bits = this._options.clientMaxWindowBits;
|
102 | } else if (this._options.clientMaxWindowBits == null) {
|
103 | params.client_max_window_bits = true;
|
104 | }
|
105 |
|
106 | return params;
|
107 | }
|
108 |
|
109 | |
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | accept (configurations) {
|
117 | configurations = this.normalizeParams(configurations);
|
118 |
|
119 | this.params = this._isServer
|
120 | ? this.acceptAsServer(configurations)
|
121 | : this.acceptAsClient(configurations);
|
122 |
|
123 | return this.params;
|
124 | }
|
125 |
|
126 | |
127 |
|
128 |
|
129 |
|
130 |
|
131 | cleanup () {
|
132 | if (this._inflate) {
|
133 | if (this._inflate[kWriteInProgress]) {
|
134 | this._inflate[kPendingClose] = true;
|
135 | } else {
|
136 | this._inflate.close();
|
137 | this._inflate = null;
|
138 | }
|
139 | }
|
140 | if (this._deflate) {
|
141 | if (this._deflate[kWriteInProgress]) {
|
142 | this._deflate[kPendingClose] = true;
|
143 | } else {
|
144 | this._deflate.close();
|
145 | this._deflate = null;
|
146 | }
|
147 | }
|
148 | }
|
149 |
|
150 | |
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | acceptAsServer (offers) {
|
158 | const opts = this._options;
|
159 | const accepted = offers.find((params) => {
|
160 | if (
|
161 | (opts.serverNoContextTakeover === false &&
|
162 | params.server_no_context_takeover) ||
|
163 | (params.server_max_window_bits &&
|
164 | (opts.serverMaxWindowBits === false ||
|
165 | (typeof opts.serverMaxWindowBits === 'number' &&
|
166 | opts.serverMaxWindowBits > params.server_max_window_bits))) ||
|
167 | (typeof opts.clientMaxWindowBits === 'number' &&
|
168 | !params.client_max_window_bits)
|
169 | ) {
|
170 | return false;
|
171 | }
|
172 |
|
173 | return true;
|
174 | });
|
175 |
|
176 | if (!accepted) {
|
177 | throw new Error('None of the extension offers can be accepted');
|
178 | }
|
179 |
|
180 | if (opts.serverNoContextTakeover) {
|
181 | accepted.server_no_context_takeover = true;
|
182 | }
|
183 | if (opts.clientNoContextTakeover) {
|
184 | accepted.client_no_context_takeover = true;
|
185 | }
|
186 | if (typeof opts.serverMaxWindowBits === 'number') {
|
187 | accepted.server_max_window_bits = opts.serverMaxWindowBits;
|
188 | }
|
189 | if (typeof opts.clientMaxWindowBits === 'number') {
|
190 | accepted.client_max_window_bits = opts.clientMaxWindowBits;
|
191 | } else if (
|
192 | accepted.client_max_window_bits === true ||
|
193 | opts.clientMaxWindowBits === false
|
194 | ) {
|
195 | delete accepted.client_max_window_bits;
|
196 | }
|
197 |
|
198 | return accepted;
|
199 | }
|
200 |
|
201 | |
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 | acceptAsClient (response) {
|
209 | const params = response[0];
|
210 |
|
211 | if (
|
212 | this._options.clientNoContextTakeover === false &&
|
213 | params.client_no_context_takeover
|
214 | ) {
|
215 | throw new Error('Unexpected parameter "client_no_context_takeover"');
|
216 | }
|
217 |
|
218 | if (!params.client_max_window_bits) {
|
219 | if (typeof this._options.clientMaxWindowBits === 'number') {
|
220 | params.client_max_window_bits = this._options.clientMaxWindowBits;
|
221 | }
|
222 | } else if (
|
223 | this._options.clientMaxWindowBits === false ||
|
224 | (typeof this._options.clientMaxWindowBits === 'number' &&
|
225 | params.client_max_window_bits > this._options.clientMaxWindowBits)
|
226 | ) {
|
227 | throw new Error(
|
228 | 'Unexpected or invalid parameter "client_max_window_bits"'
|
229 | );
|
230 | }
|
231 |
|
232 | return params;
|
233 | }
|
234 |
|
235 | |
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 | normalizeParams (configurations) {
|
243 | configurations.forEach((params) => {
|
244 | Object.keys(params).forEach((key) => {
|
245 | var value = params[key];
|
246 |
|
247 | if (value.length > 1) {
|
248 | throw new Error(`Parameter "${key}" must have only a single value`);
|
249 | }
|
250 |
|
251 | value = value[0];
|
252 |
|
253 | if (key === 'client_max_window_bits') {
|
254 | if (value !== true) {
|
255 | const num = +value;
|
256 | if (!Number.isInteger(num) || num < 8 || num > 15) {
|
257 | throw new TypeError(
|
258 | `Invalid value for parameter "${key}": ${value}`
|
259 | );
|
260 | }
|
261 | value = num;
|
262 | } else if (!this._isServer) {
|
263 | throw new TypeError(
|
264 | `Invalid value for parameter "${key}": ${value}`
|
265 | );
|
266 | }
|
267 | } else if (key === 'server_max_window_bits') {
|
268 | const num = +value;
|
269 | if (!Number.isInteger(num) || num < 8 || num > 15) {
|
270 | throw new TypeError(
|
271 | `Invalid value for parameter "${key}": ${value}`
|
272 | );
|
273 | }
|
274 | value = num;
|
275 | } else if (
|
276 | key === 'client_no_context_takeover' ||
|
277 | key === 'server_no_context_takeover'
|
278 | ) {
|
279 | if (value !== true) {
|
280 | throw new TypeError(
|
281 | `Invalid value for parameter "${key}": ${value}`
|
282 | );
|
283 | }
|
284 | } else {
|
285 | throw new Error(`Unknown parameter "${key}"`);
|
286 | }
|
287 |
|
288 | params[key] = value;
|
289 | });
|
290 | });
|
291 |
|
292 | return configurations;
|
293 | }
|
294 |
|
295 | |
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 | decompress (data, fin, callback) {
|
304 | zlibLimiter.push((done) => {
|
305 | this._decompress(data, fin, (err, result) => {
|
306 | done();
|
307 | callback(err, result);
|
308 | });
|
309 | });
|
310 | }
|
311 |
|
312 | |
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 | compress (data, fin, callback) {
|
321 | zlibLimiter.push((done) => {
|
322 | this._compress(data, fin, (err, result) => {
|
323 | done();
|
324 | callback(err, result);
|
325 | });
|
326 | });
|
327 | }
|
328 |
|
329 | |
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 | _decompress (data, fin, callback) {
|
338 | const endpoint = this._isServer ? 'client' : 'server';
|
339 |
|
340 | if (!this._inflate) {
|
341 | const key = `${endpoint}_max_window_bits`;
|
342 | const windowBits = typeof this.params[key] !== 'number'
|
343 | ? zlib.Z_DEFAULT_WINDOWBITS
|
344 | : this.params[key];
|
345 |
|
346 | this._inflate = zlib.createInflateRaw(
|
347 | Object.assign({}, this._options.zlibInflateOptions, { windowBits })
|
348 | );
|
349 | this._inflate[kPerMessageDeflate] = this;
|
350 | this._inflate[kTotalLength] = 0;
|
351 | this._inflate[kBuffers] = [];
|
352 | this._inflate.on('error', inflateOnError);
|
353 | this._inflate.on('data', inflateOnData);
|
354 | }
|
355 |
|
356 | this._inflate[kCallback] = callback;
|
357 | this._inflate[kWriteInProgress] = true;
|
358 |
|
359 | this._inflate.write(data);
|
360 | if (fin) this._inflate.write(TRAILER);
|
361 |
|
362 | this._inflate.flush(() => {
|
363 | const err = this._inflate[kError];
|
364 |
|
365 | if (err) {
|
366 | this._inflate.close();
|
367 | this._inflate = null;
|
368 | callback(err);
|
369 | return;
|
370 | }
|
371 |
|
372 | const data = bufferUtil.concat(
|
373 | this._inflate[kBuffers],
|
374 | this._inflate[kTotalLength]
|
375 | );
|
376 |
|
377 | if (
|
378 | (fin && this.params[`${endpoint}_no_context_takeover`]) ||
|
379 | this._inflate[kPendingClose]
|
380 | ) {
|
381 | this._inflate.close();
|
382 | this._inflate = null;
|
383 | } else {
|
384 | this._inflate[kWriteInProgress] = false;
|
385 | this._inflate[kTotalLength] = 0;
|
386 | this._inflate[kBuffers] = [];
|
387 | }
|
388 |
|
389 | callback(null, data);
|
390 | });
|
391 | }
|
392 |
|
393 | |
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 |
|
401 | _compress (data, fin, callback) {
|
402 | if (!data || data.length === 0) {
|
403 | process.nextTick(callback, null, EMPTY_BLOCK);
|
404 | return;
|
405 | }
|
406 |
|
407 | const endpoint = this._isServer ? 'server' : 'client';
|
408 |
|
409 | if (!this._deflate) {
|
410 | const key = `${endpoint}_max_window_bits`;
|
411 | const windowBits = typeof this.params[key] !== 'number'
|
412 | ? zlib.Z_DEFAULT_WINDOWBITS
|
413 | : this.params[key];
|
414 |
|
415 | this._deflate = zlib.createDeflateRaw(
|
416 | Object.assign({}, this._options.zlibDeflateOptions, { windowBits })
|
417 | );
|
418 |
|
419 | this._deflate[kTotalLength] = 0;
|
420 | this._deflate[kBuffers] = [];
|
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 | this._deflate.on('data', deflateOnData);
|
428 | }
|
429 |
|
430 | this._deflate[kWriteInProgress] = true;
|
431 |
|
432 | this._deflate.write(data);
|
433 | this._deflate.flush(zlib.Z_SYNC_FLUSH, () => {
|
434 | var data = bufferUtil.concat(
|
435 | this._deflate[kBuffers],
|
436 | this._deflate[kTotalLength]
|
437 | );
|
438 |
|
439 | if (fin) data = data.slice(0, data.length - 4);
|
440 |
|
441 | if (
|
442 | (fin && this.params[`${endpoint}_no_context_takeover`]) ||
|
443 | this._deflate[kPendingClose]
|
444 | ) {
|
445 | this._deflate.close();
|
446 | this._deflate = null;
|
447 | } else {
|
448 | this._deflate[kWriteInProgress] = false;
|
449 | this._deflate[kTotalLength] = 0;
|
450 | this._deflate[kBuffers] = [];
|
451 | }
|
452 |
|
453 | callback(null, data);
|
454 | });
|
455 | }
|
456 | }
|
457 |
|
458 | module.exports = PerMessageDeflate;
|
459 |
|
460 |
|
461 |
|
462 |
|
463 |
|
464 |
|
465 |
|
466 | function deflateOnData (chunk) {
|
467 | this[kBuffers].push(chunk);
|
468 | this[kTotalLength] += chunk.length;
|
469 | }
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 |
|
477 | function inflateOnData (chunk) {
|
478 | this[kTotalLength] += chunk.length;
|
479 |
|
480 | if (
|
481 | this[kPerMessageDeflate]._maxPayload < 1 ||
|
482 | this[kTotalLength] <= this[kPerMessageDeflate]._maxPayload
|
483 | ) {
|
484 | this[kBuffers].push(chunk);
|
485 | return;
|
486 | }
|
487 |
|
488 | this[kError] = new RangeError('Max payload size exceeded');
|
489 | this[kError][constants.kStatusCode] = 1009;
|
490 | this.removeListener('data', inflateOnData);
|
491 | this.reset();
|
492 | }
|
493 |
|
494 |
|
495 |
|
496 |
|
497 |
|
498 |
|
499 |
|
500 | function inflateOnError (err) {
|
501 |
|
502 |
|
503 |
|
504 |
|
505 | this[kPerMessageDeflate]._inflate = null;
|
506 | err[constants.kStatusCode] = 1007;
|
507 | this[kCallback](err);
|
508 | }
|