diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2023-12-04 16:05:40 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-04 21:05:40 +0000 |
commit | a24d3e8763bc48b69936db9231efb76766914303 (patch) | |
tree | 7091c5354d1e341c34e5847ea0271213c8730c69 /ext/node | |
parent | 9eb25e3cff22d1f704ede0d4066abc846a26a919 (diff) |
perf(node/fs): faster `existsSync` when not exists (#21458)
Diffstat (limited to 'ext/node')
-rw-r--r-- | ext/node/lib.rs | 17 | ||||
-rw-r--r-- | ext/node/ops/fs.rs | 26 | ||||
-rw-r--r-- | ext/node/ops/mod.rs | 1 | ||||
-rw-r--r-- | ext/node/polyfills/_fs/_fs_exists.ts | 9 |
4 files changed, 44 insertions, 9 deletions
diff --git a/ext/node/lib.rs b/ext/node/lib.rs index 7fa5b893b..547f1d60a 100644 --- a/ext/node/lib.rs +++ b/ext/node/lib.rs @@ -50,7 +50,15 @@ pub trait NodePermissions { url: &Url, api_name: &str, ) -> Result<(), AnyError>; - fn check_read(&self, path: &Path) -> Result<(), AnyError>; + #[inline(always)] + fn check_read(&self, path: &Path) -> Result<(), AnyError> { + self.check_read_with_api_name(path, None) + } + fn check_read_with_api_name( + &self, + path: &Path, + api_name: Option<&str>, + ) -> Result<(), AnyError>; fn check_sys(&self, kind: &str, api_name: &str) -> Result<(), AnyError>; } @@ -64,7 +72,11 @@ impl NodePermissions for AllowAllNodePermissions { ) -> Result<(), AnyError> { Ok(()) } - fn check_read(&self, _path: &Path) -> Result<(), AnyError> { + fn check_read_with_api_name( + &self, + _path: &Path, + _api_name: Option<&str>, + ) -> Result<(), AnyError> { Ok(()) } fn check_sys(&self, _kind: &str, _api_name: &str) -> Result<(), AnyError> { @@ -227,6 +239,7 @@ deno_core::extension!(deno_node, ops::crypto::x509::op_node_x509_get_valid_to, ops::crypto::x509::op_node_x509_get_serial_number, ops::crypto::x509::op_node_x509_key_usage, + ops::fs::op_node_fs_exists_sync<P>, ops::winerror::op_node_sys_to_uv_error, ops::v8::op_v8_cached_data_version_tag, ops::v8::op_v8_get_heap_statistics, diff --git a/ext/node/ops/fs.rs b/ext/node/ops/fs.rs new file mode 100644 index 000000000..b21652634 --- /dev/null +++ b/ext/node/ops/fs.rs @@ -0,0 +1,26 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. + +use std::path::PathBuf; + +use deno_core::error::AnyError; +use deno_core::op2; +use deno_core::OpState; +use deno_fs::FileSystemRc; + +use crate::NodePermissions; + +#[op2(fast)] +pub fn op_node_fs_exists_sync<P>( + state: &mut OpState, + #[string] path: String, +) -> Result<bool, AnyError> +where + P: NodePermissions + 'static, +{ + let path = PathBuf::from(path); + state + .borrow_mut::<P>() + .check_read_with_api_name(&path, Some("node:fs.existsSync()"))?; + let fs = state.borrow::<FileSystemRc>(); + Ok(fs.lstat_sync(&path).is_ok()) +} diff --git a/ext/node/ops/mod.rs b/ext/node/ops/mod.rs index d1bb4b7f4..ec4324da3 100644 --- a/ext/node/ops/mod.rs +++ b/ext/node/ops/mod.rs @@ -1,6 +1,7 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. pub mod crypto; +pub mod fs; pub mod http; pub mod http2; pub mod idna; diff --git a/ext/node/polyfills/_fs/_fs_exists.ts b/ext/node/polyfills/_fs/_fs_exists.ts index 9c8458b58..db41aedec 100644 --- a/ext/node/polyfills/_fs/_fs_exists.ts +++ b/ext/node/polyfills/_fs/_fs_exists.ts @@ -2,7 +2,7 @@ // TODO(petamoriken): enable prefer-primordials for node polyfills // deno-lint-ignore-file prefer-primordials - +const core = globalThis.__bootstrap.core; import { pathFromURL } from "ext:deno_web/00_infra.js"; type ExistsCallback = (exists: boolean) => void; @@ -35,10 +35,5 @@ Object.defineProperty(exists, kCustomPromisifiedSymbol, { */ export function existsSync(path: string | URL): boolean { path = path instanceof URL ? pathFromURL(path) : path; - try { - Deno.lstatSync(path); - return true; - } catch (_err) { - return false; - } + return core.ops.op_node_fs_exists_sync(path); } |