Initial commit
commit
a35b0cb669
@ -0,0 +1,2 @@
|
||||
/node_modules/
|
||||
/lib/
|
@ -0,0 +1 @@
|
||||
/node_modules/
|
@ -0,0 +1,67 @@
|
||||
# await-server
|
||||
|
||||
Waits for a TCP server of some kind to become available, using Promises. Useful for things like build tooling and testing scripts.
|
||||
|
||||
Currently you can only watch ports on `localhost` - this is unlikely to change, since hammering a server with repeated connection attempts over the network is not generally appreciated :)
|
||||
|
||||
__Be aware that this library will keep polling *constantly*, and there is no limit to its attempts.__ You should therefore only ever use it in scenarios where you're *certain* that the server will come online at some point, such as buildscripts.
|
||||
|
||||
## License
|
||||
|
||||
[WTFPL](http://www.wtfpl.net/txt/copying/) or [CC0](https://creativecommons.org/publicdomain/zero/1.0/), whichever you prefer. A donation and/or attribution are appreciated, but not required.
|
||||
|
||||
## Donate
|
||||
|
||||
Maintaining open-source projects takes a lot of time, and the more donations I receive, the more time I can dedicate to open-source. If this module is useful to you, consider [making a donation](http://cryto.net/~joepie91/donate.html)!
|
||||
|
||||
You can donate using Bitcoin, PayPal, Flattr, cash-in-mail, SEPA transfers, and pretty much anything else. Thank you!
|
||||
|
||||
## Contributing
|
||||
|
||||
Pull requests welcome. Please make sure your modifications are in line with the overall code style, and ensure that you're editing the files in `src/`, not those in `lib/`.
|
||||
|
||||
Build tool of choice is `gulp`; simply run `gulp` while developing, and it will watch for changes.
|
||||
|
||||
Be aware that by making a pull request, you agree to release your modifications under the licenses stated above.
|
||||
|
||||
## Usage
|
||||
|
||||
A simple usage example:
|
||||
|
||||
```javascript
|
||||
const Promise = require("bluebird");
|
||||
const awaitServer = require("await-server");
|
||||
|
||||
Promise.try(() => {
|
||||
return awaitServer(3000);
|
||||
}).then(() => {
|
||||
console.log("Server has started!");
|
||||
});
|
||||
```
|
||||
|
||||
By default, this library uses a timeout of 50ms, which translates to roughly 20 polling attempts per second.
|
||||
|
||||
If you want to specify a custom timeout - for example, to reduce attempts per second or to get a faster response time, you can do so:
|
||||
|
||||
```javascript
|
||||
const Promise = require("bluebird");
|
||||
const awaitServer = require("await-server");
|
||||
|
||||
Promise.try(() => {
|
||||
return awaitServer(3000, {
|
||||
timeout: 300 // in milliseconds
|
||||
});
|
||||
}).then(() => {
|
||||
console.log("Server has started!");
|
||||
});
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### awaitServer(port, [options])
|
||||
|
||||
Returns a Promise, that resolves when a TCP server comes online on the specified port.
|
||||
|
||||
* __port__: The port to check.
|
||||
* __options__: *Optional.*
|
||||
* __timeout__: *Defaults to `50`.* The timeout in milliseconds for each connection attempt. Setting this too low may result in never detecting the server coming online.
|
@ -0,0 +1,18 @@
|
||||
var gulp = require("gulp");
|
||||
var presetES2015 = require("@joepie91/gulp-preset-es2015");
|
||||
|
||||
var source = ["src/**/*.js"]
|
||||
|
||||
gulp.task('babel', function() {
|
||||
return gulp.src(source)
|
||||
.pipe(presetES2015({
|
||||
basePath: __dirname
|
||||
}))
|
||||
.pipe(gulp.dest("lib/"));
|
||||
});
|
||||
|
||||
gulp.task("watch", function () {
|
||||
gulp.watch(source, ["babel"]);
|
||||
});
|
||||
|
||||
gulp.task("default", ["babel", "watch"]);
|
@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = require("./lib");
|
@ -0,0 +1,30 @@
|
||||
{
|
||||
"name": "await-server",
|
||||
"version": "1.0.0",
|
||||
"description": "Waits for a TCP server of some kind to become available",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "http://git.cryto.net/joepie91/node-await-server.git"
|
||||
},
|
||||
"keywords": [
|
||||
"tcp",
|
||||
"wait",
|
||||
"promises",
|
||||
"server"
|
||||
],
|
||||
"author": "Sven Slootweg",
|
||||
"license": "WTFPL",
|
||||
"dependencies": {
|
||||
"bluebird": "^3.3.3",
|
||||
"default-value": "^1.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@joepie91/gulp-preset-es2015": "^1.0.1",
|
||||
"babel-preset-es2015": "^6.6.0",
|
||||
"gulp": "^3.9.1"
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
'use strict';
|
||||
|
||||
const Promise = require("bluebird");
|
||||
const net = require("net");
|
||||
const defaultValue = require("default-value");
|
||||
|
||||
module.exports = function waitForServer(port, options = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
function check() {
|
||||
let sock = new net.Socket();
|
||||
sock.setTimeout(defaultValue(options.timeout, 50));
|
||||
|
||||
sock
|
||||
.on("connect", () => {
|
||||
resolve();
|
||||
sock.destroy();
|
||||
})
|
||||
.on("timeout", check)
|
||||
.on("error", check)
|
||||
.connect(port);
|
||||
}
|
||||
|
||||
return check();
|
||||
});
|
||||
};
|
Loading…
Reference in New Issue