@cossackframework/node-adapter 0.9.0 → 0.9.2

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.
@@ -16,7 +16,16 @@ export interface StaticServeOptions {
16
16
  * Create a middleware that serves static files from a directory.
17
17
  * This is for Node.js runtime only - uses fs to read files.
18
18
  *
19
+ * Files are streamed. Responses carry a weak `ETag`, `Last-Modified` and
20
+ * `Accept-Ranges: bytes`; `If-None-Match`/`If-Modified-Since` return 304, and a
21
+ * single `Range` on GET returns 206 (416 when unsatisfiable). Several ranges
22
+ * receive the whole file with 200.
23
+ *
19
24
  * @param options - Static serving options
20
25
  * @returns Hono middleware
21
26
  */
22
27
  export declare function serveStatic(options: StaticServeOptions): (c: Context, next: Next) => Promise<void | Response>;
28
+ /** Filesystem-backed ASSETS binding for SSR manifest and CSS reads. */
29
+ export declare function createStaticAssets(options: StaticServeOptions): {
30
+ fetch(request: Request): Promise<Response>;
31
+ };
package/dist/storage.d.ts CHANGED
@@ -11,6 +11,9 @@ export declare class LocalStorageProvider implements StorageProvider {
11
11
  constructor(options: {
12
12
  root: string;
13
13
  });
14
+ /** Shared by concurrent callers; cleared on failure and when the root disappears. */
15
+ private initialized;
16
+ private ready;
14
17
  private initialize;
15
18
  private prefix;
16
19
  private own;
package/dist/storage.js CHANGED
@@ -76,6 +76,13 @@ var y = class {
76
76
  if (!e?.root || typeof e.root != "string") throw new u();
77
77
  this.root = o.resolve(e.root);
78
78
  }
79
+ initialized;
80
+ ready() {
81
+ let e = this.initialized ??= this.initialize().catch((t) => {
82
+ throw this.initialized === e && (this.initialized = void 0), t;
83
+ });
84
+ return e;
85
+ }
79
86
  async initialize() {
80
87
  let e = this.root;
81
88
  for (;;) try {
@@ -103,7 +110,15 @@ var y = class {
103
110
  }), c = a.then(() => s);
104
111
  d.set(i, c);
105
112
  try {
106
- return await _(a, t), f(t), await this.initialize(), await n(r);
113
+ await _(a, t), f(t);
114
+ let e = this.ready();
115
+ await e;
116
+ try {
117
+ return await n(r);
118
+ } catch (i) {
119
+ if (!h(i)) throw i;
120
+ return this.initialized === e && (this.initialized = void 0), f(t), await this.ready(), await n(r);
121
+ }
107
122
  } catch (e) {
108
123
  throw e?.name === "AbortError" || e instanceof u ? e : new u();
109
124
  } finally {
@@ -0,0 +1,32 @@
1
+ //#region ../core/dist/shared/transport-routes.js
2
+ var e = Object.freeze({
3
+ rpc: "/crpc",
4
+ upload: "/upload",
5
+ sse: "/sse/:componentRouteId",
6
+ websocket: "/ws/:id"
7
+ }), t = {
8
+ rpc: "RPC calls",
9
+ upload: "RPC calls with file uploads",
10
+ sse: "server-sent event streams",
11
+ websocket: "component WebSocket connections"
12
+ };
13
+ function n(n) {
14
+ let r = e[n], i = r.split("/"), a = i.findIndex((e) => e.startsWith(":") || e.includes("*"));
15
+ return a === -1 ? {
16
+ path: r,
17
+ match: "exact",
18
+ route: r,
19
+ purpose: t[n]
20
+ } : {
21
+ path: i.slice(0, a).join("/"),
22
+ match: "namespace",
23
+ route: r,
24
+ purpose: t[n]
25
+ };
26
+ }
27
+ var r = Object.freeze(Object.keys(e).map((e) => Object.freeze(n(e))));
28
+ function i(t, ...n) {
29
+ return (n.length ? r.filter((t) => n.some((n) => e[n] === t.route)) : r).some((e) => e.match === "exact" ? t === e.path : t.startsWith(`${e.path}/`));
30
+ }
31
+ //#endregion
32
+ export { i as t };
package/dist/vite.js CHANGED
@@ -1,12 +1,18 @@
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";
1
+ import { t as e } from "./transport-routes-CjDWcC93.js";
2
+ import { relative as t } from "node:path";
3
+ import { getRequestListener as n } from "@hono/node-server";
4
+ import { isRunnableDevEnvironment as r, loadEnv as i } from "vite";
5
+ import { EventEmitter as a } from "node:events";
5
6
  //#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;
7
+ function o(e) {
8
+ if (r(e)) return !0;
9
+ let t = e.runner;
10
+ return typeof t?.import == "function" && typeof t.clearCache == "function" && typeof e.moduleGraph?.invalidateAll == "function";
11
+ }
12
+ function s(r = {}) {
13
+ let s = r.shutdownTimeoutMs ?? 5e3;
14
+ if (!Number.isFinite(s) || s <= 0) throw TypeError("shutdownTimeoutMs must be positive and finite");
15
+ let c, l;
10
16
  return {
11
17
  name: "cossack-node-development",
12
18
  apply: "serve",
@@ -14,98 +20,98 @@ function a(a = {}) {
14
20
  appType: "custom",
15
21
  environments: { ssr: { resolve: { noExternal: ["@cossackframework/node-adapter", "@cossackframework/core"] } } }
16
22
  }),
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
+ configureServer(t) {
24
+ if (t.config.server.middlewareMode) return;
25
+ let u = t.environments.ssr;
26
+ if (!o(u)) throw Error("Cossack Node development requires a runnable Vite SSR environment");
27
+ let d = {
28
+ ...i(t.config.mode, t.config.envDir, ""),
23
29
  ...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");
30
+ }, f, p = !1, m = 0, h = !1, g, _ = /* @__PURE__ */ new Set(), v = /* @__PURE__ */ new Set(), y = /* @__PURE__ */ new WeakSet(), b = (e) => {
31
+ if (!e.runtime || y.has(e.runtime)) return;
32
+ y.add(e.runtime);
33
+ let n = Promise.resolve().then(() => e.runtime?.dispose()).then(() => {});
34
+ v.add(n), n.catch((e) => t.config.logger.error(`[cossack node] cleanup failed: ${e}`)).finally(() => v.delete(n));
35
+ }, x = () => {
36
+ f &&= (f.detach?.(), b(f.entry), void 0);
37
+ }, S = () => p ? Promise.reject(/* @__PURE__ */ Error("Cossack Node development server is closing")) : f ? Promise.resolve(f) : g ??= (async () => {
38
+ for (; !p;) {
39
+ let e = m;
40
+ h &&= (u.moduleGraph.invalidateAll(), u.runner.clearCache(), !1);
41
+ let t = await u.runner.import(r.entry ?? "/src/index.ts");
42
+ if (typeof t.app?.fetch != "function") throw b(t), TypeError("Cossack Node entry must export app.fetch");
37
43
  let n;
38
44
  try {
39
- n = t.createEnv ? await t.createEnv(u) : t.env;
45
+ n = t.createEnv ? await t.createEnv(d) : t.env;
40
46
  } catch (e) {
41
- throw y(t), e;
47
+ throw b(t), e;
42
48
  }
43
- if (f || e !== p) {
44
- y(t);
49
+ if (p || e !== m) {
50
+ b(t);
45
51
  continue;
46
52
  }
47
- let r = {
53
+ let i = {
48
54
  entry: t,
49
55
  env: n,
50
- upgrades: new i()
56
+ upgrades: new a()
51
57
  };
52
58
  try {
53
- r.detach = t.runtime?.attach?.(r.upgrades, (e) => t.app.fetch(e, n));
59
+ i.detach = t.runtime?.attach?.(i.upgrades, (e) => t.app.fetch(e, n));
54
60
  } catch (e) {
55
- throw y(t), e;
61
+ throw b(t), e;
56
62
  }
57
- return d = r, r;
63
+ return f = i, i;
58
64
  }
59
65
  throw Error("Cossack Node development server is closing");
60
66
  })().catch((e) => {
61
- throw m = !0, e;
67
+ throw h = !0, e;
62
68
  }).finally(() => {
63
- h = void 0;
69
+ g = void 0;
64
70
  });
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();
71
+ l = () => (m++, h = !0, x(), S());
72
+ let C = (n, r, i) => {
73
+ if (!e(n.url?.split("?", 1)[0] ?? "", "websocket")) return;
74
+ _.add(r);
75
+ let a = () => _.delete(r);
76
+ r.once("close", a), r.on("error", () => r.destroy()), S().then((e) => {
77
+ p || r.destroyed || e.upgrades.emit("upgrade", n, r, i) || r.end("HTTP/1.1 503 Unavailable\r\nConnection: close\r\nContent-Length: 0\r\n\r\n");
78
+ }).catch((e) => {
79
+ t.config.logger.error(`[cossack node] upgrade failed: ${e}`), r.destroy();
74
80
  }).finally(() => {
75
- i(), n.off("close", i);
81
+ a(), r.off("close", a);
76
82
  });
77
83
  };
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([..._]);
84
+ t.httpServer?.on("upgrade", C);
85
+ let w;
86
+ return c = () => w ??= (async () => {
87
+ p = !0, l = void 0, t.httpServer?.off("upgrade", C);
88
+ for (let e of _) e.destroy();
89
+ _.clear(), x();
90
+ let e = (async () => {
91
+ await g?.catch(() => {}), await Promise.allSettled([...v]);
86
92
  })(), n;
87
93
  try {
88
- await Promise.race([t, new Promise((t) => {
94
+ await Promise.race([e, new Promise((e) => {
89
95
  n = setTimeout(() => {
90
- e.config.logger.warn("[cossack node] application cleanup exceeded the shutdown deadline"), t();
91
- }, o);
96
+ t.config.logger.warn("[cossack node] application cleanup exceeded the shutdown deadline"), e();
97
+ }, s);
92
98
  })]);
93
99
  } finally {
94
100
  clearTimeout(n);
95
101
  }
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);
102
+ })(), () => t.middlewares.use((e, t, r) => {
103
+ S().then(({ entry: r, env: i }) => n((e) => r.app.fetch(e, i))(e, t)).catch(r);
98
104
  });
99
105
  },
100
106
  hotUpdate: {
101
107
  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)))) {
108
+ async handler({ file: e, modules: n, read: r, type: i, server: a }) {
109
+ if (this.environment.name !== "ssr" || !l) return;
110
+ let o = t(a.config.root, e).replaceAll("\\", "/"), s = /\.[cm]?[jt]sx?$|\.mdx?$/.test(o) && !o.includes("node_modules/"), c = o.startsWith("../") && i !== "update";
111
+ if (!(!n.length && !(s && (c || o.startsWith("src/") && !o.startsWith("src/client/"))))) {
106
112
  i !== "delete" && await r();
107
113
  try {
108
- await c();
114
+ await l();
109
115
  } catch (e) {
110
116
  a.config.logger.error(`[cossack node] reload failed: ${e}`);
111
117
  }
@@ -114,9 +120,9 @@ function a(a = {}) {
114
120
  }
115
121
  },
116
122
  async closeBundle() {
117
- await s?.();
123
+ await c?.();
118
124
  }
119
125
  };
120
126
  }
121
127
  //#endregion
122
- export { a as cossackNode };
128
+ export { s as cossackNode };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cossackframework/node-adapter",
3
- "version": "0.9.0",
3
+ "version": "0.9.2",
4
4
  "type": "module",
5
5
  "description": "Node.js runtime adapter for the Cossack Framework",
6
6
  "license": "MIT",
@@ -38,22 +38,22 @@
38
38
  "access": "public"
39
39
  },
40
40
  "dependencies": {
41
- "nodemailer": "^10.0.6",
41
+ "@hono/node-server": "^2.1.6",
42
+ "nodemailer": "^10.4.0",
42
43
  "ws": ">=8.21.0",
43
- "@hono/node-server": "^2.1.0",
44
- "@cossackframework/core": "0.9.0"
44
+ "@cossackframework/core": "0.9.2"
45
45
  },
46
46
  "devDependencies": {
47
47
  "@types/nodemailer": "^8.0.1",
48
48
  "@types/ws": "^8.18.0",
49
- "hono": "^4.13.7",
49
+ "hono": "^4.13.18",
50
50
  "typescript": "^7.0.2",
51
- "vite": "^8.1.4",
52
- "vitest": "^4.1.11"
51
+ "vite": "^8.1.6",
52
+ "vitest": "^4.1.12"
53
53
  },
54
54
  "peerDependencies": {
55
- "hono": "^4.13.7",
56
- "vite": "^8.1.4"
55
+ "hono": "^4.13.18",
56
+ "vite": "^8.1.6"
57
57
  },
58
58
  "peerDependenciesMeta": {
59
59
  "vite": {
package/dist/runtime.d.ts DELETED
@@ -1,7 +0,0 @@
1
- import { WebSocket } from 'ws';
2
- import { InMemoryWebSocketRuntime, type Cossack } from '@cossackframework/core';
3
- import type { Context } from 'hono';
4
- export declare class NodeWebSocketRuntime extends InMemoryWebSocketRuntime<WebSocket> {
5
- constructor(component: Cossack);
6
- addClient(ws: WebSocket, user?: unknown, context?: Context, authorize?: (client: WebSocket, operation: (context: Context) => Promise<void>) => Promise<boolean>): void;
7
- }