- Timestamp:
- 11/25/21 22:08:24 (3 years ago)
- Branches:
- master
- Children:
- 8d391a1
- Parents:
- 59329aa
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trip-planner-front/node_modules/socket.io/dist/socket.js
r59329aa re29cc2e 5 5 Object.defineProperty(exports, "__esModule", { value: true }); 6 6 exports.Socket = exports.RESERVED_EVENTS = void 0; 7 const events_1 = require("events");8 7 const socket_io_parser_1 = require("socket.io-parser"); 9 const url = require("url");10 8 const debug_1 = __importDefault(require("debug")); 9 const typed_events_1 = require("./typed-events"); 11 10 const base64id_1 = __importDefault(require("base64id")); 12 const debug = debug_1.default("socket.io:socket"); 11 const broadcast_operator_1 = require("./broadcast-operator"); 12 const debug = (0, debug_1.default)("socket.io:socket"); 13 13 exports.RESERVED_EVENTS = new Set([ 14 14 "connect", … … 16 16 "disconnect", 17 17 "disconnecting", 18 // EventEmitter reserved events: https://nodejs.org/api/events.html#events_event_newlistener19 18 "newListener", 20 19 "removeListener", 21 20 ]); 22 class Socket extends events_1.EventEmitter {21 class Socket extends typed_events_1.StrictEventEmitter { 23 22 /** 24 23 * Interface to a `Client` for a given `Namespace`. … … 33 32 this.nsp = nsp; 34 33 this.client = client; 34 /** 35 * Additional information that can be attached to the Socket instance and which will be used in the fetchSockets method 36 */ 37 this.data = {}; 38 this.connected = false; 35 39 this.acks = new Map(); 36 40 this.fns = []; 37 41 this.flags = {}; 38 this._rooms = new Set();39 42 this.server = nsp.server; 40 43 this.adapter = this.nsp.adapter; … … 46 49 this.id = base64id_1.default.generateId(); // don't reuse the Engine.IO id because it's sensitive information 47 50 } 48 this.connected = true;49 this.disconnected = false;50 51 this.handshake = this.buildHandshake(auth); 51 52 } … … 65 66 issued: +new Date(), 66 67 url: this.request.url, 67 query: url.parse(this.request.url, true).query, 68 // @ts-ignore 69 query: this.request._query, 68 70 auth, 69 71 }; … … 79 81 throw new Error(`"${ev}" is a reserved event name`); 80 82 } 81 args.unshift(ev);83 const data = [ev, ...args]; 82 84 const packet = { 83 85 type: socket_io_parser_1.PacketType.EVENT, 84 data: args,86 data: data, 85 87 }; 86 88 // access last argument to see if it's an ACK callback 87 if (typeof args[args.length - 1] === "function") { 88 if (this._rooms.size || this.flags.broadcast) { 89 throw new Error("Callbacks are not supported when broadcasting"); 90 } 91 debug("emitting packet with ack id %d", this.nsp._ids); 92 this.acks.set(this.nsp._ids, args.pop()); 93 packet.id = this.nsp._ids++; 94 } 95 const rooms = new Set(this._rooms); 89 if (typeof data[data.length - 1] === "function") { 90 const id = this.nsp._ids++; 91 debug("emitting packet with ack id %d", id); 92 this.registerAckCallback(id, data.pop()); 93 packet.id = id; 94 } 96 95 const flags = Object.assign({}, this.flags); 97 // reset flags98 this._rooms.clear();99 96 this.flags = {}; 100 if (rooms.size || flags.broadcast) { 101 this.adapter.broadcast(packet, { 102 except: new Set([this.id]), 103 rooms: rooms, 104 flags: flags, 105 }); 106 } 107 else { 108 // dispatch packet 109 this.packet(packet, flags); 110 } 97 this.packet(packet, flags); 111 98 return true; 112 99 } 113 100 /** 101 * @private 102 */ 103 registerAckCallback(id, ack) { 104 const timeout = this.flags.timeout; 105 if (timeout === undefined) { 106 this.acks.set(id, ack); 107 return; 108 } 109 const timer = setTimeout(() => { 110 debug("event with ack id %d has timed out after %d ms", id, timeout); 111 this.acks.delete(id); 112 ack.call(this, new Error("operation has timed out")); 113 }, timeout); 114 this.acks.set(id, (...args) => { 115 clearTimeout(timer); 116 ack.apply(this, [null, ...args]); 117 }); 118 } 119 /** 114 120 * Targets a room when broadcasting. 115 121 * 116 * @param name122 * @param room 117 123 * @return self 118 124 * @public 119 125 */ 120 to(name) { 121 this._rooms.add(name); 122 return this; 126 to(room) { 127 return this.newBroadcastOperator().to(room); 123 128 } 124 129 /** 125 130 * Targets a room when broadcasting. 126 131 * 127 * @param name132 * @param room 128 133 * @return self 129 134 * @public 130 135 */ 131 in(name) { 132 this._rooms.add(name); 133 return this; 136 in(room) { 137 return this.newBroadcastOperator().in(room); 138 } 139 /** 140 * Excludes a room when broadcasting. 141 * 142 * @param room 143 * @return self 144 * @public 145 */ 146 except(room) { 147 return this.newBroadcastOperator().except(room); 134 148 } 135 149 /** … … 205 219 _onconnect() { 206 220 debug("socket connected - writing packet"); 221 this.connected = true; 207 222 this.join(this.id); 208 223 if (this.conn.protocol === 3) { … … 317 332 _onerror(err) { 318 333 if (this.listeners("error").length) { 319 super.emit("error", err);334 this.emitReserved("error", err); 320 335 } 321 336 else { … … 336 351 return this; 337 352 debug("closing socket - reason %s", reason); 338 super.emit("disconnecting", reason);353 this.emitReserved("disconnecting", reason); 339 354 this.leaveAll(); 340 355 this.nsp._remove(this); 341 356 this.client._remove(this); 342 357 this.connected = false; 343 this.disconnected = true; 344 super.emit("disconnect", reason); 358 this.emitReserved("disconnect", reason); 345 359 return; 346 360 } … … 406 420 */ 407 421 get broadcast() { 408 this.flags.broadcast = true; 409 return this; 422 return this.newBroadcastOperator(); 410 423 } 411 424 /** … … 416 429 */ 417 430 get local() { 418 this.flags.local = true; 431 return this.newBroadcastOperator().local; 432 } 433 /** 434 * Sets a modifier for a subsequent event emission that the callback will be called with an error when the 435 * given number of milliseconds have elapsed without an acknowledgement from the client: 436 * 437 * ``` 438 * socket.timeout(5000).emit("my-event", (err) => { 439 * if (err) { 440 * // the client did not acknowledge the event in the given delay 441 * } 442 * }); 443 * ``` 444 * 445 * @returns self 446 * @public 447 */ 448 timeout(timeout) { 449 this.flags.timeout = timeout; 419 450 return this; 420 451 } … … 433 464 } 434 465 if (this.connected) { 435 super.emit .apply(this, event);466 super.emitUntyped.apply(this, event); 436 467 } 437 468 else { … … 478 509 } 479 510 /** 511 * Whether the socket is currently disconnected 512 */ 513 get disconnected() { 514 return !this.connected; 515 } 516 /** 480 517 * A reference to the request that originated the underlying Engine.IO Socket. 481 518 * … … 556 593 return this._anyListeners || []; 557 594 } 595 newBroadcastOperator() { 596 const flags = Object.assign({}, this.flags); 597 this.flags = {}; 598 return new broadcast_operator_1.BroadcastOperator(this.adapter, new Set(), new Set([this.id]), flags); 599 } 558 600 } 559 601 exports.Socket = Socket;
Note:
See TracChangeset
for help on using the changeset viewer.