-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: only use server listen to detect port (#13)
use npm scripts instead of Makefile
- Loading branch information
Showing
13 changed files
with
146 additions
and
214 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
test/fixtures | ||
logs | ||
run | ||
coverage |
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,3 @@ | ||
{ | ||
"extends": "eslint-config-egg" | ||
} |
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,8 +1,12 @@ | ||
sudo: false | ||
language: node_js | ||
node_js: | ||
- "4" | ||
- "5" | ||
- "6" | ||
sudo: false | ||
script: make travis | ||
after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" | ||
- '4' | ||
- '6' | ||
- '7' | ||
install: | ||
- npm i npminstall && npminstall | ||
script: | ||
- npm run ci | ||
after_script: | ||
- npminstall codecov && codecov |
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
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,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 |
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,73 +1,39 @@ | ||
'use strict'; | ||
|
||
const debug = require('debug')('detect-port'); | ||
const net = require('net'); | ||
|
||
module.exports = function() { | ||
const args = Array.prototype.slice.call(arguments); | ||
|
||
const promise = new Promise((resolve, reject) => { | ||
if (!args.length) { | ||
return reject('wrong number of arguments'); | ||
} | ||
|
||
const port = parseInt(args[0], 10); | ||
|
||
if (isNaN(port)) { | ||
return reject(`wrong type of arguments with: '${args[0]}'`); | ||
} | ||
|
||
const loop = port => { | ||
const socket = new net.Socket(); | ||
|
||
socket.once('error', () => { | ||
socket.removeAllListeners('error'); | ||
socket.removeAllListeners('connect'); | ||
socket.end(); | ||
socket.destroy(); | ||
socket.unref(); | ||
|
||
const server = new net.Server(); | ||
|
||
server.on('error', () => { | ||
port++; | ||
loop(port); | ||
}); | ||
|
||
server.listen(port, () => { | ||
server.once('close', () => { | ||
resolve(port); | ||
}); | ||
server.close(); | ||
}); | ||
}); | ||
|
||
socket.once('connect', () => { | ||
port++; | ||
loop(port); | ||
socket.removeAllListeners('error'); | ||
socket.removeAllListeners('connect'); | ||
socket.end(); | ||
socket.destroy(); | ||
socket.unref(); | ||
}); | ||
module.exports = (port, callback) => { | ||
if (typeof port === 'function') { | ||
callback = port; | ||
port = null; | ||
} | ||
if (typeof callback === 'function') { | ||
return tryListen(port, callback); | ||
} | ||
// promise | ||
return new Promise(resolve => { | ||
tryListen(port, (_, realPort) => { | ||
resolve(realPort); | ||
}); | ||
}); | ||
}; | ||
|
||
socket.connect({ | ||
port: port | ||
}); | ||
}; | ||
function tryListen(port, callback) { | ||
port = parseInt(port) || 0; | ||
const server = new net.Server(); | ||
|
||
loop(port); | ||
server.on('error', err => { | ||
debug('listen %s error: %s', port, err); | ||
port = 0; | ||
server.close(); | ||
return tryListen(port, callback); | ||
}); | ||
|
||
if (args.length > 1) { | ||
const cb = args[1]; | ||
|
||
promise.then(data => { | ||
process.nextTick(cb.bind(null, null, data)); | ||
}, err => { | ||
process.nextTick(cb.bind(null, err)); | ||
}); | ||
} else { | ||
return promise; | ||
} | ||
}; | ||
server.listen({ port }, () => { | ||
port = server.address().port; | ||
server.close(); | ||
debug('get free port: %s', port); | ||
callback(null, port); | ||
}); | ||
} |
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
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
Oops, something went wrong.