Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(base): checking more strictly whether source map inlining is possible #458

Merged
merged 5 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ reqwest_v011.workspace = true
tracing-subscriber = { workspace = true, optional = true, features = ["env-filter", "tracing-log"] }
num-traits.workspace = true
tempfile.workspace = true
either.workspace = true

tls-listener = { version = "0.10", features = ["rustls"] }
strum = { version = "0.25", features = ["derive"] }
Expand Down
25 changes: 14 additions & 11 deletions crates/base/src/deno_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,11 @@ where
let is_user_worker = conf.is_user_worker();
let is_some_entry_point = maybe_entrypoint.is_some();

let maybe_user_conf = conf.as_user_worker();
let user_context = maybe_user_conf
.and_then(|it| it.context.clone())
.unwrap_or_default();

if is_some_entry_point {
main_module_url = Url::parse(&maybe_entrypoint.unwrap())?;
}
Expand All @@ -386,7 +391,7 @@ where
let mut allow_remote_modules = true;

if is_user_worker {
let user_conf = conf.as_user_worker().unwrap();
let user_conf = maybe_user_conf.unwrap();

net_access_disabled = user_conf.net_access_disabled;
allow_remote_modules = user_conf.allow_remote_modules;
Expand Down Expand Up @@ -514,12 +519,17 @@ where
}

let has_inspector = maybe_inspector.is_some();
let need_source_map = user_context
.get("sourceMap")
.and_then(serde_json::Value::as_bool)
.unwrap_or_default();

let rt_provider = create_module_loader_for_standalone_from_eszip_kind(
eszip,
base_dir_path.clone(),
maybe_import_map,
import_map_path,
has_inspector,
has_inspector || need_source_map,
)
.await?;

Expand Down Expand Up @@ -645,7 +655,7 @@ where
let beforeunload_mem_threshold = ArcSwapOption::<u64>::from_pointee(None);

if conf.is_user_worker() {
let conf = conf.as_user_worker().unwrap();
let conf = maybe_user_conf.unwrap();
let memory_limit_bytes = mib_to_bytes(conf.memory_limit_mb) as usize;

beforeunload_mem_threshold.store(
Expand Down Expand Up @@ -792,14 +802,7 @@ where
let extra_context = {
let mut context = serde_json::json!(RuntimeContext::get_extra_context());

json::merge_object(
&mut context,
&conf
.as_user_worker()
.and_then(|it| it.context.clone())
.map(serde_json::Value::Object)
.unwrap_or_else(|| serde_json::json!({})),
);
json::merge_object(&mut context, &serde_json::Value::Object(user_context));

context
};
Expand Down
8 changes: 8 additions & 0 deletions crates/base/test_cases/issue-456/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { STATUS_CODE } from "jsr:@std/http";
console.log(STATUS_CODE.Accepted);

export default {
fetch() {
return new Response("meow");
}
}
6 changes: 5 additions & 1 deletion crates/base/test_cases/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ Deno.serve((req: Request) => {
const importMapPath = null;
const envVarsObj = Deno.env.toObject();
const envVars = Object.keys(envVarsObj).map(k => [k, envVarsObj[k]]);
const context = {
sourceMap: req.headers.get("x-context-source-map") == "true"
};

return await EdgeRuntime.userWorkers.create({
servicePath,
Expand All @@ -50,7 +53,8 @@ Deno.serve((req: Request) => {
cpuTimeHardLimitMs,
noModuleCache,
importMapPath,
envVars
envVars,
context
});
}

Expand Down
18 changes: 18 additions & 0 deletions crates/base/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2351,6 +2351,24 @@ async fn test_issue_420() {
);
}

#[tokio::test]
#[serial]
async fn test_issue_456() {
let tb = TestBedBuilder::new("./test_cases/main").build().await;
let resp = tb
.request(|b| {
b.uri("/issue-456")
.header("x-context-source-map", "true")
.body(Body::empty())
.context("can't make request")
})
.await
.unwrap();

assert_eq!(resp.status().as_u16(), StatusCode::OK);
tb.exit(Duration::from_secs(TESTBED_DEADLINE_SEC)).await;
}

#[tokio::test]
#[serial]
async fn test_should_render_detailed_failed_to_create_graph_error() {
Expand Down
38 changes: 28 additions & 10 deletions crates/sb_module_loader/standalone/standalone_module_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use deno_core::{ModuleSpecifier, RequestedModuleType};
use deno_semver::npm::NpmPackageReqReference;
use eszip::deno_graph;
use eszip::EszipRelativeFileBaseUrl;
use eszip::ModuleKind;
use sb_eszip_shared::AsyncEszipDataRead;
use sb_graph::resolver::CliNodeResolver;
use sb_graph::resolver::NpmModuleLoader;
Expand All @@ -41,14 +42,17 @@ impl WorkspaceEszip {
if specifier.scheme() == "file" {
let specifier_key =
EszipRelativeFileBaseUrl::new(&self.root_dir_url).specifier_key(specifier);

let module = self.eszip.ensure_module(&specifier_key)?;
let specifier = self.root_dir_url.join(&module.specifier).unwrap();

Some(WorkspaceEszipModule {
specifier,
inner: module,
})
} else {
let module = self.eszip.ensure_module(specifier.as_str())?;

Some(WorkspaceEszipModule {
specifier: ModuleSpecifier::parse(&module.specifier).unwrap(),
inner: module,
Expand Down Expand Up @@ -85,6 +89,7 @@ impl ModuleLoader for EmbeddedModuleLoader {
kind
)));
}

let current_dir = std::env::current_dir().unwrap();
deno_core::resolve_path(".", &current_dir)?
} else {
Expand Down Expand Up @@ -122,11 +127,13 @@ impl ModuleLoader for EmbeddedModuleLoader {
NodeResolutionMode::Execution,
)
.map(|res| res.into_url()),

PackageJsonDepValue::Workspace(version_req) => {
let pkg_folder = self
.shared
.workspace_resolver
.resolve_workspace_pkg_json_folder_for_pkg_json_dep(alias, version_req)?;

Ok(self
.shared
.node_resolver
Expand Down Expand Up @@ -185,6 +192,7 @@ impl ModuleLoader for EmbeddedModuleLoader {
_requested_module_type: RequestedModuleType,
) -> deno_core::ModuleLoadResponse {
let include_source_map = self.include_source_map;

if original_specifier.scheme() == "data" {
let data_url_text = match deno_graph::source::RawDataUrl::parse(original_specifier)
.and_then(|url| url.decode())
Expand All @@ -197,6 +205,7 @@ impl ModuleLoader for EmbeddedModuleLoader {
))));
}
};

return deno_core::ModuleLoadResponse::Sync(Ok(deno_core::ModuleSource::new(
deno_core::ModuleType::JavaScript,
ModuleSourceCode::String(data_url_text.into()),
Expand All @@ -209,11 +218,13 @@ impl ModuleLoader for EmbeddedModuleLoader {
let npm_module_loader = self.shared.npm_module_loader.clone();
let original_specifier = original_specifier.clone();
let maybe_referrer = maybe_referrer.cloned();

return deno_core::ModuleLoadResponse::Async(
async move {
let code_source = npm_module_loader
.load(&original_specifier, maybe_referrer.as_ref())
.await?;

Ok(deno_core::ModuleSource::new_with_redirect(
match code_source.media_type {
MediaType::Json => ModuleType::Json,
Expand Down Expand Up @@ -243,37 +254,44 @@ impl ModuleLoader for EmbeddedModuleLoader {
let code = module.inner.source().await.ok_or_else(|| {
type_error(format!("Module not found: {}", original_specifier))
})?;

let code = arc_u8_to_arc_str(code)
.map_err(|_| type_error("Module source is not utf-8"))?;

let source_map = module.inner.source_map().await;
let maybe_code_with_source_map = 'scope: {
if !include_source_map {
if !include_source_map || !matches!(module.inner.kind, ModuleKind::JavaScript) {
break 'scope code;
}

let Some(source_map) = source_map else {
break 'scope code;
};
if source_map.is_empty() {
break 'scope code;
}

let mut src = code.to_string();

if src.ends_with('\n') {
if !src.ends_with('\n') {
src.push('\n');
}

src.push_str("//# sourceMappingURL=data:application/json;base64,");
base64::prelude::BASE64_STANDARD.encode_string(source_map, &mut src);
const SOURCE_MAP_PREFIX: &str =
"//# sourceMappingURL=data:application/json;base64,";

src.push_str(SOURCE_MAP_PREFIX);

base64::prelude::BASE64_STANDARD.encode_string(source_map, &mut src);
Arc::from(src)
};

Ok(deno_core::ModuleSource::new_with_redirect(
match module.inner.kind {
eszip::ModuleKind::JavaScript => ModuleType::JavaScript,
eszip::ModuleKind::Json => ModuleType::Json,
eszip::ModuleKind::Jsonc => {
return Err(type_error("jsonc modules not supported"))
}
eszip::ModuleKind::OpaqueData => {
ModuleKind::JavaScript => ModuleType::JavaScript,
ModuleKind::Json => ModuleType::Json,
ModuleKind::Jsonc => return Err(type_error("jsonc modules not supported")),
ModuleKind::OpaqueData => {
unreachable!();
}
},
Expand Down
Loading