@mauroandre/velojs 0.0.17 → 0.0.19
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 +153 -3
- package/dist/components.js +15 -15
- package/dist/events.d.ts +91 -0
- package/dist/events.js +62 -0
- package/dist/hooks.d.ts +31 -0
- package/dist/hooks.js +73 -42
- package/dist/index.d.ts +2 -0
- package/dist/index.js +7 -5
- package/dist/server.js +124 -94
- package/dist/vite.d.ts +8 -0
- package/dist/vite.js +207 -174
- package/package.json +5 -1
package/dist/server.js
CHANGED
|
@@ -1,61 +1,61 @@
|
|
|
1
|
-
import { jsx as
|
|
1
|
+
import { jsx as _ } from "preact/jsx-runtime";
|
|
2
2
|
import { Hono as T } from "hono";
|
|
3
|
-
import { logger as
|
|
4
|
-
import { trimTrailingSlash as
|
|
5
|
-
import { render as
|
|
6
|
-
import { Router as
|
|
7
|
-
import { AsyncLocalStorage as
|
|
8
|
-
const
|
|
9
|
-
globalThis.__veloServerData =
|
|
10
|
-
const
|
|
11
|
-
let
|
|
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 (
|
|
14
|
-
t(
|
|
13
|
+
if (g) {
|
|
14
|
+
t(g);
|
|
15
15
|
return;
|
|
16
16
|
}
|
|
17
|
-
|
|
17
|
+
v.push(t);
|
|
18
18
|
}
|
|
19
|
-
function
|
|
20
|
-
|
|
21
|
-
for (const e of
|
|
22
|
-
|
|
19
|
+
function w(t) {
|
|
20
|
+
g = t;
|
|
21
|
+
for (const e of v) e(t);
|
|
22
|
+
v.length = 0;
|
|
23
23
|
}
|
|
24
|
-
const
|
|
24
|
+
const S = [];
|
|
25
25
|
function U(t) {
|
|
26
|
-
|
|
26
|
+
S.push(t);
|
|
27
27
|
}
|
|
28
|
-
const N = (t, e,
|
|
28
|
+
const N = (t, e, s) => {
|
|
29
29
|
if (t.req.query("_data") === "1") {
|
|
30
|
-
const c = globalThis.__veloBuildHash || void 0,
|
|
31
|
-
return t.json(
|
|
30
|
+
const c = globalThis.__veloBuildHash || void 0, o = s ? { ...s, __buildHash: c } : { __buildHash: c };
|
|
31
|
+
return t.json(o);
|
|
32
32
|
}
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
() =>
|
|
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"), !
|
|
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
|
-
|
|
40
|
+
s
|
|
41
41
|
)}<\/script>`;
|
|
42
42
|
return t.html(n.replace("</head>", `${a}</head>`));
|
|
43
43
|
}, O = async (t, e) => {
|
|
44
|
-
const
|
|
45
|
-
t.map(async (
|
|
46
|
-
if (!
|
|
47
|
-
const
|
|
48
|
-
return i ? { moduleId: i, loaderData:
|
|
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:
|
|
52
|
-
__query:
|
|
51
|
+
__params: s,
|
|
52
|
+
__query: r,
|
|
53
53
|
__pathname: e.req.path
|
|
54
54
|
};
|
|
55
|
-
for (const
|
|
56
|
-
|
|
55
|
+
for (const o of a)
|
|
56
|
+
o && (c[o.moduleId] = o.loaderData);
|
|
57
57
|
return {
|
|
58
|
-
components: t.map((
|
|
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__ */
|
|
67
|
+
return /* @__PURE__ */ _(n, {});
|
|
68
68
|
}
|
|
69
|
-
const
|
|
70
|
-
return e.slice(0, -1).reduceRight((n, a) => /* @__PURE__ */
|
|
71
|
-
},
|
|
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 = [...
|
|
74
|
-
...
|
|
73
|
+
const a = [...s, n.module], c = [
|
|
74
|
+
...r,
|
|
75
75
|
...n.middlewares || []
|
|
76
76
|
];
|
|
77
77
|
if (n.children)
|
|
78
|
-
|
|
78
|
+
P(
|
|
79
79
|
t,
|
|
80
80
|
n.children,
|
|
81
81
|
a,
|
|
82
82
|
c
|
|
83
83
|
);
|
|
84
84
|
else {
|
|
85
|
-
const
|
|
86
|
-
if (!
|
|
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
|
|
93
|
-
const { components:
|
|
94
|
-
return N(i,
|
|
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"], [
|
|
96
|
+
c.length > 0 ? t.on(["GET"], [o], ...c, l) : t.on(["GET"], [o], l);
|
|
97
97
|
}
|
|
98
98
|
}
|
|
99
|
-
},
|
|
100
|
-
for (const
|
|
101
|
-
const n =
|
|
102
|
-
...
|
|
103
|
-
...
|
|
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(
|
|
107
|
-
(
|
|
106
|
+
const c = Object.keys(r.module).filter(
|
|
107
|
+
(o) => o.startsWith("action_")
|
|
108
108
|
);
|
|
109
|
-
for (const
|
|
110
|
-
const
|
|
109
|
+
for (const o of c) {
|
|
110
|
+
const l = o.replace("action_", ""), i = r.module[o];
|
|
111
111
|
if (typeof i == "function") {
|
|
112
|
-
const
|
|
113
|
-
let
|
|
112
|
+
const m = `/_action/${n}/${l}`, d = async (u) => {
|
|
113
|
+
let f = {};
|
|
114
114
|
try {
|
|
115
|
-
|
|
115
|
+
f = await u.req.json();
|
|
116
116
|
} catch {
|
|
117
117
|
}
|
|
118
|
-
const
|
|
119
|
-
body:
|
|
120
|
-
params:
|
|
121
|
-
query:
|
|
122
|
-
c:
|
|
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
|
|
126
|
-
return
|
|
127
|
-
} catch (
|
|
128
|
-
const
|
|
129
|
-
return
|
|
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
|
-
[
|
|
134
|
+
[m],
|
|
135
135
|
...a,
|
|
136
|
-
|
|
137
|
-
) : t.on(["POST"], [
|
|
136
|
+
d
|
|
137
|
+
) : t.on(["POST"], [m], d);
|
|
138
138
|
}
|
|
139
139
|
}
|
|
140
140
|
}
|
|
141
|
-
|
|
141
|
+
r.children && E(t, r.children, a);
|
|
142
142
|
}
|
|
143
|
-
},
|
|
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(
|
|
146
|
-
for (const
|
|
147
|
-
await
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
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
|
-
},
|
|
154
|
-
const { routes: e, port:
|
|
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"),
|
|
157
|
-
(process.env.STATIC_BASE_URL || "").startsWith("http") ||
|
|
158
|
-
const i = n({ fetch:
|
|
159
|
-
|
|
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
|
|
191
|
+
return r;
|
|
162
192
|
};
|
|
163
193
|
export {
|
|
164
194
|
U as addRoutes,
|
|
165
|
-
|
|
166
|
-
|
|
195
|
+
$ as createApp,
|
|
196
|
+
$ as default,
|
|
167
197
|
L as onServer,
|
|
168
|
-
|
|
169
|
-
|
|
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 {
|