burger-api 0.9.7 → 1.0.0-beta
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 +201 -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 +47 -0
- package/dist/src/errors/http-error.js +146 -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 +263 -50
- package/dist/src/index.js +919 -332
- 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 +817 -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 +201 -0
- package/dist/src/router/router.js +600 -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 +172 -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 +2 -9
- package/dist/src/utils/wildcard.js +20 -58
- 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 +79 -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 +219 -0
- package/dist/src/ws/adapter.js +623 -0
- package/dist/src/ws/compiler.d.ts +37 -0
- package/dist/src/ws/compiler.js +156 -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 +81 -0
- package/dist/src/ws/platform.js +113 -0
- package/dist/src/ws/router.d.ts +48 -0
- package/dist/src/ws/router.js +174 -0
- package/dist/src/ws/scanner.d.ts +97 -0
- package/dist/src/ws/scanner.js +193 -0
- package/dist/src/ws/types.d.ts +380 -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,193 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scans a directory tree for WebSocket convention files.
|
|
3
|
+
*/
|
|
4
|
+
import { readdir } from 'node:fs/promises';
|
|
5
|
+
import * as path from 'node:path';
|
|
6
|
+
import { resolveScanDir } from '../utils/fs.js';
|
|
7
|
+
import { splitConventionName, } from '../compiler/conventions.js';
|
|
8
|
+
/** WebSocket convention stems (extension is `.ts` / `.js` / `.mjs`). */
|
|
9
|
+
const WS_CONVENTION_FILES = ['ws', 'hooks', 'config'];
|
|
10
|
+
/**
|
|
11
|
+
* WebSocket directory scanner
|
|
12
|
+
*/
|
|
13
|
+
export class WebSocketScanner {
|
|
14
|
+
wsDir;
|
|
15
|
+
prefix;
|
|
16
|
+
constructor(wsDir, prefix = '') {
|
|
17
|
+
this.wsDir = wsDir;
|
|
18
|
+
this.prefix = prefix;
|
|
19
|
+
if (!wsDir) {
|
|
20
|
+
throw new Error('WebSocket directory path is required');
|
|
21
|
+
}
|
|
22
|
+
this.wsDir = resolveScanDir(wsDir, 'WebSocket', 'wsDir');
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Scans the WebSocket directory tree and returns scanned routes
|
|
26
|
+
*/
|
|
27
|
+
async scan() {
|
|
28
|
+
const routes = [];
|
|
29
|
+
await this.walk(this.wsDir, routes);
|
|
30
|
+
// Detect global hooks at the parent directory
|
|
31
|
+
let globalHooks;
|
|
32
|
+
const parentDir = path.dirname(this.wsDir);
|
|
33
|
+
let rootEntries = [];
|
|
34
|
+
try {
|
|
35
|
+
rootEntries = await readdir(parentDir, {
|
|
36
|
+
withFileTypes: true,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
// Parent directory may not exist
|
|
41
|
+
}
|
|
42
|
+
for (const entry of rootEntries) {
|
|
43
|
+
if (!entry.isFile())
|
|
44
|
+
continue;
|
|
45
|
+
const split = splitConventionName(entry.name);
|
|
46
|
+
if (!split || split.stem !== 'hooks')
|
|
47
|
+
continue;
|
|
48
|
+
if (globalHooks) {
|
|
49
|
+
throw new Error(`Conflicting app-level hooks files "${globalHooks}" and "${path.join(parentDir, entry.name)}" — ` +
|
|
50
|
+
`use only one of .ts/.js/.mjs.`);
|
|
51
|
+
}
|
|
52
|
+
globalHooks = path.resolve(path.join(parentDir, entry.name));
|
|
53
|
+
}
|
|
54
|
+
return { routes, globalHooks };
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Recursively walks the directory, emitting a route per ws file found.
|
|
58
|
+
*/
|
|
59
|
+
async walk(dir, out) {
|
|
60
|
+
let dynamicFolderFound = false;
|
|
61
|
+
let wildcardFolderFound = false;
|
|
62
|
+
const entries = await readdir(dir, { withFileTypes: true });
|
|
63
|
+
const subDirs = [];
|
|
64
|
+
// Collect ws / hooks / config files across all accepted extensions.
|
|
65
|
+
const wsFiles = new Map();
|
|
66
|
+
for (const entry of entries) {
|
|
67
|
+
if (entry.isDirectory()) {
|
|
68
|
+
subDirs.push(entry.name);
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
if (!entry.isFile())
|
|
72
|
+
continue;
|
|
73
|
+
const split = splitConventionName(entry.name);
|
|
74
|
+
if (!split ||
|
|
75
|
+
!WS_CONVENTION_FILES.includes(split.stem)) {
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
const abs = path.resolve(path.join(dir, entry.name));
|
|
79
|
+
const existing = wsFiles.get(split.stem);
|
|
80
|
+
if (existing) {
|
|
81
|
+
throw new Error(`Conflicting convention files "${existing}" and "${abs}" in "${dir}" — ` +
|
|
82
|
+
`use only one of .ts/.js/.mjs per convention file.`);
|
|
83
|
+
}
|
|
84
|
+
wsFiles.set(split.stem, abs);
|
|
85
|
+
}
|
|
86
|
+
const wsFile = wsFiles.get('ws');
|
|
87
|
+
if (wsFile) {
|
|
88
|
+
const dirName = path.basename(dir);
|
|
89
|
+
const hooksFile = wsFiles.get('hooks');
|
|
90
|
+
const configFile = wsFiles.get('config');
|
|
91
|
+
// Build route path
|
|
92
|
+
const routePath = this.buildRoutePath(dir);
|
|
93
|
+
const params = this.extractParams(routePath);
|
|
94
|
+
const isWildcard = this.isWildcardDir(dirName);
|
|
95
|
+
const isGroup = this.isGroupDir(dirName);
|
|
96
|
+
const groupName = isGroup
|
|
97
|
+
? this.extractGroupName(dirName)
|
|
98
|
+
: undefined;
|
|
99
|
+
out.push({
|
|
100
|
+
path: routePath,
|
|
101
|
+
wsFile,
|
|
102
|
+
hooksFile,
|
|
103
|
+
configFile,
|
|
104
|
+
params,
|
|
105
|
+
isWildcard,
|
|
106
|
+
isGroup,
|
|
107
|
+
groupName,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
// Process subdirectories. Group directories only affect the URL but
|
|
111
|
+
// are still walked.
|
|
112
|
+
for (const subDir of subDirs) {
|
|
113
|
+
await this.walk(path.join(dir, subDir), out);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Builds the route path from the directory structure
|
|
118
|
+
*/
|
|
119
|
+
buildRoutePath(dir) {
|
|
120
|
+
const relativePath = path.relative(this.wsDir, dir);
|
|
121
|
+
const parts = relativePath.split(path.sep);
|
|
122
|
+
// Convert directory names to route path
|
|
123
|
+
const routeParts = [];
|
|
124
|
+
for (const part of parts) {
|
|
125
|
+
// Skip group directories in URL
|
|
126
|
+
if (this.isGroupDir(part))
|
|
127
|
+
continue;
|
|
128
|
+
// Convert dynamic folders
|
|
129
|
+
if (this.isWildcardDir(part)) {
|
|
130
|
+
routeParts.push('*');
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (this.isDynamicDir(part)) {
|
|
134
|
+
const paramName = this.extractParamName(part);
|
|
135
|
+
routeParts.push(`:${paramName}`);
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
routeParts.push(part);
|
|
139
|
+
}
|
|
140
|
+
const routePath = '/' + routeParts.join('/');
|
|
141
|
+
return this.prefix
|
|
142
|
+
? `/${this.prefix}${routePath}`.replace(/\/+/g, '/')
|
|
143
|
+
: routePath;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Extracts route parameters from the path
|
|
147
|
+
*/
|
|
148
|
+
extractParams(routePath) {
|
|
149
|
+
const params = {};
|
|
150
|
+
const parts = routePath.split('/');
|
|
151
|
+
let hasParams = false;
|
|
152
|
+
for (const part of parts) {
|
|
153
|
+
if (part.startsWith(':')) {
|
|
154
|
+
const paramName = part.slice(1);
|
|
155
|
+
params[paramName] = paramName;
|
|
156
|
+
hasParams = true;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return hasParams ? params : undefined;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Checks if a directory name is a dynamic parameter folder
|
|
163
|
+
*/
|
|
164
|
+
isDynamicDir(name) {
|
|
165
|
+
return /^\[([^\]]+)\]$/.test(name);
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Extracts the parameter name from a dynamic folder
|
|
169
|
+
*/
|
|
170
|
+
extractParamName(name) {
|
|
171
|
+
const match = name.match(/^\[([^\]]+)\]$/);
|
|
172
|
+
return match ? match[1] : name;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Checks if a directory name is a wildcard folder
|
|
176
|
+
*/
|
|
177
|
+
isWildcardDir(name) {
|
|
178
|
+
return /^\[\.\.\.([^\]]*)\]$/.test(name);
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Checks if a directory name is a group folder
|
|
182
|
+
*/
|
|
183
|
+
isGroupDir(name) {
|
|
184
|
+
return /^\(.+\)$/.test(name);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Extracts the group name from a group folder
|
|
188
|
+
*/
|
|
189
|
+
extractGroupName(name) {
|
|
190
|
+
const match = name.match(/^\((.+)\)$/);
|
|
191
|
+
return match ? (match[1] ?? name) : name;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
@@ -0,0 +1,380 @@
|
|
|
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. Set to `false` to disable auth for this route.
|
|
74
|
+
*/
|
|
75
|
+
auth?: false | {
|
|
76
|
+
required?: boolean;
|
|
77
|
+
roles?: string[];
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* WebSocket route definition
|
|
82
|
+
*/
|
|
83
|
+
export interface WebSocketRouteDefinition {
|
|
84
|
+
/**
|
|
85
|
+
* Route path (e.g., "/chat", "/notifications/:room")
|
|
86
|
+
*/
|
|
87
|
+
path: string;
|
|
88
|
+
/**
|
|
89
|
+
* Route parameters
|
|
90
|
+
*/
|
|
91
|
+
params?: Record<string, string>;
|
|
92
|
+
/**
|
|
93
|
+
* Handler functions
|
|
94
|
+
*/
|
|
95
|
+
handlers: WebSocketHandlers;
|
|
96
|
+
/**
|
|
97
|
+
* Route hooks
|
|
98
|
+
*/
|
|
99
|
+
hooks?: WebSocketHooks;
|
|
100
|
+
/**
|
|
101
|
+
* Route configuration
|
|
102
|
+
*/
|
|
103
|
+
config?: WebSocketConfig;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* WebSocket handler functions
|
|
107
|
+
*/
|
|
108
|
+
export interface WebSocketHandlers {
|
|
109
|
+
open?: (ws: BurgerWS) => void | Promise<void>;
|
|
110
|
+
message?: (ws: BurgerWS, message: string | Buffer) => void | Promise<void>;
|
|
111
|
+
close?: (ws: BurgerWS, code: number, reason: string) => void | Promise<void>;
|
|
112
|
+
drain?: (ws: BurgerWS) => void | Promise<void>;
|
|
113
|
+
ping?: (ws: BurgerWS) => void | Promise<void>;
|
|
114
|
+
pong?: (ws: BurgerWS) => void | Promise<void>;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* WebSocket hook functions
|
|
118
|
+
*/
|
|
119
|
+
export interface WebSocketHooks {
|
|
120
|
+
onOpen?: (ws: BurgerWS) => void | Promise<void>;
|
|
121
|
+
onMessage?: (ws: BurgerWS, message: string | Buffer) => void | Promise<void>;
|
|
122
|
+
onClose?: (ws: BurgerWS, code: number, reason: string) => void | Promise<void>;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Compiled WebSocket route
|
|
126
|
+
*/
|
|
127
|
+
export interface CompiledWebSocketRoute {
|
|
128
|
+
/**
|
|
129
|
+
* Route path
|
|
130
|
+
*/
|
|
131
|
+
path: string;
|
|
132
|
+
/**
|
|
133
|
+
* Route parameters
|
|
134
|
+
*/
|
|
135
|
+
params?: Record<string, string>;
|
|
136
|
+
/**
|
|
137
|
+
* Compiled handlers
|
|
138
|
+
*/
|
|
139
|
+
handlers: WebSocketHandlers;
|
|
140
|
+
/**
|
|
141
|
+
* Compiled hooks
|
|
142
|
+
*/
|
|
143
|
+
hooks?: WebSocketHooks;
|
|
144
|
+
/**
|
|
145
|
+
* Merged configuration
|
|
146
|
+
*/
|
|
147
|
+
config: WebSocketConfig;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* WebSocket context interface
|
|
151
|
+
* Provides connection methods and data access
|
|
152
|
+
*/
|
|
153
|
+
export interface BurgerWS {
|
|
154
|
+
/**
|
|
155
|
+
* Per-connection data. Typed via module augmentation of `WebSocketData`:
|
|
156
|
+
*
|
|
157
|
+
* ```typescript
|
|
158
|
+
* declare module "burger-api" {
|
|
159
|
+
* interface WebSocketData {
|
|
160
|
+
* userId: string;
|
|
161
|
+
* username: string;
|
|
162
|
+
* }
|
|
163
|
+
* }
|
|
164
|
+
* ```
|
|
165
|
+
*/
|
|
166
|
+
data: WebSocketData;
|
|
167
|
+
/**
|
|
168
|
+
* URL-decoded route params of the matched WebSocket route (e.g.
|
|
169
|
+
* `{ room: 'lobby' }` for `/chat/:room`). Empty object when none.
|
|
170
|
+
*/
|
|
171
|
+
readonly params: Record<string, string>;
|
|
172
|
+
/**
|
|
173
|
+
* Wildcard segments of the matched route, decoded per segment (e.g.
|
|
174
|
+
* `['a', 'b c']` for `/files/a/b%20c` with `/files/*`). Mirrors HTTP
|
|
175
|
+
* `ctx.wildcardParams`; `params['*']` still carries the raw remainder.
|
|
176
|
+
* Empty array for non-wildcard routes.
|
|
177
|
+
*/
|
|
178
|
+
readonly wildcardParams: string[];
|
|
179
|
+
/**
|
|
180
|
+
* The URL of the upgrade request. Parsed lazily on first access and
|
|
181
|
+
* cached for the life of the socket.
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* ```typescript
|
|
185
|
+
* const room = ws.url.pathname;
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
188
|
+
readonly url: URL;
|
|
189
|
+
/**
|
|
190
|
+
* Query string parameters of the upgrade request. Parsed lazily on first
|
|
191
|
+
* access and cached for the life of the socket.
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* ```typescript
|
|
195
|
+
* const token = ws.query.get('token');
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
readonly query: URLSearchParams;
|
|
199
|
+
/**
|
|
200
|
+
* Injected application services, populated by `burger.provide()` (the
|
|
201
|
+
* `ctx.services` equivalent). Extend `BurgerServices` via module augmentation.
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
* ```typescript
|
|
205
|
+
* const db = ws.services.db;
|
|
206
|
+
* const logger = ws.services.logger;
|
|
207
|
+
* ```
|
|
208
|
+
*/
|
|
209
|
+
services: BurgerServices;
|
|
210
|
+
/**
|
|
211
|
+
* Authenticated user from auth plugins, available when auth succeeds
|
|
212
|
+
* during the upgrade. Same type as `ctx.user` (the shared
|
|
213
|
+
* `BurgerAuthUser` augmentation point):
|
|
214
|
+
*
|
|
215
|
+
* ```typescript
|
|
216
|
+
* declare module "burger-api" {
|
|
217
|
+
* interface BurgerAuthUser {
|
|
218
|
+
* tenantId: string;
|
|
219
|
+
* }
|
|
220
|
+
* }
|
|
221
|
+
* ```
|
|
222
|
+
*/
|
|
223
|
+
user?: BurgerContext['user'];
|
|
224
|
+
/**
|
|
225
|
+
* Send a message to the client
|
|
226
|
+
*/
|
|
227
|
+
send(message: string | Buffer): void;
|
|
228
|
+
/**
|
|
229
|
+
* Send a text message to the client
|
|
230
|
+
*/
|
|
231
|
+
sendText(message: string): void;
|
|
232
|
+
/**
|
|
233
|
+
* Send a binary message to the client
|
|
234
|
+
*/
|
|
235
|
+
sendBinary(message: Buffer): void;
|
|
236
|
+
/**
|
|
237
|
+
* Subscribe to a topic for pub/sub
|
|
238
|
+
*/
|
|
239
|
+
subscribe(topic: string): void;
|
|
240
|
+
/**
|
|
241
|
+
* Unsubscribe from a topic
|
|
242
|
+
*/
|
|
243
|
+
unsubscribe(topic: string): void;
|
|
244
|
+
/**
|
|
245
|
+
* Publish a message to all subscribers of a topic
|
|
246
|
+
*/
|
|
247
|
+
publish(topic: string, message: string | Buffer): void;
|
|
248
|
+
/**
|
|
249
|
+
* Publish a text message to all subscribers of a topic
|
|
250
|
+
*/
|
|
251
|
+
publishText(topic: string, message: string): void;
|
|
252
|
+
/**
|
|
253
|
+
* Publish a binary message to all subscribers of a topic
|
|
254
|
+
*/
|
|
255
|
+
publishBinary(topic: string, message: Buffer): void;
|
|
256
|
+
/**
|
|
257
|
+
* Check if connected to a topic
|
|
258
|
+
*/
|
|
259
|
+
isSubscribed(topic: string): boolean;
|
|
260
|
+
/**
|
|
261
|
+
* Remote IP address
|
|
262
|
+
*/
|
|
263
|
+
remoteAddress: string;
|
|
264
|
+
/**
|
|
265
|
+
* Connection ready state
|
|
266
|
+
*/
|
|
267
|
+
readyState: WebSocketReadyState;
|
|
268
|
+
/**
|
|
269
|
+
* Terminate the connection immediately
|
|
270
|
+
*/
|
|
271
|
+
terminate(): void;
|
|
272
|
+
/**
|
|
273
|
+
* Close the connection with code and reason
|
|
274
|
+
*/
|
|
275
|
+
close(code?: number, reason?: string): void;
|
|
276
|
+
/**
|
|
277
|
+
* Batch messages for efficiency
|
|
278
|
+
*/
|
|
279
|
+
cork(callback: () => void): void;
|
|
280
|
+
/**
|
|
281
|
+
* The underlying raw Bun ServerWebSocket (advanced use)
|
|
282
|
+
*/
|
|
283
|
+
readonly raw: unknown;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* WebSocket module definition for a route
|
|
287
|
+
*/
|
|
288
|
+
export interface WebSocketModule {
|
|
289
|
+
/**
|
|
290
|
+
* Connection opened handler
|
|
291
|
+
*/
|
|
292
|
+
open?: (ws: BurgerWS) => void | Promise<void>;
|
|
293
|
+
/**
|
|
294
|
+
* Message received handler
|
|
295
|
+
*/
|
|
296
|
+
message?: (ws: BurgerWS, message: string | Buffer) => void | Promise<void>;
|
|
297
|
+
/**
|
|
298
|
+
* Connection closed handler
|
|
299
|
+
*/
|
|
300
|
+
close?: (ws: BurgerWS, code: number, reason: string) => void | Promise<void>;
|
|
301
|
+
/**
|
|
302
|
+
* Backpressure relieved handler
|
|
303
|
+
*/
|
|
304
|
+
drain?: (ws: BurgerWS) => void | Promise<void>;
|
|
305
|
+
/**
|
|
306
|
+
* Ping received handler
|
|
307
|
+
*/
|
|
308
|
+
ping?: (ws: BurgerWS) => void | Promise<void>;
|
|
309
|
+
/**
|
|
310
|
+
* Pong received handler
|
|
311
|
+
*/
|
|
312
|
+
pong?: (ws: BurgerWS) => void | Promise<void>;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* WebSocket hooks module
|
|
316
|
+
*/
|
|
317
|
+
export interface WebSocketHooksModule {
|
|
318
|
+
/**
|
|
319
|
+
* Hook runs when connection opens
|
|
320
|
+
*/
|
|
321
|
+
onOpen?: (ws: BurgerWS) => void | Promise<void>;
|
|
322
|
+
/**
|
|
323
|
+
* Hook runs when message received
|
|
324
|
+
*/
|
|
325
|
+
onMessage?: (ws: BurgerWS, message: string | Buffer) => void | Promise<void>;
|
|
326
|
+
/**
|
|
327
|
+
* Hook runs when connection closes
|
|
328
|
+
*/
|
|
329
|
+
onClose?: (ws: BurgerWS, code: number, reason: string) => void | Promise<void>;
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* WebSocket config module
|
|
333
|
+
*/
|
|
334
|
+
export interface WebSocketConfigModule extends WebSocketConfig {
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Default `BurgerWS` implementation wrapping the runtime's native socket.
|
|
338
|
+
*/
|
|
339
|
+
export declare class BurgerWSContext implements BurgerWS {
|
|
340
|
+
private _raw;
|
|
341
|
+
private _data;
|
|
342
|
+
private _params;
|
|
343
|
+
private _wildcardParams;
|
|
344
|
+
private _services;
|
|
345
|
+
/** Upgrade request URL captured by the adapter (not parsed yet). */
|
|
346
|
+
private _url;
|
|
347
|
+
/** Cached parsed URL — lazily built once per socket. */
|
|
348
|
+
private _parsedUrl;
|
|
349
|
+
/** Cached query view — shares the parsed URL's `searchParams`. */
|
|
350
|
+
private _query;
|
|
351
|
+
constructor(rawWebSocket: any, providers?: Map<string, unknown>, params?: Record<string, string>, url?: string, wildcardParams?: string[]);
|
|
352
|
+
get data(): WebSocketData;
|
|
353
|
+
set data(value: WebSocketData);
|
|
354
|
+
get params(): Record<string, string>;
|
|
355
|
+
get wildcardParams(): string[];
|
|
356
|
+
get url(): URL;
|
|
357
|
+
get query(): URLSearchParams;
|
|
358
|
+
get services(): BurgerServices;
|
|
359
|
+
get user(): BurgerWS['user'];
|
|
360
|
+
send(message: string | Buffer): void;
|
|
361
|
+
sendText(message: string): void;
|
|
362
|
+
sendBinary(message: Buffer): void;
|
|
363
|
+
subscribe(topic: string): void;
|
|
364
|
+
unsubscribe(topic: string): void;
|
|
365
|
+
publish(topic: string, message: string | Buffer): void;
|
|
366
|
+
publishText(topic: string, message: string): void;
|
|
367
|
+
publishBinary(topic: string, message: Buffer): void;
|
|
368
|
+
isSubscribed(topic: string): boolean;
|
|
369
|
+
get remoteAddress(): string;
|
|
370
|
+
get readyState(): WebSocketReadyState;
|
|
371
|
+
terminate(): void;
|
|
372
|
+
close(code?: number, reason?: string): void;
|
|
373
|
+
cork(callback: () => void): void;
|
|
374
|
+
get raw(): unknown;
|
|
375
|
+
/**
|
|
376
|
+
* Fails loud for Bun-only capabilities (topic pub/sub). A silent no-op
|
|
377
|
+
* would hide real delivery failures from application code.
|
|
378
|
+
*/
|
|
379
|
+
private requireBun;
|
|
380
|
+
}
|