permessage-deflate.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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|Number)} [options.serverMaxWindowBits] Request/confirm the
  36. * use of a custom server window size
  37. * @param {Boolean} [options.serverNoContextTakeover=false] Request/accept
  38. * disabling of server context takeover
  39. * @param {Number} [options.threshold=1024] Size (in bytes) below which
  40. * messages should not be compressed if context takeover is disabled
  41. * @param {Object} [options.zlibDeflateOptions] Options to pass to zlib on
  42. * deflate
  43. * @param {Object} [options.zlibInflateOptions] Options to pass to zlib on
  44. * inflate
  45. * @param {Boolean} [isServer=false] Create the instance in either server or
  46. * client mode
  47. * @param {Number} [maxPayload=0] The maximum allowed message length
  48. */
  49. constructor(options, isServer, maxPayload) {
  50. this._maxPayload = maxPayload | 0;
  51. this._options = options || {};
  52. this._threshold =
  53. this._options.threshold !== undefined ? this._options.threshold : 1024;
  54. this._isServer = !!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. !params.client_max_window_bits)
  152. ) {
  153. return false;
  154. }
  155. return true;
  156. });
  157. if (!accepted) {
  158. throw new Error('None of the extension offers can be accepted');
  159. }
  160. if (opts.serverNoContextTakeover) {
  161. accepted.server_no_context_takeover = true;
  162. }
  163. if (opts.clientNoContextTakeover) {
  164. accepted.client_no_context_takeover = true;
  165. }
  166. if (typeof opts.serverMaxWindowBits === 'number') {
  167. accepted.server_max_window_bits = opts.serverMaxWindowBits;
  168. }
  169. if (typeof opts.clientMaxWindowBits === 'number') {
  170. accepted.client_max_window_bits = opts.clientMaxWindowBits;
  171. } else if (
  172. accepted.client_max_window_bits === true ||
  173. opts.clientMaxWindowBits === false
  174. ) {
  175. delete accepted.client_max_window_bits;
  176. }
  177. return accepted;
  178. }
  179. /**
  180. * Accept the extension negotiation response.
  181. *
  182. * @param {Array} response The extension negotiation response
  183. * @return {Object} Accepted configuration
  184. * @private
  185. */
  186. acceptAsClient(response) {
  187. const params = response[0];
  188. if (
  189. this._options.clientNoContextTakeover === false &&
  190. params.client_no_context_takeover
  191. ) {
  192. throw new Error('Unexpected parameter "client_no_context_takeover"');
  193. }
  194. if (!params.client_max_window_bits) {
  195. if (typeof this._options.clientMaxWindowBits === 'number') {
  196. params.client_max_window_bits = this._options.clientMaxWindowBits;
  197. }
  198. } else if (
  199. this._options.clientMaxWindowBits === false ||
  200. (typeof this._options.clientMaxWindowBits === 'number' &&
  201. params.client_max_window_bits > this._options.clientMaxWindowBits)
  202. ) {
  203. throw new Error(
  204. 'Unexpected or invalid parameter "client_max_window_bits"'
  205. );
  206. }
  207. return params;
  208. }
  209. /**
  210. * Normalize parameters.
  211. *
  212. * @param {Array} configurations The extension negotiation offers/reponse
  213. * @return {Array} The offers/response with normalized parameters
  214. * @private
  215. */
  216. normalizeParams(configurations) {
  217. configurations.forEach((params) => {
  218. Object.keys(params).forEach((key) => {
  219. let value = params[key];
  220. if (value.length > 1) {
  221. throw new Error(`Parameter "${key}" must have only a single value`);
  222. }
  223. value = value[0];
  224. if (key === 'client_max_window_bits') {
  225. if (value !== true) {
  226. const num = +value;
  227. if (!Number.isInteger(num) || num < 8 || num > 15) {
  228. throw new TypeError(
  229. `Invalid value for parameter "${key}": ${value}`
  230. );
  231. }
  232. value = num;
  233. } else if (!this._isServer) {
  234. throw new TypeError(
  235. `Invalid value for parameter "${key}": ${value}`
  236. );
  237. }
  238. } else if (key === 'server_max_window_bits') {
  239. const num = +value;
  240. if (!Number.isInteger(num) || num < 8 || num > 15) {
  241. throw new TypeError(
  242. `Invalid value for parameter "${key}": ${value}`
  243. );
  244. }
  245. value = num;
  246. } else if (
  247. key === 'client_no_context_takeover' ||
  248. key === 'server_no_context_takeover'
  249. ) {
  250. if (value !== true) {
  251. throw new TypeError(
  252. `Invalid value for parameter "${key}": ${value}`
  253. );
  254. }
  255. } else {
  256. throw new Error(`Unknown parameter "${key}"`);
  257. }
  258. params[key] = value;
  259. });
  260. });
  261. return configurations;
  262. }
  263. /**
  264. * Decompress data. Concurrency limited.
  265. *
  266. * @param {Buffer} data Compressed data
  267. * @param {Boolean} fin Specifies whether or not this is the last fragment
  268. * @param {Function} callback Callback
  269. * @public
  270. */
  271. decompress(data, fin, callback) {
  272. zlibLimiter.add((done) => {
  273. this._decompress(data, fin, (err, result) => {
  274. done();
  275. callback(err, result);
  276. });
  277. });
  278. }
  279. /**
  280. * Compress data. Concurrency limited.
  281. *
  282. * @param {(Buffer|String)} data Data to compress
  283. * @param {Boolean} fin Specifies whether or not this is the last fragment
  284. * @param {Function} callback Callback
  285. * @public
  286. */
  287. compress(data, fin, callback) {
  288. zlibLimiter.add((done) => {
  289. this._compress(data, fin, (err, result) => {
  290. done();
  291. callback(err, result);
  292. });
  293. });
  294. }
  295. /**
  296. * Decompress data.
  297. *
  298. * @param {Buffer} data Compressed data
  299. * @param {Boolean} fin Specifies whether or not this is the last fragment
  300. * @param {Function} callback Callback
  301. * @private
  302. */
  303. _decompress(data, fin, callback) {
  304. const endpoint = this._isServer ? 'client' : 'server';
  305. if (!this._inflate) {
  306. const key = `${endpoint}_max_window_bits`;
  307. const windowBits =
  308. typeof this.params[key] !== 'number'
  309. ? zlib.Z_DEFAULT_WINDOWBITS
  310. : this.params[key];
  311. this._inflate = zlib.createInflateRaw({
  312. ...this._options.zlibInflateOptions,
  313. windowBits
  314. });
  315. this._inflate[kPerMessageDeflate] = this;
  316. this._inflate[kTotalLength] = 0;
  317. this._inflate[kBuffers] = [];
  318. this._inflate.on('error', inflateOnError);
  319. this._inflate.on('data', inflateOnData);
  320. }
  321. this._inflate[kCallback] = callback;
  322. this._inflate.write(data);
  323. if (fin) this._inflate.write(TRAILER);
  324. this._inflate.flush(() => {
  325. const err = this._inflate[kError];
  326. if (err) {
  327. this._inflate.close();
  328. this._inflate = null;
  329. callback(err);
  330. return;
  331. }
  332. const data = bufferUtil.concat(
  333. this._inflate[kBuffers],
  334. this._inflate[kTotalLength]
  335. );
  336. if (this._inflate._readableState.endEmitted) {
  337. this._inflate.close();
  338. this._inflate = null;
  339. } else {
  340. this._inflate[kTotalLength] = 0;
  341. this._inflate[kBuffers] = [];
  342. if (fin && this.params[`${endpoint}_no_context_takeover`]) {
  343. this._inflate.reset();
  344. }
  345. }
  346. callback(null, data);
  347. });
  348. }
  349. /**
  350. * Compress data.
  351. *
  352. * @param {(Buffer|String)} data Data to compress
  353. * @param {Boolean} fin Specifies whether or not this is the last fragment
  354. * @param {Function} callback Callback
  355. * @private
  356. */
  357. _compress(data, fin, callback) {
  358. const endpoint = this._isServer ? 'server' : 'client';
  359. if (!this._deflate) {
  360. const key = `${endpoint}_max_window_bits`;
  361. const windowBits =
  362. typeof this.params[key] !== 'number'
  363. ? zlib.Z_DEFAULT_WINDOWBITS
  364. : this.params[key];
  365. this._deflate = zlib.createDeflateRaw({
  366. ...this._options.zlibDeflateOptions,
  367. windowBits
  368. });
  369. this._deflate[kTotalLength] = 0;
  370. this._deflate[kBuffers] = [];
  371. this._deflate.on('data', deflateOnData);
  372. }
  373. this._deflate[kCallback] = callback;
  374. this._deflate.write(data);
  375. this._deflate.flush(zlib.Z_SYNC_FLUSH, () => {
  376. if (!this._deflate) {
  377. //
  378. // The deflate stream was closed while data was being processed.
  379. //
  380. return;
  381. }
  382. let data = bufferUtil.concat(
  383. this._deflate[kBuffers],
  384. this._deflate[kTotalLength]
  385. );
  386. if (fin) {
  387. data = new FastBuffer(data.buffer, data.byteOffset, data.length - 4);
  388. }
  389. //
  390. // Ensure that the callback will not be called again in
  391. // `PerMessageDeflate#cleanup()`.
  392. //
  393. this._deflate[kCallback] = null;
  394. this._deflate[kTotalLength] = 0;
  395. this._deflate[kBuffers] = [];
  396. if (fin && this.params[`${endpoint}_no_context_takeover`]) {
  397. this._deflate.reset();
  398. }
  399. callback(null, data);
  400. });
  401. }
  402. }
  403. module.exports = PerMessageDeflate;
  404. /**
  405. * The listener of the `zlib.DeflateRaw` stream `'data'` event.
  406. *
  407. * @param {Buffer} chunk A chunk of data
  408. * @private
  409. */
  410. function deflateOnData(chunk) {
  411. this[kBuffers].push(chunk);
  412. this[kTotalLength] += chunk.length;
  413. }
  414. /**
  415. * The listener of the `zlib.InflateRaw` stream `'data'` event.
  416. *
  417. * @param {Buffer} chunk A chunk of data
  418. * @private
  419. */
  420. function inflateOnData(chunk) {
  421. this[kTotalLength] += chunk.length;
  422. if (
  423. this[kPerMessageDeflate]._maxPayload < 1 ||
  424. this[kTotalLength] <= this[kPerMessageDeflate]._maxPayload
  425. ) {
  426. this[kBuffers].push(chunk);
  427. return;
  428. }
  429. this[kError] = new RangeError('Max payload size exceeded');
  430. this[kError].code = 'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH';
  431. this[kError][kStatusCode] = 1009;
  432. this.removeListener('data', inflateOnData);
  433. this.reset();
  434. }
  435. /**
  436. * The listener of the `zlib.InflateRaw` stream `'error'` event.
  437. *
  438. * @param {Error} err The emitted error
  439. * @private
  440. */
  441. function inflateOnError(err) {
  442. //
  443. // There is no need to call `Zlib#close()` as the handle is automatically
  444. // closed when an error is emitted.
  445. //
  446. this[kPerMessageDeflate]._inflate = null;
  447. err[kStatusCode] = 1007;
  448. this[kCallback](err);
  449. }