From df062d2c788fd76546d59c67452d8d0fe569c533 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Fri, 5 Jan 2024 18:28:33 +0530 Subject: fix(ext/node): add fs.cp, fs.cpSync, promises.cp (#21745) Fixes https://github.com/denoland/deno/issues/20803 Fixes https://github.com/denoland/deno/issues/21723 Performance: copying a 48GiB rust `target` folder (recursive) | Platform | `deno` | `node v21.5` | Improvement | | -------- | ------- | ------- | ------- | | macOS (APFS) | 3.1secs | 127.99 secs | **42x** | | Windows | 18.3secs | 67.2secs | **3.8x** | Copying files with varying sizes: ![image](https://github.com/denoland/deno/assets/34997667/58932652-6f7a-47f5-8504-896dc9ab4ddc) --- ext/node/polyfills/_fs/_fs_cp.js | 41 ++++++++++++++++++++++++++++++++++++++++ ext/node/polyfills/fs.ts | 6 ++++++ 2 files changed, 47 insertions(+) create mode 100644 ext/node/polyfills/_fs/_fs_cp.js (limited to 'ext/node/polyfills') diff --git a/ext/node/polyfills/_fs/_fs_cp.js b/ext/node/polyfills/_fs/_fs_cp.js new file mode 100644 index 000000000..dbe327974 --- /dev/null +++ b/ext/node/polyfills/_fs/_fs_cp.js @@ -0,0 +1,41 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. + +// deno-lint-ignore-file prefer-primordials + +import { + getValidatedPath, + validateCpOptions, +} from "ext:deno_node/internal/fs/utils.mjs"; +import { promisify } from "ext:deno_node/internal/util.mjs"; + +const core = globalThis.__bootstrap.core; +const ops = core.ops; +const { op_node_cp } = core.ensureFastOps(); + +export function cpSync(src, dest, options) { + validateCpOptions(options); + const srcPath = getValidatedPath(src, "src"); + const destPath = getValidatedPath(dest, "dest"); + + ops.op_node_cp_sync(srcPath, destPath); +} + +export function cp(src, dest, options, callback) { + if (typeof options === "function") { + callback = options; + options = {}; + } + validateCpOptions(options); + const srcPath = getValidatedPath(src, "src"); + const destPath = getValidatedPath(dest, "dest"); + + op_node_cp( + srcPath, + destPath, + ).then( + (res) => callback(null, res), + (err) => callback(err, null), + ); +} + +export const cpPromise = promisify(cp); diff --git a/ext/node/polyfills/fs.ts b/ext/node/polyfills/fs.ts index 881f0c139..01ac9912e 100644 --- a/ext/node/polyfills/fs.ts +++ b/ext/node/polyfills/fs.ts @@ -18,6 +18,7 @@ import { copyFilePromise, copyFileSync, } from "ext:deno_node/_fs/_fs_copy.ts"; +import { cp, cpPromise, cpSync } from "ext:deno_node/_fs/_fs_cp.js"; import Dir from "ext:deno_node/_fs/_fs_dir.ts"; import Dirent from "ext:deno_node/_fs/_fs_dirent.ts"; import { exists, existsSync } from "ext:deno_node/_fs/_fs_exists.ts"; @@ -137,6 +138,7 @@ const { const promises = { access: accessPromise, copyFile: copyFilePromise, + cp: cpPromise, open: openPromise, opendir: opendirPromise, rename: renamePromise, @@ -179,6 +181,8 @@ export default { constants, copyFile, copyFileSync, + cp, + cpSync, createReadStream, createWriteStream, Dir, @@ -280,6 +284,8 @@ export { constants, copyFile, copyFileSync, + cp, + cpSync, createReadStream, createWriteStream, Dir, -- cgit v1.2.3