-
Notifications
You must be signed in to change notification settings - Fork 1
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
alshdavid
merged 2 commits into
main
from
alsh/v3-using-buffers-for-assetgraph-napi-transfer
Jan 23, 2025
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
172
crates/atlaspack_core/src/asset_graph/serialize_asset_graph.rs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
crates/node-bindings/src/atlaspack/serialize_asset_graph.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)?; | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
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