Skip to content

Commit

Permalink
[[FEAT]] Add support for ES6 imports (#3)
Browse files Browse the repository at this point in the history
Chore: Updates the ts runtime to ts2
  • Loading branch information
jmendiara authored and JoseAntonioRodriguez committed Sep 23, 2016
1 parent 9074dc1 commit c8d17c4
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ server.terminate(function(err, terminatedByTimeout) {
Or if you are using TypeScript:
```typescript
import * as http from 'http';
import enableTerminate = require('server-terminate');
import enableTerminate from 'server-terminate';

let server: http.Server = http.createServer(function onRequest(req: http.ServerRequest, res: http.ServerResponse) {
// Do your stuff here
Expand Down
19 changes: 17 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import * as http from 'http';
/**
* Allow terminating an HTTP server in an orderly fashion
*/
import { Server } from 'http';

declare function enableTerminate(server: http.Server, opts?: Options): http.Server;
interface enableTerminateSignature {
/** Adds the `terminate` function to the provided server instance */
(server: Server, opts?: Options): Server;
default: enableTerminateSignature;
}

declare var enableTerminate: enableTerminateSignature;
export = enableTerminate;

interface Options {
Expand All @@ -9,6 +18,12 @@ interface Options {

declare module 'http' {
export interface Server {
/**
* Terminates the server in an orderly fashion by
* - closing keep-alive connections that are not being used by any HTTP request.
* - waiting for running HTTP requests to finish before closing their connections.
* - closing connections with running HTTP requests after the provided timeout
*/
terminate(cb: (err: Error, terminatedByTimeout: boolean) => void): Server;
}
}
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@
*/

'use strict';

module.exports = function enableTerminate(server, opts) {
module.exports = enableTerminate;
// Support for ES6 imports.
// Now you can make `import enableTerminate from 'enable-terminate'`
// and get a reference to this function
enableTerminate.default = enableTerminate;
function enableTerminate(server, opts) {
opts = opts || {};
opts.timeout = opts.timeout || 30 * 1000;

Expand Down Expand Up @@ -101,4 +105,4 @@ module.exports = function enableTerminate(server, opts) {
};

return server;
};
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"close"
],
"devDependencies": {
"@types/node": "^6.0.40",
"chai": "^3.5.0",
"conventional-changelog-cli": "^1.2.0",
"eslint": "^2.13.1",
Expand All @@ -38,7 +39,6 @@
"tartare": "^1.0.2",
"tslint": "^3.15.1",
"tslint-config-typings": "^0.2.3",
"typescript": "^1.8.10",
"typings": "^1.3.3"
"typescript": "^2.0.3"
}
}
14 changes: 13 additions & 1 deletion test/server-terminate.typings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as http from 'http';
import enableTerminate = require('../');
import enableTerminate from '..';
import enableTerminate2 = require('..');

let server: http.Server = http.createServer(function onRequest(req: http.ServerRequest, res: http.ServerResponse) {
res.writeHead(200);
Expand All @@ -13,4 +14,15 @@ server.listen(8080);
server.terminate((err, terminatedByTimeout) => {
// You get here when all connections have been closed
console.log('Server terminated. terminatedByTimeout:', terminatedByTimeout); // tslint:disable-line no-console

server = enableTerminate2(server);
server = enableTerminate2(server, {});
server = enableTerminate2(server, {timeout: 2000});
server.listen(8080);

server.terminate((err, terminatedByTimeout) => {
// You get here when all connections have been closed
console.log('Server terminated2. terminatedByTimeout:', terminatedByTimeout); // tslint:disable-line no-console
});

});
5 changes: 0 additions & 5 deletions typings.json

This file was deleted.

0 comments on commit c8d17c4

Please sign in to comment.