Node-Red configuration
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.

websocket.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.WebSocket = void 0;
  4. const transport_1 = require("../transport");
  5. const debug_1 = require("debug");
  6. const debug = (0, debug_1.default)("engine:ws");
  7. class WebSocket extends transport_1.Transport {
  8. /**
  9. * WebSocket transport
  10. *
  11. * @param {http.IncomingMessage}
  12. * @api public
  13. */
  14. constructor(req) {
  15. super(req);
  16. this.socket = req.websocket;
  17. this.socket.on("message", (data, isBinary) => {
  18. const message = isBinary ? data : data.toString();
  19. debug('received "%s"', message);
  20. super.onData(message);
  21. });
  22. this.socket.once("close", this.onClose.bind(this));
  23. this.socket.on("error", this.onError.bind(this));
  24. this.writable = true;
  25. this.perMessageDeflate = null;
  26. }
  27. /**
  28. * Transport name
  29. *
  30. * @api public
  31. */
  32. get name() {
  33. return "websocket";
  34. }
  35. /**
  36. * Advertise upgrade support.
  37. *
  38. * @api public
  39. */
  40. get handlesUpgrades() {
  41. return true;
  42. }
  43. /**
  44. * Advertise framing support.
  45. *
  46. * @api public
  47. */
  48. get supportsFraming() {
  49. return true;
  50. }
  51. /**
  52. * Writes a packet payload.
  53. *
  54. * @param {Array} packets
  55. * @api private
  56. */
  57. send(packets) {
  58. this.writable = false;
  59. for (let i = 0; i < packets.length; i++) {
  60. const packet = packets[i];
  61. const isLast = i + 1 === packets.length;
  62. // always creates a new object since ws modifies it
  63. const opts = {};
  64. if (packet.options) {
  65. opts.compress = packet.options.compress;
  66. }
  67. const onSent = (err) => {
  68. if (err) {
  69. return this.onError("write error", err.stack);
  70. }
  71. else if (isLast) {
  72. this.writable = true;
  73. this.emit("drain");
  74. }
  75. };
  76. const send = (data) => {
  77. if (this.perMessageDeflate) {
  78. const len = "string" === typeof data ? Buffer.byteLength(data) : data.length;
  79. if (len < this.perMessageDeflate.threshold) {
  80. opts.compress = false;
  81. }
  82. }
  83. debug('writing "%s"', data);
  84. this.socket.send(data, opts, onSent);
  85. };
  86. if (packet.options && typeof packet.options.wsPreEncoded === "string") {
  87. send(packet.options.wsPreEncoded);
  88. }
  89. else if (this._canSendPreEncodedFrame(packet)) {
  90. // the WebSocket frame was computed with WebSocket.Sender.frame()
  91. // see https://github.com/websockets/ws/issues/617#issuecomment-283002469
  92. this.socket._sender.sendFrame(packet.options.wsPreEncodedFrame, onSent);
  93. }
  94. else {
  95. this.parser.encodePacket(packet, this.supportsBinary, send);
  96. }
  97. }
  98. }
  99. /**
  100. * Whether the encoding of the WebSocket frame can be skipped.
  101. * @param packet
  102. * @private
  103. */
  104. _canSendPreEncodedFrame(packet) {
  105. var _a, _b, _c;
  106. return (!this.perMessageDeflate &&
  107. typeof ((_b = (_a = this.socket) === null || _a === void 0 ? void 0 : _a._sender) === null || _b === void 0 ? void 0 : _b.sendFrame) === "function" &&
  108. ((_c = packet.options) === null || _c === void 0 ? void 0 : _c.wsPreEncodedFrame) !== undefined);
  109. }
  110. /**
  111. * Closes the transport.
  112. *
  113. * @api private
  114. */
  115. doClose(fn) {
  116. debug("closing");
  117. this.socket.close();
  118. fn && fn();
  119. }
  120. }
  121. exports.WebSocket = WebSocket;