Skip to content

Commit

Permalink
lib: reduce the length of the temporary directory path (#1089)
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca authored Feb 21, 2025
1 parent 83ffabd commit a828b25
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/temp-directory.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { join } from 'path';
import { promises as fs, realpathSync } from 'fs';
import { randomUUID } from 'crypto';
import { randomBytes } from 'crypto';
import { tmpdir } from 'os';

import { removeDirectory } from './utils.js';

export async function create(context) {
if (context.options && context.options.tmpDir) {
context.path = join(context.options.tmpDir, randomUUID());
context.path = join(context.options.tmpDir, randomBytes(4).toString('hex'));
} else {
context.path = join(tmpdir(), randomUUID());
context.path = join(tmpdir(), randomBytes(4).toString('hex'));
}

await fs.mkdir(context.path, { recursive: true });
Expand Down
2 changes: 1 addition & 1 deletion test/test-temp-directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ test('tempDirectory.create --tmpDir:', async (t) => {
t.plan(2);
await tempDirectory.create(contextTmpDir);
t.ok(
contextTmpDir.path.match(/thisisatest[/\\].*-.*-.*-.*-.*/),
contextTmpDir.path.match(/thisisatest[/\\][0-9a-f]{8}/),
'the path should match --tmpDir'
);
const stats = await fs.stat(contextTmpDir.path);
Expand Down

0 comments on commit a828b25

Please sign in to comment.