Skip to content

Commit

Permalink
fix(deno): pass info
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Feb 25, 2024
1 parent 017d5c3 commit 2c63b37
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
4 changes: 2 additions & 2 deletions docs/2.adapters/deno.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ const { handleUpgrade } = wsAdapter({
},
});

Deno.serve({ port: 3000 }, (request) => {
Deno.serve({ port: 3000 }, (request, info) => {
if (request.headers.get("upgrade") === "websocket") {
return handleUpgrade(request);
return handleUpgrade(request, info);
}
return new Response(
`<script>new WebSocket("ws://localhost:3000").addEventListener("open", (e) => e.target.send("Hello from client!"));</script>`,
Expand Down
4 changes: 2 additions & 2 deletions playground/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { createDemo, getIndexHTML } from "./_shared.ts";

const adapter = createDemo(denoAdapter);

Deno.serve({ port: 3001 }, async (req) => {
Deno.serve({ port: 3001 }, async (req, info) => {
if (req.headers.get("upgrade") === "websocket") {
return adapter.handleUpgrade(req);
return adapter.handleUpgrade(req, info);
}

return new Response(await getIndexHTML(), {
Expand Down
13 changes: 9 additions & 4 deletions src/adapters/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { createCrossWS } from "../crossws";
import { toBufferLike } from "../_utils";

export interface DenoAdapter {
handleUpgrade(req: Request): Promise<Response>;
handleUpgrade(req: Request, info: ServeHandlerInfo): Promise<Response>;
}

export interface DenoOptions extends AdapterOptions {}
Expand All @@ -20,12 +20,13 @@ declare global {
}

type WebSocketUpgrade = import("@deno/types").Deno.WebSocketUpgrade;
type ServeHandlerInfo = any; // TODO

export default defineWebSocketAdapter<DenoAdapter, DenoOptions>(
(options = {}) => {
const crossws = createCrossWS(options);

const handleUpgrade = async (req: Request) => {
const handleUpgrade = async (req: Request, info: ServeHandlerInfo) => {
const { headers } = await crossws.upgrade({
url: req.url,
headers: req.headers,
Expand All @@ -37,7 +38,7 @@ export default defineWebSocketAdapter<DenoAdapter, DenoOptions>(
});

const peer = new DenoPeer({
deno: { ws: upgrade.socket, req },
deno: { ws: upgrade.socket, req, info },
});

upgrade.socket.addEventListener("open", () => {
Expand Down Expand Up @@ -66,7 +67,11 @@ export default defineWebSocketAdapter<DenoAdapter, DenoOptions>(
);

class DenoPeer extends Peer<{
deno: { ws: WebSocketUpgrade["socket"]; req: Request };
deno: {
ws: WebSocketUpgrade["socket"];
req: Request;
info: ServeHandlerInfo;
};
}> {
get id() {
// @ts-expect-error types missing
Expand Down

0 comments on commit 2c63b37

Please sign in to comment.