-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
V3: Using JsBuffer to transfer AssetGraph to Nodejs (#313)
- Loading branch information
Showing
6 changed files
with
81 additions
and
185 deletions.
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
77 changes: 77 additions & 0 deletions
77
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,77 @@ | ||
use napi::{Env, JsObject}; | ||
use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; | ||
use serde::Serialize; | ||
|
||
use atlaspack_core::asset_graph::{AssetGraph, AssetGraphNode, DependencyState}; | ||
use atlaspack_core::types::{Asset, Dependency}; | ||
|
||
/// Returns | ||
/// ```typescript | ||
/// type SerializedAssetGraph = { | ||
/// edges: Array<number>, | ||
/// nodes: Array<JsBuffer>, | ||
/// } | ||
/// ``` | ||
pub fn serialize_asset_graph(env: &Env, asset_graph: &AssetGraph) -> anyhow::Result<JsObject> { | ||
// Serialize graph nodes in parallel | ||
let nodes = asset_graph | ||
.nodes() | ||
.collect::<Vec<_>>() | ||
.par_iter() | ||
.map(|item| { | ||
serde_json::to_vec(&match item { | ||
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, | ||
}, | ||
}) | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
// Collect the results into a JavaScript Array | ||
// TODO NAPI3 allows removing this | ||
let mut js_nodes = env.create_array_with_length(nodes.len())?; | ||
|
||
for (i, node_bytes) in nodes.into_iter().enumerate() { | ||
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)?; | ||
} | ||
|
||
// TODO use a napi_derive::napi(object) with NAPI3 | ||
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) | ||
} | ||
|
||
#[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, | ||
}, | ||
} |
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