Skip to content

Commit

Permalink
Use crypto.randomInt()
Browse files Browse the repository at this point in the history
  • Loading branch information
novemberborn committed Jul 10, 2022
1 parent d6c36f9 commit f6336f8
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions source/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import crypto from 'node:crypto';
import net from 'node:net';
import os from 'node:os';
import util from 'node:util';
import {SharedContext} from '@ava/cooperate';

const context = new SharedContext(import.meta.url);
Expand All @@ -11,14 +12,15 @@ const localHosts = new Set([
...Object.values(os.networkInterfaces()).flatMap(interfaces => interfaces?.map(info => info.address)),
]);

const minPort = 1024;
const maxPort = 65_535;
const size = 16;
const randomInt = util.promisify(crypto.randomInt) as (max: number) => Promise<number>;

// Reserve a range of 16 addresses at a random offset.
const reserveRange = async (): Promise<number[]> => {
let from: number;
do {
from = crypto.randomBytes(2).readUInt16BE(0);
} while (from < 1024 || from > 65_520);

const range = Array.from({length: 16}, (_, index) => from + index);
const from = await randomInt(maxPort - minPort - size + 1);
const range = Array.from({length: size}, (_, index) => minPort + from + index);
return context.reserve(...range);
};

Expand Down

0 comments on commit f6336f8

Please sign in to comment.