Skip to content

Commit

Permalink
refactor: for randomness use node:crypto (#921)
Browse files Browse the repository at this point in the history
## Description

- For randomness use node:crypto

## Motivation and Context

less dependencies more better

## How Has This Been Tested?

- [x] green ci

## Types of changes

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] Documentation only change
- [x] Refactor (non-breaking change which fixes an issue without
changing functionality)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
  • Loading branch information
sverweij committed Mar 16, 2024
1 parent 1c2920e commit dcc8c10
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/report/anon/random-string.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import random from "lodash/random.js";
import { randomInt } from "node:crypto";

const NUMBER = 0;
const SEPARATOR = 1;
Expand Down Expand Up @@ -30,13 +30,13 @@ function getRandomChar(pChar) {
case SEPARATOR:
return pChar;
case NUMBER:
return random(0, lMaxDecimalChar);
return randomInt(0, lMaxDecimalChar);
case UPPERCASE:
return lLowerCaseChars[
random(0, lLowerCaseChars.length - 1)
randomInt(0, lLowerCaseChars.length - 1)
].toUpperCase();
default:
return lLowerCaseChars[random(0, lLowerCaseChars.length - 1)];
return lLowerCaseChars[randomInt(0, lLowerCaseChars.length - 1)];
}
}

Expand Down
5 changes: 3 additions & 2 deletions test/cli/init-config/build-config.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path/posix";
import { deepEqual, ok, equal } from "node:assert/strict";
import { randomBytes } from "node:crypto";
import Ajv from "ajv";
import deleteDammit from "../delete-dammit.utl.cjs";
import configurationSchema from "#configuration-schema";
Expand All @@ -12,10 +13,10 @@ const ajv = new Ajv();

const createConfigNormalized = async (pInitOptions) => {
const lConfigAsString = buildConfig(normalizeInitOptions(pInitOptions));
const lBaseAbc = 36;
const lFileNameLength = 10;
let lTemporaryFileName = join(
tmpdir(),
`${Math.random().toString(lBaseAbc).split(".").pop()}.cjs`,
`${randomBytes(lFileNameLength).toString("hex")}.cjs`,
);
writeFileSync(lTemporaryFileName, lConfigAsString, "utf8");
const lConfigAsModule = await import(`file:///${lTemporaryFileName}`);
Expand Down

0 comments on commit dcc8c10

Please sign in to comment.