Skip to content

Commit cf9c4fe

Browse files
committed
feat: valid adapters env
1 parent 8c5c181 commit cf9c4fe

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

src/adapters/bun.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ type ContextData = {
2626

2727
// https://bun.sh/docs/api/websockets
2828
const bunAdapter: Adapter<BunAdapter, BunOptions> = (options = {}) => {
29+
if (typeof Bun === "undefined") {
30+
// eslint-disable-next-line unicorn/prefer-type-error
31+
throw new Error(
32+
"[crossws] Using Bun adapter in an incompatible environment. (https://srvx.h3.dev/guide/bundler)",
33+
);
34+
}
35+
2936
const hooks = new AdapterHookable(options);
3037
const peers = new Set<BunPeer>();
3138
return {

src/adapters/deno.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ type ServeHandlerInfo = {
2525
// https://deno.land/api?s=Deno.upgradeWebSocket
2626
// https://examples.deno.land/http-server-websocket
2727
const denoAdapter: Adapter<DenoAdapter, DenoOptions> = (options = {}) => {
28+
if (typeof Deno === "undefined") {
29+
// eslint-disable-next-line unicorn/prefer-type-error
30+
throw new Error(
31+
"[crossws] Using Deno adapter in an incompatible environment (https://srvx.h3.dev/guide/bundler).",
32+
);
33+
}
34+
2835
const hooks = new AdapterHookable(options);
2936
const peers = new Set<DenoPeer>();
3037
return {

src/adapters/node.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ export interface NodeOptions extends AdapterOptions {
4444
// https://github.com/websockets/ws
4545
// https://github.com/websockets/ws/blob/master/doc/ws.md
4646
const nodeAdapter: Adapter<NodeAdapter, NodeOptions> = (options = {}) => {
47+
if ("Deno" in globalThis || "Bun" in globalThis) {
48+
throw new Error(
49+
"[crossws] Using Node.js adapter in an incompatible environment (https://srvx.h3.dev/guide/bundler).",
50+
);
51+
}
52+
4753
const hooks = new AdapterHookable(options);
4854
const peers = new Set<NodePeer>();
4955

0 commit comments

Comments
 (0)