summaryrefslogtreecommitdiff
path: root/std/node
diff options
context:
space:
mode:
authorLouis-Philippe Perron <32278+lp@users.noreply.github.com>2020-12-28 15:58:58 -0500
committerGitHub <noreply@github.com>2020-12-28 21:58:58 +0100
commit34513c032cb96dd9f80647d5d19db5ba55cfdb0b (patch)
tree1a82e35daf330371be1352d9336de7d3bb5e1abe /std/node
parent091059450eab50c1975fb0d3ecd4fcdefcb22b76 (diff)
feat(std/node): adds fs.mkdtemp & fs.mkdtempSync (#8604)
Diffstat (limited to 'std/node')
-rw-r--r--std/node/_fs/_fs_mkdtemp.ts98
-rw-r--r--std/node/_fs/_fs_mkdtemp_test.ts121
-rw-r--r--std/node/fs.ts5
3 files changed, 224 insertions, 0 deletions
diff --git a/std/node/_fs/_fs_mkdtemp.ts b/std/node/_fs/_fs_mkdtemp.ts
new file mode 100644
index 000000000..a249f1911
--- /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 000000000..c1048acba
--- /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 38682d3c4..1eaafa788 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,