Skip to content

Commit 5605c10

Browse files
committed
revert(bunny): fallback to deno adapter
1 parent 51ba934 commit 5605c10

File tree

1 file changed

+52
-7
lines changed

1 file changed

+52
-7
lines changed

src/adapters/bunny.ts

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
import { createWaitUntil } from "../_utils.ts";
1+
import {
2+
resolvePortAndHost,
3+
resolveTLSOptions,
4+
printListening,
5+
fmtURL,
6+
createWaitUntil,
7+
} from "../_utils.ts";
28
import type { Server, ServerOptions } from "../types.ts";
39
import { wrapFetch } from "../_middleware.ts";
410
import { errorPlugin } from "../_plugins.ts";
5-
import { serve as serveDeno } from "./deno.ts";
611

712
type MaybePromise<T> = T | Promise<T>;
813

@@ -39,10 +44,7 @@ declare namespace Bunny {
3944
}
4045

4146
export function serve(options: ServerOptions): Server {
42-
if (typeof Bunny !== "undefined" && Bunny.v1 && typeof Bunny.v1.serve === "function") {
43-
return new BunnyServer(options);
44-
}
45-
return serveDeno(options);
47+
return new BunnyServer(options);
4648
}
4749

4850
class BunnyServer implements Server {
@@ -52,6 +54,9 @@ class BunnyServer implements Server {
5254
private _denoServer?: Deno.HttpServer = undefined;
5355
private _started = false;
5456

57+
// Deno only properties for local use, not available in Bunny runtime
58+
#listeningPromise?: Promise<void>;
59+
5560
#wait: ReturnType<typeof createWaitUntil> | undefined;
5661

5762
constructor(options: ServerOptions) {
@@ -97,7 +102,47 @@ class BunnyServer implements Server {
97102
if (this._started) return;
98103
this._started = true;
99104

100-
Bunny.v1.serve(this.fetch);
105+
// Check if running in Bunny runtime
106+
if (typeof Bunny !== "undefined" && Bunny.v1?.serve) {
107+
Bunny.v1.serve(this.fetch);
108+
} else if (typeof Deno !== "undefined") {
109+
// Try to fallback to Deno's serve for local use
110+
if (!this.options.silent) {
111+
console.warn("[srvx] Bunny runtime not detected. Falling back to Deno for local use.");
112+
}
113+
114+
if (this._denoServer) {
115+
return Promise.resolve(this.#listeningPromise).then(() => this);
116+
}
117+
const onListenPromise = Promise.withResolvers<void>();
118+
this.#listeningPromise = onListenPromise.promise;
119+
const tls = resolveTLSOptions(this.options);
120+
121+
const denoServeOptions = {
122+
...resolvePortAndHost(this.options),
123+
reusePort: this.options.reusePort,
124+
onError: this.options.error,
125+
...(tls ? { key: tls.key, cert: tls.cert, passphrase: tls.passphrase } : {}),
126+
...this.options.deno,
127+
};
128+
this._denoServer = Deno.serve(
129+
{
130+
...denoServeOptions,
131+
onListen: (info) => {
132+
if (this.options.deno?.onListen) {
133+
this.options.deno.onListen(info);
134+
}
135+
printListening(this.options, fmtURL(info.hostname, info.port, !!denoServeOptions.cert));
136+
onListenPromise.resolve();
137+
},
138+
},
139+
this.fetch,
140+
);
141+
} else {
142+
throw new Error(
143+
"[srvx] Bunny runtime not detected and Deno is not available. Unable to start server.",
144+
);
145+
}
101146
}
102147

103148
ready(): Promise<Server> {

0 commit comments

Comments
 (0)