From eaf8e4614989d84f55771300078b82458deab0f0 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Sun, 1 Aug 2021 09:07:17 -0700 Subject: [PATCH 1/6] feat: fs_promises base implementation --- node/fs_promises.ts | 60 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 node/fs_promises.ts diff --git a/node/fs_promises.ts b/node/fs_promises.ts new file mode 100644 index 000000000000..bd5c68ef36d5 --- /dev/null +++ b/node/fs_promises.ts @@ -0,0 +1,60 @@ +import { promisify } from './util.ts'; +import * as fs from './fs.ts'; + +export const access = promisify(fs.access); +export const copyFile = promisify(fs.copyFile); +export const open = promisify(fs.open); +// export const opendir = promisify(fs.opendir); +export const rename = promisify(fs.rename); +export const truncate = promisify(fs.truncate); +// export const rm = promisify(fs.rm); +export const rmdir = promisify(fs.rmdir); +export const mkdir = promisify(fs.mkdir); +export const readdir = promisify(fs.readdir); +export const readlink = promisify(fs.readlink); +export const symlink = promisify(fs.symlink); +export const lstat = promisify(fs.lstat); +export const stat = promisify(fs.stat); +export const link = promisify(fs.link); +export const unlink = promisify(fs.unlink); +export const chmod = promisify(fs.chmod); +// export const lchmod = promisify(fs.lchmod); +// export const lchown = promisify(fs.lchown); +export const chown = promisify(fs.chown); +export const utimes = promisify(fs.utimes); +// export const lutimes = promisify(fs.lutimes); +export const realpath = promisify(fs.realpath); +export const mkdtemp = promisify(fs.mkdtemp); +export const writeFile = promisify(fs.writeFile); +export const appendFile = promisify(fs.appendFile); +export const readFile = promisify(fs.readFile); +export const watch = promisify(fs.watch); + +export default { + open, + // opendir, + rename, + truncate, + // rm, + rmdir, + mkdir, + readdir, + readlink, + symlink, + lstat, + stat, + link, + unlink, + chmod, + // lchmod, + // lchown, + chown, + utimes, + // lutimes, + realpath, + mkdtemp, + writeFile, + appendFile, + readFile, + watch +}; From dfc13c3d2da6e7dee6dbc31c96a892566e53b3ab Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Sun, 1 Aug 2021 09:09:47 -0700 Subject: [PATCH 2/6] deno fmt --- node/fs_promises.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/node/fs_promises.ts b/node/fs_promises.ts index bd5c68ef36d5..196146a7c5f7 100644 --- a/node/fs_promises.ts +++ b/node/fs_promises.ts @@ -1,5 +1,5 @@ -import { promisify } from './util.ts'; -import * as fs from './fs.ts'; +import { promisify } from "./util.ts"; +import * as fs from "./fs.ts"; export const access = promisify(fs.access); export const copyFile = promisify(fs.copyFile); @@ -56,5 +56,5 @@ export default { writeFile, appendFile, readFile, - watch + watch, }; From a29591901dac29049a57aa21d60f3da5a0608b24 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Sun, 1 Aug 2021 10:46:20 -0700 Subject: [PATCH 3/6] use fs/promises subfolder --- node/{fs_promises.ts => fs/promises.ts} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename node/{fs_promises.ts => fs/promises.ts} (95%) diff --git a/node/fs_promises.ts b/node/fs/promises.ts similarity index 95% rename from node/fs_promises.ts rename to node/fs/promises.ts index 196146a7c5f7..b4645ec86968 100644 --- a/node/fs_promises.ts +++ b/node/fs/promises.ts @@ -1,5 +1,5 @@ -import { promisify } from "./util.ts"; -import * as fs from "./fs.ts"; +import { promisify } from "../util.ts"; +import * as fs from "../fs.ts"; export const access = promisify(fs.access); export const copyFile = promisify(fs.copyFile); From b3885311dbdba0bdfd3bed918ada07aef433e868 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Sun, 1 Aug 2021 11:18:15 -0700 Subject: [PATCH 4/6] replace previous promises implementation --- node/_fs/promises/_fs_readFile.ts | 30 ------------------- node/_fs/promises/_fs_writeFile.ts | 18 ----------- node/_fs/promises/mod.ts | 2 -- .../{_fs/promises => fs}/_fs_readFile_test.ts | 8 ++--- .../promises => fs}/_fs_writeFile_test.ts | 6 ++-- 5 files changed, 7 insertions(+), 57 deletions(-) delete mode 100644 node/_fs/promises/_fs_readFile.ts delete mode 100644 node/_fs/promises/_fs_writeFile.ts delete mode 100644 node/_fs/promises/mod.ts rename node/{_fs/promises => fs}/_fs_readFile_test.ts (88%) rename node/{_fs/promises => fs}/_fs_writeFile_test.ts (96%) diff --git a/node/_fs/promises/_fs_readFile.ts b/node/_fs/promises/_fs_readFile.ts deleted file mode 100644 index 3067b301f814..000000000000 --- a/node/_fs/promises/_fs_readFile.ts +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -import type { - BinaryOptionsArgument, - FileOptionsArgument, - TextOptionsArgument, -} from "../_fs_common.ts"; -import { readFile as readFileCallback } from "../_fs_readFile.ts"; - -export function readFile( - path: string | URL, - options: TextOptionsArgument, -): Promise; -export function readFile( - path: string | URL, - options?: BinaryOptionsArgument, -): Promise; -export function readFile( - path: string | URL, - options?: FileOptionsArgument, -): Promise { - return new Promise((resolve, reject) => { - readFileCallback(path, options, (err, data): void => { - if (err) return reject(err); - if (data == null) { - return reject(new Error("Invalid state: data missing, but no error")); - } - resolve(data); - }); - }); -} diff --git a/node/_fs/promises/_fs_writeFile.ts b/node/_fs/promises/_fs_writeFile.ts deleted file mode 100644 index 554b65d24df2..000000000000 --- a/node/_fs/promises/_fs_writeFile.ts +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -import type { WriteFileOptions } from "../_fs_common.ts"; -import type { Encodings } from "../../_utils.ts"; - -import { writeFile as writeFileCallback } from "../_fs_writeFile.ts"; - -export function writeFile( - pathOrRid: string | number | URL, - data: string | Uint8Array, - options?: Encodings | WriteFileOptions, -): Promise { - return new Promise((resolve, reject) => { - writeFileCallback(pathOrRid, data, options, (err?: Error | null) => { - if (err) return reject(err); - resolve(); - }); - }); -} diff --git a/node/_fs/promises/mod.ts b/node/_fs/promises/mod.ts deleted file mode 100644 index 4cc6462b9c35..000000000000 --- a/node/_fs/promises/mod.ts +++ /dev/null @@ -1,2 +0,0 @@ -export { writeFile } from "./_fs_writeFile.ts"; -export { readFile } from "./_fs_readFile.ts"; diff --git a/node/_fs/promises/_fs_readFile_test.ts b/node/fs/_fs_readFile_test.ts similarity index 88% rename from node/_fs/promises/_fs_readFile_test.ts rename to node/fs/_fs_readFile_test.ts index 2810d1773e52..222edb722b68 100644 --- a/node/_fs/promises/_fs_readFile_test.ts +++ b/node/fs/_fs_readFile_test.ts @@ -1,10 +1,10 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -import { readFile } from "./_fs_readFile.ts"; -import * as path from "../../../path/mod.ts"; -import { assert, assertEquals } from "../../../testing/asserts.ts"; +import { readFile } from "./promises.ts"; +import * as path from "../../path/mod.ts"; +import { assert, assertEquals } from "../../testing/asserts.ts"; const moduleDir = path.dirname(path.fromFileUrl(import.meta.url)); -const testData = path.resolve(moduleDir, "..", "testdata", "hello.txt"); +const testData = path.resolve(moduleDir, "..", "_fs", "testdata", "hello.txt"); Deno.test("readFileSuccess", async function () { const data: Uint8Array = await readFile(testData); diff --git a/node/_fs/promises/_fs_writeFile_test.ts b/node/fs/_fs_writeFile_test.ts similarity index 96% rename from node/_fs/promises/_fs_writeFile_test.ts rename to node/fs/_fs_writeFile_test.ts index 644a416ca054..26993c95d590 100644 --- a/node/_fs/promises/_fs_writeFile_test.ts +++ b/node/fs/_fs_writeFile_test.ts @@ -4,9 +4,9 @@ import { assertEquals, assertNotEquals, assertThrowsAsync, -} from "../../../testing/asserts.ts"; -import { writeFile } from "./_fs_writeFile.ts"; -import type { TextEncodings } from "../../_utils.ts"; +} from "../../testing/asserts.ts"; +import { writeFile } from "./promises.ts"; +import type { TextEncodings } from "../_utils.ts"; const decoder = new TextDecoder("utf-8"); From d81f23dae7bac2e544e7aeb3c30e1735cc68b130 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Sun, 1 Aug 2021 11:19:38 -0700 Subject: [PATCH 5/6] fixup main ref --- node/fs.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node/fs.ts b/node/fs.ts index 85eb59e2a787..81809b067311 100644 --- a/node/fs.ts +++ b/node/fs.ts @@ -33,7 +33,7 @@ import { utimes, utimesSync } from "./_fs/_fs_utimes.ts"; import { watch } from "./_fs/_fs_watch.ts"; import { writeFile, writeFileSync } from "./_fs/_fs_writeFile.ts"; -import * as promises from "./_fs/promises/mod.ts"; +import * as promises from "./fs/promises.ts"; export default { access, From 705fd4ce45507870a8147c23cde96388ee269191 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Sun, 1 Aug 2021 11:22:06 -0700 Subject: [PATCH 6/6] remove uncheckable types --- node/fs/_fs_writeFile_test.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/node/fs/_fs_writeFile_test.ts b/node/fs/_fs_writeFile_test.ts index 26993c95d590..81a3d369f1c2 100644 --- a/node/fs/_fs_writeFile_test.ts +++ b/node/fs/_fs_writeFile_test.ts @@ -13,7 +13,6 @@ const decoder = new TextDecoder("utf-8"); Deno.test("Invalid encoding results in error()", function testEncodingErrors() { assertThrowsAsync( async () => { - // @ts-expect-error Type '"made-up-encoding"' is not assignable to type await writeFile("some/path", "some data", "made-up-encoding"); }, Error, @@ -22,7 +21,6 @@ Deno.test("Invalid encoding results in error()", function testEncodingErrors() { assertThrowsAsync( async () => { await writeFile("some/path", "some data", { - // @ts-expect-error Type '"made-up-encoding"' is not assignable to type encoding: "made-up-encoding", }); },