Skip to content

Commit

Permalink
refactor: update types
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jan 28, 2024
1 parent 167a971 commit 66629e9
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export default defineBuildConfig({
rollup: {
inlineDependencies: true,
},
externals: ["@cloudflare/workers-types"],
});
2 changes: 0 additions & 2 deletions playground/bun.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// 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";

Expand Down
2 changes: 1 addition & 1 deletion playground/cloudflare.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// You can run this demo using `npm run play:cf` in repo

/// <reference types="@cloudflare/workers-types" />
import type { Request, ExecutionContext } from "@cloudflare/workers-types";

import cloudflareAdapter from "../src/adapters/cloudflare";

Expand Down
3 changes: 2 additions & 1 deletion src/adapters/bun.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// https://bun.sh/docs/api/websockets

/// <reference types="bun-types" />
// @ts-nocheck
import type * as _bun from "bun-types";

import { WebSocketMessage } from "../message";
// import { WebSocketError } from "../error";
Expand Down
19 changes: 13 additions & 6 deletions src/adapters/cloudflare.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// https://developers.cloudflare.com/workers/examples/websockets/

/// <reference types="@cloudflare/workers-types" />
import type * as _cf from "@cloudflare/workers-types";

import { WebSocketPeer } from "../peer";
import { defineWebSocketAdapter } from "../adapter.js";
Expand All @@ -9,18 +9,25 @@ import { WebSocketError } from "../error";

type Env = Record<string, any>;

declare const WebSocketPair: typeof _cf.WebSocketPair;
declare const Response: typeof _cf.Response;

export interface AdapterOptions {}

export interface Adapter {
handleUpgrade(req: Request, env: Env, context: ExecutionContext): Response;
handleUpgrade(
req: _cf.Request,
env: Env,
context: _cf.ExecutionContext,
): _cf.Response;
}

export default defineWebSocketAdapter<Adapter, AdapterOptions>(
(handler, opts = {}) => {
const handleUpgrade = (
req: Request,
req: _cf.Request,
env: Env,
context: ExecutionContext,
context: _cf.ExecutionContext,
) => {
const pair = new WebSocketPair();
const client = pair[0];
Expand Down Expand Up @@ -60,8 +67,8 @@ export default defineWebSocketAdapter<Adapter, AdapterOptions>(

class CloudflareWebSocketPeer extends WebSocketPeer {
constructor(
private _client: WebSocket,
private _server: WebSocket,
private _client: _cf.WebSocket,
private _server: _cf.WebSocket,
) {
super();
}
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// https://examples.deno.land/http-server-websocket

// @ts-nocheck
import type * as _DENO_TYPES_ from "../../types/lib.deno.d.ts";
import type * as _deno from "../../types/lib.deno.d.ts";

import { WebSocketMessage } from "../message";
import { WebSocketError } from "../error";
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"module": "ESNext",
"moduleResolution": "Node",
"esModuleInterop": true,
"skipLibCheck": true,
"strict": true
},
"include": ["src"]
Expand Down

0 comments on commit 66629e9

Please sign in to comment.