| import { PacketType } from "socket.io-parser"; |
| import { on } from "./on.js"; |
| import { Emitter, } from "@socket.io/component-emitter"; |
| import debugModule from "debug"; |
| const debug = debugModule("socket.io-client:socket"); |
| |
| |
| |
| |
| const RESERVED_EVENTS = Object.freeze({ |
| connect: 1, |
| connect_error: 1, |
| disconnect: 1, |
| disconnecting: 1, |
| |
| newListener: 1, |
| removeListener: 1, |
| }); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export class Socket extends Emitter { |
| |
| |
| |
| constructor(io, nsp, opts) { |
| super(); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| this.connected = false; |
| |
| |
| |
| |
| this.recovered = false; |
| |
| |
| |
| this.receiveBuffer = []; |
| |
| |
| |
| this.sendBuffer = []; |
| |
| |
| |
| |
| |
| |
| this._queue = []; |
| |
| |
| |
| |
| this._queueSeq = 0; |
| this.ids = 0; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| this.acks = {}; |
| this.flags = {}; |
| this.io = io; |
| this.nsp = nsp; |
| if (opts && opts.auth) { |
| this.auth = opts.auth; |
| } |
| this._opts = Object.assign({}, opts); |
| if (this.io._autoConnect) |
| this.open(); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| get disconnected() { |
| return !this.connected; |
| } |
| |
| |
| |
| |
| |
| subEvents() { |
| if (this.subs) |
| return; |
| const io = this.io; |
| this.subs = [ |
| on(io, "open", this.onopen.bind(this)), |
| on(io, "packet", this.onpacket.bind(this)), |
| on(io, "error", this.onerror.bind(this)), |
| on(io, "close", this.onclose.bind(this)), |
| ]; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| get active() { |
| return !!this.subs; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| connect() { |
| if (this.connected) |
| return this; |
| this.subEvents(); |
| if (!this.io["_reconnecting"]) |
| this.io.open(); |
| if ("open" === this.io._readyState) |
| this.onopen(); |
| return this; |
| } |
| |
| |
| |
| open() { |
| return this.connect(); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| send(...args) { |
| args.unshift("message"); |
| this.emit.apply(this, args); |
| return this; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| emit(ev, ...args) { |
| var _a, _b, _c; |
| if (RESERVED_EVENTS.hasOwnProperty(ev)) { |
| throw new Error('"' + ev.toString() + '" is a reserved event name'); |
| } |
| args.unshift(ev); |
| if (this._opts.retries && !this.flags.fromQueue && !this.flags.volatile) { |
| this._addToQueue(args); |
| return this; |
| } |
| const packet = { |
| type: PacketType.EVENT, |
| data: args, |
| }; |
| packet.options = {}; |
| packet.options.compress = this.flags.compress !== false; |
| |
| if ("function" === typeof args[args.length - 1]) { |
| const id = this.ids++; |
| debug("emitting packet with ack id %d", id); |
| const ack = args.pop(); |
| this._registerAckCallback(id, ack); |
| packet.id = id; |
| } |
| const isTransportWritable = (_b = (_a = this.io.engine) === null || _a === void 0 ? void 0 : _a.transport) === null || _b === void 0 ? void 0 : _b.writable; |
| const isConnected = this.connected && !((_c = this.io.engine) === null || _c === void 0 ? void 0 : _c._hasPingExpired()); |
| const discardPacket = this.flags.volatile && !isTransportWritable; |
| if (discardPacket) { |
| debug("discard packet as the transport is not currently writable"); |
| } |
| else if (isConnected) { |
| this.notifyOutgoingListeners(packet); |
| this.packet(packet); |
| } |
| else { |
| this.sendBuffer.push(packet); |
| } |
| this.flags = {}; |
| return this; |
| } |
| |
| |
| |
| _registerAckCallback(id, ack) { |
| var _a; |
| const timeout = (_a = this.flags.timeout) !== null && _a !== void 0 ? _a : this._opts.ackTimeout; |
| if (timeout === undefined) { |
| this.acks[id] = ack; |
| return; |
| } |
| |
| const timer = this.io.setTimeoutFn(() => { |
| delete this.acks[id]; |
| for (let i = 0; i < this.sendBuffer.length; i++) { |
| if (this.sendBuffer[i].id === id) { |
| debug("removing packet with ack id %d from the buffer", id); |
| this.sendBuffer.splice(i, 1); |
| } |
| } |
| debug("event with ack id %d has timed out after %d ms", id, timeout); |
| ack.call(this, new Error("operation has timed out")); |
| }, timeout); |
| const fn = (...args) => { |
| |
| this.io.clearTimeoutFn(timer); |
| ack.apply(this, args); |
| }; |
| fn.withError = true; |
| this.acks[id] = fn; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| emitWithAck(ev, ...args) { |
| return new Promise((resolve, reject) => { |
| const fn = (arg1, arg2) => { |
| return arg1 ? reject(arg1) : resolve(arg2); |
| }; |
| fn.withError = true; |
| args.push(fn); |
| this.emit(ev, ...args); |
| }); |
| } |
| |
| |
| |
| |
| |
| _addToQueue(args) { |
| let ack; |
| if (typeof args[args.length - 1] === "function") { |
| ack = args.pop(); |
| } |
| const packet = { |
| id: this._queueSeq++, |
| tryCount: 0, |
| pending: false, |
| args, |
| flags: Object.assign({ fromQueue: true }, this.flags), |
| }; |
| args.push((err, ...responseArgs) => { |
| if (packet !== this._queue[0]) { |
| return debug("packet [%d] already acknowledged", packet.id); |
| } |
| const hasError = err !== null; |
| if (hasError) { |
| if (packet.tryCount > this._opts.retries) { |
| debug("packet [%d] is discarded after %d tries", packet.id, packet.tryCount); |
| this._queue.shift(); |
| if (ack) { |
| ack(err); |
| } |
| } |
| } |
| else { |
| debug("packet [%d] was successfully sent", packet.id); |
| this._queue.shift(); |
| if (ack) { |
| ack(null, ...responseArgs); |
| } |
| } |
| packet.pending = false; |
| return this._drainQueue(); |
| }); |
| this._queue.push(packet); |
| this._drainQueue(); |
| } |
| |
| |
| |
| |
| |
| |
| _drainQueue(force = false) { |
| debug("draining queue"); |
| if (!this.connected || this._queue.length === 0) { |
| return; |
| } |
| const packet = this._queue[0]; |
| if (packet.pending && !force) { |
| debug("packet [%d] has already been sent and is waiting for an ack", packet.id); |
| return; |
| } |
| packet.pending = true; |
| packet.tryCount++; |
| debug("sending packet [%d] (try n°%d)", packet.id, packet.tryCount); |
| this.flags = packet.flags; |
| this.emit.apply(this, packet.args); |
| } |
| |
| |
| |
| |
| |
| |
| packet(packet) { |
| packet.nsp = this.nsp; |
| this.io._packet(packet); |
| } |
| |
| |
| |
| |
| |
| onopen() { |
| debug("transport is open - connecting"); |
| if (typeof this.auth == "function") { |
| this.auth((data) => { |
| this._sendConnectPacket(data); |
| }); |
| } |
| else { |
| this._sendConnectPacket(this.auth); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| _sendConnectPacket(data) { |
| this.packet({ |
| type: PacketType.CONNECT, |
| data: this._pid |
| ? Object.assign({ pid: this._pid, offset: this._lastOffset }, data) |
| : data, |
| }); |
| } |
| |
| |
| |
| |
| |
| |
| onerror(err) { |
| if (!this.connected) { |
| this.emitReserved("connect_error", err); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| onclose(reason, description) { |
| debug("close (%s)", reason); |
| this.connected = false; |
| delete this.id; |
| this.emitReserved("disconnect", reason, description); |
| this._clearAcks(); |
| } |
| |
| |
| |
| |
| |
| |
| _clearAcks() { |
| Object.keys(this.acks).forEach((id) => { |
| const isBuffered = this.sendBuffer.some((packet) => String(packet.id) === id); |
| if (!isBuffered) { |
| |
| const ack = this.acks[id]; |
| delete this.acks[id]; |
| if (ack.withError) { |
| ack.call(this, new Error("socket has been disconnected")); |
| } |
| } |
| }); |
| } |
| |
| |
| |
| |
| |
| |
| onpacket(packet) { |
| const sameNamespace = packet.nsp === this.nsp; |
| if (!sameNamespace) |
| return; |
| switch (packet.type) { |
| case PacketType.CONNECT: |
| if (packet.data && packet.data.sid) { |
| this.onconnect(packet.data.sid, packet.data.pid); |
| } |
| else { |
| this.emitReserved("connect_error", new Error("It seems you are trying to reach a Socket.IO server in v2.x with a v3.x client, but they are not compatible (more information here: https://socket.io/docs/v3/migrating-from-2-x-to-3-0/)")); |
| } |
| break; |
| case PacketType.EVENT: |
| case PacketType.BINARY_EVENT: |
| this.onevent(packet); |
| break; |
| case PacketType.ACK: |
| case PacketType.BINARY_ACK: |
| this.onack(packet); |
| break; |
| case PacketType.DISCONNECT: |
| this.ondisconnect(); |
| break; |
| case PacketType.CONNECT_ERROR: |
| this.destroy(); |
| const err = new Error(packet.data.message); |
| |
| err.data = packet.data.data; |
| this.emitReserved("connect_error", err); |
| break; |
| } |
| } |
| |
| |
| |
| |
| |
| |
| onevent(packet) { |
| const args = packet.data || []; |
| debug("emitting event %j", args); |
| if (null != packet.id) { |
| debug("attaching ack callback to event"); |
| args.push(this.ack(packet.id)); |
| } |
| if (this.connected) { |
| this.emitEvent(args); |
| } |
| else { |
| this.receiveBuffer.push(Object.freeze(args)); |
| } |
| } |
| emitEvent(args) { |
| if (this._anyListeners && this._anyListeners.length) { |
| const listeners = this._anyListeners.slice(); |
| for (const listener of listeners) { |
| listener.apply(this, args); |
| } |
| } |
| super.emit.apply(this, args); |
| if (this._pid && args.length && typeof args[args.length - 1] === "string") { |
| this._lastOffset = args[args.length - 1]; |
| } |
| } |
| |
| |
| |
| |
| |
| ack(id) { |
| const self = this; |
| let sent = false; |
| return function (...args) { |
| |
| if (sent) |
| return; |
| sent = true; |
| debug("sending ack %j", args); |
| self.packet({ |
| type: PacketType.ACK, |
| id: id, |
| data: args, |
| }); |
| }; |
| } |
| |
| |
| |
| |
| |
| |
| onack(packet) { |
| const ack = this.acks[packet.id]; |
| if (typeof ack !== "function") { |
| debug("bad ack %s", packet.id); |
| return; |
| } |
| delete this.acks[packet.id]; |
| debug("calling ack %s with %j", packet.id, packet.data); |
| |
| if (ack.withError) { |
| packet.data.unshift(null); |
| } |
| |
| ack.apply(this, packet.data); |
| } |
| |
| |
| |
| |
| |
| onconnect(id, pid) { |
| debug("socket connected with id %s", id); |
| this.id = id; |
| this.recovered = pid && this._pid === pid; |
| this._pid = pid; |
| this.connected = true; |
| this.emitBuffered(); |
| this._drainQueue(true); |
| this.emitReserved("connect"); |
| } |
| |
| |
| |
| |
| |
| emitBuffered() { |
| this.receiveBuffer.forEach((args) => this.emitEvent(args)); |
| this.receiveBuffer = []; |
| this.sendBuffer.forEach((packet) => { |
| this.notifyOutgoingListeners(packet); |
| this.packet(packet); |
| }); |
| this.sendBuffer = []; |
| } |
| |
| |
| |
| |
| |
| ondisconnect() { |
| debug("server disconnect (%s)", this.nsp); |
| this.destroy(); |
| this.onclose("io server disconnect"); |
| } |
| |
| |
| |
| |
| |
| |
| |
| destroy() { |
| if (this.subs) { |
| |
| this.subs.forEach((subDestroy) => subDestroy()); |
| this.subs = undefined; |
| } |
| this.io["_destroy"](this); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| disconnect() { |
| if (this.connected) { |
| debug("performing disconnect (%s)", this.nsp); |
| this.packet({ type: PacketType.DISCONNECT }); |
| } |
| |
| this.destroy(); |
| if (this.connected) { |
| |
| this.onclose("io client disconnect"); |
| } |
| return this; |
| } |
| |
| |
| |
| |
| |
| close() { |
| return this.disconnect(); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| compress(compress) { |
| this.flags.compress = compress; |
| return this; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| get volatile() { |
| this.flags.volatile = true; |
| return this; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| timeout(timeout) { |
| this.flags.timeout = timeout; |
| return this; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| onAny(listener) { |
| this._anyListeners = this._anyListeners || []; |
| this._anyListeners.push(listener); |
| return this; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| prependAny(listener) { |
| this._anyListeners = this._anyListeners || []; |
| this._anyListeners.unshift(listener); |
| return this; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| offAny(listener) { |
| if (!this._anyListeners) { |
| return this; |
| } |
| if (listener) { |
| const listeners = this._anyListeners; |
| for (let i = 0; i < listeners.length; i++) { |
| if (listener === listeners[i]) { |
| listeners.splice(i, 1); |
| return this; |
| } |
| } |
| } |
| else { |
| this._anyListeners = []; |
| } |
| return this; |
| } |
| |
| |
| |
| |
| listenersAny() { |
| return this._anyListeners || []; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| onAnyOutgoing(listener) { |
| this._anyOutgoingListeners = this._anyOutgoingListeners || []; |
| this._anyOutgoingListeners.push(listener); |
| return this; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| prependAnyOutgoing(listener) { |
| this._anyOutgoingListeners = this._anyOutgoingListeners || []; |
| this._anyOutgoingListeners.unshift(listener); |
| return this; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| offAnyOutgoing(listener) { |
| if (!this._anyOutgoingListeners) { |
| return this; |
| } |
| if (listener) { |
| const listeners = this._anyOutgoingListeners; |
| for (let i = 0; i < listeners.length; i++) { |
| if (listener === listeners[i]) { |
| listeners.splice(i, 1); |
| return this; |
| } |
| } |
| } |
| else { |
| this._anyOutgoingListeners = []; |
| } |
| return this; |
| } |
| |
| |
| |
| |
| listenersAnyOutgoing() { |
| return this._anyOutgoingListeners || []; |
| } |
| |
| |
| |
| |
| |
| |
| |
| notifyOutgoingListeners(packet) { |
| if (this._anyOutgoingListeners && this._anyOutgoingListeners.length) { |
| const listeners = this._anyOutgoingListeners.slice(); |
| for (const listener of listeners) { |
| listener.apply(this, packet.data); |
| } |
| } |
| } |
| } |
|
|