From 34513c032cb96dd9f80647d5d19db5ba55cfdb0b Mon Sep 17 00:00:00 2001 From: Louis-Philippe Perron <32278+lp@users.noreply.github.com> Date: Mon, 28 Dec 2020 15:58:58 -0500 Subject: [PATCH] feat(std/node): adds fs.mkdtemp & fs.mkdtempSync (#8604) --- std/node/_fs/_fs_mkdtemp.ts | 98 +++++++++++++++++++++++++ std/node/_fs/_fs_mkdtemp_test.ts | 121 +++++++++++++++++++++++++++++++ std/node/fs.ts | 5 ++ 3 files changed, 224 insertions(+) create mode 100644 std/node/_fs/_fs_mkdtemp.ts create mode 100644 std/node/_fs/_fs_mkdtemp_test.ts diff --git a/std/node/_fs/_fs_mkdtemp.ts b/std/node/_fs/_fs_mkdtemp.ts new file mode 100644 index 0000000000..a249f19112 --- /dev/null +++ b/std/node/_fs/_fs_mkdtemp.ts @@ -0,0 +1,98 @@ +// Copyright Node.js contributors. All rights reserved. MIT License. +import { existsSync } from "./_fs_exists.ts"; +import { mkdir, mkdirSync } from "./_fs_mkdir.ts"; +import { + ERR_INVALID_CALLBACK, + ERR_INVALID_OPT_VALUE_ENCODING, +} from "../_errors.ts"; + +export type mkdtempCallback = ( + err: Error | undefined, + directory?: string, +) => void; + +// https://nodejs.org/dist/latest-v15.x/docs/api/fs.html#fs_fs_mkdtemp_prefix_options_callback +export function mkdtemp(prefix: string, callback: mkdtempCallback): void; +export function mkdtemp( + prefix: string, + options: { encoding: string } | string, + callback: mkdtempCallback, +): void; +export function mkdtemp( + prefix: string, + optionsOrCallback: { encoding: string } | string | mkdtempCallback, + maybeCallback?: mkdtempCallback, +): void { + const callback: mkdtempCallback | undefined = + typeof optionsOrCallback == "function" ? optionsOrCallback : maybeCallback; + if (!callback) throw new ERR_INVALID_CALLBACK(callback); + + const encoding: string | undefined = parseEncoding(optionsOrCallback); + const path = tempDirPath(prefix); + + mkdir( + path, + { recursive: false, mode: 0o700 }, + (err: Error | null | undefined) => { + if (err) callback(err); + else callback(undefined, decode(path, encoding)); + }, + ); +} + +// https://nodejs.org/dist/latest-v15.x/docs/api/fs.html#fs_fs_mkdtempsync_prefix_options +export function mkdtempSync( + prefix: string, + options?: { encoding: string } | string, +): string { + const encoding: string | undefined = parseEncoding(options); + const path = tempDirPath(prefix); + + mkdirSync(path, { recursive: false, mode: 0o700 }); + return decode(path, encoding); +} + +function parseEncoding( + optionsOrCallback?: { encoding: string } | string | mkdtempCallback, +): string | undefined { + let encoding: string | undefined; + if (typeof optionsOrCallback == "function") encoding = undefined; + else if (optionsOrCallback instanceof Object) { + encoding = optionsOrCallback?.encoding; + } else encoding = optionsOrCallback; + + if (encoding) { + try { + new TextDecoder(encoding); + } catch (error) { + throw new ERR_INVALID_OPT_VALUE_ENCODING(encoding); + } + } + + return encoding; +} + +function decode(str: string, encoding?: string): string { + if (!encoding) return str; + else { + const decoder = new TextDecoder(encoding); + const encoder = new TextEncoder(); + return decoder.decode(encoder.encode(str)); + } +} + +const CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; +function randomName(): string { + return [...Array(6)].map(() => + CHARS[Math.floor(Math.random() * CHARS.length)] + ).join(""); +} + +function tempDirPath(prefix: string): string { + let path: string; + do { + path = prefix + randomName(); + } while (existsSync(path)); + + return path; +} diff --git a/std/node/_fs/_fs_mkdtemp_test.ts b/std/node/_fs/_fs_mkdtemp_test.ts new file mode 100644 index 0000000000..c1048acbab --- /dev/null +++ b/std/node/_fs/_fs_mkdtemp_test.ts @@ -0,0 +1,121 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { assert } from "../../testing/asserts.ts"; +import { mkdtemp, mkdtempSync } from "./_fs_mkdtemp.ts"; +import { existsSync } from "./_fs_exists.ts"; +import { env } from "../process.ts"; +import { isWindows } from "../../_util/os.ts"; +import { promisify } from "../_util/_util_promisify.ts"; + +const prefix = isWindows ? env.TEMP + "\\" : (env.TMPDIR || "/tmp") + "/"; +const doesNotExists = "/does/not/exists/"; +const options = { encoding: "ascii" }; +const badOptions = { encoding: "bogus" }; + +const mkdtempP = promisify(mkdtemp); + +Deno.test({ + name: "[node/fs] mkdtemp", + fn: async () => { + try { + const directory = await mkdtempP(prefix); + assert(existsSync(directory)); + Deno.removeSync(directory); + } catch (error) { + assert(false); + } + }, +}); + +Deno.test({ + name: "[node/fs] mkdtemp (does not exists)", + fn: async () => { + try { + const directory = await mkdtempP(doesNotExists); + + // should have thrown already... + assert(!existsSync(directory)); + Deno.removeSync(directory); + } catch (error) { + assert(true); + } + }, +}); + +Deno.test({ + name: "[node/fs] mkdtemp (with options)", + fn: async () => { + try { + const directory = await mkdtempP(prefix, options); + assert(existsSync(directory)); + Deno.removeSync(directory); + } catch (error) { + assert(false); + } + }, +}); + +Deno.test({ + name: "[node/fs] mkdtemp (with bad options)", + fn: async () => { + try { + const directory = await mkdtempP(prefix, badOptions); + + // should have thrown already... + assert(!existsSync(directory)); + Deno.removeSync(directory); + } catch (error) { + assert(true); + } + }, +}); + +Deno.test({ + name: "[node/fs] mkdtempSync", + fn: () => { + const directory = mkdtempSync(prefix); + const dirExists = existsSync(directory); + Deno.removeSync(directory); + assert(dirExists); + }, +}); + +Deno.test({ + name: "[node/fs] mkdtempSync (does not exists)", + fn: () => { + try { + const directory = mkdtempSync(doesNotExists); + // should have thrown already... + + const dirExists = existsSync(directory); + Deno.removeSync(directory); + assert(!dirExists); + } catch (error) { + assert(true); + } + }, +}); + +Deno.test({ + name: "[node/fs] mkdtempSync (with options)", + fn: () => { + const directory = mkdtempSync(prefix, options); + const dirExists = existsSync(directory); + Deno.removeSync(directory); + assert(dirExists); + }, +}); + +Deno.test({ + name: "[node/fs] mkdtempSync (with bad options)", + fn: () => { + try { + const directory = mkdtempSync(prefix, badOptions); + // should have thrown already... + + Deno.removeSync(directory); + assert(false); + } catch (error) { + assert(true); + } + }, +}); diff --git a/std/node/fs.ts b/std/node/fs.ts index 38682d3c4c..1eaafa788c 100644 --- a/std/node/fs.ts +++ b/std/node/fs.ts @@ -9,6 +9,7 @@ import { readFile, readFileSync } from "./_fs/_fs_readFile.ts"; import { readlink, readlinkSync } from "./_fs/_fs_readlink.ts"; import { exists, existsSync } from "./_fs/_fs_exists.ts"; import { mkdir, mkdirSync } from "./_fs/_fs_mkdir.ts"; +import { mkdtemp, mkdtempSync } from "./_fs/_fs_mkdtemp.ts"; import { copyFile, copyFileSync } from "./_fs/_fs_copy.ts"; import { writeFile, writeFileSync } from "./_fs/_fs_writeFile.ts"; import { readdir, readdirSync } from "./_fs/_fs_readdir.ts"; @@ -43,6 +44,8 @@ export default { lstatSync, mkdir, mkdirSync, + mkdtemp, + mkdtempSync, open, openSync, promises, @@ -87,6 +90,8 @@ export { lstatSync, mkdir, mkdirSync, + mkdtemp, + mkdtempSync, open, openSync, promises,