Skip to content

Commit

Permalink
refactor: extract bun types from upstream types
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jan 28, 2024
1 parent abe042a commit 167a971
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 71 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const { websocket } = bunAdapter({ onMessage: console.log });

Bun.serve({
port: 3000,
websocket,
fetch(req, server) {
if (server.upgrade(req)) {
return;
Expand All @@ -87,7 +88,6 @@ Bun.serve({
{ headers: { "content-type": "text/html" } },
);
},
websocket,
});
```

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"@types/web": "^0.0.135",
"@types/ws": "^8.5.10",
"@vitest/coverage-v8": "^1.2.2",
"bun-types": "^1.0.25",
"changelogen": "^0.5.5",
"eslint": "^8.56.0",
"eslint-config-unjs": "^0.2.1",
Expand Down
4 changes: 3 additions & 1 deletion playground/bun.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
// You can run this demo using `bun --bun ./bun.ts` or `npm run play:bun` in repo

/// <reference types="bun-types" />

import bunAdapter from "../src/adapters/bun";
import { createDemo, getIndexHTMLURL } from "./_common";

const adapter = createDemo(bunAdapter);

Bun.serve({
port: 3001,
websocket: adapter.websocket,
fetch(req, server) {
if (server.upgrade(req)) {
return;
Expand All @@ -15,5 +18,4 @@ Bun.serve({
headers: { "Content-Type": "text/html" },
});
},
websocket: adapter.websocket,
});
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 18 additions & 9 deletions src/adapters/bun.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
// https://bun.sh/docs/api/websockets

import type { BunWSOptions, BunServerWebSocket } from "../../types/bun";
/// <reference types="bun-types" />

import { WebSocketMessage } from "../message";
import { WebSocketError } from "../error";
// import { WebSocketError } from "../error";
import { WebSocketPeer } from "../peer";
import { defineWebSocketAdapter } from "../adapter";

export const WebSocket = globalThis.WebSocket;

export interface AdapterOptions {}

type WebSocketHandler<T = unknown> = Extract<
Parameters<typeof Bun.serve<T>>[0],
{ websocket: any }
>["websocket"];

type ServerWebSocket = Parameters<WebSocketHandler["message"]>[0];

export interface Adapter {
websocket: BunWSOptions;
websocket: WebSocketHandler;
}

export default defineWebSocketAdapter<Adapter, AdapterOptions>(
Expand All @@ -33,11 +41,12 @@ export default defineWebSocketAdapter<Adapter, AdapterOptions>(
const peer = new BunWebSocketPeer(ws);
handler.onClose?.(peer, 0, "");
},
error: (ws, error) => {
handler.onEvent?.("bun:error", ws, error);
const peer = new BunWebSocketPeer(ws);
handler.onError?.(peer, new WebSocketError(error));
},
// TODO
// error: (ws, error) => {
// handler.onEvent?.("bun:error", ws, error);
// const peer = new BunWebSocketPeer(ws);
// handler.onError?.(peer, new WebSocketError(error));
// },
drain: (ws) => {
handler.onEvent?.("bun:drain", ws);
},
Expand All @@ -47,7 +56,7 @@ export default defineWebSocketAdapter<Adapter, AdapterOptions>(
);

class BunWebSocketPeer extends WebSocketPeer {
constructor(private _ws: BunServerWebSocket) {
constructor(private _ws: ServerWebSocket) {
super();
}

Expand Down
60 changes: 0 additions & 60 deletions types/bun.ts

This file was deleted.

0 comments on commit 167a971

Please sign in to comment.