@cossackframework/node-adapter 0.8.3 → 0.9.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 +80 -14
- package/agent-docs.json +59 -0
- package/dist/adapter.d.ts +37 -0
- package/dist/index.d.ts +7 -3
- package/dist/index.js +411 -103
- package/dist/notifications.d.ts +4 -0
- package/dist/notifications.js +40 -0
- package/dist/runtime.d.ts +2 -1
- package/dist/shutdown.d.ts +24 -0
- package/dist/storage.d.ts +23 -0
- package/dist/storage.js +226 -0
- package/dist/vite.d.ts +23 -0
- package/dist/vite.js +122 -0
- package/package.json +25 -5
package/dist/runtime.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { WebSocket } from 'ws';
|
|
2
2
|
import { InMemoryWebSocketRuntime, type Cossack } from '@cossackframework/core';
|
|
3
|
+
import type { Context } from 'hono';
|
|
3
4
|
export declare class NodeWebSocketRuntime extends InMemoryWebSocketRuntime<WebSocket> {
|
|
4
5
|
constructor(component: Cossack);
|
|
5
|
-
addClient(ws: WebSocket, user?: unknown): void;
|
|
6
|
+
addClient(ws: WebSocket, user?: unknown, context?: Context, authorize?: (client: WebSocket, operation: (context: Context) => Promise<void>) => Promise<boolean>): void;
|
|
6
7
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { EventEmitter } from 'node:events';
|
|
2
|
+
/** Operations shared by Node HTTP, HTTPS and HTTP/2 compatibility servers. */
|
|
3
|
+
export interface NodeShutdownServer extends Pick<EventEmitter, 'on' | 'off'> {
|
|
4
|
+
close(callback: () => void): unknown;
|
|
5
|
+
closeIdleConnections?(): void;
|
|
6
|
+
closeAllConnections?(): void;
|
|
7
|
+
}
|
|
8
|
+
export interface NodeShutdownOptions {
|
|
9
|
+
/** Deadline for HTTP requests and runtime cleanup together. Default: 10 seconds. */
|
|
10
|
+
timeoutMs?: number;
|
|
11
|
+
dispose(): Promise<void>;
|
|
12
|
+
/** Called once after draining or the deadline. Signal handlers do not call process.exit. */
|
|
13
|
+
onComplete?: (result: NodeShutdownResult) => void;
|
|
14
|
+
}
|
|
15
|
+
export interface NodeShutdownResult {
|
|
16
|
+
forced: boolean;
|
|
17
|
+
cleanupCompleted: boolean;
|
|
18
|
+
cleanupFailed: boolean;
|
|
19
|
+
}
|
|
20
|
+
/** Explicit process ownership: stop accepting HTTP, dispose the runtime, and bound draining. */
|
|
21
|
+
export declare function installNodeShutdown(server: NodeShutdownServer, options: NodeShutdownOptions): {
|
|
22
|
+
shutdown: () => Promise<NodeShutdownResult>;
|
|
23
|
+
uninstall: () => void;
|
|
24
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { StorageError } from '@cossackframework/core/storage';
|
|
2
|
+
import type { StorageProvider, StorageMetadata, StorageObject, StoragePutOptions, StorageReadOptions } from '@cossackframework/core/storage';
|
|
3
|
+
export declare class LocalStorageError extends StorageError {
|
|
4
|
+
constructor();
|
|
5
|
+
}
|
|
6
|
+
/** Persistent filesystem storage for a root exclusively owned by one Node process.
|
|
7
|
+
* Objects use immutable generations; atomic manifest rename publishes complete bytes. */
|
|
8
|
+
export declare class LocalStorageProvider implements StorageProvider {
|
|
9
|
+
readonly name = "local-filesystem";
|
|
10
|
+
readonly root: string;
|
|
11
|
+
constructor(options: {
|
|
12
|
+
root: string;
|
|
13
|
+
});
|
|
14
|
+
private initialize;
|
|
15
|
+
private prefix;
|
|
16
|
+
private own;
|
|
17
|
+
private safeOpen;
|
|
18
|
+
private manifest;
|
|
19
|
+
put(key: string, body: ReadableStream<Uint8Array>, options: StoragePutOptions): Promise<StorageMetadata>;
|
|
20
|
+
head(key: string, options?: StorageReadOptions): Promise<StorageMetadata | null>;
|
|
21
|
+
get(key: string, options?: StorageReadOptions): Promise<StorageObject | null>;
|
|
22
|
+
delete(key: string, options?: StorageReadOptions): Promise<void>;
|
|
23
|
+
}
|
package/dist/storage.js
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { constants as e } from "node:fs";
|
|
2
|
+
import { mkdir as t, open as n, realpath as r, rename as i, unlink as a } from "node:fs/promises";
|
|
3
|
+
import o from "node:path";
|
|
4
|
+
import { createHash as s, randomUUID as c } from "node:crypto";
|
|
5
|
+
import { StorageError as l } from "@cossackframework/core/storage";
|
|
6
|
+
//#region src/storage.ts
|
|
7
|
+
var u = class extends l {
|
|
8
|
+
constructor() {
|
|
9
|
+
super("COSSACK_STORAGE_PROVIDER_FAILED", "Local storage failed. Check the isolated root, object metadata, and filesystem permissions."), this.name = "LocalStorageError";
|
|
10
|
+
}
|
|
11
|
+
}, d = /* @__PURE__ */ new Map();
|
|
12
|
+
function f(e) {
|
|
13
|
+
if (e?.aborted) throw new DOMException("Storage operation was cancelled.", "AbortError");
|
|
14
|
+
}
|
|
15
|
+
function p(e) {
|
|
16
|
+
if (typeof e != "string" || e.length > 1024 || !e || e.split("/").some((e) => !/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,127}$/.test(e) || e === "." || e === "..")) throw new u();
|
|
17
|
+
}
|
|
18
|
+
function m(e) {
|
|
19
|
+
if (!Number.isSafeInteger(e.size) || e.size < 0 || e.contentType !== void 0 && (typeof e.contentType != "string" || e.contentType.length > 256 || /[\x00-\x1f\x7f]/.test(e.contentType))) throw new u();
|
|
20
|
+
let t;
|
|
21
|
+
if (e.customMetadata !== void 0) {
|
|
22
|
+
if (!e.customMetadata || ![Object.prototype, null].includes(Object.getPrototypeOf(e.customMetadata))) throw new u();
|
|
23
|
+
t = Object.create(null);
|
|
24
|
+
for (let n of Reflect.ownKeys(e.customMetadata)) {
|
|
25
|
+
let r = Object.getOwnPropertyDescriptor(e.customMetadata, n);
|
|
26
|
+
if (typeof n != "string" || !/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,127}$/.test(n) || !("value" in r) || typeof r.value != "string" || r.value.length > 2048) throw new u();
|
|
27
|
+
t[n] = r.value;
|
|
28
|
+
}
|
|
29
|
+
if (Object.keys(t).length > 64 || Buffer.byteLength(JSON.stringify(t)) > 8192) throw new u();
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
size: e.size,
|
|
33
|
+
...e.contentType === void 0 ? {} : { contentType: e.contentType },
|
|
34
|
+
...t ? { customMetadata: t } : {}
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function h(e) {
|
|
38
|
+
return e?.code === "ENOENT";
|
|
39
|
+
}
|
|
40
|
+
function g(e) {
|
|
41
|
+
return {
|
|
42
|
+
key: e.key,
|
|
43
|
+
size: e.size,
|
|
44
|
+
etag: e.etag,
|
|
45
|
+
uploaded: new Date(e.uploaded),
|
|
46
|
+
...e.contentType === void 0 ? {} : { contentType: e.contentType },
|
|
47
|
+
...e.customMetadata ? { customMetadata: { ...e.customMetadata } } : {}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
async function _(e, t) {
|
|
51
|
+
if (f(t), !t) return e;
|
|
52
|
+
let n, r = new Promise((e, r) => {
|
|
53
|
+
n = () => r(new DOMException("Storage operation was cancelled.", "AbortError")), t.addEventListener("abort", n, { once: !0 });
|
|
54
|
+
});
|
|
55
|
+
try {
|
|
56
|
+
f(t), await Promise.race([e, r]);
|
|
57
|
+
} finally {
|
|
58
|
+
t.removeEventListener("abort", n);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
async function v(e, t) {
|
|
62
|
+
if (f(t), !t) return e.read();
|
|
63
|
+
let n, r = new Promise((e, r) => {
|
|
64
|
+
n = () => r(new DOMException("Storage operation was cancelled.", "AbortError")), t.addEventListener("abort", n, { once: !0 });
|
|
65
|
+
});
|
|
66
|
+
try {
|
|
67
|
+
return f(t), await Promise.race([e.read(), r]);
|
|
68
|
+
} finally {
|
|
69
|
+
t.removeEventListener("abort", n);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
var y = class {
|
|
73
|
+
name = "local-filesystem";
|
|
74
|
+
root;
|
|
75
|
+
constructor(e) {
|
|
76
|
+
if (!e?.root || typeof e.root != "string") throw new u();
|
|
77
|
+
this.root = o.resolve(e.root);
|
|
78
|
+
}
|
|
79
|
+
async initialize() {
|
|
80
|
+
let e = this.root;
|
|
81
|
+
for (;;) try {
|
|
82
|
+
if (await r(e) !== e) throw new u();
|
|
83
|
+
break;
|
|
84
|
+
} catch (t) {
|
|
85
|
+
if (!h(t)) throw t;
|
|
86
|
+
let n = o.dirname(e);
|
|
87
|
+
if (n === e) throw t;
|
|
88
|
+
e = n;
|
|
89
|
+
}
|
|
90
|
+
if (await t(this.root, {
|
|
91
|
+
recursive: !0,
|
|
92
|
+
mode: 448
|
|
93
|
+
}), await r(this.root) !== this.root) throw new u();
|
|
94
|
+
}
|
|
95
|
+
prefix(e) {
|
|
96
|
+
return p(e), s("sha256").update(e).digest("hex");
|
|
97
|
+
}
|
|
98
|
+
async own(e, t, n) {
|
|
99
|
+
let r = this.prefix(e);
|
|
100
|
+
f(t);
|
|
101
|
+
let i = this.root + "/" + r, a = d.get(i) ?? Promise.resolve(), o, s = new Promise((e) => {
|
|
102
|
+
o = e;
|
|
103
|
+
}), c = a.then(() => s);
|
|
104
|
+
d.set(i, c);
|
|
105
|
+
try {
|
|
106
|
+
return await _(a, t), f(t), await this.initialize(), await n(r);
|
|
107
|
+
} catch (e) {
|
|
108
|
+
throw e?.name === "AbortError" || e instanceof u ? e : new u();
|
|
109
|
+
} finally {
|
|
110
|
+
o(), c.then(() => {
|
|
111
|
+
d.get(i) === c && d.delete(i);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
async safeOpen(t, i, a) {
|
|
116
|
+
let s = o.join(this.root, t), c = await n(s, i | e.O_NOFOLLOW | e.O_NONBLOCK, a);
|
|
117
|
+
try {
|
|
118
|
+
let e = await c.stat();
|
|
119
|
+
if (await r(s) !== s || !e.isFile() || e.nlink !== 1) throw new u();
|
|
120
|
+
return c;
|
|
121
|
+
} catch (e) {
|
|
122
|
+
throw await c.close(), e;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
async manifest(t, n) {
|
|
126
|
+
let r;
|
|
127
|
+
try {
|
|
128
|
+
r = await this.safeOpen(n + ".json", e.O_RDONLY);
|
|
129
|
+
} catch (e) {
|
|
130
|
+
if (h(e)) return null;
|
|
131
|
+
throw e;
|
|
132
|
+
}
|
|
133
|
+
try {
|
|
134
|
+
if ((await r.stat()).size > 16384) throw new u();
|
|
135
|
+
let e = JSON.parse(await r.readFile("utf8"));
|
|
136
|
+
if (e.key !== t || !RegExp(`^${n}\\.[a-f0-9-]{36}\\.blob$`).test(e.file) || !/^"[a-f0-9]{64}"$/.test(e.etag) || !Number.isSafeInteger(e.uploaded) || e.uploaded < 0) throw new u();
|
|
137
|
+
return m(e), e;
|
|
138
|
+
} finally {
|
|
139
|
+
await r.close();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
async put(t, n, r) {
|
|
143
|
+
let l = m(r);
|
|
144
|
+
return this.own(t, r.signal, async (d) => {
|
|
145
|
+
let p = await this.manifest(t, d), m = c(), h = `${d}.${m}.blob`, _ = `${d}.${m}.json.tmp`, y, b, x, S = !1;
|
|
146
|
+
try {
|
|
147
|
+
y = await this.safeOpen(h, e.O_WRONLY | e.O_CREAT | e.O_EXCL, 384), x = n.getReader();
|
|
148
|
+
let c = s("sha256"), m = 0;
|
|
149
|
+
for (;;) {
|
|
150
|
+
let e = await v(x, r.signal);
|
|
151
|
+
if (e.done) break;
|
|
152
|
+
if (!(e.value instanceof Uint8Array) || m + e.value.byteLength > l.size) throw new u();
|
|
153
|
+
let t = 0;
|
|
154
|
+
for (; t < e.value.byteLength;) {
|
|
155
|
+
f(r.signal);
|
|
156
|
+
let n = await y.write(e.value, t, e.value.byteLength - t);
|
|
157
|
+
if (!n.bytesWritten) throw new u();
|
|
158
|
+
t += n.bytesWritten;
|
|
159
|
+
}
|
|
160
|
+
c.update(e.value), m += e.value.byteLength;
|
|
161
|
+
}
|
|
162
|
+
if (f(r.signal), m !== l.size) throw new u();
|
|
163
|
+
await y.sync(), await y.close(), y = void 0;
|
|
164
|
+
let C = {
|
|
165
|
+
key: t,
|
|
166
|
+
...l,
|
|
167
|
+
etag: `"${c.digest("hex")}"`,
|
|
168
|
+
uploaded: Date.now(),
|
|
169
|
+
file: h
|
|
170
|
+
};
|
|
171
|
+
return b = await this.safeOpen(_, e.O_WRONLY | e.O_CREAT | e.O_EXCL, 384), await b.writeFile(JSON.stringify(C)), await b.sync(), await b.close(), b = void 0, f(r.signal), await i(o.join(this.root, _), o.join(this.root, d + ".json")), S = !0, p && await a(o.join(this.root, p.file)).catch(() => {}), g(C);
|
|
172
|
+
} finally {
|
|
173
|
+
x && (S || x.cancel(new DOMException("Storage operation was cancelled.", "AbortError")).catch(() => {}), x.releaseLock()), await y?.close(), await b?.close(), await a(o.join(this.root, _)).catch(() => {}), S || await a(o.join(this.root, h)).catch(() => {});
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
async head(e, t = {}) {
|
|
178
|
+
return this.own(e, t.signal, async (n) => {
|
|
179
|
+
let r = await this.manifest(e, n);
|
|
180
|
+
return f(t.signal), r ? g(r) : null;
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
async get(t, n = {}) {
|
|
184
|
+
return this.own(t, n.signal, async (r) => {
|
|
185
|
+
let i = await this.manifest(t, r);
|
|
186
|
+
if (!i) return null;
|
|
187
|
+
let a = await this.safeOpen(i.file, e.O_RDONLY);
|
|
188
|
+
try {
|
|
189
|
+
if ((await a.stat()).size !== i.size) throw new u();
|
|
190
|
+
} catch (e) {
|
|
191
|
+
throw await a.close(), e;
|
|
192
|
+
}
|
|
193
|
+
let o, s, c = () => (n.signal?.removeEventListener("abort", s), o ??= a.close()), l = new ReadableStream({
|
|
194
|
+
start(e) {
|
|
195
|
+
s = () => {
|
|
196
|
+
e.error(new DOMException("Storage operation was cancelled.", "AbortError")), c().catch(() => {});
|
|
197
|
+
}, n.signal?.addEventListener("abort", s, { once: !0 }), n.signal?.aborted && s();
|
|
198
|
+
},
|
|
199
|
+
async pull(e) {
|
|
200
|
+
try {
|
|
201
|
+
f(n.signal);
|
|
202
|
+
let t = new Uint8Array(64 * 1024), { bytesRead: r } = await a.read(t);
|
|
203
|
+
r ? e.enqueue(t.subarray(0, r)) : (await c(), e.close());
|
|
204
|
+
} catch {
|
|
205
|
+
await c(), e.error(n.signal?.aborted ? new DOMException("Storage operation was cancelled.", "AbortError") : new u());
|
|
206
|
+
}
|
|
207
|
+
},
|
|
208
|
+
async cancel() {
|
|
209
|
+
await c();
|
|
210
|
+
}
|
|
211
|
+
}, { highWaterMark: 0 });
|
|
212
|
+
return {
|
|
213
|
+
...g(i),
|
|
214
|
+
body: l
|
|
215
|
+
};
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
async delete(e, t = {}) {
|
|
219
|
+
return this.own(e, t.signal, async (n) => {
|
|
220
|
+
let r = await this.manifest(e, n);
|
|
221
|
+
f(t.signal), r && (await a(o.join(this.root, n + ".json")), await a(o.join(this.root, r.file)).catch(() => {}));
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
//#endregion
|
|
226
|
+
export { u as LocalStorageError, y as LocalStorageProvider };
|
package/dist/vite.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type Plugin } from 'vite';
|
|
2
|
+
import type { NodeUpgradeServer } from './adapter.js';
|
|
3
|
+
/** Structural application entry; this tooling never imports the framework. */
|
|
4
|
+
export interface NodeDevelopmentEntry {
|
|
5
|
+
app: {
|
|
6
|
+
fetch(request: Request, env?: Record<string, unknown>): Response | Promise<Response>;
|
|
7
|
+
};
|
|
8
|
+
env?: Record<string, unknown>;
|
|
9
|
+
/** Build server bindings from Vite's environment files and process overrides. */
|
|
10
|
+
createEnv?(variables: Record<string, string | undefined>): Record<string, unknown> | Promise<Record<string, unknown>>;
|
|
11
|
+
runtime?: {
|
|
12
|
+
attach?(server: NodeUpgradeServer, fetch: (request: Request) => Response | Promise<Response>): () => void;
|
|
13
|
+
dispose(): void | Promise<void>;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export interface NodeDevelopmentOptions {
|
|
17
|
+
/** Vite server entry, relative to the project root. */
|
|
18
|
+
entry?: string;
|
|
19
|
+
/** Maximum time to wait for application cleanup when closing Vite. */
|
|
20
|
+
shutdownTimeoutMs?: number;
|
|
21
|
+
}
|
|
22
|
+
/** Serve generated Node applications through Vite's SSR environment. */
|
|
23
|
+
export declare function cossackNode(options?: NodeDevelopmentOptions): Plugin;
|
package/dist/vite.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { relative as e } from "node:path";
|
|
2
|
+
import { getRequestListener as t } from "@hono/node-server";
|
|
3
|
+
import { isRunnableDevEnvironment as n, loadEnv as r } from "vite";
|
|
4
|
+
import { EventEmitter as i } from "node:events";
|
|
5
|
+
//#region src/vite.ts
|
|
6
|
+
function a(a = {}) {
|
|
7
|
+
let o = a.shutdownTimeoutMs ?? 5e3;
|
|
8
|
+
if (!Number.isFinite(o) || o <= 0) throw TypeError("shutdownTimeoutMs must be positive and finite");
|
|
9
|
+
let s, c;
|
|
10
|
+
return {
|
|
11
|
+
name: "cossack-node-development",
|
|
12
|
+
apply: "serve",
|
|
13
|
+
config: () => ({
|
|
14
|
+
appType: "custom",
|
|
15
|
+
environments: { ssr: { resolve: { noExternal: ["@cossackframework/node-adapter", "@cossackframework/core"] } } }
|
|
16
|
+
}),
|
|
17
|
+
configureServer(e) {
|
|
18
|
+
if (e.config.server.middlewareMode) return;
|
|
19
|
+
let l = e.environments.ssr;
|
|
20
|
+
if (!n(l)) throw Error("Cossack Node development requires a runnable Vite SSR environment");
|
|
21
|
+
let u = {
|
|
22
|
+
...r(e.config.mode, e.config.envDir, ""),
|
|
23
|
+
...process.env
|
|
24
|
+
}, d, f = !1, p = 0, m = !1, h, g = /* @__PURE__ */ new Set(), _ = /* @__PURE__ */ new Set(), v = /* @__PURE__ */ new WeakSet(), y = (t) => {
|
|
25
|
+
if (!t.runtime || v.has(t.runtime)) return;
|
|
26
|
+
v.add(t.runtime);
|
|
27
|
+
let n = Promise.resolve().then(() => t.runtime?.dispose()).then(() => {});
|
|
28
|
+
_.add(n), n.catch((t) => e.config.logger.error(`[cossack node] cleanup failed: ${t}`)).finally(() => _.delete(n));
|
|
29
|
+
}, b = () => {
|
|
30
|
+
d &&= (d.detach?.(), y(d.entry), void 0);
|
|
31
|
+
}, x = () => f ? Promise.reject(/* @__PURE__ */ Error("Cossack Node development server is closing")) : d ? Promise.resolve(d) : h ??= (async () => {
|
|
32
|
+
for (; !f;) {
|
|
33
|
+
let e = p;
|
|
34
|
+
m &&= (l.moduleGraph.invalidateAll(), l.runner.clearCache(), !1);
|
|
35
|
+
let t = await l.runner.import(a.entry ?? "/src/index.ts");
|
|
36
|
+
if (typeof t.app?.fetch != "function") throw y(t), TypeError("Cossack Node entry must export app.fetch");
|
|
37
|
+
let n;
|
|
38
|
+
try {
|
|
39
|
+
n = t.createEnv ? await t.createEnv(u) : t.env;
|
|
40
|
+
} catch (e) {
|
|
41
|
+
throw y(t), e;
|
|
42
|
+
}
|
|
43
|
+
if (f || e !== p) {
|
|
44
|
+
y(t);
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
let r = {
|
|
48
|
+
entry: t,
|
|
49
|
+
env: n,
|
|
50
|
+
upgrades: new i()
|
|
51
|
+
};
|
|
52
|
+
try {
|
|
53
|
+
r.detach = t.runtime?.attach?.(r.upgrades, (e) => t.app.fetch(e, n));
|
|
54
|
+
} catch (e) {
|
|
55
|
+
throw y(t), e;
|
|
56
|
+
}
|
|
57
|
+
return d = r, r;
|
|
58
|
+
}
|
|
59
|
+
throw Error("Cossack Node development server is closing");
|
|
60
|
+
})().catch((e) => {
|
|
61
|
+
throw m = !0, e;
|
|
62
|
+
}).finally(() => {
|
|
63
|
+
h = void 0;
|
|
64
|
+
});
|
|
65
|
+
c = () => (p++, m = !0, b(), x());
|
|
66
|
+
let S = (t, n, r) => {
|
|
67
|
+
if (!t.url?.startsWith("/ws/")) return;
|
|
68
|
+
g.add(n);
|
|
69
|
+
let i = () => g.delete(n);
|
|
70
|
+
n.once("close", i), n.on("error", () => n.destroy()), x().then((e) => {
|
|
71
|
+
f || n.destroyed || e.upgrades.emit("upgrade", t, n, r) || n.end("HTTP/1.1 503 Unavailable\r\nConnection: close\r\nContent-Length: 0\r\n\r\n");
|
|
72
|
+
}).catch((t) => {
|
|
73
|
+
e.config.logger.error(`[cossack node] upgrade failed: ${t}`), n.destroy();
|
|
74
|
+
}).finally(() => {
|
|
75
|
+
i(), n.off("close", i);
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
e.httpServer?.on("upgrade", S);
|
|
79
|
+
let C;
|
|
80
|
+
return s = () => C ??= (async () => {
|
|
81
|
+
f = !0, c = void 0, e.httpServer?.off("upgrade", S);
|
|
82
|
+
for (let e of g) e.destroy();
|
|
83
|
+
g.clear(), b();
|
|
84
|
+
let t = (async () => {
|
|
85
|
+
await h?.catch(() => {}), await Promise.allSettled([..._]);
|
|
86
|
+
})(), n;
|
|
87
|
+
try {
|
|
88
|
+
await Promise.race([t, new Promise((t) => {
|
|
89
|
+
n = setTimeout(() => {
|
|
90
|
+
e.config.logger.warn("[cossack node] application cleanup exceeded the shutdown deadline"), t();
|
|
91
|
+
}, o);
|
|
92
|
+
})]);
|
|
93
|
+
} finally {
|
|
94
|
+
clearTimeout(n);
|
|
95
|
+
}
|
|
96
|
+
})(), () => e.middlewares.use((e, n, r) => {
|
|
97
|
+
x().then(({ entry: r, env: i }) => t((e) => r.app.fetch(e, i))(e, n)).catch(r);
|
|
98
|
+
});
|
|
99
|
+
},
|
|
100
|
+
hotUpdate: {
|
|
101
|
+
order: "post",
|
|
102
|
+
async handler({ file: t, modules: n, read: r, type: i, server: a }) {
|
|
103
|
+
if (this.environment.name !== "ssr" || !c) return;
|
|
104
|
+
let o = e(a.config.root, t).replaceAll("\\", "/");
|
|
105
|
+
if (!(!n.length && !(o.startsWith("src/") && !o.startsWith("src/client/") && /\.[cm]?[jt]sx?$|\.mdx?$/.test(o)))) {
|
|
106
|
+
i !== "delete" && await r();
|
|
107
|
+
try {
|
|
108
|
+
await c();
|
|
109
|
+
} catch (e) {
|
|
110
|
+
a.config.logger.error(`[cossack node] reload failed: ${e}`);
|
|
111
|
+
}
|
|
112
|
+
return [];
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
async closeBundle() {
|
|
117
|
+
await s?.();
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
//#endregion
|
|
122
|
+
export { a as cossackNode };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cossackframework/node-adapter",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Node.js runtime adapter for the Cossack Framework",
|
|
6
6
|
"license": "MIT",
|
|
@@ -16,10 +16,23 @@
|
|
|
16
16
|
".": {
|
|
17
17
|
"import": "./dist/index.js",
|
|
18
18
|
"types": "./dist/index.d.ts"
|
|
19
|
+
},
|
|
20
|
+
"./vite": {
|
|
21
|
+
"types": "./dist/vite.d.ts",
|
|
22
|
+
"import": "./dist/vite.js"
|
|
23
|
+
},
|
|
24
|
+
"./notifications": {
|
|
25
|
+
"types": "./dist/notifications.d.ts",
|
|
26
|
+
"import": "./dist/notifications.js"
|
|
27
|
+
},
|
|
28
|
+
"./storage": {
|
|
29
|
+
"types": "./dist/storage.d.ts",
|
|
30
|
+
"import": "./dist/storage.js"
|
|
19
31
|
}
|
|
20
32
|
},
|
|
21
33
|
"files": [
|
|
22
|
-
"dist"
|
|
34
|
+
"dist",
|
|
35
|
+
"agent-docs.json"
|
|
23
36
|
],
|
|
24
37
|
"publishConfig": {
|
|
25
38
|
"access": "public"
|
|
@@ -27,7 +40,8 @@
|
|
|
27
40
|
"dependencies": {
|
|
28
41
|
"nodemailer": "^10.0.6",
|
|
29
42
|
"ws": ">=8.21.0",
|
|
30
|
-
"@
|
|
43
|
+
"@hono/node-server": "^2.1.0",
|
|
44
|
+
"@cossackframework/core": "0.9.0"
|
|
31
45
|
},
|
|
32
46
|
"devDependencies": {
|
|
33
47
|
"@types/nodemailer": "^8.0.1",
|
|
@@ -38,10 +52,16 @@
|
|
|
38
52
|
"vitest": "^4.1.11"
|
|
39
53
|
},
|
|
40
54
|
"peerDependencies": {
|
|
41
|
-
"hono": "^4.13.7"
|
|
55
|
+
"hono": "^4.13.7",
|
|
56
|
+
"vite": "^8.1.4"
|
|
57
|
+
},
|
|
58
|
+
"peerDependenciesMeta": {
|
|
59
|
+
"vite": {
|
|
60
|
+
"optional": true
|
|
61
|
+
}
|
|
42
62
|
},
|
|
43
63
|
"scripts": {
|
|
44
|
-
"build": "vite build && tsc -p tsconfig.declarations.json",
|
|
64
|
+
"build": "vite build && tsc -p tsconfig.declarations.json && node ../../scripts/rewrite-declaration-imports.mjs dist",
|
|
45
65
|
"test": "vitest --run"
|
|
46
66
|
}
|
|
47
67
|
}
|