Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 43 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ ustr = { package = "ustr-fxhash", version = "1.0.1" }
xxhash-rust = { version = "0.8.14" }

# Pinned
napi = { version = "3.0.0-alpha.24", features = ["anyhow"] }
napi = { version = "3.0.0-alpha.33", features = ["anyhow"] }
napi-build = { version = "2.1.6" }
napi-derive = { version = "3.0.0-alpha.22" }
napi-derive = { version = "3.0.0-alpha.29" }

# Serialize and Deserialize
inventory = { version = "0.3.17" }
Expand Down
11 changes: 7 additions & 4 deletions crates/node_binding/src/filename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ use std::{fmt::Debug, sync::Arc};

use futures::future::BoxFuture;
use napi::{
bindgen_prelude::{FromNapiValue, TypeName},
bindgen_prelude::{FnArgs, FromNapiValue, TypeName},
Either,
};
use rspack_core::{Filename, FilenameFn, LocalFilenameFn, PathData, PublicPath};
use rspack_napi::threadsafe_function::ThreadsafeFunction;

use crate::{AssetInfo, JsPathData};

type FilenameValue =
Either<String, ThreadsafeFunction<FnArgs<(JsPathData, Option<AssetInfo>)>, String>>;

/// A js filename value. Either a string or a function
#[derive(Debug)]
pub struct JsFilename {
pub filename: Either<String, ThreadsafeFunction<(JsPathData, Option<AssetInfo>), String>>,
pub filename: FilenameValue,
}

impl FromNapiValue for JsFilename {
Expand Down Expand Up @@ -44,7 +47,7 @@ impl From<JsFilename> for Filename {
Either::B(f) => Filename::from(Arc::new(ThreadSafeFilenameFn(Arc::new(
move |path_data, asset_info| {
let f = f.clone();
Box::pin(async move { f.call_with_sync((path_data, asset_info)).await })
Box::pin(async move { f.call_with_sync((path_data, asset_info).into()).await })
},
))) as Arc<dyn FilenameFn>),
}
Expand All @@ -58,7 +61,7 @@ impl From<JsFilename> for PublicPath {
Either::B(f) => PublicPath::Filename(Filename::from(Arc::new(ThreadSafeFilenameFn(Arc::new(
move |path_data, asset_info| {
let f = f.clone();
Box::pin(async move { f.call_with_sync((path_data, asset_info)).await })
Box::pin(async move { f.call_with_sync((path_data, asset_info).into()).await })
},
))) as Arc<dyn FilenameFn>)),
}
Expand Down
24 changes: 15 additions & 9 deletions crates/node_binding/src/fs_node/node.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
use napi::{
bindgen_prelude::{Buffer, Either3, Promise},
bindgen_prelude::{Buffer, Either3, FnArgs, Promise},
Either,
};
use napi_derive::napi;
use rspack_fs::FileMetadata;
use rspack_napi::threadsafe_function::ThreadsafeFunction;

type Open = ThreadsafeFunction<FnArgs<(String, String)>, Promise<Either<i32, ()>>>;
type Write = ThreadsafeFunction<FnArgs<(i32, Buffer, u32)>, Promise<Either<u32, ()>>>;
type Read = ThreadsafeFunction<FnArgs<(i32, u32, u32)>, Promise<Either<Buffer, ()>>>;
type ReadUtil = ThreadsafeFunction<FnArgs<(i32, u8, u32)>, Promise<Either<Buffer, ()>>>;
type ReadToEnd = ThreadsafeFunction<FnArgs<(i32, u32)>, Promise<Either<Buffer, ()>>>;

#[derive(Debug)]
#[napi(object, object_to_js = false, js_name = "ThreadsafeNodeFS")]
pub struct ThreadsafeNodeFS {
#[napi(ts_type = "(name: string, content: Buffer) => Promise<void>")]
pub write_file: ThreadsafeFunction<(String, Buffer), Promise<()>>,
pub write_file: ThreadsafeFunction<FnArgs<(String, Buffer)>, Promise<()>>,
#[napi(ts_type = "(name: string) => Promise<void>")]
pub remove_file: ThreadsafeFunction<String, Promise<()>>,
#[napi(ts_type = "(name: string) => Promise<void>")]
Expand All @@ -28,21 +34,21 @@ pub struct ThreadsafeNodeFS {
#[napi(ts_type = "(name: string) => Promise<NodeFsStats | void>")]
pub lstat: ThreadsafeFunction<String, Promise<Either<NodeFsStats, ()>>>,
#[napi(ts_type = "(name: string, flags: string) => Promise<number | void>")]
pub open: ThreadsafeFunction<(String, String), Promise<Either<i32, ()>>>,
pub open: Open,
#[napi(ts_type = "(from: string, to: string) => Promise<void>")]
pub rename: ThreadsafeFunction<(String, String), Promise<()>>,
pub rename: ThreadsafeFunction<FnArgs<(String, String)>, Promise<()>>,
#[napi(ts_type = "(fd: number) => Promise<void>")]
pub close: ThreadsafeFunction<i32, Promise<()>>,
#[napi(ts_type = "(fd: number, content: Buffer, position: number) => Promise<number | void>")]
pub write: ThreadsafeFunction<(i32, Buffer, u32), Promise<Either<u32, ()>>>,
pub write: Write,
#[napi(ts_type = "(fd: number, content: Buffer) => Promise<number | void>")]
pub write_all: ThreadsafeFunction<(i32, Buffer), Promise<()>>,
pub write_all: ThreadsafeFunction<FnArgs<(i32, Buffer)>, Promise<()>>,
#[napi(ts_type = "(fd: number, length: number, position: number) => Promise<Buffer | void>")]
pub read: ThreadsafeFunction<(i32, u32, u32), Promise<Either<Buffer, ()>>>,
pub read: Read,
#[napi(ts_type = "(fd: number, code: number, position: number) => Promise<Buffer | void>")]
pub read_until: ThreadsafeFunction<(i32, u8, u32), Promise<Either<Buffer, ()>>>,
pub read_until: ReadUtil,
#[napi(ts_type = "(fd: number, position: number) => Promise<Buffer | void>")]
pub read_to_end: ThreadsafeFunction<(i32, u32), Promise<Either<Buffer, ()>>>,
pub read_to_end: ReadToEnd,
}

#[napi(object, object_to_js = false)]
Expand Down
18 changes: 9 additions & 9 deletions crates/node_binding/src/fs_node/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl WritableFileSystem for NodeFileSystem {
self
.0
.write_file
.call_with_promise((file, data.into()))
.call_with_promise((file, data.into()).into())
.await
.map_err(map_error_to_fs_error)
}
Expand Down Expand Up @@ -145,7 +145,7 @@ impl IntermediateFileSystemExtras for NodeFileSystem {
self
.0
.rename
.call_with_promise((from, to))
.call_with_promise((from, to).into())
.await
.map_err(map_error_to_fs_error)
}
Expand Down Expand Up @@ -174,7 +174,7 @@ impl NodeReadStream {
pub async fn try_new(file: &Utf8Path, fs: Arc<ThreadsafeNodeFS>) -> Result<Self> {
let res = fs
.open
.call_with_promise((file.as_str().to_string(), "r".to_string()))
.call_with_promise((file.as_str().to_string(), "r".to_string()).into())
.await
.map_err(map_error_to_fs_error)?;

Expand All @@ -191,7 +191,7 @@ impl ReadStream for NodeReadStream {
let buffer = self
.fs
.read
.call_with_promise((self.fd, length as u32, self.pos as u32))
.call_with_promise((self.fd, length as u32, self.pos as u32).into())
.await
.map_err(map_error_to_fs_error)?;

Expand All @@ -208,7 +208,7 @@ impl ReadStream for NodeReadStream {
let buffer = self
.fs
.read_until
.call_with_promise((self.fd, byte, self.pos as u32))
.call_with_promise((self.fd, byte, self.pos as u32).into())
.await
.map_err(map_error_to_fs_error)?;

Expand All @@ -224,7 +224,7 @@ impl ReadStream for NodeReadStream {
let buffer = self
.fs
.read_to_end
.call_with_promise((self.fd, self.pos as u32))
.call_with_promise((self.fd, self.pos as u32).into())
.await
.map_err(map_error_to_fs_error)?;

Expand Down Expand Up @@ -261,7 +261,7 @@ impl NodeWriteStream {
pub async fn try_new(file: &Utf8Path, fs: Arc<ThreadsafeNodeFS>) -> Result<Self> {
let res = fs
.open
.call_with_promise((file.as_str().to_string(), "w+".to_string()))
.call_with_promise((file.as_str().to_string(), "w+".to_string()).into())
.await
.map_err(map_error_to_fs_error)?;

Expand All @@ -283,7 +283,7 @@ impl WriteStream for NodeWriteStream {
let res = self
.fs
.write
.call_with_promise((self.fd, buf.to_vec().into(), self.pos as u32))
.call_with_promise((self.fd, buf.to_vec().into(), self.pos as u32).into())
.await
.map_err(map_error_to_fs_error)?;

Expand All @@ -299,7 +299,7 @@ impl WriteStream for NodeWriteStream {
self
.fs
.write_all
.call_with_promise((self.fd, buf.to_vec().into()))
.call_with_promise((self.fd, buf.to_vec().into()).into())
.await
.map_err(map_error_to_fs_error)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/node_binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ enum TraceState {
}

#[cfg(not(target_family = "wasm"))]
#[ctor]
#[napi::ctor::ctor(crate_path = ::napi::ctor)]
fn init() {
use std::{
sync::atomic::{AtomicUsize, Ordering},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use derive_more::Debug;
use napi::Either;
use napi::{bindgen_prelude::FnArgs, Either};
use napi_derive::napi;
use rspack_napi::threadsafe_function::ThreadsafeFunction;
use rspack_plugin_circular_dependencies::{
Expand Down Expand Up @@ -33,10 +33,10 @@ pub struct RawCircularDependencyRspackPluginOptions {
pub ignored_connections: Option<Vec<(ConnectionPattern, ConnectionPattern)>>,
#[debug(skip)]
#[napi(ts_type = "(entrypoint: Module, modules: string[], compilation: JsCompilation) => void")]
pub on_detected: Option<ThreadsafeFunction<CycleHookParams, ()>>,
pub on_detected: Option<ThreadsafeFunction<FnArgs<CycleHookParams>, ()>>,
#[debug(skip)]
#[napi(ts_type = "(entrypoint: Module, modules: string[], compilation: JsCompilation) => void")]
pub on_ignored: Option<ThreadsafeFunction<CycleHookParams, ()>>,
pub on_ignored: Option<ThreadsafeFunction<FnArgs<CycleHookParams>, ()>>,
#[debug(skip)]
#[napi(ts_type = "(compilation: JsCompilation) => void")]
pub on_start: Option<ThreadsafeFunction<JsCompilationWrapper, ()>>,
Expand All @@ -55,7 +55,7 @@ impl From<RawCircularDependencyRspackPluginOptions> for CircularDependencyRspack
let callback = callback.clone();
Box::pin(async move {
callback
.call_with_sync((entrypoint, modules, JsCompilationWrapper::new(compilation)))
.call_with_sync((entrypoint, modules, JsCompilationWrapper::new(compilation)).into())
.await?;
Ok(())
})
Expand All @@ -68,7 +68,7 @@ impl From<RawCircularDependencyRspackPluginOptions> for CircularDependencyRspack
let callback = callback.clone();
async move {
callback
.call_with_sync((entrypoint, modules, JsCompilationWrapper::new(compilation)))
.call_with_sync((entrypoint, modules, JsCompilationWrapper::new(compilation)).into())
.await?;
Ok(())
}
Expand Down
11 changes: 7 additions & 4 deletions crates/node_binding/src/raw_options/raw_builtins/raw_copy.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use cow_utils::CowUtils;
use derive_more::Debug;
use napi::{bindgen_prelude::Buffer, Either};
use napi::{
bindgen_prelude::{Buffer, FnArgs},
Either,
};
use napi_derive::napi;
use rspack_core::rspack_sources::RawSource;
use rspack_napi::threadsafe_function::ThreadsafeFunction;
Expand All @@ -9,7 +12,7 @@ use rspack_plugin_copy::{
Transformer,
};

type TransformerFn = ThreadsafeFunction<(Buffer, String), Either<String, Buffer>>;
type TransformerFn = ThreadsafeFunction<FnArgs<(Buffer, String)>, Either<String, Buffer>>;
type RawTransformer = Either<RawTransformOptions, TransformerFn>;

type RawToFn = ThreadsafeFunction<RawToOptions, String>;
Expand Down Expand Up @@ -167,7 +170,7 @@ impl From<RawCopyPattern> for CopyPattern {
}

Box::pin(async move {
f.call_with_sync((input.into(), absolute_filename.to_owned()))
f.call_with_sync((input.into(), absolute_filename.to_owned()).into())
.await
.map(convert_to_enum)
})
Expand All @@ -185,7 +188,7 @@ impl From<RawCopyPattern> for CopyPattern {
}

Box::pin(async move {
f.call_with_sync((input.into(), absolute_filename.to_owned()))
f.call_with_sync((input.into(), absolute_filename.to_owned()).into())
.await
.map(convert_to_enum)
})
Expand Down
Loading
Loading