index.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Adapter = void 0;
  4. const events_1 = require("events");
  5. class Adapter extends events_1.EventEmitter {
  6. /**
  7. * In-memory adapter constructor.
  8. *
  9. * @param {Namespace} nsp
  10. */
  11. constructor(nsp) {
  12. super();
  13. this.nsp = nsp;
  14. this.rooms = new Map();
  15. this.sids = new Map();
  16. this.encoder = nsp.server.encoder;
  17. }
  18. /**
  19. * To be overridden
  20. */
  21. init() { }
  22. /**
  23. * To be overridden
  24. */
  25. close() { }
  26. /**
  27. * Adds a socket to a list of room.
  28. *
  29. * @param {SocketId} id the socket id
  30. * @param {Set<Room>} rooms a set of rooms
  31. * @public
  32. */
  33. addAll(id, rooms) {
  34. if (!this.sids.has(id)) {
  35. this.sids.set(id, new Set());
  36. }
  37. for (const room of rooms) {
  38. this.sids.get(id).add(room);
  39. if (!this.rooms.has(room)) {
  40. this.rooms.set(room, new Set());
  41. this.emit("create-room", room);
  42. }
  43. if (!this.rooms.get(room).has(id)) {
  44. this.rooms.get(room).add(id);
  45. this.emit("join-room", room, id);
  46. }
  47. }
  48. }
  49. /**
  50. * Removes a socket from a room.
  51. *
  52. * @param {SocketId} id the socket id
  53. * @param {Room} room the room name
  54. */
  55. del(id, room) {
  56. if (this.sids.has(id)) {
  57. this.sids.get(id).delete(room);
  58. }
  59. this._del(room, id);
  60. }
  61. _del(room, id) {
  62. const _room = this.rooms.get(room);
  63. if (_room != null) {
  64. const deleted = _room.delete(id);
  65. if (deleted) {
  66. this.emit("leave-room", room, id);
  67. }
  68. if (_room.size === 0 && this.rooms.delete(room)) {
  69. this.emit("delete-room", room);
  70. }
  71. }
  72. }
  73. /**
  74. * Removes a socket from all rooms it's joined.
  75. *
  76. * @param {SocketId} id the socket id
  77. */
  78. delAll(id) {
  79. if (!this.sids.has(id)) {
  80. return;
  81. }
  82. for (const room of this.sids.get(id)) {
  83. this._del(room, id);
  84. }
  85. this.sids.delete(id);
  86. }
  87. /**
  88. * Broadcasts a packet.
  89. *
  90. * Options:
  91. * - `flags` {Object} flags for this packet
  92. * - `except` {Array} sids that should be excluded
  93. * - `rooms` {Array} list of rooms to broadcast to
  94. *
  95. * @param {Object} packet the packet object
  96. * @param {Object} opts the options
  97. * @public
  98. */
  99. broadcast(packet, opts) {
  100. const flags = opts.flags || {};
  101. const packetOpts = {
  102. preEncoded: true,
  103. volatile: flags.volatile,
  104. compress: flags.compress
  105. };
  106. packet.nsp = this.nsp.name;
  107. const encodedPackets = this.encoder.encode(packet);
  108. this.apply(opts, socket => {
  109. socket.client.writeToEngine(encodedPackets, packetOpts);
  110. });
  111. }
  112. /**
  113. * Gets a list of sockets by sid.
  114. *
  115. * @param {Set<Room>} rooms the explicit set of rooms to check.
  116. */
  117. sockets(rooms) {
  118. const sids = new Set();
  119. this.apply({ rooms }, socket => {
  120. sids.add(socket.id);
  121. });
  122. return Promise.resolve(sids);
  123. }
  124. /**
  125. * Gets the list of rooms a given socket has joined.
  126. *
  127. * @param {SocketId} id the socket id
  128. */
  129. socketRooms(id) {
  130. return this.sids.get(id);
  131. }
  132. /**
  133. * Returns the matching socket instances
  134. *
  135. * @param opts - the filters to apply
  136. */
  137. fetchSockets(opts) {
  138. const sockets = [];
  139. this.apply(opts, socket => {
  140. sockets.push(socket);
  141. });
  142. return Promise.resolve(sockets);
  143. }
  144. /**
  145. * Makes the matching socket instances join the specified rooms
  146. *
  147. * @param opts - the filters to apply
  148. * @param rooms - the rooms to join
  149. */
  150. addSockets(opts, rooms) {
  151. this.apply(opts, socket => {
  152. socket.join(rooms);
  153. });
  154. }
  155. /**
  156. * Makes the matching socket instances leave the specified rooms
  157. *
  158. * @param opts - the filters to apply
  159. * @param rooms - the rooms to leave
  160. */
  161. delSockets(opts, rooms) {
  162. this.apply(opts, socket => {
  163. rooms.forEach(room => socket.leave(room));
  164. });
  165. }
  166. /**
  167. * Makes the matching socket instances disconnect
  168. *
  169. * @param opts - the filters to apply
  170. * @param close - whether to close the underlying connection
  171. */
  172. disconnectSockets(opts, close) {
  173. this.apply(opts, socket => {
  174. socket.disconnect(close);
  175. });
  176. }
  177. apply(opts, callback) {
  178. const rooms = opts.rooms;
  179. const except = this.computeExceptSids(opts.except);
  180. if (rooms.size) {
  181. const ids = new Set();
  182. for (const room of rooms) {
  183. if (!this.rooms.has(room))
  184. continue;
  185. for (const id of this.rooms.get(room)) {
  186. if (ids.has(id) || except.has(id))
  187. continue;
  188. const socket = this.nsp.sockets.get(id);
  189. if (socket) {
  190. callback(socket);
  191. ids.add(id);
  192. }
  193. }
  194. }
  195. }
  196. else {
  197. for (const [id] of this.sids) {
  198. if (except.has(id))
  199. continue;
  200. const socket = this.nsp.sockets.get(id);
  201. if (socket)
  202. callback(socket);
  203. }
  204. }
  205. }
  206. computeExceptSids(exceptRooms) {
  207. const exceptSids = new Set();
  208. if (exceptRooms && exceptRooms.size > 0) {
  209. for (const room of exceptRooms) {
  210. if (this.rooms.has(room)) {
  211. this.rooms.get(room).forEach(sid => exceptSids.add(sid));
  212. }
  213. }
  214. }
  215. return exceptSids;
  216. }
  217. /**
  218. * Send a packet to the other Socket.IO servers in the cluster
  219. * @param packet - an array of arguments, which may include an acknowledgement callback at the end
  220. */
  221. serverSideEmit(packet) {
  222. throw new Error("this adapter does not support the serverSideEmit() functionality");
  223. }
  224. }
  225. exports.Adapter = Adapter;