1- import { createWaitUntil } from "../_utils.ts" ;
1+ import {
2+ resolvePortAndHost ,
3+ resolveTLSOptions ,
4+ printListening ,
5+ fmtURL ,
6+ createWaitUntil ,
7+ } from "../_utils.ts" ;
28import type { Server , ServerOptions } from "../types.ts" ;
39import { wrapFetch } from "../_middleware.ts" ;
410import { errorPlugin } from "../_plugins.ts" ;
5- import { serve as serveDeno } from "./deno.ts" ;
611
712type MaybePromise < T > = T | Promise < T > ;
813
@@ -39,10 +44,7 @@ declare namespace Bunny {
3944}
4045
4146export 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
4850class 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