Added logging, changed some directory structure

This commit is contained in:
2018-01-13 21:33:40 -05:00
parent f079a5f067
commit 8e72ffb917
73656 changed files with 35284 additions and 53718 deletions

View File

@@ -0,0 +1,4 @@
test/fixtures
logs
run
coverage

View File

@@ -0,0 +1,3 @@
{
"extends": "eslint-config-egg"
}

View File

@@ -0,0 +1,13 @@
.travis.yml
.drone.yml
examples/
test/
Makefile
.jshintrc
.jshintignore
coverage/
*.html
*.sw*
plugins/
docs/
*.un~

View File

@@ -0,0 +1,5 @@
# Contributing to detect-port
- Fork the project, make a change, and send a pull request;
- Have a look at code style now before starting;
- Make sure the tests case (`$ make test`) pass before sending a pull request;

View File

@@ -0,0 +1,39 @@
1.1.2 / 2017-05-11
==================
* fix: should double check 0.0.0.0 and localhost (#20)
* docs: ignore type of port when checking if it's occupied (#18)
# 1.1.1 / 2017-03-17
* fix: try to use next available port (#16)
# 1.1.0 / 2016-01-17
* Use server listen to detect port
# 1.0.7 / 2016-12-11
* Early return for rejected promise
* Prevent promsie swallow in callback
# 1.0.6 / 2016-11-29
* Bump version for new Repo
# 0.1.4 / 2015-08-24
* Support promise
# 0.1.2 / 2014-05-31
* Fix commander
# 0.1.1 / 2014-05-30
* Add command line support
# 0.1.0 / 2014-05-29
* Initial release

View File

@@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright (c) 2015 xdf
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -0,0 +1,108 @@
[![logo][logo-image]][logo-url]
---
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][codecov-image]][codecov-url]
[![npm download][download-image]][download-url]
[logo-image]: ./logo.png
[logo-url]: https://npmjs.org/package/detect-port
[npm-image]: https://img.shields.io/npm/v/detect-port.svg?style=flat-square
[npm-url]: https://npmjs.org/package/detect-port
[travis-image]: https://img.shields.io/travis/node-modules/detect-port.svg?style=flat-square
[travis-url]: https://travis-ci.org/node-modules/detect-port
[codecov-image]: https://codecov.io/gh/node-modules/detect-port/branch/master/graph/badge.svg
[codecov-url]: https://codecov.io/gh/node-modules/detect-port
[download-image]: https://img.shields.io/npm/dm/detect-port.svg?style=flat-square
[download-url]: https://npmjs.org/package/detect-port
> JavaScript Implementation of Port Detector
## Usage
```bash
$ npm i detect-port --save
```
```js
const detect = require('detect-port');
/**
* callback usage
*/
detect(port, (err, _port) => {
if (err) {
console.log(err);
}
if (port == _port) {
console.log(`port: ${port} was not occupied`);
} else {
console.log(`port: ${port} was occupied, try port: ${_port}`);
}
});
/**
* for a yield syntax instead of callback function implement
*/
const co = require('co');
co(function *() {
const _port = yield detect(port);
if (port == _port) {
console.log(`port: ${port} was not occupied`);
} else {
console.log(`port: ${port} was occupied, try port: ${_port}`);
}
});
/**
* use as a promise
*/
detect(port)
.then(_port => {
if (port == _port) {
console.log(`port: ${port} was not occupied`);
} else {
console.log(`port: ${port} was occupied, try port: ${_port}`);
}
})
.catch(err => {
console.log(err);
});
```
## Command Line Tool
```shell
$ npm i detect-port -g
```
### Quick Start
```shell
# get an available port randomly
$ detect
# detect pointed port
$ detect 80
# more help
$ detect --help
```
## Authors
- [xudafeng](//github.com/xudafeng)
- [zenzhu](//github.com/zenzhu)
## License
[MIT](LICENSE)

View File

@@ -0,0 +1,16 @@
environment:
matrix:
- nodejs_version: '4'
- nodejs_version: '6'
- nodejs_version: '7'
install:
- ps: Install-Product node $env:nodejs_version
- npm i npminstall && node_modules\.bin\npminstall
test_script:
- node --version
- npm --version
- npm run ci
build: off

View File

@@ -0,0 +1,56 @@
#!/usr/bin/env node
'use strict';
const pkg = require('../package');
const args = process.argv.slice(2);
const arg_0 = args[0];
if (!!~['-v', '--version'].indexOf(arg_0)) {
console.log(pkg.version);
process.exit(0);
}
const port = parseInt(arg_0, 10);
const main = require('..');
if (!arg_0) {
const random = Math.floor(9000 + Math.random() * (65535 - 9000));
main(random, (err, port) => {
if (err) {
console.log(`get available port failed with ${err}`);
}
console.log(`get available port ${port} randomly`);
});
} else if (isNaN(port)) {
console.log();
console.log(` \u001b[37m${pkg.description}\u001b[0m`);
console.log();
console.log(' Usage:');
console.log();
console.log(` ${pkg.name} [port]`);
console.log();
console.log(' Options:');
console.log();
console.log(' -v, --version show version and exit');
console.log(' -h, --help output usage information');
console.log();
console.log(' Further help:');
console.log();
console.log(` ${pkg.homepage}`);
console.log();
} else {
main(port, (err, _port) => {
if (err) {
console.log(`get available port failed with ${err}`);
}
if (port !== _port) {
console.log(`port ${port} was occupied`);
}
console.log(`get available port ${_port}`);
});
}

View File

@@ -0,0 +1,3 @@
'use strict';
module.exports = require('./lib/detect-port');

View File

@@ -0,0 +1,102 @@
'use strict'
const debug = require('debug')('detect-port')
const net = require('net')
const address = require('address')
module.exports = (port, host, callback) => {
if (typeof port === 'function') {
callback = port
port = null
} else if (typeof host === 'function') {
callback = host
host = null
}
port = parseInt(port) || 0
let maxPort = port + 10
if (maxPort > 65535) {
maxPort = 65535
}
debug('detect free port between [%s, %s)', port, maxPort)
if (typeof callback === 'function') {
return tryListen(host, port, maxPort, callback)
}
// promise
return new Promise((resolve, reject) => {
tryListen(host, port, maxPort, (error, realPort) => {
if (error) {
reject(error);
} else {
resolve(realPort);
}
})
})
}
function tryListen(host, port, maxPort, callback) {
function handleError() {
port++
if (port >= maxPort) {
debug(
'port: %s >= maxPort: %s, give up and use random port',
port,
maxPort
)
port = 0
maxPort = 0
}
tryListen(host, port, maxPort, callback)
}
// 1. check specified host (or null)
listen(port, host, (err, realPort) => {
// ignore random listening
if (port === 0) {
return callback(err, realPort)
}
if (err) {
return handleError(err)
}
// 2. check default host
listen(port, null, err => {
if (err) {
return handleError(err)
}
// 3. check localhost
listen(port, 'localhost', err => {
if (err) {
return handleError(err)
}
// 4. check current ip
listen(port, address.ip(), (err, realPort) => {
if (err) {
return handleError(err)
}
callback(null, realPort)
})
})
})
})
}
function listen(port, hostname, callback) {
const server = new net.Server()
server.on('error', err => {
debug('listen %s:%s error: %s', hostname, port, err)
server.close()
return callback(err)
})
server.listen(port, hostname, () => {
port = server.address().port
server.close()
debug('get free %s:%s', hostname, port)
return callback(null, port)
})
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

View File

@@ -0,0 +1,74 @@
{
"_args": [
[
"detect-port-alt@1.1.3",
"C:\\Users\\deranjer\\go\\src\\github.com\\deranjer\\goTorrent\\torrent-project"
]
],
"_from": "detect-port-alt@1.1.3",
"_id": "detect-port-alt@1.1.3",
"_inBundle": false,
"_integrity": "sha1-pNLwYddXoDTs83xRQmCph1DysTE=",
"_location": "/react-scripts/detect-port-alt",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "detect-port-alt@1.1.3",
"name": "detect-port-alt",
"escapedName": "detect-port-alt",
"rawSpec": "1.1.3",
"saveSpec": null,
"fetchSpec": "1.1.3"
},
"_requiredBy": [
"/react-scripts/react-dev-utils"
],
"_resolved": "https://registry.npmjs.org/detect-port-alt/-/detect-port-alt-1.1.3.tgz",
"_spec": "1.1.3",
"_where": "C:\\Users\\deranjer\\go\\src\\github.com\\deranjer\\goTorrent\\torrent-project",
"bin": {
"detect": "./bin/detect-port",
"detect-port": "./bin/detect-port"
},
"bugs": {
"url": "https://github.com/node-modules/detect-port/issues"
},
"ci": {
"version": "4, 6, 7"
},
"dependencies": {
"address": "^1.0.1",
"debug": "^2.6.0"
},
"description": "detect available port",
"devDependencies": {
"command-line-test": "^1.0.8",
"egg-bin": "^1.10.3",
"egg-ci": "^1.1.0",
"eslint": "^3.13.1",
"eslint-config-egg": "^3.1.0",
"pedding": "^1.1.0"
},
"engines": {
"node": ">= 4.2.1"
},
"homepage": "https://github.com/node-modules/detect-port",
"keywords": [
"detect",
"port"
],
"license": "MIT",
"main": "index.js",
"name": "detect-port-alt",
"repository": {
"type": "git",
"url": "git://github.com/node-modules/detect-port.git"
},
"scripts": {
"ci": "npm run lint && egg-bin cov",
"lint": "eslint .",
"test": "egg-bin test"
},
"version": "1.1.3"
}

File diff suppressed because it is too large Load Diff