@mauroandre/velojs 0.0.25 → 0.0.27

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.
@@ -0,0 +1,13 @@
1
+ /**
2
+ * In-memory WebSocket session for tests.
3
+ *
4
+ * Invokes a `socket_*` handler directly with a mock Context — bypassing the
5
+ * real HTTP upgrade path. Middlewares do NOT run; set `opts.user` to supply
6
+ * `c.get("user")`.
7
+ */
8
+ import type { Hono } from "hono";
9
+ import type { SocketHandler } from "../sockets.js";
10
+ import type { SocketTestOptions, TestSocketSession } from "./types.js";
11
+ export declare function buildSocketSession(hono: Hono, handlerInput: SocketHandler | {
12
+ __path?: string;
13
+ } | string, opts?: SocketTestOptions): Promise<TestSocketSession>;
@@ -4,6 +4,7 @@
4
4
  import type { Hono, Context } from "hono";
5
5
  import type { AppRoutes } from "../types.js";
6
6
  import type { EventStream } from "../events.js";
7
+ import type { SocketHandler, SocketStub } from "../sockets.js";
7
8
  export type Cookies = Record<string, string>;
8
9
  export type Headers = Record<string, string>;
9
10
  export type Query = Record<string, string | string[]>;
@@ -64,6 +65,30 @@ export interface NextOptions {
64
65
  /** Reject if no event arrives within this many ms. */
65
66
  timeoutMs: number;
66
67
  }
68
+ export interface SocketTestOptions extends RequestOptions {
69
+ /** Channel ID — sent as `?channel=...` on the WS URL. */
70
+ channel?: string;
71
+ /** URL params (for socket paths with `:param`). */
72
+ params?: Params;
73
+ /** `c.get("user")` value (shortcut — avoids a full middleware chain). */
74
+ user?: any;
75
+ }
76
+ export interface TestSocketSession {
77
+ /** Send a frame. Objects are `JSON.stringify`'d, strings/Uint8Array pass through. */
78
+ send(msg: string | Uint8Array | object): void;
79
+ /** Wait for the next incoming frame from the server. Rejects on timeout. */
80
+ next(opts: NextOptions): Promise<string | Uint8Array>;
81
+ /** Wait for N incoming frames total (order preserved). */
82
+ nextN(n: number, opts: NextOptions): Promise<(string | Uint8Array)[]>;
83
+ /** All frames received from the server so far. */
84
+ readonly messages: ReadonlyArray<string | Uint8Array>;
85
+ /** True once the server-side handler has finished (or the session was aborted). */
86
+ readonly closed: boolean;
87
+ /** Client-side close — aborts the server handler's abortSignal. */
88
+ close(code?: number, reason?: string): Promise<void>;
89
+ /** Resolves when the handler finishes or the session is aborted. */
90
+ readonly done: Promise<void>;
91
+ }
67
92
  export interface TestSubscription<TEvent = any, TSnapshot = any> {
68
93
  /** HTTP status of the initial SSE response (200, 403, etc). */
69
94
  readonly status: number;
@@ -109,6 +134,17 @@ export interface TestApp {
109
134
  */
110
135
  loader<T = any>(fn: Function | string, opts?: LoaderRequestOptions): Promise<T | TestResponse>;
111
136
  subscribe<TEvent = any, TSnapshot = any>(stream: EventStream<TEvent, TSnapshot> | string, opts?: SubscribeOptions): Promise<TestSubscription<TEvent, TSnapshot>>;
137
+ /**
138
+ * Open an in-memory session against a `socket_*` handler.
139
+ *
140
+ * Accepts the socket handler function directly (server-imported), a
141
+ * `{ __path }` stub, or a string path. **Middleware is not simulated** —
142
+ * test middleware separately through a regular endpoint or page that uses
143
+ * it. Use `opts.user` to shortcut `c.get("user")`.
144
+ */
145
+ socket(handler: SocketHandler | SocketStub | {
146
+ __path: string;
147
+ } | string, opts?: SocketTestOptions): Promise<TestSocketSession>;
112
148
  /** Build cookies for a user via `getSessionCookie` config. */
113
149
  sessionCookies(input: {
114
150
  user: any;
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: RouteModule;
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
@@ -12,8 +12,26 @@ export declare function transformActionsForClient(code: string, moduleId: string
12
12
  * só rodam no server. O client precisa apenas do path para abrir o EventSource.
13
13
  */
14
14
  export declare function transformStreamsForClient(code: string, moduleId: string): string;
15
+ /**
16
+ * Transforms `export const socket_xxx = async (...) => {...}` on the client
17
+ * into a stub with only `{ __isVeloSocket: true, __path: "/_socket/{moduleId}/{name}" }`.
18
+ *
19
+ * The body is discarded — the handler runs only on the server. The client
20
+ * needs just the path to open the WebSocket.
21
+ */
22
+ export declare function transformSocketsForClient(code: string, moduleId: string): string;
15
23
  export declare function removeLoaders(code: string): string;
16
24
  export declare function removeMiddlewares(code: string): string;
25
+ /**
26
+ * On client builds, strip EndpointRoute objects from the default-export array
27
+ * of `routes.tsx` so server-only handler code (and its imports) don't ship to
28
+ * the browser. Mirrors the pattern used by `removeMiddlewares`.
29
+ *
30
+ * Detection: any ObjectExpression that has a `handler` property is treated as
31
+ * an endpoint and removed from its containing array. Imports referenced only
32
+ * by removed endpoints are dropped afterwards.
33
+ */
34
+ export declare function removeEndpointRoutes(code: string): string;
17
35
  export interface PathInfo {
18
36
  fullPath: string;
19
37
  path: string;