Skip to content

Commit

Permalink
Merge pull request #25 from bvanjoi/next
Browse files Browse the repository at this point in the history
release: 0.0.20
  • Loading branch information
bvanjoi committed Jul 12, 2022
2 parents 57e03bd + 107afda commit aee8900
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 12 deletions.
20 changes: 20 additions & 0 deletions __test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,23 @@ test('load sideeffects', (t) => {
t.is(result?.arrayVal, undefined)
t.is(result?.pkgFilePath, path.resolve(__dirname, "./fixture/node_modules/a/package.json"))
})

test("shared cache", (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)
})
6 changes: 5 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ export interface ResolverInternal {

}
export function create(options: RawResolverOptions): ExternalObject<ResolverInternal>
export function createResolverAndInheritUnsafeCacheFromAnother(options: RawResolverOptions, another: ExternalObject<Resolver>): ExternalObject<ResolverInternal>
export function createWithExternalCache(options: RawResolverOptions, external_cache: ExternalObject<ResolverCacheInternal>): ExternalObject<ResolverInternal>
export interface ResolverCacheInternal {

}
export function createExternalCache(): ExternalObject<ResolverCacheInternal>
export function resolve(resolver: ExternalObject<ResolverInternal>, base_dir: string, id: string): string
export interface SideEffectsStats {
boolVal?: boolean
Expand Down
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,10 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`)
}

const { create, createResolverAndInheritUnsafeCacheFromAnother, resolve, loadSideEffects } = nativeBinding
const { create, createWithExternalCache, createExternalCache, resolve, loadSideEffects } = nativeBinding

module.exports.create = create
module.exports.createResolverAndInheritUnsafeCacheFromAnother = createResolverAndInheritUnsafeCacheFromAnother
module.exports.createWithExternalCache = createWithExternalCache
module.exports.createExternalCache = createExternalCache
module.exports.resolve = resolve
module.exports.loadSideEffects = loadSideEffects
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nodejs-resolver",
"version": "0.0.19",
"version": "0.0.20",
"description": "node binding for nodejs-resolver",
"main": "index.js",
"license": "MIT",
Expand Down
33 changes: 25 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,26 @@ pub fn create(options: RawResolverOptions) -> Result<External<Resolver>, napi::E
Ok(External::new(resolver))
}

#[napi(ts_return_type = "ExternalObject<ResolverInternal>")]
pub fn create_resolver_and_inherit_unsafe_cache_from_another(

#[napi(object)]
pub struct ResolverCacheInternal {}

#[napi(ts_return_type = "ExternalObject<ResolverCacheInternal>")]
pub fn create_external_cache() -> Result<External<Arc<ResolverUnsafeCache>>, napi::Error> {
Ok(External::new(
Resolver::new(Default::default()).unsafe_cache.unwrap(),
))
}

#[napi(
ts_args_type = "options: RawResolverOptions, external_cache: ExternalObject<ResolverCacheInternal>",
ts_return_type = "ExternalObject<ResolverInternal>"
)]
pub fn create_with_external_cache(
options: RawResolverOptions,
another: External<Resolver>,
external_cache: External<Arc<ResolverUnsafeCache>>,
) -> Result<External<Resolver>, napi::Error> {
let options = options.normalized(another.unsafe_cache.clone());
let options = options.normalized(Some(external_cache.clone()));
let resolver = Resolver::new(options);
Ok(External::new(resolver))
}
Expand Down Expand Up @@ -134,10 +148,13 @@ pub fn load_side_effects(
) -> Result<Option<SideEffectsStats>, napi::Error> {
match (*resolver).load_sideeffects(&Path::new(&path)) {
Ok(val) => Ok(val.map(|val| {
let (bool_val, array_val) = val.1.map(|side_effects| match side_effects {
SideEffects::Bool(bool) => (Some(bool), None),
SideEffects::Array(array) => (None, Some(array)),
}).unwrap_or((None, None));
let (bool_val, array_val) = val
.1
.map(|side_effects| match side_effects {
SideEffects::Bool(bool) => (Some(bool), None),
SideEffects::Array(array) => (None, Some(array)),
})
.unwrap_or((None, None));
SideEffectsStats {
pkg_file_path: val.0.display().to_string(),
bool_val,
Expand Down

0 comments on commit aee8900

Please sign in to comment.