Skip to content

Commit

Permalink
feat: portRange support
Browse files Browse the repository at this point in the history
also defaulting range to 3000...3100
  • Loading branch information
pi0 committed Apr 11, 2022
1 parent 9d6388d commit 44c8acd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ interface GetPortOptions {

random?: boolean
port?: number
portRange?: [from: number, to: number]
ports?: number[]
host?: string

Expand All @@ -63,7 +64,11 @@ First port to check. Default is `process.env.PORT || 3000`

### `ports`

Alternative ports to check. Default is `[4000, 5000, 7000, 8000]`
Alternative ports to check.

### `portRange`

Alternative port range to check. Deefault is `[3000,3100]`

### `host`

Expand Down
19 changes: 17 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface GetPortOptions {
random: boolean
port: number
ports: number[]
portRange: [from: number, to: number]
host: string
memoDir: string
memoName: string
Expand All @@ -29,7 +30,8 @@ export async function getPort (config?: GetPortInput): Promise<PortNumber> {
name: 'default',
random: false,
port: parseInt(process.env.PORT || '') || 3000,
ports: [4000, 5000, 7000, 8000],
ports: [],
portRange: [3000, 3100],
host: undefined,
memoName: 'port',
...config
Expand All @@ -40,9 +42,11 @@ export async function getPort (config?: GetPortInput): Promise<PortNumber> {
}

// Ports to check

const portsToCheck: PortNumber[] = [
options.port,
...options.ports
...options.ports,
...generateRange(options.portRange[0], options.portRange[1])
].filter(port => port && isSafePort(port))

// Memo
Expand Down Expand Up @@ -108,6 +112,17 @@ export async function checkPort (port: PortNumber, host: HostAddress | HostAddre

// ----- Internal -----

function generateRange (from: number, to: number): number[] {
if (to < from) {
return []
}
const r = []
for (let i = from; i < to; i++) {
r.push(i)
}
return r
}

function _checkPort (port: PortNumber, host: HostAddress): Promise<PortNumber|false> {
return new Promise((resolve) => {
const server = createServer()
Expand Down
6 changes: 3 additions & 3 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('getPort ()', () => {
test('default port is in use', async () => {
portBlocker = await blockPort(3000)
const port = await getPort()
expect(port).toEqual(4000)
expect(port).toEqual(3001)
})
})
})
Expand All @@ -40,7 +40,7 @@ describe('getPort (host)', () => {
test('default port is IN USE', async () => {
portBlocker = await blockPort(3000, 'localhost')
const port = await getPort({ host: 'localhost' })
expect(port).toEqual(4000)
expect(port).toEqual(3001)
})
})
})
Expand All @@ -56,6 +56,6 @@ describe('getPort (host)', () => {
process.env.HOST = 'localhost'
portBlocker = await blockPort(3000, 'localhost')
const port = await getPort()
expect(port).toEqual(4000)
expect(port).toEqual(3001)
})
})

0 comments on commit 44c8acd

Please sign in to comment.