forked from hyperledger-cacti/cacti
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Useful when working with HTTP servers especially for test case authoring where there's a lot of server creation for the purpose of allocating random ports prior to launching an API server instance. Fixes hyperledger-cacti#260 Fixes hyperledger-cacti#267 Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
- Loading branch information
Showing
4 changed files
with
69 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 0 additions & 32 deletions
32
packages/cactus-cmd-api-server/src/main/typescript/common/servers.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./public-api"; | ||
export { IListenOptions, Servers } from "./servers"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { AddressInfo, ListenOptions } from "net"; | ||
import { Server } from "http"; | ||
import { Server as SecureServer } from "https"; | ||
import { Checks } from "./checks"; | ||
|
||
export interface IListenOptions { | ||
server: Server | SecureServer; | ||
port: number; | ||
hostname: string; | ||
} | ||
|
||
/** | ||
* Utility class for handling common tasks for NodeJS HTTP/S server objects. | ||
*/ | ||
export class Servers { | ||
/** | ||
* Returns with a promise that resolves when the server has been shut down. Rejects if anything goes wrong of if the | ||
* parameters are invalid. | ||
* | ||
* @param server The server object that will be shut down. | ||
*/ | ||
public static async shutdown(server: Server | SecureServer): Promise<void> { | ||
if (!server) { | ||
throw new TypeError(`Servers#shutdown() server was falsy. Need object.`); | ||
} | ||
return new Promise<void>((resolve, reject) => { | ||
server.close((err: any) => { | ||
if (err) { | ||
reject( | ||
new Error( | ||
`Servers#shutdown() Failed to shut down server: ${err.stack}` | ||
) | ||
); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
public static async listen(options: IListenOptions): Promise<any> { | ||
const fnTag = "Servers#listen()"; | ||
|
||
Checks.truthy(options, `${fnTag} arg options`); | ||
Checks.truthy(options.server, `${fnTag} arg options.server`); | ||
Checks.truthy(options.port, `${fnTag} arg options.port`); | ||
Checks.truthy(options.hostname, `${fnTag} arg options.hostname`); | ||
const { server, port, hostname } = options; | ||
|
||
return new Promise((resolve, reject) => { | ||
server.on("error", (ex) => reject(ex)); | ||
|
||
const listenOptions: ListenOptions = { | ||
host: hostname, | ||
port, | ||
}; | ||
|
||
// called when the `listening` event is fired | ||
const onListeningHandler = () => { | ||
const addressInfo = options.server.address() as AddressInfo; | ||
resolve(addressInfo); | ||
}; | ||
|
||
server.listen(listenOptions, onListeningHandler); | ||
}); | ||
} | ||
} |