@mauroandre/velojs 0.0.50 → 0.0.52

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/dist/server.js CHANGED
@@ -1,94 +1,94 @@
1
- import { jsx as _ } from "preact/jsx-runtime";
2
- import { Hono as j } from "hono";
3
- import { logger as E } from "hono/logger";
4
- import { trimTrailingSlash as T } from "hono/trailing-slash";
1
+ import { jsx as v } from "preact/jsx-runtime";
2
+ import { Hono as R } from "hono";
3
+ import { logger as T } from "hono/logger";
4
+ import { trimTrailingSlash as E } from "hono/trailing-slash";
5
5
  import { render as C } from "preact-render-to-string";
6
- import { Router as k } from "wouter-preact";
7
- import { AsyncLocalStorage as A } from "node:async_hooks";
8
- import { getAppContext as w } from "./app-context.js";
9
- import { flushPendingStreamRoutes as D, registerStreamHandler as $ } from "./events.js";
10
- import { registerSocketRoutes as I, injectWebSocketServer as S } from "./sockets.js";
11
- const P = new A();
6
+ import { Router as $ } from "wouter-preact";
7
+ import { AsyncLocalStorage as k } from "node:async_hooks";
8
+ import { getAppContext as _ } from "./app-context.js";
9
+ import { flushPendingStreamRoutes as A, registerStreamHandler as D } from "./events.js";
10
+ import { registerSocketRoutes as O, injectWebSocketServer as S } from "./sockets.js";
11
+ const P = new k();
12
12
  globalThis.__veloServerData = P;
13
- let v = null;
13
+ let g = null;
14
14
  function Z(e) {
15
- if (v) {
16
- e(v);
15
+ if (g) {
16
+ e(g);
17
17
  return;
18
18
  }
19
- w().serverCallbacks.push(e);
19
+ _().serverCallbacks.push(e);
20
20
  }
21
21
  function b(e) {
22
- v = e;
23
- const t = w();
22
+ g = e;
23
+ const t = _();
24
24
  for (const s of t.serverCallbacks) s(e);
25
25
  t.serverCallbacks.length = 0;
26
26
  }
27
27
  function ee(e) {
28
- w().pendingRoutes.push(e);
28
+ _().pendingRoutes.push(e);
29
29
  }
30
- const N = (e) => JSON.stringify(e).replace(/</g, "\\u003c").replace(/>/g, "\\u003e").replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029"), O = (e, t, s, r) => {
31
- if (r && r !== 200 && e.status(r), e.req.query("_data") === "1") {
32
- const c = globalThis.__veloBuildHash || void 0, i = s ? { ...s, __buildHash: c } : { __buildHash: c };
33
- return e.json(i);
30
+ const I = (e) => JSON.stringify(e).replace(/</g, "\\u003c").replace(/>/g, "\\u003e").replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029"), N = (e, t, s, a) => {
31
+ if (a && a !== 200 && e.status(a), e.req.query("_data") === "1") {
32
+ const c = globalThis.__veloBuildHash || void 0, r = s ? { ...s, __buildHash: c } : { __buildHash: c };
33
+ return e.json(r);
34
34
  }
35
35
  const o = e.req.path, n = P.run(
36
36
  s ?? {},
37
- () => C(/* @__PURE__ */ _(k, { ssrPath: o, children: t }))
37
+ () => C(/* @__PURE__ */ v($, { ssrPath: o, children: t }))
38
38
  );
39
39
  if (e.header("Cache-Control", "no-cache"), !s)
40
40
  return e.html(n);
41
- const a = `<script>window.__PAGE_DATA__=${N(s)}<\/script>`;
42
- return e.html(n.replace("</head>", () => `${a}</head>`));
43
- }, G = async (e, t) => {
44
- const s = t.req.param(), r = t.req.query(), o = { params: s, query: r, c: t }, n = await Promise.all(
45
- e.map(async (i) => {
46
- if (!i.loader) return null;
47
- const l = await i.loader(o), d = i.metadata?.moduleId;
48
- return d ? { moduleId: d, loaderData: l } : null;
41
+ const i = `<script>window.__PAGE_DATA__=${I(s)}<\/script>`;
42
+ return e.html(n.replace("</head>", () => `${i}</head>`));
43
+ }, H = async (e, t) => {
44
+ const s = t.req.param(), a = t.req.query(), o = { params: s, query: a, c: t }, n = await Promise.all(
45
+ e.map(async (r) => {
46
+ if (!r.loader) return null;
47
+ const f = await r.loader(o), l = r.metadata?.moduleId;
48
+ return l ? { moduleId: l, loaderData: f } : null;
49
49
  })
50
50
  );
51
- let a;
52
- for (const i of n)
53
- if (i && i.loaderData instanceof Response) {
54
- a = i.loaderData;
51
+ let i;
52
+ for (const r of n)
53
+ if (r && r.loaderData instanceof Response) {
54
+ i = r.loaderData;
55
55
  break;
56
56
  }
57
57
  const c = {
58
58
  __params: s,
59
- __query: r,
59
+ __query: a,
60
60
  __pathname: t.req.path
61
61
  };
62
- for (const i of n)
63
- i && !(i.loaderData instanceof Response) && (c[i.moduleId] = i.loaderData);
62
+ for (const r of n)
63
+ r && !(r.loaderData instanceof Response) && (c[r.moduleId] = r.loaderData);
64
64
  return {
65
- components: e.map((i) => i.Component),
65
+ components: e.map((r) => r.Component),
66
66
  data: c,
67
- shortCircuit: a
67
+ shortCircuit: i
68
68
  };
69
- }, H = (e) => {
69
+ }, G = (e) => {
70
70
  if (e.length === 0) return null;
71
71
  const t = e.filter(Boolean);
72
72
  if (t.length === 0) return null;
73
73
  if (t.length === 1) {
74
74
  const o = t[0];
75
- return /* @__PURE__ */ _(o, {});
75
+ return /* @__PURE__ */ v(o, {});
76
76
  }
77
77
  const s = t[t.length - 1];
78
- return t.slice(0, -1).reduceRight((o, n) => /* @__PURE__ */ _(n, { children: o }), /* @__PURE__ */ _(s, {}));
79
- }, y = (e, t, s = [], r = []) => {
78
+ return t.slice(0, -1).reduceRight((o, n) => /* @__PURE__ */ v(n, { children: o }), /* @__PURE__ */ v(s, {}));
79
+ }, y = (e, t, s = [], a = []) => {
80
80
  for (const o of t) {
81
81
  if (o.handler && !o.module) {
82
82
  o.children && y(
83
83
  e,
84
84
  o.children,
85
85
  s,
86
- [...r, ...o.middlewares || []]
86
+ [...a, ...o.middlewares || []]
87
87
  );
88
88
  continue;
89
89
  }
90
- const n = o.module ? [...s, o.module] : s, a = [
91
- ...r,
90
+ const n = o.module ? [...s, o.module] : s, i = [
91
+ ...a,
92
92
  ...o.middlewares || []
93
93
  ];
94
94
  if (o.children)
@@ -96,104 +96,104 @@ const N = (e) => JSON.stringify(e).replace(/</g, "\\u003c").replace(/>/g, "\\u00
96
96
  e,
97
97
  o.children,
98
98
  n,
99
- a
99
+ i
100
100
  );
101
101
  else if (o.module) {
102
- const c = o.path === "*", i = o.statusCode ?? (c ? 404 : void 0), l = o.module.metadata?.fullPath;
103
- if (!l && !c) {
102
+ const c = o.path === "*", r = o.statusCode ?? (c ? 404 : void 0), f = o.module.metadata?.fullPath;
103
+ if (!f && !c) {
104
104
  console.warn(
105
105
  `Module ${o.module.metadata?.moduleId} has no fullPath`
106
106
  );
107
107
  continue;
108
108
  }
109
- const d = async (h) => {
110
- const { components: u, data: m, shortCircuit: f } = await G(n, h);
111
- if (f) {
112
- const g = f.headers.get("Location");
113
- return h.req.query("_data") === "1" && g && f.status >= 300 && f.status < 400 ? h.json({ __redirect: g }) : f;
109
+ const l = async (d) => {
110
+ const { components: u, data: h, shortCircuit: m } = await H(n, d);
111
+ if (m) {
112
+ const w = m.headers.get("Location");
113
+ return d.req.query("_data") === "1" && w && m.status >= 300 && m.status < 400 ? d.json({ __redirect: w }) : m;
114
114
  }
115
- const p = H(u);
116
- return O(h, p, m, i);
115
+ const p = G(u);
116
+ return N(d, p, h, r);
117
117
  };
118
- c ? e.notFound(d) : a.length > 0 ? e.on(["GET"], [l], ...a, d) : e.on(["GET"], [l], d);
118
+ c ? e.notFound(l) : i.length > 0 ? e.on(["GET"], [f], ...i, l) : e.on(["GET"], [f], l);
119
119
  } else
120
120
  continue;
121
121
  }
122
122
  }, q = (e, t, s = []) => {
123
- for (const r of t) {
124
- const o = r.module?.metadata?.moduleId, n = [
123
+ for (const a of t) {
124
+ const o = a.module?.metadata?.moduleId, n = [
125
125
  ...s,
126
- ...r.middlewares || []
126
+ ...a.middlewares || []
127
127
  ];
128
- if (r.module && o) {
129
- const a = Object.keys(r.module).filter(
128
+ if (a.module && o) {
129
+ const i = Object.keys(a.module).filter(
130
130
  (c) => c.startsWith("action_")
131
131
  );
132
- for (const c of a) {
133
- const i = c.replace("action_", ""), l = r.module[c];
134
- if (typeof l == "function") {
135
- const d = `/_action/${o}/${i}`, h = async (u) => {
136
- let m = {};
132
+ for (const c of i) {
133
+ const r = c.replace("action_", ""), f = a.module[c];
134
+ if (typeof f == "function") {
135
+ const l = `/_action/${o}/${r}`, d = async (u) => {
136
+ let h = {};
137
137
  try {
138
- m = await u.req.json();
138
+ h = await u.req.json();
139
139
  } catch {
140
140
  }
141
- const f = {
142
- body: m,
141
+ const m = {
142
+ body: h,
143
143
  params: u.req.param(),
144
144
  query: u.req.query(),
145
145
  c: u
146
146
  };
147
147
  try {
148
- const p = await l(f);
148
+ const p = await f(m);
149
149
  return u.json(p ?? { ok: !0 });
150
150
  } catch (p) {
151
- const g = p instanceof Error ? p.message : "Action failed";
152
- return u.json({ error: g }, 500);
151
+ const w = p instanceof Error ? p.message : "Action failed";
152
+ return u.json({ error: w }, 500);
153
153
  }
154
154
  };
155
155
  n.length > 0 ? e.on(
156
156
  ["POST"],
157
- [d],
157
+ [l],
158
158
  ...n,
159
- h
160
- ) : e.on(["POST"], [d], h);
159
+ d
160
+ ) : e.on(["POST"], [l], d);
161
161
  }
162
162
  }
163
163
  }
164
- r.children && q(e, r.children, n);
164
+ a.children && q(e, a.children, n);
165
165
  }
166
166
  }, x = async (e, t, s = []) => {
167
- const r = (o, n) => {
168
- for (const a of o) {
169
- const c = a.module?.metadata?.moduleId, i = [
167
+ const a = (o, n) => {
168
+ for (const i of o) {
169
+ const c = i.module?.metadata?.moduleId, r = [
170
170
  ...n,
171
- ...a.middlewares || []
171
+ ...i.middlewares || []
172
172
  ];
173
- if (a.module && c) {
174
- const l = Object.keys(a.module).filter(
175
- (d) => d.startsWith("stream_")
173
+ if (i.module && c) {
174
+ const f = Object.keys(i.module).filter(
175
+ (l) => l.startsWith("stream_")
176
176
  );
177
- for (const d of l) {
178
- const h = d.replace("stream_", ""), u = a.module[d];
177
+ for (const l of f) {
178
+ const d = l.replace("stream_", ""), u = i.module[l];
179
179
  if (u?.__isVeloEventStream) {
180
- const m = `/_event/${c}/${h}`;
181
- u.__path = m, $(
180
+ const h = `/_event/${c}/${d}`;
181
+ u.__path = h, D(
182
182
  e,
183
- m,
183
+ h,
184
184
  u,
185
- i
185
+ r
186
186
  );
187
187
  }
188
188
  }
189
189
  }
190
- a.children && r(a.children, i);
190
+ i.children && a(i.children, r);
191
191
  }
192
192
  };
193
- r(t, s);
193
+ a(t, s);
194
194
  }, V = (e, t) => t ? t.startsWith("/") ? e + t : e ? e + "/" + t : "/" + t : e || "", W = (e) => {
195
- const t = /* @__PURE__ */ new Set(), s = (r) => {
196
- for (const o of r) {
195
+ const t = /* @__PURE__ */ new Set(), s = (a) => {
196
+ for (const o of a) {
197
197
  if (o.module && !o.children) {
198
198
  const n = o.module.metadata?.fullPath;
199
199
  n && t.add(n);
@@ -202,68 +202,77 @@ const N = (e) => JSON.stringify(e).replace(/</g, "\\u003c").replace(/>/g, "\\u00
202
202
  }
203
203
  };
204
204
  return s(e), t;
205
- }, R = (e, t, s, r = "", o = []) => {
205
+ }, j = (e, t, s, a = "", o = []) => {
206
206
  for (const n of t) {
207
- const a = V(r, n.path), c = [
207
+ const i = V(a, n.path), c = [
208
208
  ...o,
209
209
  ...n.middlewares || []
210
- ], i = !!n.handler, l = !!n.method;
211
- if (i && !l ? console.warn(
212
- `[velojs] invalid route at "${a || "/"}" — "handler" set without "method"; endpoint skipped`
213
- ) : l && !i ? console.warn(
214
- `[velojs] invalid route at "${a || "/"}" — "method" set without "handler"; endpoint skipped`
215
- ) : i && n.module ? console.warn(
216
- `[velojs] ambiguous route at "${a || "/"}" — both "module" and "handler" set; endpoint ignored, page kept`
217
- ) : i && n.isRoot && console.warn(
218
- `[velojs] "isRoot" has no effect on endpoint at "${a || "/"}"; ignored`
219
- ), i && l && !n.module) {
220
- const h = n.method;
221
- h === "GET" && s.has(a) && console.warn(
222
- `[velojs] path "${a}" has both a page GET and an endpoint GET; the page wins and the endpoint is unreachable. Consider renaming one.`
210
+ ], r = !!n.handler, f = !!n.method;
211
+ if (r && !f ? console.warn(
212
+ `[velojs] invalid route at "${i || "/"}" — "handler" set without "method"; endpoint skipped`
213
+ ) : f && !r ? console.warn(
214
+ `[velojs] invalid route at "${i || "/"}" — "method" set without "handler"; endpoint skipped`
215
+ ) : r && n.module ? console.warn(
216
+ `[velojs] ambiguous route at "${i || "/"}" — both "module" and "handler" set; endpoint ignored, page kept`
217
+ ) : r && n.isRoot && console.warn(
218
+ `[velojs] "isRoot" has no effect on endpoint at "${i || "/"}"; ignored`
219
+ ), r && f && !n.module) {
220
+ const d = n.method;
221
+ d === "GET" && s.has(i) && console.warn(
222
+ `[velojs] path "${i}" has both a page GET and an endpoint GET; the page wins and the endpoint is unreachable. Consider renaming one.`
223
223
  );
224
- const u = n.handler, m = async (f) => await u({
225
- c: f,
226
- params: f.req.param(),
227
- query: f.req.query()
224
+ const u = n.handler, h = async (m) => await u({
225
+ c: m,
226
+ params: m.req.param(),
227
+ query: m.req.query()
228
228
  });
229
- c.length > 0 ? e.on([h], [a], ...c, m) : e.on([h], [a], m);
229
+ c.length > 0 ? e.on([d], [i], ...c, h) : e.on([d], [i], h);
230
230
  }
231
- n.children && R(
231
+ n.children && j(
232
232
  e,
233
233
  n.children,
234
234
  s,
235
- a,
235
+ i,
236
236
  c
237
237
  );
238
238
  }
239
239
  }, K = async (e) => {
240
- const t = new j();
241
- t.use(T()), process.env.NODE_ENV !== "production" && t.use("*", E());
242
- const r = w().pendingRoutes.splice(0);
243
- for (const n of r)
240
+ const t = new R();
241
+ t.use(E()), process.env.NODE_ENV !== "production" && t.use("*", T());
242
+ const a = _().pendingRoutes.splice(0);
243
+ for (const n of a)
244
244
  await n(t);
245
245
  y(t, e), q(t, e), await x(t, e);
246
246
  const o = W(e);
247
- if (R(t, e, o), await I(t, e), D(t), process.env.NODE_ENV !== "production" && !v) {
247
+ if (j(t, e, o), await O(t, e), A(t), process.env.NODE_ENV !== "production" && !g) {
248
248
  const n = globalThis.__veloDevServer;
249
249
  n && (b(n), await S(t, n));
250
250
  }
251
251
  return t;
252
252
  }, te = async (e) => {
253
- const { routes: t } = e, s = Number(process.env.PORT) || e.port || 3e3, r = await K(t);
253
+ const { routes: t } = e, s = Number(process.env.PORT) || e.port || 3e3, a = process.env.HOST || e.hostname || void 0, o = await K(t);
254
254
  if (process.env.NODE_ENV === "production" && !process.env.VELO_STATIC) {
255
- const { serve: o } = await import("@hono/node-server"), { serveStatic: n } = await import("@hono/node-server/serve-static"), { join: a } = await import("node:path"), c = a(process.cwd(), "dist/client");
256
- (process.env.STATIC_BASE_URL || "").startsWith("http") || r.use("/*", n({ root: c })), console.log(`Server running on http://localhost:${s}`);
257
- const l = o({ fetch: r.fetch, port: s });
258
- await S(r, l), b(l);
255
+ const { serve: n } = await import("@hono/node-server"), { serveStatic: i } = await import("@hono/node-server/serve-static"), { join: c } = await import("node:path"), r = c(process.cwd(), "dist/client");
256
+ (process.env.STATIC_BASE_URL || "").startsWith("http") || o.use("/*", i({ root: r }));
257
+ const l = n({ fetch: o.fetch, port: s, ...a ? { hostname: a } : {} });
258
+ l.listening || await new Promise((u) => l.once("listening", u));
259
+ const d = l.address();
260
+ if (d && typeof d == "object") {
261
+ const h = d.address === "::" || d.address === "0.0.0.0" ? "0.0.0.0 (all interfaces)" : d.address;
262
+ console.log(`Server running on ${h}:${d.port}`);
263
+ } else
264
+ console.log(`Server running on port ${s}`);
265
+ await S(o, l), b(l), l.once("close", () => {
266
+ g === l && (g = null);
267
+ });
259
268
  }
260
- return r;
269
+ return o;
261
270
  };
262
271
  export {
263
272
  ee as addRoutes,
264
273
  K as createApp,
265
274
  K as default,
266
- N as jsonForScript,
275
+ I as jsonForScript,
267
276
  Z as onServer,
268
277
  P as serverDataStorage,
269
278
  te as startServer