@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,187 @@
1
+ /*! https://mths.be/utf8js v2.1.2 by @mathias */
2
+ var stringFromCharCode = String.fromCharCode;
3
+ // Taken from https://mths.be/punycode
4
+ function ucs2decode(string) {
5
+ var output = [];
6
+ var counter = 0;
7
+ var length = string.length;
8
+ var value;
9
+ var extra;
10
+ while (counter < length) {
11
+ value = string.charCodeAt(counter++);
12
+ if (value >= 0xD800 && value <= 0xDBFF && counter < length) {
13
+ // high surrogate, and there is a next character
14
+ extra = string.charCodeAt(counter++);
15
+ if ((extra & 0xFC00) == 0xDC00) { // low surrogate
16
+ output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000);
17
+ }
18
+ else {
19
+ // unmatched surrogate; only append this code unit, in case the next
20
+ // code unit is the high surrogate of a surrogate pair
21
+ output.push(value);
22
+ counter--;
23
+ }
24
+ }
25
+ else {
26
+ output.push(value);
27
+ }
28
+ }
29
+ return output;
30
+ }
31
+ // Taken from https://mths.be/punycode
32
+ function ucs2encode(array) {
33
+ var length = array.length;
34
+ var index = -1;
35
+ var value;
36
+ var output = '';
37
+ while (++index < length) {
38
+ value = array[index];
39
+ if (value > 0xFFFF) {
40
+ value -= 0x10000;
41
+ output += stringFromCharCode(value >>> 10 & 0x3FF | 0xD800);
42
+ value = 0xDC00 | value & 0x3FF;
43
+ }
44
+ output += stringFromCharCode(value);
45
+ }
46
+ return output;
47
+ }
48
+ function checkScalarValue(codePoint, strict) {
49
+ if (codePoint >= 0xD800 && codePoint <= 0xDFFF) {
50
+ if (strict) {
51
+ throw Error('Lone surrogate U+' + codePoint.toString(16).toUpperCase() +
52
+ ' is not a scalar value');
53
+ }
54
+ return false;
55
+ }
56
+ return true;
57
+ }
58
+ /*--------------------------------------------------------------------------*/
59
+ function createByte(codePoint, shift) {
60
+ return stringFromCharCode(((codePoint >> shift) & 0x3F) | 0x80);
61
+ }
62
+ function encodeCodePoint(codePoint, strict) {
63
+ if ((codePoint & 0xFFFFFF80) == 0) { // 1-byte sequence
64
+ return stringFromCharCode(codePoint);
65
+ }
66
+ var symbol = '';
67
+ if ((codePoint & 0xFFFFF800) == 0) { // 2-byte sequence
68
+ symbol = stringFromCharCode(((codePoint >> 6) & 0x1F) | 0xC0);
69
+ }
70
+ else if ((codePoint & 0xFFFF0000) == 0) { // 3-byte sequence
71
+ if (!checkScalarValue(codePoint, strict)) {
72
+ codePoint = 0xFFFD;
73
+ }
74
+ symbol = stringFromCharCode(((codePoint >> 12) & 0x0F) | 0xE0);
75
+ symbol += createByte(codePoint, 6);
76
+ }
77
+ else if ((codePoint & 0xFFE00000) == 0) { // 4-byte sequence
78
+ symbol = stringFromCharCode(((codePoint >> 18) & 0x07) | 0xF0);
79
+ symbol += createByte(codePoint, 12);
80
+ symbol += createByte(codePoint, 6);
81
+ }
82
+ symbol += stringFromCharCode((codePoint & 0x3F) | 0x80);
83
+ return symbol;
84
+ }
85
+ function utf8encode(string, opts) {
86
+ opts = opts || {};
87
+ var strict = false !== opts.strict;
88
+ var codePoints = ucs2decode(string);
89
+ var length = codePoints.length;
90
+ var index = -1;
91
+ var codePoint;
92
+ var byteString = '';
93
+ while (++index < length) {
94
+ codePoint = codePoints[index];
95
+ byteString += encodeCodePoint(codePoint, strict);
96
+ }
97
+ return byteString;
98
+ }
99
+ /*--------------------------------------------------------------------------*/
100
+ function readContinuationByte() {
101
+ if (byteIndex >= byteCount) {
102
+ throw Error('Invalid byte index');
103
+ }
104
+ var continuationByte = byteArray[byteIndex] & 0xFF;
105
+ byteIndex++;
106
+ if ((continuationByte & 0xC0) == 0x80) {
107
+ return continuationByte & 0x3F;
108
+ }
109
+ // If we end up here, it’s not a continuation byte
110
+ throw Error('Invalid continuation byte');
111
+ }
112
+ function decodeSymbol(strict) {
113
+ var byte1;
114
+ var byte2;
115
+ var byte3;
116
+ var byte4;
117
+ var codePoint;
118
+ if (byteIndex > byteCount) {
119
+ throw Error('Invalid byte index');
120
+ }
121
+ if (byteIndex == byteCount) {
122
+ return false;
123
+ }
124
+ // Read first byte
125
+ byte1 = byteArray[byteIndex] & 0xFF;
126
+ byteIndex++;
127
+ // 1-byte sequence (no continuation bytes)
128
+ if ((byte1 & 0x80) == 0) {
129
+ return byte1;
130
+ }
131
+ // 2-byte sequence
132
+ if ((byte1 & 0xE0) == 0xC0) {
133
+ byte2 = readContinuationByte();
134
+ codePoint = ((byte1 & 0x1F) << 6) | byte2;
135
+ if (codePoint >= 0x80) {
136
+ return codePoint;
137
+ }
138
+ else {
139
+ throw Error('Invalid continuation byte');
140
+ }
141
+ }
142
+ // 3-byte sequence (may include unpaired surrogates)
143
+ if ((byte1 & 0xF0) == 0xE0) {
144
+ byte2 = readContinuationByte();
145
+ byte3 = readContinuationByte();
146
+ codePoint = ((byte1 & 0x0F) << 12) | (byte2 << 6) | byte3;
147
+ if (codePoint >= 0x0800) {
148
+ return checkScalarValue(codePoint, strict) ? codePoint : 0xFFFD;
149
+ }
150
+ else {
151
+ throw Error('Invalid continuation byte');
152
+ }
153
+ }
154
+ // 4-byte sequence
155
+ if ((byte1 & 0xF8) == 0xF0) {
156
+ byte2 = readContinuationByte();
157
+ byte3 = readContinuationByte();
158
+ byte4 = readContinuationByte();
159
+ codePoint = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0C) |
160
+ (byte3 << 0x06) | byte4;
161
+ if (codePoint >= 0x010000 && codePoint <= 0x10FFFF) {
162
+ return codePoint;
163
+ }
164
+ }
165
+ throw Error('Invalid UTF-8 detected');
166
+ }
167
+ var byteArray;
168
+ var byteCount;
169
+ var byteIndex;
170
+ function utf8decode(byteString, opts) {
171
+ opts = opts || {};
172
+ var strict = false !== opts.strict;
173
+ byteArray = ucs2decode(byteString);
174
+ byteCount = byteArray.length;
175
+ byteIndex = 0;
176
+ var codePoints = [];
177
+ var tmp;
178
+ while ((tmp = decodeSymbol(strict)) !== false) {
179
+ codePoints.push(tmp);
180
+ }
181
+ return ucs2encode(codePoints);
182
+ }
183
+ module.exports = {
184
+ version: '2.1.2',
185
+ encode: utf8encode,
186
+ decode: utf8decode
187
+ };
@@ -0,0 +1,267 @@
1
+ import { EventEmitter } from "events";
2
+ import { Socket } from "./socket";
3
+ import type { IncomingMessage, Server as HttpServer, ServerResponse } from "http";
4
+ import type { CookieSerializeOptions } from "cookie";
5
+ import type { CorsOptions, CorsOptionsDelegate } from "cors";
6
+ import type { Duplex } from "stream";
7
+ import type { EngineRequest } from "./transport";
8
+ type Transport = "polling" | "websocket" | "webtransport";
9
+ export interface AttachOptions {
10
+ /**
11
+ * name of the path to capture
12
+ * @default "/engine.io"
13
+ */
14
+ path?: string;
15
+ /**
16
+ * destroy unhandled upgrade requests
17
+ * @default true
18
+ */
19
+ destroyUpgrade?: boolean;
20
+ /**
21
+ * milliseconds after which unhandled requests are ended
22
+ * @default 1000
23
+ */
24
+ destroyUpgradeTimeout?: number;
25
+ /**
26
+ * Whether we should add a trailing slash to the request path.
27
+ * @default true
28
+ */
29
+ addTrailingSlash?: boolean;
30
+ }
31
+ export interface ServerOptions {
32
+ /**
33
+ * how many ms without a pong packet to consider the connection closed
34
+ * @default 20000
35
+ */
36
+ pingTimeout?: number;
37
+ /**
38
+ * how many ms before sending a new ping packet
39
+ * @default 25000
40
+ */
41
+ pingInterval?: number;
42
+ /**
43
+ * how many ms before an uncompleted transport upgrade is cancelled
44
+ * @default 10000
45
+ */
46
+ upgradeTimeout?: number;
47
+ /**
48
+ * how many bytes or characters a message can be, before closing the session (to avoid DoS).
49
+ * @default 1e5 (100 KB)
50
+ */
51
+ maxHttpBufferSize?: number;
52
+ /**
53
+ * A function that receives a given handshake or upgrade request as its first parameter,
54
+ * and can decide whether to continue or not. The second argument is a function that needs
55
+ * to be called with the decided information: fn(err, success), where success is a boolean
56
+ * value where false means that the request is rejected, and err is an error code.
57
+ */
58
+ allowRequest?: (req: IncomingMessage, fn: (err: string | null | undefined, success: boolean) => void) => void;
59
+ /**
60
+ * The low-level transports that are enabled. WebTransport is disabled by default and must be manually enabled:
61
+ *
62
+ * @example
63
+ * new Server({
64
+ * transports: ["polling", "websocket", "webtransport"]
65
+ * });
66
+ *
67
+ * @default ["polling", "websocket"]
68
+ */
69
+ transports?: Transport[];
70
+ /**
71
+ * whether to allow transport upgrades
72
+ * @default true
73
+ */
74
+ allowUpgrades?: boolean;
75
+ /**
76
+ * parameters of the WebSocket permessage-deflate extension (see ws module api docs). Set to false to disable.
77
+ * @default false
78
+ */
79
+ perMessageDeflate?: boolean | object;
80
+ /**
81
+ * parameters of the http compression for the polling transports (see zlib api docs). Set to false to disable.
82
+ * @default true
83
+ */
84
+ httpCompression?: boolean | object;
85
+ /**
86
+ * what WebSocket server implementation to use. Specified module must
87
+ * conform to the ws interface (see ws module api docs).
88
+ * An alternative c++ addon is also available by installing eiows module.
89
+ *
90
+ * @default `require("ws").Server`
91
+ */
92
+ wsEngine?: any;
93
+ /**
94
+ * an optional packet which will be concatenated to the handshake packet emitted by Engine.IO.
95
+ */
96
+ initialPacket?: any;
97
+ /**
98
+ * configuration of the cookie that contains the client sid to send as part of handshake response headers. This cookie
99
+ * might be used for sticky-session. Defaults to not sending any cookie.
100
+ * @default false
101
+ */
102
+ cookie?: (CookieSerializeOptions & {
103
+ name: string;
104
+ }) | boolean;
105
+ /**
106
+ * the options that will be forwarded to the cors module
107
+ */
108
+ cors?: CorsOptions | CorsOptionsDelegate;
109
+ /**
110
+ * whether to enable compatibility with Socket.IO v2 clients
111
+ * @default false
112
+ */
113
+ allowEIO3?: boolean;
114
+ }
115
+ /**
116
+ * An Express-compatible middleware.
117
+ *
118
+ * Middleware functions are functions that have access to the request object (req), the response object (res), and the
119
+ * next middleware function in the application’s request-response cycle.
120
+ *
121
+ * @see https://expressjs.com/en/guide/using-middleware.html
122
+ */
123
+ type Middleware = (req: IncomingMessage, res: ServerResponse, next: (err?: any) => void) => void;
124
+ export declare abstract class BaseServer extends EventEmitter {
125
+ opts: ServerOptions;
126
+ protected clients: Record<string, Socket>;
127
+ clientsCount: number;
128
+ protected middlewares: Middleware[];
129
+ /**
130
+ * Server constructor.
131
+ *
132
+ * @param {Object} opts - options
133
+ */
134
+ constructor(opts?: ServerOptions);
135
+ protected abstract init(): any;
136
+ /**
137
+ * Compute the pathname of the requests that are handled by the server
138
+ * @param options
139
+ * @protected
140
+ */
141
+ protected _computePath(options: AttachOptions): string;
142
+ /**
143
+ * Returns a list of available transports for upgrade given a certain transport.
144
+ *
145
+ * @return {Array}
146
+ */
147
+ upgrades(transport: string): any;
148
+ /**
149
+ * Verifies a request.
150
+ *
151
+ * @param {EngineRequest} req
152
+ * @param upgrade - whether it's an upgrade request
153
+ * @param fn
154
+ * @protected
155
+ */
156
+ protected verify(req: any, upgrade: boolean, fn: (errorCode?: number, errorContext?: any) => void): void;
157
+ /**
158
+ * Adds a new middleware.
159
+ *
160
+ * @example
161
+ * import helmet from "helmet";
162
+ *
163
+ * engine.use(helmet());
164
+ *
165
+ * @param fn
166
+ */
167
+ use(fn: any): void;
168
+ /**
169
+ * Apply the middlewares to the request.
170
+ *
171
+ * @param req
172
+ * @param res
173
+ * @param callback
174
+ * @protected
175
+ */
176
+ protected _applyMiddlewares(req: IncomingMessage, res: ServerResponse, callback: (err?: any) => void): void;
177
+ /**
178
+ * Closes all clients.
179
+ */
180
+ close(): this;
181
+ protected abstract cleanup(): any;
182
+ /**
183
+ * generate a socket id.
184
+ * Overwrite this method to generate your custom socket id
185
+ *
186
+ * @param {IncomingMessage} req - the request object
187
+ */
188
+ generateId(req: IncomingMessage): any;
189
+ /**
190
+ * Handshakes a new client.
191
+ *
192
+ * @param {String} transportName
193
+ * @param {Object} req - the request object
194
+ * @param {Function} closeConnection
195
+ *
196
+ * @protected
197
+ */
198
+ protected handshake(transportName: string, req: any, closeConnection: (errorCode?: number, errorContext?: any) => void): Promise<any>;
199
+ onWebTransportSession(session: any): Promise<any>;
200
+ protected abstract createTransport(transportName: any, req: any): any;
201
+ /**
202
+ * Protocol errors mappings.
203
+ */
204
+ static errors: {
205
+ UNKNOWN_TRANSPORT: number;
206
+ UNKNOWN_SID: number;
207
+ BAD_HANDSHAKE_METHOD: number;
208
+ BAD_REQUEST: number;
209
+ FORBIDDEN: number;
210
+ UNSUPPORTED_PROTOCOL_VERSION: number;
211
+ };
212
+ static errorMessages: {
213
+ 0: string;
214
+ 1: string;
215
+ 2: string;
216
+ 3: string;
217
+ 4: string;
218
+ 5: string;
219
+ };
220
+ }
221
+ /**
222
+ * An Engine.IO server based on Node.js built-in HTTP server and the `ws` package for WebSocket connections.
223
+ */
224
+ export declare class Server extends BaseServer {
225
+ httpServer?: HttpServer;
226
+ private ws;
227
+ /**
228
+ * Initialize websocket server
229
+ *
230
+ * @protected
231
+ */
232
+ protected init(): void;
233
+ protected cleanup(): void;
234
+ /**
235
+ * Prepares a request by processing the query string.
236
+ *
237
+ * @private
238
+ */
239
+ private prepare;
240
+ protected createTransport(transportName: string, req: IncomingMessage): any;
241
+ /**
242
+ * Handles an Engine.IO HTTP request.
243
+ *
244
+ * @param {EngineRequest} req
245
+ * @param {ServerResponse} res
246
+ */
247
+ handleRequest(req: EngineRequest, res: ServerResponse): void;
248
+ /**
249
+ * Handles an Engine.IO HTTP Upgrade.
250
+ */
251
+ handleUpgrade(req: EngineRequest, socket: Duplex, upgradeHead: Buffer): void;
252
+ /**
253
+ * Called upon a ws.io connection.
254
+ *
255
+ * @param {ws.Socket} websocket
256
+ * @private
257
+ */
258
+ private onWebSocket;
259
+ /**
260
+ * Captures upgrade requests for a http.Server.
261
+ *
262
+ * @param {http.Server} server
263
+ * @param {Object} options
264
+ */
265
+ attach(server: HttpServer, options?: AttachOptions): void;
266
+ }
267
+ export {};