@mauroandre/velojs 0.0.18 → 0.0.20

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/hooks.js CHANGED
@@ -1,61 +1,61 @@
1
- import { signal as _, useSignal as d } from "@preact/signals";
2
- import { useRef as g, useEffect as h } from "preact/hooks";
3
- import { useLocation as v, useParams as D } from "wouter-preact";
4
- const p = _(!1);
5
- function m(e) {
1
+ import { signal as v, useSignal as c } from "@preact/signals";
2
+ import { useEffect as h, useRef as p } from "preact/hooks";
3
+ import { useLocation as w, useParams as g } from "wouter-preact";
4
+ const S = v(!1);
5
+ function L(e) {
6
6
  e.value !== null && typeof e.value == "object" && (e.value = { ...e.value });
7
7
  }
8
- function P(e) {
9
- const a = _(!1);
8
+ function m(e) {
9
+ const o = v(!1);
10
10
  if (typeof window > "u")
11
11
  return { data: {
12
12
  get value() {
13
13
  return e ? globalThis.__veloServerData?.getStore?.()?.[e] ?? null : null;
14
14
  }
15
- }, loading: a };
16
- const t = window.__PAGE_DATA__, n = e && t?.[e] ? t[e] : null;
17
- return { data: _(n), loading: a };
15
+ }, loading: o };
16
+ const t = window.__PAGE_DATA__, r = e && t?.[e] ? t[e] : null;
17
+ return { data: v(r), loading: o };
18
18
  }
19
- function T(e, a) {
20
- let t, n;
21
- Array.isArray(e) ? n = e : (t = e, n = a);
22
- let r = null;
19
+ function A(e, o) {
20
+ let t, r;
21
+ Array.isArray(e) ? r = e : (t = e, r = o);
22
+ let n = null;
23
23
  if (typeof window > "u" && t) {
24
- const c = globalThis.__veloServerData?.getStore?.();
25
- c?.[t] && (r = c[t]);
24
+ const i = globalThis.__veloServerData?.getStore?.();
25
+ i?.[t] && (n = i[t]);
26
26
  }
27
27
  if (typeof window < "u" && t) {
28
- const o = window.__PAGE_DATA__;
29
- o?.[t] && (r = o[t], delete o[t]);
28
+ const a = window.__PAGE_DATA__;
29
+ a?.[t] && (n = a[t], delete a[t]);
30
30
  }
31
- const u = d(r), i = d(!1), l = () => {
31
+ const u = c(n), s = c(!1), _ = () => {
32
32
  if (typeof window > "u" || !t) return;
33
- const o = window.location.pathname;
34
- i.value = !0;
35
- const w = typeof __VELO_STATIC__ < "u" && __VELO_STATIC__ ? `${o === "/" ? "" : o}/index.json` : (() => {
36
- const s = new URLSearchParams(window.location.search);
37
- return s.set("_data", "1"), `${o}?${s.toString()}`;
33
+ const a = window.location.pathname;
34
+ s.value = !0;
35
+ const f = typeof __VELO_STATIC__ < "u" && __VELO_STATIC__ ? `${a === "/" ? "" : a}/index.json` : (() => {
36
+ const l = new URLSearchParams(window.location.search);
37
+ return l.set("_data", "1"), `${a}?${l.toString()}`;
38
38
  })();
39
- fetch(w, { cache: "no-cache" }).then((s) => s.json()).then((s) => {
40
- typeof __VELO_BUILD_HASH__ < "u" && s.__buildHash && s.__buildHash !== __VELO_BUILD_HASH__ && (p.value = !0), u.value = s[t], i.value = !1;
39
+ fetch(f, { cache: "no-cache" }).then((l) => l.json()).then((l) => {
40
+ typeof __VELO_BUILD_HASH__ < "u" && l.__buildHash && l.__buildHash !== __VELO_BUILD_HASH__ && (S.value = !0), u.value = l[t], s.value = !1;
41
41
  }).catch(() => {
42
- i.value = !1;
42
+ s.value = !1;
43
43
  });
44
- }, f = g(r !== null);
44
+ }, d = p(n !== null);
45
45
  return h(() => {
46
- if (f.current) {
47
- f.current = !1;
46
+ if (d.current) {
47
+ d.current = !1;
48
48
  return;
49
49
  }
50
- l();
51
- }, n ?? []), { data: u, loading: i, refetch: l };
50
+ _();
51
+ }, r ?? []), { data: u, loading: s, refetch: _ };
52
52
  }
53
- function L() {
54
- const [, e] = v();
53
+ function P() {
54
+ const [, e] = w();
55
55
  return e;
56
56
  }
57
- function E() {
58
- return typeof window > "u" ? globalThis.__veloServerData?.getStore?.()?.__params ?? {} : D();
57
+ function T() {
58
+ return typeof window > "u" ? globalThis.__veloServerData?.getStore?.()?.__params ?? {} : g();
59
59
  }
60
60
  function b() {
61
61
  if (typeof window > "u")
@@ -63,21 +63,54 @@ function b() {
63
63
  const e = window.__PAGE_DATA__;
64
64
  if (e?.__query)
65
65
  return e.__query;
66
- const a = new URLSearchParams(window.location.search), t = {};
67
- return a.forEach((n, r) => {
68
- t[r] = n;
66
+ const o = new URLSearchParams(window.location.search), t = {};
67
+ return o.forEach((r, n) => {
68
+ t[n] = r;
69
69
  }), t;
70
70
  }
71
71
  function H() {
72
- return typeof window > "u" ? globalThis.__veloServerData?.getStore?.()?.__pathname ?? "/" : (v(), window.location.pathname);
72
+ return typeof window > "u" ? globalThis.__veloServerData?.getStore?.()?.__pathname ?? "/" : (w(), window.location.pathname);
73
+ }
74
+ function U(e, o = {}) {
75
+ const t = c(null), r = c(null), n = c(!1), u = c(null), { channel: s, enabled: _ = !0 } = o;
76
+ return h(() => {
77
+ if (typeof window > "u" || !_) return;
78
+ if (!e.__path) {
79
+ console.warn("[velojs] useEventStream: stream has no __path");
80
+ return;
81
+ }
82
+ t.value = null, r.value = null, n.value = !1, u.value = null;
83
+ const d = s ? `${e.__path}?channel=${encodeURIComponent(s)}` : e.__path, a = new EventSource(d);
84
+ return a.addEventListener("snapshot", (i) => {
85
+ try {
86
+ r.value = JSON.parse(i.data);
87
+ } catch (f) {
88
+ u.value = f;
89
+ }
90
+ }), a.addEventListener("message", (i) => {
91
+ try {
92
+ t.value = JSON.parse(i.data);
93
+ } catch (f) {
94
+ u.value = f;
95
+ }
96
+ }), a.addEventListener("heartbeat", () => {
97
+ }), a.addEventListener("close", () => {
98
+ n.value = !0, a.close();
99
+ }), a.addEventListener("error", () => {
100
+ a.readyState === EventSource.CLOSED && (n.value = !0);
101
+ }), () => {
102
+ a.close();
103
+ };
104
+ }, [e, s, _]), { data: t, snapshot: r, closed: n, error: u };
73
105
  }
74
106
  export {
75
- P as Loader,
76
- p as __veloUpdatePending,
77
- m as touch,
78
- T as useLoader,
79
- L as useNavigate,
80
- E as useParams,
107
+ m as Loader,
108
+ S as __veloUpdatePending,
109
+ L as touch,
110
+ U as useEventStream,
111
+ A as useLoader,
112
+ P as useNavigate,
113
+ T as useParams,
81
114
  H as usePathname,
82
115
  b as useQuery
83
116
  };
package/dist/index.d.ts CHANGED
@@ -2,4 +2,6 @@ export type { AppRoutes, RouteNode, RouteModule, LoaderArgs, ActionArgs, Metadat
2
2
  export type { VeloConfig } from "./config.js";
3
3
  export { defineConfig } from "./config.js";
4
4
  export { Scripts, Link } from "./components.js";
5
+ export { createEventStream, poll } from "./events.js";
6
+ export type { EventStream, EventStreamConfig, EmitFn, EmitOptions, SourceFn, } from "./events.js";
5
7
  export type { Context, MiddlewareHandler, Next } from "hono";
package/dist/index.js CHANGED
@@ -1,7 +1,10 @@
1
- import { defineConfig as e } from "./config.js";
2
- import { Link as i, Scripts as n } from "./components.js";
1
+ import { defineConfig as o } from "./config.js";
2
+ import { Link as f, Scripts as p } from "./components.js";
3
+ import { createEventStream as m, poll as n } from "./events.js";
3
4
  export {
4
- i as Link,
5
- n as Scripts,
6
- e as defineConfig
5
+ f as Link,
6
+ p as Scripts,
7
+ m as createEventStream,
8
+ o as defineConfig,
9
+ n as poll
7
10
  };
package/dist/server.js CHANGED
@@ -1,61 +1,61 @@
1
- import { jsx as m } from "preact/jsx-runtime";
1
+ import { jsx as _ } from "preact/jsx-runtime";
2
2
  import { Hono as T } from "hono";
3
- import { logger as A } from "hono/logger";
4
- import { trimTrailingSlash as b } from "hono/trailing-slash";
5
- import { render as D } from "preact-render-to-string";
6
- import { Router as R } from "wouter-preact";
7
- import { AsyncLocalStorage as C } from "node:async_hooks";
8
- const v = new C();
9
- globalThis.__veloServerData = v;
10
- const _ = [];
11
- let p = null;
3
+ import { logger as b } from "hono/logger";
4
+ import { trimTrailingSlash as A } from "hono/trailing-slash";
5
+ import { render as R } from "preact-render-to-string";
6
+ import { Router as D } from "wouter-preact";
7
+ import { AsyncLocalStorage as I } from "node:async_hooks";
8
+ const y = new I();
9
+ globalThis.__veloServerData = y;
10
+ const v = [];
11
+ let g = null;
12
12
  function L(t) {
13
- if (p) {
14
- t(p);
13
+ if (g) {
14
+ t(g);
15
15
  return;
16
16
  }
17
- _.push(t);
17
+ v.push(t);
18
18
  }
19
- function y(t) {
20
- p = t;
21
- for (const e of _) e(t);
22
- _.length = 0;
19
+ function w(t) {
20
+ g = t;
21
+ for (const e of v) e(t);
22
+ v.length = 0;
23
23
  }
24
- const w = [];
24
+ const S = [];
25
25
  function U(t) {
26
- w.push(t);
26
+ S.push(t);
27
27
  }
28
- const N = (t, e, o) => {
28
+ const N = (t, e, s) => {
29
29
  if (t.req.query("_data") === "1") {
30
- const c = globalThis.__veloBuildHash || void 0, r = o ? { ...o, __buildHash: c } : { __buildHash: c };
31
- return t.json(r);
30
+ const c = globalThis.__veloBuildHash || void 0, o = s ? { ...s, __buildHash: c } : { __buildHash: c };
31
+ return t.json(o);
32
32
  }
33
- const s = t.req.path, n = v.run(
34
- o ?? {},
35
- () => D(/* @__PURE__ */ m(R, { ssrPath: s, children: e }))
33
+ const r = t.req.path, n = y.run(
34
+ s ?? {},
35
+ () => R(/* @__PURE__ */ _(D, { ssrPath: r, children: e }))
36
36
  );
37
- if (t.header("Cache-Control", "no-cache"), !o)
37
+ if (t.header("Cache-Control", "no-cache"), !s)
38
38
  return t.html(n);
39
39
  const a = `<script>window.__PAGE_DATA__=${JSON.stringify(
40
- o
40
+ s
41
41
  )}<\/script>`;
42
42
  return t.html(n.replace("</head>", `${a}</head>`));
43
43
  }, O = async (t, e) => {
44
- const o = e.req.param(), s = e.req.query(), n = { params: o, query: s, c: e }, a = await Promise.all(
45
- t.map(async (r) => {
46
- if (!r.loader) return null;
47
- const u = await r.loader(n), i = r.metadata?.moduleId;
48
- return i ? { moduleId: i, loaderData: u } : null;
44
+ const s = e.req.param(), r = e.req.query(), n = { params: s, query: r, c: e }, a = await Promise.all(
45
+ t.map(async (o) => {
46
+ if (!o.loader) return null;
47
+ const l = await o.loader(n), i = o.metadata?.moduleId;
48
+ return i ? { moduleId: i, loaderData: l } : null;
49
49
  })
50
50
  ), c = {
51
- __params: o,
52
- __query: s,
51
+ __params: s,
52
+ __query: r,
53
53
  __pathname: e.req.path
54
54
  };
55
- for (const r of a)
56
- r && (c[r.moduleId] = r.loaderData);
55
+ for (const o of a)
56
+ o && (c[o.moduleId] = o.loaderData);
57
57
  return {
58
- components: t.map((r) => r.Component),
58
+ components: t.map((o) => o.Component),
59
59
  data: c
60
60
  };
61
61
  }, j = (t) => {
@@ -64,107 +64,137 @@ const N = (t, e, o) => {
64
64
  if (e.length === 0) return null;
65
65
  if (e.length === 1) {
66
66
  const n = e[0];
67
- return /* @__PURE__ */ m(n, {});
67
+ return /* @__PURE__ */ _(n, {});
68
68
  }
69
- const o = e[e.length - 1];
70
- return e.slice(0, -1).reduceRight((n, a) => /* @__PURE__ */ m(a, { children: n }), /* @__PURE__ */ m(o, {}));
71
- }, S = (t, e, o = [], s = []) => {
69
+ const s = e[e.length - 1];
70
+ return e.slice(0, -1).reduceRight((n, a) => /* @__PURE__ */ _(a, { children: n }), /* @__PURE__ */ _(s, {}));
71
+ }, P = (t, e, s = [], r = []) => {
72
72
  for (const n of e) {
73
- const a = [...o, n.module], c = [
74
- ...s,
73
+ const a = [...s, n.module], c = [
74
+ ...r,
75
75
  ...n.middlewares || []
76
76
  ];
77
77
  if (n.children)
78
- S(
78
+ P(
79
79
  t,
80
80
  n.children,
81
81
  a,
82
82
  c
83
83
  );
84
84
  else {
85
- const r = n.module.metadata?.fullPath;
86
- if (!r) {
85
+ const o = n.module.metadata?.fullPath;
86
+ if (!o) {
87
87
  console.warn(
88
88
  `Module ${n.module.metadata?.moduleId} has no fullPath`
89
89
  );
90
90
  continue;
91
91
  }
92
- const u = async (i) => {
93
- const { components: d, data: f } = await O(a, i), l = j(d);
94
- return N(i, l, f);
92
+ const l = async (i) => {
93
+ const { components: m, data: d } = await O(a, i), u = j(m);
94
+ return N(i, u, d);
95
95
  };
96
- c.length > 0 ? t.on(["GET"], [r], ...c, u) : t.on(["GET"], [r], u);
96
+ c.length > 0 ? t.on(["GET"], [o], ...c, l) : t.on(["GET"], [o], l);
97
97
  }
98
98
  }
99
- }, q = (t, e, o = []) => {
100
- for (const s of e) {
101
- const n = s.module.metadata?.moduleId, a = [
102
- ...o,
103
- ...s.middlewares || []
99
+ }, E = (t, e, s = []) => {
100
+ for (const r of e) {
101
+ const n = r.module.metadata?.moduleId, a = [
102
+ ...s,
103
+ ...r.middlewares || []
104
104
  ];
105
105
  if (n) {
106
- const c = Object.keys(s.module).filter(
107
- (r) => r.startsWith("action_")
106
+ const c = Object.keys(r.module).filter(
107
+ (o) => o.startsWith("action_")
108
108
  );
109
- for (const r of c) {
110
- const u = r.replace("action_", ""), i = s.module[r];
109
+ for (const o of c) {
110
+ const l = o.replace("action_", ""), i = r.module[o];
111
111
  if (typeof i == "function") {
112
- const d = `/_action/${n}/${u}`, f = async (l) => {
113
- let g = {};
112
+ const m = `/_action/${n}/${l}`, d = async (u) => {
113
+ let f = {};
114
114
  try {
115
- g = await l.req.json();
115
+ f = await u.req.json();
116
116
  } catch {
117
117
  }
118
- const E = {
119
- body: g,
120
- params: l.req.param(),
121
- query: l.req.query(),
122
- c: l
118
+ const h = {
119
+ body: f,
120
+ params: u.req.param(),
121
+ query: u.req.query(),
122
+ c: u
123
123
  };
124
124
  try {
125
- const h = await i(E);
126
- return l.json(h ?? { ok: !0 });
127
- } catch (h) {
128
- const P = h instanceof Error ? h.message : "Action failed";
129
- return l.json({ error: P }, 500);
125
+ const p = await i(h);
126
+ return u.json(p ?? { ok: !0 });
127
+ } catch (p) {
128
+ const q = p instanceof Error ? p.message : "Action failed";
129
+ return u.json({ error: q }, 500);
130
130
  }
131
131
  };
132
132
  a.length > 0 ? t.on(
133
133
  ["POST"],
134
- [d],
134
+ [m],
135
135
  ...a,
136
- f
137
- ) : t.on(["POST"], [d], f);
136
+ d
137
+ ) : t.on(["POST"], [m], d);
138
138
  }
139
139
  }
140
140
  }
141
- s.children && q(t, s.children, a);
141
+ r.children && E(t, r.children, a);
142
142
  }
143
- }, I = async (t) => {
143
+ }, C = async (t, e, s = []) => {
144
+ const { registerStreamHandler: r } = await import("./events.js"), n = (a, c) => {
145
+ for (const o of a) {
146
+ const l = o.module.metadata?.moduleId, i = [
147
+ ...c,
148
+ ...o.middlewares || []
149
+ ];
150
+ if (l) {
151
+ const m = Object.keys(o.module).filter(
152
+ (d) => d.startsWith("stream_")
153
+ );
154
+ for (const d of m) {
155
+ const u = d.replace("stream_", ""), f = o.module[d];
156
+ if (f?.__isVeloEventStream) {
157
+ const h = `/_event/${l}/${u}`;
158
+ f.__path = h, r(
159
+ t,
160
+ h,
161
+ f,
162
+ i
163
+ );
164
+ }
165
+ }
166
+ }
167
+ o.children && n(o.children, i);
168
+ }
169
+ };
170
+ n(e, s);
171
+ }, $ = async (t) => {
144
172
  const e = new T();
145
- e.use(b()), process.env.NODE_ENV !== "production" && e.use("*", A());
146
- for (const o of w)
147
- await o(e);
148
- if (S(e, t), q(e, t), process.env.NODE_ENV !== "production" && !p) {
149
- const o = globalThis.__veloDevServer;
150
- o && y(o);
173
+ e.use(A()), process.env.NODE_ENV !== "production" && e.use("*", b());
174
+ for (const r of S)
175
+ await r(e);
176
+ P(e, t), E(e, t), await C(e, t);
177
+ const { flushPendingStreamRoutes: s } = await import("./events.js");
178
+ if (s(e), process.env.NODE_ENV !== "production" && !g) {
179
+ const r = globalThis.__veloDevServer;
180
+ r && w(r);
151
181
  }
152
182
  return e;
153
- }, x = async (t) => {
154
- const { routes: e, port: o = Number(process.env.SERVER_PORT) || 3e3 } = t, s = await I(e);
183
+ }, W = async (t) => {
184
+ const { routes: e, port: s = Number(process.env.SERVER_PORT) || 3e3 } = t, r = await $(e);
155
185
  if (process.env.NODE_ENV === "production" && !process.env.VELO_STATIC) {
156
- const { serve: n } = await import("@hono/node-server"), { serveStatic: a } = await import("@hono/node-server/serve-static"), { join: c } = await import("node:path"), r = c(process.cwd(), "dist/client");
157
- (process.env.STATIC_BASE_URL || "").startsWith("http") || s.use("/*", a({ root: r })), console.log(`Server running on http://localhost:${o}`);
158
- const i = n({ fetch: s.fetch, port: o });
159
- y(i);
186
+ const { serve: n } = await import("@hono/node-server"), { serveStatic: a } = await import("@hono/node-server/serve-static"), { join: c } = await import("node:path"), o = c(process.cwd(), "dist/client");
187
+ (process.env.STATIC_BASE_URL || "").startsWith("http") || r.use("/*", a({ root: o })), console.log(`Server running on http://localhost:${s}`);
188
+ const i = n({ fetch: r.fetch, port: s });
189
+ w(i);
160
190
  }
161
- return s;
191
+ return r;
162
192
  };
163
193
  export {
164
194
  U as addRoutes,
165
- I as createApp,
166
- I as default,
195
+ $ as createApp,
196
+ $ as default,
167
197
  L as onServer,
168
- v as serverDataStorage,
169
- x as startServer
198
+ y as serverDataStorage,
199
+ W as startServer
170
200
  };
package/dist/vite.d.ts CHANGED
@@ -4,6 +4,14 @@ import * as t from "@babel/types";
4
4
  export declare function injectMetadata(code: string, moduleId: string, fullPath?: string, path?: string): string;
5
5
  export declare function transformLoaderFunctions(code: string, moduleId: string): string;
6
6
  export declare function transformActionsForClient(code: string, moduleId: string): string;
7
+ /**
8
+ * Transforma `export const stream_xxx = createEventStream({...})` no client em
9
+ * um stub com apenas { __isVeloEventStream: true, __path: "/_event/{moduleId}/{name}" }.
10
+ *
11
+ * O body do `createEventStream` é descartado — listeners, snapshot, channel funcs etc.
12
+ * só rodam no server. O client precisa apenas do path para abrir o EventSource.
13
+ */
14
+ export declare function transformStreamsForClient(code: string, moduleId: string): string;
7
15
  export declare function removeLoaders(code: string): string;
8
16
  export declare function removeMiddlewares(code: string): string;
9
17
  export interface PathInfo {