Skip to content

Conversation

rijkvanzanten
Copy link

h3 directly relies on crossws for it's WS handling, but doesn't make the crossws types available to consumers of h3. This in turn makes it quite hard to properly type functions that rely on the returned values in the WS handlers, as it forces the consumer of h3 to also add crossws as a devDependency and ensure that the version of crossws exactly matches the version in use by h3 to ensure there's no type conflicts. This is a pretty poor DX. By re-exporting the types used for the crossws logic that's relied on by h3 this problem is resolved.

I've identified Hooks, Peer, AdapterInternal, and Message as the ones you immediately run into when using defineWebSocketHandler, but there might be others. I didn't want to go as far as to export * as that'll add a bunch of unused types and other noise to h3s global scope as well.

Fixes #716

@rijkvanzanten rijkvanzanten requested a review from pi0 as a code owner September 6, 2025 19:00
src/utils/ws.ts Outdated
import type { Hooks as WSHooks } from "crossws";
import type { EventHandler } from "../types/handler.ts";

export type { Hooks, Peer, AdapterInternal, Message } from "crossws";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to alias-prefix this types with WebSocket from h3

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pi0 Do you want it prefixed with WebSocket, or WS so it matches the WSHooks import on line 3?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefixed with WS now to match the other ws.ts usage, but happy to adjust :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WS reads a little more cryptic perhaps good time we make types consitent with WebSocket* prefix. Are other WS exports exported from package publicly?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oki! I don't believe any other WS prefixed things are exported at the moment no

@pi0 pi0 changed the title fix: re-export used crossws types types: export websocket related types Sep 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Re-export websocket types
2 participants