@mauroandre/velojs 0.0.60 → 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.
@@ -1,38 +1,38 @@
1
- import { getRegisteredStreams as W } from "../events.js";
2
- import { trackSocketSession as L, getAppSockets as M, injectWebSocketServer as B, abortAllSocketSessions as F } from "../sockets.js";
3
- import { registerChannels as N, getChannelMap as G, __resetChannels as z } from "../channels.js";
1
+ import { getRegisteredStreams as U } from "../events.js";
2
+ import { trackSocketSession as W, getAppSockets as O, injectWebSocketServer as B, abortAllSocketSessions as G } from "../sockets.js";
3
+ import { registerChannels as M, getChannelMap as F, __resetChannels as z } from "../channels.js";
4
4
  import { createIsolatedContext as Q, withAppContext as K } from "../app-context.js";
5
- function q(e) {
5
+ function N(e) {
6
6
  if (!e) return;
7
7
  const s = Object.entries(e);
8
8
  if (s.length !== 0)
9
- return s.map(([t, l]) => `${encodeURIComponent(t)}=${encodeURIComponent(l)}`).join("; ");
9
+ return s.map(([n, f]) => `${encodeURIComponent(n)}=${encodeURIComponent(f)}`).join("; ");
10
10
  }
11
11
  function X(e) {
12
- const s = {}, t = e.getSetCookie?.() ?? [];
13
- for (const l of t) {
14
- const c = l.indexOf(";"), f = c === -1 ? l : l.slice(0, c), u = f.indexOf("=");
15
- if (u === -1) continue;
16
- const p = f.slice(0, u).trim(), o = f.slice(u + 1).trim();
17
- p && (s[p] = decodeURIComponent(o));
12
+ const s = {}, n = e.getSetCookie?.() ?? [];
13
+ for (const f of n) {
14
+ const c = f.indexOf(";"), h = c === -1 ? f : f.slice(0, c), l = h.indexOf("=");
15
+ if (l === -1) continue;
16
+ const w = h.slice(0, l).trim(), i = h.slice(l + 1).trim();
17
+ w && (s[w] = decodeURIComponent(i));
18
18
  }
19
19
  return s;
20
20
  }
21
21
  function H(e) {
22
22
  if (!e) return "";
23
23
  const s = new URLSearchParams();
24
- for (const [l, c] of Object.entries(e))
24
+ for (const [f, c] of Object.entries(e))
25
25
  if (Array.isArray(c))
26
- for (const f of c) s.append(l, f);
26
+ for (const h of c) s.append(f, h);
27
27
  else
28
- s.set(l, c);
29
- const t = s.toString();
30
- return t ? `?${t}` : "";
28
+ s.set(f, c);
29
+ const n = s.toString();
30
+ return n ? `?${n}` : "";
31
31
  }
32
32
  function Y(e) {
33
33
  const s = {};
34
- return e.forEach((t, l) => {
35
- s[l] = t;
34
+ return e.forEach((n, f) => {
35
+ s[f] = n;
36
36
  }), s;
37
37
  }
38
38
  function Z(e) {
@@ -42,49 +42,49 @@ function Z(e) {
42
42
  };
43
43
  }
44
44
  const V = "http://velojs.test";
45
- function O(e, s) {
46
- const t = H(s);
47
- return e.startsWith("http://") || e.startsWith("https://") ? e + t : (e.startsWith("/") || (e = "/" + e), V + e + t);
45
+ function J(e, s) {
46
+ const n = H(s);
47
+ return e.startsWith("http://") || e.startsWith("https://") ? e + n : (e.startsWith("/") || (e = "/" + e), V + e + n);
48
48
  }
49
- function D(e = {}) {
50
- const { user: s, params: t = {}, query: l = {}, body: c, headers: f = {}, cookies: u } = e, p = q(u), o = new Headers(f);
51
- p && !o.has("cookie") && o.set("cookie", p);
52
- const n = V + "/_mock" + H(l), i = {
49
+ function L(e = {}) {
50
+ const { user: s, params: n = {}, query: f = {}, body: c, headers: h = {}, cookies: l } = e, w = N(l), i = new Headers(h);
51
+ w && !i.has("cookie") && i.set("cookie", w);
52
+ const t = V + "/_mock" + H(f), r = {
53
53
  method: c == null ? "GET" : "POST",
54
- headers: o
54
+ headers: i
55
55
  };
56
- c != null && (typeof c == "string" || c instanceof FormData || c instanceof Blob ? i.body = c : (i.body = JSON.stringify(c), o.has("content-type") || o.set("content-type", "application/json")));
57
- const d = new Request(n, i), a = /* @__PURE__ */ new Map();
58
- return s !== void 0 && a.set("user", s), {
56
+ c != null && (typeof c == "string" || c instanceof FormData || c instanceof Blob ? r.body = c : (r.body = JSON.stringify(c), i.has("content-type") || i.set("content-type", "application/json")));
57
+ const a = new Request(t, r), u = /* @__PURE__ */ new Map();
58
+ return s !== void 0 && u.set("user", s), {
59
59
  req: {
60
- raw: d,
61
- url: d.url,
62
- method: d.method,
63
- param(r) {
64
- return r === void 0 ? t : t[r];
60
+ raw: a,
61
+ url: a.url,
62
+ method: a.method,
63
+ param(o) {
64
+ return o === void 0 ? n : n[o];
65
65
  },
66
- query(r) {
67
- if (r === void 0) {
68
- const h = {};
69
- for (const [v, j] of Object.entries(l))
70
- h[v] = String(j);
71
- return h;
66
+ query(o) {
67
+ if (o === void 0) {
68
+ const d = {};
69
+ for (const [k, b] of Object.entries(f))
70
+ d[k] = String(b);
71
+ return d;
72
72
  }
73
- return l[r];
73
+ return f[o];
74
74
  },
75
- queries(r) {
76
- const h = l[r];
77
- if (h !== void 0)
78
- return Array.isArray(h) ? h : [h];
75
+ queries(o) {
76
+ const d = f[o];
77
+ if (d !== void 0)
78
+ return Array.isArray(d) ? d : [d];
79
79
  },
80
- header(r) {
81
- if (r === void 0) {
82
- const h = {};
83
- return o.forEach((v, j) => {
84
- h[j] = v;
85
- }), h;
80
+ header(o) {
81
+ if (o === void 0) {
82
+ const d = {};
83
+ return i.forEach((k, b) => {
84
+ d[b] = k;
85
+ }), d;
86
86
  }
87
- return o.get(r) ?? void 0;
87
+ return i.get(o) ?? void 0;
88
88
  },
89
89
  async json() {
90
90
  return c == null ? null : typeof c == "object" ? c : JSON.parse(String(c));
@@ -100,257 +100,257 @@ function D(e = {}) {
100
100
  res: void 0,
101
101
  env: {},
102
102
  finalized: !1,
103
- get(r) {
104
- return a.get(r);
103
+ get(o) {
104
+ return u.get(o);
105
105
  },
106
- set(r, h) {
107
- a.set(r, h);
106
+ set(o, d) {
107
+ u.set(o, d);
108
108
  },
109
109
  var: new Proxy({}, {
110
- get(r, h) {
111
- return a.get(h);
110
+ get(o, d) {
111
+ return u.get(d);
112
112
  }
113
113
  }),
114
- json(r, h = 200) {
115
- return new Response(JSON.stringify(r), {
116
- status: h,
114
+ json(o, d = 200) {
115
+ return new Response(JSON.stringify(o), {
116
+ status: d,
117
117
  headers: { "content-type": "application/json" }
118
118
  });
119
119
  },
120
- text(r, h = 200) {
121
- return new Response(r, { status: h });
120
+ text(o, d = 200) {
121
+ return new Response(o, { status: d });
122
122
  },
123
- html(r, h = 200) {
124
- return new Response(r, {
125
- status: h,
123
+ html(o, d = 200) {
124
+ return new Response(o, {
125
+ status: d,
126
126
  headers: { "content-type": "text/html; charset=UTF-8" }
127
127
  });
128
128
  },
129
- body(r, h = 200) {
130
- return new Response(r, { status: h });
129
+ body(o, d = 200) {
130
+ return new Response(o, { status: d });
131
131
  },
132
- redirect(r, h = 302) {
133
- return new Response(null, { status: h, headers: { location: r } });
132
+ redirect(o, d = 302) {
133
+ return new Response(null, { status: d, headers: { location: o } });
134
134
  },
135
- header(r, h) {
135
+ header(o, d) {
136
136
  },
137
- status(r) {
137
+ status(o) {
138
138
  }
139
139
  };
140
140
  }
141
141
  function ee(e, s) {
142
142
  if (typeof s == "string") {
143
- const t = M(e).get(s);
144
- if (!t)
143
+ const n = O(e).get(s);
144
+ if (!n)
145
145
  throw new Error(
146
146
  `[velojs/testing] no socket handler registered at "${s}". Make sure the route exports a socket_* handler and routes are wired.`
147
147
  );
148
- return { handler: t.handler, path: s };
148
+ return { handler: n.handler, path: s };
149
149
  }
150
150
  if (typeof s == "function") {
151
- const t = s.__path;
152
- if (!t)
151
+ const n = s.__path;
152
+ if (!n)
153
153
  throw new Error(
154
154
  "[velojs/testing] socket handler has no __path. Make sure your vitest.config.ts loads veloPlugin() and your routes are registered."
155
155
  );
156
- return { handler: s, path: t };
156
+ return { handler: s, path: n };
157
157
  }
158
158
  if (s && typeof s == "object" && typeof s.__path == "string") {
159
- const t = s.__path, l = M(e).get(t);
160
- if (!l)
159
+ const n = s.__path, f = O(e).get(n);
160
+ if (!f)
161
161
  throw new Error(
162
- `[velojs/testing] stub with __path="${t}" has no matching server handler.`
162
+ `[velojs/testing] stub with __path="${n}" has no matching server handler.`
163
163
  );
164
- return { handler: l.handler, path: t };
164
+ return { handler: f.handler, path: n };
165
165
  }
166
166
  throw new Error("[velojs/testing] socket(): unrecognized handler argument");
167
167
  }
168
- async function te(e, s, t = {}) {
169
- const { handler: l, path: c } = ee(e, s), f = {
170
- params: t.params ?? {},
168
+ async function te(e, s, n = {}) {
169
+ const { handler: f, path: c } = ee(e, s), h = {
170
+ params: n.params ?? {},
171
171
  query: {
172
- ...t.query,
173
- ...t.channel ? { channel: t.channel } : {}
172
+ ...n.query,
173
+ ...n.channel ? { channel: n.channel } : {}
174
174
  }
175
175
  };
176
- t.user !== void 0 && (f.user = t.user), t.headers && (f.headers = t.headers), t.cookies && (f.cookies = t.cookies);
177
- const u = D(f), p = [];
178
- let o = null, n = !1;
179
- const i = {
176
+ n.user !== void 0 && (h.user = n.user), n.headers && (h.headers = n.headers), n.cookies && (h.cookies = n.cookies);
177
+ const l = L(h), w = [];
178
+ let i = null, t = !1;
179
+ const r = {
180
180
  async *[Symbol.asyncIterator]() {
181
181
  for (; ; ) {
182
- for (; p.length > 0; )
183
- yield p.shift();
184
- if (n) return;
185
- await new Promise((y) => o = y), o = null;
182
+ for (; w.length > 0; )
183
+ yield w.shift();
184
+ if (t) return;
185
+ await new Promise((p) => i = p), i = null;
186
186
  }
187
187
  }
188
- }, d = (y) => {
189
- n || (p.push(y), o?.());
190
- }, a = () => {
191
- n || (n = !0, o?.());
192
- }, w = [];
193
- let r = [];
194
- const h = (y) => {
195
- w.push(y);
196
- const P = r;
197
- r = [];
198
- for (const R of P)
188
+ }, a = (p) => {
189
+ t || (w.push(p), i?.());
190
+ }, u = () => {
191
+ t || (t = !0, i?.());
192
+ }, g = [];
193
+ let o = [];
194
+ const d = (p) => {
195
+ g.push(p);
196
+ const P = o;
197
+ o = [];
198
+ for (const $ of P)
199
199
  try {
200
- R();
200
+ $();
201
201
  } catch {
202
202
  }
203
- }, v = new AbortController(), j = L(e, v);
204
- let m = !1, g = !1;
205
- const k = (y) => {
206
- g || (typeof y == "string" || y instanceof Uint8Array ? h(y) : h(JSON.stringify(y)));
207
- }, E = (y, P) => {
208
- C();
209
- }, _ = () => {
210
- m = !0;
203
+ }, k = new AbortController(), b = W(e, k);
204
+ let x = !1, y = !1;
205
+ const m = (p) => {
206
+ y || (typeof p == "string" || p instanceof Uint8Array ? d(p) : d(JSON.stringify(p)));
207
+ }, S = (p, P) => {
208
+ E();
211
209
  }, C = () => {
212
- if (g) return;
213
- if (g = !0, !v.signal.aborted)
210
+ x = !0;
211
+ }, E = () => {
212
+ if (y) return;
213
+ if (y = !0, !k.signal.aborted)
214
214
  try {
215
- v.abort();
215
+ k.abort();
216
216
  } catch {
217
217
  }
218
- j(), a();
219
- const y = r;
220
- r = [];
221
- for (const P of y)
218
+ b(), u();
219
+ const p = o;
220
+ o = [];
221
+ for (const P of p)
222
222
  try {
223
223
  P();
224
224
  } catch {
225
225
  }
226
- }, b = {
227
- incoming: i,
228
- send: k,
229
- close: E,
230
- keepOpen: _,
231
- abortSignal: v.signal,
232
- c: u,
233
- params: t.params ?? {},
226
+ }, A = {
227
+ incoming: r,
228
+ send: m,
229
+ close: S,
230
+ keepOpen: C,
231
+ abortSignal: k.signal,
232
+ c: l,
233
+ params: n.params ?? {},
234
234
  query: {
235
- ...t.query ?? {},
236
- ...t.channel ? { channel: t.channel } : {}
235
+ ...n.query ?? {},
236
+ ...n.channel ? { channel: n.channel } : {}
237
237
  }
238
- }, T = (async () => {
238
+ }, v = (async () => {
239
239
  try {
240
- await l(b);
241
- } catch (y) {
242
- console.error("[velojs/testing] socket handler threw:", y);
240
+ await f(A);
241
+ } catch (p) {
242
+ console.error("[velojs/testing] socket handler threw:", p);
243
243
  }
244
- m || C();
245
- })(), x = (y) => new Promise((P, R) => {
246
- const $ = setTimeout(() => {
247
- r = r.filter((U) => U !== I), R(new Error(`[velojs/testing] socket.next() timed out after ${y}ms`));
248
- }, y), I = () => {
249
- clearTimeout($), P();
244
+ x || E();
245
+ })(), T = (p) => new Promise((P, $) => {
246
+ const I = setTimeout(() => {
247
+ o = o.filter((D) => D !== R), $(new Error(`[velojs/testing] socket.next() timed out after ${p}ms`));
248
+ }, p), R = () => {
249
+ clearTimeout(I), P();
250
250
  };
251
- r.push(I);
251
+ o.push(R);
252
252
  });
253
- let S = 0;
253
+ let j = 0;
254
254
  return {
255
- send(y) {
256
- g || (typeof y == "string" || y instanceof Uint8Array ? d(y) : d(JSON.stringify(y)));
255
+ send(p) {
256
+ y || (typeof p == "string" || p instanceof Uint8Array ? a(p) : a(JSON.stringify(p)));
257
257
  },
258
258
  get messages() {
259
- return w;
259
+ return g;
260
260
  },
261
261
  get closed() {
262
- return g;
262
+ return y;
263
263
  },
264
- async next(y) {
265
- for (; S >= w.length; ) {
266
- if (g)
264
+ async next(p) {
265
+ for (; j >= g.length; ) {
266
+ if (y)
267
267
  throw new Error("[velojs/testing] socket closed before next()");
268
- await x(y.timeoutMs);
268
+ await T(p.timeoutMs);
269
269
  }
270
- return w[S++];
270
+ return g[j++];
271
271
  },
272
- async nextN(y, P) {
273
- const R = Date.now() + P.timeoutMs, $ = [];
274
- for (; $.length < y; ) {
275
- if (S < w.length) {
276
- $.push(w[S++]);
272
+ async nextN(p, P) {
273
+ const $ = Date.now() + P.timeoutMs, I = [];
274
+ for (; I.length < p; ) {
275
+ if (j < g.length) {
276
+ I.push(g[j++]);
277
277
  continue;
278
278
  }
279
- if (g)
279
+ if (y)
280
280
  throw new Error("[velojs/testing] socket closed before nextN()");
281
- const I = R - Date.now();
282
- if (I <= 0)
281
+ const R = $ - Date.now();
282
+ if (R <= 0)
283
283
  throw new Error(
284
284
  `[velojs/testing] socket.nextN() timed out after ${P.timeoutMs}ms`
285
285
  );
286
- await x(I);
286
+ await T(R);
287
287
  }
288
- return $;
288
+ return I;
289
289
  },
290
- async close(y, P) {
291
- C(), await Promise.race([
292
- T,
293
- new Promise((R) => setTimeout(R, 50))
290
+ async close(p, P) {
291
+ E(), await Promise.race([
292
+ v,
293
+ new Promise(($) => setTimeout($, 50))
294
294
  ]);
295
295
  },
296
- done: T
296
+ done: v
297
297
  };
298
298
  }
299
299
  function ne(e) {
300
- const s = /* @__PURE__ */ new Map(), t = /* @__PURE__ */ new Map();
301
- function l(c) {
302
- for (const f of c) {
303
- const u = f.module?.metadata?.moduleId, p = f.module?.metadata?.fullPath;
304
- if (f.module && u) {
305
- for (const n of Object.keys(f.module)) {
306
- if (!n.startsWith("action_")) continue;
307
- const i = f.module[n];
308
- if (typeof i != "function") continue;
309
- const d = n.replace("action_", "");
310
- s.set(i, {
311
- name: d,
312
- fn: i,
313
- moduleId: u,
314
- url: `/_action/${u}/${d}`
300
+ const s = /* @__PURE__ */ new Map(), n = /* @__PURE__ */ new Map();
301
+ function f(c) {
302
+ for (const h of c) {
303
+ const l = h.module?.metadata?.moduleId, w = h.module?.metadata?.fullPath;
304
+ if (h.module && l) {
305
+ for (const t of Object.keys(h.module)) {
306
+ if (!t.startsWith("action_")) continue;
307
+ const r = h.module[t];
308
+ if (typeof r != "function") continue;
309
+ const a = t.replace("action_", "");
310
+ s.set(r, {
311
+ name: a,
312
+ fn: r,
313
+ moduleId: l,
314
+ url: `/_action/${l}/${a}`
315
315
  });
316
316
  }
317
- const o = f.module.loader;
318
- if (typeof o == "function" && p) {
319
- const n = t.get(o);
320
- n ? n.paths.push(p) : t.set(o, {
321
- fn: o,
322
- moduleId: u,
323
- paths: [p]
317
+ const i = h.module.loader;
318
+ if (typeof i == "function" && w) {
319
+ const t = n.get(i);
320
+ t ? t.paths.push(w) : n.set(i, {
321
+ fn: i,
322
+ moduleId: l,
323
+ paths: [w]
324
324
  });
325
325
  }
326
326
  }
327
- f.children && l(f.children);
327
+ h.children && f(h.children);
328
328
  }
329
329
  }
330
- return l(e), { actions: s, loaders: t };
330
+ return f(e), { actions: s, loaders: n };
331
331
  }
332
332
  function se(e, s) {
333
333
  if (typeof s == "string") return s;
334
- const t = e.actions.get(s);
335
- if (!t)
334
+ const n = e.actions.get(s);
335
+ if (!n)
336
336
  throw new Error(
337
337
  `[velojs/testing] action_* function was not found in the route tree.
338
338
  Make sure your Vitest config loads the VeloJS Vite plugin so that metadata is injected. See https://github.com/mauro-andre/velojs#testing`
339
339
  );
340
- return t.url;
340
+ return n.url;
341
341
  }
342
- function oe(e, s, t) {
342
+ function oe(e, s, n) {
343
343
  if (typeof s == "string") return s;
344
- const l = e.loaders.get(s);
345
- if (!l)
344
+ const f = e.loaders.get(s);
345
+ if (!f)
346
346
  throw new Error(
347
347
  `[velojs/testing] loader function was not found in the route tree.
348
348
  Make sure your Vitest config loads the VeloJS Vite plugin so that metadata is injected.`
349
349
  );
350
- let c = l.paths[0];
351
- if (t)
352
- for (const [f, u] of Object.entries(t))
353
- c = c.replace(`:${f}`, encodeURIComponent(u));
350
+ let c = f.paths[0];
351
+ if (n)
352
+ for (const [h, l] of Object.entries(n))
353
+ c = c.replace(`:${h}`, encodeURIComponent(l));
354
354
  return c;
355
355
  }
356
356
  class re {
@@ -361,397 +361,438 @@ class re {
361
361
  };
362
362
  push(s) {
363
363
  this.buffer += s;
364
- const t = [];
365
- let l;
366
- for (; (l = this.buffer.indexOf(`
364
+ const n = [];
365
+ let f;
366
+ for (; (f = this.buffer.indexOf(`
367
367
  `)) !== -1; ) {
368
- const c = this.buffer.slice(0, l);
369
- if (this.buffer = this.buffer.slice(l + 1), c === "") {
368
+ const c = this.buffer.slice(0, f);
369
+ if (this.buffer = this.buffer.slice(f + 1), c === "") {
370
370
  if (this.current.data.length > 0 || this.current.event !== "message") {
371
- const o = {
371
+ const i = {
372
372
  event: this.current.event,
373
373
  data: this.current.data.join(`
374
374
  `)
375
375
  };
376
- this.current.id !== void 0 && (o.id = this.current.id), t.push(o);
376
+ this.current.id !== void 0 && (i.id = this.current.id), n.push(i);
377
377
  }
378
378
  this.current = { event: "message", data: [] };
379
379
  continue;
380
380
  }
381
381
  if (c.startsWith(":")) continue;
382
- const f = c.indexOf(":");
383
- if (f === -1) continue;
384
- const u = c.slice(0, f);
385
- let p = c.slice(f + 1);
386
- p.startsWith(" ") && (p = p.slice(1)), u === "event" ? this.current.event = p : u === "data" ? this.current.data.push(p) : u === "id" && (this.current.id = p);
382
+ const h = c.indexOf(":");
383
+ if (h === -1) continue;
384
+ const l = c.slice(0, h);
385
+ let w = c.slice(h + 1);
386
+ w.startsWith(" ") && (w = w.slice(1)), l === "event" ? this.current.event = w : l === "data" ? this.current.data.push(w) : l === "id" && (this.current.id = w);
387
387
  }
388
- return t;
388
+ return n;
389
389
  }
390
390
  }
391
- async function J(e) {
392
- const { response: s, snapshotEvents: t = !1, channelEvents: l = !1 } = e, c = s.status, f = [], u = [];
393
- let p = null, o = !1, n = null, i = 0, d = 0;
394
- const a = [], w = [];
395
- let r = null, h = Promise.resolve(void 0);
396
- const v = (m) => {
397
- for (; a.length > 0; ) {
398
- const g = a.shift();
399
- m ? g.reject(m) : g.reject(new Error("[velojs/testing] subscription closed before next event"));
391
+ async function q(e) {
392
+ const { response: s, snapshotEvents: n = !1, channelEvents: f = !1, liveEvents: c = !1 } = e, h = s.status, l = [], w = [];
393
+ let i = null, t = !1, r = null, a = 0, u = 0;
394
+ const g = [], o = [];
395
+ let d = null, k = Promise.resolve(void 0);
396
+ const b = (y) => {
397
+ for (; g.length > 0; ) {
398
+ const m = g.shift();
399
+ y ? m.reject(y) : m.reject(new Error("[velojs/testing] subscription closed before next event"));
400
400
  }
401
- for (; w.length > 0; ) {
402
- const g = w.shift();
403
- m ? g.reject(m) : g.reject(new Error("[velojs/testing] subscription closed before next event"));
401
+ for (; o.length > 0; ) {
402
+ const m = o.shift();
403
+ y ? m.reject(y) : m.reject(new Error("[velojs/testing] subscription closed before next event"));
404
404
  }
405
405
  };
406
- if (c >= 200 && c < 300 && s.body) {
407
- r = s.body.getReader();
408
- const m = new TextDecoder(), g = new re();
409
- h = (async () => {
406
+ if (h >= 200 && h < 300 && s.body) {
407
+ d = s.body.getReader();
408
+ const y = new TextDecoder(), m = new re();
409
+ k = (async () => {
410
410
  try {
411
411
  for (; ; ) {
412
- const { value: k, done: E } = await r.read();
413
- if (E) {
414
- o || (o = !0), v();
412
+ const { value: S, done: C } = await d.read();
413
+ if (C) {
414
+ t || (t = !0), b();
415
415
  return;
416
416
  }
417
- const _ = m.decode(k, { stream: !0 }), C = g.push(_);
418
- for (const b of C) {
419
- const T = b.event === "slice";
420
- if (b.event === "snapshot" || l && T) {
421
- let x;
417
+ const E = y.decode(S, { stream: !0 }), A = m.push(E);
418
+ for (const v of A) {
419
+ const T = v.event === "slice";
420
+ if (v.event === "snapshot" || f && T) {
421
+ let j;
422
422
  try {
423
- x = JSON.parse(b.data);
424
- } catch (S) {
425
- n = S;
423
+ j = JSON.parse(v.data);
424
+ } catch (_) {
425
+ r = _;
426
426
  }
427
- if (x !== void 0) {
428
- if (T || (p = x), t || l && T) {
429
- const S = x;
430
- f.push(S);
431
- const A = a.shift();
432
- A && A.resolve(S);
427
+ if (j !== void 0) {
428
+ if (T || (i = j), n || f && T) {
429
+ const _ = j;
430
+ l.push(_);
431
+ const p = g.shift();
432
+ p && p.resolve(_);
433
433
  }
434
- if (l) {
435
- const S = {
434
+ if (f) {
435
+ const _ = {
436
436
  type: T ? "slice" : "snapshot",
437
- data: x
437
+ data: j
438
438
  };
439
- u.push(S);
440
- const A = w.shift();
441
- A && A.resolve(S);
439
+ w.push(_);
440
+ const p = o.shift();
441
+ p && p.resolve(_);
442
442
  }
443
443
  }
444
444
  continue;
445
445
  }
446
- if (b.event === "close") {
447
- o = !0, v(), r.cancel().catch(() => {
446
+ if (c && v.event === "error") {
447
+ let j;
448
+ try {
449
+ j = JSON.parse(v.data);
450
+ } catch {
451
+ j = { message: v.data };
452
+ }
453
+ const _ = {
454
+ type: "error",
455
+ data: j
456
+ };
457
+ w.push(_);
458
+ const p = o.shift();
459
+ p && p.resolve(_);
460
+ continue;
461
+ }
462
+ if (v.event === "close") {
463
+ t = !0, b(), d.cancel().catch(() => {
448
464
  });
449
465
  return;
450
466
  }
451
- if (b.event !== "heartbeat")
467
+ if (v.event !== "heartbeat")
452
468
  try {
453
- const x = JSON.parse(b.data);
454
- f.push(x);
455
- const S = a.shift();
456
- S && S.resolve(x);
457
- } catch (x) {
458
- n = x;
469
+ const j = JSON.parse(v.data);
470
+ l.push(j);
471
+ const _ = g.shift();
472
+ _ && _.resolve(j);
473
+ } catch (j) {
474
+ r = j;
459
475
  }
460
476
  }
461
477
  }
462
- } catch (k) {
463
- if (k?.name === "AbortError") {
464
- v();
478
+ } catch (S) {
479
+ if (S?.name === "AbortError") {
480
+ b();
465
481
  return;
466
482
  }
467
- v(k);
483
+ b(S);
468
484
  }
469
485
  })();
470
486
  } else
471
- await s.text().catch(() => ""), o = !0;
472
- const j = {
487
+ await s.text().catch(() => ""), t = !0;
488
+ const x = {
473
489
  get status() {
474
- return c;
490
+ return h;
475
491
  },
476
492
  get events() {
477
- return f;
493
+ return l;
478
494
  },
479
495
  get snapshot() {
480
- return p;
496
+ return i;
481
497
  },
482
498
  get closed() {
483
- return o;
499
+ return t;
484
500
  },
485
- async next({ timeoutMs: m }) {
486
- if (n) throw n;
487
- if (f.length > i) {
488
- const g = f[i];
489
- return i++, g;
501
+ async next({ timeoutMs: y }) {
502
+ if (r) throw r;
503
+ if (l.length > a) {
504
+ const m = l[a];
505
+ return a++, m;
490
506
  }
491
- if (o)
507
+ if (t)
492
508
  throw new Error("[velojs/testing] subscription is closed");
493
- return new Promise((g, k) => {
494
- const E = setTimeout(() => {
495
- const b = a.findIndex((T) => T.resolve === _);
496
- b !== -1 && a.splice(b, 1), k(new Error(
497
- `[velojs/testing] timed out after ${m}ms waiting for next event`
509
+ return new Promise((m, S) => {
510
+ const C = setTimeout(() => {
511
+ const v = g.findIndex((T) => T.resolve === E);
512
+ v !== -1 && g.splice(v, 1), S(new Error(
513
+ `[velojs/testing] timed out after ${y}ms waiting for next event`
498
514
  ));
499
- }, m), _ = (b) => {
500
- clearTimeout(E), i++, g(b);
501
- }, C = (b) => {
502
- clearTimeout(E), k(b);
515
+ }, y), E = (v) => {
516
+ clearTimeout(C), a++, m(v);
517
+ }, A = (v) => {
518
+ clearTimeout(C), S(v);
503
519
  };
504
- a.push({ resolve: _, reject: C });
520
+ g.push({ resolve: E, reject: A });
505
521
  });
506
522
  },
507
- async nextEvent({ timeoutMs: m }) {
508
- if (n) throw n;
509
- if (u.length > d) {
510
- const g = u[d];
511
- return d++, g;
523
+ async nextEvent({ timeoutMs: y }) {
524
+ if (r) throw r;
525
+ if (w.length > u) {
526
+ const m = w[u];
527
+ return u++, m;
512
528
  }
513
- if (o)
529
+ if (t)
514
530
  throw new Error("[velojs/testing] subscription is closed");
515
- return new Promise((g, k) => {
516
- const E = setTimeout(() => {
517
- const b = w.findIndex((T) => T.resolve === _);
518
- b !== -1 && w.splice(b, 1), k(new Error(
519
- `[velojs/testing] timed out after ${m}ms waiting for next event`
531
+ return new Promise((m, S) => {
532
+ const C = setTimeout(() => {
533
+ const v = o.findIndex((T) => T.resolve === E);
534
+ v !== -1 && o.splice(v, 1), S(new Error(
535
+ `[velojs/testing] timed out after ${y}ms waiting for next event`
520
536
  ));
521
- }, m), _ = (b) => {
522
- clearTimeout(E), d++, g(b);
523
- }, C = (b) => {
524
- clearTimeout(E), k(b);
537
+ }, y), E = (v) => {
538
+ clearTimeout(C), u++, m(v);
539
+ }, A = (v) => {
540
+ clearTimeout(C), S(v);
525
541
  };
526
- w.push({ resolve: _, reject: C });
542
+ o.push({ resolve: E, reject: A });
527
543
  });
528
544
  },
529
- async nextN(m, { timeoutMs: g }) {
530
- const k = Date.now(), E = [];
531
- for (let _ = 0; _ < m; _++) {
532
- const C = g - (Date.now() - k);
533
- if (C <= 0)
545
+ async nextN(y, { timeoutMs: m }) {
546
+ const S = Date.now(), C = [];
547
+ for (let E = 0; E < y; E++) {
548
+ const A = m - (Date.now() - S);
549
+ if (A <= 0)
534
550
  throw new Error(
535
- `[velojs/testing] timed out after ${g}ms waiting for ${m} events (got ${E.length})`
551
+ `[velojs/testing] timed out after ${m}ms waiting for ${y} events (got ${C.length})`
536
552
  );
537
- E.push(await j.next({ timeoutMs: C }));
553
+ C.push(await x.next({ timeoutMs: A }));
538
554
  }
539
- return E;
555
+ return C;
540
556
  },
541
557
  async close() {
542
- if (r)
558
+ if (d)
543
559
  try {
544
- await r.cancel();
560
+ await d.cancel();
545
561
  } catch {
546
562
  }
547
- o = !0, v(), await h.catch(() => {
563
+ t = !0, b(), await k.catch(() => {
548
564
  });
549
565
  }
550
566
  };
551
- return j;
567
+ return x;
552
568
  }
553
- async function ge(e) {
569
+ async function ye(e) {
554
570
  const s = Q();
555
571
  return await K(s, async () => {
556
572
  e.bootstrap && await e.bootstrap();
557
- const t = {
573
+ const n = {
558
574
  ...e.channelOptions,
559
575
  coalesceMs: e.channelOptions?.coalesceMs ?? 0,
560
576
  idleMs: e.channelOptions?.idleMs ?? 0,
561
577
  heartbeatMs: e.channelOptions?.heartbeatMs ?? 0
562
578
  };
563
- e.channels ? N(e.channels, t) : N(G(), t);
564
- const { createApp: l } = await import("../server.js"), c = await l(e.routes), f = ne(e.routes), u = e.port !== void 0 ? await ie(c, e.port, e.hostname) : void 0;
565
- return ae(c, s, f, e, void 0, u);
579
+ e.channels ? M(e.channels, n) : M(F(), n);
580
+ const { createApp: f } = await import("../server.js"), c = await f(e.routes), h = ne(e.routes), l = e.port !== void 0 ? await ie(c, e.port, e.hostname) : void 0;
581
+ return ce(c, s, h, e, void 0, l);
566
582
  });
567
583
  }
568
- async function ie(e, s, t) {
569
- const { serve: l } = await import("@hono/node-server"), { flushServerCallbacks: c, clearActiveServer: f } = await import("../server.js"), u = l({
584
+ async function ie(e, s, n) {
585
+ const { serve: f } = await import("@hono/node-server"), { flushServerCallbacks: c, clearActiveServer: h } = await import("../server.js"), l = f({
570
586
  fetch: e.fetch,
571
587
  port: s,
572
- ...t ? { hostname: t } : {}
573
- }), p = /* @__PURE__ */ new Set();
574
- u.on("connection", (a) => {
575
- p.add(a), a.on("close", () => p.delete(a));
576
- }), await ce(u, s, t), await B(e, u), c(u);
577
- const o = u.address(), n = o && typeof o == "object" ? o.port : s, i = t && t !== "0.0.0.0" && t !== "::" ? t : "localhost";
578
- let d = !1;
588
+ ...n ? { hostname: n } : {}
589
+ }), w = /* @__PURE__ */ new Set();
590
+ l.on("connection", (u) => {
591
+ w.add(u), u.on("close", () => w.delete(u));
592
+ }), await ae(l, s, n), await B(e, l), c(l);
593
+ const i = l.address(), t = i && typeof i == "object" ? i.port : s, r = n && n !== "0.0.0.0" && n !== "::" ? n : "localhost";
594
+ let a = !1;
579
595
  return {
580
- port: n,
581
- url: `http://${i}:${n}`,
596
+ port: t,
597
+ url: `http://${r}:${t}`,
582
598
  async close() {
583
- if (d) return;
584
- d = !0;
585
- const a = new Promise((w) => {
586
- u.close(() => w());
599
+ if (a) return;
600
+ a = !0;
601
+ const u = new Promise((g) => {
602
+ l.close(() => g());
587
603
  });
588
- for (const w of p) w.destroy();
589
- p.clear(), u.closeAllConnections(), await a, f(u);
604
+ for (const g of w) g.destroy();
605
+ w.clear(), l.closeAllConnections(), await u, h(l);
590
606
  }
591
607
  };
592
608
  }
593
- function ce(e, s, t) {
594
- return e.listening ? Promise.resolve() : new Promise((l, c) => {
595
- const f = `${t ?? "0.0.0.0"}:${s}`, u = () => {
596
- e.off("listening", p), e.off("error", o);
597
- }, p = () => {
598
- u(), l();
599
- }, o = (n) => {
600
- u();
601
- const i = n.code ? `${n.code}: ${n.message}` : n.message;
609
+ function ae(e, s, n) {
610
+ return e.listening ? Promise.resolve() : new Promise((f, c) => {
611
+ const h = `${n ?? "0.0.0.0"}:${s}`, l = () => {
612
+ e.off("listening", w), e.off("error", i);
613
+ }, w = () => {
614
+ l(), f();
615
+ }, i = (t) => {
616
+ l();
617
+ const r = t.code ? `${t.code}: ${t.message}` : t.message;
602
618
  c(new Error(
603
- `[velojs/testing] could not bind the test server on ${f} — ${i}`
619
+ `[velojs/testing] could not bind the test server on ${h} — ${r}`
604
620
  ));
605
621
  };
606
- e.once("listening", p), e.once("error", o);
622
+ e.once("listening", w), e.once("error", i);
607
623
  });
608
624
  }
609
- function ae(e, s, t, l, c, f) {
610
- async function u(n, i, d = {}) {
611
- const a = O(i, d.query), w = new Headers(d.headers ?? {}), r = { ...d.cookies ?? {} }, h = q(r);
612
- h && w.set("cookie", h);
613
- let v = null;
614
- if (d.body !== void 0) {
615
- const k = Z(d.body);
616
- v = k.body, k.contentType && !w.has("content-type") && w.set("content-type", k.contentType);
625
+ function ce(e, s, n, f, c, h) {
626
+ async function l(t, r, a = {}) {
627
+ const u = J(r, a.query), g = new Headers(a.headers ?? {}), o = { ...a.cookies ?? {} }, d = N(o);
628
+ d && g.set("cookie", d);
629
+ let k = null;
630
+ if (a.body !== void 0) {
631
+ const m = Z(a.body);
632
+ k = m.body, m.contentType && !g.has("content-type") && g.set("content-type", m.contentType);
617
633
  }
618
- const j = { method: n, headers: w };
619
- v !== null && (j.body = v);
620
- const m = new Request(a, j), g = await e.fetch(m);
621
- return await le(g);
634
+ const b = { method: t, headers: g };
635
+ k !== null && (b.body = k);
636
+ const x = new Request(u, b), y = await e.fetch(x);
637
+ return await le(y);
622
638
  }
623
- async function p(n, i, d = {}) {
624
- const a = O(i, {
625
- ...d.query ?? {},
626
- ...d.channel ? { channel: d.channel } : {}
627
- }), w = new Headers(d.headers ?? {}), r = { ...d.cookies ?? {} }, h = q(r);
628
- h && w.set("cookie", h);
629
- const v = new Request(a, { method: n, headers: w });
630
- return await e.fetch(v);
639
+ async function w(t, r, a = {}) {
640
+ const u = J(r, {
641
+ ...a.query ?? {},
642
+ ...a.channel ? { channel: a.channel } : {}
643
+ }), g = new Headers(a.headers ?? {}), o = { ...a.cookies ?? {} }, d = N(o);
644
+ d && g.set("cookie", d);
645
+ const k = new Request(u, { method: t, headers: g });
646
+ return await e.fetch(k);
631
647
  }
632
- const o = {
648
+ const i = {
633
649
  hono: e,
634
650
  // `undefined` unless `port` was passed — no listener, no behavior change.
635
- port: f?.port,
636
- url: f?.url,
651
+ port: h?.port,
652
+ url: h?.url,
637
653
  // HTTP
638
- get: (n, i) => u("GET", n, i ?? {}),
639
- post: (n, i) => u("POST", n, i ?? {}),
640
- put: (n, i) => u("PUT", n, i ?? {}),
641
- patch: (n, i) => u("PATCH", n, i ?? {}),
642
- delete: (n, i) => u("DELETE", n, i ?? {}),
643
- async action(n, i = {}) {
644
- const d = se(t, n);
645
- return await u("POST", d, {
646
- ...i,
647
- body: i.body ?? {}
654
+ get: (t, r) => l("GET", t, r ?? {}),
655
+ post: (t, r) => l("POST", t, r ?? {}),
656
+ put: (t, r) => l("PUT", t, r ?? {}),
657
+ patch: (t, r) => l("PATCH", t, r ?? {}),
658
+ delete: (t, r) => l("DELETE", t, r ?? {}),
659
+ async action(t, r = {}) {
660
+ const a = se(n, t);
661
+ return await l("POST", a, {
662
+ ...r,
663
+ body: r.body ?? {}
648
664
  });
649
665
  },
650
- async loader(n, i = {}) {
651
- const d = oe(t, n, i.params), a = await u("GET", d, {
652
- ...i,
653
- query: { ...i.query ?? {}, _data: "1" }
666
+ async loader(t, r = {}) {
667
+ const a = oe(n, t, r.params), u = await l("GET", a, {
668
+ ...r,
669
+ query: { ...r.query ?? {}, _data: "1" }
654
670
  });
655
- if (a.status >= 300 && a.status < 400 || a.status >= 400) return a;
656
- const w = typeof n == "function" ? t.loaders.get(n)?.moduleId : void 0, r = await a.json();
657
- return w && r && typeof r == "object" && w in r ? r[w] : r;
658
- },
659
- async subscribe(n, i = {}) {
660
- const d = typeof n == "string" ? n : n.__path ?? fe(), a = await p("GET", d, i);
661
- return await J({ response: a });
662
- },
663
- async channel(n, i, d = {}) {
664
- const a = typeof n == "string" ? n : n?.metadata?.moduleId;
665
- a || ue(n);
666
- const w = d.params ? { _route: JSON.stringify(d.params) } : {}, r = await p(
671
+ if (u.status >= 300 && u.status < 400 || u.status >= 400) return u;
672
+ const g = typeof t == "function" ? n.loaders.get(t)?.moduleId : void 0, o = await u.json();
673
+ return g && o && typeof o == "object" && g in o ? o[g] : o;
674
+ },
675
+ async subscribe(t, r = {}) {
676
+ const a = typeof t == "string" ? t : t.__path ?? de(), u = await w("GET", a, r);
677
+ return await q({ response: u });
678
+ },
679
+ async channel(t, r, a = {}) {
680
+ const u = typeof t == "string" ? t : t?.metadata?.moduleId;
681
+ u || ue(t);
682
+ const g = a.params ? { _route: JSON.stringify(a.params) } : {}, o = await w(
667
683
  "GET",
668
- `/_channel/${a}/${i}`,
684
+ `/_channel/${u}/${r}`,
669
685
  {
670
- ...d,
671
- query: { ...d.query ?? {}, ...w }
686
+ ...a,
687
+ query: { ...a.query ?? {}, ...g }
672
688
  }
673
689
  );
674
- return await J({
675
- response: r,
690
+ return await q({
691
+ response: o,
676
692
  snapshotEvents: !0,
677
693
  channelEvents: !0
678
694
  });
679
695
  },
680
- async socket(n, i = {}) {
681
- return await te(e, n, i);
696
+ async liveAction(t, r, a = {}) {
697
+ const u = typeof t == "string" ? t : t?.metadata?.moduleId;
698
+ u || fe(t);
699
+ const g = a.args !== void 0 ? { _args: JSON.stringify(a.args) } : {}, o = await w(
700
+ "GET",
701
+ `/_live/${u}/${r}`,
702
+ {
703
+ ...a,
704
+ query: { ...a.query ?? {}, ...g }
705
+ }
706
+ );
707
+ return await q({
708
+ response: o,
709
+ snapshotEvents: !0,
710
+ channelEvents: !0,
711
+ liveEvents: !0
712
+ });
682
713
  },
683
- async sessionCookies({ user: n }) {
684
- if (!l.getSessionCookie)
714
+ async socket(t, r = {}) {
715
+ return await te(e, t, r);
716
+ },
717
+ async sessionCookies({ user: t }) {
718
+ if (!f.getSessionCookie)
685
719
  throw new Error(
686
720
  "[velojs/testing] sessionCookies/as requires `getSessionCookie` in createTestApp options."
687
721
  );
688
- return await l.getSessionCookie({ user: n });
722
+ return await f.getSessionCookie({ user: t });
689
723
  },
690
- as({ user: n }) {
691
- if (!l.getSessionCookie)
724
+ as({ user: t }) {
725
+ if (!f.getSessionCookie)
692
726
  throw new Error(
693
727
  "[velojs/testing] app.as() requires `getSessionCookie` in createTestApp options."
694
728
  );
695
- let i = null;
696
- const d = () => (i || (i = Promise.resolve(l.getSessionCookie({ user: n }))), i), a = (h) => (async (...v) => {
697
- const j = await d(), [m, g] = v, k = {
698
- ...g ?? {},
699
- cookies: { ...j, ...g?.cookies ?? {} }
729
+ let r = null;
730
+ const a = () => (r || (r = Promise.resolve(f.getSessionCookie({ user: t }))), r), u = (k) => (async (...b) => {
731
+ const x = await a(), [y, m] = b, S = {
732
+ ...m ?? {},
733
+ cookies: { ...x, ...m?.cookies ?? {} }
700
734
  };
701
- return await h.call(o, m, k);
702
- }), w = (h) => (async (v, j, m = {}) => {
703
- const g = await d();
704
- return await h.call(o, v, j, {
705
- ...m,
706
- cookies: { ...g, ...m?.cookies ?? {} }
735
+ return await k.call(i, y, S);
736
+ }), g = (k) => (async (b, x, y = {}) => {
737
+ const m = await a();
738
+ return await k.call(i, b, x, {
739
+ ...y,
740
+ cookies: { ...m, ...y?.cookies ?? {} }
741
+ });
742
+ }), o = (k) => (async (b, x, y = {}) => {
743
+ const m = await a();
744
+ return await k.call(i, b, x, {
745
+ ...y,
746
+ cookies: { ...m, ...y?.cookies ?? {} }
707
747
  });
708
748
  });
709
749
  return {
710
750
  hono: e,
711
- port: o.port,
712
- url: o.url,
713
- get: a(o.get),
714
- post: a(o.post),
715
- put: a(o.put),
716
- patch: a(o.patch),
717
- delete: a(o.delete),
718
- action: a(o.action),
719
- loader: a(o.loader),
720
- subscribe: a(o.subscribe),
721
- channel: w(o.channel),
722
- socket: a(o.socket),
723
- sessionCookies: o.sessionCookies,
724
- as: o.as,
751
+ port: i.port,
752
+ url: i.url,
753
+ get: u(i.get),
754
+ post: u(i.post),
755
+ put: u(i.put),
756
+ patch: u(i.patch),
757
+ delete: u(i.delete),
758
+ action: u(i.action),
759
+ loader: u(i.loader),
760
+ subscribe: u(i.subscribe),
761
+ channel: g(i.channel),
762
+ liveAction: o(i.liveAction),
763
+ socket: u(i.socket),
764
+ sessionCookies: i.sessionCookies,
765
+ as: i.as,
725
766
  // chaining .as.as works — last wins
726
- mockContext: o.mockContext,
727
- reset: o.reset,
728
- close: o.close
767
+ mockContext: i.mockContext,
768
+ reset: i.reset,
769
+ close: i.close
729
770
  };
730
771
  },
731
- mockContext(n = {}) {
732
- return D(n);
772
+ mockContext(t = {}) {
773
+ return L(t);
733
774
  },
734
775
  async reset() {
735
- for (const n of W())
776
+ for (const t of U())
736
777
  try {
737
- n.__reset();
738
- } catch (i) {
739
- console.error("[velojs/testing] stream.__reset failed:", i);
778
+ t.__reset();
779
+ } catch (r) {
780
+ console.error("[velojs/testing] stream.__reset failed:", r);
740
781
  }
741
782
  z();
742
783
  },
743
784
  async close() {
744
- await o.reset(), F(e);
745
- for (const n of s.disposers)
785
+ await i.reset(), G(e);
786
+ for (const t of s.disposers)
746
787
  try {
747
- n();
748
- } catch (i) {
749
- console.error("[velojs/testing] disposer failed:", i);
788
+ t();
789
+ } catch (r) {
790
+ console.error("[velojs/testing] disposer failed:", r);
750
791
  }
751
- s.disposers.clear(), await f?.close();
792
+ s.disposers.clear(), await h?.close();
752
793
  }
753
794
  };
754
- return o;
795
+ return i;
755
796
  }
756
797
  async function le(e) {
757
798
  return {
@@ -771,10 +812,16 @@ function ue(e) {
771
812
  );
772
813
  }
773
814
  function fe(e) {
815
+ const s = typeof e == "string" ? e : "(a module without metadata)";
816
+ throw new Error(
817
+ `[velojs/testing] liveAction(module, name) needs the module's metadata.moduleId — got ${s}. Pass the imported route module (the Vite plugin injects the metadata) or the moduleId string.`
818
+ );
819
+ }
820
+ function de(e) {
774
821
  throw new Error(
775
822
  "[velojs/testing] subscribe(stream) — stream has no __path. If this is a stream_* declaration, make sure your routes import is processed by the VeloJS Vite plugin (your vitest.config.ts must include `veloPlugin()`)."
776
823
  );
777
824
  }
778
825
  export {
779
- ge as createTestApp
826
+ ye as createTestApp
780
827
  };