From 975646d3360191e09fe461364351d7c4f1fb4909 Mon Sep 17 00:00:00 2001 From: Jonatan Nilsson Date: Sun, 27 Mar 2022 14:48:11 +0000 Subject: [PATCH] Flaska: If http server supports listenAsync (service-core for example) then use that instead of callback listen() --- flaska.mjs | 4 ++++ package.json | 2 +- test/flaska.api.test.mjs | 49 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/flaska.mjs b/flaska.mjs index 2db8339..b3329ba 100644 --- a/flaska.mjs +++ b/flaska.mjs @@ -1066,6 +1066,10 @@ ctx.state.nonce = nonce; this.compile() this.server = this.http.createServer(this.requestStart.bind(this)) + if (this.server.listenAsync && typeof(this.server.listenAsync) === 'function') { + return this.server.listenAsync(port, ip) + } + return new Promise((res, rej) => { this.server.listen(port, ip, function(err) { if (err) return rej(err) diff --git a/package.json b/package.json index 7513e93..5aeac59 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "flaska", - "version": "1.2.0", + "version": "1.2.1", "description": "Flaska is a micro web-framework for node. It is designed to be fast, simple and lightweight, and is distributed as a single file module with no dependencies.", "main": "flaska.mjs", "scripts": { diff --git a/test/flaska.api.test.mjs b/test/flaska.api.test.mjs index e92b1cd..c9b827d 100644 --- a/test/flaska.api.test.mjs +++ b/test/flaska.api.test.mjs @@ -925,7 +925,7 @@ t.describe('#listenAsync()', function() { assert.strictEqual(checkIp, assertIp) }) - t.test('call http correctly if only port specified', async function() { + t.test('call http and listen correctly if only port specified', async function() { const assertPort = 325897235 let checkPort = null let checkIp = null @@ -945,6 +945,53 @@ t.describe('#listenAsync()', function() { assert.strictEqual(checkPort, assertPort) assert.strictEqual(checkIp, '::') }) + + t.test('call http and listenAsync correctly if supported', async function() { + const assertPort = 4632 + const assertIp = 'asdf' + const assertReturns = { a: 1 } + const stubListenAsync = stub().resolves(assertReturns) + + let flaska = new Flaska({}, { + createServer: function() { + return { + listenAsync: stubListenAsync, + } + } + }) + assert.ok(flaska.requestStart) + flaska.requestStart = function() { + checkInternalThis = this + checkIsTrue = true + } + let res = await flaska.listenAsync(assertPort, assertIp) + assert.strictEqual(res, assertReturns) + assert.strictEqual(stubListenAsync.firstCall[0], assertPort) + assert.strictEqual(stubListenAsync.firstCall[1], assertIp) + }) + + t.test('call http and listenAsync correctly if supported and ip is null', async function() { + const assertPort = 325897235 + const assertReturns = { a: 1 } + const stubListenAsync = stub().resolves(assertReturns) + + let flaska = new Flaska({}, { + createServer: function() { + return { + listenAsync: stubListenAsync, + } + } + }) + assert.ok(flaska.requestStart) + flaska.requestStart = function() { + checkInternalThis = this + checkIsTrue = true + } + let res = await flaska.listenAsync(assertPort) + assert.strictEqual(res, assertReturns) + assert.strictEqual(stubListenAsync.firstCall[0], assertPort) + assert.strictEqual(stubListenAsync.firstCall[1], '::') + }) t.test('register requestStart if no async', async function() { let checkIsTrue = false