From cacbab1230dfa65972b5eed3cd283fa53345a372 Mon Sep 17 00:00:00 2001 From: Nyannyacha Date: Thu, 13 Feb 2025 10:53:26 +0000 Subject: [PATCH] fix: unblock `Deno.realPathSync` --- crates/base/src/deno_runtime.rs | 6 ++++-- crates/base/test_cases/use-tmp-fs-3/index.ts | 15 +++++++++++++++ .../user-worker-san-check/.blocklisted | 1 - crates/base/tests/integration_tests.rs | 17 +++++++++++++++++ crates/fs/impl/tmp_fs.rs | 6 ++++++ ext/core/js/bootstrap.js | 1 + 6 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 crates/base/test_cases/use-tmp-fs-3/index.ts diff --git a/crates/base/src/deno_runtime.rs b/crates/base/src/deno_runtime.rs index c00b8cc0c..f541b53e9 100644 --- a/crates/base/src/deno_runtime.rs +++ b/crates/base/src/deno_runtime.rs @@ -653,8 +653,10 @@ where let build_file_system_fn = |base_fs: Arc| -> Result, AnyError> { let tmp_fs = TmpFs::try_from(maybe_tmp_fs_config.unwrap_or_default())?; - let fs = PrefixFs::new("/tmp", tmp_fs, Some(base_fs)) - .tmp_dir("/tmp"); + let tmp_fs_actual_path = tmp_fs.actual_path().to_path_buf(); + let fs = PrefixFs::new("/tmp", tmp_fs.clone(), Some(base_fs)) + .tmp_dir("/tmp") + .add_fs(tmp_fs_actual_path, tmp_fs); Ok(if let Some(s3_fs) = maybe_s3_fs_config.map(S3Fs::new).transpose()? { maybe_s3_fs = Some(s3_fs.clone()); diff --git a/crates/base/test_cases/use-tmp-fs-3/index.ts b/crates/base/test_cases/use-tmp-fs-3/index.ts new file mode 100644 index 000000000..dec50649f --- /dev/null +++ b/crates/base/test_cases/use-tmp-fs-3/index.ts @@ -0,0 +1,15 @@ +const meowFile = await Deno.create('/tmp/meow'); +const meow = new TextEncoder().encode('meowmeow'); +await meowFile.write(meow); + +const meowRealPath = Deno.realPathSync('/tmp/meow'); +const meowFileContent = await Deno.readTextFile(meowRealPath); +const isValid = meowFileContent == 'meowmeow'; + +export default { + fetch() { + return new Response(null, { + status: isValid ? 200 : 500, + }); + }, +}; diff --git a/crates/base/test_cases/user-worker-san-check/.blocklisted b/crates/base/test_cases/user-worker-san-check/.blocklisted index 86d75ff57..e116c170c 100644 --- a/crates/base/test_cases/user-worker-san-check/.blocklisted +++ b/crates/base/test_cases/user-worker-san-check/.blocklisted @@ -31,7 +31,6 @@ openSync readDirSync readLink readLinkSync -realPathSync removeSync rename renameSync diff --git a/crates/base/tests/integration_tests.rs b/crates/base/tests/integration_tests.rs index 5edc2b338..cdfdcb6d3 100644 --- a/crates/base/tests/integration_tests.rs +++ b/crates/base/tests/integration_tests.rs @@ -2826,6 +2826,23 @@ async fn test_tmp_fs_usage() { TerminationToken::new() ); } + + { + integration_test!( + "./test_cases/main", + NON_SECURE_PORT, + "use-tmp-fs-3", + None, + None, + None, + None, + (|resp| async { + let resp = resp.unwrap(); + assert_eq!(resp.status().as_u16(), 200); + }), + TerminationToken::new() + ); + } } #[tokio::test] diff --git a/crates/fs/impl/tmp_fs.rs b/crates/fs/impl/tmp_fs.rs index 1a69e849b..b554eba35 100644 --- a/crates/fs/impl/tmp_fs.rs +++ b/crates/fs/impl/tmp_fs.rs @@ -227,6 +227,12 @@ pub struct TmpFs { quota: Arc, } +impl TmpFs { + pub fn actual_path(&self) -> &Path { + self.root.path() + } +} + #[async_trait::async_trait(?Send)] impl deno_fs::FileSystem for TmpFs { fn cwd(&self) -> FsResult { diff --git a/ext/core/js/bootstrap.js b/ext/core/js/bootstrap.js index fb132b703..e25703617 100644 --- a/ext/core/js/bootstrap.js +++ b/ext/core/js/bootstrap.js @@ -641,6 +641,7 @@ globalThis.bootstrapSBEdge = (opts, ctx) => { 'open': true, 'stat': true, 'realPath': true, + 'realPathSync': true, 'create': true, 'remove': true, 'writeFile': true,