Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V3: Using JsBuffer to transfer AssetGraph to Nodejs #313

Merged
merged 2 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 0 additions & 2 deletions crates/atlaspack_core/src/asset_graph/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#[allow(clippy::module_inception)]
mod asset_graph;
mod propagate_requested_symbols;
mod serialize_asset_graph;

pub use self::asset_graph::*;
pub use self::propagate_requested_symbols::*;
pub use self::serialize_asset_graph::*;
172 changes: 0 additions & 172 deletions crates/atlaspack_core/src/asset_graph/serialize_asset_graph.rs

This file was deleted.

12 changes: 2 additions & 10 deletions crates/node-bindings/src/atlaspack/atlaspack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ use atlaspack::rpc::nodejs::NodejsRpcFactory;
use atlaspack::rpc::nodejs::NodejsWorker;
use atlaspack::rpc::RpcFactoryRef;
use atlaspack::Atlaspack;
use atlaspack_core::asset_graph::serialize_asset_graph;
use atlaspack_core::types::AtlaspackOptions;
use atlaspack_napi_helpers::JsTransferable;
use atlaspack_package_manager::PackageManagerRef;

use super::file_system_napi::FileSystemNapi;
use super::napi_result::NapiAtlaspackResult;
use super::package_manager_napi::PackageManagerNapi;
use super::serialize_asset_graph::serialize_asset_graph;

#[napi(object)]
pub struct AtlaspackNapiBuildOptions {
Expand Down Expand Up @@ -148,15 +148,7 @@ impl AtlaspackNapi {
// not supplied as JavaScript Error types. The JavaScript layer needs to handle conversions
deferred.resolve(move |env| match result {
Ok(asset_graph) => {
let mut js_object = env.create_object()?;

js_object.set_named_property("edges", env.to_js_value(&asset_graph.edges())?)?;
js_object.set_named_property(
"nodes",
serialize_asset_graph(&asset_graph, MAX_STRING_LENGTH)?,
)?;

NapiAtlaspackResult::ok(&env, js_object)
NapiAtlaspackResult::ok(&env, serialize_asset_graph(&env, &asset_graph)?)
}
Err(error) => {
let js_object = env.to_js_value(&AtlaspackError::from(&error))?;
Expand Down
1 change: 1 addition & 0 deletions crates/node-bindings/src/atlaspack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ pub mod file_system_napi;
pub mod monitoring;
pub mod napi_result;
pub mod package_manager_napi;
pub mod serialize_asset_graph;
pub mod worker;
66 changes: 66 additions & 0 deletions crates/node-bindings/src/atlaspack/serialize_asset_graph.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use napi::{Env, JsObject};
use serde::Serialize;

use atlaspack_core::asset_graph::{AssetGraph, AssetGraphNode, DependencyState};
use atlaspack_core::types::{Asset, Dependency};

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SerializedDependency {
id: String,
dependency: Dependency,
}

#[derive(Debug, Serialize)]
#[serde(tag = "type", rename_all = "camelCase")]
enum SerializedAssetGraphNode {
Root,
Entry,
Asset {
value: Asset,
},
Dependency {
value: SerializedDependency,
has_deferred: bool,
},
}

/// Returns
/// ```typescript
/// type SerializedAssetGraph = {
/// edges: Array<number>,
/// nodes: Array<JsBuffer>,
/// }
/// ```
pub fn serialize_asset_graph(env: &Env, asset_graph: &AssetGraph) -> anyhow::Result<JsObject> {
let mut js_nodes = env.create_empty_array()?;

for (i, node) in asset_graph.nodes().enumerate() {
let serialized_node = match node {
AssetGraphNode::Root => SerializedAssetGraphNode::Root,
AssetGraphNode::Entry => SerializedAssetGraphNode::Entry,
AssetGraphNode::Asset(asset_node) => SerializedAssetGraphNode::Asset {
value: asset_node.asset.clone(),
},
AssetGraphNode::Dependency(dependency_node) => SerializedAssetGraphNode::Dependency {
value: SerializedDependency {
id: dependency_node.dependency.id(),
dependency: dependency_node.dependency.as_ref().clone(),
},
has_deferred: dependency_node.state == DependencyState::Deferred,
},
};

let node_bytes = serde_json::to_vec(&serialized_node)?;
let js_buffer = env.create_buffer_with_data(node_bytes)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we do this instead of just returning an object?

if this is better why not serialise the entire thing as JSON?

Copy link
Contributor Author

@alshdavid alshdavid Jan 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we do this instead of just returning an object?

If you're referring to a napi_derive::napi(object) it's because the macros are broken in the IDE (though the build will pass). We can either move to napi_v3 (prerelease) or manually cast the properties to an object.

If you mean why convert to JSON in the first place, it's because creating the objects takes in Rust takes much longer

if this is better why not serialise the entire thing as JSON?

Returning a single JSON exceeds the max string length limit in Nodejs. Having each node be its own json allows for the total JSON size to be broken up into smaller pieces. Using buffers isn't necessary but allows the values to be moved rather than copied

let js_buffer = js_buffer.into_unknown();

js_nodes.set_element(i as u32, js_buffer)?;
}

let mut napi_asset_graph = env.create_object()?;
napi_asset_graph.set_named_property("edges", asset_graph.edges())?;
napi_asset_graph.set_named_property("nodes", js_nodes)?;

Ok(napi_asset_graph)
}
2 changes: 1 addition & 1 deletion packages/core/core/src/requests/AssetGraphRequestRust.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function createAssetGraphRequestRust(
let options = input.options;
let serializedAssetGraph = await rustAtlaspack.buildAssetGraph();

serializedAssetGraph.nodes = serializedAssetGraph.nodes.flatMap((node) =>
serializedAssetGraph.nodes = serializedAssetGraph.nodes.map((node) =>
JSON.parse(node),
);

Expand Down
Loading