@mauroandre/velojs 0.0.59 → 0.0.61

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,67 +1,67 @@
1
1
  import { jsx as y } from "preact/jsx-runtime";
2
- import { Hono as I } from "hono";
3
- import { logger as O } from "hono/logger";
4
- import { trimTrailingSlash as H } from "hono/trailing-slash";
2
+ import { Hono as O } from "hono";
3
+ import { logger as H } from "hono/logger";
4
+ import { trimTrailingSlash as L } from "hono/trailing-slash";
5
5
  import { render as V } from "preact-render-to-string";
6
6
  import { Router as x } from "wouter-preact";
7
7
  import { AsyncLocalStorage as G } from "node:async_hooks";
8
8
  import { getAppContext as _ } from "./app-context.js";
9
- import { flushPendingStreamRoutes as L, registerStreamHandler as B } from "./events.js";
10
- import { registerSocketRoutes as M, injectWebSocketServer as E } from "./sockets.js";
11
- import { inspectChannels as W, registerChannelRoute as K } from "./channels.js";
12
- import { emit as Se, registerChannels as Pe } from "./channels.js";
13
- import { trace as P, jsonBytes as C, responseBytes as R, byteLength as F } from "./telemetry.js";
14
- import { initNodeTelemetry as U } from "./telemetry-node.js";
15
- const j = new G();
16
- globalThis.__veloServerData = j;
9
+ import { flushPendingStreamRoutes as M, registerStreamHandler as B } from "./events.js";
10
+ import { registerSocketRoutes as W, injectWebSocketServer as j } from "./sockets.js";
11
+ import { inspectChannels as K, registerChannelRoute as F, registerLiveActionRoute as U } from "./channels.js";
12
+ import { emit as Re, registerChannels as je } from "./channels.js";
13
+ import { trace as P, jsonBytes as R, responseBytes as A, byteLength as J } from "./telemetry.js";
14
+ import { initNodeTelemetry as z } from "./telemetry-node.js";
15
+ const C = new G();
16
+ globalThis.__veloServerData = C;
17
17
  let w = null;
18
- function ve(e) {
18
+ function ye(e) {
19
19
  if (w) {
20
20
  e(w);
21
21
  return;
22
22
  }
23
23
  _().serverCallbacks.push(e);
24
24
  }
25
- function k(e) {
25
+ function E(e) {
26
26
  w = e;
27
27
  const t = _();
28
28
  for (const i of t.serverCallbacks) i(e);
29
29
  t.serverCallbacks.length = 0;
30
30
  }
31
- function J(e) {
31
+ function Q(e) {
32
32
  w === e && (w = null);
33
33
  }
34
- function we(e) {
34
+ function _e(e) {
35
35
  _().pendingRoutes.push(e);
36
36
  }
37
- const z = (e) => JSON.stringify(e).replace(/</g, "\\u003c").replace(/>/g, "\\u003e").replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029"), Q = (e, t, i, s, o) => {
38
- if (s && s !== 200 && e.status(s), e.req.query("_data") === "1") {
37
+ const X = (e) => JSON.stringify(e).replace(/</g, "\\u003c").replace(/>/g, "\\u003e").replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029"), Y = (e, t, i, n, o) => {
38
+ if (n && n !== 200 && e.status(n), e.req.query("_data") === "1") {
39
39
  const r = globalThis.__veloBuildHash || void 0, a = i ? { ...i, __buildHash: r } : { __buildHash: r };
40
- return { response: e.json(a), bytes: o.enabled ? C(a) : void 0 };
40
+ return { response: e.json(a), bytes: o.enabled ? R(a) : void 0 };
41
41
  }
42
- const n = o.span("render");
42
+ const s = o.span("render");
43
43
  try {
44
44
  const r = e.req.path;
45
- let a = j.run(
45
+ let a = C.run(
46
46
  i ?? {},
47
47
  () => V(/* @__PURE__ */ y(x, { ssrPath: r, children: t }))
48
48
  );
49
49
  if (e.header("Cache-Control", "no-cache"), i) {
50
- const l = `<script>window.__PAGE_DATA__=${z(i)}<\/script>`;
50
+ const l = `<script>window.__PAGE_DATA__=${X(i)}<\/script>`;
51
51
  a = a.replace("</head>", () => `${l}</head>`);
52
52
  }
53
- return n.end("ok"), { response: e.html(a), bytes: o.enabled ? F(a) : void 0 };
53
+ return s.end("ok"), { response: e.html(a), bytes: o.enabled ? J(a) : void 0 };
54
54
  } catch (r) {
55
- throw n.end("error"), r;
55
+ throw s.end("error"), r;
56
56
  }
57
- }, X = async (e, t, i) => {
58
- const s = t.req.param(), o = t.req.query(), n = { params: s, query: o, c: t }, r = await Promise.all(
57
+ }, Z = async (e, t, i) => {
58
+ const n = t.req.param(), o = t.req.query(), s = { params: n, query: o, c: t }, r = await Promise.all(
59
59
  e.map(async (c) => {
60
60
  if (!c.loader) return null;
61
- const h = c.metadata?.moduleId, u = i.span(`loader:${h ?? "anonymous"}`);
61
+ const m = c.metadata?.moduleId, u = i.span(`loader:${m ?? "anonymous"}`);
62
62
  try {
63
- const d = await c.loader(n);
64
- return u.end("ok"), h ? { moduleId: h, loaderData: d } : null;
63
+ const d = await c.loader(s);
64
+ return u.end("ok"), m ? { moduleId: m, loaderData: d } : null;
65
65
  } catch (d) {
66
66
  throw u.end("error"), d;
67
67
  }
@@ -74,7 +74,7 @@ const z = (e) => JSON.stringify(e).replace(/</g, "\\u003c").replace(/>/g, "\\u00
74
74
  break;
75
75
  }
76
76
  const l = {
77
- __params: s,
77
+ __params: n,
78
78
  __query: o,
79
79
  __pathname: t.req.path
80
80
  };
@@ -85,7 +85,7 @@ const z = (e) => JSON.stringify(e).replace(/</g, "\\u003c").replace(/>/g, "\\u00
85
85
  data: l,
86
86
  shortCircuit: a
87
87
  };
88
- }, Y = (e) => {
88
+ }, ee = (e) => {
89
89
  if (e.length === 0) return null;
90
90
  const t = e.filter(Boolean);
91
91
  if (t.length === 0) return null;
@@ -94,27 +94,27 @@ const z = (e) => JSON.stringify(e).replace(/</g, "\\u003c").replace(/>/g, "\\u00
94
94
  return /* @__PURE__ */ y(o, {});
95
95
  }
96
96
  const i = t[t.length - 1];
97
- return t.slice(0, -1).reduceRight((o, n) => /* @__PURE__ */ y(n, { children: o }), /* @__PURE__ */ y(i, {}));
98
- }, S = (e, t, i = [], s = []) => {
97
+ return t.slice(0, -1).reduceRight((o, s) => /* @__PURE__ */ y(s, { children: o }), /* @__PURE__ */ y(i, {}));
98
+ }, S = (e, t, i = [], n = []) => {
99
99
  for (const o of t) {
100
100
  if (o.handler && !o.module) {
101
101
  o.children && S(
102
102
  e,
103
103
  o.children,
104
104
  i,
105
- [...s, ...o.middlewares || []]
105
+ [...n, ...o.middlewares || []]
106
106
  );
107
107
  continue;
108
108
  }
109
- const n = o.module ? [...i, o.module] : i, r = [
110
- ...s,
109
+ const s = o.module ? [...i, o.module] : i, r = [
110
+ ...n,
111
111
  ...o.middlewares || []
112
112
  ];
113
113
  if (o.children)
114
114
  S(
115
115
  e,
116
116
  o.children,
117
- n,
117
+ s,
118
118
  r
119
119
  );
120
120
  else if (o.module) {
@@ -125,100 +125,100 @@ const z = (e) => JSON.stringify(e).replace(/</g, "\\u003c").replace(/>/g, "\\u00
125
125
  );
126
126
  continue;
127
127
  }
128
- const h = async (u) => {
129
- const d = u.req.query("_data") === "1", f = n[n.length - 1], m = P(d ? "data" : "page", {
130
- route: c ?? f?.metadata?.fullPath,
131
- module: f?.metadata?.moduleId
128
+ const m = async (u) => {
129
+ const d = u.req.query("_data") === "1", h = s[s.length - 1], f = P(d ? "data" : "page", {
130
+ route: c ?? h?.metadata?.fullPath,
131
+ module: h?.metadata?.moduleId
132
132
  });
133
133
  try {
134
- const { components: v, data: p, shortCircuit: g } = await X(n, u, m);
134
+ const { components: v, data: p, shortCircuit: g } = await Z(s, u, f);
135
135
  if (g) {
136
136
  const b = g.headers.get("Location");
137
- return d && b && g.status >= 300 && g.status < 400 ? (m.end({
137
+ return d && b && g.status >= 300 && g.status < 400 ? (f.end({
138
138
  status: "ok",
139
- bytes: m.enabled ? C({ __redirect: b }) : void 0
140
- }), u.json({ __redirect: b })) : (m.end({
139
+ bytes: f.enabled ? R({ __redirect: b }) : void 0
140
+ }), u.json({ __redirect: b })) : (f.end({
141
141
  status: "ok",
142
- bytes: m.enabled ? await R(g) : void 0
142
+ bytes: f.enabled ? await A(g) : void 0
143
143
  }), g);
144
144
  }
145
- const $ = Y(v), { response: D, bytes: N } = Q(u, $, p, l, m);
146
- return m.end({ status: "ok", bytes: N }), D;
145
+ const I = ee(v), { response: N, bytes: D } = Y(u, I, p, l, f);
146
+ return f.end({ status: "ok", bytes: D }), N;
147
147
  } catch (v) {
148
- throw m.end({ status: "error" }), v;
148
+ throw f.end({ status: "error" }), v;
149
149
  }
150
150
  };
151
- a ? e.notFound(h) : r.length > 0 ? e.on(["GET"], [c], ...r, h) : e.on(["GET"], [c], h);
151
+ a ? e.notFound(m) : r.length > 0 ? e.on(["GET"], [c], ...r, m) : e.on(["GET"], [c], m);
152
152
  } else
153
153
  continue;
154
154
  }
155
- }, T = (e, t, i = []) => {
156
- for (const s of t) {
157
- const o = s.module?.metadata?.moduleId, n = [
155
+ }, k = (e, t, i = []) => {
156
+ for (const n of t) {
157
+ const o = n.module?.metadata?.moduleId, s = [
158
158
  ...i,
159
- ...s.middlewares || []
159
+ ...n.middlewares || []
160
160
  ];
161
- if (s.module && o) {
162
- const r = Object.keys(s.module).filter(
161
+ if (n.module && o) {
162
+ const r = Object.keys(n.module).filter(
163
163
  (a) => a.startsWith("action_")
164
164
  );
165
165
  for (const a of r) {
166
- const l = a.replace("action_", ""), c = s.module[a];
166
+ const l = a.replace("action_", ""), c = n.module[a];
167
167
  if (typeof c == "function") {
168
- const h = `/_action/${o}/${l}`, u = async (d) => {
169
- const f = P("action", {
170
- route: s.module?.metadata?.fullPath,
168
+ const m = `/_action/${o}/${l}`, u = async (d) => {
169
+ const h = P("action", {
170
+ route: n.module?.metadata?.fullPath,
171
171
  module: o,
172
172
  name: l
173
173
  });
174
- let m = {};
174
+ let f = {};
175
175
  try {
176
- m = await d.req.json();
176
+ f = await d.req.json();
177
177
  } catch {
178
178
  }
179
179
  const v = {
180
- body: m,
180
+ body: f,
181
181
  params: d.req.param(),
182
182
  query: d.req.query(),
183
183
  c: d
184
184
  };
185
185
  try {
186
186
  const g = await c(v) ?? { ok: !0 };
187
- return f.end({ status: "ok", bytes: f.enabled ? C(g) : void 0 }), d.json(g);
187
+ return h.end({ status: "ok", bytes: h.enabled ? R(g) : void 0 }), d.json(g);
188
188
  } catch (p) {
189
189
  const g = p instanceof Error ? p.message : "Action failed";
190
- return f.end({ status: "error" }), d.json({ error: g }, 500);
190
+ return h.end({ status: "error" }), d.json({ error: g }, 500);
191
191
  }
192
192
  };
193
- n.length > 0 ? e.on(
193
+ s.length > 0 ? e.on(
194
194
  ["POST"],
195
- [h],
196
- ...n,
195
+ [m],
196
+ ...s,
197
197
  u
198
- ) : e.on(["POST"], [h], u);
198
+ ) : e.on(["POST"], [m], u);
199
199
  }
200
200
  }
201
201
  }
202
- s.children && T(e, s.children, n);
202
+ n.children && k(e, n.children, s);
203
203
  }
204
- }, Z = async (e, t, i = []) => {
205
- const s = (o, n) => {
204
+ }, te = async (e, t, i = []) => {
205
+ const n = (o, s) => {
206
206
  for (const r of o) {
207
207
  const a = r.module?.metadata?.moduleId, l = [
208
- ...n,
208
+ ...s,
209
209
  ...r.middlewares || []
210
210
  ];
211
211
  if (r.module && a) {
212
212
  const c = Object.keys(r.module).filter(
213
- (h) => h.startsWith("stream_")
213
+ (m) => m.startsWith("stream_")
214
214
  );
215
- for (const h of c) {
216
- const u = h.replace("stream_", ""), d = r.module[h];
215
+ for (const m of c) {
216
+ const u = m.replace("stream_", ""), d = r.module[m];
217
217
  if (d?.__isVeloEventStream) {
218
- const f = `/_event/${a}/${u}`;
219
- d.__path = f, B(
218
+ const h = `/_event/${a}/${u}`;
219
+ d.__path = h, B(
220
220
  e,
221
- f,
221
+ h,
222
222
  d,
223
223
  l,
224
224
  { moduleId: a, name: u }
@@ -226,108 +226,130 @@ const z = (e) => JSON.stringify(e).replace(/</g, "\\u003c").replace(/>/g, "\\u00
226
226
  }
227
227
  }
228
228
  }
229
- r.children && s(r.children, l);
229
+ r.children && n(r.children, l);
230
230
  }
231
231
  };
232
- s(t, i);
233
- }, q = (e, t, i = []) => {
234
- for (const s of t) {
235
- const o = s.module?.metadata?.moduleId, n = [
232
+ n(t, i);
233
+ }, T = (e, t, i = []) => {
234
+ for (const n of t) {
235
+ const o = n.module?.metadata?.moduleId, s = [
236
236
  ...i,
237
- ...s.middlewares || []
237
+ ...n.middlewares || []
238
238
  ];
239
- if (s.module && o) {
240
- const r = s.module.channels;
239
+ if (n.module && o) {
240
+ const r = n.module.channels;
241
241
  if (Array.isArray(r))
242
242
  for (const a of r)
243
- K(
243
+ F(
244
244
  e,
245
245
  `/_channel/${o}/${a}`,
246
246
  a,
247
- s.module,
247
+ n.module,
248
+ o,
249
+ s
250
+ );
251
+ }
252
+ n.children && T(e, n.children, s);
253
+ }
254
+ }, q = (e, t, i = []) => {
255
+ for (const n of t) {
256
+ const o = n.module?.metadata?.moduleId, s = [
257
+ ...i,
258
+ ...n.middlewares || []
259
+ ];
260
+ if (n.module && o) {
261
+ const r = n.module.liveActions;
262
+ if (r && typeof r == "object")
263
+ for (const [a, l] of Object.entries(r))
264
+ U(
265
+ e,
266
+ `/_live/${o}/${a}`,
267
+ a,
268
+ l,
269
+ n.module,
248
270
  o,
249
- n
271
+ s
250
272
  );
251
273
  }
252
- s.children && q(e, s.children, n);
274
+ n.children && q(e, n.children, s);
253
275
  }
254
- }, ee = (e, t) => t ? t.startsWith("/") ? e + t : e ? e + "/" + t : "/" + t : e || "", te = (e) => {
255
- const t = /* @__PURE__ */ new Set(), i = (s) => {
256
- for (const o of s) {
276
+ }, oe = (e, t) => t ? t.startsWith("/") ? e + t : e ? e + "/" + t : "/" + t : e || "", ne = (e) => {
277
+ const t = /* @__PURE__ */ new Set(), i = (n) => {
278
+ for (const o of n) {
257
279
  if (o.module && !o.children) {
258
- const n = o.module.metadata?.fullPath;
259
- n && t.add(n);
280
+ const s = o.module.metadata?.fullPath;
281
+ s && t.add(s);
260
282
  }
261
283
  o.children && i(o.children);
262
284
  }
263
285
  };
264
286
  return i(e), t;
265
- }, A = (e, t, i, s = "", o = []) => {
266
- for (const n of t) {
267
- const r = ee(s, n.path), a = [
287
+ }, $ = (e, t, i, n = "", o = []) => {
288
+ for (const s of t) {
289
+ const r = oe(n, s.path), a = [
268
290
  ...o,
269
- ...n.middlewares || []
270
- ], l = !!n.handler, c = !!n.method;
291
+ ...s.middlewares || []
292
+ ], l = !!s.handler, c = !!s.method;
271
293
  if (l && !c ? console.warn(
272
294
  `[velojs] invalid route at "${r || "/"}" — "handler" set without "method"; endpoint skipped`
273
295
  ) : c && !l ? console.warn(
274
296
  `[velojs] invalid route at "${r || "/"}" — "method" set without "handler"; endpoint skipped`
275
- ) : l && n.module ? console.warn(
297
+ ) : l && s.module ? console.warn(
276
298
  `[velojs] ambiguous route at "${r || "/"}" — both "module" and "handler" set; endpoint ignored, page kept`
277
- ) : l && n.isRoot && console.warn(
299
+ ) : l && s.isRoot && console.warn(
278
300
  `[velojs] "isRoot" has no effect on endpoint at "${r || "/"}"; ignored`
279
- ), l && c && !n.module) {
280
- const u = n.method;
301
+ ), l && c && !s.module) {
302
+ const u = s.method;
281
303
  u === "GET" && i.has(r) && console.warn(
282
304
  `[velojs] path "${r}" has both a page GET and an endpoint GET; the page wins and the endpoint is unreachable. Consider renaming one.`
283
305
  );
284
- const d = n.handler, f = async (m) => {
306
+ const d = s.handler, h = async (f) => {
285
307
  const v = P("endpoint", { route: r, method: u });
286
308
  try {
287
309
  const p = await d({
288
- c: m,
289
- params: m.req.param(),
290
- query: m.req.query()
310
+ c: f,
311
+ params: f.req.param(),
312
+ query: f.req.query()
291
313
  });
292
314
  return v.end({
293
315
  status: "ok",
294
- bytes: v.enabled && p instanceof Response ? await R(p) : void 0
316
+ bytes: v.enabled && p instanceof Response ? await A(p) : void 0
295
317
  }), p;
296
318
  } catch (p) {
297
319
  throw v.end({ status: "error" }), p;
298
320
  }
299
321
  };
300
- a.length > 0 ? e.on([u], [r], ...a, f) : e.on([u], [r], f);
322
+ a.length > 0 ? e.on([u], [r], ...a, h) : e.on([u], [r], h);
301
323
  }
302
- n.children && A(
324
+ s.children && $(
303
325
  e,
304
- n.children,
326
+ s.children,
305
327
  i,
306
328
  r,
307
329
  a
308
330
  );
309
331
  }
310
- }, oe = async (e) => {
332
+ }, re = async (e) => {
311
333
  if (process.env.NODE_ENV !== "production" || process.env.VELO_STATIC || (process.env.STATIC_BASE_URL || "").startsWith("http")) return;
312
- const { serveStatic: i } = await import("@hono/node-server/serve-static"), { join: s } = await import("node:path");
313
- e.use("/*", i({ root: s(process.cwd(), "dist/client") }));
314
- }, ne = async (e) => {
315
- const t = new I();
316
- await U(), t.use(H()), process.env.NODE_ENV !== "production" && t.use("*", O()), await oe(t);
317
- const s = _().pendingRoutes.splice(0);
318
- for (const n of s)
319
- await n(t);
320
- S(t, e), T(t, e), await Z(t, e), q(t, e), process.env.NODE_ENV !== "production" && t.get("/_channel-inspect", (n) => n.json(W()));
321
- const o = te(e);
322
- if (A(t, e, o), await M(t, e), L(t), process.env.NODE_ENV !== "production" && !w) {
323
- const n = globalThis.__veloDevServer;
324
- n && (k(n), await E(t, n));
334
+ const { serveStatic: i } = await import("@hono/node-server/serve-static"), { join: n } = await import("node:path");
335
+ e.use("/*", i({ root: n(process.cwd(), "dist/client") }));
336
+ }, se = async (e) => {
337
+ const t = new O();
338
+ await z(), t.use(L()), process.env.NODE_ENV !== "production" && t.use("*", H()), await re(t);
339
+ const n = _().pendingRoutes.splice(0);
340
+ for (const s of n)
341
+ await s(t);
342
+ S(t, e), k(t, e), await te(t, e), T(t, e), q(t, e), process.env.NODE_ENV !== "production" && t.get("/_channel-inspect", (s) => s.json(K()));
343
+ const o = ne(e);
344
+ if ($(t, e, o), await W(t, e), M(t), process.env.NODE_ENV !== "production" && !w) {
345
+ const s = globalThis.__veloDevServer;
346
+ s && (E(s), await j(t, s));
325
347
  }
326
348
  return t;
327
- }, ye = async (e) => {
328
- const { routes: t } = e, i = Number(process.env.PORT) || e.port || 3e3, s = process.env.HOST || e.hostname || void 0, o = await ne(t);
349
+ }, be = async (e) => {
350
+ const { routes: t } = e, i = Number(process.env.PORT) || e.port || 3e3, n = process.env.HOST || e.hostname || void 0, o = await se(t);
329
351
  if (process.env.NODE_ENV === "production" && !process.env.VELO_STATIC) {
330
- const { serve: n } = await import("@hono/node-server"), r = n({ fetch: o.fetch, port: i, ...s ? { hostname: s } : {} });
352
+ const { serve: s } = await import("@hono/node-server"), r = s({ fetch: o.fetch, port: i, ...n ? { hostname: n } : {} });
331
353
  r.listening || await new Promise((l) => r.once("listening", l));
332
354
  const a = r.address();
333
355
  if (a && typeof a == "object") {
@@ -335,23 +357,23 @@ const z = (e) => JSON.stringify(e).replace(/</g, "\\u003c").replace(/>/g, "\\u00
335
357
  console.log(`Server running on ${c}:${a.port}`);
336
358
  } else
337
359
  console.log(`Server running on port ${i}`);
338
- await E(o, r), k(r), r.once("close", () => {
339
- J(r);
360
+ await j(o, r), E(r), r.once("close", () => {
361
+ Q(r);
340
362
  });
341
363
  }
342
364
  return o;
343
365
  };
344
366
  export {
345
- we as addRoutes,
346
- J as clearActiveServer,
347
- ne as createApp,
348
- ne as default,
349
- Se as emit,
350
- k as flushServerCallbacks,
351
- W as inspectChannels,
352
- z as jsonForScript,
353
- ve as onServer,
354
- Pe as registerChannels,
355
- j as serverDataStorage,
356
- ye as startServer
367
+ _e as addRoutes,
368
+ Q as clearActiveServer,
369
+ se as createApp,
370
+ se as default,
371
+ Re as emit,
372
+ E as flushServerCallbacks,
373
+ K as inspectChannels,
374
+ X as jsonForScript,
375
+ ye as onServer,
376
+ je as registerChannels,
377
+ C as serverDataStorage,
378
+ be as startServer
357
379
  };
@@ -41,6 +41,7 @@ export interface TelemetryReport {
41
41
  routes: RankRow[];
42
42
  actions: RankRow[];
43
43
  channels: RankRow[];
44
+ liveActions: RankRow[];
44
45
  timeline: TimelineEntry[];
45
46
  }
46
47
  /** `--last` accepts `ms`, `s`, `m`, `h`, `d`; a bare number counts seconds. */