Skip to content

Commit 6edf6c3

Browse files
authored
fix(base): pass import meta resolve callback when initialize JsRuntime (#421)
* fix(base): pass import meta resolve callback when initialize `JsRuntime` * chore: add an integration test
1 parent 539b706 commit 6edf6c3

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

crates/base/src/deno_runtime.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use deno_core::error::AnyError;
1212
use deno_core::url::Url;
1313
use deno_core::v8::{GCCallbackFlags, GCType, HeapStatistics, Isolate};
1414
use deno_core::{
15-
located_script_name, serde_json, JsRuntime, ModuleCodeString, ModuleId, PollEventLoopOptions,
16-
RuntimeOptions,
15+
located_script_name, serde_json, JsRuntime, ModuleCodeString, ModuleId, ModuleLoader,
16+
ModuleSpecifier, PollEventLoopOptions, ResolutionKind, RuntimeOptions,
1717
};
1818
use deno_http::DefaultHttpPropertyExtractor;
1919
use deno_tls::deno_native_certs::load_native_certs;
@@ -529,6 +529,7 @@ where
529529
compiled_wasm_module_store: None,
530530
startup_snapshot: snapshot::snapshot(),
531531
module_loader: Some(module_loader),
532+
import_meta_resolve_callback: Some(Box::new(import_meta_resolve_callback)),
532533
..Default::default()
533534
};
534535

@@ -1001,6 +1002,14 @@ where
10011002
}
10021003
}
10031004

1005+
pub fn import_meta_resolve_callback(
1006+
loader: &dyn ModuleLoader,
1007+
specifier: String,
1008+
referrer: String,
1009+
) -> Result<ModuleSpecifier, AnyError> {
1010+
loader.resolve(&specifier, &referrer, ResolutionKind::DynamicImport)
1011+
}
1012+
10041013
fn get_current_cpu_time_ns() -> Result<i64, Error> {
10051014
get_thread_time().context("can't get current thread time")
10061015
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as _meow from "npm:@imagemagick/[email protected]";
2+
3+
export default {
4+
fetch() {
5+
return new Response(import.meta.resolve("npm:@imagemagick/[email protected]"));
6+
}
7+
}

crates/base/tests/integration_tests.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2325,6 +2325,29 @@ async fn test_declarative_style_fetch_handler() {
23252325
);
23262326
}
23272327

2328+
#[tokio::test]
2329+
#[serial]
2330+
async fn test_issue_420() {
2331+
integration_test!(
2332+
"./test_cases/main",
2333+
NON_SECURE_PORT,
2334+
"issue-420",
2335+
None,
2336+
None,
2337+
None,
2338+
None,
2339+
(|resp| async {
2340+
let text = resp.unwrap().text().await.unwrap();
2341+
2342+
assert!(text.starts_with("file:///"));
2343+
assert!(text.ends_with(
2344+
"/node_modules/localhost/@imagemagick/magick-wasm/0.0.30/dist/index.js"
2345+
));
2346+
}),
2347+
TerminationToken::new()
2348+
);
2349+
}
2350+
23282351
trait AsyncReadWrite: AsyncRead + AsyncWrite + Send + Unpin {}
23292352

23302353
impl<T> AsyncReadWrite for T where T: AsyncRead + AsyncWrite + Send + Unpin {}

0 commit comments

Comments
 (0)