summaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
Diffstat (limited to 'std')
-rw-r--r--std/node/_fs/_fs_access.ts8
-rw-r--r--std/node/_fs/_fs_appendFile.ts13
-rw-r--r--std/node/_fs/_fs_appendFile_test.ts26
-rw-r--r--std/node/_fs/_fs_chmod.ts12
-rw-r--r--std/node/_fs/_fs_chown.ts13
-rw-r--r--std/node/_fs/_fs_copy.ts8
-rw-r--r--std/node/_fs/_fs_exists.ts9
-rw-r--r--std/node/_fs/_fs_mkdir.ts11
-rw-r--r--std/node/_fs/_fs_readFile.ts7
-rw-r--r--std/node/_fs/_fs_readlink.ts9
10 files changed, 86 insertions, 30 deletions
diff --git a/std/node/_fs/_fs_access.ts b/std/node/_fs/_fs_access.ts
index a8cec14db..ee93c4c7f 100644
--- a/std/node/_fs/_fs_access.ts
+++ b/std/node/_fs/_fs_access.ts
@@ -5,19 +5,19 @@ import { notImplemented } from "../_utils.ts";
/** Revist once https://github.com/denoland/deno/issues/4017 lands */
-//TODO - 'path' can also be a Buffer or URL. Neither of these polyfills
+//TODO - 'path' can also be a Buffer. Neither of these polyfills
//is available yet. See https://github.com/denoland/deno/issues/3403
export function access(
- path: string, // eslint-disable-line @typescript-eslint/no-unused-vars
+ path: string | URL, // eslint-disable-line @typescript-eslint/no-unused-vars
modeOrCallback: number | Function, // eslint-disable-line @typescript-eslint/no-unused-vars
callback?: CallbackWithError // eslint-disable-line @typescript-eslint/no-unused-vars
): void {
notImplemented("Not yet available");
}
-//TODO - 'path' can also be a Buffer or URL. Neither of these polyfills
+//TODO - 'path' can also be a Buffer. Neither of these polyfills
//is available yet. See https://github.com/denoland/deno/issues/3403
// eslint-disable-next-line @typescript-eslint/no-unused-vars
-export function accessSync(path: string, mode?: number): void {
+export function accessSync(path: string | URL, mode?: number): void {
notImplemented("Not yet available");
}
diff --git a/std/node/_fs/_fs_appendFile.ts b/std/node/_fs/_fs_appendFile.ts
index 48b05ff80..b116589a0 100644
--- a/std/node/_fs/_fs_appendFile.ts
+++ b/std/node/_fs/_fs_appendFile.ts
@@ -1,17 +1,19 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { FileOptions, isFileOptions, CallbackWithError } from "./_fs_common.ts";
import { notImplemented } from "../_utils.ts";
+import { fromFileUrl } from "../path.ts";
/**
- * TODO: Also accept 'data' parameter as a Node polyfill Buffer or URL type once these
+ * TODO: Also accept 'data' parameter as a Node polyfill Buffer type once these
* are implemented. See https://github.com/denoland/deno/issues/3403
*/
export function appendFile(
- pathOrRid: string | number,
+ pathOrRid: string | number | URL,
data: string,
optionsOrCallback: string | FileOptions | CallbackWithError,
callback?: CallbackWithError
): void {
+ pathOrRid = pathOrRid instanceof URL ? fromFileUrl(pathOrRid) : pathOrRid;
const callbackFn: CallbackWithError | undefined =
optionsOrCallback instanceof Function ? optionsOrCallback : callback;
const options: string | FileOptions | undefined =
@@ -39,7 +41,7 @@ export function appendFile(
//TODO rework once https://github.com/denoland/deno/issues/4017 completes
notImplemented("Deno does not yet support setting mode on create");
}
- Deno.open(pathOrRid, getOpenOptions(flag))
+ Deno.open(pathOrRid as string, getOpenOptions(flag))
.then(({ rid: openedFileRid }) => {
rid = openedFileRid;
return Deno.write(openedFileRid, buffer);
@@ -66,17 +68,18 @@ function closeRidIfNecessary(isPathString: boolean, rid: number): void {
}
/**
- * TODO: Also accept 'data' parameter as a Node polyfill Buffer or URL type once these
+ * TODO: Also accept 'data' parameter as a Node polyfill Buffer type once these
* are implemented. See https://github.com/denoland/deno/issues/3403
*/
export function appendFileSync(
- pathOrRid: string | number,
+ pathOrRid: string | number | URL,
data: string,
options?: string | FileOptions
): void {
let rid = -1;
validateEncoding(options);
+ pathOrRid = pathOrRid instanceof URL ? fromFileUrl(pathOrRid) : pathOrRid;
try {
if (typeof pathOrRid === "number") {
diff --git a/std/node/_fs/_fs_appendFile_test.ts b/std/node/_fs/_fs_appendFile_test.ts
index ca48dc5d3..402ac1c10 100644
--- a/std/node/_fs/_fs_appendFile_test.ts
+++ b/std/node/_fs/_fs_appendFile_test.ts
@@ -2,6 +2,7 @@
const { test } = Deno;
import { assertEquals, assertThrows, fail } from "../../testing/asserts.ts";
import { appendFile, appendFileSync } from "./_fs_appendFile.ts";
+import { fromFileUrl } from "../path.ts";
const decoder = new TextDecoder("utf-8");
@@ -110,6 +111,31 @@ test({
});
test({
+ name: "Async: Data is written to passed in URL",
+ async fn() {
+ const openResourcesBeforeAppend: Deno.ResourceMap = Deno.resources();
+ const fileURL = new URL("_fs_appendFile_test_file.txt", import.meta.url);
+ await new Promise((resolve, reject) => {
+ appendFile(fileURL, "hello world", (err) => {
+ if (err) reject(err);
+ else resolve();
+ });
+ })
+ .then(async () => {
+ assertEquals(Deno.resources(), openResourcesBeforeAppend);
+ const data = await Deno.readFile(fromFileUrl(fileURL));
+ assertEquals(decoder.decode(data), "hello world");
+ })
+ .catch((err) => {
+ fail("No error was expected: " + err);
+ })
+ .finally(async () => {
+ await Deno.remove(fromFileUrl(fileURL));
+ });
+ },
+});
+
+test({
name:
"Async: Callback is made with error if attempting to append data to an existing file with 'ax' flag",
async fn() {
diff --git a/std/node/_fs/_fs_chmod.ts b/std/node/_fs/_fs_chmod.ts
index 2adff59ff..b1079e0a1 100644
--- a/std/node/_fs/_fs_chmod.ts
+++ b/std/node/_fs/_fs_chmod.ts
@@ -1,28 +1,32 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { CallbackWithError } from "./_fs_common.ts";
+import { fromFileUrl } from "../path.ts";
const allowedModes = /^[0-7]{3}/;
/**
- * TODO: Also accept 'path' parameter as a Node polyfill Buffer or URL type once these
+ * TODO: Also accept 'path' parameter as a Node polyfill Buffer type once these
* are implemented. See https://github.com/denoland/deno/issues/3403
*/
export function chmod(
- path: string,
+ path: string | URL,
mode: string | number,
callback: CallbackWithError
): void {
+ path = path instanceof URL ? fromFileUrl(path) : path;
+
Deno.chmod(path, getResolvedMode(mode))
.then(() => callback())
.catch(callback);
}
/**
- * TODO: Also accept 'path' parameter as a Node polyfill Buffer or URL type once these
+ * TODO: Also accept 'path' parameter as a Node polyfill Buffer type once these
* are implemented. See https://github.com/denoland/deno/issues/3403
*/
-export function chmodSync(path: string, mode: string | number): void {
+export function chmodSync(path: string | URL, mode: string | number): void {
+ path = path instanceof URL ? fromFileUrl(path) : path;
Deno.chmodSync(path, getResolvedMode(mode));
}
diff --git a/std/node/_fs/_fs_chown.ts b/std/node/_fs/_fs_chown.ts
index c6baa4722..cd1973b1f 100644
--- a/std/node/_fs/_fs_chown.ts
+++ b/std/node/_fs/_fs_chown.ts
@@ -1,26 +1,31 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { CallbackWithError } from "./_fs_common.ts";
+import { fromFileUrl } from "../path.ts";
/**
- * TODO: Also accept 'path' parameter as a Node polyfill Buffer or URL type once these
+ * TODO: Also accept 'path' parameter as a Node polyfill Buffer type once these
* are implemented. See https://github.com/denoland/deno/issues/3403
*/
export function chown(
- path: string,
+ path: string | URL,
uid: number,
gid: number,
callback: CallbackWithError
): void {
+ path = path instanceof URL ? fromFileUrl(path) : path;
+
Deno.chown(path, uid, gid)
.then(() => callback())
.catch(callback);
}
/**
- * TODO: Also accept 'path' parameter as a Node polyfill Buffer or URL type once these
+ * TODO: Also accept 'path' parameter as a Node polyfill Buffer type once these
* are implemented. See https://github.com/denoland/deno/issues/3403
*/
-export function chownSync(path: string, uid: number, gid: number): void {
+export function chownSync(path: string | URL, uid: number, gid: number): void {
+ path = path instanceof URL ? fromFileUrl(path) : path;
+
Deno.chownSync(path, uid, gid);
}
diff --git a/std/node/_fs/_fs_copy.ts b/std/node/_fs/_fs_copy.ts
index 0193e77c4..26d9a8c9a 100644
--- a/std/node/_fs/_fs_copy.ts
+++ b/std/node/_fs/_fs_copy.ts
@@ -1,17 +1,21 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { CallbackWithError } from "./_fs_common.ts";
+import { fromFileUrl } from "../path.ts";
export function copyFile(
- source: string,
+ source: string | URL,
destination: string,
callback: CallbackWithError
): void {
+ source = source instanceof URL ? fromFileUrl(source) : source;
+
Deno.copyFile(source, destination)
.then(() => callback())
.catch(callback);
}
-export function copyFileSync(source: string, destination: string): void {
+export function copyFileSync(source: string | URL, destination: string): void {
+ source = source instanceof URL ? fromFileUrl(source) : source;
Deno.copyFileSync(source, destination);
}
diff --git a/std/node/_fs/_fs_exists.ts b/std/node/_fs/_fs_exists.ts
index c3bea0d4e..e6e6e4f2e 100644
--- a/std/node/_fs/_fs_exists.ts
+++ b/std/node/_fs/_fs_exists.ts
@@ -1,13 +1,15 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
+import { fromFileUrl } from "../path.ts";
type ExitsCallback = (exists: boolean) => void;
/**
- * TODO: Also accept 'path' parameter as a Node polyfill Buffer or URL type once these
+ * TODO: Also accept 'path' parameter as a Node polyfill Buffer type once these
* are implemented. See https://github.com/denoland/deno/issues/3403
* Deprecated in node api
*/
-export function exists(path: string, callback: ExitsCallback): void {
+export function exists(path: string | URL, callback: ExitsCallback): void {
+ path = path instanceof URL ? fromFileUrl(path) : path;
Deno.lstat(path)
.then(() => {
callback(true);
@@ -19,7 +21,8 @@ export function exists(path: string, callback: ExitsCallback): void {
* TODO: Also accept 'path' parameter as a Node polyfill Buffer or URL type once these
* are implemented. See https://github.com/denoland/deno/issues/3403
*/
-export function existsSync(path: string): boolean {
+export function existsSync(path: string | URL): boolean {
+ path = path instanceof URL ? fromFileUrl(path) : path;
try {
Deno.lstatSync(path);
return true;
diff --git a/std/node/_fs/_fs_mkdir.ts b/std/node/_fs/_fs_mkdir.ts
index fd2156c37..a65db8dba 100644
--- a/std/node/_fs/_fs_mkdir.ts
+++ b/std/node/_fs/_fs_mkdir.ts
@@ -1,21 +1,23 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { CallbackWithError } from "./_fs_common.ts";
+import { fromFileUrl } from "../path.ts";
/**
- * TODO: Also accept 'path' parameter as a Node polyfill Buffer or URL type once these
+ * TODO: Also accept 'path' parameter as a Node polyfill Buffer type once these
* are implemented. See https://github.com/denoland/deno/issues/3403
*/
-type Path = string;
type MkdirOptions =
| { recursive?: boolean; mode?: number | undefined }
| number
| boolean;
export function mkdir(
- path: Path,
+ path: string | URL,
options?: MkdirOptions | CallbackWithError,
callback?: CallbackWithError
): void {
+ path = path instanceof URL ? fromFileUrl(path) : path;
+
let mode = 0o777;
let recursive = false;
@@ -46,7 +48,8 @@ export function mkdir(
});
}
-export function mkdirSync(path: Path, options?: MkdirOptions): void {
+export function mkdirSync(path: string | URL, options?: MkdirOptions): void {
+ path = path instanceof URL ? fromFileUrl(path) : path;
let mode = 0o777;
let recursive = false;
diff --git a/std/node/_fs/_fs_readFile.ts b/std/node/_fs/_fs_readFile.ts
index 13e82bfe1..8d3c96db0 100644
--- a/std/node/_fs/_fs_readFile.ts
+++ b/std/node/_fs/_fs_readFile.ts
@@ -5,6 +5,7 @@ import {
intoCallbackAPIWithIntercept,
MaybeEmpty,
} from "../_utils.ts";
+import { fromFileUrl } from "../path.ts";
const { readFile: denoReadFile, readFileSync: denoReadFileSync } = Deno;
@@ -51,10 +52,11 @@ function maybeDecode(
}
export function readFile(
- path: string,
+ path: string | URL,
optOrCallback: ReadFileCallback | ReadFileOptions,
callback?: ReadFileCallback
): void {
+ path = path instanceof URL ? fromFileUrl(path) : path;
let cb: ReadFileCallback | undefined;
if (typeof optOrCallback === "function") {
cb = optOrCallback;
@@ -73,8 +75,9 @@ export function readFile(
}
export function readFileSync(
- path: string,
+ path: string | URL,
opt?: ReadFileOptions
): string | Uint8Array {
+ path = path instanceof URL ? fromFileUrl(path) : path;
return maybeDecode(denoReadFileSync(path), getEncoding(opt));
}
diff --git a/std/node/_fs/_fs_readlink.ts b/std/node/_fs/_fs_readlink.ts
index b32cd9dd3..d461cf390 100644
--- a/std/node/_fs/_fs_readlink.ts
+++ b/std/node/_fs/_fs_readlink.ts
@@ -4,6 +4,7 @@ import {
MaybeEmpty,
notImplemented,
} from "../_utils.ts";
+import { fromFileUrl } from "../path.ts";
const { readLink: denoReadlink, readLinkSync: denoReadlinkSync } = Deno;
@@ -49,10 +50,12 @@ function getEncoding(
}
export function readlink(
- path: string,
+ path: string | URL,
optOrCallback: ReadlinkCallback | ReadlinkOptions,
callback?: ReadlinkCallback
): void {
+ path = path instanceof URL ? fromFileUrl(path) : path;
+
let cb: ReadlinkCallback | undefined;
if (typeof optOrCallback === "function") {
cb = optOrCallback;
@@ -71,8 +74,10 @@ export function readlink(
}
export function readlinkSync(
- path: string,
+ path: string | URL,
opt?: ReadlinkOptions
): string | Uint8Array {
+ path = path instanceof URL ? fromFileUrl(path) : path;
+
return maybeEncode(denoReadlinkSync(path), getEncoding(opt));
}