import { decodePacket } from "engine.io-parser" ; import { DefaultEventsMap , Emitter } from "@socket.io/component-emitter" ; import { installTimerFunctions } from "./util.js" ; import debugModule from "debug" ; import { SocketOptions } from "./socket.js" ; const debug = debugModule ( "engine.io-client:transport" ) ; export abstract class Transport extends Emitter < DefaultEventsMap , DefaultEventsMap > { protected opts : SocketOptions ; protected supportsBinary : boolean ; protected query : object ; protected readyState : string ; protected writable : boolean = false ; protected socket : any ; protected setTimeoutFn : typeof setTimeout ; param { Object } options . api constructor ( opts ) { super ( ) ; installTimerFunctions ( this , opts ) ; this . opts = opts ; this . query = opts . query ; this . readyState = "" ; this . socket = opts . socket ; } param { String } str return { Transport } api protected onError ( msg , desc ) { const err = new Error ( msg ) ; err . type = "TransportError" ; err . description = desc ; super . emit ( "error" , err ) ; return this ; } api private open ( ) { if ( "closed" === this . readyState || "" === this . readyState ) { this . readyState = "opening" ; this . doOpen ( ) ; } return this ; } api public close ( ) { if ( "opening" === this . readyState || "open" === this . readyState ) { this . doClose ( ) ; this . onClose ( ) ; } return this ; } param { Array } packets api public send ( packets ) { if ( "open" === this . readyState ) { this . write ( packets ) ; } else { debug ( "transport is not open, discarding packets" ) ; } } api protected onOpen ( ) { this . readyState = "open" ; this . writable = true ; super . emit ( "open" ) ; } param { String } data api protected onData ( data ) { const packet = decodePacket ( data , this . socket . binaryType ) ; this . onPacket ( packet ) ; } api protected onPacket ( packet ) { super . emit ( "packet" , packet ) ; } api protected onClose ( ) { this . readyState = "closed" ; super . emit ( "close" ) ; } protected abstract doOpen ( ) ; protected abstract doClose ( ) ; protected abstract write ( packets ) ; }