Skip to content

Commit

Permalink
Ava->Mocha, prettier v1->v2, upgrade deps
Browse files Browse the repository at this point in the history
  • Loading branch information
koresar committed Feb 23, 2022
1 parent ef1c4f5 commit b31640f
Show file tree
Hide file tree
Showing 9 changed files with 300 additions and 292 deletions.
22 changes: 12 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@
},
"description": "Zero dependencies, gracefully terminates HTTP(S) server.",
"devDependencies": {
"agentkeepalive": "^4.1.4",
"ava": "^3.15.0",
"eslint": "^6.8.0",
"got": "^11.8.1",
"agentkeepalive": "^4.2.1",
"ava": "^4.0.1",
"eslint": "^8.9.0",
"got": "^11.8.3",
"mocha": "^9.2.1",
"nyc": "^15.1.0",
"pem": "^1.14.4",
"prettier": "^1.19.1"
"pem": "^1.14.6",
"prettier": "^2.5.1"
},
"eslintConfig": {
"parserOptions": {
"ecmaVersion": 2017
"ecmaVersion": 2021
},
"env": {
"es6": true,
"node": true
"node": true,
"mocha": true
},
"extends": "eslint:recommended"
},
Expand Down Expand Up @@ -50,8 +52,8 @@
},
"scripts": {
"ci": "npm i && npm t && npm run lint",
"test": "NODE_ENV=test ava --serial --verbose",
"cov": "NODE_ENV=test nyc --reporter=lcov --reporter=text-summary ava --serial --verbose",
"test": "NODE_ENV=test mocha --recursive --exit",
"cov": "NODE_ENV=test nyc --reporter=lcov --reporter=text-summary mocha --recursive --exit",
"lint": "eslint ./src ./test"
},
"version": "1.2.1"
Expand Down
2 changes: 1 addition & 1 deletion src/delay.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = function delay(time) {
return new Promise(resolve => setTimeout(() => resolve(), time).unref());
return new Promise((resolve) => setTimeout(() => resolve(), time).unref());
};
18 changes: 9 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = function HttpTerminator({
server,
gracefulTerminationTimeout = 1000,
maxWaitTimeout = 30000,
logger = console
logger = console,
} = {}) {
assert(server);

Expand All @@ -22,7 +22,7 @@ module.exports = function HttpTerminator({

let terminating;

server.on("connection", socket => {
server.on("connection", (socket) => {
if (terminating) {
socket.destroy();
} else {
Expand All @@ -34,7 +34,7 @@ module.exports = function HttpTerminator({
}
});

server.on("secureConnection", socket => {
server.on("secureConnection", (socket) => {
if (terminating) {
socket.destroy();
} else {
Expand Down Expand Up @@ -79,14 +79,14 @@ module.exports = function HttpTerminator({
let resolveTerminating;

terminating = Promise.race([
new Promise(resolve => {
new Promise((resolve) => {
resolveTerminating = resolve;
}),
delay(maxWaitTimeout).then(() => ({
success: false,
code: "TIMED_OUT",
message: `Server didn't close in ${maxWaitTimeout} msec`
}))
message: `Server didn't close in ${maxWaitTimeout} msec`,
})),
]);

server.on("request", (incomingMessage, outgoingMessage) => {
Expand Down Expand Up @@ -141,15 +141,15 @@ module.exports = function HttpTerminator({
}
}

server.close(error => {
server.close((error) => {
if (error) {
logger.warn("lil-http-terminator: server error while closing", error);
resolveTerminating({ success: false, code: "SERVER_ERROR", message: error.message, error });
} else {
resolveTerminating({
success: true,
code: "TERMINATED",
message: "Server successfully closed"
message: "Server successfully closed",
});
}
});
Expand All @@ -159,6 +159,6 @@ module.exports = function HttpTerminator({
logger.warn("lil-http-terminator: internal error", error);
return { success: false, code: "INTERNAL_ERROR", message: error.message, error };
}
}
},
};
};
4 changes: 2 additions & 2 deletions test/helpers/createHttpServer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { createServer } = require("http");
const { promisify } = require("util");

module.exports = requestHandler => {
module.exports = (requestHandler) => {
const server = createServer(requestHandler);

let serverShuttingDown;
Expand Down Expand Up @@ -32,7 +32,7 @@ module.exports = requestHandler => {
port,
server,
stop,
url
url,
});
});
});
Expand Down
8 changes: 4 additions & 4 deletions test/helpers/createHttpsServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ const { createServer } = require("https");
const { promisify } = require("util");
const pem = require("pem");

module.exports = async requestHandler => {
module.exports = async (requestHandler) => {
const { serviceKey, certificate, csr } = await promisify(pem.createCertificate)({
days: 365,
selfSigned: true
selfSigned: true,
});

const httpsOptions = {
ca: csr,
cert: certificate,
key: serviceKey
key: serviceKey,
};

const server = createServer(httpsOptions, requestHandler);
Expand Down Expand Up @@ -44,7 +44,7 @@ module.exports = async requestHandler => {
port,
server,
stop,
url
url,
});
});
});
Expand Down
60 changes: 30 additions & 30 deletions test/helpers/createTests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const assert = require("assert");
const KeepAliveHttpAgent = require("agentkeepalive");
const test = require("ava");
const delay = require("../../src/delay");
const safeGot = require("got");
const createHttpTerminator = require("../../src");
Expand All @@ -12,31 +12,31 @@ const got = safeGot.extend({

const KeepAliveHttpsAgent = KeepAliveHttpAgent.HttpsAgent;

module.exports = createHttpServer => {
test("terminates HTTP server with no connections", async t => {
module.exports = function(createHttpServer) {
it("terminates HTTP server with no connections", async function() {
const httpServer = await createHttpServer(() => {});

t.timeout(100);
this.timeout(1000);

t.true(httpServer.server.listening);
assert.strictEqual(true, httpServer.server.listening);

const terminator = createHttpTerminator({
server: httpServer.server
});

await terminator.terminate();

t.false(httpServer.server.listening);
assert.notStrictEqual(httpServer.server.listening, true);
});

test("terminates hanging sockets after gracefulTerminationTimeout", async t => {
it("terminates hanging sockets after gracefulTerminationTimeout", async function() {
let serverCreated = false;

const httpServer = await createHttpServer(() => {
serverCreated = true;
});

t.timeout(500);
this.timeout(500);

const terminator = createHttpTerminator({
gracefulTerminationTimeout: 150,
Expand All @@ -47,21 +47,21 @@ module.exports = createHttpServer => {

await delay(50);

t.true(serverCreated);
assert.strictEqual(true, serverCreated);

terminator.terminate();

await delay(100);

// The timeout has not passed.
t.is(await httpServer.getConnections(), 1);
assert.strictEqual(await httpServer.getConnections(), 1);

await delay(100);

t.is(await httpServer.getConnections(), 0);
assert.strictEqual(await httpServer.getConnections(), 0);
});

test("server stops accepting new connections after terminator.terminate() is called", async t => {
it("server stops accepting new connections after terminator.terminate() is called", async function() {
let callCount = 0;

function requestHandler(incomingMessage, outgoingMessage) {
Expand All @@ -77,7 +77,7 @@ module.exports = createHttpServer => {

const httpServer = await createHttpServer(requestHandler);

t.timeout(500);
this.timeout(500);

const terminator = createHttpTerminator({
gracefulTerminationTimeout: 150,
Expand All @@ -100,22 +100,22 @@ module.exports = createHttpServer => {
});

// @todo https://stackoverflow.com/q/59832897/368691
await t.throwsAsync(request1);
await assert.rejects(request1);

const response0 = await request0;

t.is(response0.headers.connection, "close");
t.is(response0.body, "foo");
assert.strictEqual(response0.headers.connection, "close");
assert.strictEqual(response0.body, "foo");
});

test("ongoing requests receive {connection: close} header", async t => {
it("ongoing requests receive {connection: close} header", async function() {
const httpServer = await createHttpServer((incomingMessage, outgoingMessage) => {
setTimeout(() => {
outgoingMessage.end("foo");
}, 100);
});

t.timeout(600);
this.timeout(600);

const terminator = createHttpTerminator({
gracefulTerminationTimeout: 150,
Expand Down Expand Up @@ -143,11 +143,11 @@ module.exports = createHttpServer => {

const response = await request;

t.is(response.headers.connection, "close");
t.is(response.body, "foo");
assert.strictEqual(response.headers.connection, "close");
assert.strictEqual(response.body, "foo");
});

test("ongoing requests receive {connection: close} header (new request reusing an existing socket)", async t => {
it("ongoing requests receive {connection: close} header (new request reusing an existing socket)", async function() {
let callCount = 0;

function requestHandler(incomingMessage, outgoingMessage) {
Expand All @@ -171,7 +171,7 @@ module.exports = createHttpServer => {

const httpServer = await createHttpServer(requestHandler);

t.timeout(1000);
this.timeout(1000);

const terminator = createHttpTerminator({
gracefulTerminationTimeout: 150,
Expand Down Expand Up @@ -207,27 +207,27 @@ module.exports = createHttpServer => {

await delay(75);

t.is(callCount, 2);
assert.strictEqual(callCount, 2);

const response0 = await request0;

t.is(response0.headers.connection, "keep-alive");
t.is(response0.body, "foobar");
assert.strictEqual(response0.headers.connection, "keep-alive");
assert.strictEqual(response0.body, "foobar");

const response1 = await request1;

t.is(response1.headers.connection, "close");
t.is(response1.body, "baz");
assert.strictEqual(response1.headers.connection, "close");
assert.strictEqual(response1.body, "baz");
});

test("does not send {connection: close} when server is not terminating", async t => {
it("does not send {connection: close} when server is not terminating", async function() {
const httpServer = await createHttpServer((incomingMessage, outgoingMessage) => {
setTimeout(() => {
outgoingMessage.end("foo");
}, 50);
});

t.timeout(100);
this.timeout(1000);

createHttpTerminator({
server: httpServer.server
Expand All @@ -248,6 +248,6 @@ module.exports = createHttpServer => {
}
});

t.is(response.headers.connection, "keep-alive");
assert.strictEqual(response.headers.connection, "keep-alive");
});
};
Loading

0 comments on commit b31640f

Please sign in to comment.