-
Notifications
You must be signed in to change notification settings - Fork 28
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
feat: refactor with async/await instead of callback #59
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 |
---|---|---|
@@ -1,6 +1,9 @@ | ||
import detectPort from './detect-port.js'; | ||
import { detectPort } from './detect-port.js'; | ||
|
||
export default detectPort; | ||
|
||
export { detectPort }; | ||
export * from './detect-port.js'; | ||
// keep alias detectPort to detect | ||
fengmk2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
export const detect = detectPort; | ||
|
||
export * from './wait-port.js'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,8 @@ import net from 'node:net'; | |
import { strict as assert } from 'node:assert'; | ||
import { ip } from 'address'; | ||
import mm from 'mm'; | ||
|
||
import detectPort from '../src/detect-port.js'; | ||
import detect from '../src/index.js'; | ||
import { detect as detect2, detectPort } from '../src/index.js'; | ||
|
||
describe('test/detect-port.test.ts', () => { | ||
afterEach(mm.restore); | ||
|
@@ -37,6 +37,13 @@ describe('test/detect-port.test.ts', () => { | |
server3.listen(28080, '0.0.0.0', cb); | ||
servers.push(server3); | ||
|
||
const server4 = new net.Server(); | ||
server4.listen(25000, '127.0.0.1', cb); | ||
server4.on('error', err => { | ||
console.error('listen 127.0.0.1 error:', err); | ||
}); | ||
servers.push(server4); | ||
Comment on lines
+40
to
+45
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. 💡 Codebase verification Inconsistent error handling patterns found across server setups The verification reveals inconsistency in error handling:
🔗 Analysis chainLGTM! Server setup addresses the reported issue The addition of server4 listening on 127.0.0.1:25000 directly addresses the port detection issue reported in #57. The error handling is consistent with existing patterns. Let's verify that all server setups follow consistent patterns: 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Verify consistency in server setup patterns
# Expected: All server setups should follow similar error handling patterns
rg -A 5 "new net.Server\(\)" test/detect-port.test.ts
Length of output: 918 |
||
|
||
for (let port = 27000; port < 27010; port++) { | ||
const server = new net.Server(); | ||
if (port % 3 === 0) { | ||
|
@@ -68,6 +75,13 @@ describe('test/detect-port.test.ts', () => { | |
assert(port >= 1024 && port < 65535); | ||
}); | ||
|
||
it('should detect work', async () => { | ||
let port = await detect(); | ||
assert(port >= 1024 && port < 65535); | ||
port = await detect2(); | ||
assert(port >= 1024 && port < 65535); | ||
}); | ||
|
||
it('with occupied port, like "listen EACCES: permission denied"', async () => { | ||
const port = 80; | ||
const realPort = await detectPort(port); | ||
|
@@ -81,6 +95,13 @@ describe('test/detect-port.test.ts', () => { | |
assert.equal(realPort, 23001); | ||
}); | ||
|
||
it('work with listening next port 25001 because 25000 was listened to 127.0.0.1', async () => { | ||
const port = 25000; | ||
const realPort = await detectPort(port); | ||
assert(realPort); | ||
assert.equal(realPort, 25001); | ||
}); | ||
|
||
it('should listen next port 24001 when localhost is not binding', async () => { | ||
mm(dns, 'lookup', (...args: any[]) => { | ||
mm.restore(); | ||
|
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.
🛠️ Refactor suggestion
Refactor recursive calls to iterative loops
The functions
tryListen
andhandleError
use recursion to retry listening on incremented ports, which could lead to a stack overflow if many ports are unavailable. Consider refactoring this logic to use iterative loops to enhance reliability and prevent potential stack issues.🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 71-71: src/detect-port.ts#L71
Added line #L71 was not covered by tests
[warning] 83-84: src/detect-port.ts#L83-L84
Added lines #L83 - L84 were not covered by tests
[warning] 92-93: src/detect-port.ts#L92-L93
Added lines #L92 - L93 were not covered by tests
[warning] 99-100: src/detect-port.ts#L99-L100
Added lines #L99 - L100 were not covered by tests
[warning] 106-111: src/detect-port.ts#L106-L111
Added lines #L106 - L111 were not covered by tests
[warning] 117-117: src/detect-port.ts#L117
Added line #L117 was not covered by tests