Skip to content

Commit

Permalink
refactor: update to crossws 0.2 api
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Feb 25, 2024
1 parent 21b1fb9 commit b29607f
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 40 deletions.
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -44,7 +44,7 @@
"citty": "^0.1.6",
"clipboardy": "^4.0.0",
"consola": "^3.2.3",
"crossws": "^0.1.3",
"crossws": "^0.2.0",
"defu": "^6.1.4",
"get-port-please": "^3.1.2",
"h3": "^1.10.2",
Expand Down Expand Up @@ -73,4 +73,4 @@
"vitest": "^1.3.1"
},
"packageManager": "pnpm@8.15.0"
}
}
28 changes: 15 additions & 13 deletions playground/index.ts
@@ -1,5 +1,5 @@
import { createApp, defineEventHandler } from "h3";
import { defineWebSocketHooks } from "crossws";
import { defineHooks } from "crossws";

export const app = createApp();

Expand All @@ -17,15 +17,17 @@ app.use(
defineEventHandler(() => ({ hello: "world!" })),
);

export const websocket = defineWebSocketHooks({
open(peer) {
console.log("[ws] open", peer);
peer.send("Hello!");
},
message(peer, message) {
console.log("[ws] message", peer);
if (message.text() === "ping") {
peer.send("pong");
}
},
});
export const websocket = {
hooks: defineHooks({
open(peer) {
console.log("[ws] open", peer);
peer.send("Hello!");
},
message(peer, message) {
console.log("[ws] message", peer);
if (message.text() === "ping") {
peer.send("pong");
}
},
}),
};
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

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

11 changes: 3 additions & 8 deletions src/listen.ts
Expand Up @@ -11,6 +11,7 @@ import { defu } from "defu";
import { ColorName, getColor, colors } from "consola/utils";
import { renderUnicodeCompact as renderQRCode } from "uqr";
import type { Tunnel } from "untun";
import type { AdapterOptions as CrossWSOptions } from "crossws";
import { open } from "./lib/open";
import type {
ListenOptions,
Expand All @@ -19,7 +20,6 @@ import type {
HTTPSOptions,
ListenURL,
GetURLOptions,
WebSocketOptions,
} from "./types";
import {
formatURL,
Expand Down Expand Up @@ -141,13 +141,8 @@ export async function listen(
const nodeWSAdapter = await import("crossws/adapters/node").then(
(r) => r.default || r,
);
const { $resolve, $options, ...hooks } =
listhenOptions.ws === true
? ({} as WebSocketOptions)
: listhenOptions.ws;
const { handleUpgrade } = nodeWSAdapter(hooks, {
...$options,
resolve: $resolve,
const { handleUpgrade } = nodeWSAdapter({
...(listhenOptions.ws as CrossWSOptions),
});
server.on("upgrade", handleUpgrade);
}
Expand Down
14 changes: 7 additions & 7 deletions src/server/dev.ts
Expand Up @@ -4,7 +4,7 @@ import { consola } from "consola";
import { dirname, join, resolve } from "pathe";
import type { ConsolaInstance } from "consola";
import { resolve as _resolve } from "mlly";
import type { WebSocketOptions, ListenOptions } from "../types";
import type { CrossWSOptions, ListenOptions } from "../types";
import { createResolver } from "./_resolver";

export interface DevServerOptions {
Expand Down Expand Up @@ -56,15 +56,15 @@ export async function createDevServer(
// Create app instance
const app = createApp();

const webSocketHooks = Object.create(null); // Dynamically updated with HMR
const dynamicWS = Object.create(null);
let _ws: DevServerOptions["ws"] = options.ws;
if (_ws && typeof _ws !== "function") {
_ws = {
...(options.ws as WebSocketOptions),
async $resolve(info) {
...(options.ws as CrossWSOptions),
async resolve(info) {
return {
...webSocketHooks,
...(await (options.ws as WebSocketOptions).$resolve?.(info)),
...dynamicWS.hooks,
...(await (options.ws as CrossWSOptions).resolve?.(info)),
};
},
};
Expand Down Expand Up @@ -150,7 +150,7 @@ export async function createDevServer(

if (options.ws) {
Object.assign(
webSocketHooks,
dynamicWS,
_loadedEntry.webSocket ||
_loadedEntry.websocket ||
_handler.webSocket ||
Expand Down
9 changes: 3 additions & 6 deletions src/types.ts
Expand Up @@ -2,12 +2,9 @@ import type { IncomingMessage, Server } from "node:http";
import type { Server as HTTPServer } from "node:https";
import { AddressInfo } from "node:net";
import type { GetPortInput } from "get-port-please";
import type { UserHooks as WSHooks, CrossWSOptions } from "crossws";
import type { NodeOptions } from "crossws/adapters/node";

export interface WebSocketOptions extends WSHooks {
$resolve: CrossWSOptions["resolve"];
$options: CrossWSOptions;
}
export type CrossWSOptions = NodeOptions;

export interface Certificate {
key: string;
Expand Down Expand Up @@ -68,7 +65,7 @@ export interface ListenOptions {
*/
ws?:
| boolean
| WebSocketOptions
| CrossWSOptions
| ((req: IncomingMessage, head: Buffer) => void);
}

Expand Down

0 comments on commit b29607f

Please sign in to comment.