summaryrefslogtreecommitdiff
path: root/std/node
diff options
context:
space:
mode:
Diffstat (limited to 'std/node')
-rw-r--r--std/node/_fs/_fs_appendFile.ts49
-rw-r--r--std/node/_fs/_fs_chmod.ts17
-rw-r--r--std/node/_fs/_fs_chown.ts17
-rw-r--r--std/node/_fs/_fs_copy.ts17
-rw-r--r--std/node/_fs/_fs_dir.ts37
-rw-r--r--std/node/_fs/_fs_exists.ts24
-rw-r--r--std/node/_fs/_fs_mkdir.ts15
7 files changed, 68 insertions, 108 deletions
diff --git a/std/node/_fs/_fs_appendFile.ts b/std/node/_fs/_fs_appendFile.ts
index f3b44dd7b..48b05ff80 100644
--- a/std/node/_fs/_fs_appendFile.ts
+++ b/std/node/_fs/_fs_appendFile.ts
@@ -21,34 +21,31 @@ export function appendFile(
}
validateEncoding(options);
-
let rid = -1;
- new Promise(async (resolve, reject) => {
- try {
- if (typeof pathOrRid === "number") {
- rid = pathOrRid;
- } else {
- const mode: number | undefined = isFileOptions(options)
- ? options.mode
- : undefined;
- const flag: string | undefined = isFileOptions(options)
- ? options.flag
- : undefined;
-
- if (mode) {
- //TODO rework once https://github.com/denoland/deno/issues/4017 completes
- notImplemented("Deno does not yet support setting mode on create");
- }
- const file = await Deno.open(pathOrRid, getOpenOptions(flag));
- rid = file.rid;
- }
-
- const buffer: Uint8Array = new TextEncoder().encode(data);
+ const buffer: Uint8Array = new TextEncoder().encode(data);
+ new Promise((resolve, reject) => {
+ if (typeof pathOrRid === "number") {
+ rid = pathOrRid;
+ Deno.write(rid, buffer).then(resolve).catch(reject);
+ } else {
+ const mode: number | undefined = isFileOptions(options)
+ ? options.mode
+ : undefined;
+ const flag: string | undefined = isFileOptions(options)
+ ? options.flag
+ : undefined;
- await Deno.write(rid, buffer);
- resolve();
- } catch (err) {
- reject(err);
+ if (mode) {
+ //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))
+ .then(({ rid: openedFileRid }) => {
+ rid = openedFileRid;
+ return Deno.write(openedFileRid, buffer);
+ })
+ .then(resolve)
+ .catch(reject);
}
})
.then(() => {
diff --git a/std/node/_fs/_fs_chmod.ts b/std/node/_fs/_fs_chmod.ts
index 0eb01a8a6..306113047 100644
--- a/std/node/_fs/_fs_chmod.ts
+++ b/std/node/_fs/_fs_chmod.ts
@@ -13,20 +13,9 @@ export function chmod(
mode: string | number,
callback: CallbackWithError
): void {
- new Promise(async (resolve, reject) => {
- try {
- await Deno.chmod(path, getResolvedMode(mode));
- resolve();
- } catch (err) {
- reject(err);
- }
- })
- .then(() => {
- callback();
- })
- .catch((err) => {
- callback(err);
- });
+ Deno.chmod(path, getResolvedMode(mode))
+ .then(() => callback())
+ .catch(callback);
}
/**
diff --git a/std/node/_fs/_fs_chown.ts b/std/node/_fs/_fs_chown.ts
index 94b7401d0..c6baa4722 100644
--- a/std/node/_fs/_fs_chown.ts
+++ b/std/node/_fs/_fs_chown.ts
@@ -12,20 +12,9 @@ export function chown(
gid: number,
callback: CallbackWithError
): void {
- new Promise(async (resolve, reject) => {
- try {
- await Deno.chown(path, uid, gid);
- resolve();
- } catch (err) {
- reject(err);
- }
- })
- .then(() => {
- callback();
- })
- .catch((err) => {
- callback(err);
- });
+ Deno.chown(path, uid, gid)
+ .then(() => callback())
+ .catch(callback);
}
/**
diff --git a/std/node/_fs/_fs_copy.ts b/std/node/_fs/_fs_copy.ts
index 4fdc63008..320c2fb3e 100644
--- a/std/node/_fs/_fs_copy.ts
+++ b/std/node/_fs/_fs_copy.ts
@@ -7,20 +7,9 @@ export function copyFile(
destination: string,
callback: CallbackWithError
): void {
- new Promise(async (resolve, reject) => {
- try {
- await Deno.copyFile(source, destination);
- resolve();
- } catch (err) {
- reject(err);
- }
- })
- .then(() => {
- callback();
- })
- .catch((err) => {
- callback(err);
- });
+ Deno.copyFile(source, destination)
+ .then(() => callback())
+ .catch(callback);
}
export function copyFileSync(source: string, destination: string): void {
diff --git a/std/node/_fs/_fs_dir.ts b/std/node/_fs/_fs_dir.ts
index 144014cce..543cf14ee 100644
--- a/std/node/_fs/_fs_dir.ts
+++ b/std/node/_fs/_fs_dir.ts
@@ -1,4 +1,5 @@
import Dirent from "./_fs_dirent.ts";
+import { assert } from "../../testing/asserts.ts";
export default class Dir {
private dirPath: string | Uint8Array;
@@ -17,25 +18,25 @@ export default class Dir {
}
read(callback?: Function): Promise<Dirent | null> {
- return new Promise(async (resolve, reject) => {
- try {
- if (!this.asyncIterator) {
- this.asyncIterator = Deno.readdir(this.path)[Symbol.asyncIterator]();
- }
-
- const result: Dirent | null = await (await this.asyncIterator?.next())
- .value;
- resolve(result ? result : null);
-
- if (callback) {
- callback(null, result ? result : null);
- }
- } catch (err) {
- if (callback) {
- callback(err, null);
- }
- reject(err);
+ return new Promise((resolve, reject) => {
+ if (!this.asyncIterator) {
+ this.asyncIterator = Deno.readdir(this.path)[Symbol.asyncIterator]();
}
+ assert(this.asyncIterator);
+ this.asyncIterator
+ .next()
+ .then(({ value }) => {
+ resolve(value ? value : null);
+ if (callback) {
+ callback(null, value ? value : null);
+ }
+ })
+ .catch((err) => {
+ if (callback) {
+ callback(err, null);
+ }
+ reject(err);
+ });
});
}
diff --git a/std/node/_fs/_fs_exists.ts b/std/node/_fs/_fs_exists.ts
index cc4f65736..c3bea0d4e 100644
--- a/std/node/_fs/_fs_exists.ts
+++ b/std/node/_fs/_fs_exists.ts
@@ -2,25 +2,23 @@
type ExitsCallback = (exists: boolean) => void;
-/* Deprecated in node api */
-
+/**
+ * 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
+ * Deprecated in node api
+ */
export function exists(path: string, callback: ExitsCallback): void {
- new Promise(async (resolve, reject) => {
- try {
- await Deno.lstat(path);
- resolve();
- } catch (err) {
- reject(err);
- }
- })
+ Deno.lstat(path)
.then(() => {
callback(true);
})
- .catch(() => {
- callback(false);
- });
+ .catch(() => callback(false));
}
+/**
+ * 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 {
try {
Deno.lstatSync(path);
diff --git a/std/node/_fs/_fs_mkdir.ts b/std/node/_fs/_fs_mkdir.ts
index 47c6c935a..5add5778d 100644
--- a/std/node/_fs/_fs_mkdir.ts
+++ b/std/node/_fs/_fs_mkdir.ts
@@ -1,7 +1,11 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { CallbackWithError } from "./_fs_common.ts";
-type Path = string; // TODO path can also be a Buffer or URL.
+/**
+ * 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
+ */
+type Path = string;
type MkdirOptions =
| { recursive?: boolean; mode?: number | undefined }
| number
@@ -29,14 +33,7 @@ export function mkdir(
throw new Deno.errors.InvalidData(
"invalid recursive option , must be a boolean"
);
- new Promise(async (resolve, reject) => {
- try {
- await Deno.mkdir(path, { recursive, mode });
- resolve();
- } catch (err) {
- reject(err);
- }
- })
+ Deno.mkdir(path, { recursive, mode })
.then(() => {
if (callback && typeof callback == "function") {
callback();