@mauroandre/velojs 0.0.27 → 0.0.28

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.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { type Signal } from "@preact/signals";
2
2
  import type { EventStream } from "./events.js";
3
- import type { SocketStub } from "./sockets.js";
3
+ import type { SocketHandler, SocketStub } from "./sockets.js";
4
4
  export declare const __veloUpdatePending: Signal<boolean>;
5
5
  /**
6
6
  * Força o signal a notificar mudanças após mutação de propriedades aninhadas
@@ -201,7 +201,7 @@ export interface UseSocketApi {
201
201
  * send({ type: "resize", cols: 80, rows: 24 });
202
202
  * ```
203
203
  */
204
- export declare function useSocket(stub: SocketStub | {
204
+ export declare function useSocket(stub: SocketHandler | SocketStub | {
205
205
  __path: string;
206
206
  }, options?: UseSocketOptions): UseSocketApi;
207
207
  export {};
package/dist/sockets.d.ts CHANGED
@@ -52,7 +52,15 @@ export interface SocketHandlerArgs {
52
52
  /** Query-string values. */
53
53
  query: Record<string, string>;
54
54
  }
55
- export type SocketHandler = (args: SocketHandlerArgs) => void | Promise<void>;
55
+ export type SocketHandler = ((args: SocketHandlerArgs) => void | Promise<void>) & {
56
+ /**
57
+ * Populated at runtime by the socket registrar (and by the Vite plugin on the
58
+ * client bundle, where the function is replaced by a stub). Lets consumers
59
+ * pass the declared `socket_*` value straight into `useSocket(...)` without
60
+ * casting.
61
+ */
62
+ readonly __path?: string;
63
+ };
56
64
  /** Client-side stub shape — what the Vite plugin replaces `socket_*` with on the client bundle. */
57
65
  export interface SocketStub {
58
66
  readonly __isVeloSocket: true;
@@ -89,6 +97,13 @@ export declare function registerSocketRoutes(app: Hono, nodes: RouteNode[], pare
89
97
  * returns (prod) or with Vite's dev http server (dev).
90
98
  *
91
99
  * No-op when no sockets are registered on `app`.
100
+ *
101
+ * The server is wrapped so that `upgrade` listeners registered by
102
+ * `@hono/node-ws` only fire for requests whose pathname matches a registered
103
+ * `socket_*` route. Without this, the adapter's listener treats every upgrade
104
+ * as a candidate and replies with an HTTP status, which corrupts the handshake
105
+ * for other listeners on the same server (notably Vite's HMR WebSocket in
106
+ * dev mode).
92
107
  */
93
108
  export declare function injectWebSocketServer(app: Hono, server: any): Promise<void>;
94
109
  export {};
package/dist/sockets.js CHANGED
@@ -8,28 +8,28 @@ async function* W(e) {
8
8
  }
9
9
  }
10
10
  }
11
- const S = /* @__PURE__ */ new WeakMap(), g = /* @__PURE__ */ new WeakMap(), k = /* @__PURE__ */ new WeakMap();
12
- async function m(e) {
13
- let t = S.get(e);
11
+ const g = /* @__PURE__ */ new WeakMap(), k = /* @__PURE__ */ new WeakMap(), S = /* @__PURE__ */ new WeakMap();
12
+ async function p(e) {
13
+ let t = g.get(e);
14
14
  if (t) return t;
15
15
  try {
16
- return t = (await import("@hono/node-ws")).createNodeWebSocket({ app: e }), S.set(e, t), t;
16
+ return t = (await import("@hono/node-ws")).createNodeWebSocket({ app: e }), g.set(e, t), t;
17
17
  } catch (n) {
18
18
  return console.error("[velojs] failed to load @hono/node-ws:", n), null;
19
19
  }
20
20
  }
21
- function p(e, t) {
22
- let n = k.get(e);
23
- n || (n = /* @__PURE__ */ new Set(), k.set(e, n)), n.add(t);
24
- }
25
21
  function b(e, t) {
26
- k.get(e)?.delete(t);
22
+ let n = S.get(e);
23
+ n || (n = /* @__PURE__ */ new Set(), S.set(e, n)), n.add(t);
24
+ }
25
+ function m(e, t) {
26
+ S.get(e)?.delete(t);
27
27
  }
28
28
  function _(e, t) {
29
- return p(e, t), () => b(e, t);
29
+ return b(e, t), () => m(e, t);
30
30
  }
31
31
  function M(e) {
32
- const t = k.get(e);
32
+ const t = S.get(e);
33
33
  if (t) {
34
34
  for (const n of t)
35
35
  try {
@@ -39,64 +39,64 @@ function M(e) {
39
39
  t.clear();
40
40
  }
41
41
  }
42
- function O(e) {
43
- return g.get(e) ?? /* @__PURE__ */ new Map();
42
+ function x(e) {
43
+ return k.get(e) ?? /* @__PURE__ */ new Map();
44
44
  }
45
45
  function j(e, t, n) {
46
- const s = [];
47
- let a = null, i = !1, c = !1, o = null;
48
- const f = new AbortController();
49
- p(e, f);
46
+ const i = [];
47
+ let r = null, s = !1, c = !1, o = null;
48
+ const l = new AbortController();
49
+ b(e, l);
50
50
  const u = {
51
51
  async *[Symbol.asyncIterator]() {
52
52
  for (; ; ) {
53
- for (; s.length > 0; )
54
- yield s.shift();
55
- if (i) return;
56
- await new Promise((l) => {
57
- a = l;
58
- }), a = null;
53
+ for (; i.length > 0; )
54
+ yield i.shift();
55
+ if (s) return;
56
+ await new Promise((f) => {
57
+ r = f;
58
+ }), r = null;
59
59
  }
60
60
  }
61
- }, d = (l) => {
62
- if (!(!o || i))
61
+ }, d = (f) => {
62
+ if (!(!o || s))
63
63
  try {
64
- typeof l == "string" || l instanceof Uint8Array ? o.send(l) : o.send(JSON.stringify(l));
65
- } catch (r) {
66
- console.error("[velojs] socket send failed:", r);
64
+ typeof f == "string" || f instanceof Uint8Array ? o.send(f) : o.send(JSON.stringify(f));
65
+ } catch (a) {
66
+ console.error("[velojs] socket send failed:", a);
67
67
  }
68
- }, h = (l, r) => {
68
+ }, w = (f, a) => {
69
69
  if (o)
70
70
  try {
71
- o.close(l, r);
71
+ o.close(f, a);
72
72
  } catch {
73
73
  }
74
74
  }, y = () => {
75
75
  c = !0;
76
- }, w = () => {
77
- if (i || (i = !0, a?.()), !f.signal.aborted)
76
+ }, h = () => {
77
+ if (s || (s = !0, r?.()), !l.signal.aborted)
78
78
  try {
79
- f.abort();
79
+ l.abort();
80
80
  } catch {
81
81
  }
82
- b(e, f);
82
+ m(e, l);
83
83
  };
84
84
  return {
85
85
  events: {
86
- onOpen(l, r) {
87
- o = r, Promise.resolve(
86
+ onOpen(f, a) {
87
+ o = a, Promise.resolve(
88
88
  t({
89
89
  incoming: u,
90
90
  send: d,
91
- close: h,
91
+ close: w,
92
92
  keepOpen: y,
93
- abortSignal: f.signal,
93
+ abortSignal: l.signal,
94
94
  c: n,
95
95
  params: n.req.param(),
96
96
  query: n.req.query()
97
97
  })
98
98
  ).then(() => {
99
- if (!c && o && !i)
99
+ if (!c && o && !s)
100
100
  try {
101
101
  o.close();
102
102
  } catch {
@@ -109,33 +109,33 @@ function j(e, t, n) {
109
109
  }
110
110
  });
111
111
  },
112
- onMessage(l) {
113
- if (i) return;
114
- const r = l.data;
115
- typeof r == "string" ? s.push(r) : r instanceof ArrayBuffer ? s.push(new Uint8Array(r)) : r && typeof r == "object" && "buffer" in r && r.buffer instanceof ArrayBuffer && s.push(new Uint8Array(r.buffer)), a?.();
112
+ onMessage(f) {
113
+ if (s) return;
114
+ const a = f.data;
115
+ typeof a == "string" ? i.push(a) : a instanceof ArrayBuffer ? i.push(new Uint8Array(a)) : a && typeof a == "object" && "buffer" in a && a.buffer instanceof ArrayBuffer && i.push(new Uint8Array(a.buffer)), r?.();
116
116
  },
117
117
  onClose() {
118
- w();
118
+ h();
119
119
  },
120
120
  onError() {
121
- w();
121
+ h();
122
122
  }
123
123
  },
124
- cleanup: w
124
+ cleanup: h
125
125
  };
126
126
  }
127
- async function A(e, t, n, s = []) {
128
- let a = g.get(e);
129
- a || (a = /* @__PURE__ */ new Map(), g.set(e, a)), a.set(t, { path: t, handler: n, middlewares: s });
130
- const i = await m(e);
131
- if (!i) return;
132
- const c = i.upgradeWebSocket((o) => j(e, n, o).events);
133
- s.length > 0 ? e.on(["GET"], [t], ...s, c) : e.on(["GET"], [t], c);
127
+ async function A(e, t, n, i = []) {
128
+ let r = k.get(e);
129
+ r || (r = /* @__PURE__ */ new Map(), k.set(e, r)), r.set(t, { path: t, handler: n, middlewares: i });
130
+ const s = await p(e);
131
+ if (!s) return;
132
+ const c = s.upgradeWebSocket((o) => j(e, n, o).events);
133
+ i.length > 0 ? e.on(["GET"], [t], ...i, c) : e.on(["GET"], [t], c);
134
134
  }
135
- async function q(e, t, n = []) {
136
- const s = async (a, i) => {
137
- for (const c of a) {
138
- const o = c.module?.metadata?.moduleId, f = [...i, ...c.middlewares ?? []];
135
+ async function O(e, t, n = []) {
136
+ const i = async (r, s) => {
137
+ for (const c of r) {
138
+ const o = c.module?.metadata?.moduleId, l = [...s, ...c.middlewares ?? []];
139
139
  if (c.module && o)
140
140
  for (const u of Object.keys(c.module)) {
141
141
  if (!u.startsWith("socket_")) continue;
@@ -146,7 +146,7 @@ async function q(e, t, n = []) {
146
146
  );
147
147
  continue;
148
148
  }
149
- const h = u.replace("socket_", ""), y = `/_socket/${o}/${h}`;
149
+ const w = u.replace("socket_", ""), y = `/_socket/${o}/${w}`;
150
150
  try {
151
151
  d.__path = y;
152
152
  } catch {
@@ -155,28 +155,42 @@ async function q(e, t, n = []) {
155
155
  d.__isVeloSocket = !0;
156
156
  } catch {
157
157
  }
158
- await A(e, y, d, f);
158
+ await A(e, y, d, l);
159
159
  }
160
- c.children && await s(c.children, f);
160
+ c.children && await i(c.children, l);
161
161
  }
162
162
  };
163
- await s(t, n);
163
+ await i(t, n);
164
164
  }
165
- async function x(e, t) {
166
- const n = S.get(e);
167
- if (n)
168
- try {
169
- n.injectWebSocket(t);
170
- } catch (s) {
171
- console.error("[velojs] injectWebSocket failed:", s);
165
+ async function J(e, t) {
166
+ const n = g.get(e);
167
+ if (!n) return;
168
+ const i = new Proxy(t, {
169
+ get(r, s, c) {
170
+ return s === "on" || s === "addListener" ? (o, l) => o !== "upgrade" ? r[s](o, l) : r[s]("upgrade", (u, d, w) => {
171
+ const y = u.headers?.host ?? "localhost";
172
+ let h;
173
+ try {
174
+ h = new URL(u.url ?? "/", `http://${y}`).pathname;
175
+ } catch {
176
+ return;
177
+ }
178
+ k.get(e)?.has(h) && l(u, d, w);
179
+ }) : Reflect.get(r, s, c);
172
180
  }
181
+ });
182
+ try {
183
+ n.injectWebSocket(i);
184
+ } catch (r) {
185
+ console.error("[velojs] injectWebSocket failed:", r);
186
+ }
173
187
  }
174
188
  export {
175
189
  M as abortAllSocketSessions,
176
- O as getAppSockets,
177
- x as injectWebSocketServer,
190
+ x as getAppSockets,
191
+ J as injectWebSocketServer,
178
192
  W as parseJson,
179
193
  A as registerSocketHandler,
180
- q as registerSocketRoutes,
194
+ O as registerSocketRoutes,
181
195
  _ as trackSocketSession
182
196
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mauroandre/velojs",
3
- "version": "0.0.27",
3
+ "version": "0.0.28",
4
4
  "type": "module",
5
5
  "description": "Fullstack web framework built on Hono + Preact with Vite-powered AST transforms",
6
6
  "keywords": [