| "use strict"; |
| var __importDefault = (this && this.__importDefault) || function (mod) { |
| return (mod && mod.__esModule) ? mod : { "default": mod }; |
| }; |
| Object.defineProperty(exports, "__esModule", { value: true }); |
| exports.Socket = exports.SocketWithUpgrade = exports.SocketWithoutUpgrade = void 0; |
| const index_js_1 = require("./transports/index.js"); |
| const util_js_1 = require("./util.js"); |
| const parseqs_js_1 = require("./contrib/parseqs.js"); |
| const parseuri_js_1 = require("./contrib/parseuri.js"); |
| const component_emitter_1 = require("@socket.io/component-emitter"); |
| const engine_io_parser_1 = require("engine.io-parser"); |
| const globals_node_js_1 = require("./globals.node.js"); |
| const debug_1 = __importDefault(require("debug")); |
| const debug = (0, debug_1.default)("engine.io-client:socket"); |
| const withEventListeners = typeof addEventListener === "function" && |
| typeof removeEventListener === "function"; |
| const OFFLINE_EVENT_LISTENERS = []; |
| if (withEventListeners) { |
| |
| |
| addEventListener("offline", () => { |
| debug("closing %d connection(s) because the network was lost", OFFLINE_EVENT_LISTENERS.length); |
| OFFLINE_EVENT_LISTENERS.forEach((listener) => listener()); |
| }, false); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| class SocketWithoutUpgrade extends component_emitter_1.Emitter { |
| |
| |
| |
| |
| |
| |
| constructor(uri, opts) { |
| super(); |
| this.binaryType = globals_node_js_1.defaultBinaryType; |
| this.writeBuffer = []; |
| this._prevBufferLen = 0; |
| this._pingInterval = -1; |
| this._pingTimeout = -1; |
| this._maxPayload = -1; |
| |
| |
| |
| |
| this._pingTimeoutTime = Infinity; |
| if (uri && "object" === typeof uri) { |
| opts = uri; |
| uri = null; |
| } |
| if (uri) { |
| const parsedUri = (0, parseuri_js_1.parse)(uri); |
| opts.hostname = parsedUri.host; |
| opts.secure = |
| parsedUri.protocol === "https" || parsedUri.protocol === "wss"; |
| opts.port = parsedUri.port; |
| if (parsedUri.query) |
| opts.query = parsedUri.query; |
| } |
| else if (opts.host) { |
| opts.hostname = (0, parseuri_js_1.parse)(opts.host).host; |
| } |
| (0, util_js_1.installTimerFunctions)(this, opts); |
| this.secure = |
| null != opts.secure |
| ? opts.secure |
| : typeof location !== "undefined" && "https:" === location.protocol; |
| if (opts.hostname && !opts.port) { |
| |
| opts.port = this.secure ? "443" : "80"; |
| } |
| this.hostname = |
| opts.hostname || |
| (typeof location !== "undefined" ? location.hostname : "localhost"); |
| this.port = |
| opts.port || |
| (typeof location !== "undefined" && location.port |
| ? location.port |
| : this.secure |
| ? "443" |
| : "80"); |
| this.transports = []; |
| this._transportsByName = {}; |
| opts.transports.forEach((t) => { |
| const transportName = t.prototype.name; |
| this.transports.push(transportName); |
| this._transportsByName[transportName] = t; |
| }); |
| this.opts = Object.assign({ |
| path: "/engine.io", |
| agent: false, |
| withCredentials: false, |
| upgrade: true, |
| timestampParam: "t", |
| rememberUpgrade: false, |
| addTrailingSlash: true, |
| rejectUnauthorized: true, |
| perMessageDeflate: { |
| threshold: 1024, |
| }, |
| transportOptions: {}, |
| closeOnBeforeunload: false, |
| }, opts); |
| this.opts.path = |
| this.opts.path.replace(/\/$/, "") + |
| (this.opts.addTrailingSlash ? "/" : ""); |
| if (typeof this.opts.query === "string") { |
| this.opts.query = (0, parseqs_js_1.decode)(this.opts.query); |
| } |
| if (withEventListeners) { |
| if (this.opts.closeOnBeforeunload) { |
| |
| |
| |
| this._beforeunloadEventListener = () => { |
| if (this.transport) { |
| |
| this.transport.removeAllListeners(); |
| this.transport.close(); |
| } |
| }; |
| addEventListener("beforeunload", this._beforeunloadEventListener, false); |
| } |
| if (this.hostname !== "localhost") { |
| debug("adding listener for the 'offline' event"); |
| this._offlineEventListener = () => { |
| this._onClose("transport close", { |
| description: "network connection lost", |
| }); |
| }; |
| OFFLINE_EVENT_LISTENERS.push(this._offlineEventListener); |
| } |
| } |
| if (this.opts.withCredentials) { |
| this._cookieJar = (0, globals_node_js_1.createCookieJar)(); |
| } |
| this._open(); |
| } |
| |
| |
| |
| |
| |
| |
| |
| createTransport(name) { |
| debug('creating transport "%s"', name); |
| const query = Object.assign({}, this.opts.query); |
| |
| query.EIO = engine_io_parser_1.protocol; |
| |
| query.transport = name; |
| |
| if (this.id) |
| query.sid = this.id; |
| const opts = Object.assign({}, this.opts, { |
| query, |
| socket: this, |
| hostname: this.hostname, |
| secure: this.secure, |
| port: this.port, |
| }, this.opts.transportOptions[name]); |
| debug("options: %j", opts); |
| return new this._transportsByName[name](opts); |
| } |
| |
| |
| |
| |
| |
| _open() { |
| if (this.transports.length === 0) { |
| |
| this.setTimeoutFn(() => { |
| this.emitReserved("error", "No transports available"); |
| }, 0); |
| return; |
| } |
| const transportName = this.opts.rememberUpgrade && |
| SocketWithoutUpgrade.priorWebsocketSuccess && |
| this.transports.indexOf("websocket") !== -1 |
| ? "websocket" |
| : this.transports[0]; |
| this.readyState = "opening"; |
| const transport = this.createTransport(transportName); |
| transport.open(); |
| this.setTransport(transport); |
| } |
| |
| |
| |
| |
| |
| setTransport(transport) { |
| debug("setting transport %s", transport.name); |
| if (this.transport) { |
| debug("clearing existing transport %s", this.transport.name); |
| this.transport.removeAllListeners(); |
| } |
| |
| this.transport = transport; |
| |
| transport |
| .on("drain", this._onDrain.bind(this)) |
| .on("packet", this._onPacket.bind(this)) |
| .on("error", this._onError.bind(this)) |
| .on("close", (reason) => this._onClose("transport close", reason)); |
| } |
| |
| |
| |
| |
| |
| onOpen() { |
| debug("socket open"); |
| this.readyState = "open"; |
| SocketWithoutUpgrade.priorWebsocketSuccess = |
| "websocket" === this.transport.name; |
| this.emitReserved("open"); |
| this.flush(); |
| } |
| |
| |
| |
| |
| |
| _onPacket(packet) { |
| if ("opening" === this.readyState || |
| "open" === this.readyState || |
| "closing" === this.readyState) { |
| debug('socket receive: type "%s", data "%s"', packet.type, packet.data); |
| this.emitReserved("packet", packet); |
| |
| this.emitReserved("heartbeat"); |
| switch (packet.type) { |
| case "open": |
| this.onHandshake(JSON.parse(packet.data)); |
| break; |
| case "ping": |
| this._sendPacket("pong"); |
| this.emitReserved("ping"); |
| this.emitReserved("pong"); |
| this._resetPingTimeout(); |
| break; |
| case "error": |
| const err = new Error("server error"); |
| |
| err.code = packet.data; |
| this._onError(err); |
| break; |
| case "message": |
| this.emitReserved("data", packet.data); |
| this.emitReserved("message", packet.data); |
| break; |
| } |
| } |
| else { |
| debug('packet received with socket readyState "%s"', this.readyState); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| onHandshake(data) { |
| this.emitReserved("handshake", data); |
| this.id = data.sid; |
| this.transport.query.sid = data.sid; |
| this._pingInterval = data.pingInterval; |
| this._pingTimeout = data.pingTimeout; |
| this._maxPayload = data.maxPayload; |
| this.onOpen(); |
| |
| if ("closed" === this.readyState) |
| return; |
| this._resetPingTimeout(); |
| } |
| |
| |
| |
| |
| |
| _resetPingTimeout() { |
| this.clearTimeoutFn(this._pingTimeoutTimer); |
| const delay = this._pingInterval + this._pingTimeout; |
| this._pingTimeoutTime = Date.now() + delay; |
| this._pingTimeoutTimer = this.setTimeoutFn(() => { |
| this._onClose("ping timeout"); |
| }, delay); |
| if (this.opts.autoUnref) { |
| this._pingTimeoutTimer.unref(); |
| } |
| } |
| |
| |
| |
| |
| |
| _onDrain() { |
| this.writeBuffer.splice(0, this._prevBufferLen); |
| |
| |
| |
| this._prevBufferLen = 0; |
| if (0 === this.writeBuffer.length) { |
| this.emitReserved("drain"); |
| } |
| else { |
| this.flush(); |
| } |
| } |
| |
| |
| |
| |
| |
| flush() { |
| if ("closed" !== this.readyState && |
| this.transport.writable && |
| !this.upgrading && |
| this.writeBuffer.length) { |
| const packets = this._getWritablePackets(); |
| debug("flushing %d packets in socket", packets.length); |
| this.transport.send(packets); |
| |
| |
| this._prevBufferLen = packets.length; |
| this.emitReserved("flush"); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| _getWritablePackets() { |
| const shouldCheckPayloadSize = this._maxPayload && |
| this.transport.name === "polling" && |
| this.writeBuffer.length > 1; |
| if (!shouldCheckPayloadSize) { |
| return this.writeBuffer; |
| } |
| let payloadSize = 1; |
| for (let i = 0; i < this.writeBuffer.length; i++) { |
| const data = this.writeBuffer[i].data; |
| if (data) { |
| payloadSize += (0, util_js_1.byteLength)(data); |
| } |
| if (i > 0 && payloadSize > this._maxPayload) { |
| debug("only send %d out of %d packets", i, this.writeBuffer.length); |
| return this.writeBuffer.slice(0, i); |
| } |
| payloadSize += 2; |
| } |
| debug("payload size is %d (max: %d)", payloadSize, this._maxPayload); |
| return this.writeBuffer; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| _hasPingExpired() { |
| if (!this._pingTimeoutTime) |
| return true; |
| const hasExpired = Date.now() > this._pingTimeoutTime; |
| if (hasExpired) { |
| debug("throttled timer detected, scheduling connection close"); |
| this._pingTimeoutTime = 0; |
| (0, globals_node_js_1.nextTick)(() => { |
| this._onClose("ping timeout"); |
| }, this.setTimeoutFn); |
| } |
| return hasExpired; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| write(msg, options, fn) { |
| this._sendPacket("message", msg, options, fn); |
| return this; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| send(msg, options, fn) { |
| this._sendPacket("message", msg, options, fn); |
| return this; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| _sendPacket(type, data, options, fn) { |
| if ("function" === typeof data) { |
| fn = data; |
| data = undefined; |
| } |
| if ("function" === typeof options) { |
| fn = options; |
| options = null; |
| } |
| if ("closing" === this.readyState || "closed" === this.readyState) { |
| return; |
| } |
| options = options || {}; |
| options.compress = false !== options.compress; |
| const packet = { |
| type: type, |
| data: data, |
| options: options, |
| }; |
| this.emitReserved("packetCreate", packet); |
| this.writeBuffer.push(packet); |
| if (fn) |
| this.once("flush", fn); |
| this.flush(); |
| } |
| |
| |
| |
| close() { |
| const close = () => { |
| this._onClose("forced close"); |
| debug("socket closing - telling transport to close"); |
| this.transport.close(); |
| }; |
| const cleanupAndClose = () => { |
| this.off("upgrade", cleanupAndClose); |
| this.off("upgradeError", cleanupAndClose); |
| close(); |
| }; |
| const waitForUpgrade = () => { |
| |
| this.once("upgrade", cleanupAndClose); |
| this.once("upgradeError", cleanupAndClose); |
| }; |
| if ("opening" === this.readyState || "open" === this.readyState) { |
| this.readyState = "closing"; |
| if (this.writeBuffer.length) { |
| this.once("drain", () => { |
| if (this.upgrading) { |
| waitForUpgrade(); |
| } |
| else { |
| close(); |
| } |
| }); |
| } |
| else if (this.upgrading) { |
| waitForUpgrade(); |
| } |
| else { |
| close(); |
| } |
| } |
| return this; |
| } |
| |
| |
| |
| |
| |
| _onError(err) { |
| debug("socket error %j", err); |
| SocketWithoutUpgrade.priorWebsocketSuccess = false; |
| if (this.opts.tryAllTransports && |
| this.transports.length > 1 && |
| this.readyState === "opening") { |
| debug("trying next transport"); |
| this.transports.shift(); |
| return this._open(); |
| } |
| this.emitReserved("error", err); |
| this._onClose("transport error", err); |
| } |
| |
| |
| |
| |
| |
| _onClose(reason, description) { |
| if ("opening" === this.readyState || |
| "open" === this.readyState || |
| "closing" === this.readyState) { |
| debug('socket close with reason: "%s"', reason); |
| |
| this.clearTimeoutFn(this._pingTimeoutTimer); |
| |
| this.transport.removeAllListeners("close"); |
| |
| this.transport.close(); |
| |
| this.transport.removeAllListeners(); |
| if (withEventListeners) { |
| if (this._beforeunloadEventListener) { |
| removeEventListener("beforeunload", this._beforeunloadEventListener, false); |
| } |
| if (this._offlineEventListener) { |
| const i = OFFLINE_EVENT_LISTENERS.indexOf(this._offlineEventListener); |
| if (i !== -1) { |
| debug("removing listener for the 'offline' event"); |
| OFFLINE_EVENT_LISTENERS.splice(i, 1); |
| } |
| } |
| } |
| |
| this.readyState = "closed"; |
| |
| this.id = null; |
| |
| this.emitReserved("close", reason, description); |
| |
| |
| this.writeBuffer = []; |
| this._prevBufferLen = 0; |
| } |
| } |
| } |
| exports.SocketWithoutUpgrade = SocketWithoutUpgrade; |
| SocketWithoutUpgrade.protocol = engine_io_parser_1.protocol; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| class SocketWithUpgrade extends SocketWithoutUpgrade { |
| constructor() { |
| super(...arguments); |
| this._upgrades = []; |
| } |
| onOpen() { |
| super.onOpen(); |
| if ("open" === this.readyState && this.opts.upgrade) { |
| debug("starting upgrade probes"); |
| for (let i = 0; i < this._upgrades.length; i++) { |
| this._probe(this._upgrades[i]); |
| } |
| } |
| } |
| |
| |
| |
| |
| |
| |
| _probe(name) { |
| debug('probing transport "%s"', name); |
| let transport = this.createTransport(name); |
| let failed = false; |
| SocketWithoutUpgrade.priorWebsocketSuccess = false; |
| const onTransportOpen = () => { |
| if (failed) |
| return; |
| debug('probe transport "%s" opened', name); |
| transport.send([{ type: "ping", data: "probe" }]); |
| transport.once("packet", (msg) => { |
| if (failed) |
| return; |
| if ("pong" === msg.type && "probe" === msg.data) { |
| debug('probe transport "%s" pong', name); |
| this.upgrading = true; |
| this.emitReserved("upgrading", transport); |
| if (!transport) |
| return; |
| SocketWithoutUpgrade.priorWebsocketSuccess = |
| "websocket" === transport.name; |
| debug('pausing current transport "%s"', this.transport.name); |
| this.transport.pause(() => { |
| if (failed) |
| return; |
| if ("closed" === this.readyState) |
| return; |
| debug("changing transport and sending upgrade packet"); |
| cleanup(); |
| this.setTransport(transport); |
| transport.send([{ type: "upgrade" }]); |
| this.emitReserved("upgrade", transport); |
| transport = null; |
| this.upgrading = false; |
| this.flush(); |
| }); |
| } |
| else { |
| debug('probe transport "%s" failed', name); |
| const err = new Error("probe error"); |
| |
| err.transport = transport.name; |
| this.emitReserved("upgradeError", err); |
| } |
| }); |
| }; |
| function freezeTransport() { |
| if (failed) |
| return; |
| |
| failed = true; |
| cleanup(); |
| transport.close(); |
| transport = null; |
| } |
| |
| const onerror = (err) => { |
| const error = new Error("probe error: " + err); |
| |
| error.transport = transport.name; |
| freezeTransport(); |
| debug('probe transport "%s" failed because of error: %s', name, err); |
| this.emitReserved("upgradeError", error); |
| }; |
| function onTransportClose() { |
| onerror("transport closed"); |
| } |
| |
| function onclose() { |
| onerror("socket closed"); |
| } |
| |
| function onupgrade(to) { |
| if (transport && to.name !== transport.name) { |
| debug('"%s" works - aborting "%s"', to.name, transport.name); |
| freezeTransport(); |
| } |
| } |
| |
| const cleanup = () => { |
| transport.removeListener("open", onTransportOpen); |
| transport.removeListener("error", onerror); |
| transport.removeListener("close", onTransportClose); |
| this.off("close", onclose); |
| this.off("upgrading", onupgrade); |
| }; |
| transport.once("open", onTransportOpen); |
| transport.once("error", onerror); |
| transport.once("close", onTransportClose); |
| this.once("close", onclose); |
| this.once("upgrading", onupgrade); |
| if (this._upgrades.indexOf("webtransport") !== -1 && |
| name !== "webtransport") { |
| |
| this.setTimeoutFn(() => { |
| if (!failed) { |
| transport.open(); |
| } |
| }, 200); |
| } |
| else { |
| transport.open(); |
| } |
| } |
| onHandshake(data) { |
| this._upgrades = this._filterUpgrades(data.upgrades); |
| super.onHandshake(data); |
| } |
| |
| |
| |
| |
| |
| |
| _filterUpgrades(upgrades) { |
| const filteredUpgrades = []; |
| for (let i = 0; i < upgrades.length; i++) { |
| if (~this.transports.indexOf(upgrades[i])) |
| filteredUpgrades.push(upgrades[i]); |
| } |
| return filteredUpgrades; |
| } |
| } |
| exports.SocketWithUpgrade = SocketWithUpgrade; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| class Socket extends SocketWithUpgrade { |
| constructor(uri, opts = {}) { |
| const o = typeof uri === "object" ? uri : opts; |
| if (!o.transports || |
| (o.transports && typeof o.transports[0] === "string")) { |
| o.transports = (o.transports || ["polling", "websocket", "webtransport"]) |
| .map((transportName) => index_js_1.transports[transportName]) |
| .filter((t) => !!t); |
| } |
| super(uri, o); |
| } |
| } |
| exports.Socket = Socket; |
|
|