Skip to content

Commit a174612

Browse files
0x5457nyannyacha
andauthored
fix(import-map): add support for import maps in user workers (#626)
* feat(import-map): add support for import maps in user workers * chore: deno fmt * chore: cargo clippy * chore: update dependency * stamp: polishing --------- Co-authored-by: Nyannyacha <meow@nnc.gg>
1 parent 13d6282 commit a174612

File tree

12 files changed

+156
-23
lines changed

12 files changed

+156
-23
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/base/src/runtime/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,11 @@ where
517517
let base_dir_path =
518518
std::env::current_dir().map(|p| p.join(&service_path))?;
519519

520+
let maybe_import_map_path = context
521+
.get("importMapPath")
522+
.and_then(|it| it.as_str())
523+
.map(str::to_string);
524+
520525
let eszip = if let Some(eszip_payload) = maybe_eszip {
521526
eszip_payload
522527
} else {
@@ -585,7 +590,9 @@ where
585590
if let Some(module_url) = main_module_url.as_ref() {
586591
builder.set_entrypoint(Some(module_url.to_file_path().unwrap()));
587592
}
588-
builder.set_no_npm(no_npm);
593+
builder
594+
.set_no_npm(no_npm)
595+
.set_import_map_path(maybe_import_map_path.clone());
589596

590597
emitter_factory.set_deno_options(builder.build()?);
591598

@@ -634,10 +641,6 @@ where
634641
.get("sourceMap")
635642
.and_then(serde_json::Value::as_bool)
636643
.unwrap_or_default();
637-
let maybe_import_map_path = context
638-
.get("importMapPath")
639-
.and_then(|it| it.as_str())
640-
.map(str::to_string);
641644

642645
let rt_provider = create_module_loader_for_standalone_from_eszip_kind(
643646
eszip,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function getHelperMessage(): string {
2+
return "import map works!";
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"imports": {
3+
"helper-from-import-map": "./helper.ts"
4+
}
5+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import * as path from "jsr:@std/path";
2+
3+
Deno.serve(async (req: Request) => {
4+
const basePath = "test_cases/user-worker-with-import-map";
5+
const url = new URL(req.url);
6+
const { pathname } = url;
7+
8+
const userWorkerPath = path.join(basePath, "user_worker");
9+
if (pathname === "/import_map") {
10+
const worker = await EdgeRuntime.userWorkers.create({
11+
servicePath: userWorkerPath,
12+
forceCreate: true,
13+
context: {
14+
importMapPath: path.join(basePath, "import_map.json"),
15+
},
16+
});
17+
return worker.fetch(req);
18+
}
19+
if (pathname === "/inline_import_map") {
20+
const inlineImportMap = {
21+
imports: {
22+
"helper-from-import-map": `./${path.join(basePath, "helper.ts")}`,
23+
},
24+
};
25+
const importMapPath = `data:${
26+
encodeURIComponent(JSON.stringify(inlineImportMap))
27+
}`;
28+
29+
const worker = await EdgeRuntime.userWorkers.create({
30+
servicePath: userWorkerPath,
31+
forceCreate: true,
32+
context: {
33+
importMapPath: importMapPath,
34+
},
35+
});
36+
return worker.fetch(req);
37+
}
38+
39+
return new Response("Not Found", { status: 404 });
40+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { getHelperMessage } from "helper-from-import-map";
2+
3+
Deno.serve((_req: Request) => {
4+
return new Response(
5+
JSON.stringify({
6+
message: getHelperMessage(),
7+
success: true,
8+
}),
9+
{
10+
headers: {
11+
"Content-Type": "application/json",
12+
Connection: "keep-alive",
13+
},
14+
},
15+
);
16+
});

crates/base/tests/integration_tests.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ use base::server::ServerEvent;
2525
use base::server::ServerFlags;
2626
use base::server::ServerHealth;
2727
use base::server::Tls;
28+
use base::utils::test_utils;
2829
use base::utils::test_utils::create_test_user_worker;
2930
use base::utils::test_utils::ensure_npm_package_installed;
3031
use base::utils::test_utils::test_user_runtime_opts;
3132
use base::utils::test_utils::test_user_worker_pool_policy;
3233
use base::utils::test_utils::TestBed;
3334
use base::utils::test_utils::TestBedBuilder;
34-
use base::utils::test_utils::{self};
3535
use base::worker;
3636
use base::worker::TerminationToken;
3737
use base::WorkerKind;
@@ -4176,6 +4176,54 @@ directory."
41764176
}
41774177
}
41784178

4179+
#[tokio::test]
4180+
#[serial]
4181+
async fn test_user_worker_with_import_map() {
4182+
let assert_fn = |resp: Result<Response, reqwest::Error>| async {
4183+
let res = resp.unwrap();
4184+
let status = res.status().as_u16();
4185+
4186+
let body_bytes = res.bytes().await.unwrap();
4187+
let body_str = String::from_utf8_lossy(&body_bytes);
4188+
4189+
assert_eq!(
4190+
status, 200,
4191+
"Expected 200, got {} with body: {}",
4192+
status, body_str
4193+
);
4194+
4195+
assert!(
4196+
body_str.contains("import map works!"),
4197+
"Expected import map works!, got: {}",
4198+
body_str
4199+
);
4200+
};
4201+
{
4202+
integration_test!(
4203+
"./test_cases/user-worker-with-import-map",
4204+
NON_SECURE_PORT,
4205+
"import_map",
4206+
None,
4207+
None,
4208+
None,
4209+
(assert_fn),
4210+
TerminationToken::new()
4211+
);
4212+
}
4213+
{
4214+
integration_test!(
4215+
"./test_cases/user-worker-with-import-map",
4216+
NON_SECURE_PORT,
4217+
"inline_import_map",
4218+
None,
4219+
None,
4220+
None,
4221+
(assert_fn),
4222+
TerminationToken::new()
4223+
);
4224+
}
4225+
}
4226+
41794227
#[derive(Deserialize)]
41804228
struct ErrorResponsePayload {
41814229
msg: String,

crates/deno_facade/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ thiserror.workspace = true
3939
tokio.workspace = true
4040
tracing.workspace = true
4141
url.workspace = true
42-
urlencoding.workspace = true
4342

4443
dashmap = "5.5.3"
4544
hashlink = "0.8"

crates/deno_facade/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ mod eszip;
1515

1616
pub mod errors;
1717
pub mod graph;
18-
pub mod import_map;
1918
pub mod jsr;
2019
pub mod metadata;
2120
pub mod module_loader;

deno/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ thiserror.workspace = true
7777
tokio.workspace = true
7878
tokio-util.workspace = true
7979
twox-hash.workspace = true
80+
urlencoding.workspace = true
8081

8182
bincode = "=1.3.3"
8283
dissimilar = "=1.0.4"

0 commit comments

Comments
 (0)