Skip to content

Commit

Permalink
not using async
Browse files Browse the repository at this point in the history
  • Loading branch information
Yanis Benson committed Mar 25, 2019
1 parent e711e66 commit a9154ed
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
39 changes: 25 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,35 @@ const getAvailablePort = options => new Promise((resolve, reject) => {
});
});

module.exports = async options => {
if (options && options.port) {
const ports = (typeof options.port === 'number') ? [options.port] : options.port;

for (const port of ports) {
const gotPort = await getAvailablePort(Object.assign({}, options, {port})); // eslint-disable-line no-await-in-loop
if (gotPort !== null) {
return gotPort;
module.exports = options => {
const ports = (options && options.port) ?
(typeof options.port === 'number') ? [options.port] : options.port :
[];

const iterator = ports[Symbol.iterator]();
let zeroNotTried = true;

const tryNext = () => {
let {value: port, done} = iterator.next();
if (done) {
if (zeroNotTried) {
zeroNotTried = false;
port = 0;
} else {
return Promise.reject(new Error('no available ports found'));
}
}
}

const gotPort = await getAvailablePort(Object.assign({}, options, {port: 0}));
if (gotPort === null) {
throw new Error('no available ports found');
}
return getAvailablePort(Object.assign({}, options, {port})).then(gotPort => {
if (gotPort === null) {
return tryNext();
}

return gotPort;
});
};

return gotPort;
return tryNext();
};

class PortRangeIterator {
Expand Down
22 changes: 11 additions & 11 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import test from 'ava';
import pify from 'pify';
import getPort from '.';

test('port can be bound when promise resolves', async t => {
test.serial('port can be bound when promise resolves', async t => {
const port = await getPort();
t.is(typeof port, 'number');
t.true(port > 0);
Expand All @@ -14,14 +14,14 @@ test('port can be bound when promise resolves', async t => {
t.is(server.address().port, port);
});

test('preferred port', async t => {
test.serial('preferred port', async t => {
const desiredPort = 8080;
const port = await getPort({port: desiredPort});

t.is(port, desiredPort);
});

test('preferred port unavailable', async t => {
test.serial('preferred port unavailable', async t => {
const desiredPort = 8282;
const server = net.createServer();
await pify(server.listen.bind(server))(desiredPort);
Expand All @@ -32,7 +32,7 @@ test('preferred port unavailable', async t => {
t.not(port, desiredPort);
});

test('port can be bound to IPv4 host when promise resolves', async t => {
test.serial('port can be bound to IPv4 host when promise resolves', async t => {
const port = await getPort({host: '0.0.0.0'});
t.is(typeof port, 'number');
t.true(port > 0);
Expand All @@ -43,7 +43,7 @@ test('port can be bound to IPv4 host when promise resolves', async t => {
t.is(server.address().port, port);
});

test('preferred port given IPv4 host', async t => {
test.serial('preferred port given IPv4 host', async t => {
const desiredPort = 8080;
const port = await getPort({
port: desiredPort,
Expand All @@ -53,7 +53,7 @@ test('preferred port given IPv4 host', async t => {
t.is(port, desiredPort);
});

test('preferred ports', async t => {
test.serial('preferred ports', async t => {
const desiredPorts = [9910, 9912, 9913];
const port = await getPort({
port: desiredPorts,
Expand All @@ -63,7 +63,7 @@ test('preferred ports', async t => {
t.is(port, desiredPorts[0]);
});

test('first port in preferred ports array is unavailable', async t => {
test.serial('first port in preferred ports array is unavailable', async t => {
const desiredPorts = [9090, 9091];

const server = net.createServer();
Expand All @@ -76,7 +76,7 @@ test('first port in preferred ports array is unavailable', async t => {
t.is(port, desiredPorts[1]);
});

test('all preferred ports in array are unavailable', async t => {
test.serial('all preferred ports in array are unavailable', async t => {
const desiredPorts = [9990, 9991];

const server1 = net.createServer();
Expand All @@ -94,7 +94,7 @@ test('all preferred ports in array are unavailable', async t => {
t.not(port, desiredPorts[1]);
});

test('non-array iterables work', async t => {
test.serial('non-array iterables work', async t => {
const desiredPorts = (function * () {
yield 9920;
})();
Expand All @@ -106,14 +106,14 @@ test('non-array iterables work', async t => {
t.is(port, 9920);
});

test('makeRange throws on invalid ranges', t => {
test.serial('makeRange throws on invalid ranges', t => {
t.throws(() => getPort.makeRange(0, 0));
t.throws(() => getPort.makeRange(1023, 1023));
t.throws(() => getPort.makeRange(65536, 65536));
t.throws(() => getPort.makeRange(1025, 1024));
});

test('makeRange produces valid ranges', t => {
test.serial('makeRange produces valid ranges', t => {
t.deepEqual([...getPort.makeRange(1024, 1025)], [1024]);
t.deepEqual([...getPort.makeRange(1024, 1027)], [1024, 1025, 1026]);
});

0 comments on commit a9154ed

Please sign in to comment.