@richie-rpc/server 1.2.3 → 1.2.4
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 +271 -54
- package/dist/cjs/index.cjs +263 -2
- package/dist/cjs/index.cjs.map +3 -3
- package/dist/cjs/package.json +1 -1
- package/dist/mjs/index.mjs +263 -2
- package/dist/mjs/index.mjs.map +3 -3
- package/dist/mjs/package.json +1 -1
- package/dist/types/index.d.ts +96 -5
- package/dist/types/websocket.d.ts +125 -0
- package/package.json +2 -2
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import type { ExtractClientMessage, ExtractWSHeaders, ExtractWSParams, ExtractWSQuery, WebSocketContract, WebSocketContractDefinition } from '@richie-rpc/core';
|
|
2
|
+
import type { z } from 'zod';
|
|
3
|
+
/**
|
|
4
|
+
* Validation error for WebSocket messages
|
|
5
|
+
*/
|
|
6
|
+
export declare class WebSocketValidationError extends Error {
|
|
7
|
+
messageType: string;
|
|
8
|
+
issues: z.ZodIssue[];
|
|
9
|
+
constructor(messageType: string, issues: z.ZodIssue[]);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Data attached to WebSocket connections for routing
|
|
13
|
+
*/
|
|
14
|
+
export interface WebSocketData<T extends WebSocketContractDefinition = WebSocketContractDefinition, S = unknown> {
|
|
15
|
+
endpointName: string;
|
|
16
|
+
endpoint: T;
|
|
17
|
+
params: ExtractWSParams<T>;
|
|
18
|
+
query: ExtractWSQuery<T>;
|
|
19
|
+
headers: ExtractWSHeaders<T>;
|
|
20
|
+
context: unknown;
|
|
21
|
+
state: S;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Typed WebSocket wrapper for sending messages
|
|
25
|
+
*/
|
|
26
|
+
export interface TypedServerWebSocket<T extends WebSocketContractDefinition, S = unknown> {
|
|
27
|
+
/** The underlying Bun WebSocket */
|
|
28
|
+
readonly raw: WebSocket;
|
|
29
|
+
/** Send a typed message to the client */
|
|
30
|
+
send<K extends keyof T['serverMessages']>(type: K, payload: z.infer<T['serverMessages'][K]['payload']>): void;
|
|
31
|
+
/** Subscribe to a topic for pub/sub */
|
|
32
|
+
subscribe(topic: string): void;
|
|
33
|
+
/** Unsubscribe from a topic */
|
|
34
|
+
unsubscribe(topic: string): void;
|
|
35
|
+
/** Publish a message to a topic */
|
|
36
|
+
publish<K extends keyof T['serverMessages']>(topic: string, type: K, payload: z.infer<T['serverMessages'][K]['payload']>): void;
|
|
37
|
+
/** Close the connection */
|
|
38
|
+
close(code?: number, reason?: string): void;
|
|
39
|
+
/** Connection data */
|
|
40
|
+
readonly data: WebSocketData<T, S>;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Handler functions for a WebSocket endpoint
|
|
44
|
+
*/
|
|
45
|
+
export interface WebSocketEndpointHandlers<T extends WebSocketContractDefinition, C = unknown, S = unknown> {
|
|
46
|
+
/** Called when connection opens */
|
|
47
|
+
open?(ws: TypedServerWebSocket<T, S>, ctx: C): void | Promise<void>;
|
|
48
|
+
/** Called for each validated message */
|
|
49
|
+
message(ws: TypedServerWebSocket<T, S>, message: ExtractClientMessage<T>, ctx: C): void | Promise<void>;
|
|
50
|
+
/** Called when connection closes */
|
|
51
|
+
close?(ws: TypedServerWebSocket<T, S>, ctx: C): void;
|
|
52
|
+
/** Called when message validation fails */
|
|
53
|
+
validationError?(ws: TypedServerWebSocket<T, S>, error: WebSocketValidationError, ctx: C): void;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Contract handlers mapping for WebSocket endpoints
|
|
57
|
+
*/
|
|
58
|
+
export type WebSocketContractHandlers<T extends WebSocketContract, C = unknown, S = unknown> = {
|
|
59
|
+
[K in keyof T]: WebSocketEndpointHandlers<T[K], C, S>;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Upgrade data returned by matchAndPrepareUpgrade
|
|
63
|
+
*/
|
|
64
|
+
export interface UpgradeData<S = unknown> {
|
|
65
|
+
endpointName: string;
|
|
66
|
+
endpoint: WebSocketContractDefinition;
|
|
67
|
+
params: Record<string, string>;
|
|
68
|
+
query: Record<string, string | string[]>;
|
|
69
|
+
headers: Record<string, string>;
|
|
70
|
+
context: unknown;
|
|
71
|
+
state: S;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Options for WebSocket router
|
|
75
|
+
*/
|
|
76
|
+
export interface WebSocketRouterOptions<C = unknown, S = unknown> {
|
|
77
|
+
basePath?: string;
|
|
78
|
+
context?: (request: Request, endpointName: string, endpoint: WebSocketContractDefinition) => C | Promise<C>;
|
|
79
|
+
/** Type hint for per-connection state. Use `{} as YourStateType` */
|
|
80
|
+
state?: S;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Bun WebSocket handler type (subset of Bun's types)
|
|
84
|
+
*/
|
|
85
|
+
export interface BunWebSocketHandler<T = unknown> {
|
|
86
|
+
open(ws: Bun.ServerWebSocket<T>): void | Promise<void>;
|
|
87
|
+
message(ws: Bun.ServerWebSocket<T>, message: string | Buffer<ArrayBuffer>): void | Promise<void>;
|
|
88
|
+
close(ws: Bun.ServerWebSocket<T>, code: number, reason: string): void;
|
|
89
|
+
drain(ws: Bun.ServerWebSocket<T>): void;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* WebSocket router for managing WebSocket contract endpoints
|
|
93
|
+
*/
|
|
94
|
+
export declare class WebSocketRouter<T extends WebSocketContract, C = unknown, S = unknown> {
|
|
95
|
+
private contract;
|
|
96
|
+
private handlers;
|
|
97
|
+
private basePath;
|
|
98
|
+
private contextFactory?;
|
|
99
|
+
constructor(contract: T, handlers: WebSocketContractHandlers<T, C, S>, options?: WebSocketRouterOptions<C, S>);
|
|
100
|
+
/**
|
|
101
|
+
* Find matching endpoint for a path
|
|
102
|
+
*/
|
|
103
|
+
private findEndpoint;
|
|
104
|
+
/**
|
|
105
|
+
* Parse and validate upgrade request parameters
|
|
106
|
+
*/
|
|
107
|
+
private parseUpgradeParams;
|
|
108
|
+
/**
|
|
109
|
+
* Match a request and prepare upgrade data
|
|
110
|
+
* Returns null if no match, or UpgradeData for server.upgrade()
|
|
111
|
+
*/
|
|
112
|
+
matchAndPrepareUpgrade(request: Request): Promise<UpgradeData<S> | null>;
|
|
113
|
+
/**
|
|
114
|
+
* Validate an incoming client message
|
|
115
|
+
*/
|
|
116
|
+
private validateMessage;
|
|
117
|
+
/**
|
|
118
|
+
* Get Bun-compatible WebSocket handler
|
|
119
|
+
*/
|
|
120
|
+
get websocketHandler(): BunWebSocketHandler<UpgradeData<S>>;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Create a WebSocket router from a contract and handlers
|
|
124
|
+
*/
|
|
125
|
+
export declare function createWebSocketRouter<T extends WebSocketContract, C = unknown, S = unknown>(contract: T, handlers: WebSocketContractHandlers<T, C, S>, options?: WebSocketRouterOptions<C, S>): WebSocketRouter<T, C, S>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@richie-rpc/server",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.4",
|
|
4
4
|
"main": "./dist/cjs/index.cjs",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
}
|
|
11
11
|
},
|
|
12
12
|
"peerDependencies": {
|
|
13
|
-
"@richie-rpc/core": "^1.2.
|
|
13
|
+
"@richie-rpc/core": "^1.2.3",
|
|
14
14
|
"typescript": "^5",
|
|
15
15
|
"zod": "^4.1.12"
|
|
16
16
|
},
|