diff --git a/Cargo.toml b/Cargo.toml index fbe7d2a..e612dec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,13 +10,13 @@ publish = false crate-type = ["cdylib"] [dependencies] -napi = "2" -napi-derive = "2" -nodejs-resolver = "0.0.30" -serde = { version = "1.0.138", features = ["derive"] } +napi = "2.6.3" +napi-derive = "2.6.0" +nodejs-resolver = "0.0.32" +serde = { version = "1.0.139", features = ["derive"] } [build-dependencies] -napi-build = "2" +napi-build = "2.0.1" [profile.release] lto = true diff --git a/__test__/fixture/node_modules/a/package.json b/__test__/fixture/node_modules/a/package.json index 5dc65ad..f3d6caa 100644 --- a/__test__/fixture/node_modules/a/package.json +++ b/__test__/fixture/node_modules/a/package.json @@ -1,4 +1,5 @@ { + "main": "./main.js", "browser": { "./node": "./browser.js" }, diff --git a/__test__/index.spec.ts b/__test__/index.spec.ts index 23f2494..896382e 100644 --- a/__test__/index.spec.ts +++ b/__test__/index.spec.ts @@ -57,7 +57,7 @@ test('alias options', (t) => { t.is(result2, "false") }) -test('load sideeffects', (t) => { +test('load side effects', (t) => { const resolver = factory.create({}) const result = factory.loadSideEffects(resolver, path.resolve(__dirname, "./fixture/node_modules/a")); t.is(result?.boolVal, false) @@ -65,27 +65,6 @@ test('load sideeffects', (t) => { t.is(result?.pkgFilePath, path.resolve(__dirname, "./fixture/node_modules/a/package.json")) }) -test("shared cache speedy ensure", (t) => { - const sharedCache = factory.createExternalCache(); - const resolver1 = factory.createWithExternalCache({}, sharedCache); - const resolver2 = factory.createWithExternalCache({}, sharedCache); - - const uncachedStart = process.hrtime.bigint(); - factory.loadSideEffects(resolver1, path.resolve(__dirname, "./fixture/node_modules/a")); - const uncachedEnd = process.hrtime.bigint(); - const uncachedDuration = uncachedEnd - uncachedStart; - - const cachedStart = process.hrtime.bigint(); - factory.loadSideEffects(resolver2, path.resolve(__dirname, "./fixture/node_modules/a")); - const cachedEnd = process.hrtime.bigint(); - const cachedDuration = cachedEnd - cachedStart; - console.log('uncached: ', uncachedDuration, 'cached: ', cachedDuration); - // maybe expose content in cache and ensure it is not empty may be a better choice. - // but I think the following statement will usefully. - t.is(cachedDuration - uncachedDuration < 0, true) -}) - - test("without cache", (t) => { const resolver1 = factory.create({ browserField: true, diff --git a/package.json b/package.json index 00fe82d..f85095b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nodejs-resolver", - "version": "0.0.21", + "version": "0.0.22", "description": "node binding for nodejs-resolver", "main": "index.js", "license": "MIT", diff --git a/src/lib.rs b/src/lib.rs index d304266..edec4b9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,6 @@ use napi::bindgen_prelude::External; use napi_derive::napi; -use nodejs_resolver::{AliasMap, Resolver, ResolverOptions, ResolverUnsafeCache, SideEffects}; +use nodejs_resolver::{AliasMap, Resolver, ResolverOptions, ResolverCache, SideEffects}; use serde::Deserialize; use std::{ path::{Path, PathBuf}, @@ -34,13 +34,16 @@ pub struct RawResolverOptions { } impl RawResolverOptions { - pub fn normalized(&self, unsafe_cache: Option>) -> ResolverOptions { + pub fn normalized(&self, external_cache: Option>) -> ResolverOptions { let default = ResolverOptions::default(); ResolverOptions { enforce_extension: self.enforce_extension.to_owned(), extensions: self.extensions.to_owned().unwrap_or(default.extensions), alias: self.alias.to_owned().map_or(default.alias, parse_alias), - browser_field: self.browser_field.to_owned().unwrap_or(default.browser_field), + browser_field: self + .browser_field + .to_owned() + .unwrap_or(default.browser_field), condition_names: self .condition_names .to_owned() @@ -54,7 +57,7 @@ impl RawResolverOptions { main_fields: self.main_fields.to_owned().unwrap_or(default.main_fields), prefer_relative: self.prefer_relative.unwrap_or(default.prefer_relative), tsconfig: self.tsconfig_path.to_owned().map(PathBuf::from), - unsafe_cache, + external_cache, } } } @@ -85,9 +88,9 @@ pub fn create(options: RawResolverOptions) -> Result, napi::E pub struct ResolverCacheInternal {} #[napi(ts_return_type = "ExternalObject")] -pub fn create_external_cache() -> Result>, napi::Error> { +pub fn create_external_cache() -> Result>, napi::Error> { Ok(External::new( - Resolver::new(Default::default()).unsafe_cache.unwrap(), + Arc::new(ResolverCache::default()), )) } @@ -97,7 +100,7 @@ pub fn create_external_cache() -> Result>, nap )] pub fn create_with_external_cache( options: RawResolverOptions, - external_cache: External>, + external_cache: External>, ) -> Result, napi::Error> { let external_cache = external_cache.as_ref().clone(); let options = options.normalized(Some(external_cache)); @@ -146,7 +149,7 @@ pub fn load_side_effects( resolver: External, path: String, ) -> Result, napi::Error> { - match (*resolver).load_sideeffects(&Path::new(&path)) { + match (*resolver).load_side_effects(&Path::new(&path)) { Ok(val) => Ok(val.map(|val| { let (bool_val, array_val) = val .1