Skip to content

Commit

Permalink
feat: allow deferred open
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Apr 5, 2021
1 parent c4874bf commit 2b4a3f8
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/index.ts
Expand Up @@ -38,9 +38,9 @@ export interface ListenOptions {

export interface Listener {
url: string,
getURL: (url: string) => string,
server: http.Server | https.Server,
close: () => Promise<any>
close: () => Promise<void>,
open: () => Promise<void>
}

export async function listen (handle: http.RequestListener, opts: Partial<ListenOptions> = {}): Promise<Listener> {
Expand Down Expand Up @@ -115,9 +115,12 @@ export async function listen (handle: http.RequestListener, opts: Partial<Listen
}
}

if (opts.open) {
const _open = async () => {
const { default: open } = await import('open')
await open(url).catch(() => {})
await open(url).catch(() => { })
}
if (opts.open) {
await _open()
}

if (opts.autoClose) {
Expand All @@ -127,6 +130,7 @@ export async function listen (handle: http.RequestListener, opts: Partial<Listen
return <Listener>{
url,
server,
open: _open,
close
}
}
Expand Down

0 comments on commit 2b4a3f8

Please sign in to comment.