elit 2.0.1 → 3.0.0
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.
- package/README.md +275 -128
- package/dist/build.d.mts +10 -1
- package/dist/build.d.ts +10 -1
- package/dist/build.js +670 -1
- package/dist/build.mjs +641 -1
- package/dist/chokidar.d.mts +134 -0
- package/dist/chokidar.d.ts +134 -0
- package/dist/chokidar.js +240 -0
- package/dist/chokidar.mjs +221 -0
- package/dist/cli.js +2792 -495
- package/dist/dom.d.mts +10 -3
- package/dist/dom.d.ts +10 -3
- package/dist/dom.js +676 -1
- package/dist/dom.mjs +647 -1
- package/dist/el.d.mts +16 -36
- package/dist/el.d.ts +16 -36
- package/dist/el.js +789 -1
- package/dist/el.mjs +583 -1
- package/dist/fs.d.mts +255 -0
- package/dist/fs.d.ts +255 -0
- package/dist/fs.js +513 -0
- package/dist/fs.mjs +469 -0
- package/dist/hmr.js +112 -1
- package/dist/hmr.mjs +91 -1
- package/dist/http.d.mts +163 -0
- package/dist/http.d.ts +163 -0
- package/dist/http.js +632 -0
- package/dist/http.mjs +605 -0
- package/dist/https.d.mts +108 -0
- package/dist/https.d.ts +108 -0
- package/dist/https.js +907 -0
- package/dist/https.mjs +901 -0
- package/dist/index.d.mts +613 -33
- package/dist/index.d.ts +613 -33
- package/dist/index.js +2589 -1
- package/dist/index.mjs +2312 -1
- package/dist/mime-types.d.mts +48 -0
- package/dist/mime-types.d.ts +48 -0
- package/dist/mime-types.js +197 -0
- package/dist/mime-types.mjs +166 -0
- package/dist/path.d.mts +163 -0
- package/dist/path.d.ts +163 -0
- package/dist/path.js +350 -0
- package/dist/path.mjs +310 -0
- package/dist/router.d.mts +3 -1
- package/dist/router.d.ts +3 -1
- package/dist/router.js +830 -1
- package/dist/router.mjs +801 -1
- package/dist/runtime.d.mts +97 -0
- package/dist/runtime.d.ts +97 -0
- package/dist/runtime.js +43 -0
- package/dist/runtime.mjs +15 -0
- package/dist/server.d.mts +5 -1
- package/dist/server.d.ts +5 -1
- package/dist/server.js +3267 -1
- package/dist/server.mjs +3241 -1
- package/dist/state.d.mts +3 -1
- package/dist/state.d.ts +3 -1
- package/dist/state.js +1036 -1
- package/dist/state.mjs +992 -1
- package/dist/style.d.mts +47 -1
- package/dist/style.d.ts +47 -1
- package/dist/style.js +551 -1
- package/dist/style.mjs +483 -1
- package/dist/{types-DOAdFFJB.d.ts → types-C0nGi6MX.d.mts} +29 -13
- package/dist/{types-DOAdFFJB.d.mts → types-Du6kfwTm.d.ts} +29 -13
- package/dist/types.d.mts +452 -3
- package/dist/types.d.ts +452 -3
- package/dist/types.js +18 -1
- package/dist/ws.d.mts +195 -0
- package/dist/ws.d.ts +195 -0
- package/dist/ws.js +380 -0
- package/dist/ws.mjs +358 -0
- package/dist/wss.d.mts +108 -0
- package/dist/wss.d.ts +108 -0
- package/dist/wss.js +1306 -0
- package/dist/wss.mjs +1300 -0
- package/package.json +53 -6
- package/dist/client.d.mts +0 -9
- package/dist/client.d.ts +0 -9
- package/dist/client.js +0 -1
- package/dist/client.mjs +0 -1
package/dist/wss.mjs
ADDED
|
@@ -0,0 +1,1300 @@
|
|
|
1
|
+
import {createRequire as __createRequire} from 'module';const require = __createRequire(import.meta.url);
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
7
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
8
|
+
}) : x)(function(x) {
|
|
9
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
10
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
11
|
+
});
|
|
12
|
+
var __esm = (fn, res) => function __init() {
|
|
13
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
14
|
+
};
|
|
15
|
+
var __export = (target, all) => {
|
|
16
|
+
for (var name in all)
|
|
17
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
18
|
+
};
|
|
19
|
+
var __copyProps = (to, from, except, desc) => {
|
|
20
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
21
|
+
for (let key of __getOwnPropNames(from))
|
|
22
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
23
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
24
|
+
}
|
|
25
|
+
return to;
|
|
26
|
+
};
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
|
|
29
|
+
// src/runtime.ts
|
|
30
|
+
var runtime, isNode, isBun, isDeno;
|
|
31
|
+
var init_runtime = __esm({
|
|
32
|
+
"src/runtime.ts"() {
|
|
33
|
+
"use strict";
|
|
34
|
+
runtime = (() => {
|
|
35
|
+
if (typeof Deno !== "undefined") return "deno";
|
|
36
|
+
if (typeof Bun !== "undefined") return "bun";
|
|
37
|
+
return "node";
|
|
38
|
+
})();
|
|
39
|
+
isNode = runtime === "node";
|
|
40
|
+
isBun = runtime === "bun";
|
|
41
|
+
isDeno = runtime === "deno";
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// src/http.ts
|
|
46
|
+
var http_exports = {};
|
|
47
|
+
__export(http_exports, {
|
|
48
|
+
Agent: () => Agent,
|
|
49
|
+
ClientRequest: () => ClientRequest,
|
|
50
|
+
IncomingMessage: () => IncomingMessage,
|
|
51
|
+
METHODS: () => METHODS,
|
|
52
|
+
STATUS_CODES: () => STATUS_CODES,
|
|
53
|
+
Server: () => Server,
|
|
54
|
+
ServerResponse: () => ServerResponse,
|
|
55
|
+
createServer: () => createServer,
|
|
56
|
+
default: () => http_default,
|
|
57
|
+
get: () => get,
|
|
58
|
+
getRuntime: () => getRuntime,
|
|
59
|
+
request: () => request
|
|
60
|
+
});
|
|
61
|
+
import { EventEmitter } from "events";
|
|
62
|
+
function queueCallback(callback) {
|
|
63
|
+
if (callback) queueMicrotask(callback);
|
|
64
|
+
}
|
|
65
|
+
function headersToInit(headers) {
|
|
66
|
+
const result = {};
|
|
67
|
+
for (const key in headers) {
|
|
68
|
+
const value = headers[key];
|
|
69
|
+
result[key] = Array.isArray(value) ? value.join(", ") : String(value);
|
|
70
|
+
}
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
function createAddress(port, address, family = "IPv4") {
|
|
74
|
+
return { port, family, address };
|
|
75
|
+
}
|
|
76
|
+
function createErrorResponse() {
|
|
77
|
+
return new Response("Internal Server Error", { status: 500 });
|
|
78
|
+
}
|
|
79
|
+
function emitListeningWithCallback(server, callback) {
|
|
80
|
+
server._listening = true;
|
|
81
|
+
server.emit("listening");
|
|
82
|
+
queueCallback(callback);
|
|
83
|
+
}
|
|
84
|
+
function closeAndEmit(server, callback) {
|
|
85
|
+
server._listening = false;
|
|
86
|
+
server.emit("close");
|
|
87
|
+
if (callback) queueMicrotask(() => callback());
|
|
88
|
+
}
|
|
89
|
+
function createServer(optionsOrListener, requestListener) {
|
|
90
|
+
return new Server(typeof optionsOrListener === "function" ? optionsOrListener : requestListener);
|
|
91
|
+
}
|
|
92
|
+
function request(url, options, callback) {
|
|
93
|
+
const urlString = typeof url === "string" ? url : url.toString();
|
|
94
|
+
const req = new ClientRequest(urlString, options);
|
|
95
|
+
if (isNode) {
|
|
96
|
+
const urlObj = new URL(urlString);
|
|
97
|
+
const client = urlObj.protocol === "https:" ? https : http;
|
|
98
|
+
const nodeReq = client.request(urlString, {
|
|
99
|
+
method: options?.method || "GET",
|
|
100
|
+
headers: options?.headers,
|
|
101
|
+
timeout: options?.timeout,
|
|
102
|
+
signal: options?.signal
|
|
103
|
+
}, (res) => {
|
|
104
|
+
const incomingMessage = new IncomingMessage(res);
|
|
105
|
+
if (callback) callback(incomingMessage);
|
|
106
|
+
req.emit("response", incomingMessage);
|
|
107
|
+
});
|
|
108
|
+
nodeReq.on("error", (error) => req.emit("error", error));
|
|
109
|
+
nodeReq.end();
|
|
110
|
+
} else {
|
|
111
|
+
queueMicrotask(async () => {
|
|
112
|
+
try {
|
|
113
|
+
const response = await fetch(urlString, {
|
|
114
|
+
method: options?.method || "GET",
|
|
115
|
+
headers: options?.headers,
|
|
116
|
+
signal: options?.signal
|
|
117
|
+
});
|
|
118
|
+
const fetchRequest = new Request(urlString);
|
|
119
|
+
const incomingMessage = new IncomingMessage(fetchRequest);
|
|
120
|
+
incomingMessage.statusCode = response.status;
|
|
121
|
+
incomingMessage.statusMessage = response.statusText;
|
|
122
|
+
if (callback) callback(incomingMessage);
|
|
123
|
+
req.emit("response", incomingMessage);
|
|
124
|
+
} catch (error) {
|
|
125
|
+
req.emit("error", error);
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
return req;
|
|
130
|
+
}
|
|
131
|
+
function get(url, options, callback) {
|
|
132
|
+
return request(url, { ...options, method: "GET" }, callback);
|
|
133
|
+
}
|
|
134
|
+
function getRuntime() {
|
|
135
|
+
return runtime;
|
|
136
|
+
}
|
|
137
|
+
var http, https, METHODS, STATUS_CODES, IncomingMessage, ServerResponse, Server, ClientRequest, Agent, http_default;
|
|
138
|
+
var init_http = __esm({
|
|
139
|
+
"src/http.ts"() {
|
|
140
|
+
"use strict";
|
|
141
|
+
init_runtime();
|
|
142
|
+
if (isNode && typeof process !== "undefined") {
|
|
143
|
+
try {
|
|
144
|
+
http = __require("http");
|
|
145
|
+
https = __require("https");
|
|
146
|
+
} catch (e) {
|
|
147
|
+
http = __require("http");
|
|
148
|
+
https = __require("https");
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
METHODS = [
|
|
152
|
+
"GET",
|
|
153
|
+
"POST",
|
|
154
|
+
"PUT",
|
|
155
|
+
"DELETE",
|
|
156
|
+
"PATCH",
|
|
157
|
+
"HEAD",
|
|
158
|
+
"OPTIONS",
|
|
159
|
+
"CONNECT",
|
|
160
|
+
"TRACE"
|
|
161
|
+
];
|
|
162
|
+
STATUS_CODES = {
|
|
163
|
+
100: "Continue",
|
|
164
|
+
101: "Switching Protocols",
|
|
165
|
+
102: "Processing",
|
|
166
|
+
200: "OK",
|
|
167
|
+
201: "Created",
|
|
168
|
+
202: "Accepted",
|
|
169
|
+
203: "Non-Authoritative Information",
|
|
170
|
+
204: "No Content",
|
|
171
|
+
205: "Reset Content",
|
|
172
|
+
206: "Partial Content",
|
|
173
|
+
300: "Multiple Choices",
|
|
174
|
+
301: "Moved Permanently",
|
|
175
|
+
302: "Found",
|
|
176
|
+
303: "See Other",
|
|
177
|
+
304: "Not Modified",
|
|
178
|
+
307: "Temporary Redirect",
|
|
179
|
+
308: "Permanent Redirect",
|
|
180
|
+
400: "Bad Request",
|
|
181
|
+
401: "Unauthorized",
|
|
182
|
+
402: "Payment Required",
|
|
183
|
+
403: "Forbidden",
|
|
184
|
+
404: "Not Found",
|
|
185
|
+
405: "Method Not Allowed",
|
|
186
|
+
406: "Not Acceptable",
|
|
187
|
+
407: "Proxy Authentication Required",
|
|
188
|
+
408: "Request Timeout",
|
|
189
|
+
409: "Conflict",
|
|
190
|
+
410: "Gone",
|
|
191
|
+
411: "Length Required",
|
|
192
|
+
412: "Precondition Failed",
|
|
193
|
+
413: "Payload Too Large",
|
|
194
|
+
414: "URI Too Long",
|
|
195
|
+
415: "Unsupported Media Type",
|
|
196
|
+
416: "Range Not Satisfiable",
|
|
197
|
+
417: "Expectation Failed",
|
|
198
|
+
418: "I'm a teapot",
|
|
199
|
+
422: "Unprocessable Entity",
|
|
200
|
+
425: "Too Early",
|
|
201
|
+
426: "Upgrade Required",
|
|
202
|
+
428: "Precondition Required",
|
|
203
|
+
429: "Too Many Requests",
|
|
204
|
+
431: "Request Header Fields Too Large",
|
|
205
|
+
451: "Unavailable For Legal Reasons",
|
|
206
|
+
500: "Internal Server Error",
|
|
207
|
+
501: "Not Implemented",
|
|
208
|
+
502: "Bad Gateway",
|
|
209
|
+
503: "Service Unavailable",
|
|
210
|
+
504: "Gateway Timeout",
|
|
211
|
+
505: "HTTP Version Not Supported",
|
|
212
|
+
506: "Variant Also Negotiates",
|
|
213
|
+
507: "Insufficient Storage",
|
|
214
|
+
508: "Loop Detected",
|
|
215
|
+
510: "Not Extended",
|
|
216
|
+
511: "Network Authentication Required"
|
|
217
|
+
};
|
|
218
|
+
IncomingMessage = class extends EventEmitter {
|
|
219
|
+
constructor(req) {
|
|
220
|
+
super();
|
|
221
|
+
this.httpVersion = "1.1";
|
|
222
|
+
this.rawHeaders = [];
|
|
223
|
+
this._req = req;
|
|
224
|
+
if (isNode) {
|
|
225
|
+
this.method = req.method;
|
|
226
|
+
this.url = req.url;
|
|
227
|
+
this.headers = req.headers;
|
|
228
|
+
this.statusCode = req.statusCode;
|
|
229
|
+
this.statusMessage = req.statusMessage;
|
|
230
|
+
this.httpVersion = req.httpVersion;
|
|
231
|
+
this.rawHeaders = req.rawHeaders;
|
|
232
|
+
this.socket = req.socket;
|
|
233
|
+
} else {
|
|
234
|
+
this.method = req.method;
|
|
235
|
+
const urlObj = new URL(req.url);
|
|
236
|
+
this.url = urlObj.pathname + urlObj.search;
|
|
237
|
+
this.headers = req.headers;
|
|
238
|
+
this.rawHeaders = [];
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
async text() {
|
|
242
|
+
if (isNode) {
|
|
243
|
+
return new Promise((resolve, reject) => {
|
|
244
|
+
const chunks = [];
|
|
245
|
+
this._req.on("data", (chunk) => chunks.push(chunk));
|
|
246
|
+
this._req.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")));
|
|
247
|
+
this._req.on("error", reject);
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
return this._req.text();
|
|
251
|
+
}
|
|
252
|
+
async json() {
|
|
253
|
+
if (isNode) {
|
|
254
|
+
const text = await this.text();
|
|
255
|
+
return JSON.parse(text);
|
|
256
|
+
}
|
|
257
|
+
return this._req.json();
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
ServerResponse = class extends EventEmitter {
|
|
261
|
+
constructor(_req, nodeRes) {
|
|
262
|
+
super();
|
|
263
|
+
this.statusCode = 200;
|
|
264
|
+
this.statusMessage = "OK";
|
|
265
|
+
this.headersSent = false;
|
|
266
|
+
this._body = "";
|
|
267
|
+
this._finished = false;
|
|
268
|
+
this._nodeRes = nodeRes;
|
|
269
|
+
this._headers = /* @__PURE__ */ Object.create(null);
|
|
270
|
+
}
|
|
271
|
+
setHeader(name, value) {
|
|
272
|
+
if (this.headersSent) {
|
|
273
|
+
throw new Error("Cannot set headers after they are sent");
|
|
274
|
+
}
|
|
275
|
+
if (isNode && this._nodeRes) {
|
|
276
|
+
this._nodeRes.setHeader(name, value);
|
|
277
|
+
}
|
|
278
|
+
this._headers[name.toLowerCase()] = value;
|
|
279
|
+
return this;
|
|
280
|
+
}
|
|
281
|
+
getHeader(name) {
|
|
282
|
+
if (isNode && this._nodeRes) {
|
|
283
|
+
return this._nodeRes.getHeader(name);
|
|
284
|
+
}
|
|
285
|
+
return this._headers[name.toLowerCase()];
|
|
286
|
+
}
|
|
287
|
+
getHeaders() {
|
|
288
|
+
if (isNode && this._nodeRes) {
|
|
289
|
+
return this._nodeRes.getHeaders();
|
|
290
|
+
}
|
|
291
|
+
return { ...this._headers };
|
|
292
|
+
}
|
|
293
|
+
getHeaderNames() {
|
|
294
|
+
if (isNode && this._nodeRes) {
|
|
295
|
+
return this._nodeRes.getHeaderNames();
|
|
296
|
+
}
|
|
297
|
+
return Object.keys(this._headers);
|
|
298
|
+
}
|
|
299
|
+
hasHeader(name) {
|
|
300
|
+
if (isNode && this._nodeRes) {
|
|
301
|
+
return this._nodeRes.hasHeader(name);
|
|
302
|
+
}
|
|
303
|
+
return name.toLowerCase() in this._headers;
|
|
304
|
+
}
|
|
305
|
+
removeHeader(name) {
|
|
306
|
+
if (this.headersSent) {
|
|
307
|
+
throw new Error("Cannot remove headers after they are sent");
|
|
308
|
+
}
|
|
309
|
+
if (isNode && this._nodeRes) {
|
|
310
|
+
this._nodeRes.removeHeader(name);
|
|
311
|
+
}
|
|
312
|
+
delete this._headers[name.toLowerCase()];
|
|
313
|
+
}
|
|
314
|
+
writeHead(statusCode, statusMessage, headers) {
|
|
315
|
+
if (this.headersSent) {
|
|
316
|
+
throw new Error("Cannot write headers after they are sent");
|
|
317
|
+
}
|
|
318
|
+
this.statusCode = statusCode;
|
|
319
|
+
if (typeof statusMessage === "string") {
|
|
320
|
+
this.statusMessage = statusMessage;
|
|
321
|
+
if (headers) {
|
|
322
|
+
for (const key in headers) {
|
|
323
|
+
this.setHeader(key, headers[key]);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
} else if (statusMessage) {
|
|
327
|
+
for (const key in statusMessage) {
|
|
328
|
+
this.setHeader(key, statusMessage[key]);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
if (isNode && this._nodeRes) {
|
|
332
|
+
if (typeof statusMessage === "string") {
|
|
333
|
+
this._nodeRes.writeHead(statusCode, statusMessage, headers);
|
|
334
|
+
} else {
|
|
335
|
+
this._nodeRes.writeHead(statusCode, statusMessage);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
this.headersSent = true;
|
|
339
|
+
return this;
|
|
340
|
+
}
|
|
341
|
+
write(chunk, encoding, callback) {
|
|
342
|
+
if (typeof encoding === "function") {
|
|
343
|
+
callback = encoding;
|
|
344
|
+
encoding = "utf8";
|
|
345
|
+
}
|
|
346
|
+
if (!this.headersSent) {
|
|
347
|
+
this.writeHead(this.statusCode);
|
|
348
|
+
}
|
|
349
|
+
if (isNode && this._nodeRes) {
|
|
350
|
+
return this._nodeRes.write(chunk, encoding, callback);
|
|
351
|
+
}
|
|
352
|
+
this._body += chunk;
|
|
353
|
+
queueCallback(callback);
|
|
354
|
+
return true;
|
|
355
|
+
}
|
|
356
|
+
end(chunk, encoding, callback) {
|
|
357
|
+
if (this._finished) {
|
|
358
|
+
return this;
|
|
359
|
+
}
|
|
360
|
+
if (typeof chunk === "function") {
|
|
361
|
+
callback = chunk;
|
|
362
|
+
chunk = void 0;
|
|
363
|
+
} else if (typeof encoding === "function") {
|
|
364
|
+
callback = encoding;
|
|
365
|
+
encoding = "utf8";
|
|
366
|
+
}
|
|
367
|
+
if (chunk !== void 0) {
|
|
368
|
+
this.write(chunk, encoding);
|
|
369
|
+
}
|
|
370
|
+
if (!this.headersSent) {
|
|
371
|
+
this.writeHead(this.statusCode);
|
|
372
|
+
}
|
|
373
|
+
this._finished = true;
|
|
374
|
+
if (isNode && this._nodeRes) {
|
|
375
|
+
this._nodeRes.end(callback);
|
|
376
|
+
this.emit("finish");
|
|
377
|
+
} else {
|
|
378
|
+
const response = new Response(this._body, {
|
|
379
|
+
status: this.statusCode,
|
|
380
|
+
statusText: this.statusMessage,
|
|
381
|
+
headers: headersToInit(this._headers)
|
|
382
|
+
});
|
|
383
|
+
if (this._resolve) {
|
|
384
|
+
this._resolve(response);
|
|
385
|
+
}
|
|
386
|
+
queueCallback(callback);
|
|
387
|
+
}
|
|
388
|
+
return this;
|
|
389
|
+
}
|
|
390
|
+
_setResolver(resolve) {
|
|
391
|
+
this._resolve = resolve;
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
Server = class extends EventEmitter {
|
|
395
|
+
constructor(requestListener) {
|
|
396
|
+
super();
|
|
397
|
+
this._listening = false;
|
|
398
|
+
this.requestListener = requestListener;
|
|
399
|
+
}
|
|
400
|
+
listen(...args) {
|
|
401
|
+
let port = 3e3;
|
|
402
|
+
let hostname = "0.0.0.0";
|
|
403
|
+
let callback;
|
|
404
|
+
const firstArg = args[0];
|
|
405
|
+
if (typeof firstArg === "number") {
|
|
406
|
+
port = firstArg;
|
|
407
|
+
const secondArg = args[1];
|
|
408
|
+
if (typeof secondArg === "string") {
|
|
409
|
+
hostname = secondArg;
|
|
410
|
+
callback = args[2] || args[3];
|
|
411
|
+
} else if (typeof secondArg === "function") {
|
|
412
|
+
callback = secondArg;
|
|
413
|
+
}
|
|
414
|
+
} else if (firstArg && typeof firstArg === "object") {
|
|
415
|
+
port = firstArg.port || 3e3;
|
|
416
|
+
hostname = firstArg.hostname || "0.0.0.0";
|
|
417
|
+
callback = args[1];
|
|
418
|
+
}
|
|
419
|
+
const self = this;
|
|
420
|
+
if (isNode) {
|
|
421
|
+
this.nativeServer = http.createServer((req, res) => {
|
|
422
|
+
const incomingMessage = new IncomingMessage(req);
|
|
423
|
+
const serverResponse = new ServerResponse(incomingMessage, res);
|
|
424
|
+
if (self.requestListener) {
|
|
425
|
+
self.requestListener(incomingMessage, serverResponse);
|
|
426
|
+
} else {
|
|
427
|
+
self.emit("request", incomingMessage, serverResponse);
|
|
428
|
+
}
|
|
429
|
+
});
|
|
430
|
+
this.nativeServer.on("upgrade", (req, socket, head) => {
|
|
431
|
+
self.emit("upgrade", req, socket, head);
|
|
432
|
+
});
|
|
433
|
+
this.nativeServer.listen(port, hostname, () => {
|
|
434
|
+
this._listening = true;
|
|
435
|
+
this.emit("listening");
|
|
436
|
+
if (callback) callback();
|
|
437
|
+
});
|
|
438
|
+
this.nativeServer.on("error", (err) => this.emit("error", err));
|
|
439
|
+
this.nativeServer.on("close", () => {
|
|
440
|
+
this._listening = false;
|
|
441
|
+
this.emit("close");
|
|
442
|
+
});
|
|
443
|
+
} else if (isBun) {
|
|
444
|
+
this.nativeServer = Bun.serve({
|
|
445
|
+
port,
|
|
446
|
+
hostname,
|
|
447
|
+
fetch: (req) => {
|
|
448
|
+
const urlObj = new URL(req.url);
|
|
449
|
+
const pathname = urlObj.pathname + urlObj.search;
|
|
450
|
+
let statusCode = 200;
|
|
451
|
+
let statusMessage = "OK";
|
|
452
|
+
let body = "";
|
|
453
|
+
const headers = /* @__PURE__ */ Object.create(null);
|
|
454
|
+
let responseReady = false;
|
|
455
|
+
const incomingMessage = {
|
|
456
|
+
method: req.method,
|
|
457
|
+
url: pathname,
|
|
458
|
+
headers: req.headers,
|
|
459
|
+
httpVersion: "1.1",
|
|
460
|
+
rawHeaders: [],
|
|
461
|
+
_req: req,
|
|
462
|
+
text: () => req.text(),
|
|
463
|
+
json: () => req.json()
|
|
464
|
+
};
|
|
465
|
+
const serverResponse = {
|
|
466
|
+
statusCode: 200,
|
|
467
|
+
statusMessage: "OK",
|
|
468
|
+
headersSent: false,
|
|
469
|
+
_headers: headers,
|
|
470
|
+
setHeader(name, value) {
|
|
471
|
+
headers[name.toLowerCase()] = Array.isArray(value) ? value.join(", ") : String(value);
|
|
472
|
+
return this;
|
|
473
|
+
},
|
|
474
|
+
getHeader(name) {
|
|
475
|
+
return headers[name.toLowerCase()];
|
|
476
|
+
},
|
|
477
|
+
getHeaders() {
|
|
478
|
+
return { ...headers };
|
|
479
|
+
},
|
|
480
|
+
writeHead(status, arg2, arg3) {
|
|
481
|
+
statusCode = status;
|
|
482
|
+
this.statusCode = status;
|
|
483
|
+
this.headersSent = true;
|
|
484
|
+
if (typeof arg2 === "string") {
|
|
485
|
+
statusMessage = arg2;
|
|
486
|
+
this.statusMessage = arg2;
|
|
487
|
+
if (arg3) {
|
|
488
|
+
for (const key in arg3) {
|
|
489
|
+
headers[key.toLowerCase()] = arg3[key];
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
} else if (arg2) {
|
|
493
|
+
for (const key in arg2) {
|
|
494
|
+
headers[key.toLowerCase()] = arg2[key];
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
return this;
|
|
498
|
+
},
|
|
499
|
+
write(chunk) {
|
|
500
|
+
if (!this.headersSent) {
|
|
501
|
+
this.writeHead(statusCode);
|
|
502
|
+
}
|
|
503
|
+
body += chunk;
|
|
504
|
+
return true;
|
|
505
|
+
},
|
|
506
|
+
end(chunk) {
|
|
507
|
+
if (chunk !== void 0) {
|
|
508
|
+
this.write(chunk);
|
|
509
|
+
}
|
|
510
|
+
if (!this.headersSent) {
|
|
511
|
+
this.writeHead(statusCode);
|
|
512
|
+
}
|
|
513
|
+
responseReady = true;
|
|
514
|
+
return this;
|
|
515
|
+
}
|
|
516
|
+
};
|
|
517
|
+
if (self.requestListener) {
|
|
518
|
+
self.requestListener(incomingMessage, serverResponse);
|
|
519
|
+
}
|
|
520
|
+
if (responseReady) {
|
|
521
|
+
return new Response(body, {
|
|
522
|
+
status: statusCode,
|
|
523
|
+
statusText: statusMessage,
|
|
524
|
+
headers
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
return new Promise((resolve) => {
|
|
528
|
+
serverResponse.end = (chunk) => {
|
|
529
|
+
if (chunk !== void 0) {
|
|
530
|
+
body += chunk;
|
|
531
|
+
}
|
|
532
|
+
resolve(new Response(body, {
|
|
533
|
+
status: statusCode,
|
|
534
|
+
statusText: statusMessage,
|
|
535
|
+
headers
|
|
536
|
+
}));
|
|
537
|
+
};
|
|
538
|
+
});
|
|
539
|
+
},
|
|
540
|
+
error: createErrorResponse
|
|
541
|
+
});
|
|
542
|
+
emitListeningWithCallback(this, callback);
|
|
543
|
+
} else if (isDeno) {
|
|
544
|
+
this.nativeServer = Deno.serve({
|
|
545
|
+
port,
|
|
546
|
+
hostname,
|
|
547
|
+
handler: (req) => {
|
|
548
|
+
return new Promise((resolve) => {
|
|
549
|
+
const incomingMessage = new IncomingMessage(req);
|
|
550
|
+
const serverResponse = new ServerResponse();
|
|
551
|
+
serverResponse._setResolver(resolve);
|
|
552
|
+
if (self.requestListener) {
|
|
553
|
+
self.requestListener(incomingMessage, serverResponse);
|
|
554
|
+
} else {
|
|
555
|
+
self.emit("request", incomingMessage, serverResponse);
|
|
556
|
+
}
|
|
557
|
+
});
|
|
558
|
+
},
|
|
559
|
+
onError: (error) => {
|
|
560
|
+
this.emit("error", error);
|
|
561
|
+
return createErrorResponse();
|
|
562
|
+
}
|
|
563
|
+
});
|
|
564
|
+
emitListeningWithCallback(this, callback);
|
|
565
|
+
}
|
|
566
|
+
return this;
|
|
567
|
+
}
|
|
568
|
+
close(callback) {
|
|
569
|
+
if (!this.nativeServer) {
|
|
570
|
+
if (callback) queueMicrotask(() => callback());
|
|
571
|
+
return this;
|
|
572
|
+
}
|
|
573
|
+
if (isNode) {
|
|
574
|
+
this.nativeServer.close(callback);
|
|
575
|
+
} else if (isBun) {
|
|
576
|
+
this.nativeServer.stop();
|
|
577
|
+
closeAndEmit(this, callback);
|
|
578
|
+
} else if (isDeno) {
|
|
579
|
+
this.nativeServer.shutdown();
|
|
580
|
+
closeAndEmit(this, callback);
|
|
581
|
+
}
|
|
582
|
+
return this;
|
|
583
|
+
}
|
|
584
|
+
address() {
|
|
585
|
+
if (!this.nativeServer) return null;
|
|
586
|
+
if (isNode) {
|
|
587
|
+
const addr = this.nativeServer.address();
|
|
588
|
+
if (!addr) return null;
|
|
589
|
+
if (typeof addr === "string") {
|
|
590
|
+
return createAddress(0, addr, "unix");
|
|
591
|
+
}
|
|
592
|
+
return addr;
|
|
593
|
+
} else if (isBun) {
|
|
594
|
+
return createAddress(this.nativeServer.port, this.nativeServer.hostname);
|
|
595
|
+
} else if (isDeno) {
|
|
596
|
+
const addr = this.nativeServer.addr;
|
|
597
|
+
return createAddress(addr.port, addr.hostname);
|
|
598
|
+
}
|
|
599
|
+
return null;
|
|
600
|
+
}
|
|
601
|
+
get listening() {
|
|
602
|
+
return this._listening;
|
|
603
|
+
}
|
|
604
|
+
};
|
|
605
|
+
ClientRequest = class extends EventEmitter {
|
|
606
|
+
constructor(_url, _options = {}) {
|
|
607
|
+
super();
|
|
608
|
+
}
|
|
609
|
+
write(_chunk) {
|
|
610
|
+
return true;
|
|
611
|
+
}
|
|
612
|
+
end(callback) {
|
|
613
|
+
queueCallback(callback);
|
|
614
|
+
}
|
|
615
|
+
};
|
|
616
|
+
Agent = class {
|
|
617
|
+
constructor(options) {
|
|
618
|
+
this.options = options;
|
|
619
|
+
}
|
|
620
|
+
};
|
|
621
|
+
http_default = {
|
|
622
|
+
createServer,
|
|
623
|
+
request,
|
|
624
|
+
get,
|
|
625
|
+
Server,
|
|
626
|
+
IncomingMessage,
|
|
627
|
+
ServerResponse,
|
|
628
|
+
Agent,
|
|
629
|
+
ClientRequest,
|
|
630
|
+
METHODS,
|
|
631
|
+
STATUS_CODES,
|
|
632
|
+
getRuntime
|
|
633
|
+
};
|
|
634
|
+
}
|
|
635
|
+
});
|
|
636
|
+
|
|
637
|
+
// src/wss.ts
|
|
638
|
+
import { EventEmitter as EventEmitter4 } from "events";
|
|
639
|
+
|
|
640
|
+
// src/https.ts
|
|
641
|
+
init_runtime();
|
|
642
|
+
import { EventEmitter as EventEmitter2 } from "events";
|
|
643
|
+
function queueCallback2(callback) {
|
|
644
|
+
if (callback) queueMicrotask(callback);
|
|
645
|
+
}
|
|
646
|
+
function createErrorResponse2() {
|
|
647
|
+
return new Response("Internal Server Error", { status: 500 });
|
|
648
|
+
}
|
|
649
|
+
function createAddress2(port, address, family = "IPv4") {
|
|
650
|
+
return { port, family, address };
|
|
651
|
+
}
|
|
652
|
+
function emitListeningWithCallback2(server, callback) {
|
|
653
|
+
server._listening = true;
|
|
654
|
+
server.emit("listening");
|
|
655
|
+
queueCallback2(callback);
|
|
656
|
+
}
|
|
657
|
+
function closeAndEmit2(server, callback) {
|
|
658
|
+
server._listening = false;
|
|
659
|
+
server.emit("close");
|
|
660
|
+
if (callback) queueMicrotask(() => callback());
|
|
661
|
+
}
|
|
662
|
+
function loadHttpClasses() {
|
|
663
|
+
const httpModule = (init_http(), __toCommonJS(http_exports));
|
|
664
|
+
return {
|
|
665
|
+
IncomingMessage: httpModule.IncomingMessage,
|
|
666
|
+
ServerResponse: httpModule.ServerResponse
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
var https2;
|
|
670
|
+
var Server2 = class extends EventEmitter2 {
|
|
671
|
+
constructor(options, requestListener) {
|
|
672
|
+
super();
|
|
673
|
+
this._listening = false;
|
|
674
|
+
this.options = options;
|
|
675
|
+
this.requestListener = requestListener;
|
|
676
|
+
}
|
|
677
|
+
listen(...args) {
|
|
678
|
+
let port = 3e3;
|
|
679
|
+
let hostname = "0.0.0.0";
|
|
680
|
+
let callback;
|
|
681
|
+
const firstArg = args[0];
|
|
682
|
+
if (typeof firstArg === "number") {
|
|
683
|
+
port = firstArg;
|
|
684
|
+
const secondArg = args[1];
|
|
685
|
+
if (typeof secondArg === "string") {
|
|
686
|
+
hostname = secondArg;
|
|
687
|
+
callback = args[2] || args[3];
|
|
688
|
+
} else if (typeof secondArg === "function") {
|
|
689
|
+
callback = secondArg;
|
|
690
|
+
}
|
|
691
|
+
} else if (firstArg && typeof firstArg === "object") {
|
|
692
|
+
port = firstArg.port || 3e3;
|
|
693
|
+
hostname = firstArg.hostname || "0.0.0.0";
|
|
694
|
+
callback = args[1];
|
|
695
|
+
}
|
|
696
|
+
const self = this;
|
|
697
|
+
if (isNode) {
|
|
698
|
+
const { IncomingMessage: IncomingMessage2, ServerResponse: ServerResponse2 } = loadHttpClasses();
|
|
699
|
+
if (!https2) https2 = __require("https");
|
|
700
|
+
this.nativeServer = https2.createServer(this.options, (req, res) => {
|
|
701
|
+
const incomingMessage = new IncomingMessage2(req);
|
|
702
|
+
const serverResponse = new ServerResponse2(incomingMessage, res);
|
|
703
|
+
if (self.requestListener) {
|
|
704
|
+
self.requestListener(incomingMessage, serverResponse);
|
|
705
|
+
} else {
|
|
706
|
+
self.emit("request", incomingMessage, serverResponse);
|
|
707
|
+
}
|
|
708
|
+
});
|
|
709
|
+
this.nativeServer.listen(port, hostname, () => {
|
|
710
|
+
this._listening = true;
|
|
711
|
+
this.emit("listening");
|
|
712
|
+
if (callback) callback();
|
|
713
|
+
});
|
|
714
|
+
this.nativeServer.on("error", (err) => this.emit("error", err));
|
|
715
|
+
this.nativeServer.on("close", () => {
|
|
716
|
+
this._listening = false;
|
|
717
|
+
this.emit("close");
|
|
718
|
+
});
|
|
719
|
+
} else if (isBun) {
|
|
720
|
+
const { IncomingMessage: IncomingMessage2, ServerResponse: ServerResponse2 } = loadHttpClasses();
|
|
721
|
+
const tlsOptions = {
|
|
722
|
+
port,
|
|
723
|
+
hostname,
|
|
724
|
+
fetch: (req) => {
|
|
725
|
+
return new Promise((resolve) => {
|
|
726
|
+
const incomingMessage = new IncomingMessage2(req);
|
|
727
|
+
const serverResponse = new ServerResponse2();
|
|
728
|
+
serverResponse._setResolver(resolve);
|
|
729
|
+
if (self.requestListener) {
|
|
730
|
+
self.requestListener(incomingMessage, serverResponse);
|
|
731
|
+
} else {
|
|
732
|
+
self.emit("request", incomingMessage, serverResponse);
|
|
733
|
+
}
|
|
734
|
+
});
|
|
735
|
+
},
|
|
736
|
+
error: (error) => {
|
|
737
|
+
this.emit("error", error);
|
|
738
|
+
return createErrorResponse2();
|
|
739
|
+
}
|
|
740
|
+
};
|
|
741
|
+
if (this.options.key || this.options.cert) {
|
|
742
|
+
tlsOptions.tls = {
|
|
743
|
+
key: this.options.key,
|
|
744
|
+
cert: this.options.cert,
|
|
745
|
+
ca: this.options.ca,
|
|
746
|
+
passphrase: this.options.passphrase
|
|
747
|
+
};
|
|
748
|
+
} else if (this.options.tls) {
|
|
749
|
+
tlsOptions.tls = this.options.tls;
|
|
750
|
+
}
|
|
751
|
+
this.nativeServer = Bun.serve(tlsOptions);
|
|
752
|
+
emitListeningWithCallback2(this, callback);
|
|
753
|
+
} else if (isDeno) {
|
|
754
|
+
const { IncomingMessage: IncomingMessage2, ServerResponse: ServerResponse2 } = loadHttpClasses();
|
|
755
|
+
const serveOptions = {
|
|
756
|
+
port,
|
|
757
|
+
hostname,
|
|
758
|
+
handler: (req) => {
|
|
759
|
+
return new Promise((resolve) => {
|
|
760
|
+
const incomingMessage = new IncomingMessage2(req);
|
|
761
|
+
const serverResponse = new ServerResponse2();
|
|
762
|
+
serverResponse._setResolver(resolve);
|
|
763
|
+
if (self.requestListener) {
|
|
764
|
+
self.requestListener(incomingMessage, serverResponse);
|
|
765
|
+
} else {
|
|
766
|
+
self.emit("request", incomingMessage, serverResponse);
|
|
767
|
+
}
|
|
768
|
+
});
|
|
769
|
+
},
|
|
770
|
+
onError: (error) => {
|
|
771
|
+
this.emit("error", error);
|
|
772
|
+
return createErrorResponse2();
|
|
773
|
+
}
|
|
774
|
+
};
|
|
775
|
+
if (this.options.key && this.options.cert) {
|
|
776
|
+
serveOptions.cert = this.options.cert;
|
|
777
|
+
serveOptions.key = this.options.key;
|
|
778
|
+
}
|
|
779
|
+
this.nativeServer = Deno.serve(serveOptions);
|
|
780
|
+
emitListeningWithCallback2(this, callback);
|
|
781
|
+
}
|
|
782
|
+
return this;
|
|
783
|
+
}
|
|
784
|
+
close(callback) {
|
|
785
|
+
if (!this.nativeServer) {
|
|
786
|
+
if (callback) queueMicrotask(() => callback());
|
|
787
|
+
return this;
|
|
788
|
+
}
|
|
789
|
+
if (isNode) {
|
|
790
|
+
this.nativeServer.close(callback);
|
|
791
|
+
} else if (isBun) {
|
|
792
|
+
this.nativeServer.stop();
|
|
793
|
+
closeAndEmit2(this, callback);
|
|
794
|
+
} else if (isDeno) {
|
|
795
|
+
this.nativeServer.shutdown();
|
|
796
|
+
closeAndEmit2(this, callback);
|
|
797
|
+
}
|
|
798
|
+
return this;
|
|
799
|
+
}
|
|
800
|
+
address() {
|
|
801
|
+
if (!this.nativeServer) return null;
|
|
802
|
+
if (isNode) {
|
|
803
|
+
const addr = this.nativeServer.address();
|
|
804
|
+
if (!addr) return null;
|
|
805
|
+
if (typeof addr === "string") {
|
|
806
|
+
return createAddress2(0, addr, "unix");
|
|
807
|
+
}
|
|
808
|
+
return addr;
|
|
809
|
+
} else if (isBun) {
|
|
810
|
+
return createAddress2(this.nativeServer.port, this.nativeServer.hostname);
|
|
811
|
+
} else if (isDeno) {
|
|
812
|
+
const addr = this.nativeServer.addr;
|
|
813
|
+
return createAddress2(addr.port, addr.hostname);
|
|
814
|
+
}
|
|
815
|
+
return null;
|
|
816
|
+
}
|
|
817
|
+
get listening() {
|
|
818
|
+
return this._listening;
|
|
819
|
+
}
|
|
820
|
+
};
|
|
821
|
+
function createServer2(options, requestListener) {
|
|
822
|
+
return new Server2(options, requestListener);
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
// src/ws.ts
|
|
826
|
+
init_runtime();
|
|
827
|
+
import { EventEmitter as EventEmitter3 } from "events";
|
|
828
|
+
var ReadyState = /* @__PURE__ */ ((ReadyState2) => {
|
|
829
|
+
ReadyState2[ReadyState2["CONNECTING"] = 0] = "CONNECTING";
|
|
830
|
+
ReadyState2[ReadyState2["OPEN"] = 1] = "OPEN";
|
|
831
|
+
ReadyState2[ReadyState2["CLOSING"] = 2] = "CLOSING";
|
|
832
|
+
ReadyState2[ReadyState2["CLOSED"] = 3] = "CLOSED";
|
|
833
|
+
return ReadyState2;
|
|
834
|
+
})(ReadyState || {});
|
|
835
|
+
var CLOSE_CODES = {
|
|
836
|
+
NORMAL: 1e3,
|
|
837
|
+
GOING_AWAY: 1001,
|
|
838
|
+
PROTOCOL_ERROR: 1002,
|
|
839
|
+
UNSUPPORTED_DATA: 1003,
|
|
840
|
+
NO_STATUS: 1005,
|
|
841
|
+
ABNORMAL: 1006,
|
|
842
|
+
INVALID_DATA: 1007,
|
|
843
|
+
POLICY_VIOLATION: 1008,
|
|
844
|
+
MESSAGE_TOO_BIG: 1009,
|
|
845
|
+
EXTENSION_REQUIRED: 1010,
|
|
846
|
+
INTERNAL_ERROR: 1011,
|
|
847
|
+
SERVICE_RESTART: 1012,
|
|
848
|
+
TRY_AGAIN_LATER: 1013,
|
|
849
|
+
BAD_GATEWAY: 1014,
|
|
850
|
+
TLS_HANDSHAKE_FAIL: 1015
|
|
851
|
+
};
|
|
852
|
+
function queueCallback3(callback, error) {
|
|
853
|
+
if (callback) {
|
|
854
|
+
queueMicrotask(() => callback(error));
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
function createNativeWebSocket(url, protocols) {
|
|
858
|
+
if (runtime === "node" && typeof globalThis.WebSocket === "undefined") {
|
|
859
|
+
throw new Error("WebSocket is not available. Please use Node.js 18+ or install ws package.");
|
|
860
|
+
}
|
|
861
|
+
return new globalThis.WebSocket(url, protocols);
|
|
862
|
+
}
|
|
863
|
+
var WebSocket = class extends EventEmitter3 {
|
|
864
|
+
constructor(address, protocols, _options) {
|
|
865
|
+
super();
|
|
866
|
+
this.readyState = 0 /* CONNECTING */;
|
|
867
|
+
this.protocol = "";
|
|
868
|
+
this.extensions = "";
|
|
869
|
+
this.binaryType = "nodebuffer";
|
|
870
|
+
this.url = typeof address === "string" ? address : address.toString();
|
|
871
|
+
const protocolsArray = Array.isArray(protocols) ? protocols : protocols ? [protocols] : void 0;
|
|
872
|
+
this._socket = createNativeWebSocket(this.url, protocolsArray);
|
|
873
|
+
this._setupNativeSocket();
|
|
874
|
+
}
|
|
875
|
+
_setupNativeSocket() {
|
|
876
|
+
this._socket.onopen = () => {
|
|
877
|
+
this.readyState = 1 /* OPEN */;
|
|
878
|
+
this.emit("open");
|
|
879
|
+
};
|
|
880
|
+
this._socket.onmessage = (event) => {
|
|
881
|
+
const isBinary = event.data instanceof ArrayBuffer || event.data instanceof Blob;
|
|
882
|
+
this.emit("message", event.data, isBinary);
|
|
883
|
+
};
|
|
884
|
+
this._socket.onclose = (event) => {
|
|
885
|
+
this.readyState = 3 /* CLOSED */;
|
|
886
|
+
this.emit("close", event.code, event.reason);
|
|
887
|
+
};
|
|
888
|
+
this._socket.onerror = () => {
|
|
889
|
+
this.emit("error", new Error("WebSocket error"));
|
|
890
|
+
};
|
|
891
|
+
}
|
|
892
|
+
/**
|
|
893
|
+
* Send data through WebSocket
|
|
894
|
+
*/
|
|
895
|
+
send(data, options, callback) {
|
|
896
|
+
const cb = typeof options === "function" ? options : callback;
|
|
897
|
+
if (this.readyState !== 1 /* OPEN */) {
|
|
898
|
+
return queueCallback3(cb, new Error("WebSocket is not open"));
|
|
899
|
+
}
|
|
900
|
+
try {
|
|
901
|
+
this._socket.send(data);
|
|
902
|
+
queueCallback3(cb);
|
|
903
|
+
} catch (error) {
|
|
904
|
+
queueCallback3(cb, error);
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
/**
|
|
908
|
+
* Close the WebSocket connection
|
|
909
|
+
*/
|
|
910
|
+
close(code, reason) {
|
|
911
|
+
if (this.readyState === 3 /* CLOSED */ || this.readyState === 2 /* CLOSING */) {
|
|
912
|
+
return;
|
|
913
|
+
}
|
|
914
|
+
this.readyState = 2 /* CLOSING */;
|
|
915
|
+
this._socket.close(code, typeof reason === "string" ? reason : reason?.toString());
|
|
916
|
+
}
|
|
917
|
+
/**
|
|
918
|
+
* Pause the socket (no-op for native WebSocket)
|
|
919
|
+
*/
|
|
920
|
+
pause() {
|
|
921
|
+
}
|
|
922
|
+
/**
|
|
923
|
+
* Resume the socket (no-op for native WebSocket)
|
|
924
|
+
*/
|
|
925
|
+
resume() {
|
|
926
|
+
}
|
|
927
|
+
/**
|
|
928
|
+
* Send a ping frame (no-op for native WebSocket)
|
|
929
|
+
*/
|
|
930
|
+
ping(_data, _mask, callback) {
|
|
931
|
+
queueCallback3(callback);
|
|
932
|
+
}
|
|
933
|
+
/**
|
|
934
|
+
* Send a pong frame (no-op for native WebSocket)
|
|
935
|
+
*/
|
|
936
|
+
pong(_data, _mask, callback) {
|
|
937
|
+
queueCallback3(callback);
|
|
938
|
+
}
|
|
939
|
+
/**
|
|
940
|
+
* Terminate the connection
|
|
941
|
+
*/
|
|
942
|
+
terminate() {
|
|
943
|
+
this._socket.close();
|
|
944
|
+
this.readyState = 3 /* CLOSED */;
|
|
945
|
+
}
|
|
946
|
+
/**
|
|
947
|
+
* Get buffered amount
|
|
948
|
+
*/
|
|
949
|
+
get bufferedAmount() {
|
|
950
|
+
return this._socket.bufferedAmount || 0;
|
|
951
|
+
}
|
|
952
|
+
};
|
|
953
|
+
var WebSocketServer = class extends EventEmitter3 {
|
|
954
|
+
constructor(options, callback) {
|
|
955
|
+
super();
|
|
956
|
+
this.clients = /* @__PURE__ */ new Set();
|
|
957
|
+
this.options = options || {};
|
|
958
|
+
this.path = options?.path || "/";
|
|
959
|
+
if (runtime === "node") {
|
|
960
|
+
if (options?.server) {
|
|
961
|
+
this._httpServer = options.server;
|
|
962
|
+
this._setupUpgradeHandler();
|
|
963
|
+
} else if (options?.noServer) {
|
|
964
|
+
} else {
|
|
965
|
+
const http2 = __require("http");
|
|
966
|
+
this._httpServer = http2.createServer();
|
|
967
|
+
this._setupUpgradeHandler();
|
|
968
|
+
if (options?.port) {
|
|
969
|
+
this._httpServer.listen(options.port, options.host, callback);
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
} else {
|
|
973
|
+
queueCallback3(callback);
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
_setupUpgradeHandler() {
|
|
977
|
+
this._httpServer.on("upgrade", (request2, socket, head) => {
|
|
978
|
+
console.log("[WebSocket] Upgrade request:", request2.url, "Expected:", this.path);
|
|
979
|
+
if (this.path && this.path !== "/" && request2.url !== this.path) {
|
|
980
|
+
console.log("[WebSocket] Path mismatch, ignoring");
|
|
981
|
+
return;
|
|
982
|
+
}
|
|
983
|
+
this.handleUpgrade(request2, socket, head, (client) => {
|
|
984
|
+
console.log("[WebSocket] Client connected");
|
|
985
|
+
this.emit("connection", client, request2);
|
|
986
|
+
});
|
|
987
|
+
});
|
|
988
|
+
}
|
|
989
|
+
/**
|
|
990
|
+
* Handle HTTP upgrade for WebSocket
|
|
991
|
+
*/
|
|
992
|
+
handleUpgrade(request2, socket, _head, callback) {
|
|
993
|
+
const key = request2.headers["sec-websocket-key"];
|
|
994
|
+
if (!key) {
|
|
995
|
+
socket.end("HTTP/1.1 400 Bad Request\r\n\r\n");
|
|
996
|
+
return;
|
|
997
|
+
}
|
|
998
|
+
const crypto = __require("crypto");
|
|
999
|
+
const acceptKey = crypto.createHash("sha1").update(key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").digest("base64");
|
|
1000
|
+
const headers = [
|
|
1001
|
+
"HTTP/1.1 101 Switching Protocols",
|
|
1002
|
+
"Upgrade: websocket",
|
|
1003
|
+
"Connection: Upgrade",
|
|
1004
|
+
`Sec-WebSocket-Accept: ${acceptKey}`,
|
|
1005
|
+
"",
|
|
1006
|
+
""
|
|
1007
|
+
];
|
|
1008
|
+
socket.write(headers.join("\r\n"));
|
|
1009
|
+
const client = this._createClientFromSocket(socket);
|
|
1010
|
+
if (this.options.clientTracking !== false) {
|
|
1011
|
+
this.clients.add(client);
|
|
1012
|
+
client.on("close", () => {
|
|
1013
|
+
this.clients.delete(client);
|
|
1014
|
+
});
|
|
1015
|
+
}
|
|
1016
|
+
callback(client);
|
|
1017
|
+
}
|
|
1018
|
+
_createClientFromSocket(socket) {
|
|
1019
|
+
const client = Object.create(WebSocket.prototype);
|
|
1020
|
+
EventEmitter3.call(client);
|
|
1021
|
+
client.readyState = 1 /* OPEN */;
|
|
1022
|
+
client.url = "ws://localhost";
|
|
1023
|
+
client.protocol = "";
|
|
1024
|
+
client.extensions = "";
|
|
1025
|
+
client.binaryType = "nodebuffer";
|
|
1026
|
+
client._socket = socket;
|
|
1027
|
+
socket.on("data", (data) => {
|
|
1028
|
+
try {
|
|
1029
|
+
const message = this._parseFrame(data);
|
|
1030
|
+
if (message) {
|
|
1031
|
+
client.emit("message", message, false);
|
|
1032
|
+
}
|
|
1033
|
+
} catch (error) {
|
|
1034
|
+
client.emit("error", error);
|
|
1035
|
+
}
|
|
1036
|
+
});
|
|
1037
|
+
socket.on("end", () => {
|
|
1038
|
+
client.readyState = 3 /* CLOSED */;
|
|
1039
|
+
client.emit("close", CLOSE_CODES.NORMAL, "");
|
|
1040
|
+
});
|
|
1041
|
+
socket.on("error", (error) => {
|
|
1042
|
+
client.emit("error", error);
|
|
1043
|
+
});
|
|
1044
|
+
client.send = (data, _options, callback) => {
|
|
1045
|
+
try {
|
|
1046
|
+
const frame = this._createFrame(data);
|
|
1047
|
+
socket.write(frame);
|
|
1048
|
+
queueCallback3(callback);
|
|
1049
|
+
} catch (error) {
|
|
1050
|
+
queueCallback3(callback, error);
|
|
1051
|
+
}
|
|
1052
|
+
};
|
|
1053
|
+
client.close = (_code, _reason) => {
|
|
1054
|
+
socket.end();
|
|
1055
|
+
client.readyState = 3 /* CLOSED */;
|
|
1056
|
+
};
|
|
1057
|
+
return client;
|
|
1058
|
+
}
|
|
1059
|
+
_parseFrame(data) {
|
|
1060
|
+
if (data.length < 2) return null;
|
|
1061
|
+
const firstByte = data[0];
|
|
1062
|
+
const secondByte = data[1];
|
|
1063
|
+
const opcode = firstByte & 15;
|
|
1064
|
+
const isMasked = (secondByte & 128) === 128;
|
|
1065
|
+
let payloadLength = secondByte & 127;
|
|
1066
|
+
let offset = 2;
|
|
1067
|
+
if (payloadLength === 126) {
|
|
1068
|
+
payloadLength = data.readUInt16BE(2);
|
|
1069
|
+
offset = 4;
|
|
1070
|
+
} else if (payloadLength === 127) {
|
|
1071
|
+
payloadLength = Number(data.readBigUInt64BE(2));
|
|
1072
|
+
offset = 10;
|
|
1073
|
+
}
|
|
1074
|
+
let payload = data.subarray(offset);
|
|
1075
|
+
if (isMasked) {
|
|
1076
|
+
const maskKey = data.subarray(offset, offset + 4);
|
|
1077
|
+
payload = data.subarray(offset + 4, offset + 4 + payloadLength);
|
|
1078
|
+
for (let i = 0; i < payload.length; i++) {
|
|
1079
|
+
payload[i] ^= maskKey[i % 4];
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
if (opcode === 1) {
|
|
1083
|
+
return payload.toString("utf8");
|
|
1084
|
+
}
|
|
1085
|
+
return null;
|
|
1086
|
+
}
|
|
1087
|
+
_createFrame(data) {
|
|
1088
|
+
const payload = typeof data === "string" ? Buffer.from(data) : data;
|
|
1089
|
+
const payloadLength = Buffer.isBuffer(payload) ? payload.length : 0;
|
|
1090
|
+
let frame;
|
|
1091
|
+
let offset = 2;
|
|
1092
|
+
if (payloadLength < 126) {
|
|
1093
|
+
frame = Buffer.allocUnsafe(2 + payloadLength);
|
|
1094
|
+
frame[1] = payloadLength;
|
|
1095
|
+
} else if (payloadLength < 65536) {
|
|
1096
|
+
frame = Buffer.allocUnsafe(4 + payloadLength);
|
|
1097
|
+
frame[1] = 126;
|
|
1098
|
+
frame.writeUInt16BE(payloadLength, 2);
|
|
1099
|
+
offset = 4;
|
|
1100
|
+
} else {
|
|
1101
|
+
frame = Buffer.allocUnsafe(10 + payloadLength);
|
|
1102
|
+
frame[1] = 127;
|
|
1103
|
+
frame.writeBigUInt64BE(BigInt(payloadLength), 2);
|
|
1104
|
+
offset = 10;
|
|
1105
|
+
}
|
|
1106
|
+
frame[0] = 129;
|
|
1107
|
+
if (Buffer.isBuffer(payload)) {
|
|
1108
|
+
payload.copy(frame, offset);
|
|
1109
|
+
}
|
|
1110
|
+
return frame;
|
|
1111
|
+
}
|
|
1112
|
+
/**
|
|
1113
|
+
* Close the server
|
|
1114
|
+
*/
|
|
1115
|
+
close(callback) {
|
|
1116
|
+
this.clients.forEach((client) => client.close());
|
|
1117
|
+
this.clients.clear();
|
|
1118
|
+
if (this._httpServer) {
|
|
1119
|
+
this._httpServer.close(callback);
|
|
1120
|
+
} else {
|
|
1121
|
+
this.emit("close");
|
|
1122
|
+
queueCallback3(callback);
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
/**
|
|
1126
|
+
* Check if server should handle request
|
|
1127
|
+
*/
|
|
1128
|
+
shouldHandle(request2) {
|
|
1129
|
+
if (this.path && request2.url !== this.path) {
|
|
1130
|
+
return false;
|
|
1131
|
+
}
|
|
1132
|
+
return true;
|
|
1133
|
+
}
|
|
1134
|
+
/**
|
|
1135
|
+
* Get server address
|
|
1136
|
+
*/
|
|
1137
|
+
address() {
|
|
1138
|
+
if (this._httpServer && this._httpServer.address) {
|
|
1139
|
+
return this._httpServer.address();
|
|
1140
|
+
}
|
|
1141
|
+
return null;
|
|
1142
|
+
}
|
|
1143
|
+
};
|
|
1144
|
+
|
|
1145
|
+
// src/wss.ts
|
|
1146
|
+
init_runtime();
|
|
1147
|
+
function queueCallback4(callback) {
|
|
1148
|
+
if (callback) {
|
|
1149
|
+
queueMicrotask(callback);
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
function buildHttpsOptions(options) {
|
|
1153
|
+
const httpsOptions = {};
|
|
1154
|
+
if (options?.key) httpsOptions.key = options.key;
|
|
1155
|
+
if (options?.cert) httpsOptions.cert = options.cert;
|
|
1156
|
+
if (options?.ca) httpsOptions.ca = options.ca;
|
|
1157
|
+
if (options?.passphrase) httpsOptions.passphrase = options.passphrase;
|
|
1158
|
+
if (options?.rejectUnauthorized !== void 0) httpsOptions.rejectUnauthorized = options.rejectUnauthorized;
|
|
1159
|
+
if (options?.requestCert !== void 0) httpsOptions.requestCert = options.requestCert;
|
|
1160
|
+
return httpsOptions;
|
|
1161
|
+
}
|
|
1162
|
+
function emitCloseWithCallback(emitter, callback) {
|
|
1163
|
+
emitter.emit("close");
|
|
1164
|
+
queueCallback4(callback);
|
|
1165
|
+
}
|
|
1166
|
+
var WSSClient = class extends WebSocket {
|
|
1167
|
+
constructor(address, protocols, options) {
|
|
1168
|
+
const urlString = typeof address === "string" ? address : address.toString();
|
|
1169
|
+
const secureUrl = urlString.replace(/^ws:\/\//i, "wss://");
|
|
1170
|
+
super(secureUrl, protocols, options);
|
|
1171
|
+
}
|
|
1172
|
+
};
|
|
1173
|
+
var WSSServer = class extends EventEmitter4 {
|
|
1174
|
+
constructor(options, callback) {
|
|
1175
|
+
super();
|
|
1176
|
+
this.clients = /* @__PURE__ */ new Set();
|
|
1177
|
+
this.options = options || {};
|
|
1178
|
+
this.path = options?.path || "/";
|
|
1179
|
+
if (runtime === "node") {
|
|
1180
|
+
if (options?.httpsServer) {
|
|
1181
|
+
this._httpsServer = options.httpsServer;
|
|
1182
|
+
this._setupServer(callback);
|
|
1183
|
+
} else if (options?.noServer) {
|
|
1184
|
+
this._wsServer = new WebSocketServer({ noServer: true });
|
|
1185
|
+
queueCallback4(callback);
|
|
1186
|
+
} else {
|
|
1187
|
+
this._httpsServer = createServer2(buildHttpsOptions(options));
|
|
1188
|
+
this._setupServer(callback);
|
|
1189
|
+
if (options?.port) {
|
|
1190
|
+
this._httpsServer.listen(options.port, options.host, callback);
|
|
1191
|
+
}
|
|
1192
|
+
}
|
|
1193
|
+
} else {
|
|
1194
|
+
this._wsServer = new WebSocketServer(options);
|
|
1195
|
+
queueCallback4(callback);
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
_setupServer(callback) {
|
|
1199
|
+
this._wsServer = new WebSocketServer({
|
|
1200
|
+
...this.options,
|
|
1201
|
+
server: this._httpsServer,
|
|
1202
|
+
noServer: false
|
|
1203
|
+
});
|
|
1204
|
+
this._wsServer.on("connection", (client, request2) => {
|
|
1205
|
+
if (this.options.clientTracking !== false) {
|
|
1206
|
+
this.clients.add(client);
|
|
1207
|
+
client.on("close", () => {
|
|
1208
|
+
this.clients.delete(client);
|
|
1209
|
+
});
|
|
1210
|
+
}
|
|
1211
|
+
this.emit("connection", client, request2);
|
|
1212
|
+
});
|
|
1213
|
+
this._wsServer.on("error", (error) => {
|
|
1214
|
+
this.emit("error", error);
|
|
1215
|
+
});
|
|
1216
|
+
if (!this.options?.port) {
|
|
1217
|
+
queueCallback4(callback);
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
/**
|
|
1221
|
+
* Handle HTTP upgrade for WebSocket
|
|
1222
|
+
*/
|
|
1223
|
+
handleUpgrade(request2, socket, head, callback) {
|
|
1224
|
+
if (this._wsServer) {
|
|
1225
|
+
this._wsServer.handleUpgrade(request2, socket, head, callback);
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
/**
|
|
1229
|
+
* Check if server should handle request
|
|
1230
|
+
*/
|
|
1231
|
+
shouldHandle(request2) {
|
|
1232
|
+
if (this._wsServer) {
|
|
1233
|
+
return this._wsServer.shouldHandle(request2);
|
|
1234
|
+
}
|
|
1235
|
+
if (this.path && request2.url !== this.path) {
|
|
1236
|
+
return false;
|
|
1237
|
+
}
|
|
1238
|
+
return true;
|
|
1239
|
+
}
|
|
1240
|
+
/**
|
|
1241
|
+
* Close the server
|
|
1242
|
+
*/
|
|
1243
|
+
close(callback) {
|
|
1244
|
+
this.clients.forEach((client) => client.close());
|
|
1245
|
+
this.clients.clear();
|
|
1246
|
+
if (this._wsServer) {
|
|
1247
|
+
this._wsServer.close(() => {
|
|
1248
|
+
if (this._httpsServer) {
|
|
1249
|
+
this._httpsServer.close(callback);
|
|
1250
|
+
} else {
|
|
1251
|
+
emitCloseWithCallback(this, callback);
|
|
1252
|
+
}
|
|
1253
|
+
});
|
|
1254
|
+
} else if (this._httpsServer) {
|
|
1255
|
+
this._httpsServer.close(callback);
|
|
1256
|
+
} else {
|
|
1257
|
+
emitCloseWithCallback(this, callback);
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
/**
|
|
1261
|
+
* Get server address
|
|
1262
|
+
*/
|
|
1263
|
+
address() {
|
|
1264
|
+
if (this._httpsServer && this._httpsServer.address) {
|
|
1265
|
+
return this._httpsServer.address();
|
|
1266
|
+
}
|
|
1267
|
+
if (this._wsServer) {
|
|
1268
|
+
return this._wsServer.address();
|
|
1269
|
+
}
|
|
1270
|
+
return null;
|
|
1271
|
+
}
|
|
1272
|
+
};
|
|
1273
|
+
function createWSSClient(address, protocols, options) {
|
|
1274
|
+
return new WSSClient(address, protocols, options);
|
|
1275
|
+
}
|
|
1276
|
+
function createWSSServer(options, callback) {
|
|
1277
|
+
return new WSSServer(options, callback);
|
|
1278
|
+
}
|
|
1279
|
+
function getRuntime2() {
|
|
1280
|
+
return runtime;
|
|
1281
|
+
}
|
|
1282
|
+
var wss_default = {
|
|
1283
|
+
WSSClient,
|
|
1284
|
+
WSSServer,
|
|
1285
|
+
createWSSClient,
|
|
1286
|
+
createWSSServer,
|
|
1287
|
+
ReadyState,
|
|
1288
|
+
CLOSE_CODES,
|
|
1289
|
+
getRuntime: getRuntime2
|
|
1290
|
+
};
|
|
1291
|
+
export {
|
|
1292
|
+
CLOSE_CODES,
|
|
1293
|
+
ReadyState,
|
|
1294
|
+
WSSClient,
|
|
1295
|
+
WSSServer,
|
|
1296
|
+
createWSSClient,
|
|
1297
|
+
createWSSServer,
|
|
1298
|
+
wss_default as default,
|
|
1299
|
+
getRuntime2 as getRuntime
|
|
1300
|
+
};
|