Node-Red configuration
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Client = void 0;
  4. const socket_io_parser_1 = require("socket.io-parser");
  5. const debugModule = require("debug");
  6. const url = require("url");
  7. const debug = debugModule("socket.io:client");
  8. class Client {
  9. /**
  10. * Client constructor.
  11. *
  12. * @param server instance
  13. * @param conn
  14. * @package
  15. */
  16. constructor(server, conn) {
  17. this.sockets = new Map();
  18. this.nsps = new Map();
  19. this.server = server;
  20. this.conn = conn;
  21. this.encoder = server.encoder;
  22. this.decoder = new server._parser.Decoder();
  23. this.id = conn.id;
  24. this.setup();
  25. }
  26. /**
  27. * @return the reference to the request that originated the Engine.IO connection
  28. *
  29. * @public
  30. */
  31. get request() {
  32. return this.conn.request;
  33. }
  34. /**
  35. * Sets up event listeners.
  36. *
  37. * @private
  38. */
  39. setup() {
  40. this.onclose = this.onclose.bind(this);
  41. this.ondata = this.ondata.bind(this);
  42. this.onerror = this.onerror.bind(this);
  43. this.ondecoded = this.ondecoded.bind(this);
  44. // @ts-ignore
  45. this.decoder.on("decoded", this.ondecoded);
  46. this.conn.on("data", this.ondata);
  47. this.conn.on("error", this.onerror);
  48. this.conn.on("close", this.onclose);
  49. this.connectTimeout = setTimeout(() => {
  50. if (this.nsps.size === 0) {
  51. debug("no namespace joined yet, close the client");
  52. this.close();
  53. }
  54. else {
  55. debug("the client has already joined a namespace, nothing to do");
  56. }
  57. }, this.server._connectTimeout);
  58. }
  59. /**
  60. * Connects a client to a namespace.
  61. *
  62. * @param {String} name - the namespace
  63. * @param {Object} auth - the auth parameters
  64. * @private
  65. */
  66. connect(name, auth = {}) {
  67. if (this.server._nsps.has(name)) {
  68. debug("connecting to namespace %s", name);
  69. return this.doConnect(name, auth);
  70. }
  71. this.server._checkNamespace(name, auth, (dynamicNspName) => {
  72. if (dynamicNspName) {
  73. this.doConnect(name, auth);
  74. }
  75. else {
  76. debug("creation of namespace %s was denied", name);
  77. this._packet({
  78. type: socket_io_parser_1.PacketType.CONNECT_ERROR,
  79. nsp: name,
  80. data: {
  81. message: "Invalid namespace",
  82. },
  83. });
  84. }
  85. });
  86. }
  87. /**
  88. * Connects a client to a namespace.
  89. *
  90. * @param name - the namespace
  91. * @param {Object} auth - the auth parameters
  92. *
  93. * @private
  94. */
  95. doConnect(name, auth) {
  96. const nsp = this.server.of(name);
  97. nsp._add(this, auth, (socket) => {
  98. this.sockets.set(socket.id, socket);
  99. this.nsps.set(nsp.name, socket);
  100. if (this.connectTimeout) {
  101. clearTimeout(this.connectTimeout);
  102. this.connectTimeout = undefined;
  103. }
  104. });
  105. }
  106. /**
  107. * Disconnects from all namespaces and closes transport.
  108. *
  109. * @private
  110. */
  111. _disconnect() {
  112. for (const socket of this.sockets.values()) {
  113. socket.disconnect();
  114. }
  115. this.sockets.clear();
  116. this.close();
  117. }
  118. /**
  119. * Removes a socket. Called by each `Socket`.
  120. *
  121. * @private
  122. */
  123. _remove(socket) {
  124. if (this.sockets.has(socket.id)) {
  125. const nsp = this.sockets.get(socket.id).nsp.name;
  126. this.sockets.delete(socket.id);
  127. this.nsps.delete(nsp);
  128. }
  129. else {
  130. debug("ignoring remove for %s", socket.id);
  131. }
  132. }
  133. /**
  134. * Closes the underlying connection.
  135. *
  136. * @private
  137. */
  138. close() {
  139. if ("open" === this.conn.readyState) {
  140. debug("forcing transport close");
  141. this.conn.close();
  142. this.onclose("forced server close");
  143. }
  144. }
  145. /**
  146. * Writes a packet to the transport.
  147. *
  148. * @param {Object} packet object
  149. * @param {Object} opts
  150. * @private
  151. */
  152. _packet(packet, opts = {}) {
  153. if (this.conn.readyState !== "open") {
  154. debug("ignoring packet write %j", packet);
  155. return;
  156. }
  157. const encodedPackets = opts.preEncoded
  158. ? packet // previous versions of the adapter incorrectly used socket.packet() instead of writeToEngine()
  159. : this.encoder.encode(packet);
  160. this.writeToEngine(encodedPackets, opts);
  161. }
  162. writeToEngine(encodedPackets, opts) {
  163. if (opts.volatile && !this.conn.transport.writable) {
  164. debug("volatile packet is discarded since the transport is not currently writable");
  165. return;
  166. }
  167. const packets = Array.isArray(encodedPackets)
  168. ? encodedPackets
  169. : [encodedPackets];
  170. for (const encodedPacket of packets) {
  171. this.conn.write(encodedPacket, opts);
  172. }
  173. }
  174. /**
  175. * Called with incoming transport data.
  176. *
  177. * @private
  178. */
  179. ondata(data) {
  180. // try/catch is needed for protocol violations (GH-1880)
  181. try {
  182. this.decoder.add(data);
  183. }
  184. catch (e) {
  185. debug("invalid packet format");
  186. this.onerror(e);
  187. }
  188. }
  189. /**
  190. * Called when parser fully decodes a packet.
  191. *
  192. * @private
  193. */
  194. ondecoded(packet) {
  195. let namespace;
  196. let authPayload;
  197. if (this.conn.protocol === 3) {
  198. const parsed = url.parse(packet.nsp, true);
  199. namespace = parsed.pathname;
  200. authPayload = parsed.query;
  201. }
  202. else {
  203. namespace = packet.nsp;
  204. authPayload = packet.data;
  205. }
  206. const socket = this.nsps.get(namespace);
  207. if (!socket && packet.type === socket_io_parser_1.PacketType.CONNECT) {
  208. this.connect(namespace, authPayload);
  209. }
  210. else if (socket &&
  211. packet.type !== socket_io_parser_1.PacketType.CONNECT &&
  212. packet.type !== socket_io_parser_1.PacketType.CONNECT_ERROR) {
  213. process.nextTick(function () {
  214. socket._onpacket(packet);
  215. });
  216. }
  217. else {
  218. debug("invalid state (packet type: %s)", packet.type);
  219. this.close();
  220. }
  221. }
  222. /**
  223. * Handles an error.
  224. *
  225. * @param {Object} err object
  226. * @private
  227. */
  228. onerror(err) {
  229. for (const socket of this.sockets.values()) {
  230. socket._onerror(err);
  231. }
  232. this.conn.close();
  233. }
  234. /**
  235. * Called upon transport close.
  236. *
  237. * @param reason
  238. * @param description
  239. * @private
  240. */
  241. onclose(reason, description) {
  242. debug("client close with reason %s", reason);
  243. // ignore a potential subsequent `close` event
  244. this.destroy();
  245. // `nsps` and `sockets` are cleaned up seamlessly
  246. for (const socket of this.sockets.values()) {
  247. socket._onclose(reason, description);
  248. }
  249. this.sockets.clear();
  250. this.decoder.destroy(); // clean up decoder
  251. }
  252. /**
  253. * Cleans up event listeners.
  254. * @private
  255. */
  256. destroy() {
  257. this.conn.removeListener("data", this.ondata);
  258. this.conn.removeListener("error", this.onerror);
  259. this.conn.removeListener("close", this.onclose);
  260. // @ts-ignore
  261. this.decoder.removeListener("decoded", this.ondecoded);
  262. if (this.connectTimeout) {
  263. clearTimeout(this.connectTimeout);
  264. this.connectTimeout = undefined;
  265. }
  266. }
  267. }
  268. exports.Client = Client;