@richie-rpc/server 1.2.4 → 1.2.5
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/cjs/package.json +1 -1
- package/dist/cjs/websocket.cjs +243 -0
- package/dist/cjs/websocket.cjs.map +10 -0
- package/dist/mjs/package.json +1 -1
- package/dist/mjs/websocket.mjs +212 -0
- package/dist/mjs/websocket.mjs.map +10 -0
- package/package.json +2 -2
package/dist/cjs/package.json
CHANGED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
// @bun @bun-cjs
|
|
2
|
+
(function(exports, require, module, __filename, __dirname) {var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __moduleCache = /* @__PURE__ */ new WeakMap;
|
|
7
|
+
var __toCommonJS = (from) => {
|
|
8
|
+
var entry = __moduleCache.get(from), desc;
|
|
9
|
+
if (entry)
|
|
10
|
+
return entry;
|
|
11
|
+
entry = __defProp({}, "__esModule", { value: true });
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function")
|
|
13
|
+
__getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
|
|
14
|
+
get: () => from[key],
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
}));
|
|
17
|
+
__moduleCache.set(from, entry);
|
|
18
|
+
return entry;
|
|
19
|
+
};
|
|
20
|
+
var __export = (target, all) => {
|
|
21
|
+
for (var name in all)
|
|
22
|
+
__defProp(target, name, {
|
|
23
|
+
get: all[name],
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
set: (newValue) => all[name] = () => newValue
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// packages/server/websocket.ts
|
|
31
|
+
var exports_websocket = {};
|
|
32
|
+
__export(exports_websocket, {
|
|
33
|
+
createWebSocketRouter: () => createWebSocketRouter,
|
|
34
|
+
WebSocketValidationError: () => WebSocketValidationError,
|
|
35
|
+
WebSocketRouter: () => WebSocketRouter
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(exports_websocket);
|
|
38
|
+
var import_core = require("@richie-rpc/core");
|
|
39
|
+
|
|
40
|
+
class WebSocketValidationError extends Error {
|
|
41
|
+
messageType;
|
|
42
|
+
issues;
|
|
43
|
+
constructor(messageType, issues) {
|
|
44
|
+
super(`Validation failed for WebSocket message type: ${messageType}`);
|
|
45
|
+
this.messageType = messageType;
|
|
46
|
+
this.issues = issues;
|
|
47
|
+
this.name = "WebSocketValidationError";
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function createTypedWebSocket(ws) {
|
|
51
|
+
return {
|
|
52
|
+
get raw() {
|
|
53
|
+
return ws;
|
|
54
|
+
},
|
|
55
|
+
send(type, payload) {
|
|
56
|
+
ws.send(JSON.stringify({ type, payload }));
|
|
57
|
+
},
|
|
58
|
+
subscribe(topic) {
|
|
59
|
+
ws.subscribe(topic);
|
|
60
|
+
},
|
|
61
|
+
unsubscribe(topic) {
|
|
62
|
+
ws.unsubscribe(topic);
|
|
63
|
+
},
|
|
64
|
+
publish(topic, type, payload) {
|
|
65
|
+
ws.publish(topic, JSON.stringify({ type, payload }));
|
|
66
|
+
},
|
|
67
|
+
close(code, reason) {
|
|
68
|
+
ws.close(code, reason);
|
|
69
|
+
},
|
|
70
|
+
get data() {
|
|
71
|
+
return ws.data;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
class WebSocketRouter {
|
|
77
|
+
contract;
|
|
78
|
+
handlers;
|
|
79
|
+
basePath;
|
|
80
|
+
contextFactory;
|
|
81
|
+
constructor(contract, handlers, options) {
|
|
82
|
+
this.contract = contract;
|
|
83
|
+
this.handlers = handlers;
|
|
84
|
+
const bp = options?.basePath || "";
|
|
85
|
+
if (bp) {
|
|
86
|
+
this.basePath = bp.startsWith("/") ? bp : `/${bp}`;
|
|
87
|
+
this.basePath = this.basePath.endsWith("/") ? this.basePath.slice(0, -1) : this.basePath;
|
|
88
|
+
} else {
|
|
89
|
+
this.basePath = "";
|
|
90
|
+
}
|
|
91
|
+
this.contextFactory = options?.context;
|
|
92
|
+
}
|
|
93
|
+
findEndpoint(path) {
|
|
94
|
+
for (const [name, endpoint] of Object.entries(this.contract)) {
|
|
95
|
+
const params = import_core.matchPath(endpoint.path, path);
|
|
96
|
+
if (params !== null) {
|
|
97
|
+
return {
|
|
98
|
+
name,
|
|
99
|
+
endpoint,
|
|
100
|
+
params
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
parseUpgradeParams(request, endpoint, pathParams) {
|
|
107
|
+
const url = new URL(request.url);
|
|
108
|
+
let params = pathParams;
|
|
109
|
+
if (endpoint.params) {
|
|
110
|
+
const result = endpoint.params.safeParse(pathParams);
|
|
111
|
+
if (!result.success) {
|
|
112
|
+
throw new Error(`Invalid path params: ${result.error.message}`);
|
|
113
|
+
}
|
|
114
|
+
params = result.data;
|
|
115
|
+
}
|
|
116
|
+
let query = {};
|
|
117
|
+
if (endpoint.query) {
|
|
118
|
+
const queryData = import_core.parseQuery(url.searchParams);
|
|
119
|
+
const result = endpoint.query.safeParse(queryData);
|
|
120
|
+
if (!result.success) {
|
|
121
|
+
throw new Error(`Invalid query params: ${result.error.message}`);
|
|
122
|
+
}
|
|
123
|
+
query = result.data;
|
|
124
|
+
}
|
|
125
|
+
let headers = {};
|
|
126
|
+
if (endpoint.headers) {
|
|
127
|
+
const headersObj = {};
|
|
128
|
+
request.headers.forEach((value, key) => {
|
|
129
|
+
headersObj[key] = value;
|
|
130
|
+
});
|
|
131
|
+
const result = endpoint.headers.safeParse(headersObj);
|
|
132
|
+
if (!result.success) {
|
|
133
|
+
throw new Error(`Invalid headers: ${result.error.message}`);
|
|
134
|
+
}
|
|
135
|
+
headers = result.data;
|
|
136
|
+
}
|
|
137
|
+
return { params, query, headers };
|
|
138
|
+
}
|
|
139
|
+
async matchAndPrepareUpgrade(request) {
|
|
140
|
+
const url = new URL(request.url);
|
|
141
|
+
let path = url.pathname;
|
|
142
|
+
if (this.basePath && path.startsWith(this.basePath)) {
|
|
143
|
+
path = path.slice(this.basePath.length) || "/";
|
|
144
|
+
}
|
|
145
|
+
const match = this.findEndpoint(path);
|
|
146
|
+
if (!match) {
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
const { name, endpoint, params: rawParams } = match;
|
|
150
|
+
try {
|
|
151
|
+
const { params, query, headers } = this.parseUpgradeParams(request, endpoint, rawParams);
|
|
152
|
+
const context = this.contextFactory ? await this.contextFactory(request, String(name), endpoint) : undefined;
|
|
153
|
+
return {
|
|
154
|
+
endpointName: String(name),
|
|
155
|
+
endpoint,
|
|
156
|
+
params,
|
|
157
|
+
query,
|
|
158
|
+
headers,
|
|
159
|
+
context,
|
|
160
|
+
state: {}
|
|
161
|
+
};
|
|
162
|
+
} catch (err) {
|
|
163
|
+
console.error("WebSocket upgrade validation failed:", err);
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
validateMessage(endpoint, rawMessage) {
|
|
168
|
+
const messageStr = typeof rawMessage === "string" ? rawMessage : new TextDecoder().decode(rawMessage);
|
|
169
|
+
const parsed = JSON.parse(messageStr);
|
|
170
|
+
const { type, payload } = parsed;
|
|
171
|
+
const messageDef = endpoint.clientMessages[type];
|
|
172
|
+
if (!messageDef) {
|
|
173
|
+
throw new WebSocketValidationError(type, [
|
|
174
|
+
{
|
|
175
|
+
code: "custom",
|
|
176
|
+
path: ["type"],
|
|
177
|
+
message: `Unknown message type: ${type}`
|
|
178
|
+
}
|
|
179
|
+
]);
|
|
180
|
+
}
|
|
181
|
+
const result = messageDef.payload.safeParse(payload);
|
|
182
|
+
if (!result.success) {
|
|
183
|
+
throw new WebSocketValidationError(type, result.error.issues);
|
|
184
|
+
}
|
|
185
|
+
return { type, payload: result.data };
|
|
186
|
+
}
|
|
187
|
+
get websocketHandler() {
|
|
188
|
+
return {
|
|
189
|
+
open: async (ws) => {
|
|
190
|
+
const data = ws.data;
|
|
191
|
+
const endpointHandlers = this.handlers[data.endpointName];
|
|
192
|
+
if (!endpointHandlers)
|
|
193
|
+
return;
|
|
194
|
+
const typedWs = createTypedWebSocket(ws);
|
|
195
|
+
if (endpointHandlers.open) {
|
|
196
|
+
await endpointHandlers.open(typedWs, data.context);
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
message: async (ws, message) => {
|
|
200
|
+
const data = ws.data;
|
|
201
|
+
const endpointHandlers = this.handlers[data.endpointName];
|
|
202
|
+
if (!endpointHandlers)
|
|
203
|
+
return;
|
|
204
|
+
const typedWs = createTypedWebSocket(ws);
|
|
205
|
+
try {
|
|
206
|
+
const validatedMessage = this.validateMessage(data.endpoint, typeof message === "string" ? message : message.buffer);
|
|
207
|
+
await endpointHandlers.message(typedWs, validatedMessage, data.context);
|
|
208
|
+
} catch (err) {
|
|
209
|
+
if (err instanceof WebSocketValidationError) {
|
|
210
|
+
if (endpointHandlers.validationError) {
|
|
211
|
+
endpointHandlers.validationError(typedWs, err, data.context);
|
|
212
|
+
} else {
|
|
213
|
+
typedWs.send("error", {
|
|
214
|
+
code: "VALIDATION_ERROR",
|
|
215
|
+
message: err.message,
|
|
216
|
+
issues: err.issues
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
} else {
|
|
220
|
+
console.error("WebSocket message handler error:", err);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
},
|
|
224
|
+
close: (ws, _code, _reason) => {
|
|
225
|
+
const data = ws.data;
|
|
226
|
+
const endpointHandlers = this.handlers[data.endpointName];
|
|
227
|
+
if (!endpointHandlers)
|
|
228
|
+
return;
|
|
229
|
+
const typedWs = createTypedWebSocket(ws);
|
|
230
|
+
if (endpointHandlers.close) {
|
|
231
|
+
endpointHandlers.close(typedWs, data.context);
|
|
232
|
+
}
|
|
233
|
+
},
|
|
234
|
+
drain: () => {}
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
function createWebSocketRouter(contract, handlers, options) {
|
|
239
|
+
return new WebSocketRouter(contract, handlers, options);
|
|
240
|
+
}
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
//# debugId=6D55F1711647622B64756E2164756E21
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../websocket.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import type {\n ExtractClientMessage,\n ExtractWSHeaders,\n ExtractWSParams,\n ExtractWSQuery,\n WebSocketContract,\n WebSocketContractDefinition,\n} from '@richie-rpc/core';\nimport { matchPath, parseQuery } from '@richie-rpc/core';\nimport type { z } from 'zod';\n\n/**\n * Validation error for WebSocket messages\n */\nexport class WebSocketValidationError extends Error {\n constructor(\n public messageType: string,\n public issues: z.ZodIssue[],\n ) {\n super(`Validation failed for WebSocket message type: ${messageType}`);\n this.name = 'WebSocketValidationError';\n }\n}\n\n/**\n * Data attached to WebSocket connections for routing\n */\nexport interface WebSocketData<\n T extends WebSocketContractDefinition = WebSocketContractDefinition,\n S = unknown,\n> {\n endpointName: string;\n endpoint: T;\n params: ExtractWSParams<T>;\n query: ExtractWSQuery<T>;\n headers: ExtractWSHeaders<T>;\n context: unknown;\n state: S;\n}\n\n/**\n * Typed WebSocket wrapper for sending messages\n */\nexport interface TypedServerWebSocket<T extends WebSocketContractDefinition, S = unknown> {\n /** The underlying Bun WebSocket */\n readonly raw: WebSocket;\n /** Send a typed message to the client */\n send<K extends keyof T['serverMessages']>(\n type: K,\n payload: z.infer<T['serverMessages'][K]['payload']>,\n ): void;\n /** Subscribe to a topic for pub/sub */\n subscribe(topic: string): void;\n /** Unsubscribe from a topic */\n unsubscribe(topic: string): void;\n /** Publish a message to a topic */\n publish<K extends keyof T['serverMessages']>(\n topic: string,\n type: K,\n payload: z.infer<T['serverMessages'][K]['payload']>,\n ): void;\n /** Close the connection */\n close(code?: number, reason?: string): void;\n /** Connection data */\n readonly data: WebSocketData<T, S>;\n}\n\n/**\n * Handler functions for a WebSocket endpoint\n */\nexport interface WebSocketEndpointHandlers<\n T extends WebSocketContractDefinition,\n C = unknown,\n S = unknown,\n> {\n /** Called when connection opens */\n open?(ws: TypedServerWebSocket<T, S>, ctx: C): void | Promise<void>;\n /** Called for each validated message */\n message(\n ws: TypedServerWebSocket<T, S>,\n message: ExtractClientMessage<T>,\n ctx: C,\n ): void | Promise<void>;\n /** Called when connection closes */\n close?(ws: TypedServerWebSocket<T, S>, ctx: C): void;\n /** Called when message validation fails */\n validationError?(ws: TypedServerWebSocket<T, S>, error: WebSocketValidationError, ctx: C): void;\n}\n\n/**\n * Contract handlers mapping for WebSocket endpoints\n */\nexport type WebSocketContractHandlers<T extends WebSocketContract, C = unknown, S = unknown> = {\n [K in keyof T]: WebSocketEndpointHandlers<T[K], C, S>;\n};\n\n/**\n * Upgrade data returned by matchAndPrepareUpgrade\n */\nexport interface UpgradeData<S = unknown> {\n endpointName: string;\n endpoint: WebSocketContractDefinition;\n params: Record<string, string>;\n query: Record<string, string | string[]>;\n headers: Record<string, string>;\n context: unknown;\n state: S;\n}\n\n/**\n * Options for WebSocket router\n */\nexport interface WebSocketRouterOptions<C = unknown, S = unknown> {\n basePath?: string;\n context?: (\n request: Request,\n endpointName: string,\n endpoint: WebSocketContractDefinition,\n ) => C | Promise<C>;\n /** Type hint for per-connection state. Use `{} as YourStateType` */\n state?: S;\n}\n\n/**\n * Bun WebSocket handler type (subset of Bun's types)\n */\nexport interface BunWebSocketHandler<T = unknown> {\n open(ws: Bun.ServerWebSocket<T>): void | Promise<void>;\n message(ws: Bun.ServerWebSocket<T>, message: string | Buffer<ArrayBuffer>): void | Promise<void>;\n close(ws: Bun.ServerWebSocket<T>, code: number, reason: string): void;\n drain(ws: Bun.ServerWebSocket<T>): void;\n}\n\n/**\n * Create a typed WebSocket wrapper\n */\nfunction createTypedWebSocket<T extends WebSocketContractDefinition, S = unknown>(\n ws: WebSocket & { data: WebSocketData<T, S> },\n): TypedServerWebSocket<T, S> {\n return {\n get raw() {\n return ws;\n },\n send(type, payload) {\n ws.send(JSON.stringify({ type, payload }));\n },\n subscribe(topic) {\n (ws as any).subscribe(topic);\n },\n unsubscribe(topic) {\n (ws as any).unsubscribe(topic);\n },\n publish(topic, type, payload) {\n (ws as any).publish(topic, JSON.stringify({ type, payload }));\n },\n close(code, reason) {\n ws.close(code, reason);\n },\n get data() {\n return ws.data;\n },\n };\n}\n\n/**\n * WebSocket router for managing WebSocket contract endpoints\n */\nexport class WebSocketRouter<T extends WebSocketContract, C = unknown, S = unknown> {\n private basePath: string;\n private contextFactory?: (\n request: Request,\n endpointName: string,\n endpoint: WebSocketContractDefinition,\n ) => C | Promise<C>;\n\n constructor(\n private contract: T,\n private handlers: WebSocketContractHandlers<T, C, S>,\n options?: WebSocketRouterOptions<C, S>,\n ) {\n // Normalize basePath\n const bp = options?.basePath || '';\n if (bp) {\n this.basePath = bp.startsWith('/') ? bp : `/${bp}`;\n this.basePath = this.basePath.endsWith('/') ? this.basePath.slice(0, -1) : this.basePath;\n } else {\n this.basePath = '';\n }\n this.contextFactory = options?.context;\n }\n\n /**\n * Find matching endpoint for a path\n */\n private findEndpoint(path: string): {\n name: keyof T;\n endpoint: WebSocketContractDefinition;\n params: Record<string, string>;\n } | null {\n for (const [name, endpoint] of Object.entries(this.contract)) {\n const params = matchPath(endpoint.path, path);\n if (params !== null) {\n return {\n name,\n endpoint: endpoint as WebSocketContractDefinition,\n params,\n };\n }\n }\n return null;\n }\n\n /**\n * Parse and validate upgrade request parameters\n */\n private parseUpgradeParams(\n request: Request,\n endpoint: WebSocketContractDefinition,\n pathParams: Record<string, string>,\n ): { params: any; query: any; headers: any } {\n const url = new URL(request.url);\n\n // Parse and validate path params\n let params: any = pathParams;\n if (endpoint.params) {\n const result = endpoint.params.safeParse(pathParams);\n if (!result.success) {\n throw new Error(`Invalid path params: ${result.error.message}`);\n }\n params = result.data;\n }\n\n // Parse and validate query params\n let query: any = {};\n if (endpoint.query) {\n const queryData = parseQuery(url.searchParams);\n const result = endpoint.query.safeParse(queryData);\n if (!result.success) {\n throw new Error(`Invalid query params: ${result.error.message}`);\n }\n query = result.data;\n }\n\n // Parse and validate headers\n let headers: any = {};\n if (endpoint.headers) {\n const headersObj: Record<string, string> = {};\n request.headers.forEach((value, key) => {\n headersObj[key] = value;\n });\n const result = endpoint.headers.safeParse(headersObj);\n if (!result.success) {\n throw new Error(`Invalid headers: ${result.error.message}`);\n }\n headers = result.data;\n }\n\n return { params, query, headers };\n }\n\n /**\n * Match a request and prepare upgrade data\n * Returns null if no match, or UpgradeData for server.upgrade()\n */\n async matchAndPrepareUpgrade(request: Request): Promise<UpgradeData<S> | null> {\n const url = new URL(request.url);\n let path = url.pathname;\n\n // Strip basePath if configured\n if (this.basePath && path.startsWith(this.basePath)) {\n path = path.slice(this.basePath.length) || '/';\n }\n\n const match = this.findEndpoint(path);\n if (!match) {\n return null;\n }\n\n const { name, endpoint, params: rawParams } = match;\n\n try {\n const { params, query, headers } = this.parseUpgradeParams(request, endpoint, rawParams);\n\n // Create context if factory provided\n const context = this.contextFactory\n ? await this.contextFactory(request, String(name), endpoint)\n : (undefined as C);\n\n return {\n endpointName: String(name),\n endpoint,\n params,\n query,\n headers,\n context,\n state: {} as S,\n };\n } catch (err) {\n // Validation failed during upgrade\n console.error('WebSocket upgrade validation failed:', err);\n return null;\n }\n }\n\n /**\n * Validate an incoming client message\n */\n private validateMessage(\n endpoint: WebSocketContractDefinition,\n rawMessage: string | ArrayBuffer,\n ): ExtractClientMessage<typeof endpoint> {\n // Parse message\n const messageStr =\n typeof rawMessage === 'string' ? rawMessage : new TextDecoder().decode(rawMessage);\n const parsed = JSON.parse(messageStr) as { type: string; payload: unknown };\n\n const { type, payload } = parsed;\n\n // Find the schema for this message type\n const messageDef = endpoint.clientMessages[type];\n if (!messageDef) {\n throw new WebSocketValidationError(type, [\n {\n code: 'custom',\n path: ['type'],\n message: `Unknown message type: ${type}`,\n },\n ]);\n }\n\n // Validate payload\n const result = messageDef.payload.safeParse(payload);\n if (!result.success) {\n throw new WebSocketValidationError(type, result.error.issues);\n }\n\n return { type, payload: result.data } as ExtractClientMessage<typeof endpoint>;\n }\n\n /**\n * Get Bun-compatible WebSocket handler\n */\n get websocketHandler(): BunWebSocketHandler<UpgradeData<S>> {\n return {\n open: async (ws) => {\n const data = ws.data;\n const endpointHandlers = this.handlers[data.endpointName as keyof T];\n if (!endpointHandlers) return;\n\n // Create typed wrapper with properly typed data\n const typedWs = createTypedWebSocket(\n ws as unknown as WebSocket & {\n data: WebSocketData<WebSocketContractDefinition, S>;\n },\n );\n\n if (endpointHandlers.open) {\n await endpointHandlers.open(typedWs as any, data.context as C);\n }\n },\n\n message: async (ws, message) => {\n const data = ws.data;\n const endpointHandlers = this.handlers[data.endpointName as keyof T];\n if (!endpointHandlers) return;\n\n const typedWs = createTypedWebSocket(\n ws as unknown as WebSocket & {\n data: WebSocketData<WebSocketContractDefinition, S>;\n },\n );\n\n try {\n // Validate the message\n const validatedMessage = this.validateMessage(\n data.endpoint,\n typeof message === 'string' ? message : message.buffer,\n );\n\n // Call handler with validated message\n await endpointHandlers.message(\n typedWs as any,\n validatedMessage as any,\n data.context as C,\n );\n } catch (err) {\n if (err instanceof WebSocketValidationError) {\n // Call validation error handler if provided\n if (endpointHandlers.validationError) {\n endpointHandlers.validationError(typedWs as any, err, data.context as C);\n } else {\n // Default: send error message back\n typedWs.send(\n 'error' as any,\n {\n code: 'VALIDATION_ERROR',\n message: err.message,\n issues: err.issues,\n } as any,\n );\n }\n } else {\n console.error('WebSocket message handler error:', err);\n }\n }\n },\n\n close: (ws, _code, _reason) => {\n const data = ws.data;\n const endpointHandlers = this.handlers[data.endpointName as keyof T];\n if (!endpointHandlers) return;\n\n const typedWs = createTypedWebSocket(\n ws as unknown as WebSocket & {\n data: WebSocketData<WebSocketContractDefinition, S>;\n },\n );\n\n if (endpointHandlers.close) {\n endpointHandlers.close(typedWs as any, data.context as C);\n }\n },\n drain: () => {\n // not used\n },\n };\n }\n}\n\n/**\n * Create a WebSocket router from a contract and handlers\n */\nexport function createWebSocketRouter<T extends WebSocketContract, C = unknown, S = unknown>(\n contract: T,\n handlers: WebSocketContractHandlers<T, C, S>,\n options?: WebSocketRouterOptions<C, S>,\n): WebSocketRouter<T, C, S> {\n return new WebSocketRouter(contract, handlers, options);\n}\n"
|
|
6
|
+
],
|
|
7
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAQsC,IAAtC;AAAA;AAMO,MAAM,iCAAiC,MAAM;AAAA,EAEzC;AAAA,EACA;AAAA,EAFT,WAAW,CACF,aACA,QACP;AAAA,IACA,MAAM,iDAAiD,aAAa;AAAA,IAH7D;AAAA,IACA;AAAA,IAGP,KAAK,OAAO;AAAA;AAEhB;AAkHA,SAAS,oBAAwE,CAC/E,IAC4B;AAAA,EAC5B,OAAO;AAAA,QACD,GAAG,GAAG;AAAA,MACR,OAAO;AAAA;AAAA,IAET,IAAI,CAAC,MAAM,SAAS;AAAA,MAClB,GAAG,KAAK,KAAK,UAAU,EAAE,MAAM,QAAQ,CAAC,CAAC;AAAA;AAAA,IAE3C,SAAS,CAAC,OAAO;AAAA,MACd,GAAW,UAAU,KAAK;AAAA;AAAA,IAE7B,WAAW,CAAC,OAAO;AAAA,MAChB,GAAW,YAAY,KAAK;AAAA;AAAA,IAE/B,OAAO,CAAC,OAAO,MAAM,SAAS;AAAA,MAC3B,GAAW,QAAQ,OAAO,KAAK,UAAU,EAAE,MAAM,QAAQ,CAAC,CAAC;AAAA;AAAA,IAE9D,KAAK,CAAC,MAAM,QAAQ;AAAA,MACl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un
|
|
2
|
+
// packages/server/websocket.ts
|
|
3
|
+
import { matchPath, parseQuery } from "@richie-rpc/core";
|
|
4
|
+
|
|
5
|
+
class WebSocketValidationError extends Error {
|
|
6
|
+
messageType;
|
|
7
|
+
issues;
|
|
8
|
+
constructor(messageType, issues) {
|
|
9
|
+
super(`Validation failed for WebSocket message type: ${messageType}`);
|
|
10
|
+
this.messageType = messageType;
|
|
11
|
+
this.issues = issues;
|
|
12
|
+
this.name = "WebSocketValidationError";
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function createTypedWebSocket(ws) {
|
|
16
|
+
return {
|
|
17
|
+
get raw() {
|
|
18
|
+
return ws;
|
|
19
|
+
},
|
|
20
|
+
send(type, payload) {
|
|
21
|
+
ws.send(JSON.stringify({ type, payload }));
|
|
22
|
+
},
|
|
23
|
+
subscribe(topic) {
|
|
24
|
+
ws.subscribe(topic);
|
|
25
|
+
},
|
|
26
|
+
unsubscribe(topic) {
|
|
27
|
+
ws.unsubscribe(topic);
|
|
28
|
+
},
|
|
29
|
+
publish(topic, type, payload) {
|
|
30
|
+
ws.publish(topic, JSON.stringify({ type, payload }));
|
|
31
|
+
},
|
|
32
|
+
close(code, reason) {
|
|
33
|
+
ws.close(code, reason);
|
|
34
|
+
},
|
|
35
|
+
get data() {
|
|
36
|
+
return ws.data;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
class WebSocketRouter {
|
|
42
|
+
contract;
|
|
43
|
+
handlers;
|
|
44
|
+
basePath;
|
|
45
|
+
contextFactory;
|
|
46
|
+
constructor(contract, handlers, options) {
|
|
47
|
+
this.contract = contract;
|
|
48
|
+
this.handlers = handlers;
|
|
49
|
+
const bp = options?.basePath || "";
|
|
50
|
+
if (bp) {
|
|
51
|
+
this.basePath = bp.startsWith("/") ? bp : `/${bp}`;
|
|
52
|
+
this.basePath = this.basePath.endsWith("/") ? this.basePath.slice(0, -1) : this.basePath;
|
|
53
|
+
} else {
|
|
54
|
+
this.basePath = "";
|
|
55
|
+
}
|
|
56
|
+
this.contextFactory = options?.context;
|
|
57
|
+
}
|
|
58
|
+
findEndpoint(path) {
|
|
59
|
+
for (const [name, endpoint] of Object.entries(this.contract)) {
|
|
60
|
+
const params = matchPath(endpoint.path, path);
|
|
61
|
+
if (params !== null) {
|
|
62
|
+
return {
|
|
63
|
+
name,
|
|
64
|
+
endpoint,
|
|
65
|
+
params
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
parseUpgradeParams(request, endpoint, pathParams) {
|
|
72
|
+
const url = new URL(request.url);
|
|
73
|
+
let params = pathParams;
|
|
74
|
+
if (endpoint.params) {
|
|
75
|
+
const result = endpoint.params.safeParse(pathParams);
|
|
76
|
+
if (!result.success) {
|
|
77
|
+
throw new Error(`Invalid path params: ${result.error.message}`);
|
|
78
|
+
}
|
|
79
|
+
params = result.data;
|
|
80
|
+
}
|
|
81
|
+
let query = {};
|
|
82
|
+
if (endpoint.query) {
|
|
83
|
+
const queryData = parseQuery(url.searchParams);
|
|
84
|
+
const result = endpoint.query.safeParse(queryData);
|
|
85
|
+
if (!result.success) {
|
|
86
|
+
throw new Error(`Invalid query params: ${result.error.message}`);
|
|
87
|
+
}
|
|
88
|
+
query = result.data;
|
|
89
|
+
}
|
|
90
|
+
let headers = {};
|
|
91
|
+
if (endpoint.headers) {
|
|
92
|
+
const headersObj = {};
|
|
93
|
+
request.headers.forEach((value, key) => {
|
|
94
|
+
headersObj[key] = value;
|
|
95
|
+
});
|
|
96
|
+
const result = endpoint.headers.safeParse(headersObj);
|
|
97
|
+
if (!result.success) {
|
|
98
|
+
throw new Error(`Invalid headers: ${result.error.message}`);
|
|
99
|
+
}
|
|
100
|
+
headers = result.data;
|
|
101
|
+
}
|
|
102
|
+
return { params, query, headers };
|
|
103
|
+
}
|
|
104
|
+
async matchAndPrepareUpgrade(request) {
|
|
105
|
+
const url = new URL(request.url);
|
|
106
|
+
let path = url.pathname;
|
|
107
|
+
if (this.basePath && path.startsWith(this.basePath)) {
|
|
108
|
+
path = path.slice(this.basePath.length) || "/";
|
|
109
|
+
}
|
|
110
|
+
const match = this.findEndpoint(path);
|
|
111
|
+
if (!match) {
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
const { name, endpoint, params: rawParams } = match;
|
|
115
|
+
try {
|
|
116
|
+
const { params, query, headers } = this.parseUpgradeParams(request, endpoint, rawParams);
|
|
117
|
+
const context = this.contextFactory ? await this.contextFactory(request, String(name), endpoint) : undefined;
|
|
118
|
+
return {
|
|
119
|
+
endpointName: String(name),
|
|
120
|
+
endpoint,
|
|
121
|
+
params,
|
|
122
|
+
query,
|
|
123
|
+
headers,
|
|
124
|
+
context,
|
|
125
|
+
state: {}
|
|
126
|
+
};
|
|
127
|
+
} catch (err) {
|
|
128
|
+
console.error("WebSocket upgrade validation failed:", err);
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
validateMessage(endpoint, rawMessage) {
|
|
133
|
+
const messageStr = typeof rawMessage === "string" ? rawMessage : new TextDecoder().decode(rawMessage);
|
|
134
|
+
const parsed = JSON.parse(messageStr);
|
|
135
|
+
const { type, payload } = parsed;
|
|
136
|
+
const messageDef = endpoint.clientMessages[type];
|
|
137
|
+
if (!messageDef) {
|
|
138
|
+
throw new WebSocketValidationError(type, [
|
|
139
|
+
{
|
|
140
|
+
code: "custom",
|
|
141
|
+
path: ["type"],
|
|
142
|
+
message: `Unknown message type: ${type}`
|
|
143
|
+
}
|
|
144
|
+
]);
|
|
145
|
+
}
|
|
146
|
+
const result = messageDef.payload.safeParse(payload);
|
|
147
|
+
if (!result.success) {
|
|
148
|
+
throw new WebSocketValidationError(type, result.error.issues);
|
|
149
|
+
}
|
|
150
|
+
return { type, payload: result.data };
|
|
151
|
+
}
|
|
152
|
+
get websocketHandler() {
|
|
153
|
+
return {
|
|
154
|
+
open: async (ws) => {
|
|
155
|
+
const data = ws.data;
|
|
156
|
+
const endpointHandlers = this.handlers[data.endpointName];
|
|
157
|
+
if (!endpointHandlers)
|
|
158
|
+
return;
|
|
159
|
+
const typedWs = createTypedWebSocket(ws);
|
|
160
|
+
if (endpointHandlers.open) {
|
|
161
|
+
await endpointHandlers.open(typedWs, data.context);
|
|
162
|
+
}
|
|
163
|
+
},
|
|
164
|
+
message: async (ws, message) => {
|
|
165
|
+
const data = ws.data;
|
|
166
|
+
const endpointHandlers = this.handlers[data.endpointName];
|
|
167
|
+
if (!endpointHandlers)
|
|
168
|
+
return;
|
|
169
|
+
const typedWs = createTypedWebSocket(ws);
|
|
170
|
+
try {
|
|
171
|
+
const validatedMessage = this.validateMessage(data.endpoint, typeof message === "string" ? message : message.buffer);
|
|
172
|
+
await endpointHandlers.message(typedWs, validatedMessage, data.context);
|
|
173
|
+
} catch (err) {
|
|
174
|
+
if (err instanceof WebSocketValidationError) {
|
|
175
|
+
if (endpointHandlers.validationError) {
|
|
176
|
+
endpointHandlers.validationError(typedWs, err, data.context);
|
|
177
|
+
} else {
|
|
178
|
+
typedWs.send("error", {
|
|
179
|
+
code: "VALIDATION_ERROR",
|
|
180
|
+
message: err.message,
|
|
181
|
+
issues: err.issues
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
} else {
|
|
185
|
+
console.error("WebSocket message handler error:", err);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
},
|
|
189
|
+
close: (ws, _code, _reason) => {
|
|
190
|
+
const data = ws.data;
|
|
191
|
+
const endpointHandlers = this.handlers[data.endpointName];
|
|
192
|
+
if (!endpointHandlers)
|
|
193
|
+
return;
|
|
194
|
+
const typedWs = createTypedWebSocket(ws);
|
|
195
|
+
if (endpointHandlers.close) {
|
|
196
|
+
endpointHandlers.close(typedWs, data.context);
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
drain: () => {}
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
function createWebSocketRouter(contract, handlers, options) {
|
|
204
|
+
return new WebSocketRouter(contract, handlers, options);
|
|
205
|
+
}
|
|
206
|
+
export {
|
|
207
|
+
createWebSocketRouter,
|
|
208
|
+
WebSocketValidationError,
|
|
209
|
+
WebSocketRouter
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
//# debugId=47CE29C068793DD164756E2164756E21
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../websocket.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import type {\n ExtractClientMessage,\n ExtractWSHeaders,\n ExtractWSParams,\n ExtractWSQuery,\n WebSocketContract,\n WebSocketContractDefinition,\n} from '@richie-rpc/core';\nimport { matchPath, parseQuery } from '@richie-rpc/core';\nimport type { z } from 'zod';\n\n/**\n * Validation error for WebSocket messages\n */\nexport class WebSocketValidationError extends Error {\n constructor(\n public messageType: string,\n public issues: z.ZodIssue[],\n ) {\n super(`Validation failed for WebSocket message type: ${messageType}`);\n this.name = 'WebSocketValidationError';\n }\n}\n\n/**\n * Data attached to WebSocket connections for routing\n */\nexport interface WebSocketData<\n T extends WebSocketContractDefinition = WebSocketContractDefinition,\n S = unknown,\n> {\n endpointName: string;\n endpoint: T;\n params: ExtractWSParams<T>;\n query: ExtractWSQuery<T>;\n headers: ExtractWSHeaders<T>;\n context: unknown;\n state: S;\n}\n\n/**\n * Typed WebSocket wrapper for sending messages\n */\nexport interface TypedServerWebSocket<T extends WebSocketContractDefinition, S = unknown> {\n /** The underlying Bun WebSocket */\n readonly raw: WebSocket;\n /** Send a typed message to the client */\n send<K extends keyof T['serverMessages']>(\n type: K,\n payload: z.infer<T['serverMessages'][K]['payload']>,\n ): void;\n /** Subscribe to a topic for pub/sub */\n subscribe(topic: string): void;\n /** Unsubscribe from a topic */\n unsubscribe(topic: string): void;\n /** Publish a message to a topic */\n publish<K extends keyof T['serverMessages']>(\n topic: string,\n type: K,\n payload: z.infer<T['serverMessages'][K]['payload']>,\n ): void;\n /** Close the connection */\n close(code?: number, reason?: string): void;\n /** Connection data */\n readonly data: WebSocketData<T, S>;\n}\n\n/**\n * Handler functions for a WebSocket endpoint\n */\nexport interface WebSocketEndpointHandlers<\n T extends WebSocketContractDefinition,\n C = unknown,\n S = unknown,\n> {\n /** Called when connection opens */\n open?(ws: TypedServerWebSocket<T, S>, ctx: C): void | Promise<void>;\n /** Called for each validated message */\n message(\n ws: TypedServerWebSocket<T, S>,\n message: ExtractClientMessage<T>,\n ctx: C,\n ): void | Promise<void>;\n /** Called when connection closes */\n close?(ws: TypedServerWebSocket<T, S>, ctx: C): void;\n /** Called when message validation fails */\n validationError?(ws: TypedServerWebSocket<T, S>, error: WebSocketValidationError, ctx: C): void;\n}\n\n/**\n * Contract handlers mapping for WebSocket endpoints\n */\nexport type WebSocketContractHandlers<T extends WebSocketContract, C = unknown, S = unknown> = {\n [K in keyof T]: WebSocketEndpointHandlers<T[K], C, S>;\n};\n\n/**\n * Upgrade data returned by matchAndPrepareUpgrade\n */\nexport interface UpgradeData<S = unknown> {\n endpointName: string;\n endpoint: WebSocketContractDefinition;\n params: Record<string, string>;\n query: Record<string, string | string[]>;\n headers: Record<string, string>;\n context: unknown;\n state: S;\n}\n\n/**\n * Options for WebSocket router\n */\nexport interface WebSocketRouterOptions<C = unknown, S = unknown> {\n basePath?: string;\n context?: (\n request: Request,\n endpointName: string,\n endpoint: WebSocketContractDefinition,\n ) => C | Promise<C>;\n /** Type hint for per-connection state. Use `{} as YourStateType` */\n state?: S;\n}\n\n/**\n * Bun WebSocket handler type (subset of Bun's types)\n */\nexport interface BunWebSocketHandler<T = unknown> {\n open(ws: Bun.ServerWebSocket<T>): void | Promise<void>;\n message(ws: Bun.ServerWebSocket<T>, message: string | Buffer<ArrayBuffer>): void | Promise<void>;\n close(ws: Bun.ServerWebSocket<T>, code: number, reason: string): void;\n drain(ws: Bun.ServerWebSocket<T>): void;\n}\n\n/**\n * Create a typed WebSocket wrapper\n */\nfunction createTypedWebSocket<T extends WebSocketContractDefinition, S = unknown>(\n ws: WebSocket & { data: WebSocketData<T, S> },\n): TypedServerWebSocket<T, S> {\n return {\n get raw() {\n return ws;\n },\n send(type, payload) {\n ws.send(JSON.stringify({ type, payload }));\n },\n subscribe(topic) {\n (ws as any).subscribe(topic);\n },\n unsubscribe(topic) {\n (ws as any).unsubscribe(topic);\n },\n publish(topic, type, payload) {\n (ws as any).publish(topic, JSON.stringify({ type, payload }));\n },\n close(code, reason) {\n ws.close(code, reason);\n },\n get data() {\n return ws.data;\n },\n };\n}\n\n/**\n * WebSocket router for managing WebSocket contract endpoints\n */\nexport class WebSocketRouter<T extends WebSocketContract, C = unknown, S = unknown> {\n private basePath: string;\n private contextFactory?: (\n request: Request,\n endpointName: string,\n endpoint: WebSocketContractDefinition,\n ) => C | Promise<C>;\n\n constructor(\n private contract: T,\n private handlers: WebSocketContractHandlers<T, C, S>,\n options?: WebSocketRouterOptions<C, S>,\n ) {\n // Normalize basePath\n const bp = options?.basePath || '';\n if (bp) {\n this.basePath = bp.startsWith('/') ? bp : `/${bp}`;\n this.basePath = this.basePath.endsWith('/') ? this.basePath.slice(0, -1) : this.basePath;\n } else {\n this.basePath = '';\n }\n this.contextFactory = options?.context;\n }\n\n /**\n * Find matching endpoint for a path\n */\n private findEndpoint(path: string): {\n name: keyof T;\n endpoint: WebSocketContractDefinition;\n params: Record<string, string>;\n } | null {\n for (const [name, endpoint] of Object.entries(this.contract)) {\n const params = matchPath(endpoint.path, path);\n if (params !== null) {\n return {\n name,\n endpoint: endpoint as WebSocketContractDefinition,\n params,\n };\n }\n }\n return null;\n }\n\n /**\n * Parse and validate upgrade request parameters\n */\n private parseUpgradeParams(\n request: Request,\n endpoint: WebSocketContractDefinition,\n pathParams: Record<string, string>,\n ): { params: any; query: any; headers: any } {\n const url = new URL(request.url);\n\n // Parse and validate path params\n let params: any = pathParams;\n if (endpoint.params) {\n const result = endpoint.params.safeParse(pathParams);\n if (!result.success) {\n throw new Error(`Invalid path params: ${result.error.message}`);\n }\n params = result.data;\n }\n\n // Parse and validate query params\n let query: any = {};\n if (endpoint.query) {\n const queryData = parseQuery(url.searchParams);\n const result = endpoint.query.safeParse(queryData);\n if (!result.success) {\n throw new Error(`Invalid query params: ${result.error.message}`);\n }\n query = result.data;\n }\n\n // Parse and validate headers\n let headers: any = {};\n if (endpoint.headers) {\n const headersObj: Record<string, string> = {};\n request.headers.forEach((value, key) => {\n headersObj[key] = value;\n });\n const result = endpoint.headers.safeParse(headersObj);\n if (!result.success) {\n throw new Error(`Invalid headers: ${result.error.message}`);\n }\n headers = result.data;\n }\n\n return { params, query, headers };\n }\n\n /**\n * Match a request and prepare upgrade data\n * Returns null if no match, or UpgradeData for server.upgrade()\n */\n async matchAndPrepareUpgrade(request: Request): Promise<UpgradeData<S> | null> {\n const url = new URL(request.url);\n let path = url.pathname;\n\n // Strip basePath if configured\n if (this.basePath && path.startsWith(this.basePath)) {\n path = path.slice(this.basePath.length) || '/';\n }\n\n const match = this.findEndpoint(path);\n if (!match) {\n return null;\n }\n\n const { name, endpoint, params: rawParams } = match;\n\n try {\n const { params, query, headers } = this.parseUpgradeParams(request, endpoint, rawParams);\n\n // Create context if factory provided\n const context = this.contextFactory\n ? await this.contextFactory(request, String(name), endpoint)\n : (undefined as C);\n\n return {\n endpointName: String(name),\n endpoint,\n params,\n query,\n headers,\n context,\n state: {} as S,\n };\n } catch (err) {\n // Validation failed during upgrade\n console.error('WebSocket upgrade validation failed:', err);\n return null;\n }\n }\n\n /**\n * Validate an incoming client message\n */\n private validateMessage(\n endpoint: WebSocketContractDefinition,\n rawMessage: string | ArrayBuffer,\n ): ExtractClientMessage<typeof endpoint> {\n // Parse message\n const messageStr =\n typeof rawMessage === 'string' ? rawMessage : new TextDecoder().decode(rawMessage);\n const parsed = JSON.parse(messageStr) as { type: string; payload: unknown };\n\n const { type, payload } = parsed;\n\n // Find the schema for this message type\n const messageDef = endpoint.clientMessages[type];\n if (!messageDef) {\n throw new WebSocketValidationError(type, [\n {\n code: 'custom',\n path: ['type'],\n message: `Unknown message type: ${type}`,\n },\n ]);\n }\n\n // Validate payload\n const result = messageDef.payload.safeParse(payload);\n if (!result.success) {\n throw new WebSocketValidationError(type, result.error.issues);\n }\n\n return { type, payload: result.data } as ExtractClientMessage<typeof endpoint>;\n }\n\n /**\n * Get Bun-compatible WebSocket handler\n */\n get websocketHandler(): BunWebSocketHandler<UpgradeData<S>> {\n return {\n open: async (ws) => {\n const data = ws.data;\n const endpointHandlers = this.handlers[data.endpointName as keyof T];\n if (!endpointHandlers) return;\n\n // Create typed wrapper with properly typed data\n const typedWs = createTypedWebSocket(\n ws as unknown as WebSocket & {\n data: WebSocketData<WebSocketContractDefinition, S>;\n },\n );\n\n if (endpointHandlers.open) {\n await endpointHandlers.open(typedWs as any, data.context as C);\n }\n },\n\n message: async (ws, message) => {\n const data = ws.data;\n const endpointHandlers = this.handlers[data.endpointName as keyof T];\n if (!endpointHandlers) return;\n\n const typedWs = createTypedWebSocket(\n ws as unknown as WebSocket & {\n data: WebSocketData<WebSocketContractDefinition, S>;\n },\n );\n\n try {\n // Validate the message\n const validatedMessage = this.validateMessage(\n data.endpoint,\n typeof message === 'string' ? message : message.buffer,\n );\n\n // Call handler with validated message\n await endpointHandlers.message(\n typedWs as any,\n validatedMessage as any,\n data.context as C,\n );\n } catch (err) {\n if (err instanceof WebSocketValidationError) {\n // Call validation error handler if provided\n if (endpointHandlers.validationError) {\n endpointHandlers.validationError(typedWs as any, err, data.context as C);\n } else {\n // Default: send error message back\n typedWs.send(\n 'error' as any,\n {\n code: 'VALIDATION_ERROR',\n message: err.message,\n issues: err.issues,\n } as any,\n );\n }\n } else {\n console.error('WebSocket message handler error:', err);\n }\n }\n },\n\n close: (ws, _code, _reason) => {\n const data = ws.data;\n const endpointHandlers = this.handlers[data.endpointName as keyof T];\n if (!endpointHandlers) return;\n\n const typedWs = createTypedWebSocket(\n ws as unknown as WebSocket & {\n data: WebSocketData<WebSocketContractDefinition, S>;\n },\n );\n\n if (endpointHandlers.close) {\n endpointHandlers.close(typedWs as any, data.context as C);\n }\n },\n drain: () => {\n // not used\n },\n };\n }\n}\n\n/**\n * Create a WebSocket router from a contract and handlers\n */\nexport function createWebSocketRouter<T extends WebSocketContract, C = unknown, S = unknown>(\n contract: T,\n handlers: WebSocketContractHandlers<T, C, S>,\n options?: WebSocketRouterOptions<C, S>,\n): WebSocketRouter<T, C, S> {\n return new WebSocketRouter(contract, handlers, options);\n}\n"
|
|
6
|
+
],
|
|
7
|
+
"mappings": ";;AAQA;AAAA;AAMO,MAAM,iCAAiC,MAAM;AAAA,EAEzC;AAAA,EACA;AAAA,EAFT,WAAW,CACF,aACA,QACP;AAAA,IACA,MAAM,iDAAiD,aAAa;AAAA,IAH7D;AAAA,IACA;AAAA,IAGP,KAAK,OAAO;AAAA;AAEhB;AAkHA,SAAS,oBAAwE,CAC/E,IAC4B;AAAA,EAC5B,OAAO;AAAA,QACD,GAAG,GAAG;AAAA,MACR,OAAO;AAAA;AAAA,IAET,IAAI,CAAC,MAAM,SAAS;AAAA,MAClB,GAAG,KAAK,KAAK,UAAU,EAAE,MAAM,QAAQ,CAAC,CAAC;AAAA;AAAA,IAE3C,SAAS,CAAC,OAAO;AAAA,MACd,GAAW,UAAU,KAAK;AAAA;AAAA,IAE7B,WAAW,CAAC,OAAO;AAAA,MAChB,GAAW,YAAY,KAAK;AAAA;AAAA,IAE/B,OAAO,CAAC,OAAO,MAAM,SAAS;AAAA,MAC3B,GAAW,QAAQ,OAAO,KAAK,UAAU,EAAE,MAAM,QAAQ,CAAC,CAAC;AAAA;AAAA,IAE9D,KAAK,CAAC,MAAM,QAAQ;AAAA,MAClB,GAAG,MAAM,MAAM,MAAM;AAAA;AAAA,QAEnB,IAAI,GAAG;AAAA,MACT,OAAO,GAAG;AAAA;AAAA,EAEd;AAAA;AAAA;AAMK,MAAM,gBAAuE;AAAA,EASxE;AAAA,EACA;AAA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M,mBAAmB,KAAK,gBAC5B,KAAK,UACL,OAAO,YAAY,WAAW,UAAU,QAAQ,MAClD;AAAA,UAGA,MAAM,iBAAiB,QACrB,SACA,kBACA,KAAK,OACP;AAAA,UACA,OAAO,KAAK;AAAA,UACZ,IAAI,eAAe,0BAA0B;AAAA,YAE3C,IAAI,iBAAiB,iBAAiB;AAAA,cACpC,iBAAiB,gBAAgB,SAAgB,KAAK,KAAK,OAAY;AAAA,YACzE,EAAO;AAAA,cAEL,QAAQ,KACN,SACA;AAAA,gBACE,MAAM;AAAA,gBACN,SAAS,IAAI;AAAA,gBACb,QAAQ,IAAI;AAAA,cACd,CACF;AAAA;AAAA,UAEJ,EAAO;AAAA,YACL,QAAQ,MAAM,oCAAoC,GAAG;AAAA;AAAA;AAAA;AAAA,MAK3D,OAAO,CAAC,IAAI,OAAO,YAAY;AAAA,QAC7B,MAAM,OAAO,GAAG;AAAA,QAChB,MAAM,mBAAmB,KAAK,SAAS,KAAK;AAAA,QAC5C,IAAI,CAAC;AAAA,UAAkB;AAAA,QAEvB,MAAM,UAAU,qBACd,EAGF;AAAA,QAEA,IAAI,iBAAiB,OAAO;AAAA,UAC1B,iBAAiB,MAAM,SAAgB,KAAK,OAAY;AAAA,QAC1D;AAAA;AAAA,MAEF,OAAO,MAAM;AAAA,IAGf;AAAA;AAEJ;AAKO,SAAS,qBAA4E,CAC1F,UACA,UACA,SAC0B;AAAA,EAC1B,OAAO,IAAI,gBAAgB,UAAU,UAAU,OAAO;AAAA;",
|
|
8
|
+
"debugId": "47CE29C068793DD164756E2164756E21",
|
|
9
|
+
"names": []
|
|
10
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@richie-rpc/server",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.5",
|
|
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.4",
|
|
14
14
|
"typescript": "^5",
|
|
15
15
|
"zod": "^4.1.12"
|
|
16
16
|
},
|