Skip to content

Commit

Permalink
Fix typing + Add a failure event
Browse files Browse the repository at this point in the history
  • Loading branch information
Vuzi committed Jul 2, 2021
1 parent adb391b commit b15638f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
19 changes: 12 additions & 7 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const RANGE_REGEX = /bytes=(\d+)?-(\d+)?/
interface ServerEvents {
'start': (host: string, port: number, source: string) => void
'error': (path: string, e: AppError, time: number) => void
'failure': (error: Error) => void,
'response': (path: string, r: Response, time: number) => void
'request': (req: IncomingMessage) => void
}
Expand Down Expand Up @@ -82,7 +83,7 @@ export class StaticServer extends TypedEmitter<ServerEvents> {
this.source = conf.source
this.noCache = conf.noCache
this.allowedOrigins = conf.allowedOrigins.find(v => v === '*') ? [] : conf.allowedOrigins
this.directory = path.join(__dirname, conf.source)
this.directory = path.normalize(conf.source)
this.serverOrigin = `http://${conf.host}:${conf.port}`

this.encoders = {
Expand All @@ -94,12 +95,16 @@ export class StaticServer extends TypedEmitter<ServerEvents> {
}

start(): void {
this.server = http.createServer((req, res) => this.handleRequest(req, res))
this.terminator = createHttpTerminator({ server: this.server })

this.server.listen(this.port, this.host, () => {
this.emit('start', this.host, this.port, this.source)
})
try {
this.server = http.createServer((req, res) => this.handleRequest(req, res))
this.terminator = createHttpTerminator({ server: this.server })

this.server.listen(this.port, this.host, () => {
this.emit('start', this.host, this.port, this.source)
})
} catch (e) {
this.emit('failure', e)
}
}

async stop(): Promise<void> {
Expand Down
6 changes: 5 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"outDir": "dist",
"declaration": true,
"rootDirs": [
"src",
"tests"
Expand All @@ -17,5 +18,8 @@
"@root/*": ["./*"]
}
},
"exclude": ["./tests"]
"exclude": [
"./tests",
"./dist"
]
}

0 comments on commit b15638f

Please sign in to comment.