@cossackframework/node-adapter 0.8.3 → 0.9.1
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 +91 -19
- package/agent-docs.json +59 -0
- package/dist/adapter.d.ts +55 -0
- package/dist/application.d.ts +39 -0
- package/dist/index.d.ts +5 -55
- package/dist/index.js +445 -177
- package/dist/notifications.d.ts +4 -0
- package/dist/notifications.js +40 -0
- package/dist/shutdown.d.ts +24 -0
- package/dist/static-serve.d.ts +9 -0
- package/dist/storage.d.ts +26 -0
- package/dist/storage.js +241 -0
- package/dist/transport-routes-CjDWcC93.js +32 -0
- package/dist/vite.d.ts +23 -0
- package/dist/vite.js +128 -0
- package/package.json +28 -8
- package/dist/runtime.d.ts +0 -6
package/dist/index.js
CHANGED
|
@@ -1,78 +1,138 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
isOpen: (t) => t.readyState === e.OPEN,
|
|
12
|
-
onError: (e) => console.error("[Cossack] Ignoring malformed WebSocket message:", e)
|
|
13
|
-
});
|
|
14
|
-
}
|
|
15
|
-
addClient(e, t) {
|
|
16
|
-
super.addClient(e, t), e.on("close", () => {
|
|
17
|
-
this.removeClient(e);
|
|
18
|
-
}), e.on("message", (t) => {
|
|
19
|
-
this.onClientMessage(e, t.toString()).catch((e) => {
|
|
20
|
-
console.error("[Cossack] Error handling Node WebSocket message:", e);
|
|
21
|
-
});
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
//#endregion
|
|
1
|
+
import { t as e } from "./transport-routes-CjDWcC93.js";
|
|
2
|
+
import * as t from "fs";
|
|
3
|
+
import * as n from "path";
|
|
4
|
+
import { Readable as r } from "node:stream";
|
|
5
|
+
import { Hono as i } from "hono";
|
|
6
|
+
import a from "nodemailer";
|
|
7
|
+
import { WebSocket as o, WebSocketServer as s } from "ws";
|
|
8
|
+
import { InMemoryWebSocketRuntime as c, closeUnauthorizedSocket as l, setClientAddress as u } from "@cossackframework/core";
|
|
9
|
+
import { serve as d } from "@hono/node-server";
|
|
10
|
+
import { fileURLToPath as f, pathToFileURL as p } from "node:url";
|
|
26
11
|
//#region src/static-serve.ts
|
|
27
|
-
function
|
|
12
|
+
function m(e) {
|
|
28
13
|
let t = e, n = t.indexOf("?");
|
|
29
14
|
n !== -1 && (t = t.substring(0, n));
|
|
30
15
|
let r = t.indexOf("#");
|
|
31
16
|
return r !== -1 && (t = t.substring(0, r)), t.startsWith("/") || (t = "/" + t), t !== "/" && t.endsWith("/") && (t = t.slice(0, -1)), t;
|
|
32
17
|
}
|
|
33
|
-
function
|
|
34
|
-
let
|
|
35
|
-
|
|
36
|
-
|
|
18
|
+
function h(e) {
|
|
19
|
+
let t = e.startsWith("/") ? e : "/" + e;
|
|
20
|
+
for (; t.length > 1 && t.endsWith("/");) t = t.slice(0, -1);
|
|
21
|
+
return t;
|
|
22
|
+
}
|
|
23
|
+
function g(e) {
|
|
24
|
+
return `W/"${Math.floor(e.mtimeMs).toString(16)}-${e.size.toString(16)}"`;
|
|
25
|
+
}
|
|
26
|
+
function _(e, t) {
|
|
27
|
+
let n = t.replace(/^W\//, "");
|
|
28
|
+
return e.split(",").some((e) => {
|
|
29
|
+
let t = e.trim();
|
|
30
|
+
return t === "*" || t.replace(/^W\//, "") === n;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
function v(e, t) {
|
|
34
|
+
let n = e ? Date.parse(e) : NaN;
|
|
35
|
+
return Number.isFinite(n) && n <= t ? n : void 0;
|
|
36
|
+
}
|
|
37
|
+
function y(e, t, n, r) {
|
|
38
|
+
let i = e.req.header("if-none-match");
|
|
39
|
+
if (i) return _(i, t);
|
|
40
|
+
let a = v(e.req.header("if-modified-since"), r);
|
|
41
|
+
return a !== void 0 && n <= a;
|
|
42
|
+
}
|
|
43
|
+
function b(e, t) {
|
|
44
|
+
if (!e) return;
|
|
45
|
+
let n = /^\s*bytes\s*=\s*(\d*)\s*-\s*(\d*)\s*$/i.exec(e);
|
|
46
|
+
if (!n || !n[1] && !n[2]) return;
|
|
47
|
+
let [, r, i] = n;
|
|
48
|
+
if (!r) {
|
|
49
|
+
let e = Number(i);
|
|
50
|
+
return e === 0 || t === 0 ? null : {
|
|
51
|
+
start: Math.max(0, t - e),
|
|
52
|
+
end: t - 1
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
let a = Number(r), o = i ? Number(i) : Infinity;
|
|
56
|
+
if (!(o < a)) return a >= t ? null : {
|
|
57
|
+
start: a,
|
|
58
|
+
end: Math.min(o, t - 1)
|
|
37
59
|
};
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
60
|
+
}
|
|
61
|
+
function x(e, t, n) {
|
|
62
|
+
if (e === void 0) return !0;
|
|
63
|
+
let r = e.trim();
|
|
64
|
+
return r.startsWith("\"") || r.startsWith("W/") ? !1 : v(r, n) === t && n - t >= 1e3;
|
|
65
|
+
}
|
|
66
|
+
function S(e) {
|
|
67
|
+
let { root: i, index: a = !0, cacheControl: o } = e, s = h(e.prefix ?? "/"), c = n.resolve(i), l = c + n.sep, u = (e) => {
|
|
68
|
+
let t = n.resolve(e);
|
|
69
|
+
return t === c || t.startsWith(l);
|
|
70
|
+
};
|
|
71
|
+
return async (e, l) => {
|
|
72
|
+
let d = e.req.method;
|
|
73
|
+
if (d !== "GET" && d !== "HEAD") return l();
|
|
74
|
+
let f = m(e.req.path), p;
|
|
75
|
+
if (s === "/") p = f;
|
|
76
|
+
else if (f === s) p = "/";
|
|
77
|
+
else if (f.startsWith(s + "/")) p = f.slice(s.length);
|
|
78
|
+
else return l();
|
|
79
|
+
let h;
|
|
45
80
|
if (p === "/") {
|
|
46
|
-
if (!
|
|
47
|
-
|
|
48
|
-
} else
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
if (!l(m)) return c();
|
|
81
|
+
if (!a) return l();
|
|
82
|
+
h = n.join(i, "index.html");
|
|
83
|
+
} else h = n.join(i, p);
|
|
84
|
+
if (!u(h)) return l();
|
|
85
|
+
let _;
|
|
53
86
|
try {
|
|
54
|
-
let
|
|
55
|
-
if (
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
87
|
+
let i = await t.promises.stat(h);
|
|
88
|
+
if (i.isDirectory()) {
|
|
89
|
+
if (!a) return l();
|
|
90
|
+
h = n.join(h, "index.html"), i = await t.promises.stat(h);
|
|
91
|
+
}
|
|
92
|
+
let [s, u] = await Promise.all([t.promises.realpath(c), t.promises.realpath(h)]), p = n.relative(s, u);
|
|
93
|
+
if (p === ".." || p.startsWith(`..${n.sep}`) || n.isAbsolute(p) || !i.isFile() || d === "GET" && (_ = await t.promises.open(u, "r"), i = await _.stat(), !i.isFile())) return l();
|
|
94
|
+
let m = Date.now(), v = g(i), S = Math.min(Math.floor(i.mtimeMs / 1e3), Math.floor(m / 1e3)) * 1e3, C = new Headers({ ETag: v }), T = typeof o == "function" ? o(h, f) : o;
|
|
95
|
+
if (T && C.set("Cache-Control", T), y(e, v, S, m)) return new Response(null, {
|
|
96
|
+
status: 304,
|
|
97
|
+
headers: C
|
|
98
|
+
});
|
|
99
|
+
C.set("Last-Modified", new Date(S).toUTCString()), C.set("Accept-Ranges", "bytes");
|
|
100
|
+
let E = d === "GET" && x(e.req.header("if-range"), S, m) ? b(e.req.header("range"), i.size) : void 0;
|
|
101
|
+
if (E === null) return new Response(null, {
|
|
102
|
+
status: 416,
|
|
103
|
+
headers: {
|
|
104
|
+
"Content-Range": `bytes */${i.size}`,
|
|
105
|
+
"Accept-Ranges": "bytes",
|
|
106
|
+
"Content-Length": "0"
|
|
61
107
|
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
108
|
+
});
|
|
109
|
+
C.set("Content-Type", w(h));
|
|
110
|
+
let D = E?.start ?? 0, O = E?.end ?? i.size - 1, k = E ? 206 : 200;
|
|
111
|
+
if (E && C.set("Content-Range", `bytes ${D}-${O}/${i.size}`), C.set("Content-Length", String(O - D + 1)), !_ || i.size === 0) return new Response(null, {
|
|
112
|
+
status: k,
|
|
113
|
+
headers: C
|
|
114
|
+
});
|
|
115
|
+
let A = _.createReadStream({
|
|
116
|
+
start: D,
|
|
117
|
+
end: O,
|
|
118
|
+
autoClose: !0
|
|
119
|
+
});
|
|
120
|
+
return _ = void 0, new Response(r.toWeb(A), {
|
|
121
|
+
status: k,
|
|
122
|
+
headers: C
|
|
69
123
|
});
|
|
70
124
|
} catch {
|
|
71
|
-
return
|
|
125
|
+
return l();
|
|
126
|
+
} finally {
|
|
127
|
+
await _?.close().catch(() => {});
|
|
72
128
|
}
|
|
73
129
|
};
|
|
74
130
|
}
|
|
75
|
-
function
|
|
131
|
+
function C(e) {
|
|
132
|
+
let t = new i();
|
|
133
|
+
return t.use("*", S(e)), { fetch: async (e) => t.fetch(e) };
|
|
134
|
+
}
|
|
135
|
+
function w(e) {
|
|
76
136
|
return {
|
|
77
137
|
".html": "text/html",
|
|
78
138
|
".css": "text/css",
|
|
@@ -96,12 +156,12 @@ function f(e) {
|
|
|
96
156
|
".eot": "application/vnd.ms-fontobject",
|
|
97
157
|
".pdf": "application/pdf",
|
|
98
158
|
".zip": "application/zip"
|
|
99
|
-
}[
|
|
159
|
+
}[n.extname(e).toLowerCase()] || "application/octet-stream";
|
|
100
160
|
}
|
|
101
161
|
//#endregion
|
|
102
162
|
//#region src/email.ts
|
|
103
|
-
function
|
|
104
|
-
let t =
|
|
163
|
+
function T(e) {
|
|
164
|
+
let t = a.createTransport({
|
|
105
165
|
host: e.host,
|
|
106
166
|
port: e.port,
|
|
107
167
|
secure: e.secure ?? e.port === 465,
|
|
@@ -126,132 +186,340 @@ function p(e) {
|
|
|
126
186
|
} };
|
|
127
187
|
}
|
|
128
188
|
//#endregion
|
|
129
|
-
//#region
|
|
130
|
-
var
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
189
|
+
//#region ../core/dist/shared/server-diagnostics.js
|
|
190
|
+
var E = /* @__PURE__ */ new Set([
|
|
191
|
+
"action",
|
|
192
|
+
"action-cleanup",
|
|
193
|
+
"parse",
|
|
194
|
+
"dispatch",
|
|
195
|
+
"delivery",
|
|
196
|
+
"task-setup",
|
|
197
|
+
"task-cleanup",
|
|
198
|
+
"component-cleanup",
|
|
199
|
+
"service-setup-cleanup",
|
|
200
|
+
"service-cleanup",
|
|
201
|
+
"authentication",
|
|
202
|
+
"scope",
|
|
203
|
+
"upgrade",
|
|
204
|
+
"initialization",
|
|
205
|
+
"initial-state",
|
|
206
|
+
"socket",
|
|
207
|
+
"runtime-cleanup"
|
|
208
|
+
]), D = /* @__PURE__ */ new Set([
|
|
209
|
+
"COSSACK_RUNTIME_FAILED",
|
|
210
|
+
"COSSACK_RPC_INTERNAL_ERROR",
|
|
211
|
+
"COSSACK_RPC_CLEANUP_FAILED",
|
|
212
|
+
"COSSACK_WEBSOCKET_MESSAGE_INVALID",
|
|
213
|
+
"COSSACK_TASK_FAILED",
|
|
214
|
+
"COSSACK_TASK_CLEANUP_FAILED",
|
|
215
|
+
"COSSACK_COMPONENT_CLEANUP_FAILED",
|
|
216
|
+
"COSSACK_SERVICE_CLEANUP_FAILED"
|
|
217
|
+
]), O = /* @__PURE__ */ new Set([
|
|
218
|
+
"cloudflare",
|
|
219
|
+
"cloudflare-workers",
|
|
220
|
+
"node",
|
|
221
|
+
"browser",
|
|
222
|
+
"web",
|
|
223
|
+
"unknown"
|
|
224
|
+
]), k = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
225
|
+
function A(e, t) {
|
|
226
|
+
try {
|
|
227
|
+
let n = Object.getOwnPropertyDescriptor(e, t);
|
|
228
|
+
return n && "value" in n ? n.value : void 0;
|
|
229
|
+
} catch {
|
|
230
|
+
return;
|
|
139
231
|
}
|
|
140
|
-
return n;
|
|
141
232
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
233
|
+
function j(e, t) {
|
|
234
|
+
return typeof e == "string" && e.replace(/[^a-zA-Z0-9_.$/\[\]:*-]/g, "_").slice(0, t) || "unknown";
|
|
235
|
+
}
|
|
236
|
+
function M(e, t, n = "COSSACK_RUNTIME_FAILED") {
|
|
237
|
+
let r = A(e, "requestId"), i = A(e, "runtime"), a = A(e, "startedAt"), o = typeof a == "number" && Number.isFinite(a) ? performance.now() - a : 0;
|
|
238
|
+
console.error({
|
|
239
|
+
event: "cossack.runtime.failure",
|
|
240
|
+
requestId: typeof r == "string" && k.test(r) ? r : "unavailable",
|
|
241
|
+
route: j(A(e, "route"), 160),
|
|
242
|
+
action: j(A(e, "action"), 80),
|
|
243
|
+
component: j(A(e, "component"), 80),
|
|
244
|
+
runtime: typeof i == "string" && O.has(i) ? i : "unknown",
|
|
245
|
+
durationMs: Number.isFinite(o) ? Math.max(0, Math.round(o)) : 0,
|
|
246
|
+
stage: E.has(t) ? t : "unknown",
|
|
247
|
+
code: D.has(n) ? n : "COSSACK_RUNTIME_FAILED"
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
//#endregion
|
|
251
|
+
//#region src/adapter.ts
|
|
252
|
+
var N = 1024 * 1024;
|
|
253
|
+
function P(e) {
|
|
254
|
+
let t = e ?? 1048576;
|
|
255
|
+
if (!Number.isSafeInteger(t) || t < 1) throw TypeError("WebSocket maxPayload must be a positive integer byte count");
|
|
256
|
+
return t;
|
|
257
|
+
}
|
|
258
|
+
function F(t = {}) {
|
|
259
|
+
let n = new s({
|
|
260
|
+
noServer: !0,
|
|
261
|
+
maxPayload: P(t.maxPayload)
|
|
262
|
+
}), r = /* @__PURE__ */ new WeakMap();
|
|
263
|
+
n.on("headers", (e, t) => {
|
|
264
|
+
let n = r.get(t);
|
|
265
|
+
if (n) {
|
|
266
|
+
for (let [t, r] of n) /^(connection|upgrade|content-length|transfer-encoding|set-cookie|sec-websocket-.*|x-cossack-upgrade)$/i.test(t) || e.push(`${t}: ${r}`);
|
|
267
|
+
for (let t of n.getSetCookie()) e.push(`set-cookie: ${t}`);
|
|
162
268
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
269
|
+
});
|
|
270
|
+
let i = /* @__PURE__ */ new WeakMap(), a = /* @__PURE__ */ new Map(), d = /* @__PURE__ */ new Map(), f = /* @__PURE__ */ new Set(), p = /* @__PURE__ */ new Set(), m = /* @__PURE__ */ new Set(), h = t.maxInstances ?? 512, g = t.idleTimeoutMs ?? 15 * 6e4, _ = !1, v;
|
|
271
|
+
if (!Number.isInteger(h) || h < 1 || !Number.isFinite(g) || g < 0) throw TypeError("Node runtime requires positive integer maxInstances and nonnegative finite idleTimeoutMs");
|
|
272
|
+
let y = async (e) => {
|
|
273
|
+
let t = (await Promise.allSettled(e)).filter((e) => e.status === "rejected").map((e) => e.reason);
|
|
274
|
+
if (t.length) throw AggregateError(t, "Node runtime cleanup failed");
|
|
275
|
+
}, b = (e) => {
|
|
276
|
+
let t = e.dispose();
|
|
277
|
+
return f.add(t), t.then(() => f.delete(t), () => f.delete(t)), t;
|
|
278
|
+
}, x = async (e = !1) => {
|
|
279
|
+
let t = [], n = [...a].filter(([e, t]) => !d.has(e) && t.clients.size === 0).sort((e, t) => e[1].lastActive - t[1].lastActive);
|
|
280
|
+
for (let [r, i] of n) Date.now() - i.lastActive < g && a.size + d.size < h + +!e || (a.delete(r), t.push(b(i)));
|
|
281
|
+
await y(t);
|
|
282
|
+
}, S = (e) => {
|
|
283
|
+
if (_) return Promise.reject(/* @__PURE__ */ Error("Node runtime is disposed"));
|
|
284
|
+
let t = JSON.stringify([e.componentId, e.target]), n = d.get(t);
|
|
285
|
+
if (n) return n;
|
|
286
|
+
let r = a.get(t);
|
|
287
|
+
if (r) return Promise.resolve(r);
|
|
288
|
+
let i = x(!0);
|
|
289
|
+
if (a.size + d.size >= h) return i.then(() => {
|
|
290
|
+
throw Error("Node runtime instance limit reached");
|
|
291
|
+
});
|
|
292
|
+
let o = i.then(async () => {
|
|
293
|
+
if (_) throw Error("Node runtime is disposed");
|
|
294
|
+
let n = await e.createComponent(), r;
|
|
182
295
|
try {
|
|
183
|
-
|
|
296
|
+
let e = {
|
|
297
|
+
runtime: new c(n, { onError: () => M({ runtime: "node" }, "dispatch") }),
|
|
298
|
+
clients: /* @__PURE__ */ new Set(),
|
|
299
|
+
actions: /* @__PURE__ */ new Set(),
|
|
300
|
+
lastActive: Date.now(),
|
|
301
|
+
dispose: () => r ??= (async () => {
|
|
302
|
+
for (let t of e.clients) e.runtime.removeClient(t), t.close(1001, "Runtime shutting down"), t.terminate();
|
|
303
|
+
e.clients.clear(), await Promise.allSettled([...e.actions]), await n.destroyAsync();
|
|
304
|
+
})()
|
|
305
|
+
};
|
|
306
|
+
return a.set(t, e), e;
|
|
184
307
|
} catch (e) {
|
|
185
|
-
|
|
186
|
-
return;
|
|
308
|
+
throw await n.destroyAsync(), e;
|
|
187
309
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
}, _ = Reflect.getMetadata("page:options", f), v;
|
|
202
|
-
try {
|
|
203
|
-
v = _?.scope ? await _.scope(g) : `user:${p?.id || "anonymous"}`;
|
|
204
|
-
} catch (e) {
|
|
205
|
-
console.error("[Cossack] Failed to resolve Node WebSocket scope:", e), t.close(1008, "Invalid WebSocket scope");
|
|
310
|
+
}).finally(() => d.delete(t));
|
|
311
|
+
return d.set(t, o), o;
|
|
312
|
+
}, C = async (e, t) => {
|
|
313
|
+
if (e.on("error", (e) => M({ runtime: "node" }, "socket")), t.rejectAuthorization) {
|
|
314
|
+
l(e);
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
try {
|
|
318
|
+
let n = await S(t);
|
|
319
|
+
if (_ || e.readyState !== o.OPEN) {
|
|
320
|
+
await x();
|
|
206
321
|
return;
|
|
207
322
|
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
323
|
+
n.clients.add(e), n.runtime.addClient(e, t.user, t.authorize ? void 0 : t.context, t.authorize ? (e, n) => t.authorize(n) : void 0), n.lastActive = Date.now(), e.on("close", () => {
|
|
324
|
+
n.clients.delete(e), n.runtime.removeClient(e), n.lastActive = Date.now(), x().catch((e) => M({ runtime: "node" }, "runtime-cleanup", "COSSACK_COMPONENT_CLEANUP_FAILED"));
|
|
325
|
+
}), e.on("message", (t) => {
|
|
326
|
+
if (_ || e.readyState !== o.OPEN) return;
|
|
327
|
+
n.lastActive = Date.now();
|
|
328
|
+
let r = n.runtime.onClientMessage(e, t.toString());
|
|
329
|
+
n.actions.add(r), r.catch((t) => {
|
|
330
|
+
M({ runtime: "node" }, "dispatch"), e.close(1013, "Runtime unavailable");
|
|
331
|
+
}).finally(() => n.actions.delete(r));
|
|
332
|
+
}), await n.runtime.sendInitialState(e);
|
|
333
|
+
} catch {
|
|
334
|
+
M({ runtime: "node" }, "initialization"), e.close(1013, "Runtime unavailable");
|
|
335
|
+
}
|
|
336
|
+
}, w = () => {
|
|
337
|
+
if (v) return v;
|
|
338
|
+
_ = !0;
|
|
339
|
+
for (let e of m) e();
|
|
340
|
+
m.clear();
|
|
341
|
+
for (let e of p) e.destroy();
|
|
342
|
+
return p.clear(), v = (async () => {
|
|
343
|
+
await Promise.allSettled([...d.values()]);
|
|
344
|
+
let e = [...a.values()];
|
|
345
|
+
a.clear();
|
|
346
|
+
try {
|
|
347
|
+
await y([...f, ...e.map(b)]);
|
|
348
|
+
} finally {
|
|
349
|
+
n.close();
|
|
211
350
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
351
|
+
})(), v;
|
|
352
|
+
};
|
|
353
|
+
return {
|
|
354
|
+
name: "node",
|
|
355
|
+
get instanceCount() {
|
|
356
|
+
return a.size;
|
|
357
|
+
},
|
|
358
|
+
getClientMetadata: () => ({ platform: "web" }),
|
|
359
|
+
dispose: w,
|
|
360
|
+
handleWebSocketUpgrade(e, t) {
|
|
361
|
+
let n = i.get(e.req.raw);
|
|
362
|
+
return _ || !n ? new Response("WebSocket upgrade unavailable", { status: 503 }) : (n.upgrade = {
|
|
363
|
+
...t,
|
|
364
|
+
context: t.context ?? e
|
|
365
|
+
}, new Response(null, {
|
|
366
|
+
status: 204,
|
|
367
|
+
headers: { "x-cossack-upgrade": n.token }
|
|
368
|
+
}));
|
|
369
|
+
},
|
|
370
|
+
attach(a, o) {
|
|
371
|
+
if (_) throw Error("Node runtime is disposed");
|
|
372
|
+
let s = (s, c, l) => {
|
|
373
|
+
if (!e(s.url?.split("?", 1)[0] ?? "", "websocket")) {
|
|
374
|
+
(t.destroyUnmatchedUpgrades ?? (typeof a.listenerCount == "function" && a.listenerCount("upgrade") <= 1)) && !c.destroyed && (c.on("error", () => c.destroy()), c.end("HTTP/1.1 404 Not Found\r\nConnection: close\r\nContent-Length: 0\r\n\r\n", () => c.destroy()));
|
|
218
375
|
return;
|
|
219
376
|
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
377
|
+
p.add(c), c.once("close", () => p.delete(c)), c.on("error", () => c.destroy()), (async () => {
|
|
378
|
+
let e = new Headers();
|
|
379
|
+
for (let t = 0; t < s.rawHeaders.length; t += 2) e.append(s.rawHeaders[t], s.rawHeaders[t + 1]);
|
|
380
|
+
let t = s.socket.encrypted ? "https" : "http", a = new Request(new URL(s.url, `${t}://${s.headers.host}`), { headers: e }), d = {
|
|
381
|
+
token: crypto.randomUUID(),
|
|
382
|
+
upgrade: void 0
|
|
383
|
+
};
|
|
384
|
+
u(a, s.socket.remoteAddress), i.set(a, d);
|
|
385
|
+
let f;
|
|
386
|
+
try {
|
|
387
|
+
f = await o(a);
|
|
388
|
+
} finally {
|
|
389
|
+
i.delete(a);
|
|
390
|
+
}
|
|
391
|
+
if (!(_ || c.destroyed)) {
|
|
392
|
+
if (!d.upgrade || f.status !== 204 || f.headers.get("x-cossack-upgrade") !== d.token) {
|
|
393
|
+
let e = f.status >= 400 && f.status <= 599 ? f.status : 403;
|
|
394
|
+
await f.body?.cancel(), c.end(`HTTP/1.1 ${e} Rejected\r\nConnection: close\r\nContent-Length: 0\r\n\r\n`);
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
r.set(s, f.headers);
|
|
398
|
+
try {
|
|
399
|
+
n.handleUpgrade(s, c, l, (e) => {
|
|
400
|
+
C(e, d.upgrade);
|
|
401
|
+
});
|
|
402
|
+
} finally {
|
|
403
|
+
r.delete(s);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
})().catch((e) => {
|
|
407
|
+
M({ runtime: "node" }, "upgrade"), c.destroy();
|
|
408
|
+
});
|
|
409
|
+
}, c = () => {
|
|
410
|
+
w().catch((e) => M({ runtime: "node" }, "runtime-cleanup", "COSSACK_COMPONENT_CLEANUP_FAILED"));
|
|
411
|
+
};
|
|
412
|
+
a.on("upgrade", s), a.once("close", c);
|
|
413
|
+
let l = () => {
|
|
414
|
+
a.off("upgrade", s), a.off("close", c), m.delete(l);
|
|
415
|
+
};
|
|
416
|
+
return m.add(l), l;
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
//#endregion
|
|
421
|
+
//#region src/shutdown.ts
|
|
422
|
+
function I(e, t) {
|
|
423
|
+
let n = t.timeoutMs ?? 1e4;
|
|
424
|
+
if (!Number.isFinite(n) || n <= 0) throw TypeError("Shutdown timeout must be positive and finite");
|
|
425
|
+
let r = /* @__PURE__ */ new Set(), i = /* @__PURE__ */ new Set(), a, o = (e) => {
|
|
426
|
+
r.add(e), e.once("close", () => r.delete(e));
|
|
427
|
+
}, s = (e) => {
|
|
428
|
+
i.add(e), e.once("close", () => i.delete(e)), a && e.close();
|
|
429
|
+
}, c = (t, n) => {
|
|
430
|
+
n.once("finish", () => {
|
|
431
|
+
a && setImmediate(() => e.closeIdleConnections?.());
|
|
242
432
|
});
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
let
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
433
|
+
}, l = () => {
|
|
434
|
+
process.off("SIGTERM", d), process.off("SIGINT", d), e.off("connection", o), e.off("request", c), e.off("session", s);
|
|
435
|
+
}, u = () => a ??= (async () => {
|
|
436
|
+
let a = !1, o = !1, s = new Promise((t) => e.close(() => t()));
|
|
437
|
+
e.closeIdleConnections?.();
|
|
438
|
+
for (let e of i) e.close();
|
|
439
|
+
let c = Promise.resolve().then(() => t.dispose()).then(() => {
|
|
440
|
+
a = !0;
|
|
441
|
+
}, () => {
|
|
442
|
+
a = !0, o = !0;
|
|
443
|
+
}), u, d = await Promise.race([Promise.all([s, c]).then(() => !1), new Promise((e) => {
|
|
444
|
+
u = setTimeout(() => e(!0), n);
|
|
445
|
+
})]);
|
|
446
|
+
if (clearTimeout(u), d) {
|
|
447
|
+
e.closeAllConnections?.();
|
|
448
|
+
for (let e of i) e.destroy();
|
|
449
|
+
for (let e of r) e.destroy();
|
|
450
|
+
}
|
|
451
|
+
r.clear(), i.clear(), l();
|
|
452
|
+
let f = {
|
|
453
|
+
forced: d,
|
|
454
|
+
cleanupCompleted: a,
|
|
455
|
+
cleanupFailed: o
|
|
456
|
+
};
|
|
457
|
+
return t.onComplete?.(f), f;
|
|
458
|
+
})(), d = () => {
|
|
459
|
+
u();
|
|
460
|
+
};
|
|
461
|
+
return e.on("connection", o), e.on("request", c), e.on("session", s), process.on("SIGTERM", d), process.on("SIGINT", d), {
|
|
462
|
+
shutdown: u,
|
|
463
|
+
uninstall: l
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
//#endregion
|
|
467
|
+
//#region ../core/dist/shared/static-assets.js
|
|
468
|
+
var L = "assets";
|
|
469
|
+
Object.freeze(["/*", `!/${L}/*`]);
|
|
470
|
+
//#endregion
|
|
471
|
+
//#region src/application.ts
|
|
472
|
+
async function R(e) {
|
|
473
|
+
let t = new i();
|
|
474
|
+
t.use(`/${L}/*`, S({
|
|
475
|
+
root: f(new URL(`../client/${L}`, e.entry)),
|
|
476
|
+
prefix: `/${L}`,
|
|
477
|
+
index: !1,
|
|
478
|
+
cacheControl: (e, t) => /\.[a-zA-Z0-9_-]{8,}\.[^/]+$/.test(t) ? "public, max-age=31536000, immutable" : "public, max-age=0, must-revalidate"
|
|
479
|
+
})), t.all("*", (t) => e.fetch(t.req.raw, t.env));
|
|
480
|
+
let n = async (t = process.env) => {
|
|
481
|
+
let n = {
|
|
482
|
+
...t,
|
|
483
|
+
ASSETS: C({
|
|
484
|
+
root: f(new URL("../client", e.entry)),
|
|
485
|
+
index: !1,
|
|
486
|
+
cacheControl: "public, max-age=0, must-revalidate"
|
|
487
|
+
})
|
|
488
|
+
};
|
|
489
|
+
if (t.SMTP_HOST) {
|
|
490
|
+
let { SMTP_USER: e, SMTP_PASS: r } = t;
|
|
491
|
+
if (!e || !r) throw Error("SMTP_HOST requires SMTP_USER and SMTP_PASS");
|
|
492
|
+
n.EMAIL = T({
|
|
493
|
+
host: t.SMTP_HOST,
|
|
494
|
+
port: Number(t.SMTP_PORT ?? 587),
|
|
495
|
+
secure: t.SMTP_SECURE === "true",
|
|
496
|
+
auth: {
|
|
497
|
+
user: e,
|
|
498
|
+
pass: r
|
|
499
|
+
},
|
|
500
|
+
from: t.MAIL_FROM ?? "no-reply@example.com"
|
|
501
|
+
});
|
|
253
502
|
}
|
|
503
|
+
return {
|
|
504
|
+
...n,
|
|
505
|
+
...await e.bindings?.(t)
|
|
506
|
+
};
|
|
507
|
+
}, r = e.dev ? {} : await n();
|
|
508
|
+
if (process.argv[1] && e.entry === p(process.argv[1]).href) {
|
|
509
|
+
let n = (e, n) => (u(e, n?.incoming?.socket?.remoteAddress), t.fetch(e, r)), i = d({
|
|
510
|
+
fetch: n,
|
|
511
|
+
port: Number(process.env.PORT) || 3e3
|
|
512
|
+
});
|
|
513
|
+
e.runtime.attach(i, n), e.started?.(r), I(i, {
|
|
514
|
+
dispose: () => e.runtime.dispose(),
|
|
515
|
+
onComplete: (e) => process.exit(e.forced || e.cleanupFailed ? 1 : 0)
|
|
516
|
+
});
|
|
254
517
|
}
|
|
255
|
-
|
|
518
|
+
return {
|
|
519
|
+
app: t,
|
|
520
|
+
createEnv: n,
|
|
521
|
+
env: r
|
|
522
|
+
};
|
|
523
|
+
}
|
|
256
524
|
//#endregion
|
|
257
|
-
export {
|
|
525
|
+
export { N as DEFAULT_WEBSOCKET_MAX_PAYLOAD, F as createNodeAdapter, T as createNodeEmailSender, C as createStaticAssets, I as installNodeShutdown, R as nodeApplication, S as serveStatic };
|