Skip to content

Commit 1bc020c

Browse files
authored
fix: do not error loading Wasm module from JSR (#562)
1 parent a504ff9 commit 1bc020c

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

src/graph.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -4277,7 +4277,20 @@ impl<'a, 'graph> Builder<'a, 'graph> {
42774277
ParseModuleAndSourceInfoOptions {
42784278
specifier: requested_specifier.clone(),
42794279
maybe_headers: Default::default(),
4280-
content: Arc::new([]) as Arc<[u8]>, // we'll load the content later
4280+
// we'll load the content later
4281+
content: if MediaType::from_specifier(
4282+
&requested_specifier,
4283+
) == MediaType::Wasm
4284+
{
4285+
// TODO(#561): temporary hack until a larger refactor can be done
4286+
Arc::new([
4287+
// minimum allowed wasm module
4288+
0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00, 0x0A,
4289+
0x01, 0x00,
4290+
])
4291+
} else {
4292+
Arc::new([]) as Arc<[u8]>
4293+
},
42814294
maybe_attribute_type: maybe_attribute_type.as_ref(),
42824295
maybe_referrer: maybe_range.as_ref(),
42834296
is_root,

tests/integration_test.rs

+81
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,87 @@ async fn test_jsr_version_not_found_then_found() {
307307
}
308308
}
309309

310+
#[tokio::test]
311+
async fn test_jsr_wasm_module() {
312+
struct TestLoader;
313+
314+
impl deno_graph::source::Loader for TestLoader {
315+
fn load(
316+
&self,
317+
specifier: &ModuleSpecifier,
318+
options: LoadOptions,
319+
) -> LoadFuture {
320+
assert!(!options.is_dynamic);
321+
let specifier = specifier.clone();
322+
match specifier.as_str() {
323+
"file:///main.ts" => Box::pin(async move {
324+
Ok(Some(LoadResponse::Module {
325+
specifier: specifier.clone(),
326+
maybe_headers: None,
327+
content: b"import 'jsr:@scope/a@1".to_vec().into(),
328+
}))
329+
}),
330+
"https://jsr.io/@scope/a/meta.json" => Box::pin(async move {
331+
Ok(Some(LoadResponse::Module {
332+
specifier: specifier.clone(),
333+
maybe_headers: None,
334+
content: br#"{ "versions": { "1.0.0": {} } }"#.to_vec().into(),
335+
}))
336+
}),
337+
"https://jsr.io/@scope/a/1.0.0_meta.json" => Box::pin(async move {
338+
Ok(Some(LoadResponse::Module {
339+
specifier: specifier.clone(),
340+
maybe_headers: None,
341+
content: br#"{
342+
"exports": { ".": "./math.wasm" },
343+
"manifest": {
344+
"/math.wasm": {
345+
"size": 123,
346+
"checksum": "sha256-b8059cfb1ea623e79efbf432db31595df213c99c6534c58bec9d5f5e069344df"
347+
}
348+
},
349+
"moduleGraph2": {
350+
"/math.wasm": {
351+
"dependencies": []
352+
}
353+
}
354+
}"#
355+
.to_vec()
356+
.into(),
357+
}))
358+
}),
359+
"https://jsr.io/@scope/a/1.0.0/math.wasm" => Box::pin(async move {
360+
if options.cache_setting == CacheSetting::Only {
361+
Ok(None)
362+
} else {
363+
Ok(Some(LoadResponse::Module {
364+
specifier: specifier.clone(),
365+
maybe_headers: None,
366+
content: std::fs::read("./tests/testdata/math.wasm")
367+
.unwrap()
368+
.into(),
369+
}))
370+
}
371+
}),
372+
_ => unreachable!(),
373+
}
374+
}
375+
}
376+
377+
{
378+
let loader = TestLoader;
379+
let mut graph = ModuleGraph::new(GraphKind::All);
380+
graph
381+
.build(
382+
vec![Url::parse("file:///main.ts").unwrap()],
383+
&loader,
384+
Default::default(),
385+
)
386+
.await;
387+
graph.valid().unwrap();
388+
}
389+
}
390+
310391
#[tokio::test]
311392
async fn test_dynamic_imports_with_template_arg() {
312393
async fn run_test(

0 commit comments

Comments
 (0)