spooder 4.5.1 → 4.5.2

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.
Files changed (3) hide show
  1. package/README.md +4 -0
  2. package/package.json +1 -1
  3. package/src/api.ts +11 -3
package/README.md CHANGED
@@ -933,6 +933,10 @@ server.websocket('/path/to/websocket', {
933
933
  // validates a request before it is upgraded
934
934
  // returns HTTP 401 if FALSE is returned
935
935
  // allows you to check headers/authentication
936
+
937
+ // if an OBJECT is returned, the object will
938
+ // be accessible on the websocket as ws.data.*
939
+
936
940
  return true;
937
941
  },
938
942
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "spooder",
3
3
  "type": "module",
4
- "version": "4.5.1",
4
+ "version": "4.5.2",
5
5
  "exports": {
6
6
  ".": {
7
7
  "bun": "./src/api.ts",
package/src/api.ts CHANGED
@@ -810,10 +810,18 @@ 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 (await handlers.accept?.(req) === false)
814
- return 401; // Unauthorized
813
+ let context_data = undefined;
814
+ if (handlers.accept) {
815
+ const res = await handlers.accept(req);
816
+
817
+ if (typeof res === 'object') {
818
+ context_data = res;
819
+ } else if (!res) {
820
+ return 401; // Unauthorized
821
+ }
822
+ }
815
823
 
816
- if (server.upgrade(req))
824
+ if (server.upgrade(req, { data: context_data }))
817
825
  return 101; // Switching Protocols
818
826
 
819
827
  return new Response('WebSocket upgrade failed', { status: 500 });