burger-api 0.9.7 → 1.0.0-beta.1
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 +84 -241
- package/dist/src/adapter/bun/index.d.ts +14 -0
- package/dist/src/adapter/bun/index.js +73 -0
- package/dist/src/adapter/bun/types.d.ts +15 -0
- package/dist/src/adapter/bun/types.js +1 -0
- package/dist/src/adapter/index.d.ts +1 -0
- package/dist/src/adapter/index.js +1 -0
- package/dist/src/adapter/types.d.ts +49 -0
- package/dist/src/adapter/types.js +1 -0
- package/dist/src/adapter/web-standard/fetch-handler.d.ts +39 -0
- package/dist/src/adapter/web-standard/fetch-handler.js +54 -0
- package/dist/src/adapter/web-standard/index.d.ts +8 -0
- package/dist/src/adapter/web-standard/index.js +7 -0
- package/dist/src/analysis/route-access-analyzer.d.ts +10 -0
- package/dist/src/analysis/route-access-analyzer.js +287 -0
- package/dist/src/chain/chain.d.ts +16 -0
- package/dist/src/chain/chain.js +17 -0
- package/dist/src/chain/flattener.d.ts +3 -0
- package/dist/src/chain/flattener.js +78 -0
- package/dist/src/chain/node.d.ts +25 -0
- package/dist/src/chain/node.js +1 -0
- package/dist/src/compiler/conventions.d.ts +67 -0
- package/dist/src/compiler/conventions.js +129 -0
- package/dist/src/compiler/index.d.ts +13 -0
- package/dist/src/compiler/index.js +11 -0
- package/dist/src/compiler/module-loader.d.ts +75 -0
- package/dist/src/compiler/module-loader.js +276 -0
- package/dist/src/compiler/route-module.d.ts +97 -0
- package/dist/src/compiler/route-module.js +1 -0
- package/dist/src/compiler/route-tree.d.ts +19 -0
- package/dist/src/compiler/route-tree.js +28 -0
- package/dist/src/compiler/scanner.d.ts +42 -0
- package/dist/src/compiler/scanner.js +240 -0
- package/dist/src/context/context-set.d.ts +26 -0
- package/dist/src/context/context-set.js +45 -0
- package/dist/src/context/context.d.ts +357 -0
- package/dist/src/context/context.js +401 -0
- package/dist/src/context/query-parser.d.ts +23 -0
- package/dist/src/context/query-parser.js +132 -0
- package/dist/src/context/route-access.d.ts +7 -0
- package/dist/src/context/route-access.js +18 -0
- package/dist/src/context/types.d.ts +62 -0
- package/dist/src/context/types.js +5 -0
- package/dist/src/core/asset-mime.d.ts +9 -0
- package/dist/src/core/asset-mime.js +35 -0
- package/dist/src/core/assets.d.ts +30 -0
- package/dist/src/core/assets.js +67 -0
- package/dist/src/core/docs-providers.d.ts +13 -0
- package/dist/src/core/docs-providers.js +89 -0
- package/dist/src/core/embedded-assets.d.ts +18 -0
- package/dist/src/core/embedded-assets.js +13 -0
- package/dist/src/core/openapi.d.ts +9 -10
- package/dist/src/core/openapi.js +333 -127
- package/dist/src/core/page-router.d.ts +13 -17
- package/dist/src/core/page-router.js +86 -55
- package/dist/src/core/server.d.ts +20 -11
- package/dist/src/core/server.js +41 -45
- package/dist/src/errors/forbidden.d.ts +14 -0
- package/dist/src/errors/forbidden.js +16 -0
- package/dist/src/errors/http-error.d.ts +49 -0
- package/dist/src/errors/http-error.js +155 -0
- package/dist/src/errors/method-not-allowed.d.ts +13 -0
- package/dist/src/errors/method-not-allowed.js +15 -0
- package/dist/src/errors/not-found.d.ts +14 -0
- package/dist/src/errors/not-found.js +16 -0
- package/dist/src/errors/unauthorized.d.ts +14 -0
- package/dist/src/errors/unauthorized.js +16 -0
- package/dist/src/index.d.ts +275 -50
- package/dist/src/index.js +974 -333
- package/dist/src/lifecycle/executor.d.ts +64 -0
- package/dist/src/lifecycle/executor.js +201 -0
- package/dist/src/lifecycle/hook-runner.d.ts +11 -0
- package/dist/src/lifecycle/hook-runner.js +156 -0
- package/dist/src/lifecycle/jit.d.ts +50 -0
- package/dist/src/lifecycle/jit.js +258 -0
- package/dist/src/lifecycle/transform.d.ts +26 -0
- package/dist/src/lifecycle/transform.js +99 -0
- package/dist/src/lifecycle/types.d.ts +135 -0
- package/dist/src/lifecycle/types.js +1 -0
- package/dist/src/plugin/composer.d.ts +3 -0
- package/dist/src/plugin/composer.js +28 -0
- package/dist/src/plugin/registry.d.ts +25 -0
- package/dist/src/plugin/registry.js +107 -0
- package/dist/src/plugin/types.d.ts +20 -0
- package/dist/src/plugin/types.js +1 -0
- package/dist/src/router/allow-cache.d.ts +20 -0
- package/dist/src/router/allow-cache.js +26 -0
- package/dist/src/router/compiler.d.ts +39 -0
- package/dist/src/router/compiler.js +867 -0
- package/dist/src/router/define.d.ts +67 -0
- package/dist/src/router/define.js +34 -0
- package/dist/src/router/index.d.ts +6 -0
- package/dist/src/router/index.js +5 -0
- package/dist/src/router/param-extract.d.ts +33 -0
- package/dist/src/router/param-extract.js +87 -0
- package/dist/src/router/regex-matcher.d.ts +42 -0
- package/dist/src/router/regex-matcher.js +216 -0
- package/dist/src/router/router.d.ts +214 -0
- package/dist/src/router/router.js +670 -0
- package/dist/src/router/static-map.d.ts +39 -0
- package/dist/src/router/static-map.js +48 -0
- package/dist/src/router/trie.d.ts +69 -0
- package/dist/src/router/trie.js +572 -0
- package/dist/src/router/types.d.ts +104 -0
- package/dist/src/router/types.js +1 -0
- package/dist/src/runtime/capabilities.d.ts +29 -0
- package/dist/src/runtime/capabilities.js +48 -0
- package/dist/src/types/index.d.ts +260 -109
- package/dist/src/types/index.js +1 -1
- package/dist/src/types/inference.d.ts +78 -0
- package/dist/src/types/inference.js +20 -0
- package/dist/src/types/openapi-config.d.ts +164 -0
- package/dist/src/types/openapi-config.js +8 -0
- package/dist/src/utils/env.d.ts +13 -0
- package/dist/src/utils/env.js +24 -0
- package/dist/src/utils/fs.d.ts +29 -0
- package/dist/src/utils/fs.js +75 -0
- package/dist/src/utils/index.d.ts +1 -1
- package/dist/src/utils/index.js +2 -2
- package/dist/src/utils/pathConversion.js +13 -7
- package/dist/src/utils/response.d.ts +42 -6
- package/dist/src/utils/response.js +189 -7
- package/dist/src/utils/routing.d.ts +32 -13
- package/dist/src/utils/routing.js +41 -18
- package/dist/src/utils/thenable.d.ts +8 -0
- package/dist/src/utils/thenable.js +12 -0
- package/dist/src/utils/timing-safe.d.ts +9 -0
- package/dist/src/utils/timing-safe.js +20 -0
- package/dist/src/utils/wildcard.d.ts +6 -7
- package/dist/src/utils/wildcard.js +38 -55
- package/dist/src/validation/adapter.d.ts +54 -0
- package/dist/src/validation/adapter.js +75 -0
- package/dist/src/validation/adapters/standard.d.ts +11 -0
- package/dist/src/validation/adapters/standard.js +110 -0
- package/dist/src/validation/adapters/zod.d.ts +9 -0
- package/dist/src/validation/adapters/zod.js +128 -0
- package/dist/src/validation/cache.d.ts +26 -0
- package/dist/src/validation/cache.js +35 -0
- package/dist/src/validation/coerce.d.ts +28 -0
- package/dist/src/validation/coerce.js +176 -0
- package/dist/src/validation/compiler.d.ts +27 -0
- package/dist/src/validation/compiler.js +191 -0
- package/dist/src/validation/error.d.ts +69 -0
- package/dist/src/validation/error.js +137 -0
- package/dist/src/validation/index.d.ts +16 -0
- package/dist/src/validation/index.js +12 -0
- package/dist/src/validation/response.d.ts +25 -0
- package/dist/src/validation/response.js +85 -0
- package/dist/src/validation/types.d.ts +148 -0
- package/dist/src/validation/types.js +6 -0
- package/dist/src/validation/validator.d.ts +39 -0
- package/dist/src/validation/validator.js +441 -0
- package/dist/src/ws/adapter.d.ts +230 -0
- package/dist/src/ws/adapter.js +660 -0
- package/dist/src/ws/compiler.d.ts +37 -0
- package/dist/src/ws/compiler.js +178 -0
- package/dist/src/ws/index.d.ts +11 -0
- package/dist/src/ws/index.js +8 -0
- package/dist/src/ws/platform.d.ts +82 -0
- package/dist/src/ws/platform.js +122 -0
- package/dist/src/ws/router.d.ts +47 -0
- package/dist/src/ws/router.js +183 -0
- package/dist/src/ws/scanner.d.ts +97 -0
- package/dist/src/ws/scanner.js +253 -0
- package/dist/src/ws/types.d.ts +381 -0
- package/dist/src/ws/types.js +213 -0
- package/package.json +72 -60
- package/dist/src/core/api-router.d.ts +0 -65
- package/dist/src/core/api-router.js +0 -264
- package/dist/src/core/swagger-ui.d.ts +0 -1
- package/dist/src/core/swagger-ui.js +0 -33
- package/dist/src/middleware/validator.d.ts +0 -14
- package/dist/src/middleware/validator.js +0 -133
- package/dist/src/utils/error.d.ts +0 -17
- package/dist/src/utils/error.js +0 -63
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebSocket types for BurgerAPI
|
|
3
|
+
*/
|
|
4
|
+
import type { BurgerContext, BurgerServices } from '../context/context.js';
|
|
5
|
+
/**
|
|
6
|
+
* Per-connection data, extended via module augmentation:
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* declare module "burger-api" {
|
|
11
|
+
* interface WebSocketData {
|
|
12
|
+
* userId: string;
|
|
13
|
+
* username: string;
|
|
14
|
+
* }
|
|
15
|
+
* }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export interface WebSocketData {
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* WebSocket connection states
|
|
22
|
+
*/
|
|
23
|
+
export declare const enum WebSocketReadyState {
|
|
24
|
+
CONNECTING = 0,
|
|
25
|
+
OPEN = 1,
|
|
26
|
+
CLOSING = 2,
|
|
27
|
+
CLOSED = 3
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* WebSocket close codes
|
|
31
|
+
*/
|
|
32
|
+
export declare const enum WebSocketCloseCode {
|
|
33
|
+
NORMAL_CLOSURE = 1000,
|
|
34
|
+
GOING_AWAY = 1001,
|
|
35
|
+
PROTOCOL_ERROR = 1002,
|
|
36
|
+
UNSUPPORTED_DATA = 1003,
|
|
37
|
+
NO_STATUS_RECEIVED = 1005,
|
|
38
|
+
ABNORMAL_CLOSURE = 1006,
|
|
39
|
+
INVALID_FRAME_PAYLOAD_DATA = 1007,
|
|
40
|
+
POLICY_VIOLATION = 1008,
|
|
41
|
+
MESSAGE_TOO_BIG = 1009,
|
|
42
|
+
MANDATORY_EXTENSION = 1010,
|
|
43
|
+
INTERNAL_ERROR = 1011,
|
|
44
|
+
SERVICE_RESTART = 1012,
|
|
45
|
+
TRY_AGAIN_LATER = 1013,
|
|
46
|
+
BAD_GATEWAY = 1014
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* WebSocket configuration options
|
|
50
|
+
*/
|
|
51
|
+
export interface WebSocketConfig {
|
|
52
|
+
/**
|
|
53
|
+
* Maximum length of incoming message in bytes (default: 1024 * 1024 = 1MB)
|
|
54
|
+
*/
|
|
55
|
+
maxPayloadLength?: number;
|
|
56
|
+
/**
|
|
57
|
+
* Idle timeout in seconds (default: 30)
|
|
58
|
+
*/
|
|
59
|
+
idleTimeout?: number;
|
|
60
|
+
/**
|
|
61
|
+
* Backpressure limit in bytes (default: 1024 * 1024 = 1MB)
|
|
62
|
+
*/
|
|
63
|
+
backpressureLimit?: number;
|
|
64
|
+
/**
|
|
65
|
+
* Close connection when backpressure limit reached (default: false)
|
|
66
|
+
*/
|
|
67
|
+
closeOnBackpressureLimit?: boolean;
|
|
68
|
+
/**
|
|
69
|
+
* Require server per-message compression (default: false)
|
|
70
|
+
*/
|
|
71
|
+
compression?: boolean;
|
|
72
|
+
/**
|
|
73
|
+
* Auth configuration. `true` requires an authenticated user; `false`
|
|
74
|
+
* disables auth for this route.
|
|
75
|
+
*/
|
|
76
|
+
auth?: true | false | {
|
|
77
|
+
required?: boolean;
|
|
78
|
+
roles?: string[];
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* WebSocket route definition
|
|
83
|
+
*/
|
|
84
|
+
export interface WebSocketRouteDefinition {
|
|
85
|
+
/**
|
|
86
|
+
* Route path (e.g., "/chat", "/notifications/:room")
|
|
87
|
+
*/
|
|
88
|
+
path: string;
|
|
89
|
+
/**
|
|
90
|
+
* Route parameters
|
|
91
|
+
*/
|
|
92
|
+
params?: Record<string, string>;
|
|
93
|
+
/**
|
|
94
|
+
* Handler functions
|
|
95
|
+
*/
|
|
96
|
+
handlers: WebSocketHandlers;
|
|
97
|
+
/**
|
|
98
|
+
* Route hooks
|
|
99
|
+
*/
|
|
100
|
+
hooks?: WebSocketHooks;
|
|
101
|
+
/**
|
|
102
|
+
* Route configuration
|
|
103
|
+
*/
|
|
104
|
+
config?: WebSocketConfig;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* WebSocket handler functions
|
|
108
|
+
*/
|
|
109
|
+
export interface WebSocketHandlers {
|
|
110
|
+
open?: (ws: BurgerWS) => void | Promise<void>;
|
|
111
|
+
message?: (ws: BurgerWS, message: string | Buffer) => void | Promise<void>;
|
|
112
|
+
close?: (ws: BurgerWS, code: number, reason: string) => void | Promise<void>;
|
|
113
|
+
drain?: (ws: BurgerWS) => void | Promise<void>;
|
|
114
|
+
ping?: (ws: BurgerWS) => void | Promise<void>;
|
|
115
|
+
pong?: (ws: BurgerWS) => void | Promise<void>;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* WebSocket hook functions
|
|
119
|
+
*/
|
|
120
|
+
export interface WebSocketHooks {
|
|
121
|
+
onOpen?: (ws: BurgerWS) => void | Promise<void>;
|
|
122
|
+
onMessage?: (ws: BurgerWS, message: string | Buffer) => void | Promise<void>;
|
|
123
|
+
onClose?: (ws: BurgerWS, code: number, reason: string) => void | Promise<void>;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Compiled WebSocket route
|
|
127
|
+
*/
|
|
128
|
+
export interface CompiledWebSocketRoute {
|
|
129
|
+
/**
|
|
130
|
+
* Route path
|
|
131
|
+
*/
|
|
132
|
+
path: string;
|
|
133
|
+
/**
|
|
134
|
+
* Route parameters
|
|
135
|
+
*/
|
|
136
|
+
params?: Record<string, string>;
|
|
137
|
+
/**
|
|
138
|
+
* Compiled handlers
|
|
139
|
+
*/
|
|
140
|
+
handlers: WebSocketHandlers;
|
|
141
|
+
/**
|
|
142
|
+
* Compiled hooks
|
|
143
|
+
*/
|
|
144
|
+
hooks?: WebSocketHooks;
|
|
145
|
+
/**
|
|
146
|
+
* Merged configuration
|
|
147
|
+
*/
|
|
148
|
+
config: WebSocketConfig;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* WebSocket context interface
|
|
152
|
+
* Provides connection methods and data access
|
|
153
|
+
*/
|
|
154
|
+
export interface BurgerWS {
|
|
155
|
+
/**
|
|
156
|
+
* Per-connection data. Typed via module augmentation of `WebSocketData`:
|
|
157
|
+
*
|
|
158
|
+
* ```typescript
|
|
159
|
+
* declare module "burger-api" {
|
|
160
|
+
* interface WebSocketData {
|
|
161
|
+
* userId: string;
|
|
162
|
+
* username: string;
|
|
163
|
+
* }
|
|
164
|
+
* }
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
data: WebSocketData;
|
|
168
|
+
/**
|
|
169
|
+
* URL-decoded route params of the matched WebSocket route (e.g.
|
|
170
|
+
* `{ room: 'lobby' }` for `/chat/:room`). Empty object when none.
|
|
171
|
+
*/
|
|
172
|
+
readonly params: Record<string, string>;
|
|
173
|
+
/**
|
|
174
|
+
* Wildcard segments of the matched route, decoded per segment (e.g.
|
|
175
|
+
* `['a', 'b c']` for `/files/a/b%20c` with `/files/*`). Mirrors HTTP
|
|
176
|
+
* `ctx.wildcardParams`; `params['*']` still carries the raw remainder.
|
|
177
|
+
* Empty array for non-wildcard routes.
|
|
178
|
+
*/
|
|
179
|
+
readonly wildcardParams: string[];
|
|
180
|
+
/**
|
|
181
|
+
* The URL of the upgrade request. Parsed lazily on first access and
|
|
182
|
+
* cached for the life of the socket.
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* ```typescript
|
|
186
|
+
* const room = ws.url.pathname;
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
readonly url: URL;
|
|
190
|
+
/**
|
|
191
|
+
* Query string parameters of the upgrade request. Parsed lazily on first
|
|
192
|
+
* access and cached for the life of the socket.
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* ```typescript
|
|
196
|
+
* const token = ws.query.get('token');
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
199
|
+
readonly query: URLSearchParams;
|
|
200
|
+
/**
|
|
201
|
+
* Injected application services, populated by `burger.provide()` (the
|
|
202
|
+
* `ctx.services` equivalent). Extend `BurgerServices` via module augmentation.
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* ```typescript
|
|
206
|
+
* const db = ws.services.db;
|
|
207
|
+
* const logger = ws.services.logger;
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
services: BurgerServices;
|
|
211
|
+
/**
|
|
212
|
+
* Authenticated user from auth plugins, available when auth succeeds
|
|
213
|
+
* during the upgrade. Same type as `ctx.user` (the shared
|
|
214
|
+
* `BurgerAuthUser` augmentation point):
|
|
215
|
+
*
|
|
216
|
+
* ```typescript
|
|
217
|
+
* declare module "burger-api" {
|
|
218
|
+
* interface BurgerAuthUser {
|
|
219
|
+
* tenantId: string;
|
|
220
|
+
* }
|
|
221
|
+
* }
|
|
222
|
+
* ```
|
|
223
|
+
*/
|
|
224
|
+
user?: BurgerContext['user'];
|
|
225
|
+
/**
|
|
226
|
+
* Send a message to the client
|
|
227
|
+
*/
|
|
228
|
+
send(message: string | Buffer): void;
|
|
229
|
+
/**
|
|
230
|
+
* Send a text message to the client
|
|
231
|
+
*/
|
|
232
|
+
sendText(message: string): void;
|
|
233
|
+
/**
|
|
234
|
+
* Send a binary message to the client
|
|
235
|
+
*/
|
|
236
|
+
sendBinary(message: Buffer): void;
|
|
237
|
+
/**
|
|
238
|
+
* Subscribe to a topic for pub/sub
|
|
239
|
+
*/
|
|
240
|
+
subscribe(topic: string): void;
|
|
241
|
+
/**
|
|
242
|
+
* Unsubscribe from a topic
|
|
243
|
+
*/
|
|
244
|
+
unsubscribe(topic: string): void;
|
|
245
|
+
/**
|
|
246
|
+
* Publish a message to all subscribers of a topic
|
|
247
|
+
*/
|
|
248
|
+
publish(topic: string, message: string | Buffer): void;
|
|
249
|
+
/**
|
|
250
|
+
* Publish a text message to all subscribers of a topic
|
|
251
|
+
*/
|
|
252
|
+
publishText(topic: string, message: string): void;
|
|
253
|
+
/**
|
|
254
|
+
* Publish a binary message to all subscribers of a topic
|
|
255
|
+
*/
|
|
256
|
+
publishBinary(topic: string, message: Buffer): void;
|
|
257
|
+
/**
|
|
258
|
+
* Check if connected to a topic
|
|
259
|
+
*/
|
|
260
|
+
isSubscribed(topic: string): boolean;
|
|
261
|
+
/**
|
|
262
|
+
* Remote IP address
|
|
263
|
+
*/
|
|
264
|
+
remoteAddress: string;
|
|
265
|
+
/**
|
|
266
|
+
* Connection ready state
|
|
267
|
+
*/
|
|
268
|
+
readyState: WebSocketReadyState;
|
|
269
|
+
/**
|
|
270
|
+
* Terminate the connection immediately
|
|
271
|
+
*/
|
|
272
|
+
terminate(): void;
|
|
273
|
+
/**
|
|
274
|
+
* Close the connection with code and reason
|
|
275
|
+
*/
|
|
276
|
+
close(code?: number, reason?: string): void;
|
|
277
|
+
/**
|
|
278
|
+
* Batch messages for efficiency
|
|
279
|
+
*/
|
|
280
|
+
cork(callback: () => void): void;
|
|
281
|
+
/**
|
|
282
|
+
* The underlying raw Bun ServerWebSocket (advanced use)
|
|
283
|
+
*/
|
|
284
|
+
readonly raw: unknown;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* WebSocket module definition for a route
|
|
288
|
+
*/
|
|
289
|
+
export interface WebSocketModule {
|
|
290
|
+
/**
|
|
291
|
+
* Connection opened handler
|
|
292
|
+
*/
|
|
293
|
+
open?: (ws: BurgerWS) => void | Promise<void>;
|
|
294
|
+
/**
|
|
295
|
+
* Message received handler
|
|
296
|
+
*/
|
|
297
|
+
message?: (ws: BurgerWS, message: string | Buffer) => void | Promise<void>;
|
|
298
|
+
/**
|
|
299
|
+
* Connection closed handler
|
|
300
|
+
*/
|
|
301
|
+
close?: (ws: BurgerWS, code: number, reason: string) => void | Promise<void>;
|
|
302
|
+
/**
|
|
303
|
+
* Backpressure relieved handler
|
|
304
|
+
*/
|
|
305
|
+
drain?: (ws: BurgerWS) => void | Promise<void>;
|
|
306
|
+
/**
|
|
307
|
+
* Ping received handler
|
|
308
|
+
*/
|
|
309
|
+
ping?: (ws: BurgerWS) => void | Promise<void>;
|
|
310
|
+
/**
|
|
311
|
+
* Pong received handler
|
|
312
|
+
*/
|
|
313
|
+
pong?: (ws: BurgerWS) => void | Promise<void>;
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* WebSocket hooks module
|
|
317
|
+
*/
|
|
318
|
+
export interface WebSocketHooksModule {
|
|
319
|
+
/**
|
|
320
|
+
* Hook runs when connection opens
|
|
321
|
+
*/
|
|
322
|
+
onOpen?: (ws: BurgerWS) => void | Promise<void>;
|
|
323
|
+
/**
|
|
324
|
+
* Hook runs when message received
|
|
325
|
+
*/
|
|
326
|
+
onMessage?: (ws: BurgerWS, message: string | Buffer) => void | Promise<void>;
|
|
327
|
+
/**
|
|
328
|
+
* Hook runs when connection closes
|
|
329
|
+
*/
|
|
330
|
+
onClose?: (ws: BurgerWS, code: number, reason: string) => void | Promise<void>;
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* WebSocket config module
|
|
334
|
+
*/
|
|
335
|
+
export interface WebSocketConfigModule extends WebSocketConfig {
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Default `BurgerWS` implementation wrapping the runtime's native socket.
|
|
339
|
+
*/
|
|
340
|
+
export declare class BurgerWSContext implements BurgerWS {
|
|
341
|
+
private _raw;
|
|
342
|
+
private _data;
|
|
343
|
+
private _params;
|
|
344
|
+
private _wildcardParams;
|
|
345
|
+
private _services;
|
|
346
|
+
/** Upgrade request URL captured by the adapter (not parsed yet). */
|
|
347
|
+
private _url;
|
|
348
|
+
/** Cached parsed URL — lazily built once per socket. */
|
|
349
|
+
private _parsedUrl;
|
|
350
|
+
/** Cached query view — shares the parsed URL's `searchParams`. */
|
|
351
|
+
private _query;
|
|
352
|
+
constructor(rawWebSocket: any, providers?: Map<string, unknown>, params?: Record<string, string>, url?: string, wildcardParams?: string[]);
|
|
353
|
+
get data(): WebSocketData;
|
|
354
|
+
set data(value: WebSocketData);
|
|
355
|
+
get params(): Record<string, string>;
|
|
356
|
+
get wildcardParams(): string[];
|
|
357
|
+
get url(): URL;
|
|
358
|
+
get query(): URLSearchParams;
|
|
359
|
+
get services(): BurgerServices;
|
|
360
|
+
get user(): BurgerWS['user'];
|
|
361
|
+
send(message: string | Buffer): void;
|
|
362
|
+
sendText(message: string): void;
|
|
363
|
+
sendBinary(message: Buffer): void;
|
|
364
|
+
subscribe(topic: string): void;
|
|
365
|
+
unsubscribe(topic: string): void;
|
|
366
|
+
publish(topic: string, message: string | Buffer): void;
|
|
367
|
+
publishText(topic: string, message: string): void;
|
|
368
|
+
publishBinary(topic: string, message: Buffer): void;
|
|
369
|
+
isSubscribed(topic: string): boolean;
|
|
370
|
+
get remoteAddress(): string;
|
|
371
|
+
get readyState(): WebSocketReadyState;
|
|
372
|
+
terminate(): void;
|
|
373
|
+
close(code?: number, reason?: string): void;
|
|
374
|
+
cork(callback: () => void): void;
|
|
375
|
+
get raw(): unknown;
|
|
376
|
+
/**
|
|
377
|
+
* Fails loud for Bun-only capabilities (topic pub/sub). A silent no-op
|
|
378
|
+
* would hide real delivery failures from application code.
|
|
379
|
+
*/
|
|
380
|
+
private requireBun;
|
|
381
|
+
}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebSocket types for BurgerAPI
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* WebSocket connection states
|
|
6
|
+
*/
|
|
7
|
+
export var WebSocketReadyState;
|
|
8
|
+
(function (WebSocketReadyState) {
|
|
9
|
+
WebSocketReadyState[WebSocketReadyState["CONNECTING"] = 0] = "CONNECTING";
|
|
10
|
+
WebSocketReadyState[WebSocketReadyState["OPEN"] = 1] = "OPEN";
|
|
11
|
+
WebSocketReadyState[WebSocketReadyState["CLOSING"] = 2] = "CLOSING";
|
|
12
|
+
WebSocketReadyState[WebSocketReadyState["CLOSED"] = 3] = "CLOSED";
|
|
13
|
+
})(WebSocketReadyState || (WebSocketReadyState = {}));
|
|
14
|
+
/**
|
|
15
|
+
* WebSocket close codes
|
|
16
|
+
*/
|
|
17
|
+
export var WebSocketCloseCode;
|
|
18
|
+
(function (WebSocketCloseCode) {
|
|
19
|
+
WebSocketCloseCode[WebSocketCloseCode["NORMAL_CLOSURE"] = 1000] = "NORMAL_CLOSURE";
|
|
20
|
+
WebSocketCloseCode[WebSocketCloseCode["GOING_AWAY"] = 1001] = "GOING_AWAY";
|
|
21
|
+
WebSocketCloseCode[WebSocketCloseCode["PROTOCOL_ERROR"] = 1002] = "PROTOCOL_ERROR";
|
|
22
|
+
WebSocketCloseCode[WebSocketCloseCode["UNSUPPORTED_DATA"] = 1003] = "UNSUPPORTED_DATA";
|
|
23
|
+
WebSocketCloseCode[WebSocketCloseCode["NO_STATUS_RECEIVED"] = 1005] = "NO_STATUS_RECEIVED";
|
|
24
|
+
WebSocketCloseCode[WebSocketCloseCode["ABNORMAL_CLOSURE"] = 1006] = "ABNORMAL_CLOSURE";
|
|
25
|
+
WebSocketCloseCode[WebSocketCloseCode["INVALID_FRAME_PAYLOAD_DATA"] = 1007] = "INVALID_FRAME_PAYLOAD_DATA";
|
|
26
|
+
WebSocketCloseCode[WebSocketCloseCode["POLICY_VIOLATION"] = 1008] = "POLICY_VIOLATION";
|
|
27
|
+
WebSocketCloseCode[WebSocketCloseCode["MESSAGE_TOO_BIG"] = 1009] = "MESSAGE_TOO_BIG";
|
|
28
|
+
WebSocketCloseCode[WebSocketCloseCode["MANDATORY_EXTENSION"] = 1010] = "MANDATORY_EXTENSION";
|
|
29
|
+
WebSocketCloseCode[WebSocketCloseCode["INTERNAL_ERROR"] = 1011] = "INTERNAL_ERROR";
|
|
30
|
+
WebSocketCloseCode[WebSocketCloseCode["SERVICE_RESTART"] = 1012] = "SERVICE_RESTART";
|
|
31
|
+
WebSocketCloseCode[WebSocketCloseCode["TRY_AGAIN_LATER"] = 1013] = "TRY_AGAIN_LATER";
|
|
32
|
+
WebSocketCloseCode[WebSocketCloseCode["BAD_GATEWAY"] = 1014] = "BAD_GATEWAY";
|
|
33
|
+
})(WebSocketCloseCode || (WebSocketCloseCode = {}));
|
|
34
|
+
/**
|
|
35
|
+
* Default `BurgerWS` implementation wrapping the runtime's native socket.
|
|
36
|
+
*/
|
|
37
|
+
export class BurgerWSContext {
|
|
38
|
+
// Platform boundary: wraps the runtime's native server-side socket (Bun,
|
|
39
|
+
// Cloudflare/Deno, or `ws` on Node). Methods probe the raw socket
|
|
40
|
+
// structurally and fail loud where a capability does not exist.
|
|
41
|
+
_raw;
|
|
42
|
+
_data = {};
|
|
43
|
+
_params;
|
|
44
|
+
_wildcardParams;
|
|
45
|
+
_services = Object.create(null);
|
|
46
|
+
/** Upgrade request URL captured by the adapter (not parsed yet). */
|
|
47
|
+
_url;
|
|
48
|
+
/** Cached parsed URL — lazily built once per socket. */
|
|
49
|
+
_parsedUrl;
|
|
50
|
+
/** Cached query view — shares the parsed URL's `searchParams`. */
|
|
51
|
+
_query;
|
|
52
|
+
// The raw socket is the platform's server-side WebSocket; `providers` is
|
|
53
|
+
// the same map passed to `BurgerContext.create`.
|
|
54
|
+
constructor(rawWebSocket, providers, params = {}, url, wildcardParams = []) {
|
|
55
|
+
this._raw = rawWebSocket;
|
|
56
|
+
this._params = params;
|
|
57
|
+
this._wildcardParams = wildcardParams;
|
|
58
|
+
this._url = url;
|
|
59
|
+
this._parsedUrl = undefined;
|
|
60
|
+
this._query = undefined;
|
|
61
|
+
// Copy data from the raw socket (typed via the `WebSocketData`
|
|
62
|
+
// augmentation).
|
|
63
|
+
if (rawWebSocket.data) {
|
|
64
|
+
this._data = { ...rawWebSocket.data };
|
|
65
|
+
}
|
|
66
|
+
this._services = providers
|
|
67
|
+
? Object.fromEntries(providers)
|
|
68
|
+
: Object.create(null);
|
|
69
|
+
}
|
|
70
|
+
get data() {
|
|
71
|
+
return this._data;
|
|
72
|
+
}
|
|
73
|
+
set data(value) {
|
|
74
|
+
this._data = value;
|
|
75
|
+
// Sync back to raw WebSocket
|
|
76
|
+
if (this._raw) {
|
|
77
|
+
this._raw.data = value;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
get params() {
|
|
81
|
+
return this._params;
|
|
82
|
+
}
|
|
83
|
+
get wildcardParams() {
|
|
84
|
+
return this._wildcardParams;
|
|
85
|
+
}
|
|
86
|
+
get url() {
|
|
87
|
+
if (this._parsedUrl !== undefined)
|
|
88
|
+
return this._parsedUrl;
|
|
89
|
+
// Wired sockets carry the URL from the upgrade; a raw socket that
|
|
90
|
+
// exposes its own `url` (some framing libraries) is the fallback.
|
|
91
|
+
const raw = this._url ?? this._raw?.url;
|
|
92
|
+
if (typeof raw !== 'string') {
|
|
93
|
+
throw new Error('[burger-api] ws.url is unavailable: this socket was not ' +
|
|
94
|
+
'created by the BurgerAPI WebSocket upgrade path.');
|
|
95
|
+
}
|
|
96
|
+
return (this._parsedUrl = new URL(raw));
|
|
97
|
+
}
|
|
98
|
+
get query() {
|
|
99
|
+
return (this._query ??= this.url.searchParams);
|
|
100
|
+
}
|
|
101
|
+
get services() {
|
|
102
|
+
return this._services;
|
|
103
|
+
}
|
|
104
|
+
get user() {
|
|
105
|
+
// `user` is not declared on `WebSocketData`; auth plugins seed it.
|
|
106
|
+
return this._data.user;
|
|
107
|
+
}
|
|
108
|
+
send(message) {
|
|
109
|
+
if (typeof this._raw.sendText === 'function') {
|
|
110
|
+
// Bun socket: dedicated text/binary entry points.
|
|
111
|
+
if (typeof message === 'string') {
|
|
112
|
+
this._raw.sendText(message);
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
this._raw.sendBinary(message);
|
|
116
|
+
}
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
// Standard WebSocket (Workers / Deno / `ws`): `send` takes strings
|
|
120
|
+
// and binary views.
|
|
121
|
+
this._raw.send(typeof message === 'string'
|
|
122
|
+
? message
|
|
123
|
+
: new Uint8Array(message.buffer, message.byteOffset, message.byteLength));
|
|
124
|
+
}
|
|
125
|
+
sendText(message) {
|
|
126
|
+
if (typeof this._raw.sendText === 'function') {
|
|
127
|
+
this._raw.sendText(message);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
this._raw.send(message);
|
|
131
|
+
}
|
|
132
|
+
sendBinary(message) {
|
|
133
|
+
if (typeof this._raw.sendBinary === 'function') {
|
|
134
|
+
this._raw.sendBinary(message);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
this._raw.send(new Uint8Array(message.buffer, message.byteOffset, message.byteLength));
|
|
138
|
+
}
|
|
139
|
+
subscribe(topic) {
|
|
140
|
+
this.requireBun('subscribe', topic);
|
|
141
|
+
this._raw.subscribe(topic);
|
|
142
|
+
}
|
|
143
|
+
unsubscribe(topic) {
|
|
144
|
+
this.requireBun('unsubscribe', topic);
|
|
145
|
+
this._raw.unsubscribe(topic);
|
|
146
|
+
}
|
|
147
|
+
publish(topic, message) {
|
|
148
|
+
this.requireBun('publish', topic);
|
|
149
|
+
if (typeof message === 'string') {
|
|
150
|
+
this._raw.publishText(topic, message);
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
this._raw.publishBinary(topic, message);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
publishText(topic, message) {
|
|
157
|
+
this.requireBun('publish', topic);
|
|
158
|
+
this._raw.publishText(topic, message);
|
|
159
|
+
}
|
|
160
|
+
publishBinary(topic, message) {
|
|
161
|
+
this.requireBun('publish', topic);
|
|
162
|
+
this._raw.publishBinary(topic, message);
|
|
163
|
+
}
|
|
164
|
+
isSubscribed(topic) {
|
|
165
|
+
if (typeof this._raw.isSubscribed !== 'function') {
|
|
166
|
+
throw new Error('[burger-api] ws.isSubscribed() requires Bun topic pub/sub, ' +
|
|
167
|
+
'which this runtime does not provide.');
|
|
168
|
+
}
|
|
169
|
+
return this._raw.isSubscribed(topic);
|
|
170
|
+
}
|
|
171
|
+
get remoteAddress() {
|
|
172
|
+
return this._raw.remoteAddress ?? '';
|
|
173
|
+
}
|
|
174
|
+
get readyState() {
|
|
175
|
+
// Other runtimes expose the same numeric states.
|
|
176
|
+
return this._raw.readyState ?? WebSocketReadyState.OPEN;
|
|
177
|
+
}
|
|
178
|
+
terminate() {
|
|
179
|
+
if (typeof this._raw.terminate !== 'function') {
|
|
180
|
+
this._raw.close(1001, 'Going Away');
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
this._raw.terminate();
|
|
184
|
+
}
|
|
185
|
+
close(code, reason) {
|
|
186
|
+
this._raw.close(code, reason);
|
|
187
|
+
}
|
|
188
|
+
cork(callback) {
|
|
189
|
+
if (typeof this._raw.cork !== 'function') {
|
|
190
|
+
// No batching on this runtime — run directly.
|
|
191
|
+
callback();
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
this._raw.cork(callback);
|
|
195
|
+
}
|
|
196
|
+
get raw() {
|
|
197
|
+
return this._raw;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Fails loud for Bun-only capabilities (topic pub/sub). A silent no-op
|
|
201
|
+
* would hide real delivery failures from application code.
|
|
202
|
+
*/
|
|
203
|
+
requireBun(method, topic) {
|
|
204
|
+
const available = typeof this._raw.subscribe === 'function' &&
|
|
205
|
+
typeof this._raw.publishText === 'function';
|
|
206
|
+
if (available)
|
|
207
|
+
return;
|
|
208
|
+
throw new Error(`[burger-api] ws.${method}(${topic ? `"${topic}"` : ''}) requires ` +
|
|
209
|
+
'Bun topic pub/sub, which this runtime does not provide. ' +
|
|
210
|
+
'Track per-connection state in ws.data and fan out with an ' +
|
|
211
|
+
'explicit connection registry instead.');
|
|
212
|
+
}
|
|
213
|
+
}
|