Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sender.js 10KB

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