summaryrefslogtreecommitdiff
path: root/std/node/_fs
diff options
context:
space:
mode:
Diffstat (limited to 'std/node/_fs')
-rw-r--r--std/node/_fs/_fs_writeFile.ts9
-rw-r--r--std/node/_fs/_fs_writeFile_test.ts40
-rw-r--r--std/node/_fs/promises/_fs_writeFile.ts2
3 files changed, 48 insertions, 3 deletions
diff --git a/std/node/_fs/_fs_writeFile.ts b/std/node/_fs/_fs_writeFile.ts
index b46620d9b..72e75c97a 100644
--- a/std/node/_fs/_fs_writeFile.ts
+++ b/std/node/_fs/_fs_writeFile.ts
@@ -1,5 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { notImplemented } from "../_utils.ts";
+import { fromFileUrl } from "../path.ts";
import {
WriteFileOptions,
@@ -10,7 +11,7 @@ import {
} from "./_fs_common.ts";
export function writeFile(
- pathOrRid: string | number,
+ pathOrRid: string | number | URL,
data: string | Uint8Array,
optOrCallback: string | CallbackWithError | WriteFileOptions | undefined,
callback?: CallbackWithError
@@ -24,6 +25,8 @@ export function writeFile(
throw new TypeError("Callback must be a function.");
}
+ pathOrRid = pathOrRid instanceof URL ? fromFileUrl(pathOrRid) : pathOrRid;
+
const flag: string | undefined = isFileOptions(options)
? options.flag
: undefined;
@@ -65,10 +68,12 @@ export function writeFile(
}
export function writeFileSync(
- pathOrRid: string | number,
+ pathOrRid: string | number | URL,
data: string | Uint8Array,
options?: string | WriteFileOptions
): void {
+ pathOrRid = pathOrRid instanceof URL ? fromFileUrl(pathOrRid) : pathOrRid;
+
const flag: string | undefined = isFileOptions(options)
? options.flag
: undefined;
diff --git a/std/node/_fs/_fs_writeFile_test.ts b/std/node/_fs/_fs_writeFile_test.ts
index 2a5db33ef..486c55fa1 100644
--- a/std/node/_fs/_fs_writeFile_test.ts
+++ b/std/node/_fs/_fs_writeFile_test.ts
@@ -8,7 +8,9 @@ import {
assertThrows,
} from "../../testing/asserts.ts";
import { writeFile, writeFileSync } from "./_fs_writeFile.ts";
+import * as path from "../../path/mod.ts";
+const testDataDir = path.resolve(path.join("node", "_fs", "testdata"));
const decoder = new TextDecoder("utf-8");
test("Callback must be a function error", function fn() {
@@ -139,6 +141,27 @@ test("Data is written to correct file", async function testCorrectWriteUsingPath
assertEquals(decoder.decode(data), "hello world");
});
+test("Path can be an URL", async function testCorrectWriteUsingURL() {
+ const url = new URL(
+ Deno.build.os === "windows"
+ ? "file:///" +
+ path
+ .join(testDataDir, "_fs_writeFile_test_file_url.txt")
+ .replace(/\\/g, "/")
+ : "file://" + path.join(testDataDir, "_fs_writeFile_test_file_url.txt")
+ );
+ const filePath = path.fromFileUrl(url);
+ const res = await new Promise((resolve) => {
+ writeFile(url, "hello world", resolve);
+ });
+ assert(res === null);
+
+ const data = await Deno.readFile(filePath);
+ await Deno.remove(filePath);
+ assertEquals(res, null);
+ assertEquals(decoder.decode(data), "hello world");
+});
+
test("Mode is correctly set", async function testCorrectFileMode() {
if (Deno.build.os === "windows") return;
const filename = "_fs_writeFile_test_file.txt";
@@ -204,6 +227,23 @@ test("Data is written synchronously to correct file", function testCorrectWriteS
assertEquals(decoder.decode(data), "hello world");
});
+test("sync: Path can be an URL", function testCorrectWriteSyncUsingURL() {
+ const filePath = path.join(
+ testDataDir,
+ "_fs_writeFileSync_test_file_url.txt"
+ );
+ const url = new URL(
+ Deno.build.os === "windows"
+ ? "file:///" + filePath.replace(/\\/g, "/")
+ : "file://" + filePath
+ );
+ writeFileSync(url, "hello world");
+
+ const data = Deno.readFileSync(filePath);
+ Deno.removeSync(filePath);
+ assertEquals(decoder.decode(data), "hello world");
+});
+
test("Mode is correctly set when writing synchronously", function testCorrectFileModeSync() {
if (Deno.build.os === "windows") return;
const filename = "_fs_writeFileSync_test_file.txt";
diff --git a/std/node/_fs/promises/_fs_writeFile.ts b/std/node/_fs/promises/_fs_writeFile.ts
index a8f9586a2..e89276c97 100644
--- a/std/node/_fs/promises/_fs_writeFile.ts
+++ b/std/node/_fs/promises/_fs_writeFile.ts
@@ -4,7 +4,7 @@ import { WriteFileOptions } from "../_fs_common.ts";
import { writeFile as writeFileCallback } from "../_fs_writeFile.ts";
export function writeFile(
- pathOrRid: string | number,
+ pathOrRid: string | number | URL,
data: string | Uint8Array,
options?: string | WriteFileOptions
): Promise<void> {