Skip to content

Commit

Permalink
refactor: import bun types from @types/bun
Browse files Browse the repository at this point in the history
Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
  • Loading branch information
pi0 and brc-dd committed Jan 29, 2024
1 parent 6e2b296 commit ba40b53
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 17 deletions.
2 changes: 1 addition & 1 deletion build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default defineBuildConfig({
rollup: {
inlineDependencies: true,
},
externals: ["@cloudflare/workers-types", "bun-types"],
externals: ["@cloudflare/workers-types", "bun"],
hooks: {
async "build:done"(ctx) {
const entries = Object.keys(ctx.pkg.exports || {})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20240117.0",
"@types/bun": "^1.0.4",
"@types/node": "^20.11.10",
"@types/web": "^0.0.135",
"@types/ws": "^8.5.10",
"bun-types": "^1.0.25",
"changelogen": "^0.5.5",
"consola": "^3.2.3",
"eslint": "^8.56.0",
Expand Down
12 changes: 9 additions & 3 deletions pnpm-lock.yaml

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

16 changes: 4 additions & 12 deletions src/adapters/bun.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// https://bun.sh/docs/api/websockets

// @ts-expect-error
import type {} from "bun-types";
import type { WebSocketHandler, ServerWebSocket } from "bun";

import { WebSocketMessage } from "../message";
import { WebSocketError } from "../error";
Expand All @@ -12,20 +11,13 @@ export interface AdapterOptions {}

type ContextData = { _peer?: WebSocketPeer };

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

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

export interface Adapter {
websocket: WebSocketHooks;
websocket: WebSocketHandler<ContextData>;
}

export default defineWebSocketAdapter<Adapter, AdapterOptions>(
(hooks, opts = {}) => {
const getPeer = (ws: ServerWebSocket) => {
const getPeer = (ws: ServerWebSocket<ContextData>) => {
if (ws.data?._peer) {
return ws.data._peer;
}
Expand Down Expand Up @@ -76,7 +68,7 @@ export default defineWebSocketAdapter<Adapter, AdapterOptions>(
);

class WebSocketPeer extends WebSocketPeerBase<{
bun: { ws: ServerWebSocket };
bun: { ws: ServerWebSocket<ContextData> };
}> {
get id() {
let addr = this.ctx.bun.ws.remoteAddress;
Expand Down

0 comments on commit ba40b53

Please sign in to comment.