Skip to content

Commit

Permalink
refactor(build-env): adjust uniquePort logic to be truly unique
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton committed Sep 13, 2024
1 parent 989d38a commit 1946fbf
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 13 deletions.
3 changes: 2 additions & 1 deletion tooling/build-env/src/internal/utils/npm.unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ describe('logError', () => {
});
});

// @TODO understand why chdir is not mocked
describe.skip('setupNpmWorkspace', () => {
let cwdSpy;
let chdirSpy;
Expand All @@ -42,11 +41,13 @@ describe.skip('setupNpmWorkspace', () => {
beforeEach(() => {
cwdSpy = vi.spyOn(process, 'cwd').mockReturnValue(MEMFS_VOLUME);
chdirSpy = vi.spyOn(process, 'chdir').mockImplementation(vi.fn());
logInfoSpy = vi.mocked(logInfo).mockImplementation(vi.fn());
});

afterEach(() => {
cwdSpy.mockRestore();
chdirSpy.mockRestore();
logInfoSpy.mockRestore();
});

it('should create npm workspace in given folder', () => {
Expand Down
6 changes: 3 additions & 3 deletions tooling/build-env/src/internal/utils/target.unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ describe('getTargetOutputPath', () => {
});

it('should throw if no outputPath is given in options', () => {
expect(
expect(() =>
getTargetOutputPath({
options: {},
})
).toThrow('outputPath is required');
});

it('should throw if empty onject is passed', () => {
expect(getTargetOutputPath({})).toThrow('outputPath is required');
it('should throw if empty object is passed', () => {
expect(() => getTargetOutputPath({})).toThrow('outputPath is required');
});
});
18 changes: 16 additions & 2 deletions tooling/build-env/src/internal/utils/unique-port.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
export function uniquePort(): number {
return Number((6000 + Number(Math.random() * 1000)).toFixed(0));
import { getRandomValues } from 'node:crypto';

const uniqueNumbers = new Set();
export function uniquePort() {
const a = 0;
const b = 1000;
const rnd =
6000 +
((a + ((b - a + 1) * getRandomValues(new Uint32Array(1))[0]) / 2 ** 32) |
0);
if (uniqueNumbers.has(rnd)) {
return uniquePort();
} else {
uniqueNumbers.add(rnd);
return rnd;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { uniquePort } from './unique-port';

describe('uniquePort', () => {
it('should return a different number on every call', () => {
const portsArray = new Array(100).fill(0).map(() => uniquePort());
const portsArray = new Array(10).fill(0).map(() => uniquePort());
expect(portsArray.length).toBe(new Set(portsArray).size);
});

Expand Down
10 changes: 4 additions & 6 deletions tooling/build-env/src/internal/verdaccio/verdaccio-npm-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,11 @@ export async function bootstrapEnvironment({
return activeRegistry;
}

export type ConfigureRegistryOptions = VerdaccioProcessResult & {
userconfig?: string;
};
export function configureRegistry(
{
port,
host,
url,
userconfig,
}: VerdaccioProcessResult & { userconfig?: string },
{ port, host, url, userconfig }: ConfigureRegistryOptions,
verbose?: boolean
) {
const setRegistry = `npm config set registry="${url}" ${objectToCliArgs({
Expand Down

0 comments on commit 1946fbf

Please sign in to comment.