@cossackframework/node-adapter 0.5.2 → 0.7.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 +70 -1
- package/dist/email.d.ts +69 -0
- package/dist/index.d.ts +44 -1
- package/dist/index.js +122 -65
- package/dist/{src/runtime.d.ts → runtime.d.ts} +1 -2
- package/dist/{src/static-serve.d.ts → static-serve.d.ts} +1 -2
- package/package.json +13 -9
- package/dist/src/index.d.ts +0 -16
package/README.md
CHANGED
|
@@ -1,6 +1,75 @@
|
|
|
1
1
|
# Cossack Framework Node Adapter
|
|
2
2
|
|
|
3
|
-
This package provides a Node.js adapter for the Cossack Framework, allowing you to run your Cossack applications in a Node.js environment. It includes utilities for handling HTTP requests and responses,
|
|
3
|
+
This package provides a Node.js adapter for the Cossack Framework, allowing you to run your Cossack applications in a Node.js environment. It includes utilities for handling HTTP requests and responses, WebSocket upgrades, static file serving, and runtime polyfills that keep application code identical across Cloudflare Workers and Node.js.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
Refer to our [Installation Guide](https://cossack.dev/docs/installation) for detailed instructions on how to set up the Cossack Framework and its packages.
|
|
7
|
+
|
|
8
|
+
## Exports
|
|
9
|
+
|
|
10
|
+
- `CossackNodeAdapter` — handles WebSocket upgrades and component bootstrapping on a Node `http.Server`.
|
|
11
|
+
- `serveStatic` — Node `fs`-based Hono middleware for serving static assets.
|
|
12
|
+
- `NodeWebSocketRuntime` — the WebSocket runtime used by the adapter.
|
|
13
|
+
- `createNodeEmailSender` — Node.js polyfill for Cloudflare's `send_email` binding (`env.EMAIL`).
|
|
14
|
+
|
|
15
|
+
## Unified `env.EMAIL` (Cloudflare email binding polyfill)
|
|
16
|
+
|
|
17
|
+
Cloudflare Workers expose `env.EMAIL.send({ to, from, subject, html, text })` when you declare a `send_email` binding in `wrangler.jsonc`. To keep the same call site working on Node.js, this package ships a nodemailer-backed sender with the identical shape.
|
|
18
|
+
|
|
19
|
+
### Cloudflare (`wrangler.jsonc`)
|
|
20
|
+
```jsonc
|
|
21
|
+
{
|
|
22
|
+
"send_email": [{ "name": "EMAIL" }]
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
Then `await env.EMAIL.send({ to, from, subject, html, text })` works directly.
|
|
26
|
+
|
|
27
|
+
### Node.js
|
|
28
|
+
Build the sender from SMTP env vars and inject it into `env`, then pass `env` both to `app.fetch` (HTTP transport) and to `CossackNodeAdapter` (WebSocket transport):
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { serve } from '@hono/node-server';
|
|
32
|
+
import { createApp } from '@cossackframework/framework/router';
|
|
33
|
+
import { CossackNodeAdapter, createNodeEmailSender } from '@cossackframework/node-adapter';
|
|
34
|
+
import { App } from './App';
|
|
35
|
+
import { template } from './root';
|
|
36
|
+
|
|
37
|
+
const app = createApp({ AppComponent: App, htmlTemplate: template });
|
|
38
|
+
|
|
39
|
+
const env = {
|
|
40
|
+
EMAIL: createNodeEmailSender({
|
|
41
|
+
host: process.env.SMTP_HOST!,
|
|
42
|
+
port: Number(process.env.SMTP_PORT ?? 587),
|
|
43
|
+
secure: process.env.SMTP_SECURE === 'true',
|
|
44
|
+
auth: { user: process.env.SMTP_USER!, pass: process.env.SMTP_PASS! },
|
|
45
|
+
from: process.env.MAIL_FROM ?? 'no-reply@example.com',
|
|
46
|
+
}),
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// HTTP: pass env to app.fetch so `c.env` (and `this.env`) is populated.
|
|
50
|
+
const server = serve(
|
|
51
|
+
{ fetch: (req) => app.fetch(req, env), port: Number(process.env.PORT) || 3000 },
|
|
52
|
+
(info) => console.log(`Listening on http://localhost:${info.port}`),
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
// WebSocket: pass the same env so `@Server` methods over WS see `this.env.EMAIL`.
|
|
56
|
+
new CossackNodeAdapter({
|
|
57
|
+
server,
|
|
58
|
+
componentRegistry,
|
|
59
|
+
env,
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Environment variables
|
|
64
|
+
Configure via `.dev.vars` (Cloudflare dev) or your shell/`.env` (Node):
|
|
65
|
+
|
|
66
|
+
| Variable | Description | Default |
|
|
67
|
+
| -------------- | -------------------------------------------- | ------------------ |
|
|
68
|
+
| `SMTP_HOST` | SMTP server host (e.g. `smtp.gmail.com`) | — |
|
|
69
|
+
| `SMTP_PORT` | SMTP server port | `587` |
|
|
70
|
+
| `SMTP_SECURE` | Use TLS (`"true"` for port 465) | `port === 465` |
|
|
71
|
+
| `SMTP_USER` | SMTP username | — |
|
|
72
|
+
| `SMTP_PASS` | SMTP password | — |
|
|
73
|
+
| `MAIL_FROM` | Default `from` address when omitted | `no-reply@example.com` |
|
|
74
|
+
|
|
75
|
+
Now `await this.env.EMAIL.send(...)` works identically in `@Server` methods on both Cloudflare Workers and Node.js.
|
package/dist/email.d.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
export interface EmailAddress {
|
|
2
|
+
name?: string;
|
|
3
|
+
address: string;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Message shape — compatible with Cloudflare's `EmailMessageBuilder` so the
|
|
7
|
+
* same call site works on both runtimes.
|
|
8
|
+
*/
|
|
9
|
+
export interface EmailMessageInput {
|
|
10
|
+
to: string | EmailAddress | (string | EmailAddress)[];
|
|
11
|
+
from?: string | EmailAddress;
|
|
12
|
+
subject: string;
|
|
13
|
+
html?: string;
|
|
14
|
+
text?: string;
|
|
15
|
+
cc?: string | EmailAddress | (string | EmailAddress)[];
|
|
16
|
+
bcc?: string | EmailAddress | (string | EmailAddress)[];
|
|
17
|
+
replyTo?: string | EmailAddress;
|
|
18
|
+
}
|
|
19
|
+
export interface EmailSendResult {
|
|
20
|
+
messageId: string;
|
|
21
|
+
}
|
|
22
|
+
export interface NodeEmailOptions {
|
|
23
|
+
/** SMTP host, e.g. "smtp.gmail.com". Read from `SMTP_HOST` in your app. */
|
|
24
|
+
host: string;
|
|
25
|
+
/** SMTP port. Read from `SMTP_PORT` (defaults to 587). */
|
|
26
|
+
port: number;
|
|
27
|
+
/** Use TLS (port 465). Defaults to `port === 465`. */
|
|
28
|
+
secure?: boolean;
|
|
29
|
+
auth: {
|
|
30
|
+
user: string;
|
|
31
|
+
pass: string;
|
|
32
|
+
};
|
|
33
|
+
/** Default `from` address when the message omits one. Read from `MAIL_FROM`. */
|
|
34
|
+
from?: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Result type returned by {@link createNodeEmailSender}. Structurally
|
|
38
|
+
* compatible with Cloudflare's `SendEmail` binding (`env.EMAIL`).
|
|
39
|
+
*/
|
|
40
|
+
export interface NodeEmailSender {
|
|
41
|
+
send(message: EmailMessageInput): Promise<EmailSendResult>;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Build a Node.js email sender backed by nodemailer SMTP. The returned object
|
|
45
|
+
* matches the shape of Cloudflare's `env.EMAIL` binding, so the same
|
|
46
|
+
* `await env.EMAIL.send({ to, from, subject, html, text })` call works on both
|
|
47
|
+
* runtimes.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* import { createNodeEmailSender } from '@cossackframework/node-adapter';
|
|
52
|
+
*
|
|
53
|
+
* const email = createNodeEmailSender({
|
|
54
|
+
* host: process.env.SMTP_HOST!,
|
|
55
|
+
* port: Number(process.env.SMTP_PORT ?? 587),
|
|
56
|
+
* secure: process.env.SMTP_SECURE === 'true',
|
|
57
|
+
* auth: { user: process.env.SMTP_USER!, pass: process.env.SMTP_PASS! },
|
|
58
|
+
* from: process.env.MAIL_FROM ?? 'no-reply@example.com',
|
|
59
|
+
* });
|
|
60
|
+
*
|
|
61
|
+
* // Identical call on Cloudflare (real binding) and Node (this polyfill):
|
|
62
|
+
* await env.EMAIL.send({ to, from, subject, html, text });
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* Configure via environment variables (set them in `.dev.vars` for Cloudflare
|
|
66
|
+
* dev, or your shell/`.env` for Node):
|
|
67
|
+
* SMTP_HOST, SMTP_PORT, SMTP_SECURE, SMTP_USER, SMTP_PASS, MAIL_FROM
|
|
68
|
+
*/
|
|
69
|
+
export declare function createNodeEmailSender(options: NodeEmailOptions): NodeEmailSender;
|
package/dist/index.d.ts
CHANGED
|
@@ -1 +1,44 @@
|
|
|
1
|
-
|
|
1
|
+
import type { IncomingMessage, Server } from 'http';
|
|
2
|
+
import type { Cossack } from '@cossackframework/core';
|
|
3
|
+
export * from './runtime';
|
|
4
|
+
export { serveStatic, type StaticServeOptions } from './static-serve';
|
|
5
|
+
export { createNodeEmailSender, type NodeEmailOptions, type NodeEmailSender, type EmailMessageInput, type EmailSendResult, type EmailAddress, } from './email';
|
|
6
|
+
export interface CossackNodeAdapterOptions {
|
|
7
|
+
server: Server;
|
|
8
|
+
componentRegistry: Map<string, new () => Cossack>;
|
|
9
|
+
path?: string;
|
|
10
|
+
/**
|
|
11
|
+
* Allowed Origin values for WebSocket upgrades. Defaults to same-origin
|
|
12
|
+
* (the request's own origin). Missing Origin headers are rejected, which
|
|
13
|
+
* blocks cross-site WebSocket hijacking.
|
|
14
|
+
*/
|
|
15
|
+
allowedOrigins?: string[];
|
|
16
|
+
/**
|
|
17
|
+
* Authenticate the WebSocket upgrade, returning the user object the
|
|
18
|
+
* component should run as (e.g. parsed from a cookie/JWT on `request`).
|
|
19
|
+
* Falls back to `defaultUser` then to `{ id: 'anonymous' }`. Provide this
|
|
20
|
+
* for any app with per-user state or authorization — otherwise every
|
|
21
|
+
* connection shares one anonymous identity.
|
|
22
|
+
*/
|
|
23
|
+
authenticate?: (request: IncomingMessage) => Promise<unknown> | unknown;
|
|
24
|
+
/** User used when no `authenticate` hook is provided. Defaults to `{ id: 'anonymous' }`. */
|
|
25
|
+
defaultUser?: unknown;
|
|
26
|
+
/**
|
|
27
|
+
* Runtime bindings to expose to components as `this.env` (mirrors
|
|
28
|
+
* Cloudflare's `env` argument). Use it to supply polyfills such as
|
|
29
|
+
* `{ EMAIL: createNodeEmailSender({...}) }` so the same
|
|
30
|
+
* `this.env.EMAIL.send(...)` call works on both runtimes.
|
|
31
|
+
*/
|
|
32
|
+
env?: Record<string, unknown>;
|
|
33
|
+
}
|
|
34
|
+
export declare class CossackNodeAdapter {
|
|
35
|
+
private wss;
|
|
36
|
+
private instances;
|
|
37
|
+
private componentRegistry;
|
|
38
|
+
private allowedOrigins?;
|
|
39
|
+
private authenticate?;
|
|
40
|
+
private defaultUser;
|
|
41
|
+
private env?;
|
|
42
|
+
constructor(options: CossackNodeAdapterOptions);
|
|
43
|
+
private handleUpgrade;
|
|
44
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -1,16 +1,11 @@
|
|
|
1
1
|
import { WebSocket as e, WebSocketServer as t } from "ws";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
return e;
|
|
10
|
-
}, d = (e, t, n) => (n = e == null ? {} : r(s(e)), u(t || !e || !e.__esModule ? i(n, "default", {
|
|
11
|
-
value: e,
|
|
12
|
-
enumerable: !0
|
|
13
|
-
}) : n, e)), f = class {
|
|
2
|
+
import { createInstance as n, isOriginAllowed as r } from "@cossackframework/core";
|
|
3
|
+
import { URL as i } from "url";
|
|
4
|
+
import * as a from "fs";
|
|
5
|
+
import * as o from "path";
|
|
6
|
+
import s from "nodemailer";
|
|
7
|
+
//#region src/runtime.ts
|
|
8
|
+
var c = class {
|
|
14
9
|
component;
|
|
15
10
|
clients = /* @__PURE__ */ new Set();
|
|
16
11
|
constructor(e) {
|
|
@@ -20,15 +15,22 @@ var r = Object.create, i = Object.defineProperty, a = Object.getOwnPropertyDescr
|
|
|
20
15
|
this.clients.add(e), e.user = t, e.on("close", () => {
|
|
21
16
|
this.clients.delete(e);
|
|
22
17
|
}), e.on("message", (t) => {
|
|
23
|
-
this.onClientMessage(e, t.toString())
|
|
18
|
+
this.onClientMessage(e, t.toString()).catch((e) => {
|
|
19
|
+
console.error("[Cossack] Error handling Node WebSocket message:", e);
|
|
20
|
+
});
|
|
24
21
|
});
|
|
25
22
|
}
|
|
26
23
|
async onClientMessage(e, t) {
|
|
27
|
-
let n = e, r
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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;
|
|
31
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);
|
|
32
34
|
}
|
|
33
35
|
broadcastState(t) {
|
|
34
36
|
let n = JSON.stringify({
|
|
@@ -54,43 +56,54 @@ var r = Object.create, i = Object.defineProperty, a = Object.getOwnPropertyDescr
|
|
|
54
56
|
i.readyState === e.OPEN && i.send(a);
|
|
55
57
|
}
|
|
56
58
|
async persistState() {}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
function
|
|
59
|
+
};
|
|
60
|
+
//#endregion
|
|
61
|
+
//#region src/static-serve.ts
|
|
62
|
+
function l(e) {
|
|
61
63
|
let t = e, n = t.indexOf("?");
|
|
62
64
|
n !== -1 && (t = t.substring(0, n));
|
|
63
65
|
let r = t.indexOf("#");
|
|
64
66
|
return r !== -1 && (t = t.substring(0, r)), t.startsWith("/") || (t = "/" + t), t !== "/" && t.endsWith("/") && (t = t.slice(0, -1)), t;
|
|
65
67
|
}
|
|
66
|
-
function
|
|
67
|
-
let { root: t, prefix: n = "/", index: r = !0 } = e
|
|
68
|
+
function u(e) {
|
|
69
|
+
let { root: t, prefix: n = "/", index: r = !0 } = e, i = o.resolve(t), s = i + o.sep, c = (e) => {
|
|
70
|
+
let t = o.resolve(e);
|
|
71
|
+
return t === i || t.startsWith(s);
|
|
72
|
+
};
|
|
68
73
|
return async (e, i) => {
|
|
69
74
|
if (e.req.method !== "GET" && e.req.method !== "HEAD") return i();
|
|
70
|
-
let
|
|
71
|
-
if (!
|
|
72
|
-
let
|
|
73
|
-
|
|
74
|
-
let
|
|
75
|
-
if (
|
|
76
|
-
else if (
|
|
77
|
-
let e =
|
|
78
|
-
|
|
75
|
+
let s = l(e.req.path);
|
|
76
|
+
if (!s.startsWith(n)) return i();
|
|
77
|
+
let u = s.substring(n.length);
|
|
78
|
+
u === "" && (u = "/");
|
|
79
|
+
let f;
|
|
80
|
+
if (u === "/") f = o.join(t, "index.html");
|
|
81
|
+
else if (f = o.join(t, u), r && !a.existsSync(f)) {
|
|
82
|
+
let e = o.join(f, "index.html");
|
|
83
|
+
a.existsSync(e) && (f = e);
|
|
79
84
|
}
|
|
85
|
+
if (!c(f)) return i();
|
|
80
86
|
try {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
87
|
+
let e = await a.promises.stat(f);
|
|
88
|
+
if (e.isDirectory()) if (r) {
|
|
89
|
+
let t = o.join(f, "index.html");
|
|
90
|
+
try {
|
|
91
|
+
await a.promises.access(t), f = t, e = await a.promises.stat(f);
|
|
92
|
+
} catch {
|
|
93
|
+
return i();
|
|
94
|
+
}
|
|
85
95
|
} else return i();
|
|
86
|
-
let t =
|
|
87
|
-
return
|
|
96
|
+
let t = await a.promises.readFile(f), n = d(f);
|
|
97
|
+
return new Response(new Uint8Array(t), {
|
|
98
|
+
status: 200,
|
|
99
|
+
headers: { "Content-Type": n }
|
|
100
|
+
});
|
|
88
101
|
} catch {
|
|
89
102
|
return i();
|
|
90
103
|
}
|
|
91
104
|
};
|
|
92
105
|
}
|
|
93
|
-
function
|
|
106
|
+
function d(e) {
|
|
94
107
|
return {
|
|
95
108
|
".html": "text/html",
|
|
96
109
|
".css": "text/css",
|
|
@@ -110,64 +123,108 @@ function g(e) {
|
|
|
110
123
|
".eot": "application/vnd.ms-fontobject",
|
|
111
124
|
".pdf": "application/pdf",
|
|
112
125
|
".zip": "application/zip"
|
|
113
|
-
}[
|
|
126
|
+
}[o.extname(e).toLowerCase()] || "application/octet-stream";
|
|
127
|
+
}
|
|
128
|
+
//#endregion
|
|
129
|
+
//#region src/email.ts
|
|
130
|
+
function f(e) {
|
|
131
|
+
let t = s.createTransport({
|
|
132
|
+
host: e.host,
|
|
133
|
+
port: e.port,
|
|
134
|
+
secure: e.secure ?? e.port === 465,
|
|
135
|
+
auth: {
|
|
136
|
+
user: e.auth.user,
|
|
137
|
+
pass: e.auth.pass
|
|
138
|
+
}
|
|
139
|
+
}), n = e.from;
|
|
140
|
+
return { async send(e) {
|
|
141
|
+
let r = e.from ?? n;
|
|
142
|
+
if (!r) throw Error("Missing \"from\" email address. Provide message.from or configure options.from.");
|
|
143
|
+
return { messageId: (await t.sendMail({
|
|
144
|
+
from: r,
|
|
145
|
+
to: e.to,
|
|
146
|
+
subject: e.subject,
|
|
147
|
+
html: e.html,
|
|
148
|
+
text: e.text,
|
|
149
|
+
cc: e.cc,
|
|
150
|
+
bcc: e.bcc,
|
|
151
|
+
replyTo: e.replyTo
|
|
152
|
+
})).messageId };
|
|
153
|
+
} };
|
|
114
154
|
}
|
|
115
155
|
//#endregion
|
|
116
156
|
//#region src/index.ts
|
|
117
|
-
var
|
|
157
|
+
var p = class {
|
|
118
158
|
wss;
|
|
119
159
|
instances = /* @__PURE__ */ new Map();
|
|
120
160
|
componentRegistry;
|
|
161
|
+
allowedOrigins;
|
|
162
|
+
authenticate;
|
|
163
|
+
defaultUser;
|
|
164
|
+
env;
|
|
121
165
|
constructor(e) {
|
|
122
|
-
this.wss = new t({ noServer: !0 }), this.componentRegistry = e.componentRegistry, e.server.on("upgrade", (t,
|
|
123
|
-
new
|
|
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 i(t.url || "", `http://${t.headers.host}`).pathname.startsWith(e.path || "/ws") && this.handleUpgrade(t, n, r);
|
|
124
168
|
});
|
|
125
169
|
}
|
|
126
|
-
handleUpgrade(e, t,
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
170
|
+
handleUpgrade(e, t, a) {
|
|
171
|
+
let o = new i(e.url || "", `http://${e.headers.host}`).toString(), s = e.headers.origin;
|
|
172
|
+
if (!r(s, o, this.allowedOrigins)) {
|
|
173
|
+
t.destroy();
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
this.wss.handleUpgrade(e, t, a, async (t) => {
|
|
177
|
+
let r = new i(e.url || "", `http://${e.headers.host}`), a = r.pathname.split("/"), o = a.pop(), s = a.pop(), l = r.searchParams, u = l.get("componentId");
|
|
178
|
+
if (!o || !u) {
|
|
130
179
|
t.close(1008, "Missing target or componentId");
|
|
131
180
|
return;
|
|
132
181
|
}
|
|
133
|
-
let
|
|
134
|
-
if (!
|
|
135
|
-
let e = this.componentRegistry.get(
|
|
182
|
+
let d = this.instances.get(o);
|
|
183
|
+
if (!d) {
|
|
184
|
+
let e = this.componentRegistry.get(u);
|
|
136
185
|
if (!e) {
|
|
137
186
|
t.close(1008, "Component not found");
|
|
138
187
|
return;
|
|
139
188
|
}
|
|
140
|
-
let
|
|
141
|
-
path:
|
|
189
|
+
let r = n(e), i = l.get("pathname") || "/", a = { req: {
|
|
190
|
+
path: i,
|
|
142
191
|
param: (e) => {
|
|
143
|
-
if (e) return
|
|
192
|
+
if (e) return l.get(e);
|
|
144
193
|
let t = {};
|
|
145
|
-
return
|
|
194
|
+
return l.forEach((e, n) => {
|
|
146
195
|
t[n] = e;
|
|
147
196
|
}), t;
|
|
148
197
|
},
|
|
149
198
|
query: (e) => {
|
|
150
|
-
if (e) return
|
|
199
|
+
if (e) return l.get(e);
|
|
151
200
|
let t = {};
|
|
152
|
-
return
|
|
201
|
+
return l.forEach((e, n) => {
|
|
153
202
|
t[n] = e;
|
|
154
203
|
}), t;
|
|
155
204
|
}
|
|
156
205
|
} };
|
|
157
|
-
await
|
|
158
|
-
context:
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
206
|
+
await r.bootstrap({
|
|
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;
|
|
214
|
+
try {
|
|
215
|
+
f = this.authenticate ? await this.authenticate(e) : this.defaultUser;
|
|
216
|
+
} catch (e) {
|
|
217
|
+
console.error("[Cossack] Authentication failed, rejecting upgrade:", e), t.close(1008, "Authentication failed");
|
|
218
|
+
return;
|
|
162
219
|
}
|
|
163
|
-
|
|
164
|
-
let
|
|
220
|
+
d.addClient(t, f);
|
|
221
|
+
let p = d.component.getInitialState();
|
|
165
222
|
t.send(JSON.stringify({
|
|
166
223
|
type: "state-update",
|
|
167
|
-
state:
|
|
224
|
+
state: p
|
|
168
225
|
}));
|
|
169
226
|
});
|
|
170
227
|
}
|
|
171
228
|
};
|
|
172
229
|
//#endregion
|
|
173
|
-
export {
|
|
230
|
+
export { p as CossackNodeAdapter, c as NodeWebSocketRuntime, f as createNodeEmailSender, u as serveStatic };
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { WebSocket } from 'ws';
|
|
2
|
-
import { Cossack, CossackServerRuntime } from '@cossackframework/core';
|
|
3
|
-
|
|
2
|
+
import type { Cossack, CossackServerRuntime } from '@cossackframework/core';
|
|
4
3
|
export declare class NodeWebSocketRuntime implements CossackServerRuntime {
|
|
5
4
|
private component;
|
|
6
5
|
private clients;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cossackframework/node-adapter",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Node.js runtime adapter for the Cossack Framework",
|
|
6
6
|
"license": "MIT",
|
|
@@ -9,13 +9,15 @@
|
|
|
9
9
|
"url": "https://github.com/cossackframework/cossack",
|
|
10
10
|
"directory": "packages/node-adapter"
|
|
11
11
|
},
|
|
12
|
+
"main": "./dist/index.js",
|
|
13
|
+
"module": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
12
15
|
"exports": {
|
|
13
16
|
".": {
|
|
14
17
|
"import": "./dist/index.js",
|
|
15
18
|
"types": "./dist/index.d.ts"
|
|
16
19
|
}
|
|
17
20
|
},
|
|
18
|
-
"types": "./dist/index.d.ts",
|
|
19
21
|
"files": [
|
|
20
22
|
"dist"
|
|
21
23
|
],
|
|
@@ -23,18 +25,20 @@
|
|
|
23
25
|
"access": "public"
|
|
24
26
|
},
|
|
25
27
|
"dependencies": {
|
|
26
|
-
"hono": "^4.12.
|
|
28
|
+
"hono": "^4.12.31",
|
|
29
|
+
"nodemailer": "^6.9.16",
|
|
27
30
|
"ws": ">=8.21.0",
|
|
28
|
-
"@cossackframework/core": "0.
|
|
31
|
+
"@cossackframework/core": "0.7.0"
|
|
29
32
|
},
|
|
30
33
|
"devDependencies": {
|
|
34
|
+
"@types/nodemailer": "^6.4.17",
|
|
31
35
|
"@types/ws": "^8.18.0",
|
|
32
|
-
"typescript": "^
|
|
33
|
-
"vite": "^8.1.
|
|
34
|
-
"
|
|
35
|
-
"vite-tsconfig-paths": "^5.1.4"
|
|
36
|
+
"typescript": "^7.0.2",
|
|
37
|
+
"vite": "^8.1.4",
|
|
38
|
+
"vitest": "^4.1.10"
|
|
36
39
|
},
|
|
37
40
|
"scripts": {
|
|
38
|
-
"build": "vite build"
|
|
41
|
+
"build": "vite build && tsc -p tsconfig.declarations.json",
|
|
42
|
+
"test": "vitest --run"
|
|
39
43
|
}
|
|
40
44
|
}
|
package/dist/src/index.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { Server } from 'http';
|
|
2
|
-
import { Cossack } from '@cossackframework/core';
|
|
3
|
-
|
|
4
|
-
export * from './runtime';
|
|
5
|
-
export { serveStatic, type StaticServeOptions } from './static-serve';
|
|
6
|
-
export declare class CossackNodeAdapter {
|
|
7
|
-
private wss;
|
|
8
|
-
private instances;
|
|
9
|
-
private componentRegistry;
|
|
10
|
-
constructor(options: {
|
|
11
|
-
server: Server;
|
|
12
|
-
componentRegistry: Map<string, new () => Cossack>;
|
|
13
|
-
path?: string;
|
|
14
|
-
});
|
|
15
|
-
private handleUpgrade;
|
|
16
|
-
}
|