Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

permessage-deflate.js 14KB

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