Skip to content
Permalink
Browse files
fix(local-server): Fixed the issue with the local server not getting …
…stopped correctly
  • Loading branch information
jan-molak committed Apr 10, 2019
1 parent 6dc4464 commit 9b0ea01cb0290e750b6bc60695c94307ba4fdd91
@@ -47,7 +47,8 @@
"npm": ">= 5"
},
"dependencies": {
"get-port": "4.0.0"
"get-port": "^5.0.0",
"http-shutdown": "^1.2.0"
},
"peerDependencies": {
"@serenity-js/core": "2.x"
@@ -1,6 +1,7 @@
import { Ability, UsesAbilities } from '@serenity-js/core';
import getPort = require('get-port');
import * as http from 'http';
import withShutdownSupport = require('http-shutdown');
import * as net from 'net';

/**
@@ -53,7 +54,7 @@ export class ManageALocalServer implements Ability {
? http.createServer(listener)
: listener;

return new ManageALocalServer(server);
return new ManageALocalServer(withShutdownSupport(server));
}

/**
@@ -70,9 +71,11 @@ export class ManageALocalServer implements Ability {

/**
* @param {net~Server} server
* A Node.js server requestListener
* A Node.js server requestListener, with support for server shutdown.
*
* @see https://www.npmjs.com/package/http-shutdown
*/
constructor(private readonly server: net.Server) {
constructor(private readonly server: net.Server & { shutdown: (callback: (error?: Error) => void) => void }) {
}

/**
@@ -83,7 +86,7 @@ export class ManageALocalServer implements Ability {
* @returns {Promise<void>}
*/
listen(preferredPorts: number[]): Promise<void> {
return getPort(preferredPorts).then(port => new Promise((resolve, reject) => {
return getPort({ port: preferredPorts }).then(port => new Promise<void>((resolve, reject) => {
this.server.listen(port, '127.0.0.1', (error: Error) => {
if (!! error) {
return reject(error);
@@ -101,7 +104,7 @@ export class ManageALocalServer implements Ability {
* @param fn
* @returns {T}
*/
mapInstance<T>(fn: (server: net.Server) => T): T {
mapInstance<T>(fn: (server: net.Server & { shutdown: (callback: (error?: Error) => void) => void }) => T): T {
return fn(this.server);
}
}
@@ -8,7 +8,7 @@ export class StopLocalServer extends Interaction {

performAs(actor: UsesAbilities & CollectsArtifacts & AnswersQuestions): Promise<void> {
return ManageALocalServer.as(actor).mapInstance(server => new Promise((resolve, reject) => {
server.close((error: Error) => {
server.shutdown((error: Error) => {
if (!! error) {
return reject(error);
}

0 comments on commit 9b0ea01

Please sign in to comment.