diff --git a/Cargo.lock b/Cargo.lock index 3efdb1077b2a..4a789a0d21bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -860,6 +860,15 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "convert_case" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baaaa0ecca5b51987b9423ccdc971514dd8b0bb7b4060b983d3664dad3f1f89f" +dependencies = [ + "unicode-segmentation", +] + [[package]] name = "cooked-waker" version = "5.0.0" @@ -1152,14 +1161,20 @@ dependencies = [ [[package]] name = "ctor" -version = "0.2.9" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32a2785755761f3ddc1492979ce1e48d2c00d09311c39e4466429188f3dd6501" +checksum = "07e9666f4a9a948d4f1dff0c08a4512b0f7c86414b23960104c243c10d79f4c3" dependencies = [ - "quote", - "syn 2.0.95", + "ctor-proc-macro", + "dtor", ] +[[package]] +name = "ctor-proc-macro" +version = "0.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f211af61d8efdd104f96e57adf5e426ba1bc3ed7a4ead616e15e5881fd79c4d" + [[package]] name = "darling" version = "0.14.4" @@ -1479,6 +1494,21 @@ dependencies = [ "dtoa", ] +[[package]] +name = "dtor" +version = "0.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "222ef136a1c687d4aa0395c175f2c4586e379924c352fd02f7870cf7de783c23" +dependencies = [ + "dtor-proc-macro", +] + +[[package]] +name = "dtor-proc-macro" +version = "0.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7454e41ff9012c00d53cf7f475c5e3afa3b91b7c90568495495e8d9bf47a1055" + [[package]] name = "dunce" version = "1.0.5" @@ -2687,7 +2717,7 @@ version = "1.0.0-alpha.43" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "84c12744d1279367caed41739ef094c325d53fb0ffcd4f9b84a368796f870252" dependencies = [ - "convert_case", + "convert_case 0.6.0", "proc-macro2", "quote", "syn 1.0.109", @@ -2989,9 +3019,9 @@ dependencies = [ [[package]] name = "napi" -version = "3.0.0-alpha.24" +version = "3.0.0-alpha.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5de0beff58a431b3bd6568b690bcf55d72d8dd7f8e0e613a0193a8a9a8eef26b" +checksum = "c857a2b38c994db8bec785554ab4216d45ad63469832070c86a992be0b5491ad" dependencies = [ "anyhow", "bitflags 2.6.0", @@ -3011,11 +3041,11 @@ checksum = "e28acfa557c083f6e254a786e01ba253fc56f18ee000afcd4f79af735f73a6da" [[package]] name = "napi-derive" -version = "3.0.0-alpha.22" +version = "3.0.0-alpha.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81f5e3b98fb51282253a2fa9903ae62273c68d89d6f8f304876de30354e86e1e" +checksum = "c7165d931d54f68115e651330d5fe0ae0081133d3f4ee3ab55b0b808f0c23f71" dependencies = [ - "convert_case", + "convert_case 0.8.0", "napi-derive-backend", "proc-macro2", "quote", @@ -3024,11 +3054,11 @@ dependencies = [ [[package]] name = "napi-derive-backend" -version = "2.0.0-alpha.22" +version = "2.0.0-alpha.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a986ad1072af191e8e1e10a170644025f5b2ee28f2148f3acda818210960cc1a" +checksum = "ce3f36354262054df8e1c3a73bdcd36ea13f130feb1e4d86b67cab9e10d6ef6d" dependencies = [ - "convert_case", + "convert_case 0.8.0", "proc-macro2", "quote", "semver 1.0.24", diff --git a/Cargo.toml b/Cargo.toml index ad764a2fa5e0..5bba88c03163 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/crates/node_binding/src/filename.rs b/crates/node_binding/src/filename.rs index 7f861059830f..7237a1ffcf9c 100644 --- a/crates/node_binding/src/filename.rs +++ b/crates/node_binding/src/filename.rs @@ -2,7 +2,7 @@ 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}; @@ -10,10 +10,13 @@ use rspack_napi::threadsafe_function::ThreadsafeFunction; use crate::{AssetInfo, JsPathData}; +type FilenameValue = + Either)>, String>>; + /// A js filename value. Either a string or a function #[derive(Debug)] pub struct JsFilename { - pub filename: Either), String>>, + pub filename: FilenameValue, } impl FromNapiValue for JsFilename { @@ -44,7 +47,7 @@ impl From 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), } @@ -58,7 +61,7 @@ impl From 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)), } diff --git a/crates/node_binding/src/fs_node/node.rs b/crates/node_binding/src/fs_node/node.rs index 9d2bb061bee5..2b78b28d49f4 100644 --- a/crates/node_binding/src/fs_node/node.rs +++ b/crates/node_binding/src/fs_node/node.rs @@ -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, Promise>>; +type Write = ThreadsafeFunction, Promise>>; +type Read = ThreadsafeFunction, Promise>>; +type ReadUtil = ThreadsafeFunction, Promise>>; +type ReadToEnd = ThreadsafeFunction, Promise>>; + #[derive(Debug)] #[napi(object, object_to_js = false, js_name = "ThreadsafeNodeFS")] pub struct ThreadsafeNodeFS { #[napi(ts_type = "(name: string, content: Buffer) => Promise")] - pub write_file: ThreadsafeFunction<(String, Buffer), Promise<()>>, + pub write_file: ThreadsafeFunction, Promise<()>>, #[napi(ts_type = "(name: string) => Promise")] pub remove_file: ThreadsafeFunction>, #[napi(ts_type = "(name: string) => Promise")] @@ -28,21 +34,21 @@ pub struct ThreadsafeNodeFS { #[napi(ts_type = "(name: string) => Promise")] pub lstat: ThreadsafeFunction>>, #[napi(ts_type = "(name: string, flags: string) => Promise")] - pub open: ThreadsafeFunction<(String, String), Promise>>, + pub open: Open, #[napi(ts_type = "(from: string, to: string) => Promise")] - pub rename: ThreadsafeFunction<(String, String), Promise<()>>, + pub rename: ThreadsafeFunction, Promise<()>>, #[napi(ts_type = "(fd: number) => Promise")] pub close: ThreadsafeFunction>, #[napi(ts_type = "(fd: number, content: Buffer, position: number) => Promise")] - pub write: ThreadsafeFunction<(i32, Buffer, u32), Promise>>, + pub write: Write, #[napi(ts_type = "(fd: number, content: Buffer) => Promise")] - pub write_all: ThreadsafeFunction<(i32, Buffer), Promise<()>>, + pub write_all: ThreadsafeFunction, Promise<()>>, #[napi(ts_type = "(fd: number, length: number, position: number) => Promise")] - pub read: ThreadsafeFunction<(i32, u32, u32), Promise>>, + pub read: Read, #[napi(ts_type = "(fd: number, code: number, position: number) => Promise")] - pub read_until: ThreadsafeFunction<(i32, u8, u32), Promise>>, + pub read_until: ReadUtil, #[napi(ts_type = "(fd: number, position: number) => Promise")] - pub read_to_end: ThreadsafeFunction<(i32, u32), Promise>>, + pub read_to_end: ReadToEnd, } #[napi(object, object_to_js = false)] diff --git a/crates/node_binding/src/fs_node/write.rs b/crates/node_binding/src/fs_node/write.rs index d6effdafcc98..efd0a8ed22bf 100644 --- a/crates/node_binding/src/fs_node/write.rs +++ b/crates/node_binding/src/fs_node/write.rs @@ -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) } @@ -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) } @@ -174,7 +174,7 @@ impl NodeReadStream { pub async fn try_new(file: &Utf8Path, fs: Arc) -> Result { 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)?; @@ -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)?; @@ -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)?; @@ -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)?; @@ -261,7 +261,7 @@ impl NodeWriteStream { pub async fn try_new(file: &Utf8Path, fs: Arc) -> Result { 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)?; @@ -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)?; @@ -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) } diff --git a/crates/node_binding/src/lib.rs b/crates/node_binding/src/lib.rs index 66fb30fe33bd..01f9abc98479 100644 --- a/crates/node_binding/src/lib.rs +++ b/crates/node_binding/src/lib.rs @@ -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}, diff --git a/crates/node_binding/src/raw_options/raw_builtins/raw_circular_dependency.rs b/crates/node_binding/src/raw_options/raw_builtins/raw_circular_dependency.rs index c7a8063952db..3533b77eedd9 100644 --- a/crates/node_binding/src/raw_options/raw_builtins/raw_circular_dependency.rs +++ b/crates/node_binding/src/raw_options/raw_builtins/raw_circular_dependency.rs @@ -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::{ @@ -33,10 +33,10 @@ pub struct RawCircularDependencyRspackPluginOptions { pub ignored_connections: Option>, #[debug(skip)] #[napi(ts_type = "(entrypoint: Module, modules: string[], compilation: JsCompilation) => void")] - pub on_detected: Option>, + pub on_detected: Option, ()>>, #[debug(skip)] #[napi(ts_type = "(entrypoint: Module, modules: string[], compilation: JsCompilation) => void")] - pub on_ignored: Option>, + pub on_ignored: Option, ()>>, #[debug(skip)] #[napi(ts_type = "(compilation: JsCompilation) => void")] pub on_start: Option>, @@ -55,7 +55,7 @@ impl From 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(()) }) @@ -68,7 +68,7 @@ impl From 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(()) } diff --git a/crates/node_binding/src/raw_options/raw_builtins/raw_copy.rs b/crates/node_binding/src/raw_options/raw_builtins/raw_copy.rs index 9b4fba4a92e9..87d4a12487b8 100644 --- a/crates/node_binding/src/raw_options/raw_builtins/raw_copy.rs +++ b/crates/node_binding/src/raw_options/raw_builtins/raw_copy.rs @@ -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; @@ -9,7 +12,7 @@ use rspack_plugin_copy::{ Transformer, }; -type TransformerFn = ThreadsafeFunction<(Buffer, String), Either>; +type TransformerFn = ThreadsafeFunction, Either>; type RawTransformer = Either; type RawToFn = ThreadsafeFunction; @@ -167,7 +170,7 @@ impl From 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) }) @@ -185,7 +188,7 @@ impl From 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) }) diff --git a/crates/node_binding/src/raw_options/raw_builtins/raw_http_uri.rs b/crates/node_binding/src/raw_options/raw_builtins/raw_http_uri.rs index e1378651b0e2..becc20ca15a3 100644 --- a/crates/node_binding/src/raw_options/raw_builtins/raw_http_uri.rs +++ b/crates/node_binding/src/raw_options/raw_builtins/raw_http_uri.rs @@ -1,7 +1,7 @@ use std::{collections::HashMap, fmt::Debug, sync::Arc}; use async_trait::async_trait; -use napi::bindgen_prelude::{Buffer, Either, Promise}; +use napi::bindgen_prelude::{Buffer, Either, FnArgs, Promise}; use napi_derive::napi; use rspack_fs::WritableFileSystem; use rspack_napi::threadsafe_function::ThreadsafeFunction; @@ -11,6 +11,9 @@ use rspack_plugin_schemes::{ use rspack_regex::RspackRegex; use rspack_util::asset_condition::{AssetCondition, AssetConditions}; +type HttpClientRequest = + ThreadsafeFunction)>, Promise>; + #[napi(object, object_to_js = false)] #[derive(Debug)] pub struct RawHttpUriPluginOptions { @@ -22,8 +25,7 @@ pub struct RawHttpUriPluginOptions { // pub proxy: Option, // pub frozen: Option, #[napi(ts_type = "(url: string, headers: Record) => Promise")] - pub http_client: - ThreadsafeFunction<(String, HashMap), Promise>, + pub http_client: HttpClientRequest, } #[napi(object)] @@ -33,9 +35,12 @@ pub struct JsHttpResponseRaw { pub body: Buffer, } +type JsHttpClientFunction = + ThreadsafeFunction)>, Promise>; + #[derive(Debug, Clone)] pub struct JsHttpClient { - function: ThreadsafeFunction<(String, HashMap), Promise>, + function: JsHttpClientFunction, } #[async_trait] @@ -50,7 +55,7 @@ impl HttpClient for JsHttpClient { let func = self.function.clone(); let result = func - .call_with_promise((url_owned, headers_owned)) + .call_with_promise((url_owned, headers_owned).into()) .await .map_err(|e| anyhow::anyhow!("Error calling JavaScript HTTP client: {}", e))?; diff --git a/crates/node_binding/src/raw_options/raw_builtins/raw_ignore.rs b/crates/node_binding/src/raw_options/raw_builtins/raw_ignore.rs index 5979c6d8a476..45aa1ca0603f 100644 --- a/crates/node_binding/src/raw_options/raw_builtins/raw_ignore.rs +++ b/crates/node_binding/src/raw_options/raw_builtins/raw_ignore.rs @@ -1,9 +1,10 @@ +use napi::bindgen_prelude::FnArgs; use napi_derive::napi; use rspack_napi::threadsafe_function::ThreadsafeFunction; use rspack_plugin_ignore::{CheckResourceContent, IgnorePluginOptions}; use rspack_regex::RspackRegex; -type RawCheckResource = ThreadsafeFunction<(String, String), bool>; +type RawCheckResource = ThreadsafeFunction, bool>; #[derive(Debug)] #[napi(object, object_to_js = false)] @@ -27,7 +28,7 @@ impl From for IgnorePluginOptions { let f = check_resource.clone(); Box::pin(async move { - f.call_with_sync((resource.to_owned(), context.to_owned())) + f.call_with_sync((resource.to_owned(), context.to_owned()).into()) .await }) })) diff --git a/crates/node_binding/src/raw_options/raw_builtins/raw_progress.rs b/crates/node_binding/src/raw_options/raw_builtins/raw_progress.rs index e4502fcc9c29..c856f9b4add2 100644 --- a/crates/node_binding/src/raw_options/raw_builtins/raw_progress.rs +++ b/crates/node_binding/src/raw_options/raw_builtins/raw_progress.rs @@ -1,12 +1,12 @@ use std::sync::Arc; 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_progress::{ProgressPluginDisplayOptions, ProgressPluginOptions}; -type HandlerFn = ThreadsafeFunction<(f64, String, Vec), ()>; +type HandlerFn = ThreadsafeFunction)>, ()>; #[derive(Debug)] #[napi(object, object_to_js = false)] pub struct RawProgressPluginOptions { @@ -31,7 +31,7 @@ impl From for ProgressPluginOptions { if let Some(f) = value.handler { Self::Handler(Arc::new(move |percent, msg, items| { let f = f.clone(); - Box::pin(async move { f.call_with_sync((percent, msg, items)).await }) + Box::pin(async move { f.call_with_sync((percent, msg, items).into()).await }) })) } else { Self::Default(ProgressPluginDisplayOptions { diff --git a/crates/node_binding/src/raw_options/raw_module/mod.rs b/crates/node_binding/src/raw_options/raw_module/mod.rs index 08fa27a986fe..c51b62afa81e 100644 --- a/crates/node_binding/src/raw_options/raw_module/mod.rs +++ b/crates/node_binding/src/raw_options/raw_module/mod.rs @@ -2,7 +2,7 @@ use std::{collections::HashMap, fmt::Formatter, sync::Arc}; use derive_more::Debug; use napi::{ - bindgen_prelude::{Buffer, Either3}, + bindgen_prelude::{Buffer, Either3, FnArgs}, Either, }; use napi_derive::napi; @@ -624,7 +624,7 @@ impl From for AssetResourceGeneratorOptions { type RawAssetGeneratorDataUrl = Either< RawAssetGeneratorDataUrlOptions, - ThreadsafeFunction<(Buffer, RawAssetGeneratorDataUrlFnCtx), String>, + ThreadsafeFunction, String>, >; struct RawAssetGeneratorDataUrlWrapper(RawAssetGeneratorDataUrl); @@ -652,7 +652,7 @@ impl From for AssetGeneratorDataUrl { let b = b.clone(); let source = source.into(); let ctx = ctx.into(); - Box::pin(async move { b.call_with_sync((source, ctx)).await }) + Box::pin(async move { b.call_with_sync((source, ctx).into()).await }) })), } } diff --git a/crates/node_binding/src/source.rs b/crates/node_binding/src/source.rs index a6708088a219..72ff2bb734a0 100644 --- a/crates/node_binding/src/source.rs +++ b/crates/node_binding/src/source.rs @@ -43,7 +43,6 @@ impl<'s> From> for BoxSource { } #[napi(object)] -#[derive(Clone)] pub struct JsCompatSourceOwned { pub source: Either, pub map: Option,