spooder 4.5.0 → 4.5.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/package.json +1 -1
- package/src/api.ts +2 -2
- package/test/index.ts +0 -29
- package/test.ts +0 -7
package/package.json
CHANGED
package/src/api.ts
CHANGED
|
@@ -105,7 +105,7 @@ export async function caution(err_message_or_obj: string | object, ...err: objec
|
|
|
105
105
|
}
|
|
106
106
|
|
|
107
107
|
type WebsocketHandlers = {
|
|
108
|
-
accept?: (req: Request) => boolean
|
|
108
|
+
accept?: (req: Request) => boolean | Promise<boolean>,
|
|
109
109
|
message?: (ws: WebSocket, message: string) => void,
|
|
110
110
|
message_json?: (ws: WebSocket, message: JsonSerializable) => void,
|
|
111
111
|
open?: (ws: WebSocket) => void,
|
|
@@ -810,7 +810,7 @@ export function serve(port: number) {
|
|
|
810
810
|
/** Add a route to upgrade connections to websockets. */
|
|
811
811
|
websocket: (path: string, handlers: WebsocketHandlers): void => {
|
|
812
812
|
routes.push([path.split('/'), async (req: Request, url: URL) => {
|
|
813
|
-
if (handlers.accept?.(req) === false)
|
|
813
|
+
if (await handlers.accept?.(req) === false)
|
|
814
814
|
return 401; // Unauthorized
|
|
815
815
|
|
|
816
816
|
if (server.upgrade(req))
|
package/test/index.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { serve } from '../src/api.ts';
|
|
2
|
-
|
|
3
|
-
const server = serve(4000);
|
|
4
|
-
|
|
5
|
-
server.route('/test', () => {
|
|
6
|
-
return 'Hello world!';
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
server.websocket('/websocket', {
|
|
10
|
-
accept: (req) => {
|
|
11
|
-
return Math.random() > 0.5;
|
|
12
|
-
},
|
|
13
|
-
|
|
14
|
-
open: (ws) => {
|
|
15
|
-
console.log('websocket opened');
|
|
16
|
-
},
|
|
17
|
-
|
|
18
|
-
close: (ws, code, reason) => {
|
|
19
|
-
console.log('websocket close');
|
|
20
|
-
},
|
|
21
|
-
|
|
22
|
-
message_json: (ws, message) => {
|
|
23
|
-
console.log(message);
|
|
24
|
-
},
|
|
25
|
-
|
|
26
|
-
drain: (ws) => {
|
|
27
|
-
console.log('websocket drain');
|
|
28
|
-
}
|
|
29
|
-
});
|