@cossackframework/node-adapter 0.0.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.
@@ -0,0 +1 @@
1
+ export * from './src/index'
package/dist/index.js ADDED
@@ -0,0 +1,158 @@
1
+ var b = Object.defineProperty;
2
+ var O = (i, t, e) => t in i ? b(i, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : i[t] = e;
3
+ var d = (i, t, e) => O(i, typeof t != "symbol" ? t + "" : t, e);
4
+ import { WebSocket as y, WebSocketServer as j } from "ws";
5
+ import { URL as w } from "url";
6
+ class E {
7
+ constructor(t) {
8
+ d(this, "component");
9
+ d(this, "clients", /* @__PURE__ */ new Set());
10
+ this.component = t, this.component._runtime = this;
11
+ }
12
+ addClient(t, e) {
13
+ this.clients.add(t), t.user = e, t.on("close", () => {
14
+ this.clients.delete(t);
15
+ }), t.on("message", (s) => {
16
+ this.onClientMessage(t, s.toString());
17
+ });
18
+ }
19
+ async onClientMessage(t, e) {
20
+ const s = t, n = JSON.parse(e);
21
+ if (n.type === "action") {
22
+ const o = s.user;
23
+ await this.component.executeAction(n.action, n.payload, o, t);
24
+ }
25
+ }
26
+ broadcastState(t) {
27
+ const e = JSON.stringify({ type: "state-update", state: t });
28
+ for (const s of this.clients)
29
+ s.readyState === y.OPEN && s.send(e);
30
+ }
31
+ broadcastEvent(t, e) {
32
+ const s = JSON.stringify({ type: "event", eventName: t, payload: e });
33
+ for (const n of this.clients)
34
+ n.readyState === y.OPEN && n.send(s);
35
+ }
36
+ sendClientAction(t, e, s) {
37
+ const n = t, o = JSON.stringify({ type: "client-action", action: e, payload: s });
38
+ n.readyState === y.OPEN && n.send(o);
39
+ }
40
+ async persistState() {
41
+ }
42
+ }
43
+ function I(i) {
44
+ let t = i;
45
+ const e = t.indexOf("?");
46
+ e !== -1 && (t = t.substring(0, e));
47
+ const s = t.indexOf("#");
48
+ return s !== -1 && (t = t.substring(0, s)), t.startsWith("/") || (t = "/" + t), t !== "/" && t.endsWith("/") && (t = t.slice(0, -1)), t;
49
+ }
50
+ function U(i) {
51
+ const { root: t, prefix: e = "/", index: s = !0 } = i;
52
+ return async (n, o) => {
53
+ if (n.req.method !== "GET" && n.req.method !== "HEAD")
54
+ return o();
55
+ const f = I(n.req.path);
56
+ if (!f.startsWith(e))
57
+ return o();
58
+ let c = f.substring(e.length);
59
+ c === "" && (c = "/");
60
+ let a;
61
+ c === "/" ? a = (void 0)(t, "index.html") : (a = (void 0)(t, c), s && !(void 0)(a) && (void 0)((void 0)(a, "index.html")));
62
+ try {
63
+ if ((void 0)(a).isDirectory())
64
+ if (s) {
65
+ if (!(void 0)((void 0)(a, "index.html"))) return o();
66
+ } else
67
+ return o();
68
+ const m = (void 0)(a, "utf-8"), p = W(a);
69
+ return n.html(m, 200);
70
+ } catch {
71
+ return o();
72
+ }
73
+ };
74
+ }
75
+ function W(i) {
76
+ const t = (void 0)(i).toLowerCase();
77
+ return {
78
+ ".html": "text/html",
79
+ ".css": "text/css",
80
+ ".js": "application/javascript",
81
+ ".json": "application/json",
82
+ ".xml": "application/xml",
83
+ ".txt": "text/plain",
84
+ ".png": "image/png",
85
+ ".jpg": "image/jpeg",
86
+ ".jpeg": "image/jpeg",
87
+ ".gif": "image/gif",
88
+ ".svg": "image/svg+xml",
89
+ ".ico": "image/x-icon",
90
+ ".woff": "font/woff",
91
+ ".woff2": "font/woff2",
92
+ ".ttf": "font/ttf",
93
+ ".eot": "application/vnd.ms-fontobject",
94
+ ".pdf": "application/pdf",
95
+ ".zip": "application/zip"
96
+ }[t] || "application/octet-stream";
97
+ }
98
+ class z {
99
+ constructor(t) {
100
+ d(this, "wss");
101
+ // Map of target ID -> Runtime instance
102
+ d(this, "instances", /* @__PURE__ */ new Map());
103
+ d(this, "componentRegistry");
104
+ this.wss = new j({ noServer: !0 }), this.componentRegistry = t.componentRegistry, t.server.on("upgrade", (e, s, n) => {
105
+ new w(e.url || "", `http://${e.headers.host}`).pathname.startsWith(t.path || "/ws") && this.handleUpgrade(e, s, n);
106
+ });
107
+ }
108
+ handleUpgrade(t, e, s) {
109
+ this.wss.handleUpgrade(t, e, s, async (n) => {
110
+ const o = new w(t.url || "", `http://${t.headers.host}`), f = o.pathname.split("/"), c = f.pop(), a = f.pop(), r = o.searchParams, m = r.get("componentId");
111
+ if (!c || !m) {
112
+ n.close(1008, "Missing target or componentId");
113
+ return;
114
+ }
115
+ let p = this.instances.get(c);
116
+ if (!p) {
117
+ const S = this.componentRegistry.get(m);
118
+ if (!S) {
119
+ n.close(1008, "Component not found");
120
+ return;
121
+ }
122
+ const P = new S(), v = r.get("pathname") || "/", N = {
123
+ req: {
124
+ path: v,
125
+ param: (h) => {
126
+ if (h) return r.get(h);
127
+ const l = {};
128
+ return r.forEach((g, u) => {
129
+ l[u] = g;
130
+ }), l;
131
+ },
132
+ query: (h) => {
133
+ if (h) return r.get(h);
134
+ const l = {};
135
+ return r.forEach((g, u) => {
136
+ l[u] = g;
137
+ }), l;
138
+ }
139
+ }
140
+ };
141
+ await P.bootstrap({
142
+ context: N,
143
+ page: v,
144
+ providerName: a
145
+ }), p = new E(P), this.instances.set(c, p);
146
+ }
147
+ const x = { id: "guest" };
148
+ p.addClient(n, x);
149
+ const C = p.component.getInitialState();
150
+ n.send(JSON.stringify({ type: "state-update", state: C }));
151
+ });
152
+ }
153
+ }
154
+ export {
155
+ z as CossackNodeAdapter,
156
+ E as NodeWebSocketRuntime,
157
+ U as serveStatic
158
+ };
@@ -0,0 +1,16 @@
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
+ }
@@ -0,0 +1,14 @@
1
+ import { WebSocket } from 'ws';
2
+ import { Cossack, CossackServerRuntime } from '@cossackframework/core';
3
+
4
+ export declare class NodeWebSocketRuntime implements CossackServerRuntime {
5
+ private component;
6
+ private clients;
7
+ constructor(component: Cossack);
8
+ addClient(ws: WebSocket, user?: any): void;
9
+ onClientMessage(client: unknown, message: string): Promise<void>;
10
+ broadcastState(partialState: Record<string, any>): void;
11
+ broadcastEvent(eventName: string, payload: any[]): void;
12
+ sendClientAction(client: unknown, action: string, payload: any[]): void;
13
+ persistState(): Promise<void>;
14
+ }
@@ -0,0 +1,21 @@
1
+ import { Context, Next } from 'hono';
2
+
3
+ /**
4
+ * Options for static file serving middleware
5
+ */
6
+ export interface StaticServeOptions {
7
+ /** Root directory for static files */
8
+ root: string;
9
+ /** URL path prefix to serve files from (default: '/') */
10
+ prefix?: string;
11
+ /** Whether to try index.html for directory requests (default: true) */
12
+ index?: boolean;
13
+ }
14
+ /**
15
+ * Create a middleware that serves static files from a directory.
16
+ * This is for Node.js runtime only - uses fs to read files.
17
+ *
18
+ * @param options - Static serving options
19
+ * @returns Hono middleware
20
+ */
21
+ export declare function serveStatic(options: StaticServeOptions): (c: Context, next: Next) => Promise<any>;
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@cossackframework/node-adapter",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "description": "Node.js runtime adapter for the Cossack Framework",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/cossackframework/cossack",
10
+ "directory": "packages/node-adapter"
11
+ },
12
+ "exports": {
13
+ ".": {
14
+ "import": "./dist/index.js",
15
+ "types": "./dist/index.d.ts"
16
+ }
17
+ },
18
+ "types": "./dist/index.d.ts",
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "publishConfig": {
23
+ "access": "public"
24
+ },
25
+ "scripts": {
26
+ "build": "vite build"
27
+ },
28
+ "dependencies": {
29
+ "@cossackframework/core": "workspace:*",
30
+ "ws": "^8.16.0"
31
+ },
32
+ "devDependencies": {
33
+ "@types/ws": "^8.5.10",
34
+ "vite": "^5.0.0",
35
+ "vite-plugin-dts": "^3.9.1",
36
+ "vite-tsconfig-paths": "^5.1.4",
37
+ "typescript": "^5.0.0"
38
+ }
39
+ }