From e511022c7445cc22193edb1626c77d9674935425 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Wed, 19 Jul 2023 10:30:04 +0200 Subject: feat(ext/node): properly segregate node globals (#19307) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Code run within Deno-mode and Node-mode should have access to a slightly different set of globals. Previously this was done through a compile time code-transform for Node-mode, but this is not ideal and has many edge cases, for example Node's globalThis having a different identity than Deno's globalThis. This commit makes the `globalThis` of the entire runtime a semi-proxy. This proxy returns a different set of globals depending on the caller's mode. This is not a full proxy, because it is shadowed by "real" properties on globalThis. This is done to avoid the overhead of a full proxy for all globalThis operations. The globals between Deno-mode and Node-mode are now properly segregated. This means that code running in Deno-mode will not have access to Node's globals, and vice versa. Deleting a managed global in Deno-mode will NOT delete the corresponding global in Node-mode, and vice versa. --------- Co-authored-by: Bartek IwaƄczuk Co-authored-by: Aapo Alasuutari --- cli/cache/node.rs | 106 ++---------------------------------------------------- 1 file changed, 2 insertions(+), 104 deletions(-) (limited to 'cli/cache') diff --git a/cli/cache/node.rs b/cli/cache/node.rs index 825bdfef4..1637cbc78 100644 --- a/cli/cache/node.rs +++ b/cli/cache/node.rs @@ -12,26 +12,12 @@ use super::FastInsecureHasher; pub static NODE_ANALYSIS_CACHE_DB: CacheDBConfiguration = CacheDBConfiguration { - table_initializer: concat!( - "CREATE TABLE IF NOT EXISTS cjsanalysiscache ( + table_initializer: "CREATE TABLE IF NOT EXISTS cjsanalysiscache ( specifier TEXT PRIMARY KEY, source_hash TEXT NOT NULL, data TEXT NOT NULL );", - "CREATE UNIQUE INDEX IF NOT EXISTS cjsanalysiscacheidx - ON cjsanalysiscache(specifier);", - "CREATE TABLE IF NOT EXISTS esmglobalscache ( - specifier TEXT PRIMARY KEY, - source_hash TEXT NOT NULL, - data TEXT NOT NULL - );", - "CREATE UNIQUE INDEX IF NOT EXISTS esmglobalscacheidx - ON esmglobalscache(specifier);", - ), - on_version_change: concat!( - "DELETE FROM cjsanalysiscache;", - "DELETE FROM esmglobalscache;", - ), + on_version_change: "DELETE FROM cjsanalysiscache;", preheat_queries: &[], on_failure: CacheFailure::InMemory, }; @@ -91,29 +77,6 @@ impl NodeAnalysisCache { cjs_analysis, )); } - - pub fn get_esm_analysis( - &self, - specifier: &str, - expected_source_hash: &str, - ) -> Option> { - Self::ensure_ok( - self.inner.get_esm_analysis(specifier, expected_source_hash), - ) - } - - pub fn set_esm_analysis( - &self, - specifier: &str, - source_hash: &str, - top_level_decls: &Vec, - ) { - Self::ensure_ok(self.inner.set_esm_analysis( - specifier, - source_hash, - top_level_decls, - )) - } } #[derive(Clone)] @@ -172,54 +135,6 @@ impl NodeAnalysisCacheInner { )?; Ok(()) } - - pub fn get_esm_analysis( - &self, - specifier: &str, - expected_source_hash: &str, - ) -> Result>, AnyError> { - let query = " - SELECT - data - FROM - esmglobalscache - WHERE - specifier=?1 - AND source_hash=?2 - LIMIT 1"; - let res = self.conn.query_row( - query, - params![specifier, &expected_source_hash], - |row| { - let top_level_decls: String = row.get(0)?; - let decls: Vec = serde_json::from_str(&top_level_decls)?; - Ok(decls) - }, - )?; - Ok(res) - } - - pub fn set_esm_analysis( - &self, - specifier: &str, - source_hash: &str, - top_level_decls: &Vec, - ) -> Result<(), AnyError> { - let sql = " - INSERT OR REPLACE INTO - esmglobalscache (specifier, source_hash, data) - VALUES - (?1, ?2, ?3)"; - self.conn.execute( - sql, - params![ - specifier, - &source_hash, - &serde_json::to_string(top_level_decls)?, - ], - )?; - Ok(()) - } } #[cfg(test)] @@ -245,23 +160,10 @@ mod test { assert_eq!(actual_cjs_analysis.exports, cjs_analysis.exports); assert_eq!(actual_cjs_analysis.reexports, cjs_analysis.reexports); - assert!(cache.get_esm_analysis("file.js", "2").unwrap().is_none()); - let esm_analysis = vec!["esm1".to_string()]; - cache - .set_esm_analysis("file.js", "2", &esm_analysis) - .unwrap(); - assert!(cache.get_esm_analysis("file.js", "3").unwrap().is_none()); // different hash - let actual_esm_analysis = - cache.get_esm_analysis("file.js", "2").unwrap().unwrap(); - assert_eq!(actual_esm_analysis, esm_analysis); - // adding when already exists should not cause issue cache .set_cjs_analysis("file.js", "2", &cjs_analysis) .unwrap(); - cache - .set_esm_analysis("file.js", "2", &esm_analysis) - .unwrap(); // recreating with same cli version should still have it let conn = cache.conn.recreate_with_version("1.0.0"); @@ -270,14 +172,10 @@ mod test { cache.get_cjs_analysis("file.js", "2").unwrap().unwrap(); assert_eq!(actual_analysis.exports, cjs_analysis.exports); assert_eq!(actual_analysis.reexports, cjs_analysis.reexports); - let actual_esm_analysis = - cache.get_esm_analysis("file.js", "2").unwrap().unwrap(); - assert_eq!(actual_esm_analysis, esm_analysis); // now changing the cli version should clear it let conn = cache.conn.recreate_with_version("2.0.0"); let cache = NodeAnalysisCacheInner::new(conn); assert!(cache.get_cjs_analysis("file.js", "2").unwrap().is_none()); - assert!(cache.get_esm_analysis("file.js", "2").unwrap().is_none()); } } -- cgit v1.2.3