@gleamkit/engine.io 6.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,124 @@
1
+ import { EventEmitter } from "events";
2
+ import type { IncomingMessage, ServerResponse } from "http";
3
+ import { Packet, RawData } from "engine.io-parser";
4
+ type ReadyState = "open" | "closing" | "closed";
5
+ export type EngineRequest = IncomingMessage & {
6
+ _query: Record<string, string>;
7
+ res?: ServerResponse;
8
+ cleanup?: Function;
9
+ websocket?: any;
10
+ };
11
+ export declare abstract class Transport extends EventEmitter {
12
+ /**
13
+ * The session ID.
14
+ */
15
+ sid: string;
16
+ /**
17
+ * Whether the transport is currently ready to send packets.
18
+ */
19
+ writable: boolean;
20
+ /**
21
+ * The revision of the protocol:
22
+ *
23
+ * - 3 is used in Engine.IO v3 / Socket.IO v2
24
+ * - 4 is used in Engine.IO v4 and above / Socket.IO v3 and above
25
+ *
26
+ * It is found in the `EIO` query parameters of the HTTP requests.
27
+ *
28
+ * @see https://github.com/socketio/engine.io-protocol
29
+ */
30
+ protocol: number;
31
+ /**
32
+ * The current state of the transport.
33
+ * @protected
34
+ */
35
+ protected _readyState: ReadyState;
36
+ /**
37
+ * Whether the transport is discarded and can be safely closed (used during upgrade).
38
+ * @protected
39
+ */
40
+ protected discarded: boolean;
41
+ /**
42
+ * The parser to use (depends on the revision of the {@link Transport#protocol}.
43
+ * @protected
44
+ */
45
+ protected parser: any;
46
+ /**
47
+ * Whether the transport supports binary payloads (else it will be base64-encoded)
48
+ * @protected
49
+ */
50
+ protected supportsBinary: boolean;
51
+ get readyState(): ReadyState;
52
+ set readyState(state: ReadyState);
53
+ /**
54
+ * Transport constructor.
55
+ *
56
+ * @param {EngineRequest} req
57
+ */
58
+ constructor(req: {
59
+ _query: Record<string, string>;
60
+ });
61
+ /**
62
+ * Flags the transport as discarded.
63
+ *
64
+ * @package
65
+ */
66
+ discard(): void;
67
+ /**
68
+ * Called with an incoming HTTP request.
69
+ *
70
+ * @param req
71
+ * @package
72
+ */
73
+ onRequest(req: any): void;
74
+ /**
75
+ * Closes the transport.
76
+ *
77
+ * @package
78
+ */
79
+ close(fn?: () => void): void;
80
+ /**
81
+ * Called with a transport error.
82
+ *
83
+ * @param {String} msg - message error
84
+ * @param {Object} desc - error description
85
+ * @protected
86
+ */
87
+ protected onError(msg: string, desc?: any): void;
88
+ /**
89
+ * Called with parsed out a packets from the data stream.
90
+ *
91
+ * @param {Object} packet
92
+ * @protected
93
+ */
94
+ protected onPacket(packet: Packet): void;
95
+ /**
96
+ * Called with the encoded packet data.
97
+ *
98
+ * @param {String} data
99
+ * @protected
100
+ */
101
+ protected onData(data: RawData): void;
102
+ /**
103
+ * Called upon transport close.
104
+ *
105
+ * @protected
106
+ */
107
+ protected onClose(): void;
108
+ /**
109
+ * The name of the transport.
110
+ */
111
+ abstract get name(): string;
112
+ /**
113
+ * Sends an array of packets.
114
+ *
115
+ * @param {Array} packets
116
+ * @package
117
+ */
118
+ abstract send(packets: Packet[]): void;
119
+ /**
120
+ * Closes the transport.
121
+ */
122
+ abstract doClose(fn?: () => void): void;
123
+ }
124
+ export {};
@@ -0,0 +1,117 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Transport = void 0;
4
+ const events_1 = require("events");
5
+ const parser_v4 = require("engine.io-parser");
6
+ const parser_v3 = require("./parser-v3/index");
7
+ const debug_1 = require("debug");
8
+ const debug = (0, debug_1.default)("engine:transport");
9
+ function noop() { }
10
+ class Transport extends events_1.EventEmitter {
11
+ get readyState() {
12
+ return this._readyState;
13
+ }
14
+ set readyState(state) {
15
+ debug("readyState updated from %s to %s (%s)", this._readyState, state, this.name);
16
+ this._readyState = state;
17
+ }
18
+ /**
19
+ * Transport constructor.
20
+ *
21
+ * @param {EngineRequest} req
22
+ */
23
+ constructor(req) {
24
+ super();
25
+ /**
26
+ * Whether the transport is currently ready to send packets.
27
+ */
28
+ this.writable = false;
29
+ /**
30
+ * The current state of the transport.
31
+ * @protected
32
+ */
33
+ this._readyState = "open";
34
+ /**
35
+ * Whether the transport is discarded and can be safely closed (used during upgrade).
36
+ * @protected
37
+ */
38
+ this.discarded = false;
39
+ this.protocol = req._query.EIO === "4" ? 4 : 3; // 3rd revision by default
40
+ this.parser = this.protocol === 4 ? parser_v4 : parser_v3;
41
+ this.supportsBinary = !(req._query && req._query.b64);
42
+ }
43
+ /**
44
+ * Flags the transport as discarded.
45
+ *
46
+ * @package
47
+ */
48
+ discard() {
49
+ this.discarded = true;
50
+ }
51
+ /**
52
+ * Called with an incoming HTTP request.
53
+ *
54
+ * @param req
55
+ * @package
56
+ */
57
+ onRequest(req) { }
58
+ /**
59
+ * Closes the transport.
60
+ *
61
+ * @package
62
+ */
63
+ close(fn) {
64
+ if ("closed" === this.readyState || "closing" === this.readyState)
65
+ return;
66
+ this.readyState = "closing";
67
+ this.doClose(fn || noop);
68
+ }
69
+ /**
70
+ * Called with a transport error.
71
+ *
72
+ * @param {String} msg - message error
73
+ * @param {Object} desc - error description
74
+ * @protected
75
+ */
76
+ onError(msg, desc) {
77
+ if (this.listeners("error").length) {
78
+ const err = new Error(msg);
79
+ // @ts-ignore
80
+ err.type = "TransportError";
81
+ // @ts-ignore
82
+ err.description = desc;
83
+ this.emit("error", err);
84
+ }
85
+ else {
86
+ debug("ignored transport error %s (%s)", msg, desc);
87
+ }
88
+ }
89
+ /**
90
+ * Called with parsed out a packets from the data stream.
91
+ *
92
+ * @param {Object} packet
93
+ * @protected
94
+ */
95
+ onPacket(packet) {
96
+ this.emit("packet", packet);
97
+ }
98
+ /**
99
+ * Called with the encoded packet data.
100
+ *
101
+ * @param {String} data
102
+ * @protected
103
+ */
104
+ onData(data) {
105
+ this.onPacket(this.parser.decodePacket(data));
106
+ }
107
+ /**
108
+ * Called upon transport close.
109
+ *
110
+ * @protected
111
+ */
112
+ onClose() {
113
+ this.readyState = "closed";
114
+ this.emit("close");
115
+ }
116
+ }
117
+ exports.Transport = Transport;
@@ -0,0 +1,16 @@
1
+ import { Polling as XHR } from "./polling";
2
+ import { WebSocket } from "./websocket";
3
+ import { WebTransport } from "./webtransport";
4
+ declare const _default: {
5
+ polling: typeof polling;
6
+ websocket: typeof WebSocket;
7
+ webtransport: typeof WebTransport;
8
+ };
9
+ export default _default;
10
+ /**
11
+ * Polling polymorphic constructor.
12
+ */
13
+ declare function polling(req: any): XHR;
14
+ declare namespace polling {
15
+ var upgradesTo: string[];
16
+ }
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const polling_1 = require("./polling");
4
+ const polling_jsonp_1 = require("./polling-jsonp");
5
+ const websocket_1 = require("./websocket");
6
+ const webtransport_1 = require("./webtransport");
7
+ exports.default = {
8
+ polling: polling,
9
+ websocket: websocket_1.WebSocket,
10
+ webtransport: webtransport_1.WebTransport,
11
+ };
12
+ /**
13
+ * Polling polymorphic constructor.
14
+ */
15
+ function polling(req) {
16
+ if ("string" === typeof req._query.j) {
17
+ return new polling_jsonp_1.JSONP(req);
18
+ }
19
+ else {
20
+ return new polling_1.Polling(req);
21
+ }
22
+ }
23
+ polling.upgradesTo = ["websocket", "webtransport"];
@@ -0,0 +1,12 @@
1
+ import { Polling } from "./polling";
2
+ import type { RawData } from "engine.io-parser";
3
+ export declare class JSONP extends Polling {
4
+ private readonly head;
5
+ private readonly foot;
6
+ /**
7
+ * JSON-P polling transport.
8
+ */
9
+ constructor(req: any);
10
+ onData(data: RawData): void;
11
+ doWrite(data: any, options: any, callback: any): void;
12
+ }
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JSONP = void 0;
4
+ const polling_1 = require("./polling");
5
+ const qs = require("querystring");
6
+ const rDoubleSlashes = /\\\\n/g;
7
+ const rSlashes = /(\\)?\\n/g;
8
+ class JSONP extends polling_1.Polling {
9
+ /**
10
+ * JSON-P polling transport.
11
+ */
12
+ constructor(req) {
13
+ super(req);
14
+ this.head = "___eio[" + (req._query.j || "").replace(/[^0-9]/g, "") + "](";
15
+ this.foot = ");";
16
+ }
17
+ onData(data) {
18
+ // we leverage the qs module so that we get built-in DoS protection
19
+ // and the fast alternative to decodeURIComponent
20
+ data = qs.parse(data).d;
21
+ if ("string" === typeof data) {
22
+ // client will send already escaped newlines as \\\\n and newlines as \\n
23
+ // \\n must be replaced with \n and \\\\n with \\n
24
+ data = data.replace(rSlashes, function (match, slashes) {
25
+ return slashes ? match : "\n";
26
+ });
27
+ super.onData(data.replace(rDoubleSlashes, "\\n"));
28
+ }
29
+ }
30
+ doWrite(data, options, callback) {
31
+ // we must output valid javascript, not valid json
32
+ // see: http://timelessrepo.com/json-isnt-a-javascript-subset
33
+ const js = JSON.stringify(data)
34
+ .replace(/\u2028/g, "\\u2028")
35
+ .replace(/\u2029/g, "\\u2029");
36
+ // prepare response
37
+ data = this.head + js + this.foot;
38
+ super.doWrite(data, options, callback);
39
+ }
40
+ }
41
+ exports.JSONP = JSONP;
@@ -0,0 +1,87 @@
1
+ import { EngineRequest, Transport } from "../transport";
2
+ import type { Packet, RawData } from "engine.io-parser";
3
+ export declare class Polling extends Transport {
4
+ maxHttpBufferSize: number;
5
+ httpCompression: any;
6
+ private req;
7
+ private res;
8
+ private dataReq;
9
+ private dataRes;
10
+ private shouldClose;
11
+ private readonly closeTimeout;
12
+ /**
13
+ * HTTP polling constructor.
14
+ */
15
+ constructor(req: any);
16
+ /**
17
+ * Transport name
18
+ */
19
+ get name(): string;
20
+ /**
21
+ * Overrides onRequest.
22
+ *
23
+ * @param {EngineRequest} req
24
+ * @package
25
+ */
26
+ onRequest(req: EngineRequest): void;
27
+ /**
28
+ * The client sends a request awaiting for us to send data.
29
+ *
30
+ * @private
31
+ */
32
+ private onPollRequest;
33
+ /**
34
+ * The client sends a request with data.
35
+ *
36
+ * @private
37
+ */
38
+ private onDataRequest;
39
+ /**
40
+ * Processes the incoming data payload.
41
+ *
42
+ * @param data - encoded payload
43
+ * @protected
44
+ */
45
+ onData(data: RawData): void;
46
+ /**
47
+ * Overrides onClose.
48
+ *
49
+ * @private
50
+ */
51
+ onClose(): void;
52
+ send(packets: Packet[]): void;
53
+ /**
54
+ * Writes data as response to poll request.
55
+ *
56
+ * @param {String} data
57
+ * @param {Object} options
58
+ * @private
59
+ */
60
+ private write;
61
+ /**
62
+ * Performs the write.
63
+ *
64
+ * @protected
65
+ */
66
+ protected doWrite(data: any, options: any, callback: any): void;
67
+ /**
68
+ * Compresses data.
69
+ *
70
+ * @private
71
+ */
72
+ private compress;
73
+ /**
74
+ * Closes the transport.
75
+ *
76
+ * @private
77
+ */
78
+ doClose(fn: () => void): void;
79
+ /**
80
+ * Returns headers for a response.
81
+ *
82
+ * @param {http.IncomingMessage} req
83
+ * @param {Object} headers - extra headers
84
+ * @private
85
+ */
86
+ private headers;
87
+ }