|
"use strict"; |
|
Object.defineProperty(exports, "__esModule", { value: true }); |
|
exports.Transport = void 0; |
|
const events_1 = require("events"); |
|
const parser_v4 = require("engine.io-parser"); |
|
const parser_v3 = require("./parser-v3/index"); |
|
const debug_1 = require("debug"); |
|
const debug = (0, debug_1.default)("engine:transport"); |
|
|
|
|
|
|
|
|
|
|
|
function noop() { } |
|
class Transport extends events_1.EventEmitter { |
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor(req) { |
|
super(); |
|
this.readyState = "open"; |
|
this.discarded = false; |
|
this.protocol = req._query.EIO === "4" ? 4 : 3; |
|
this.parser = this.protocol === 4 ? parser_v4 : parser_v3; |
|
} |
|
get readyState() { |
|
return this._readyState; |
|
} |
|
set readyState(state) { |
|
debug("readyState updated from %s to %s (%s)", this._readyState, state, this.name); |
|
this._readyState = state; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
discard() { |
|
this.discarded = true; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
onRequest(req) { |
|
debug("setting request"); |
|
this.req = req; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
close(fn) { |
|
if ("closed" === this.readyState || "closing" === this.readyState) |
|
return; |
|
this.readyState = "closing"; |
|
this.doClose(fn || noop); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
onError(msg, desc) { |
|
if (this.listeners("error").length) { |
|
const err = new Error(msg); |
|
|
|
err.type = "TransportError"; |
|
|
|
err.description = desc; |
|
this.emit("error", err); |
|
} |
|
else { |
|
debug("ignored transport error %s (%s)", msg, desc); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
onPacket(packet) { |
|
this.emit("packet", packet); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
onData(data) { |
|
this.onPacket(this.parser.decodePacket(data)); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
onClose() { |
|
this.readyState = "closed"; |
|
this.emit("close"); |
|
} |
|
} |
|
exports.Transport = Transport; |
|
|