From b15638ffb9fd6ae7a9d3b6f80a7f0ba167ceb0fe Mon Sep 17 00:00:00 2001 From: Guillaume Villerez Date: Fri, 2 Jul 2021 17:32:06 +0200 Subject: [PATCH] Fix typing + Add a failure event --- src/server.ts | 19 ++++++++++++------- tsconfig.json | 6 +++++- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/server.ts b/src/server.ts index c04e00e..3f6c4ec 100644 --- a/src/server.ts +++ b/src/server.ts @@ -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 } @@ -82,7 +83,7 @@ export class StaticServer extends TypedEmitter { 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 = { @@ -94,12 +95,16 @@ export class StaticServer extends TypedEmitter { } 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 { diff --git a/tsconfig.json b/tsconfig.json index a44df88..28c0a04 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,6 +8,7 @@ "skipLibCheck": true, "forceConsistentCasingInFileNames": true, "outDir": "dist", + "declaration": true, "rootDirs": [ "src", "tests" @@ -17,5 +18,8 @@ "@root/*": ["./*"] } }, - "exclude": ["./tests"] + "exclude": [ + "./tests", + "./dist" + ] } \ No newline at end of file