Skip to content
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

test: listen on ::1 #60

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions test/demo/detect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { detect } from '../../dist/esm/index.js';

detect(7001)
.then(port => {
console.log(port);
})
.catch(err => {
console.error(err);
});
8 changes: 8 additions & 0 deletions test/demo/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createServer } from 'node:http';
import { hostname } from 'node:os';

const server = createServer();

server.listen(7001, hostname(), () => {
console.log('listening %s:7001, address: %o', hostname(), server.address());
});
Comment on lines +6 to +8
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling and IPv6 support

Given the PR's objective to support IPv6 (::1), consider these improvements:

  1. Add error event handler
  2. Support IPv6 explicitly
-server.listen(7001, hostname(), () => {
+server.on('error', (err) => {
+  console.error('Server error:', err);
+});
+
+// Support both IPv4 and IPv6
+server.listen(7001, '::', () => {
   console.log('listening %s:7001, address: %o', hostname(), server.address());
 });
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
server.listen(7001, hostname(), () => {
console.log('listening %s:7001, address: %o', hostname(), server.address());
});
server.on('error', (err) => {
console.error('Server error:', err);
});
// Support both IPv4 and IPv6
server.listen(7001, '::', () => {
console.log('listening %s:7001, address: %o', hostname(), server.address());
});

14 changes: 14 additions & 0 deletions test/detect-port.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ describe('test/detect-port.test.ts', () => {
});
servers.push(server4);

const server5 = new net.Server();
server5.listen(25500, '::1', cb);
server5.on('error', err => {
console.error('listen ::1 error:', err);
});
servers.push(server5);

for (let port = 27000; port < 27010; port++) {
const server = new net.Server();
if (port % 3 === 0) {
Expand Down Expand Up @@ -102,6 +109,13 @@ describe('test/detect-port.test.ts', () => {
assert.equal(realPort, 25001);
});

it('work with listening next port 25501 because 25500 was listened to ::1', async () => {
const port = 25500;
const realPort = await detectPort(port);
assert(realPort);
assert.equal(realPort, 25501);
});

it('should listen next port 24001 when localhost is not binding', async () => {
mm(dns, 'lookup', (...args: any[]) => {
mm.restore();
Expand Down
Loading