@mauroandre/velojs 0.0.25 → 0.0.26
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 +41 -0
- package/dist/client.js +43 -16
- package/dist/index.d.ts +1 -1
- package/dist/server.js +175 -118
- package/dist/static.js +9 -8
- package/dist/testing/index.js +84 -84
- package/dist/types.d.ts +10 -1
- package/dist/vite.d.ts +10 -0
- package/dist/vite.js +313 -236
- package/package.json +1 -1
package/dist/testing/index.js
CHANGED
|
@@ -4,14 +4,14 @@ function k(e) {
|
|
|
4
4
|
if (!e) return;
|
|
5
5
|
const r = Object.entries(e);
|
|
6
6
|
if (r.length !== 0)
|
|
7
|
-
return r.map(([
|
|
7
|
+
return r.map(([u, l]) => `${encodeURIComponent(u)}=${encodeURIComponent(l)}`).join("; ");
|
|
8
8
|
}
|
|
9
9
|
function _(e) {
|
|
10
|
-
const r = {},
|
|
11
|
-
for (const
|
|
12
|
-
const i =
|
|
10
|
+
const r = {}, u = e.getSetCookie?.() ?? [];
|
|
11
|
+
for (const l of u) {
|
|
12
|
+
const i = l.indexOf(";"), a = i === -1 ? l : l.slice(0, i), h = a.indexOf("=");
|
|
13
13
|
if (h === -1) continue;
|
|
14
|
-
const c =
|
|
14
|
+
const c = a.slice(0, h).trim(), t = a.slice(h + 1).trim();
|
|
15
15
|
c && (r[c] = decodeURIComponent(t));
|
|
16
16
|
}
|
|
17
17
|
return r;
|
|
@@ -19,18 +19,18 @@ function _(e) {
|
|
|
19
19
|
function x(e) {
|
|
20
20
|
if (!e) return "";
|
|
21
21
|
const r = new URLSearchParams();
|
|
22
|
-
for (const [
|
|
22
|
+
for (const [l, i] of Object.entries(e))
|
|
23
23
|
if (Array.isArray(i))
|
|
24
|
-
for (const
|
|
24
|
+
for (const a of i) r.append(l, a);
|
|
25
25
|
else
|
|
26
|
-
r.set(
|
|
27
|
-
const
|
|
28
|
-
return
|
|
26
|
+
r.set(l, i);
|
|
27
|
+
const u = r.toString();
|
|
28
|
+
return u ? `?${u}` : "";
|
|
29
29
|
}
|
|
30
30
|
function A(e) {
|
|
31
31
|
const r = {};
|
|
32
|
-
return e.forEach((
|
|
33
|
-
r[
|
|
32
|
+
return e.forEach((u, l) => {
|
|
33
|
+
r[l] = u;
|
|
34
34
|
}), r;
|
|
35
35
|
}
|
|
36
36
|
function P(e) {
|
|
@@ -41,18 +41,18 @@ function P(e) {
|
|
|
41
41
|
}
|
|
42
42
|
const C = "http://velojs.test";
|
|
43
43
|
function S(e, r) {
|
|
44
|
-
const
|
|
45
|
-
return e.startsWith("http://") || e.startsWith("https://") ? e +
|
|
44
|
+
const u = x(r);
|
|
45
|
+
return e.startsWith("http://") || e.startsWith("https://") ? e + u : (e.startsWith("/") || (e = "/" + e), C + e + u);
|
|
46
46
|
}
|
|
47
47
|
function I(e) {
|
|
48
|
-
const r = /* @__PURE__ */ new Map(),
|
|
49
|
-
function
|
|
50
|
-
for (const
|
|
51
|
-
const h =
|
|
52
|
-
if (h) {
|
|
53
|
-
for (const o of Object.keys(
|
|
48
|
+
const r = /* @__PURE__ */ new Map(), u = /* @__PURE__ */ new Map();
|
|
49
|
+
function l(i) {
|
|
50
|
+
for (const a of i) {
|
|
51
|
+
const h = a.module?.metadata?.moduleId, c = a.module?.metadata?.fullPath;
|
|
52
|
+
if (a.module && h) {
|
|
53
|
+
for (const o of Object.keys(a.module)) {
|
|
54
54
|
if (!o.startsWith("action_")) continue;
|
|
55
|
-
const f =
|
|
55
|
+
const f = a.module[o];
|
|
56
56
|
if (typeof f != "function") continue;
|
|
57
57
|
const d = o.replace("action_", "");
|
|
58
58
|
r.set(f, {
|
|
@@ -62,43 +62,43 @@ function I(e) {
|
|
|
62
62
|
url: `/_action/${h}/${d}`
|
|
63
63
|
});
|
|
64
64
|
}
|
|
65
|
-
const t =
|
|
65
|
+
const t = a.module.loader;
|
|
66
66
|
if (typeof t == "function" && c) {
|
|
67
|
-
const o =
|
|
68
|
-
o ? o.paths.push(c) :
|
|
67
|
+
const o = u.get(t);
|
|
68
|
+
o ? o.paths.push(c) : u.set(t, {
|
|
69
69
|
fn: t,
|
|
70
70
|
moduleId: h,
|
|
71
71
|
paths: [c]
|
|
72
72
|
});
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
|
-
|
|
75
|
+
a.children && l(a.children);
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
|
-
return
|
|
78
|
+
return l(e), { actions: r, loaders: u };
|
|
79
79
|
}
|
|
80
80
|
function q(e, r) {
|
|
81
81
|
if (typeof r == "string") return r;
|
|
82
|
-
const
|
|
83
|
-
if (!
|
|
82
|
+
const u = e.actions.get(r);
|
|
83
|
+
if (!u)
|
|
84
84
|
throw new Error(
|
|
85
85
|
`[velojs/testing] action_* function was not found in the route tree.
|
|
86
86
|
Make sure your Vitest config loads the VeloJS Vite plugin so that metadata is injected. See https://github.com/mauro-andre/velojs#testing`
|
|
87
87
|
);
|
|
88
|
-
return
|
|
88
|
+
return u.url;
|
|
89
89
|
}
|
|
90
|
-
function J(e, r,
|
|
90
|
+
function J(e, r, u) {
|
|
91
91
|
if (typeof r == "string") return r;
|
|
92
|
-
const
|
|
93
|
-
if (!
|
|
92
|
+
const l = e.loaders.get(r);
|
|
93
|
+
if (!l)
|
|
94
94
|
throw new Error(
|
|
95
95
|
`[velojs/testing] loader function was not found in the route tree.
|
|
96
96
|
Make sure your Vitest config loads the VeloJS Vite plugin so that metadata is injected.`
|
|
97
97
|
);
|
|
98
|
-
let i =
|
|
99
|
-
if (
|
|
100
|
-
for (const [
|
|
101
|
-
i = i.replace(`:${
|
|
98
|
+
let i = l.paths[0];
|
|
99
|
+
if (u)
|
|
100
|
+
for (const [a, h] of Object.entries(u))
|
|
101
|
+
i = i.replace(`:${a}`, encodeURIComponent(h));
|
|
102
102
|
return i;
|
|
103
103
|
}
|
|
104
104
|
class $ {
|
|
@@ -109,36 +109,36 @@ class $ {
|
|
|
109
109
|
};
|
|
110
110
|
push(r) {
|
|
111
111
|
this.buffer += r;
|
|
112
|
-
const
|
|
113
|
-
let
|
|
114
|
-
for (; (
|
|
112
|
+
const u = [];
|
|
113
|
+
let l;
|
|
114
|
+
for (; (l = this.buffer.indexOf(`
|
|
115
115
|
`)) !== -1; ) {
|
|
116
|
-
const i = this.buffer.slice(0,
|
|
117
|
-
if (this.buffer = this.buffer.slice(
|
|
116
|
+
const i = this.buffer.slice(0, l);
|
|
117
|
+
if (this.buffer = this.buffer.slice(l + 1), i === "") {
|
|
118
118
|
if (this.current.data.length > 0 || this.current.event !== "message") {
|
|
119
119
|
const t = {
|
|
120
120
|
event: this.current.event,
|
|
121
121
|
data: this.current.data.join(`
|
|
122
122
|
`)
|
|
123
123
|
};
|
|
124
|
-
this.current.id !== void 0 && (t.id = this.current.id),
|
|
124
|
+
this.current.id !== void 0 && (t.id = this.current.id), u.push(t);
|
|
125
125
|
}
|
|
126
126
|
this.current = { event: "message", data: [] };
|
|
127
127
|
continue;
|
|
128
128
|
}
|
|
129
129
|
if (i.startsWith(":")) continue;
|
|
130
|
-
const
|
|
131
|
-
if (
|
|
132
|
-
const h = i.slice(0,
|
|
133
|
-
let c = i.slice(
|
|
130
|
+
const a = i.indexOf(":");
|
|
131
|
+
if (a === -1) continue;
|
|
132
|
+
const h = i.slice(0, a);
|
|
133
|
+
let c = i.slice(a + 1);
|
|
134
134
|
c.startsWith(" ") && (c = c.slice(1)), h === "event" ? this.current.event = c : h === "data" ? this.current.data.push(c) : h === "id" && (this.current.id = c);
|
|
135
135
|
}
|
|
136
|
-
return
|
|
136
|
+
return u;
|
|
137
137
|
}
|
|
138
138
|
}
|
|
139
139
|
async function H(e) {
|
|
140
|
-
const { response: r } = e,
|
|
141
|
-
let i = null,
|
|
140
|
+
const { response: r } = e, u = r.status, l = [];
|
|
141
|
+
let i = null, a = !1, h = null;
|
|
142
142
|
const c = [];
|
|
143
143
|
let t = null, o = Promise.resolve(void 0);
|
|
144
144
|
const f = (p) => {
|
|
@@ -147,7 +147,7 @@ async function H(e) {
|
|
|
147
147
|
p ? n.reject(p) : n.reject(new Error("[velojs/testing] subscription closed before next event"));
|
|
148
148
|
}
|
|
149
149
|
};
|
|
150
|
-
if (
|
|
150
|
+
if (u >= 200 && u < 300 && r.body) {
|
|
151
151
|
t = r.body.getReader();
|
|
152
152
|
const p = new TextDecoder(), n = new $();
|
|
153
153
|
o = (async () => {
|
|
@@ -155,7 +155,7 @@ async function H(e) {
|
|
|
155
155
|
for (; ; ) {
|
|
156
156
|
const { value: s, done: w } = await t.read();
|
|
157
157
|
if (w) {
|
|
158
|
-
|
|
158
|
+
a || (a = !0), f();
|
|
159
159
|
return;
|
|
160
160
|
}
|
|
161
161
|
const g = p.decode(s, { stream: !0 }), v = n.push(g);
|
|
@@ -169,14 +169,14 @@ async function H(e) {
|
|
|
169
169
|
continue;
|
|
170
170
|
}
|
|
171
171
|
if (m.event === "close") {
|
|
172
|
-
|
|
172
|
+
a = !0, f(), t.cancel().catch(() => {
|
|
173
173
|
});
|
|
174
174
|
return;
|
|
175
175
|
}
|
|
176
176
|
if (m.event !== "heartbeat")
|
|
177
177
|
try {
|
|
178
178
|
const b = JSON.parse(m.data);
|
|
179
|
-
|
|
179
|
+
l.push(b);
|
|
180
180
|
const j = c.shift();
|
|
181
181
|
j && j.resolve(b);
|
|
182
182
|
} catch (b) {
|
|
@@ -193,27 +193,27 @@ async function H(e) {
|
|
|
193
193
|
}
|
|
194
194
|
})();
|
|
195
195
|
} else
|
|
196
|
-
await r.text().catch(() => ""),
|
|
196
|
+
await r.text().catch(() => ""), a = !0;
|
|
197
197
|
const d = {
|
|
198
198
|
get status() {
|
|
199
|
-
return
|
|
199
|
+
return u;
|
|
200
200
|
},
|
|
201
201
|
get events() {
|
|
202
|
-
return
|
|
202
|
+
return l;
|
|
203
203
|
},
|
|
204
204
|
get snapshot() {
|
|
205
205
|
return i;
|
|
206
206
|
},
|
|
207
207
|
get closed() {
|
|
208
|
-
return
|
|
208
|
+
return a;
|
|
209
209
|
},
|
|
210
210
|
async next({ timeoutMs: p }) {
|
|
211
211
|
if (h) throw h;
|
|
212
|
-
if (
|
|
213
|
-
const n =
|
|
212
|
+
if (l.length > y) {
|
|
213
|
+
const n = l[y];
|
|
214
214
|
return y++, n;
|
|
215
215
|
}
|
|
216
|
-
if (
|
|
216
|
+
if (a)
|
|
217
217
|
throw new Error("[velojs/testing] subscription is closed");
|
|
218
218
|
return new Promise((n, s) => {
|
|
219
219
|
const w = setTimeout(() => {
|
|
@@ -247,7 +247,7 @@ async function H(e) {
|
|
|
247
247
|
await t.cancel();
|
|
248
248
|
} catch {
|
|
249
249
|
}
|
|
250
|
-
|
|
250
|
+
a = !0, f(), await o.catch(() => {
|
|
251
251
|
});
|
|
252
252
|
}
|
|
253
253
|
};
|
|
@@ -255,9 +255,9 @@ async function H(e) {
|
|
|
255
255
|
return d;
|
|
256
256
|
}
|
|
257
257
|
function N(e = {}) {
|
|
258
|
-
const { user: r, params:
|
|
258
|
+
const { user: r, params: u = {}, query: l = {}, body: i, headers: a = {}, cookies: h } = e, c = k(h), t = new Headers(a);
|
|
259
259
|
c && !t.has("cookie") && t.set("cookie", c);
|
|
260
|
-
const o = C + "/_mock" + x(
|
|
260
|
+
const o = C + "/_mock" + x(l), f = {
|
|
261
261
|
method: i == null ? "GET" : "POST",
|
|
262
262
|
headers: t
|
|
263
263
|
};
|
|
@@ -269,19 +269,19 @@ function N(e = {}) {
|
|
|
269
269
|
url: d.url,
|
|
270
270
|
method: d.method,
|
|
271
271
|
param(n) {
|
|
272
|
-
return n === void 0 ?
|
|
272
|
+
return n === void 0 ? u : u[n];
|
|
273
273
|
},
|
|
274
274
|
query(n) {
|
|
275
275
|
if (n === void 0) {
|
|
276
276
|
const s = {};
|
|
277
|
-
for (const [w, g] of Object.entries(
|
|
277
|
+
for (const [w, g] of Object.entries(l))
|
|
278
278
|
s[w] = String(g);
|
|
279
279
|
return s;
|
|
280
280
|
}
|
|
281
|
-
return
|
|
281
|
+
return l[n];
|
|
282
282
|
},
|
|
283
283
|
queries(n) {
|
|
284
|
-
const s =
|
|
284
|
+
const s = l[n];
|
|
285
285
|
if (s !== void 0)
|
|
286
286
|
return Array.isArray(s) ? s : [s];
|
|
287
287
|
},
|
|
@@ -350,12 +350,12 @@ async function W(e) {
|
|
|
350
350
|
const r = E();
|
|
351
351
|
return await R(r, async () => {
|
|
352
352
|
e.bootstrap && await e.bootstrap();
|
|
353
|
-
const { createApp:
|
|
354
|
-
return V(
|
|
353
|
+
const { createApp: u } = await import("../server.js"), l = await u(e.routes), i = I(e.routes);
|
|
354
|
+
return V(l, r, i, e, void 0);
|
|
355
355
|
});
|
|
356
356
|
}
|
|
357
|
-
function V(e, r,
|
|
358
|
-
async function
|
|
357
|
+
function V(e, r, u, l, i) {
|
|
358
|
+
async function a(t, o, f = {}) {
|
|
359
359
|
const d = S(o, f.query), y = new Headers(f.headers ?? {}), p = { ...f.cookies ?? {} }, n = k(p);
|
|
360
360
|
n && y.set("cookie", n);
|
|
361
361
|
let s = null;
|
|
@@ -380,25 +380,25 @@ function V(e, r, a, u, i) {
|
|
|
380
380
|
const c = {
|
|
381
381
|
hono: e,
|
|
382
382
|
// HTTP
|
|
383
|
-
get: (t, o) =>
|
|
384
|
-
post: (t, o) =>
|
|
385
|
-
put: (t, o) =>
|
|
386
|
-
patch: (t, o) =>
|
|
387
|
-
delete: (t, o) =>
|
|
383
|
+
get: (t, o) => a("GET", t, o ?? {}),
|
|
384
|
+
post: (t, o) => a("POST", t, o ?? {}),
|
|
385
|
+
put: (t, o) => a("PUT", t, o ?? {}),
|
|
386
|
+
patch: (t, o) => a("PATCH", t, o ?? {}),
|
|
387
|
+
delete: (t, o) => a("DELETE", t, o ?? {}),
|
|
388
388
|
async action(t, o = {}) {
|
|
389
|
-
const f = q(
|
|
390
|
-
return await
|
|
389
|
+
const f = q(u, t);
|
|
390
|
+
return await a("POST", f, {
|
|
391
391
|
...o,
|
|
392
392
|
body: o.body ?? {}
|
|
393
393
|
});
|
|
394
394
|
},
|
|
395
395
|
async loader(t, o = {}) {
|
|
396
|
-
const f = J(
|
|
396
|
+
const f = J(u, t, o.params), d = await a("GET", f, {
|
|
397
397
|
...o,
|
|
398
398
|
query: { ...o.query ?? {}, _data: "1" }
|
|
399
399
|
});
|
|
400
400
|
if (d.status >= 300 && d.status < 400 || d.status >= 400) return d;
|
|
401
|
-
const y = typeof t == "function" ?
|
|
401
|
+
const y = typeof t == "function" ? u.loaders.get(t)?.moduleId : void 0, p = await d.json();
|
|
402
402
|
return y && p && typeof p == "object" && y in p ? p[y] : p;
|
|
403
403
|
},
|
|
404
404
|
async subscribe(t, o = {}) {
|
|
@@ -406,19 +406,19 @@ function V(e, r, a, u, i) {
|
|
|
406
406
|
return await H({ response: d });
|
|
407
407
|
},
|
|
408
408
|
async sessionCookies({ user: t }) {
|
|
409
|
-
if (!
|
|
409
|
+
if (!l.getSessionCookie)
|
|
410
410
|
throw new Error(
|
|
411
411
|
"[velojs/testing] sessionCookies/as requires `getSessionCookie` in createTestApp options."
|
|
412
412
|
);
|
|
413
|
-
return await
|
|
413
|
+
return await l.getSessionCookie({ user: t });
|
|
414
414
|
},
|
|
415
415
|
as({ user: t }) {
|
|
416
|
-
if (!
|
|
416
|
+
if (!l.getSessionCookie)
|
|
417
417
|
throw new Error(
|
|
418
418
|
"[velojs/testing] app.as() requires `getSessionCookie` in createTestApp options."
|
|
419
419
|
);
|
|
420
420
|
let o = null;
|
|
421
|
-
const f = () => (o || (o = Promise.resolve(
|
|
421
|
+
const f = () => (o || (o = Promise.resolve(l.getSessionCookie({ user: t }))), o), d = (p) => (async (...n) => {
|
|
422
422
|
const s = await f(), [w, g] = n, v = {
|
|
423
423
|
...g ?? {},
|
|
424
424
|
cookies: { ...s, ...g?.cookies ?? {} }
|
package/dist/types.d.ts
CHANGED
|
@@ -24,9 +24,18 @@ export interface RouteModule {
|
|
|
24
24
|
metadata?: Metadata;
|
|
25
25
|
[key: `action_${string}`]: (args: ActionArgs<any>) => Promise<any>;
|
|
26
26
|
}
|
|
27
|
+
export type HTTPMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
28
|
+
export interface EndpointHandlerArgs {
|
|
29
|
+
c: Context;
|
|
30
|
+
params: Record<string, string>;
|
|
31
|
+
query: Record<string, string>;
|
|
32
|
+
}
|
|
33
|
+
export type EndpointHandler = (args: EndpointHandlerArgs) => Response | Promise<Response>;
|
|
27
34
|
export interface RouteNode {
|
|
28
35
|
path?: string;
|
|
29
|
-
module
|
|
36
|
+
module?: RouteModule;
|
|
37
|
+
method?: HTTPMethod;
|
|
38
|
+
handler?: EndpointHandler;
|
|
30
39
|
children?: RouteNode[];
|
|
31
40
|
middlewares?: MiddlewareHandler[];
|
|
32
41
|
isRoot?: boolean;
|
package/dist/vite.d.ts
CHANGED
|
@@ -14,6 +14,16 @@ export declare function transformActionsForClient(code: string, moduleId: string
|
|
|
14
14
|
export declare function transformStreamsForClient(code: string, moduleId: string): string;
|
|
15
15
|
export declare function removeLoaders(code: string): string;
|
|
16
16
|
export declare function removeMiddlewares(code: string): string;
|
|
17
|
+
/**
|
|
18
|
+
* On client builds, strip EndpointRoute objects from the default-export array
|
|
19
|
+
* of `routes.tsx` so server-only handler code (and its imports) don't ship to
|
|
20
|
+
* the browser. Mirrors the pattern used by `removeMiddlewares`.
|
|
21
|
+
*
|
|
22
|
+
* Detection: any ObjectExpression that has a `handler` property is treated as
|
|
23
|
+
* an endpoint and removed from its containing array. Imports referenced only
|
|
24
|
+
* by removed endpoints are dropped afterwards.
|
|
25
|
+
*/
|
|
26
|
+
export declare function removeEndpointRoutes(code: string): string;
|
|
17
27
|
export interface PathInfo {
|
|
18
28
|
fullPath: string;
|
|
19
29
|
path: string;
|