@cossackframework/node-adapter 0.8.1 → 0.8.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.
- package/README.md +29 -15
- package/dist/index.d.ts +11 -0
- package/dist/index.js +148 -121
- package/dist/runtime.d.ts +3 -10
- package/dist/static-serve.d.ts +2 -0
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
# Cossack Framework Node Adapter
|
|
2
2
|
|
|
3
|
-
This package
|
|
3
|
+
This package runs Cossack applications on Node.js. It handles HTTP requests,
|
|
4
|
+
WebSocket upgrades, and static files. It also provides runtime polyfills so
|
|
5
|
+
application code can work on Cloudflare Workers and Node.js.
|
|
4
6
|
|
|
5
7
|
## Installation
|
|
6
|
-
|
|
8
|
+
See the [Installation Guide](https://cossack.dev/docs/installation) to set up
|
|
9
|
+
Cossack and its packages.
|
|
7
10
|
|
|
8
11
|
## Exports
|
|
9
12
|
|
|
10
|
-
- `CossackNodeAdapter`
|
|
11
|
-
- `
|
|
12
|
-
- `
|
|
13
|
-
- `
|
|
13
|
+
- `CossackNodeAdapter` : handles WebSocket upgrades and component bootstrapping on a Node `http.Server`.
|
|
14
|
+
- `nodeRuntimeAdapter` : pass to `createApp()` so SSR emits Node process-local WebSocket targets.
|
|
15
|
+
- `serveStatic` : Node `fs`-based Hono middleware for serving static assets.
|
|
16
|
+
- `NodeWebSocketRuntime` : the WebSocket runtime used by the adapter.
|
|
17
|
+
- `createNodeEmailSender` : Node.js polyfill for Cloudflare's `send_email` binding (`env.EMAIL`).
|
|
14
18
|
|
|
15
19
|
## Unified `env.EMAIL` (Cloudflare email binding polyfill)
|
|
16
20
|
|
|
@@ -30,11 +34,19 @@ Build the sender from SMTP env vars and inject it into `env`, then pass `env` bo
|
|
|
30
34
|
```ts
|
|
31
35
|
import { serve } from '@hono/node-server';
|
|
32
36
|
import { createApp } from '@cossackframework/framework/router';
|
|
33
|
-
import {
|
|
37
|
+
import {
|
|
38
|
+
CossackNodeAdapter,
|
|
39
|
+
createNodeEmailSender,
|
|
40
|
+
nodeRuntimeAdapter,
|
|
41
|
+
} from '@cossackframework/node-adapter';
|
|
34
42
|
import { App } from './App';
|
|
35
43
|
import { template } from './root';
|
|
36
44
|
|
|
37
|
-
const app = createApp({
|
|
45
|
+
const app = createApp({
|
|
46
|
+
AppComponent: App,
|
|
47
|
+
htmlTemplate: template,
|
|
48
|
+
runtimeAdapter: nodeRuntimeAdapter,
|
|
49
|
+
});
|
|
38
50
|
|
|
39
51
|
const env = {
|
|
40
52
|
EMAIL: createNodeEmailSender({
|
|
@@ -55,6 +67,8 @@ const server = serve(
|
|
|
55
67
|
// WebSocket: pass the same env so `@Server` methods over WS see `this.env.EMAIL`.
|
|
56
68
|
new CossackNodeAdapter({
|
|
57
69
|
server,
|
|
70
|
+
// Keys match the routePath metadata emitted by Cossack, for example
|
|
71
|
+
// new Map([['/account/:id', AccountPage]]).
|
|
58
72
|
componentRegistry,
|
|
59
73
|
env,
|
|
60
74
|
});
|
|
@@ -63,13 +77,13 @@ new CossackNodeAdapter({
|
|
|
63
77
|
### Environment variables
|
|
64
78
|
Configure via `.dev.vars` (Cloudflare dev) or your shell/`.env` (Node):
|
|
65
79
|
|
|
66
|
-
| Variable
|
|
80
|
+
| Variable | Description | Default |
|
|
67
81
|
| -------------- | -------------------------------------------- | ------------------ |
|
|
68
|
-
| `SMTP_HOST`
|
|
69
|
-
| `SMTP_PORT`
|
|
70
|
-
| `SMTP_SECURE`
|
|
71
|
-
| `SMTP_USER`
|
|
72
|
-
| `SMTP_PASS`
|
|
73
|
-
| `MAIL_FROM`
|
|
82
|
+
| `SMTP_HOST` | SMTP server host (e.g. `smtp.gmail.com`) | : |
|
|
83
|
+
| `SMTP_PORT` | SMTP server port | `587` |
|
|
84
|
+
| `SMTP_SECURE` | Use TLS (`"true"` for port 465) | `port === 465` |
|
|
85
|
+
| `SMTP_USER` | SMTP username | : |
|
|
86
|
+
| `SMTP_PASS` | SMTP password | : |
|
|
87
|
+
| `MAIL_FROM` | Default `from` address when omitted | `no-reply@example.com` |
|
|
74
88
|
|
|
75
89
|
Now `await this.env.EMAIL.send(...)` works identically in `@Server` methods on both Cloudflare Workers and Node.js.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import type { IncomingMessage, Server } from 'http';
|
|
2
2
|
import type { Cossack } from '@cossackframework/core';
|
|
3
3
|
export * from './runtime';
|
|
4
|
+
/** Pass to `createApp({ runtimeAdapter })` so SSR emits process-runtime targets. */
|
|
5
|
+
export declare const nodeRuntimeAdapter: {
|
|
6
|
+
readonly name: 'node';
|
|
7
|
+
};
|
|
4
8
|
export { serveStatic, type StaticServeOptions } from './static-serve';
|
|
5
9
|
export { createNodeEmailSender, type NodeEmailOptions, type NodeEmailSender, type EmailMessageInput, type EmailSendResult, type EmailAddress, } from './email';
|
|
6
10
|
export interface CossackNodeAdapterOptions {
|
|
@@ -30,6 +34,10 @@ export interface CossackNodeAdapterOptions {
|
|
|
30
34
|
* `this.env.EMAIL.send(...)` call works on both runtimes.
|
|
31
35
|
*/
|
|
32
36
|
env?: Record<string, unknown>;
|
|
37
|
+
/** Maximum number of process-local component instances. Defaults to 512. */
|
|
38
|
+
maxInstances?: number;
|
|
39
|
+
/** Evict disconnected instances after this idle period. Defaults to 15 minutes. */
|
|
40
|
+
idleTimeoutMs?: number;
|
|
33
41
|
}
|
|
34
42
|
export declare class CossackNodeAdapter {
|
|
35
43
|
private wss;
|
|
@@ -39,6 +47,9 @@ export declare class CossackNodeAdapter {
|
|
|
39
47
|
private authenticate?;
|
|
40
48
|
private defaultUser;
|
|
41
49
|
private env?;
|
|
50
|
+
private maxInstances;
|
|
51
|
+
private idleTimeoutMs;
|
|
42
52
|
constructor(options: CossackNodeAdapterOptions);
|
|
43
53
|
private handleUpgrade;
|
|
54
|
+
private pruneInstances;
|
|
44
55
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,114 +1,87 @@
|
|
|
1
1
|
import { WebSocket as e, WebSocketServer as t } from "ws";
|
|
2
|
-
import {
|
|
3
|
-
import { URL as
|
|
4
|
-
import * as
|
|
5
|
-
import * as
|
|
6
|
-
import
|
|
2
|
+
import { InMemoryWebSocketRuntime as n, createInstance as r, isOriginAllowed as i } from "@cossackframework/core";
|
|
3
|
+
import { URL as a } from "url";
|
|
4
|
+
import * as o from "fs";
|
|
5
|
+
import * as s from "path";
|
|
6
|
+
import c from "nodemailer";
|
|
7
7
|
//#region src/runtime.ts
|
|
8
|
-
var
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
var l = class extends n {
|
|
9
|
+
constructor(t) {
|
|
10
|
+
super(t, {
|
|
11
|
+
isOpen: (t) => t.readyState === e.OPEN,
|
|
12
|
+
onError: (e) => console.error("[Cossack] Ignoring malformed WebSocket message:", e)
|
|
13
|
+
});
|
|
13
14
|
}
|
|
14
15
|
addClient(e, t) {
|
|
15
|
-
|
|
16
|
-
this.
|
|
16
|
+
super.addClient(e, t), e.on("close", () => {
|
|
17
|
+
this.removeClient(e);
|
|
17
18
|
}), e.on("message", (t) => {
|
|
18
19
|
this.onClientMessage(e, t.toString()).catch((e) => {
|
|
19
20
|
console.error("[Cossack] Error handling Node WebSocket message:", e);
|
|
20
21
|
});
|
|
21
22
|
});
|
|
22
23
|
}
|
|
23
|
-
async onClientMessage(e, t) {
|
|
24
|
-
let n = e, r;
|
|
25
|
-
try {
|
|
26
|
-
r = JSON.parse(t);
|
|
27
|
-
} catch (e) {
|
|
28
|
-
console.error("[Cossack] Ignoring malformed WebSocket message:", e);
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
if (!r || typeof r != "object" || r.type !== "action" || typeof r.action != "string" || !Array.isArray(r.payload)) return;
|
|
32
|
-
let i = n.user;
|
|
33
|
-
await this.component.executeAction(r.action, r.payload, i, e);
|
|
34
|
-
}
|
|
35
|
-
broadcastState(t) {
|
|
36
|
-
let n = JSON.stringify({
|
|
37
|
-
type: "state-update",
|
|
38
|
-
state: t
|
|
39
|
-
});
|
|
40
|
-
for (let t of this.clients) t.readyState === e.OPEN && t.send(n);
|
|
41
|
-
}
|
|
42
|
-
broadcastEvent(t, n) {
|
|
43
|
-
let r = JSON.stringify({
|
|
44
|
-
type: "event",
|
|
45
|
-
eventName: t,
|
|
46
|
-
payload: n
|
|
47
|
-
});
|
|
48
|
-
for (let t of this.clients) t.readyState === e.OPEN && t.send(r);
|
|
49
|
-
}
|
|
50
|
-
sendClientAction(t, n, r) {
|
|
51
|
-
let i = t, a = JSON.stringify({
|
|
52
|
-
type: "client-action",
|
|
53
|
-
action: n,
|
|
54
|
-
payload: r
|
|
55
|
-
});
|
|
56
|
-
i.readyState === e.OPEN && i.send(a);
|
|
57
|
-
}
|
|
58
|
-
async persistState() {}
|
|
59
24
|
};
|
|
60
25
|
//#endregion
|
|
61
26
|
//#region src/static-serve.ts
|
|
62
|
-
function
|
|
27
|
+
function u(e) {
|
|
63
28
|
let t = e, n = t.indexOf("?");
|
|
64
29
|
n !== -1 && (t = t.substring(0, n));
|
|
65
30
|
let r = t.indexOf("#");
|
|
66
31
|
return r !== -1 && (t = t.substring(0, r)), t.startsWith("/") || (t = "/" + t), t !== "/" && t.endsWith("/") && (t = t.slice(0, -1)), t;
|
|
67
32
|
}
|
|
68
|
-
function
|
|
69
|
-
let { root: t, prefix: n = "/", index: r = !0 } = e,
|
|
70
|
-
let t =
|
|
71
|
-
return t ===
|
|
33
|
+
function d(e) {
|
|
34
|
+
let { root: t, prefix: n = "/", index: r = !0, cacheControl: i } = e, a = s.resolve(t), c = a + s.sep, l = (e) => {
|
|
35
|
+
let t = s.resolve(e);
|
|
36
|
+
return t === a || t.startsWith(c);
|
|
72
37
|
};
|
|
73
|
-
return async (e,
|
|
74
|
-
if (e.req.method !== "GET" && e.req.method !== "HEAD") return
|
|
75
|
-
let
|
|
76
|
-
if (!
|
|
77
|
-
let
|
|
78
|
-
|
|
79
|
-
let
|
|
80
|
-
if (
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
38
|
+
return async (e, c) => {
|
|
39
|
+
if (e.req.method !== "GET" && e.req.method !== "HEAD") return c();
|
|
40
|
+
let d = u(e.req.path);
|
|
41
|
+
if (!d.startsWith(n)) return c();
|
|
42
|
+
let p = d.substring(n.length);
|
|
43
|
+
p === "" && (p = "/");
|
|
44
|
+
let m;
|
|
45
|
+
if (p === "/") {
|
|
46
|
+
if (!r) return c();
|
|
47
|
+
m = s.join(t, "index.html");
|
|
48
|
+
} else if (m = s.join(t, p), r && !o.existsSync(m)) {
|
|
49
|
+
let e = s.join(m, "index.html");
|
|
50
|
+
o.existsSync(e) && (m = e);
|
|
84
51
|
}
|
|
85
|
-
if (!
|
|
52
|
+
if (!l(m)) return c();
|
|
86
53
|
try {
|
|
87
|
-
let
|
|
88
|
-
if (
|
|
89
|
-
let
|
|
54
|
+
let t = await o.promises.stat(m);
|
|
55
|
+
if (t.isDirectory()) if (r) {
|
|
56
|
+
let e = s.join(m, "index.html");
|
|
90
57
|
try {
|
|
91
|
-
await
|
|
58
|
+
await o.promises.access(e), m = e, t = await o.promises.stat(m);
|
|
92
59
|
} catch {
|
|
93
|
-
return
|
|
60
|
+
return c();
|
|
94
61
|
}
|
|
95
|
-
} else return
|
|
96
|
-
let
|
|
97
|
-
|
|
62
|
+
} else return c();
|
|
63
|
+
let [n, l] = await Promise.all([o.promises.realpath(a), o.promises.realpath(m)]), u = s.relative(n, l);
|
|
64
|
+
if (u === ".." || u.startsWith(`..${s.sep}`) || s.isAbsolute(u) || !t.isFile()) return c();
|
|
65
|
+
let p = e.req.method === "HEAD" ? null : await o.promises.readFile(l), h = f(m), g = new Headers({ "Content-Type": h }), _ = typeof i == "function" ? i(m, d) : i;
|
|
66
|
+
return _ && g.set("Cache-Control", _), new Response(p === null ? null : new Uint8Array(p), {
|
|
98
67
|
status: 200,
|
|
99
|
-
headers:
|
|
68
|
+
headers: g
|
|
100
69
|
});
|
|
101
70
|
} catch {
|
|
102
|
-
return
|
|
71
|
+
return c();
|
|
103
72
|
}
|
|
104
73
|
};
|
|
105
74
|
}
|
|
106
|
-
function
|
|
75
|
+
function f(e) {
|
|
107
76
|
return {
|
|
108
77
|
".html": "text/html",
|
|
109
78
|
".css": "text/css",
|
|
110
79
|
".js": "application/javascript",
|
|
80
|
+
".mjs": "application/javascript",
|
|
81
|
+
".cjs": "application/javascript",
|
|
111
82
|
".json": "application/json",
|
|
83
|
+
".map": "application/json",
|
|
84
|
+
".webmanifest": "application/manifest+json",
|
|
112
85
|
".xml": "application/xml",
|
|
113
86
|
".txt": "text/plain",
|
|
114
87
|
".png": "image/png",
|
|
@@ -123,12 +96,12 @@ function d(e) {
|
|
|
123
96
|
".eot": "application/vnd.ms-fontobject",
|
|
124
97
|
".pdf": "application/pdf",
|
|
125
98
|
".zip": "application/zip"
|
|
126
|
-
}[
|
|
99
|
+
}[s.extname(e).toLowerCase()] || "application/octet-stream";
|
|
127
100
|
}
|
|
128
101
|
//#endregion
|
|
129
102
|
//#region src/email.ts
|
|
130
|
-
function
|
|
131
|
-
let t =
|
|
103
|
+
function p(e) {
|
|
104
|
+
let t = c.createTransport({
|
|
132
105
|
host: e.host,
|
|
133
106
|
port: e.port,
|
|
134
107
|
secure: e.secure ?? e.port === 465,
|
|
@@ -154,7 +127,19 @@ function f(e) {
|
|
|
154
127
|
}
|
|
155
128
|
//#endregion
|
|
156
129
|
//#region src/index.ts
|
|
157
|
-
var
|
|
130
|
+
var m = { name: "node" };
|
|
131
|
+
function h(e) {
|
|
132
|
+
if (e === null) return {};
|
|
133
|
+
let t = JSON.parse(e);
|
|
134
|
+
if (!t || typeof t != "object" || Array.isArray(t)) throw TypeError("WebSocket route params must be a JSON object");
|
|
135
|
+
let n = {};
|
|
136
|
+
for (let [e, r] of Object.entries(t)) {
|
|
137
|
+
if (typeof r != "string") throw TypeError("WebSocket route params must contain strings");
|
|
138
|
+
n[e] = r;
|
|
139
|
+
}
|
|
140
|
+
return n;
|
|
141
|
+
}
|
|
142
|
+
var g = class {
|
|
158
143
|
wss;
|
|
159
144
|
instances = /* @__PURE__ */ new Map();
|
|
160
145
|
componentRegistry;
|
|
@@ -162,69 +147,111 @@ var p = class {
|
|
|
162
147
|
authenticate;
|
|
163
148
|
defaultUser;
|
|
164
149
|
env;
|
|
150
|
+
maxInstances;
|
|
151
|
+
idleTimeoutMs;
|
|
165
152
|
constructor(e) {
|
|
166
|
-
this.wss = new t({ noServer: !0 }), this.componentRegistry = e.componentRegistry, this.allowedOrigins = e.allowedOrigins, this.authenticate = e.authenticate, this.defaultUser = e.defaultUser ?? { id: "anonymous" }, this.env = e.env, e.server.on("upgrade", (t, n, r) => {
|
|
167
|
-
new
|
|
153
|
+
this.wss = new t({ noServer: !0 }), this.componentRegistry = e.componentRegistry, this.allowedOrigins = e.allowedOrigins, this.authenticate = e.authenticate, this.defaultUser = e.defaultUser ?? { id: "anonymous" }, this.env = e.env, this.maxInstances = e.maxInstances ?? 512, this.idleTimeoutMs = e.idleTimeoutMs ?? 15 * 6e4, e.server.on("upgrade", (t, n, r) => {
|
|
154
|
+
new a(t.url || "", `http://${t.headers.host}`).pathname.startsWith(e.path || "/ws") && this.handleUpgrade(t, n, r);
|
|
168
155
|
});
|
|
169
156
|
}
|
|
170
|
-
handleUpgrade(e, t,
|
|
171
|
-
let o = new
|
|
172
|
-
if (!
|
|
157
|
+
handleUpgrade(e, t, n) {
|
|
158
|
+
let o = new a(e.url || "", `http://${e.headers.host}`).toString(), s = e.headers.origin;
|
|
159
|
+
if (!i(s, o, this.allowedOrigins)) {
|
|
173
160
|
t.destroy();
|
|
174
161
|
return;
|
|
175
162
|
}
|
|
176
|
-
this.wss.handleUpgrade(e, t,
|
|
177
|
-
let
|
|
163
|
+
this.wss.handleUpgrade(e, t, n, async (t) => {
|
|
164
|
+
let n = new a(e.url || "", `http://${e.headers.host}`), i = n.pathname.split("/"), o = i.pop(), s = i.pop(), c = n.searchParams, u = c.get("routePath") || c.get("componentPath") || c.get("componentId");
|
|
178
165
|
if (!o || !u) {
|
|
179
|
-
t.close(1008, "Missing target or
|
|
166
|
+
t.close(1008, "Missing target or routePath");
|
|
180
167
|
return;
|
|
181
168
|
}
|
|
182
|
-
let d
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
169
|
+
let d;
|
|
170
|
+
try {
|
|
171
|
+
d = h(c.get("params"));
|
|
172
|
+
} catch {
|
|
173
|
+
t.close(1008, "Invalid route params");
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
let f = this.componentRegistry.get(u);
|
|
177
|
+
if (!f) {
|
|
178
|
+
t.close(1008, "Component not found");
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
let p;
|
|
182
|
+
try {
|
|
183
|
+
p = this.authenticate ? await this.authenticate(e) : this.defaultUser;
|
|
184
|
+
} catch (e) {
|
|
185
|
+
console.error("[Cossack] Authentication failed, rejecting upgrade:", e), t.close(1008, "Authentication failed");
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
let m = c.get("pathname") || "/", g = {
|
|
189
|
+
get: (e) => e === "user" ? p : void 0,
|
|
190
|
+
req: {
|
|
191
|
+
path: m,
|
|
192
|
+
param: (e) => e ? d[e] : { ...d },
|
|
198
193
|
query: (e) => {
|
|
199
|
-
if (e) return
|
|
194
|
+
if (e) return c.get(e);
|
|
200
195
|
let t = {};
|
|
201
|
-
return
|
|
196
|
+
return c.forEach((e, n) => {
|
|
202
197
|
t[n] = e;
|
|
203
198
|
}), t;
|
|
204
199
|
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
context: a,
|
|
208
|
-
env: this.env,
|
|
209
|
-
page: i,
|
|
210
|
-
providerName: s
|
|
211
|
-
}), d = new c(r), this.instances.set(o, d);
|
|
212
|
-
}
|
|
213
|
-
let f;
|
|
200
|
+
}
|
|
201
|
+
}, _ = Reflect.getMetadata("page:options", f), v;
|
|
214
202
|
try {
|
|
215
|
-
|
|
203
|
+
v = _?.scope ? await _.scope(g) : `user:${p?.id || "anonymous"}`;
|
|
216
204
|
} catch (e) {
|
|
217
|
-
console.error("[Cossack]
|
|
205
|
+
console.error("[Cossack] Failed to resolve Node WebSocket scope:", e), t.close(1008, "Invalid WebSocket scope");
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
if (o !== v) {
|
|
209
|
+
t.close(1008, "Invalid WebSocket scope");
|
|
218
210
|
return;
|
|
219
211
|
}
|
|
220
|
-
|
|
221
|
-
|
|
212
|
+
let y = `${u}:${s ?? "page"}:${o}`;
|
|
213
|
+
this.pruneInstances(!0);
|
|
214
|
+
let b = this.instances.get(y), x = b?.runtime;
|
|
215
|
+
if (!x) {
|
|
216
|
+
if (this.instances.size >= this.maxInstances) {
|
|
217
|
+
t.close(1013, "Runtime instance limit reached");
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
let e = r(f);
|
|
221
|
+
await e.bootstrap({
|
|
222
|
+
context: g,
|
|
223
|
+
user: p,
|
|
224
|
+
env: this.env,
|
|
225
|
+
runtime: {
|
|
226
|
+
platform: "web",
|
|
227
|
+
adapter: "node"
|
|
228
|
+
},
|
|
229
|
+
page: m,
|
|
230
|
+
providerName: s
|
|
231
|
+
}), x = new l(e), b = {
|
|
232
|
+
runtime: x,
|
|
233
|
+
lastActive: Date.now()
|
|
234
|
+
}, this.instances.set(y, b);
|
|
235
|
+
}
|
|
236
|
+
b.lastActive = Date.now(), x.addClient(t, p);
|
|
237
|
+
let S = x.component.getInitialState();
|
|
222
238
|
t.send(JSON.stringify({
|
|
223
239
|
type: "state-update",
|
|
224
|
-
state:
|
|
240
|
+
state: S
|
|
225
241
|
}));
|
|
226
242
|
});
|
|
227
243
|
}
|
|
244
|
+
pruneInstances(e = !1) {
|
|
245
|
+
let t = Date.now();
|
|
246
|
+
for (let [e, n] of this.instances) n.runtime.clientCount === 0 && t - n.lastActive >= this.idleTimeoutMs && this.instances.delete(e);
|
|
247
|
+
let n = Math.max(0, this.maxInstances - +!!e);
|
|
248
|
+
if (this.instances.size <= n) return;
|
|
249
|
+
let r = [...this.instances.entries()].filter(([, e]) => e.runtime.clientCount === 0).sort((e, t) => e[1].lastActive - t[1].lastActive);
|
|
250
|
+
for (let [e] of r) {
|
|
251
|
+
if (this.instances.size <= n) break;
|
|
252
|
+
this.instances.delete(e);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
228
255
|
};
|
|
229
256
|
//#endregion
|
|
230
|
-
export {
|
|
257
|
+
export { g as CossackNodeAdapter, l as NodeWebSocketRuntime, p as createNodeEmailSender, m as nodeRuntimeAdapter, d as serveStatic };
|
package/dist/runtime.d.ts
CHANGED
|
@@ -1,13 +1,6 @@
|
|
|
1
1
|
import { WebSocket } from 'ws';
|
|
2
|
-
import
|
|
3
|
-
export declare class NodeWebSocketRuntime
|
|
4
|
-
private component;
|
|
5
|
-
private clients;
|
|
2
|
+
import { InMemoryWebSocketRuntime, type Cossack } from '@cossackframework/core';
|
|
3
|
+
export declare class NodeWebSocketRuntime extends InMemoryWebSocketRuntime<WebSocket> {
|
|
6
4
|
constructor(component: Cossack);
|
|
7
|
-
addClient(ws: WebSocket, user?:
|
|
8
|
-
onClientMessage(client: unknown, message: string): Promise<void>;
|
|
9
|
-
broadcastState(partialState: Record<string, any>): void;
|
|
10
|
-
broadcastEvent(eventName: string, payload: any[]): void;
|
|
11
|
-
sendClientAction(client: unknown, action: string, payload: any[]): void;
|
|
12
|
-
persistState(): Promise<void>;
|
|
5
|
+
addClient(ws: WebSocket, user?: unknown): void;
|
|
13
6
|
}
|
package/dist/static-serve.d.ts
CHANGED
|
@@ -9,6 +9,8 @@ export interface StaticServeOptions {
|
|
|
9
9
|
prefix?: string;
|
|
10
10
|
/** Whether to try index.html for directory requests (default: true) */
|
|
11
11
|
index?: boolean;
|
|
12
|
+
/** Cache-Control value, or a resolver based on the matched file and URL. */
|
|
13
|
+
cacheControl?: string | ((filePath: string, urlPath: string) => string | undefined);
|
|
12
14
|
}
|
|
13
15
|
/**
|
|
14
16
|
* Create a middleware that serves static files from a directory.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cossackframework/node-adapter",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Node.js runtime adapter for the Cossack Framework",
|
|
6
6
|
"license": "MIT",
|
|
@@ -25,20 +25,20 @@
|
|
|
25
25
|
"access": "public"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"nodemailer": "^
|
|
28
|
+
"nodemailer": "^10.0.6",
|
|
29
29
|
"ws": ">=8.21.0",
|
|
30
|
-
"@cossackframework/core": "0.8.
|
|
30
|
+
"@cossackframework/core": "0.8.3"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@types/nodemailer": "^8.0.1",
|
|
34
34
|
"@types/ws": "^8.18.0",
|
|
35
|
-
"hono": "^4.
|
|
35
|
+
"hono": "^4.13.7",
|
|
36
36
|
"typescript": "^7.0.2",
|
|
37
37
|
"vite": "^8.1.4",
|
|
38
|
-
"vitest": "^4.1.
|
|
38
|
+
"vitest": "^4.1.11"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
|
-
"hono": "^4.
|
|
41
|
+
"hono": "^4.13.7"
|
|
42
42
|
},
|
|
43
43
|
"scripts": {
|
|
44
44
|
"build": "vite build && tsc -p tsconfig.declarations.json",
|