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

sender.js 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. 'use strict';
  2. const { randomFillSync } = require('crypto');
  3. const PerMessageDeflate = require('./permessage-deflate');
  4. const { EMPTY_BUFFER } = require('./constants');
  5. const { isValidStatusCode } = require('./validation');
  6. const { mask: applyMask, toBuffer } = require('./buffer-util');
  7. const mask = Buffer.alloc(4);
  8. /**
  9. * HyBi Sender implementation.
  10. */
  11. class Sender {
  12. /**
  13. * Creates a Sender instance.
  14. *
  15. * @param {net.Socket} socket The connection socket
  16. * @param {Object} extensions An object containing the negotiated extensions
  17. */
  18. constructor(socket, extensions) {
  19. this._extensions = extensions || {};
  20. this._socket = socket;
  21. this._firstFragment = true;
  22. this._compress = false;
  23. this._bufferedBytes = 0;
  24. this._deflating = false;
  25. this._queue = [];
  26. }
  27. /**
  28. * Frames a piece of data according to the HyBi WebSocket protocol.
  29. *
  30. * @param {Buffer} data The data to frame
  31. * @param {Object} options Options object
  32. * @param {Number} options.opcode The opcode
  33. * @param {Boolean} options.readOnly Specifies whether `data` can be modified
  34. * @param {Boolean} options.fin Specifies whether or not to set the FIN bit
  35. * @param {Boolean} options.mask Specifies whether or not to mask `data`
  36. * @param {Boolean} options.rsv1 Specifies whether or not to set the RSV1 bit
  37. * @return {Buffer[]} The framed data as a list of `Buffer` instances
  38. * @public
  39. */
  40. static frame(data, options) {
  41. const merge = options.mask && options.readOnly;
  42. let offset = options.mask ? 6 : 2;
  43. let payloadLength = data.length;
  44. if (data.length >= 65536) {
  45. offset += 8;
  46. payloadLength = 127;
  47. } else if (data.length > 125) {
  48. offset += 2;
  49. payloadLength = 126;
  50. }
  51. const target = Buffer.allocUnsafe(merge ? data.length + offset : offset);
  52. target[0] = options.fin ? options.opcode | 0x80 : options.opcode;
  53. if (options.rsv1) target[0] |= 0x40;
  54. target[1] = payloadLength;
  55. if (payloadLength === 126) {
  56. target.writeUInt16BE(data.length, 2);
  57. } else if (payloadLength === 127) {
  58. target.writeUInt32BE(0, 2);
  59. target.writeUInt32BE(data.length, 6);
  60. }
  61. if (!options.mask) return [target, data];
  62. randomFillSync(mask, 0, 4);
  63. target[1] |= 0x80;
  64. target[offset - 4] = mask[0];
  65. target[offset - 3] = mask[1];
  66. target[offset - 2] = mask[2];
  67. target[offset - 1] = mask[3];
  68. if (merge) {
  69. applyMask(data, mask, target, offset, data.length);
  70. return [target];
  71. }
  72. applyMask(data, mask, data, 0, data.length);
  73. return [target, data];
  74. }
  75. /**
  76. * Sends a close message to the other peer.
  77. *
  78. * @param {(Number|undefined)} code The status code component of the body
  79. * @param {String} data The message component of the body
  80. * @param {Boolean} mask Specifies whether or not to mask the message
  81. * @param {Function} cb Callback
  82. * @public
  83. */
  84. close(code, data, mask, cb) {
  85. let buf;
  86. if (code === undefined) {
  87. buf = EMPTY_BUFFER;
  88. } else if (typeof code !== 'number' || !isValidStatusCode(code)) {
  89. throw new TypeError('First argument must be a valid error code number');
  90. } else if (data === undefined || data === '') {
  91. buf = Buffer.allocUnsafe(2);
  92. buf.writeUInt16BE(code, 0);
  93. } else {
  94. buf = Buffer.allocUnsafe(2 + Buffer.byteLength(data));
  95. buf.writeUInt16BE(code, 0);
  96. buf.write(data, 2);
  97. }
  98. if (this._deflating) {
  99. this.enqueue([this.doClose, buf, mask, cb]);
  100. } else {
  101. this.doClose(buf, mask, cb);
  102. }
  103. }
  104. /**
  105. * Frames and sends a close message.
  106. *
  107. * @param {Buffer} data The message to send
  108. * @param {Boolean} mask Specifies whether or not to mask `data`
  109. * @param {Function} cb Callback
  110. * @private
  111. */
  112. doClose(data, mask, cb) {
  113. this.sendFrame(
  114. Sender.frame(data, {
  115. fin: true,
  116. rsv1: false,
  117. opcode: 0x08,
  118. mask,
  119. readOnly: false
  120. }),
  121. cb
  122. );
  123. }
  124. /**
  125. * Sends a ping message to the other peer.
  126. *
  127. * @param {*} data The message to send
  128. * @param {Boolean} mask Specifies whether or not to mask `data`
  129. * @param {Function} cb Callback
  130. * @public
  131. */
  132. ping(data, mask, cb) {
  133. const buf = toBuffer(data);
  134. if (this._deflating) {
  135. this.enqueue([this.doPing, buf, mask, toBuffer.readOnly, cb]);
  136. } else {
  137. this.doPing(buf, mask, toBuffer.readOnly, cb);
  138. }
  139. }
  140. /**
  141. * Frames and sends a ping message.
  142. *
  143. * @param {*} data The message to send
  144. * @param {Boolean} mask Specifies whether or not to mask `data`
  145. * @param {Boolean} readOnly Specifies whether `data` can be modified
  146. * @param {Function} cb Callback
  147. * @private
  148. */
  149. doPing(data, mask, readOnly, cb) {
  150. this.sendFrame(
  151. Sender.frame(data, {
  152. fin: true,
  153. rsv1: false,
  154. opcode: 0x09,
  155. mask,
  156. readOnly
  157. }),
  158. cb
  159. );
  160. }
  161. /**
  162. * Sends a pong message to the other peer.
  163. *
  164. * @param {*} data The message to send
  165. * @param {Boolean} mask Specifies whether or not to mask `data`
  166. * @param {Function} cb Callback
  167. * @public
  168. */
  169. pong(data, mask, cb) {
  170. const buf = toBuffer(data);
  171. if (this._deflating) {
  172. this.enqueue([this.doPong, buf, mask, toBuffer.readOnly, cb]);
  173. } else {
  174. this.doPong(buf, mask, toBuffer.readOnly, cb);
  175. }
  176. }
  177. /**
  178. * Frames and sends a pong message.
  179. *
  180. * @param {*} data The message to send
  181. * @param {Boolean} mask Specifies whether or not to mask `data`
  182. * @param {Boolean} readOnly Specifies whether `data` can be modified
  183. * @param {Function} cb Callback
  184. * @private
  185. */
  186. doPong(data, mask, readOnly, cb) {
  187. this.sendFrame(
  188. Sender.frame(data, {
  189. fin: true,
  190. rsv1: false,
  191. opcode: 0x0a,
  192. mask,
  193. readOnly
  194. }),
  195. cb
  196. );
  197. }
  198. /**
  199. * Sends a data message to the other peer.
  200. *
  201. * @param {*} data The message to send
  202. * @param {Object} options Options object
  203. * @param {Boolean} options.compress Specifies whether or not to compress `data`
  204. * @param {Boolean} options.binary Specifies whether `data` is binary or text
  205. * @param {Boolean} options.fin Specifies whether the fragment is the last one
  206. * @param {Boolean} options.mask Specifies whether or not to mask `data`
  207. * @param {Function} cb Callback
  208. * @public
  209. */
  210. send(data, options, cb) {
  211. const buf = toBuffer(data);
  212. const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
  213. let opcode = options.binary ? 2 : 1;
  214. let rsv1 = options.compress;
  215. if (this._firstFragment) {
  216. this._firstFragment = false;
  217. if (rsv1 && perMessageDeflate) {
  218. rsv1 = buf.length >= perMessageDeflate._threshold;
  219. }
  220. this._compress = rsv1;
  221. } else {
  222. rsv1 = false;
  223. opcode = 0;
  224. }
  225. if (options.fin) this._firstFragment = true;
  226. if (perMessageDeflate) {
  227. const opts = {
  228. fin: options.fin,
  229. rsv1,
  230. opcode,
  231. mask: options.mask,
  232. readOnly: toBuffer.readOnly
  233. };
  234. if (this._deflating) {
  235. this.enqueue([this.dispatch, buf, this._compress, opts, cb]);
  236. } else {
  237. this.dispatch(buf, this._compress, opts, cb);
  238. }
  239. } else {
  240. this.sendFrame(
  241. Sender.frame(buf, {
  242. fin: options.fin,
  243. rsv1: false,
  244. opcode,
  245. mask: options.mask,
  246. readOnly: toBuffer.readOnly
  247. }),
  248. cb
  249. );
  250. }
  251. }
  252. /**
  253. * Dispatches a data message.
  254. *
  255. * @param {Buffer} data The message to send
  256. * @param {Boolean} compress Specifies whether or not to compress `data`
  257. * @param {Object} options Options object
  258. * @param {Number} options.opcode The opcode
  259. * @param {Boolean} options.readOnly Specifies whether `data` can be modified
  260. * @param {Boolean} options.fin Specifies whether or not to set the FIN bit
  261. * @param {Boolean} options.mask Specifies whether or not to mask `data`
  262. * @param {Boolean} options.rsv1 Specifies whether or not to set the RSV1 bit
  263. * @param {Function} cb Callback
  264. * @private
  265. */
  266. dispatch(data, compress, options, cb) {
  267. if (!compress) {
  268. this.sendFrame(Sender.frame(data, options), cb);
  269. return;
  270. }
  271. const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
  272. this._deflating = true;
  273. perMessageDeflate.compress(data, options.fin, (_, buf) => {
  274. this._deflating = false;
  275. options.readOnly = false;
  276. this.sendFrame(Sender.frame(buf, options), cb);
  277. this.dequeue();
  278. });
  279. }
  280. /**
  281. * Executes queued send operations.
  282. *
  283. * @private
  284. */
  285. dequeue() {
  286. while (!this._deflating && this._queue.length) {
  287. const params = this._queue.shift();
  288. this._bufferedBytes -= params[1].length;
  289. Reflect.apply(params[0], this, params.slice(1));
  290. }
  291. }
  292. /**
  293. * Enqueues a send operation.
  294. *
  295. * @param {Array} params Send operation parameters.
  296. * @private
  297. */
  298. enqueue(params) {
  299. this._bufferedBytes += params[1].length;
  300. this._queue.push(params);
  301. }
  302. /**
  303. * Sends a frame.
  304. *
  305. * @param {Buffer[]} list The frame to send
  306. * @param {Function} cb Callback
  307. * @private
  308. */
  309. sendFrame(list, cb) {
  310. if (list.length === 2) {
  311. this._socket.cork();
  312. this._socket.write(list[0]);
  313. this._socket.write(list[1], cb);
  314. this._socket.uncork();
  315. } else {
  316. this._socket.write(list[0], cb);
  317. }
  318. }
  319. }
  320. module.exports = Sender;