-
Notifications
You must be signed in to change notification settings - Fork 25
feat: only use server listen to detect port #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
test/fixtures | ||
logs | ||
run | ||
coverage |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "eslint-config-egg" | ||
} |
This file was deleted.
This file was deleted.
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 was deleted.
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 |
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 }, () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 4 不支持这个解构 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 喔,看错,不是函数签名 |
||
port = server.address().port; | ||
server.close(); | ||
debug('get free port: %s', port); | ||
callback(null, port); | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
失败一次之后马上监听随机端口,确保操作系统分配成功。