Skip to content

Commit 687a374

Browse files
committed
f
1 parent f6ce4d8 commit 687a374

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

README.md

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,40 @@
4242
npm i detect-port
4343
```
4444

45+
CommonJS
46+
4547
```javascript
46-
const detect = require('detect-port');
47-
/**
48-
* use as a promise
49-
*/
48+
const { detect } = require('detect-port');
5049

5150
detect(port)
52-
.then(_port => {
53-
if (port == _port) {
51+
.then(realPort => {
52+
if (port == realPort) {
5453
console.log(`port: ${port} was not occupied`);
5554
} else {
56-
console.log(`port: ${port} was occupied, try port: ${_port}`);
55+
console.log(`port: ${port} was occupied, try port: ${realPort}`);
5756
}
5857
})
5958
.catch(err => {
6059
console.log(err);
6160
});
61+
```
62+
63+
ESM and TypeScript
6264

65+
```ts
66+
import { detect } from 'detect-port';
67+
68+
detect(port)
69+
.then(realPort => {
70+
if (port == realPort) {
71+
console.log(`port: ${port} was not occupied`);
72+
} else {
73+
console.log(`port: ${port} was occupied, try port: ${realPort}`);
74+
}
75+
})
76+
.catch(err => {
77+
console.log(err);
78+
});
6379
```
6480

6581
## Command Line Tool

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ import { detectPort } from './detect-port.js';
33
export default detectPort;
44

55
export * from './detect-port.js';
6+
// keep alias detectPort to detect
7+
export const detect = detectPort;
8+
69
export * from './wait-port.js';

test/detect-port.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { strict as assert } from 'node:assert';
44
import { ip } from 'address';
55
import mm from 'mm';
66
import { detectPort } from '../src/detect-port.js';
7+
import detect from '../src/index.js';
8+
import { detect as detect2 } from '../src/index.js';
79

810
describe('test/detect-port.test.ts', () => {
911
afterEach(mm.restore);
@@ -74,6 +76,13 @@ describe('test/detect-port.test.ts', () => {
7476
assert(port >= 1024 && port < 65535);
7577
});
7678

79+
it('should detect work', async () => {
80+
let port = await detect();
81+
assert(port >= 1024 && port < 65535);
82+
port = await detect2();
83+
assert(port >= 1024 && port < 65535);
84+
});
85+
7786
it('with occupied port, like "listen EACCES: permission denied"', async () => {
7887
const port = 80;
7988
const realPort = await detectPort(port);

0 commit comments

Comments
 (0)