|
import { decodePacket } from "engine.io-parser"; |
|
import { Emitter } from "@socket.io/component-emitter"; |
|
import { installTimerFunctions } from "./util.js"; |
|
import debugModule from "debug"; |
|
import { encode } from "./contrib/parseqs.js"; |
|
const debug = debugModule("engine.io-client:transport"); |
|
export class TransportError extends Error { |
|
constructor(reason, description, context) { |
|
super(reason); |
|
this.description = description; |
|
this.context = context; |
|
this.type = "TransportError"; |
|
} |
|
} |
|
export class Transport extends Emitter { |
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor(opts) { |
|
super(); |
|
this.writable = false; |
|
installTimerFunctions(this, opts); |
|
this.opts = opts; |
|
this.query = opts.query; |
|
this.socket = opts.socket; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
onError(reason, description, context) { |
|
super.emitReserved("error", new TransportError(reason, description, context)); |
|
return this; |
|
} |
|
|
|
|
|
|
|
open() { |
|
this.readyState = "opening"; |
|
this.doOpen(); |
|
return this; |
|
} |
|
|
|
|
|
|
|
close() { |
|
if (this.readyState === "opening" || this.readyState === "open") { |
|
this.doClose(); |
|
this.onClose(); |
|
} |
|
return this; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
send(packets) { |
|
if (this.readyState === "open") { |
|
this.write(packets); |
|
} |
|
else { |
|
|
|
debug("transport is not open, discarding packets"); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
onOpen() { |
|
this.readyState = "open"; |
|
this.writable = true; |
|
super.emitReserved("open"); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
onData(data) { |
|
const packet = decodePacket(data, this.socket.binaryType); |
|
this.onPacket(packet); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
onPacket(packet) { |
|
super.emitReserved("packet", packet); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
onClose(details) { |
|
this.readyState = "closed"; |
|
super.emitReserved("close", details); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
pause(onPause) { } |
|
createUri(schema, query = {}) { |
|
return (schema + |
|
"://" + |
|
this._hostname() + |
|
this._port() + |
|
this.opts.path + |
|
this._query(query)); |
|
} |
|
_hostname() { |
|
const hostname = this.opts.hostname; |
|
return hostname.indexOf(":") === -1 ? hostname : "[" + hostname + "]"; |
|
} |
|
_port() { |
|
if (this.opts.port && |
|
((this.opts.secure && Number(this.opts.port !== 443)) || |
|
(!this.opts.secure && Number(this.opts.port) !== 80))) { |
|
return ":" + this.opts.port; |
|
} |
|
else { |
|
return ""; |
|
} |
|
} |
|
_query(query) { |
|
const encodedQuery = encode(query); |
|
return encodedQuery.length ? "?" + encodedQuery : ""; |
|
} |
|
} |
|
|