@cossackframework/node-adapter 0.1.5 → 0.4.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/LICENSE +21 -0
- package/README.md +6 -0
- package/dist/index.js +169 -154
- package/package.json +3 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tan Nguyen
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
# Cossack Framework Node Adapter
|
|
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, as well as a development server for local testing.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
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.
|
package/dist/index.js
CHANGED
|
@@ -1,158 +1,173 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
1
|
+
import { WebSocket as e, WebSocketServer as t } from "ws";
|
|
2
|
+
import { URL as n } from "url";
|
|
3
|
+
//#region \0rolldown/runtime.js
|
|
4
|
+
var r = Object.create, i = Object.defineProperty, a = Object.getOwnPropertyDescriptor, o = Object.getOwnPropertyNames, s = Object.getPrototypeOf, c = Object.prototype.hasOwnProperty, l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t.exports), u = (e, t, n, r) => {
|
|
5
|
+
if (t && typeof t == "object" || typeof t == "function") for (var s = o(t), l = 0, u = s.length, d; l < u; l++) d = s[l], !c.call(e, d) && d !== n && i(e, d, {
|
|
6
|
+
get: ((e) => t[e]).bind(null, d),
|
|
7
|
+
enumerable: !(r = a(t, d)) || r.enumerable
|
|
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 {
|
|
14
|
+
component;
|
|
15
|
+
clients = /* @__PURE__ */ new Set();
|
|
16
|
+
constructor(e) {
|
|
17
|
+
this.component = e, this.component._runtime = this;
|
|
18
|
+
}
|
|
19
|
+
addClient(e, t) {
|
|
20
|
+
this.clients.add(e), e.user = t, e.on("close", () => {
|
|
21
|
+
this.clients.delete(e);
|
|
22
|
+
}), e.on("message", (t) => {
|
|
23
|
+
this.onClientMessage(e, t.toString());
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
async onClientMessage(e, t) {
|
|
27
|
+
let n = e, r = JSON.parse(t);
|
|
28
|
+
if (r.type === "action") {
|
|
29
|
+
let t = n.user;
|
|
30
|
+
await this.component.executeAction(r.action, r.payload, t, e);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
broadcastState(t) {
|
|
34
|
+
let n = JSON.stringify({
|
|
35
|
+
type: "state-update",
|
|
36
|
+
state: t
|
|
37
|
+
});
|
|
38
|
+
for (let t of this.clients) t.readyState === e.OPEN && t.send(n);
|
|
39
|
+
}
|
|
40
|
+
broadcastEvent(t, n) {
|
|
41
|
+
let r = JSON.stringify({
|
|
42
|
+
type: "event",
|
|
43
|
+
eventName: t,
|
|
44
|
+
payload: n
|
|
45
|
+
});
|
|
46
|
+
for (let t of this.clients) t.readyState === e.OPEN && t.send(r);
|
|
47
|
+
}
|
|
48
|
+
sendClientAction(t, n, r) {
|
|
49
|
+
let i = t, a = JSON.stringify({
|
|
50
|
+
type: "client-action",
|
|
51
|
+
action: n,
|
|
52
|
+
payload: r
|
|
53
|
+
});
|
|
54
|
+
i.readyState === e.OPEN && i.send(a);
|
|
55
|
+
}
|
|
56
|
+
async persistState() {}
|
|
57
|
+
}, p = /* @__PURE__ */ d((/* @__PURE__ */ l(((e, t) => {
|
|
58
|
+
t.exports = {};
|
|
59
|
+
})))(), 1);
|
|
60
|
+
function m(e) {
|
|
61
|
+
let t = e, n = t.indexOf("?");
|
|
62
|
+
n !== -1 && (t = t.substring(0, n));
|
|
63
|
+
let r = t.indexOf("#");
|
|
64
|
+
return r !== -1 && (t = t.substring(0, r)), t.startsWith("/") || (t = "/" + t), t !== "/" && t.endsWith("/") && (t = t.slice(0, -1)), t;
|
|
42
65
|
}
|
|
43
|
-
function
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
66
|
+
function h(e) {
|
|
67
|
+
let { root: t, prefix: n = "/", index: r = !0 } = e;
|
|
68
|
+
return async (e, i) => {
|
|
69
|
+
if (e.req.method !== "GET" && e.req.method !== "HEAD") return i();
|
|
70
|
+
let a = m(e.req.path);
|
|
71
|
+
if (!a.startsWith(n)) return i();
|
|
72
|
+
let o = a.substring(n.length);
|
|
73
|
+
o === "" && (o = "/");
|
|
74
|
+
let s;
|
|
75
|
+
if (o === "/") s = p.join(t, "index.html");
|
|
76
|
+
else if (s = p.join(t, o), r && !p.existsSync(s)) {
|
|
77
|
+
let e = p.join(s, "index.html");
|
|
78
|
+
p.existsSync(e) && (s = e);
|
|
79
|
+
}
|
|
80
|
+
try {
|
|
81
|
+
if (p.statSync(s).isDirectory()) if (r) {
|
|
82
|
+
let e = p.join(s, "index.html");
|
|
83
|
+
if (p.existsSync(e)) s = e;
|
|
84
|
+
else return i();
|
|
85
|
+
} else return i();
|
|
86
|
+
let t = p.readFileSync(s, "utf-8");
|
|
87
|
+
return g(s), e.html(t, 200);
|
|
88
|
+
} catch {
|
|
89
|
+
return i();
|
|
90
|
+
}
|
|
91
|
+
};
|
|
49
92
|
}
|
|
50
|
-
function
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
return o();
|
|
72
|
-
}
|
|
73
|
-
};
|
|
93
|
+
function g(e) {
|
|
94
|
+
return {
|
|
95
|
+
".html": "text/html",
|
|
96
|
+
".css": "text/css",
|
|
97
|
+
".js": "application/javascript",
|
|
98
|
+
".json": "application/json",
|
|
99
|
+
".xml": "application/xml",
|
|
100
|
+
".txt": "text/plain",
|
|
101
|
+
".png": "image/png",
|
|
102
|
+
".jpg": "image/jpeg",
|
|
103
|
+
".jpeg": "image/jpeg",
|
|
104
|
+
".gif": "image/gif",
|
|
105
|
+
".svg": "image/svg+xml",
|
|
106
|
+
".ico": "image/x-icon",
|
|
107
|
+
".woff": "font/woff",
|
|
108
|
+
".woff2": "font/woff2",
|
|
109
|
+
".ttf": "font/ttf",
|
|
110
|
+
".eot": "application/vnd.ms-fontobject",
|
|
111
|
+
".pdf": "application/pdf",
|
|
112
|
+
".zip": "application/zip"
|
|
113
|
+
}[p.extname(e).toLowerCase()] || "application/octet-stream";
|
|
74
114
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
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
|
|
115
|
+
//#endregion
|
|
116
|
+
//#region src/index.ts
|
|
117
|
+
var _ = class {
|
|
118
|
+
wss;
|
|
119
|
+
instances = /* @__PURE__ */ new Map();
|
|
120
|
+
componentRegistry;
|
|
121
|
+
constructor(e) {
|
|
122
|
+
this.wss = new t({ noServer: !0 }), this.componentRegistry = e.componentRegistry, e.server.on("upgrade", (t, r, i) => {
|
|
123
|
+
new n(t.url || "", `http://${t.headers.host}`).pathname.startsWith(e.path || "/ws") && this.handleUpgrade(t, r, i);
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
handleUpgrade(e, t, r) {
|
|
127
|
+
this.wss.handleUpgrade(e, t, r, async (t) => {
|
|
128
|
+
let r = new n(e.url || "", `http://${e.headers.host}`), i = r.pathname.split("/"), a = i.pop(), o = i.pop(), s = r.searchParams, c = s.get("componentId");
|
|
129
|
+
if (!a || !c) {
|
|
130
|
+
t.close(1008, "Missing target or componentId");
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
let l = this.instances.get(a);
|
|
134
|
+
if (!l) {
|
|
135
|
+
let e = this.componentRegistry.get(c);
|
|
136
|
+
if (!e) {
|
|
137
|
+
t.close(1008, "Component not found");
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
let n = new e(), r = s.get("pathname") || "/", i = { req: {
|
|
141
|
+
path: r,
|
|
142
|
+
param: (e) => {
|
|
143
|
+
if (e) return s.get(e);
|
|
144
|
+
let t = {};
|
|
145
|
+
return s.forEach((e, n) => {
|
|
146
|
+
t[n] = e;
|
|
147
|
+
}), t;
|
|
148
|
+
},
|
|
149
|
+
query: (e) => {
|
|
150
|
+
if (e) return s.get(e);
|
|
151
|
+
let t = {};
|
|
152
|
+
return s.forEach((e, n) => {
|
|
153
|
+
t[n] = e;
|
|
154
|
+
}), t;
|
|
155
|
+
}
|
|
156
|
+
} };
|
|
157
|
+
await n.bootstrap({
|
|
158
|
+
context: i,
|
|
159
|
+
page: r,
|
|
160
|
+
providerName: o
|
|
161
|
+
}), l = new f(n), this.instances.set(a, l);
|
|
162
|
+
}
|
|
163
|
+
l.addClient(t, { id: "guest" });
|
|
164
|
+
let u = l.component.getInitialState();
|
|
165
|
+
t.send(JSON.stringify({
|
|
166
|
+
type: "state-update",
|
|
167
|
+
state: u
|
|
168
|
+
}));
|
|
169
|
+
});
|
|
170
|
+
}
|
|
158
171
|
};
|
|
172
|
+
//#endregion
|
|
173
|
+
export { _ as CossackNodeAdapter, f as NodeWebSocketRuntime, h as serveStatic };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cossackframework/node-adapter",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Node.js runtime adapter for the Cossack Framework",
|
|
6
6
|
"license": "MIT",
|
|
@@ -25,12 +25,12 @@
|
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"hono": "^4.9.5",
|
|
27
27
|
"ws": "^8.16.0",
|
|
28
|
-
"@cossackframework/core": "0.
|
|
28
|
+
"@cossackframework/core": "0.4.0"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@types/ws": "^8.5.10",
|
|
32
32
|
"typescript": "^5.0.0",
|
|
33
|
-
"vite": "^
|
|
33
|
+
"vite": "^8.0.0",
|
|
34
34
|
"vite-plugin-dts": "^3.9.1",
|
|
35
35
|
"vite-tsconfig-paths": "^5.1.4"
|
|
36
36
|
},
|