Skip to content

Commit 39c0e7f

Browse files
authored
feat(napi): add async API (#191)
1 parent 2ea0ff4 commit 39c0e7f

File tree

8 files changed

+161
-11
lines changed

8 files changed

+161
-11
lines changed

.taplo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
include = ["*.toml"]
1+
include = ["Cargo.toml", "napi/Cargo.toml"]
22

33
[formatting]
44
align_entries = true

Cargo.lock

Lines changed: 95 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

napi/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.node

napi/Cargo.toml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
[package]
2-
name = "oxc_napi_resolver"
3-
version = "0.0.0"
4-
publish = false
2+
name = "oxc_napi_resolver"
3+
version = "0.0.0"
4+
publish = false
5+
edition = "2021"
6+
rust-version = "1.71"
57

68
[lib]
79
crate-type = ["cdylib"]
@@ -10,8 +12,9 @@ doctest = false
1012

1113
[dependencies]
1214
oxc_resolver = { path = ".." }
13-
napi = { version = "2.16.6", default-features = false, features = ["napi3", "serde-json"] }
15+
napi = { version = "2.16.6", default-features = false, features = ["napi3", "serde-json", "async"] }
1416
napi-derive = { version = "2.16.5" }
17+
tokio = "1.38.0"
1518

1619
[build-dependencies]
1720
napi-build = "2.1.3"

napi/index.d.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* auto-generated by NAPI-RS */
22
/* eslint-disable */
3-
43
export class ResolverFactory {
54
constructor(options: NapiResolveOptions)
65
static default(): ResolverFactory
@@ -9,6 +8,7 @@ export class ResolverFactory {
98
/** Clear the underlying cache. */
109
clearCache(): void
1110
sync(path: string, request: string): ResolveResult
11+
async(path: string, request: string): Promise<ResolveResult>
1212
}
1313

1414
export const enum EnforceExtension {
@@ -80,6 +80,15 @@ export interface NapiResolveOptions {
8080
* Default `[["exports"]]`.
8181
*/
8282
exportsFields?: (string | string[])[]
83+
/**
84+
* Fields from `package.json` which are used to provide the internal requests of a package
85+
* (requests starting with # are considered internal).
86+
*
87+
* Can be a path to a JSON object such as `["path", "to", "imports"]`.
88+
*
89+
* Default `[["imports"]]`.
90+
*/
91+
importsFields?: (string | string[])[]
8392
/**
8493
* An object which maps extension to extension aliases.
8594
*

napi/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,20 @@ function requireNative() {
246246

247247
}
248248
} else if (process.arch === 'arm') {
249+
if (isMusl()) {
250+
try {
251+
return require('./resolver.linux-arm-musleabihf.node')
252+
} catch (e) {
253+
loadErrors.push(e)
254+
}
249255
try {
256+
return require('@oxc-resolver/binding-linux-arm-musleabihf')
257+
} catch (e) {
258+
loadErrors.push(e)
259+
}
260+
261+
} else {
262+
try {
250263
return require('./resolver.linux-arm-gnueabihf.node')
251264
} catch (e) {
252265
loadErrors.push(e)
@@ -257,6 +270,7 @@ function requireNative() {
257270
loadErrors.push(e)
258271
}
259272

273+
}
260274
} else if (process.arch === 'riscv64') {
261275
if (isMusl()) {
262276
try {
@@ -283,6 +297,18 @@ function requireNative() {
283297
}
284298

285299
}
300+
} else if (process.arch === 'ppc64') {
301+
try {
302+
return require('./resolver.linux-ppc64-gnu.node')
303+
} catch (e) {
304+
loadErrors.push(e)
305+
}
306+
try {
307+
return require('@oxc-resolver/binding-linux-ppc64-gnu')
308+
} catch (e) {
309+
loadErrors.push(e)
310+
}
311+
286312
} else if (process.arch === 's390x') {
287313
try {
288314
return require('./resolver.linux-s390x-gnu.node')

napi/src/lib.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ extern crate napi;
22
extern crate napi_derive;
33
extern crate oxc_resolver;
44

5-
use std::path::{Path, PathBuf};
5+
use std::{
6+
path::{Path, PathBuf},
7+
sync::Arc,
8+
};
69

710
use napi_derive::napi;
811
use oxc_resolver::{ResolveOptions, Resolver};
@@ -37,26 +40,28 @@ pub fn sync(path: String, request: String) -> ResolveResult {
3740

3841
#[napi]
3942
pub struct ResolverFactory {
40-
resolver: Resolver,
43+
resolver: Arc<Resolver>,
4144
}
4245

4346
#[napi]
4447
impl ResolverFactory {
4548
#[napi(constructor)]
4649
pub fn new(options: NapiResolveOptions) -> Self {
47-
Self { resolver: Resolver::new(Self::normalize_options(options)) }
50+
Self { resolver: Arc::new(Resolver::new(Self::normalize_options(options))) }
4851
}
4952

5053
#[napi]
5154
pub fn default() -> Self {
5255
let default_options = ResolveOptions::default();
53-
Self { resolver: Resolver::new(default_options) }
56+
Self { resolver: Arc::new(Resolver::new(default_options)) }
5457
}
5558

5659
/// Clone the resolver using the same underlying cache.
5760
#[napi]
5861
pub fn clone_with_options(&self, options: NapiResolveOptions) -> Self {
59-
Self { resolver: self.resolver.clone_with_options(Self::normalize_options(options)) }
62+
Self {
63+
resolver: Arc::new(self.resolver.clone_with_options(Self::normalize_options(options))),
64+
}
6065
}
6166

6267
/// Clear the underlying cache.
@@ -72,6 +77,14 @@ impl ResolverFactory {
7277
resolve(&self.resolver, &path, &request)
7378
}
7479

80+
#[allow(clippy::needless_pass_by_value)]
81+
#[napi(js_name = "async")]
82+
pub async fn resolve_async(&self, path: String, request: String) -> ResolveResult {
83+
let path = PathBuf::from(path);
84+
let resolver = self.resolver.clone();
85+
tokio::spawn(async move { resolve(&resolver, &path, &request) }).await.unwrap()
86+
}
87+
7588
fn normalize_options(op: NapiResolveOptions) -> ResolveOptions {
7689
let default = ResolveOptions::default();
7790
// merging options

napi/test.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ assert.deepStrictEqual(resolver.sync(cwd, "./index.js").path, path.join(cwd, 'in
1515

1616
assert.strict(resolver.sync(cwd, "./ts").error.length > 0);
1717

18+
resolver.async(cwd, "./ts")
19+
.then((result) => assert.strict(result.error.length > 0));
20+
1821
const newResolver = resolver.cloneWithOptions({});
1922
newResolver.clearCache();
2023

0 commit comments

Comments
 (0)