permessage-deflate.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. 'use strict';
  2. const zlib = require('zlib');
  3. const bufferUtil = require('./buffer-util');
  4. const Limiter = require('./limiter');
  5. const { kStatusCode } = require('./constants');
  6. const FastBuffer = Buffer[Symbol.species];
  7. const TRAILER = Buffer.from([0x00, 0x00, 0xff, 0xff]);
  8. const kPerMessageDeflate = Symbol('permessage-deflate');
  9. const kTotalLength = Symbol('total-length');
  10. const kCallback = Symbol('callback');
  11. const kBuffers = Symbol('buffers');
  12. const kError = Symbol('error');
  13. //
  14. // We limit zlib concurrency, which prevents severe memory fragmentation
  15. // as documented in https://github.com/nodejs/node/issues/8871#issuecomment-250915913
  16. // and https://github.com/websockets/ws/issues/1202
  17. //
  18. // Intentionally global; it's the global thread pool that's an issue.
  19. //
  20. let zlibLimiter;
  21. /**
  22. * permessage-deflate implementation.
  23. */
  24. class PerMessageDeflate {
  25. /**
  26. * Creates a PerMessageDeflate instance.
  27. *
  28. * @param {Object} [options] Configuration options
  29. * @param {(Boolean|Number)} [options.clientMaxWindowBits] Advertise support
  30. * for, or request, a custom client window size
  31. * @param {Boolean} [options.clientNoContextTakeover=false] Advertise/
  32. * acknowledge disabling of client context takeover
  33. * @param {Number} [options.concurrencyLimit=10] The number of concurrent
  34. * calls to zlib
  35. * @param {Boolean} [options.isServer=false] Create the instance in either
  36. * server or client mode
  37. * @param {Number} [options.maxPayload=0] The maximum allowed message length
  38. * @param {(Boolean|Number)} [options.serverMaxWindowBits] Request/confirm the
  39. * use of a custom server window size
  40. * @param {Boolean} [options.serverNoContextTakeover=false] Request/accept
  41. * disabling of server context takeover
  42. * @param {Number} [options.threshold=1024] Size (in bytes) below which
  43. * messages should not be compressed if context takeover is disabled
  44. * @param {Object} [options.zlibDeflateOptions] Options to pass to zlib on
  45. * deflate
  46. * @param {Object} [options.zlibInflateOptions] Options to pass to zlib on
  47. * inflate
  48. */
  49. constructor(options) {
  50. this._options = options || {};
  51. this._threshold =
  52. this._options.threshold !== undefined ? this._options.threshold : 1024;
  53. this._maxPayload = this._options.maxPayload | 0;
  54. this._isServer = !!this._options.isServer;
  55. this._deflate = null;
  56. this._inflate = null;
  57. this.params = null;
  58. if (!zlibLimiter) {
  59. const concurrency =
  60. this._options.concurrencyLimit !== undefined
  61. ? this._options.concurrencyLimit
  62. : 10;
  63. zlibLimiter = new Limiter(concurrency);
  64. }
  65. }
  66. /**
  67. * @type {String}
  68. */
  69. static get extensionName() {
  70. return 'permessage-deflate';
  71. }
  72. /**
  73. * Create an extension negotiation offer.
  74. *
  75. * @return {Object} Extension parameters
  76. * @public
  77. */
  78. offer() {
  79. const params = {};
  80. if (this._options.serverNoContextTakeover) {
  81. params.server_no_context_takeover = true;
  82. }
  83. if (this._options.clientNoContextTakeover) {
  84. params.client_no_context_takeover = true;
  85. }
  86. if (this._options.serverMaxWindowBits) {
  87. params.server_max_window_bits = this._options.serverMaxWindowBits;
  88. }
  89. if (this._options.clientMaxWindowBits) {
  90. params.client_max_window_bits = this._options.clientMaxWindowBits;
  91. } else if (this._options.clientMaxWindowBits == null) {
  92. params.client_max_window_bits = true;
  93. }
  94. return params;
  95. }
  96. /**
  97. * Accept an extension negotiation offer/response.
  98. *
  99. * @param {Array} configurations The extension negotiation offers/reponse
  100. * @return {Object} Accepted configuration
  101. * @public
  102. */
  103. accept(configurations) {
  104. configurations = this.normalizeParams(configurations);
  105. this.params = this._isServer
  106. ? this.acceptAsServer(configurations)
  107. : this.acceptAsClient(configurations);
  108. return this.params;
  109. }
  110. /**
  111. * Releases all resources used by the extension.
  112. *
  113. * @public
  114. */
  115. cleanup() {
  116. if (this._inflate) {
  117. this._inflate.close();
  118. this._inflate = null;
  119. }
  120. if (this._deflate) {
  121. const callback = this._deflate[kCallback];
  122. this._deflate.close();
  123. this._deflate = null;
  124. if (callback) {
  125. callback(
  126. new Error(
  127. 'The deflate stream was closed while data was being processed'
  128. )
  129. );
  130. }
  131. }
  132. }
  133. /**
  134. * Accept an extension negotiation offer.
  135. *
  136. * @param {Array} offers The extension negotiation offers
  137. * @return {Object} Accepted configuration
  138. * @private
  139. */
  140. acceptAsServer(offers) {
  141. const opts = this._options;
  142. const accepted = offers.find((params) => {
  143. if (
  144. (opts.serverNoContextTakeover === false &&
  145. params.server_no_context_takeover) ||
  146. (params.server_max_window_bits &&
  147. (opts.serverMaxWindowBits === false ||
  148. (typeof opts.serverMaxWindowBits === 'number' &&
  149. opts.serverMaxWindowBits > params.server_max_window_bits))) ||
  150. (typeof opts.clientMaxWindowBits === 'number' &&
  151. (typeof params.client_max_window_bits === 'number'
  152. ? opts.clientMaxWindowBits > params.client_max_window_bits
  153. : !params.client_max_window_bits))
  154. ) {
  155. return false;
  156. }
  157. return true;
  158. });
  159. if (!accepted) {
  160. throw new Error('None of the extension offers can be accepted');
  161. }
  162. if (opts.serverNoContextTakeover) {
  163. accepted.server_no_context_takeover = true;
  164. }
  165. if (opts.clientNoContextTakeover) {
  166. accepted.client_no_context_takeover = true;
  167. }
  168. if (typeof opts.serverMaxWindowBits === 'number') {
  169. accepted.server_max_window_bits = opts.serverMaxWindowBits;
  170. }
  171. if (typeof opts.clientMaxWindowBits === 'number') {
  172. accepted.client_max_window_bits = opts.clientMaxWindowBits;
  173. } else if (
  174. accepted.client_max_window_bits === true ||
  175. opts.clientMaxWindowBits === false
  176. ) {
  177. delete accepted.client_max_window_bits;
  178. }
  179. return accepted;
  180. }
  181. /**
  182. * Accept the extension negotiation response.
  183. *
  184. * @param {Array} response The extension negotiation response
  185. * @return {Object} Accepted configuration
  186. * @private
  187. */
  188. acceptAsClient(response) {
  189. const params = response[0];
  190. if (
  191. this._options.clientNoContextTakeover === false &&
  192. params.client_no_context_takeover
  193. ) {
  194. throw new Error('Unexpected parameter "client_no_context_takeover"');
  195. }
  196. if (!params.client_max_window_bits) {
  197. if (typeof this._options.clientMaxWindowBits === 'number') {
  198. params.client_max_window_bits = this._options.clientMaxWindowBits;
  199. }
  200. } else if (
  201. this._options.clientMaxWindowBits === false ||
  202. (typeof this._options.clientMaxWindowBits === 'number' &&
  203. params.client_max_window_bits > this._options.clientMaxWindowBits)
  204. ) {
  205. throw new Error(
  206. 'Unexpected or invalid parameter "client_max_window_bits"'
  207. );
  208. }
  209. return params;
  210. }
  211. /**
  212. * Normalize parameters.
  213. *
  214. * @param {Array} configurations The extension negotiation offers/reponse
  215. * @return {Array} The offers/response with normalized parameters
  216. * @private
  217. */
  218. normalizeParams(configurations) {
  219. configurations.forEach((params) => {
  220. Object.keys(params).forEach((key) => {
  221. let value = params[key];
  222. if (value.length > 1) {
  223. throw new Error(`Parameter "${key}" must have only a single value`);
  224. }
  225. value = value[0];
  226. if (key === 'client_max_window_bits') {
  227. if (value !== true) {
  228. const num = +value;
  229. if (!Number.isInteger(num) || num < 8 || num > 15) {
  230. throw new TypeError(
  231. `Invalid value for parameter "${key}": ${value}`
  232. );
  233. }
  234. value = num;
  235. } else if (!this._isServer) {
  236. throw new TypeError(
  237. `Invalid value for parameter "${key}": ${value}`
  238. );
  239. }
  240. } else if (key === 'server_max_window_bits') {
  241. const num = +value;
  242. if (!Number.isInteger(num) || num < 8 || num > 15) {
  243. throw new TypeError(
  244. `Invalid value for parameter "${key}": ${value}`
  245. );
  246. }
  247. value = num;
  248. } else if (
  249. key === 'client_no_context_takeover' ||
  250. key === 'server_no_context_takeover'
  251. ) {
  252. if (value !== true) {
  253. throw new TypeError(
  254. `Invalid value for parameter "${key}": ${value}`
  255. );
  256. }
  257. } else {
  258. throw new Error(`Unknown parameter "${key}"`);
  259. }
  260. params[key] = value;
  261. });
  262. });
  263. return configurations;
  264. }
  265. /**
  266. * Decompress data. Concurrency limited.
  267. *
  268. * @param {Buffer} data Compressed data
  269. * @param {Boolean} fin Specifies whether or not this is the last fragment
  270. * @param {Function} callback Callback
  271. * @public
  272. */
  273. decompress(data, fin, callback) {
  274. zlibLimiter.add((done) => {
  275. this._decompress(data, fin, (err, result) => {
  276. done();
  277. callback(err, result);
  278. });
  279. });
  280. }
  281. /**
  282. * Compress data. Concurrency limited.
  283. *
  284. * @param {(Buffer|String)} data Data to compress
  285. * @param {Boolean} fin Specifies whether or not this is the last fragment
  286. * @param {Function} callback Callback
  287. * @public
  288. */
  289. compress(data, fin, callback) {
  290. zlibLimiter.add((done) => {
  291. this._compress(data, fin, (err, result) => {
  292. done();
  293. callback(err, result);
  294. });
  295. });
  296. }
  297. /**
  298. * Decompress data.
  299. *
  300. * @param {Buffer} data Compressed data
  301. * @param {Boolean} fin Specifies whether or not this is the last fragment
  302. * @param {Function} callback Callback
  303. * @private
  304. */
  305. _decompress(data, fin, callback) {
  306. const endpoint = this._isServer ? 'client' : 'server';
  307. if (!this._inflate) {
  308. const key = `${endpoint}_max_window_bits`;
  309. const windowBits =
  310. typeof this.params[key] !== 'number'
  311. ? zlib.Z_DEFAULT_WINDOWBITS
  312. : this.params[key];
  313. this._inflate = zlib.createInflateRaw({
  314. ...this._options.zlibInflateOptions,
  315. windowBits
  316. });
  317. this._inflate[kPerMessageDeflate] = this;
  318. this._inflate[kTotalLength] = 0;
  319. this._inflate[kBuffers] = [];
  320. this._inflate.on('error', inflateOnError);
  321. this._inflate.on('data', inflateOnData);
  322. }
  323. this._inflate[kCallback] = callback;
  324. this._inflate.write(data);
  325. if (fin) this._inflate.write(TRAILER);
  326. this._inflate.flush(() => {
  327. const err = this._inflate[kError];
  328. if (err) {
  329. this._inflate.close();
  330. this._inflate = null;
  331. callback(err);
  332. return;
  333. }
  334. const data = bufferUtil.concat(
  335. this._inflate[kBuffers],
  336. this._inflate[kTotalLength]
  337. );
  338. if (this._inflate._readableState.endEmitted) {
  339. this._inflate.close();
  340. this._inflate = null;
  341. } else {
  342. this._inflate[kTotalLength] = 0;
  343. this._inflate[kBuffers] = [];
  344. if (fin && this.params[`${endpoint}_no_context_takeover`]) {
  345. this._inflate.reset();
  346. }
  347. }
  348. callback(null, data);
  349. });
  350. }
  351. /**
  352. * Compress data.
  353. *
  354. * @param {(Buffer|String)} data Data to compress
  355. * @param {Boolean} fin Specifies whether or not this is the last fragment
  356. * @param {Function} callback Callback
  357. * @private
  358. */
  359. _compress(data, fin, callback) {
  360. const endpoint = this._isServer ? 'server' : 'client';
  361. if (!this._deflate) {
  362. const key = `${endpoint}_max_window_bits`;
  363. const windowBits =
  364. typeof this.params[key] !== 'number'
  365. ? zlib.Z_DEFAULT_WINDOWBITS
  366. : this.params[key];
  367. this._deflate = zlib.createDeflateRaw({
  368. ...this._options.zlibDeflateOptions,
  369. windowBits
  370. });
  371. this._deflate[kTotalLength] = 0;
  372. this._deflate[kBuffers] = [];
  373. this._deflate.on('data', deflateOnData);
  374. }
  375. this._deflate[kCallback] = callback;
  376. this._deflate.write(data);
  377. this._deflate.flush(zlib.Z_SYNC_FLUSH, () => {
  378. if (!this._deflate) {
  379. //
  380. // The deflate stream was closed while data was being processed.
  381. //
  382. return;
  383. }
  384. let data = bufferUtil.concat(
  385. this._deflate[kBuffers],
  386. this._deflate[kTotalLength]
  387. );
  388. if (fin) {
  389. data = new FastBuffer(data.buffer, data.byteOffset, data.length - 4);
  390. }
  391. //
  392. // Ensure that the callback will not be called again in
  393. // `PerMessageDeflate#cleanup()`.
  394. //
  395. this._deflate[kCallback] = null;
  396. this._deflate[kTotalLength] = 0;
  397. this._deflate[kBuffers] = [];
  398. if (fin && this.params[`${endpoint}_no_context_takeover`]) {
  399. this._deflate.reset();
  400. }
  401. callback(null, data);
  402. });
  403. }
  404. }
  405. module.exports = PerMessageDeflate;
  406. /**
  407. * The listener of the `zlib.DeflateRaw` stream `'data'` event.
  408. *
  409. * @param {Buffer} chunk A chunk of data
  410. * @private
  411. */
  412. function deflateOnData(chunk) {
  413. this[kBuffers].push(chunk);
  414. this[kTotalLength] += chunk.length;
  415. }
  416. /**
  417. * The listener of the `zlib.InflateRaw` stream `'data'` event.
  418. *
  419. * @param {Buffer} chunk A chunk of data
  420. * @private
  421. */
  422. function inflateOnData(chunk) {
  423. this[kTotalLength] += chunk.length;
  424. if (
  425. this[kPerMessageDeflate]._maxPayload < 1 ||
  426. this[kTotalLength] <= this[kPerMessageDeflate]._maxPayload
  427. ) {
  428. this[kBuffers].push(chunk);
  429. return;
  430. }
  431. this[kError] = new RangeError('Max payload size exceeded');
  432. this[kError].code = 'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH';
  433. this[kError][kStatusCode] = 1009;
  434. this.removeListener('data', inflateOnData);
  435. //
  436. // The choice to employ `zlib.reset()` over `zlib.close()` is dictated by the
  437. // fact that in Node.js versions prior to 13.10.0, the callback for
  438. // `zlib.flush()` is not called if `zlib.close()` is used. Utilizing
  439. // `zlib.reset()` ensures that either the callback is invoked or an error is
  440. // emitted.
  441. //
  442. this.reset();
  443. }
  444. /**
  445. * The listener of the `zlib.InflateRaw` stream `'error'` event.
  446. *
  447. * @param {Error} err The emitted error
  448. * @private
  449. */
  450. function inflateOnError(err) {
  451. //
  452. // There is no need to call `Zlib#close()` as the handle is automatically
  453. // closed when an error is emitted.
  454. //
  455. this[kPerMessageDeflate]._inflate = null;
  456. if (this[kError]) {
  457. this[kCallback](this[kError]);
  458. return;
  459. }
  460. err[kStatusCode] = 1007;
  461. this[kCallback](err);
  462. }