Skip to content

Commit

Permalink
feat: EszipV2::add_to_front() (#200)
Browse files Browse the repository at this point in the history
This new method is a generalization of `add_import_map()`. Can be used
to add any module to the front of the eszip, like for example a
serialized `deno_config::resolver::WorkspaceResolver`
  • Loading branch information
arnauorriols authored Sep 10, 2024
1 parent 74d4afd commit 0a038df
Showing 1 changed file with 54 additions and 20 deletions.
74 changes: 54 additions & 20 deletions src/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -965,47 +965,49 @@ impl EszipV2 {
/// placed at the top of the archive, so it can be read before any other
/// modules are loaded.
///
/// If a module with this specifier is already present, then this is a no-op
/// (except that this specifier will now be at the top of the archive).
/// If a module with this specifier is already present, its source is replaced
/// with the new source.
pub fn add_import_map(
&mut self,
kind: ModuleKind,
specifier: String,
source: Arc<[u8]>,
) {
debug_assert!(matches!(kind, ModuleKind::Json | ModuleKind::Jsonc));
self.add_to_front(kind, specifier.clone(), source, []);
}

/// Add an opaque data to the eszip.
pub fn add_opaque_data(&mut self, specifier: String, data: Arc<[u8]>) {
let mut modules = self.modules.0.lock().unwrap();

// If an entry with the specifier already exists, we just move that to the
// top and return without inserting new source.
if modules.contains_key(&specifier) {
modules.to_front(&specifier);
return;
}

modules.insert(
specifier.clone(),
specifier,
EszipV2Module::Module {
kind,
source: EszipV2SourceSlot::Ready(source),
kind: ModuleKind::OpaqueData,
source: EszipV2SourceSlot::Ready(data),
source_map: EszipV2SourceSlot::Ready(Arc::new([])),
},
);
modules.to_front(&specifier);
}

/// Add an opaque data to the eszip.
pub fn add_opaque_data(&mut self, specifier: String, data: Arc<[u8]>) {
// Add a module to the front of the eszip
pub fn add_to_front(
&mut self,
kind: ModuleKind,
specifier: String,
data: impl Into<Arc<[u8]>>,
source_map: impl Into<Arc<[u8]>>,
) {
let mut modules = self.modules.0.lock().unwrap();
modules.insert(
specifier,
specifier.clone(),
EszipV2Module::Module {
kind: ModuleKind::OpaqueData,
source: EszipV2SourceSlot::Ready(data),
source_map: EszipV2SourceSlot::Ready(Arc::new([])),
kind,
source: EszipV2SourceSlot::Ready(data.into()),
source_map: EszipV2SourceSlot::Ready(source_map.into()),
},
);
modules.to_front(&specifier);
}

/// Adds an npm resolution snapshot to the eszip.
Expand Down Expand Up @@ -3477,6 +3479,38 @@ mod tests {
assert_content_order!(eszip_bytes, expected_content);
}

#[tokio::test]
async fn add_to_front_adds_module_to_the_front_instead_of_the_back() {
let mut eszip = super::EszipV2::default();

eszip.add_opaque_data(
String::from("third"),
Arc::from(*b"third source added with add_opaque_data"),
);
eszip.add_to_front(
ModuleKind::OpaqueData,
String::from("second"),
*b"second source added with add_to_front",
*b"second source map added with add_to_front",
);
eszip.add_to_front(
ModuleKind::OpaqueData,
String::from("first"),
*b"first source added with add_to_front",
*b"first source map added with add_to_front",
);

let eszip_bytes = eszip.into_bytes();
let expected_content: &[&[u8]] = &[
b"first source added with add_to_front",
b"second source added with add_to_front",
b"third source added with add_opaque_data",
b"first source map added with add_to_front",
b"second source map added with add_to_front",
];
assert_content_order!(eszip_bytes, expected_content);
}

#[tokio::test]
async fn opaque_data() {
let mut eszip = super::EszipV2::default();
Expand Down

0 comments on commit 0a038df

Please sign in to comment.