Front end of the Slack clone application.

PerMessageDeflate.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. 'use strict';
  2. const safeBuffer = require('safe-buffer');
  3. const Limiter = require('async-limiter');
  4. const zlib = require('zlib');
  5. const bufferUtil = require('./BufferUtil');
  6. const Buffer = safeBuffer.Buffer;
  7. const TRAILER = Buffer.from([0x00, 0x00, 0xff, 0xff]);
  8. const EMPTY_BLOCK = Buffer.from([0x00]);
  9. const kWriteInProgress = Symbol('write-in-progress');
  10. const kPendingClose = Symbol('pending-close');
  11. const kTotalLength = Symbol('total-length');
  12. const kCallback = Symbol('callback');
  13. const kBuffers = Symbol('buffers');
  14. const kError = Symbol('error');
  15. const kOwner = Symbol('owner');
  16. //
  17. // We limit zlib concurrency, which prevents severe memory fragmentation
  18. // as documented in https://github.com/nodejs/node/issues/8871#issuecomment-250915913
  19. // and https://github.com/websockets/ws/issues/1202
  20. //
  21. // Intentionally global; it's the global thread pool that's an issue.
  22. //
  23. let zlibLimiter;
  24. /**
  25. * permessage-deflate implementation.
  26. */
  27. class PerMessageDeflate {
  28. /**
  29. * Creates a PerMessageDeflate instance.
  30. *
  31. * @param {Object} options Configuration options
  32. * @param {Boolean} options.serverNoContextTakeover Request/accept disabling
  33. * of server context takeover
  34. * @param {Boolean} options.clientNoContextTakeover Advertise/acknowledge
  35. * disabling of client context takeover
  36. * @param {(Boolean|Number)} options.serverMaxWindowBits Request/confirm the
  37. * use of a custom server window size
  38. * @param {(Boolean|Number)} options.clientMaxWindowBits Advertise support
  39. * for, or request, a custom client window size
  40. * @param {Number} options.level The value of zlib's `level` param
  41. * @param {Number} options.memLevel The value of zlib's `memLevel` param
  42. * @param {Number} options.threshold Size (in bytes) below which messages
  43. * should not be compressed
  44. * @param {Number} options.concurrencyLimit The number of concurrent calls to
  45. * zlib
  46. * @param {Boolean} isServer Create the instance in either server or client
  47. * mode
  48. * @param {Number} maxPayload The maximum allowed message length
  49. */
  50. constructor (options, isServer, maxPayload) {
  51. this._maxPayload = maxPayload | 0;
  52. this._options = options || {};
  53. this._threshold = this._options.threshold !== undefined
  54. ? this._options.threshold
  55. : 1024;
  56. this._isServer = !!isServer;
  57. this._deflate = null;
  58. this._inflate = null;
  59. this.params = null;
  60. if (!zlibLimiter) {
  61. const concurrency = this._options.concurrencyLimit !== undefined
  62. ? this._options.concurrencyLimit
  63. : 10;
  64. zlibLimiter = new Limiter({ concurrency });
  65. }
  66. }
  67. /**
  68. * @type {String}
  69. */
  70. static get extensionName () {
  71. return 'permessage-deflate';
  72. }
  73. /**
  74. * Create extension parameters offer.
  75. *
  76. * @return {Object} Extension parameters
  77. * @public
  78. */
  79. offer () {
  80. const params = {};
  81. if (this._options.serverNoContextTakeover) {
  82. params.server_no_context_takeover = true;
  83. }
  84. if (this._options.clientNoContextTakeover) {
  85. params.client_no_context_takeover = true;
  86. }
  87. if (this._options.serverMaxWindowBits) {
  88. params.server_max_window_bits = this._options.serverMaxWindowBits;
  89. }
  90. if (this._options.clientMaxWindowBits) {
  91. params.client_max_window_bits = this._options.clientMaxWindowBits;
  92. } else if (this._options.clientMaxWindowBits == null) {
  93. params.client_max_window_bits = true;
  94. }
  95. return params;
  96. }
  97. /**
  98. * Accept extension offer.
  99. *
  100. * @param {Array} paramsList Extension parameters
  101. * @return {Object} Accepted configuration
  102. * @public
  103. */
  104. accept (paramsList) {
  105. paramsList = this.normalizeParams(paramsList);
  106. var params;
  107. if (this._isServer) {
  108. params = this.acceptAsServer(paramsList);
  109. } else {
  110. params = this.acceptAsClient(paramsList);
  111. }
  112. this.params = params;
  113. return params;
  114. }
  115. /**
  116. * Releases all resources used by the extension.
  117. *
  118. * @public
  119. */
  120. cleanup () {
  121. if (this._inflate) {
  122. if (this._inflate[kWriteInProgress]) {
  123. this._inflate[kPendingClose] = true;
  124. } else {
  125. this._inflate.close();
  126. this._inflate = null;
  127. }
  128. }
  129. if (this._deflate) {
  130. if (this._deflate[kWriteInProgress]) {
  131. this._deflate[kPendingClose] = true;
  132. } else {
  133. this._deflate.close();
  134. this._deflate = null;
  135. }
  136. }
  137. }
  138. /**
  139. * Accept extension offer from client.
  140. *
  141. * @param {Array} paramsList Extension parameters
  142. * @return {Object} Accepted configuration
  143. * @private
  144. */
  145. acceptAsServer (paramsList) {
  146. const accepted = {};
  147. const result = paramsList.some((params) => {
  148. if (
  149. (this._options.serverNoContextTakeover === false &&
  150. params.server_no_context_takeover) ||
  151. (this._options.serverMaxWindowBits === false &&
  152. params.server_max_window_bits) ||
  153. (typeof this._options.serverMaxWindowBits === 'number' &&
  154. typeof params.server_max_window_bits === 'number' &&
  155. this._options.serverMaxWindowBits > params.server_max_window_bits) ||
  156. (typeof this._options.clientMaxWindowBits === 'number' &&
  157. !params.client_max_window_bits)
  158. ) {
  159. return;
  160. }
  161. if (
  162. this._options.serverNoContextTakeover ||
  163. params.server_no_context_takeover
  164. ) {
  165. accepted.server_no_context_takeover = true;
  166. }
  167. if (
  168. this._options.clientNoContextTakeover ||
  169. (this._options.clientNoContextTakeover !== false &&
  170. params.client_no_context_takeover)
  171. ) {
  172. accepted.client_no_context_takeover = true;
  173. }
  174. if (typeof this._options.serverMaxWindowBits === 'number') {
  175. accepted.server_max_window_bits = this._options.serverMaxWindowBits;
  176. } else if (typeof params.server_max_window_bits === 'number') {
  177. accepted.server_max_window_bits = params.server_max_window_bits;
  178. }
  179. if (typeof this._options.clientMaxWindowBits === 'number') {
  180. accepted.client_max_window_bits = this._options.clientMaxWindowBits;
  181. } else if (
  182. this._options.clientMaxWindowBits !== false &&
  183. typeof params.client_max_window_bits === 'number'
  184. ) {
  185. accepted.client_max_window_bits = params.client_max_window_bits;
  186. }
  187. return true;
  188. });
  189. if (!result) throw new Error("Doesn't support the offered configuration");
  190. return accepted;
  191. }
  192. /**
  193. * Accept extension response from server.
  194. *
  195. * @param {Array} paramsList Extension parameters
  196. * @return {Object} Accepted configuration
  197. * @private
  198. */
  199. acceptAsClient (paramsList) {
  200. const params = paramsList[0];
  201. if (
  202. this._options.clientNoContextTakeover === false &&
  203. params.client_no_context_takeover
  204. ) {
  205. throw new Error('Invalid value for "client_no_context_takeover"');
  206. }
  207. if (
  208. (typeof this._options.clientMaxWindowBits === 'number' &&
  209. (!params.client_max_window_bits ||
  210. params.client_max_window_bits > this._options.clientMaxWindowBits)) ||
  211. (this._options.clientMaxWindowBits === false &&
  212. params.client_max_window_bits)
  213. ) {
  214. throw new Error('Invalid value for "client_max_window_bits"');
  215. }
  216. return params;
  217. }
  218. /**
  219. * Normalize extensions parameters.
  220. *
  221. * @param {Array} paramsList Extension parameters
  222. * @return {Array} Normalized extensions parameters
  223. * @private
  224. */
  225. normalizeParams (paramsList) {
  226. return paramsList.map((params) => {
  227. Object.keys(params).forEach((key) => {
  228. var value = params[key];
  229. if (value.length > 1) {
  230. throw new Error(`Multiple extension parameters for ${key}`);
  231. }
  232. value = value[0];
  233. switch (key) {
  234. case 'server_no_context_takeover':
  235. case 'client_no_context_takeover':
  236. if (value !== true) {
  237. throw new Error(`invalid extension parameter value for ${key} (${value})`);
  238. }
  239. params[key] = true;
  240. break;
  241. case 'server_max_window_bits':
  242. case 'client_max_window_bits':
  243. if (typeof value === 'string') {
  244. value = parseInt(value, 10);
  245. if (
  246. Number.isNaN(value) ||
  247. value < zlib.Z_MIN_WINDOWBITS ||
  248. value > zlib.Z_MAX_WINDOWBITS
  249. ) {
  250. throw new Error(`invalid extension parameter value for ${key} (${value})`);
  251. }
  252. }
  253. if (!this._isServer && value === true) {
  254. throw new Error(`Missing extension parameter value for ${key}`);
  255. }
  256. params[key] = value;
  257. break;
  258. default:
  259. throw new Error(`Not defined extension parameter (${key})`);
  260. }
  261. });
  262. return params;
  263. });
  264. }
  265. /**
  266. * Decompress data. Concurrency limited by async-limiter.
  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.push((done) => {
  275. this._decompress(data, fin, (err, result) => {
  276. done();
  277. callback(err, result);
  278. });
  279. });
  280. }
  281. /**
  282. * Compress data. Concurrency limited by async-limiter.
  283. *
  284. * @param {Buffer} 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.push((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 = typeof this.params[key] !== 'number'
  310. ? zlib.Z_DEFAULT_WINDOWBITS
  311. : this.params[key];
  312. this._inflate = zlib.createInflateRaw({ windowBits });
  313. this._inflate[kTotalLength] = 0;
  314. this._inflate[kBuffers] = [];
  315. this._inflate[kOwner] = this;
  316. this._inflate.on('error', inflateOnError);
  317. this._inflate.on('data', inflateOnData);
  318. }
  319. this._inflate[kCallback] = callback;
  320. this._inflate[kWriteInProgress] = true;
  321. this._inflate.write(data);
  322. if (fin) this._inflate.write(TRAILER);
  323. this._inflate.flush(() => {
  324. const err = this._inflate[kError];
  325. if (err) {
  326. this._inflate.close();
  327. this._inflate = null;
  328. callback(err);
  329. return;
  330. }
  331. const data = bufferUtil.concat(
  332. this._inflate[kBuffers],
  333. this._inflate[kTotalLength]
  334. );
  335. if (
  336. (fin && this.params[`${endpoint}_no_context_takeover`]) ||
  337. this._inflate[kPendingClose]
  338. ) {
  339. this._inflate.close();
  340. this._inflate = null;
  341. } else {
  342. this._inflate[kWriteInProgress] = false;
  343. this._inflate[kTotalLength] = 0;
  344. this._inflate[kBuffers] = [];
  345. }
  346. callback(null, data);
  347. });
  348. }
  349. /**
  350. * Compress data.
  351. *
  352. * @param {Buffer} 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. if (!data || data.length === 0) {
  359. process.nextTick(callback, null, EMPTY_BLOCK);
  360. return;
  361. }
  362. const endpoint = this._isServer ? 'server' : 'client';
  363. if (!this._deflate) {
  364. const key = `${endpoint}_max_window_bits`;
  365. const windowBits = typeof this.params[key] !== 'number'
  366. ? zlib.Z_DEFAULT_WINDOWBITS
  367. : this.params[key];
  368. this._deflate = zlib.createDeflateRaw({
  369. memLevel: this._options.memLevel,
  370. level: this._options.level,
  371. flush: zlib.Z_SYNC_FLUSH,
  372. windowBits
  373. });
  374. this._deflate[kTotalLength] = 0;
  375. this._deflate[kBuffers] = [];
  376. //
  377. // `zlib.DeflateRaw` emits an `'error'` event only when an attempt to use
  378. // it is made after it has already been closed. This cannot happen here,
  379. // so we only add a listener for the `'data'` event.
  380. //
  381. this._deflate.on('data', deflateOnData);
  382. }
  383. this._deflate[kWriteInProgress] = true;
  384. this._deflate.write(data);
  385. this._deflate.flush(zlib.Z_SYNC_FLUSH, () => {
  386. var data = bufferUtil.concat(
  387. this._deflate[kBuffers],
  388. this._deflate[kTotalLength]
  389. );
  390. if (fin) data = data.slice(0, data.length - 4);
  391. if (
  392. (fin && this.params[`${endpoint}_no_context_takeover`]) ||
  393. this._deflate[kPendingClose]
  394. ) {
  395. this._deflate.close();
  396. this._deflate = null;
  397. } else {
  398. this._deflate[kWriteInProgress] = false;
  399. this._deflate[kTotalLength] = 0;
  400. this._deflate[kBuffers] = [];
  401. }
  402. callback(null, data);
  403. });
  404. }
  405. }
  406. module.exports = PerMessageDeflate;
  407. /**
  408. * The listener of the `zlib.DeflateRaw` stream `'data'` event.
  409. *
  410. * @param {Buffer} chunk A chunk of data
  411. * @private
  412. */
  413. function deflateOnData (chunk) {
  414. this[kBuffers].push(chunk);
  415. this[kTotalLength] += chunk.length;
  416. }
  417. /**
  418. * The listener of the `zlib.InflateRaw` stream `'data'` event.
  419. *
  420. * @param {Buffer} chunk A chunk of data
  421. * @private
  422. */
  423. function inflateOnData (chunk) {
  424. this[kTotalLength] += chunk.length;
  425. if (
  426. this[kOwner]._maxPayload < 1 ||
  427. this[kTotalLength] <= this[kOwner]._maxPayload
  428. ) {
  429. this[kBuffers].push(chunk);
  430. return;
  431. }
  432. this[kError] = new Error('max payload size exceeded');
  433. this[kError].closeCode = 1009;
  434. this.removeListener('data', inflateOnData);
  435. this.reset();
  436. }
  437. /**
  438. * The listener of the `zlib.InflateRaw` stream `'error'` event.
  439. *
  440. * @param {Error} err The emitted error
  441. * @private
  442. */
  443. function inflateOnError (err) {
  444. //
  445. // There is no need to call `Zlib#close()` as the handle is automatically
  446. // closed when an error is emitted.
  447. //
  448. this[kOwner]._inflate = null;
  449. this[kCallback](err);
  450. }