@@ -6,8 +6,14 @@ import { HttpServerNative, NativeRequest } from "./http_server_native.ts";
66import { KeyStack } from "./keyStack.ts" ;
77import { compose , Middleware } from "./middleware.ts" ;
88import { cloneState } from "./structured_clone.ts" ;
9- import { Key , Server , ServerConstructor } from "./types.d.ts" ;
10- import { assert , isConn } from "./util.ts" ;
9+ import {
10+ Key ,
11+ Listener ,
12+ Server ,
13+ ServerConstructor ,
14+ ServerRequest ,
15+ } from "./types.d.ts" ;
16+ import { assert , isConn , isNode } from "./util.ts" ;
1117
1218export interface ListenOptionsBase extends Deno . ListenOptions {
1319 secure ?: false ;
@@ -69,7 +75,7 @@ interface ApplicationListenEventListenerObject {
6975
7076interface ApplicationListenEventInit extends EventInit {
7177 hostname : string ;
72- listener : Deno . Listener ;
78+ listener : Listener ;
7379 port : number ;
7480 secure : boolean ;
7581 serverType : "native" | "custom" ;
@@ -81,7 +87,7 @@ type ApplicationListenEventListenerOrEventListenerObject =
8187
8288/** Available options that are used when creating a new instance of
8389 * {@linkcode Application}. */
84- export interface ApplicationOptions < S > {
90+ export interface ApplicationOptions < S , R extends ServerRequest > {
8591 /** Determine how when creating a new context, the state from the application
8692 * should be applied. A value of `"clone"` will set the state as a clone of
8793 * the app state. Any non-cloneable or non-enumerable properties will not be
@@ -124,7 +130,7 @@ export interface ApplicationOptions<S> {
124130 * requests.
125131 *
126132 * Generally this is only used for testing. */
127- serverConstructor ?: ServerConstructor < NativeRequest > ;
133+ serverConstructor ?: ServerConstructor < R > ;
128134
129135 /** The initial state object for the application, of which the type can be
130136 * used to infer the type of the state for both the application and any of the
@@ -136,16 +142,23 @@ interface RequestState {
136142 handling : Set < Promise < void > > ;
137143 closing : boolean ;
138144 closed : boolean ;
139- server : Server < NativeRequest > ;
145+ server : Server < ServerRequest > ;
140146}
141147
142148// deno-lint-ignore no-explicit-any
143149export type State = Record < string | number | symbol , any > ;
144150
145151const ADDR_REGEXP = / ^ \[ ? ( [ ^ \] ] * ) \] ? : ( [ 0 - 9 ] { 1 , 5 } ) $ / ;
146152
153+ const DEFAULT_SERVER : ServerConstructor < ServerRequest > = isNode ( )
154+ ? ( await import ( "./http_server_node.ts" ) ) . HttpServerNode
155+ : HttpServerNative ;
156+ // deno-lint-ignore no-explicit-any
157+ const LocalErrorEvent : typeof ErrorEvent = ( globalThis as any ) . ErrorEvent ??
158+ ( await import ( "./node_shims.ts" ) ) . ErrorEvent ;
159+
147160export class ApplicationErrorEvent < S extends AS , AS extends State >
148- extends ErrorEvent {
161+ extends LocalErrorEvent {
149162 context ?: Context < S , AS > ;
150163
151164 constructor ( eventInitDict : ApplicationErrorEventInit < S , AS > ) {
@@ -190,7 +203,7 @@ function logErrorListener<S extends AS, AS extends State>(
190203
191204export class ApplicationListenEvent extends Event {
192205 hostname : string ;
193- listener : Deno . Listener ;
206+ listener : Listener ;
194207 port : number ;
195208 secure : boolean ;
196209 serverType : "native" | "custom" ;
@@ -239,7 +252,7 @@ export class Application<AS extends State = Record<string, any>>
239252 #contextState: "clone" | "prototype" | "alias" | "empty" ;
240253 #keys?: KeyStack ;
241254 #middleware: Middleware < State , Context < State , AS > > [ ] = [ ] ;
242- #serverConstructor: ServerConstructor < NativeRequest > ;
255+ #serverConstructor: ServerConstructor < ServerRequest > ;
243256
244257 /** A set of keys, or an instance of `KeyStack` which will be used to sign
245258 * cookies read and set by the application to avoid tampering with the
@@ -278,13 +291,13 @@ export class Application<AS extends State = Record<string, any>>
278291 */
279292 state : AS ;
280293
281- constructor ( options : ApplicationOptions < AS > = { } ) {
294+ constructor ( options : ApplicationOptions < AS , ServerRequest > = { } ) {
282295 super ( ) ;
283296 const {
284297 state,
285298 keys,
286299 proxy,
287- serverConstructor = HttpServerNative ,
300+ serverConstructor = DEFAULT_SERVER ,
288301 contextState = "clone" ,
289302 logErrors = true ,
290303 } = options ;
@@ -354,7 +367,7 @@ export class Application<AS extends State = Record<string, any>>
354367
355368 /** Processing registered middleware on each request. */
356369 async #handleRequest(
357- request : NativeRequest ,
370+ request : ServerRequest ,
358371 secure : boolean ,
359372 state : RequestState ,
360373 ) : Promise < void > {
@@ -581,4 +594,26 @@ export class Application<AS extends State = Record<string, any>>
581594 inspect ( { "#middleware" : this . #middleware, keys, proxy, state } )
582595 } `;
583596 }
597+
598+ [ Symbol . for ( "nodejs.util.inspect.custom" ) ] (
599+ depth : number ,
600+ // deno-lint-ignore no-explicit-any
601+ options : any ,
602+ inspect : ( value : unknown , options ?: unknown ) => string ,
603+ ) {
604+ if ( depth < 0 ) {
605+ return options . stylize ( `[${ this . constructor . name } ]` , "special" ) ;
606+ }
607+
608+ const newOptions = Object . assign ( { } , options , {
609+ depth : options . depth === null ? null : options . depth - 1 ,
610+ } ) ;
611+ const { keys, proxy, state } = this ;
612+ return `${ options . stylize ( this . constructor . name , "special" ) } ${
613+ inspect (
614+ { "#middleware" : this . #middleware, keys, proxy, state } ,
615+ newOptions ,
616+ )
617+ } `;
618+ }
584619}
0 commit comments