Skip to content

Commit

Permalink
feat: Jsx import source (#301)
Browse files Browse the repository at this point in the history
feat: Enable JSX on Edge runtime
 Co-authored-by: Nyannyacha <meow@nnc.gg>
  • Loading branch information
andreespirela committed May 16, 2024
1 parent 196d41d commit 08ff8af
Show file tree
Hide file tree
Showing 24 changed files with 380 additions and 19 deletions.
2 changes: 2 additions & 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 @@ -22,6 +22,7 @@ deno_fs.workspace = true
deno_io = { workspace = true }
deno_core.workspace = true
deno_console = { workspace = true }
deno_config = { workspace = true }
deno_crypto = { workspace = true }
deno_fetch = { workspace = true }
deno_http = { workspace = true }
Expand Down
4 changes: 4 additions & 0 deletions crates/base/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub async fn start_server(
termination_token: Option<TerminationToken>,
static_patterns: Vec<String>,
inspector_option: Option<InspectorOption>,
jsx_specifier: Option<String>,
jsx_module: Option<String>,
) -> Result<(), Error> {
let mut server = Server::new(
ip,
Expand All @@ -40,6 +42,8 @@ pub async fn start_server(
termination_token,
static_patterns,
inspector_option.map(Inspector::from_option),
jsx_specifier,
jsx_module,
)
.await?;

Expand Down
80 changes: 76 additions & 4 deletions crates/base/src/deno_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ impl DenoRuntime {
maybe_decorator,
maybe_module_code,
static_patterns,
maybe_jsx_import_source_config,
..
} = opts;

Expand All @@ -229,8 +230,15 @@ impl DenoRuntime {

let is_user_worker = conf.is_user_worker();

// TODO: check for other potential main paths (eg: index.js, index.tsx)
let potential_exts = vec!["ts", "tsx", "js", "jsx"];
let mut main_module_url = base_url.join("index.ts")?;
for potential_ext in potential_exts {
main_module_url = base_url.join(format!("index.{}", potential_ext).as_str())?;
if main_module_url.to_file_path().unwrap().exists() {
break;
}
}

let is_some_entry_point = maybe_entrypoint.is_some();
if is_some_entry_point {
main_module_url = Url::parse(&maybe_entrypoint.unwrap())?;
Expand Down Expand Up @@ -265,6 +273,12 @@ impl DenoRuntime {
emitter_factory.set_file_fetcher_cache_strategy(cache_strategy);
emitter_factory.set_decorator_type(maybe_decorator);

if let Some(jsx_import_source_config) = maybe_jsx_import_source_config.clone() {
emitter_factory
.set_jsx_import_source(jsx_import_source_config)
.await;
}

let maybe_import_map = load_import_map(import_map_path.clone())?;

emitter_factory.set_import_map(maybe_import_map);
Expand Down Expand Up @@ -886,6 +900,7 @@ extern "C" fn mem_check_gc_prologue_callback_fn(
mod test {
use crate::deno_runtime::DenoRuntime;
use crate::rt_worker::worker::DuplexStreamEntry;
use deno_config::JsxImportSourceConfig;
use deno_core::{FastString, ModuleCodeString, PollEventLoopOptions};
use sb_graph::emitter::EmitterFactory;
use sb_graph::{generate_binary_eszip, EszipPayloadKind};
Expand All @@ -903,6 +918,7 @@ mod test {
use std::time::Duration;
use tokio::sync::mpsc;
use tokio::time::timeout;
use url::Url;

#[tokio::test]
#[serial]
Expand Down Expand Up @@ -931,6 +947,7 @@ mod test {
})
},
static_patterns: vec![],
maybe_jsx_import_source_config: None,
},
None,
)
Expand Down Expand Up @@ -975,6 +992,7 @@ mod test {
})
},
static_patterns: vec![],
maybe_jsx_import_source_config: None,
},
None,
)
Expand Down Expand Up @@ -1041,6 +1059,7 @@ mod test {
})
},
static_patterns: vec![],
maybe_jsx_import_source_config: None,
},
None,
)
Expand Down Expand Up @@ -1078,6 +1097,7 @@ mod test {
env_vars: Option<HashMap<String, String>>,
user_conf: Option<WorkerRuntimeOpts>,
static_patterns: Vec<String>,
maybe_jsx_import_source_config: Option<JsxImportSourceConfig>,
) -> DenoRuntime {
let (worker_pool_tx, _) = mpsc::unbounded_channel::<UserWorkerMsgs>();

Expand Down Expand Up @@ -1108,6 +1128,7 @@ mod test {
}
},
static_patterns,
maybe_jsx_import_source_config,
},
None,
)
Expand All @@ -1119,7 +1140,7 @@ mod test {
#[tokio::test]
#[serial]
async fn test_main_runtime_creation() {
let mut runtime = create_runtime(None, None, None, vec![]).await;
let mut runtime = create_runtime(None, None, None, vec![], None).await;

{
let scope = &mut runtime.js_runtime.handle_scope();
Expand All @@ -1144,6 +1165,7 @@ mod test {
None,
Some(WorkerRuntimeOpts::UserWorker(Default::default())),
vec![],
None,
)
.await;

Expand All @@ -1165,7 +1187,7 @@ mod test {
#[serial]
async fn test_main_rt_fs() {
let mut main_rt =
create_runtime(None, Some(std::env::vars().collect()), None, vec![]).await;
create_runtime(None, Some(std::env::vars().collect()), None, vec![], None).await;

let global_value_deno_read_file_script = main_rt
.js_runtime
Expand All @@ -1187,6 +1209,52 @@ mod test {
);
}

#[tokio::test]
#[serial]
async fn test_jsx_import_source() {
let mut main_rt = create_runtime(
Some("./test_cases/jsx-preact"),
Some(std::env::vars().collect()),
None,
vec![],
Some(JsxImportSourceConfig {
default_specifier: Some("https://esm.sh/preact".to_string()),
module: "jsx-runtime".to_string(),
base_url: Url::from_file_path(std::env::current_dir().unwrap()).unwrap(),
}),
)
.await;

let _main_mod_ev = main_rt.js_runtime.mod_evaluate(main_rt.main_module_id);
let _ = main_rt
.js_runtime
.run_event_loop(PollEventLoopOptions {
wait_for_inspector: false,
pump_v8_message_loop: true,
})
.await;

let global_value_deno_read_file_script = main_rt
.js_runtime
.execute_script(
"<anon>",
ModuleCodeString::from(
r#"
globalThis.hello;
"#
.to_string(),
),
)
.unwrap();

let jsx_read_result =
main_rt.to_value::<deno_core::serde_json::Value>(&global_value_deno_read_file_script);
assert_eq!(
jsx_read_result.unwrap().to_string(),
r#"{"type":"div","props":{"children":"Hello"},"__k":null,"__":null,"__b":0,"__e":null,"__c":null,"__v":-1,"__i":-1,"__u":0}"#
);
}

// #[tokio::test]
// async fn test_node_builtin_imports() {
// let mut main_rt = create_runtime(
Expand Down Expand Up @@ -1220,6 +1288,7 @@ mod test {
None,
Some(WorkerRuntimeOpts::UserWorker(Default::default())),
vec![String::from("./test_cases/**/*.md")],
None,
)
.await;

Expand Down Expand Up @@ -1250,6 +1319,7 @@ mod test {
None,
Some(WorkerRuntimeOpts::UserWorker(Default::default())),
vec![],
None,
)
.await;

Expand Down Expand Up @@ -1371,12 +1441,13 @@ mod test {
async fn test_os_env_vars() {
std::env::set_var("Supa_Test", "Supa_Value");
let mut main_rt =
create_runtime(None, Some(std::env::vars().collect()), None, vec![]).await;
create_runtime(None, Some(std::env::vars().collect()), None, vec![], None).await;
let mut user_rt = create_runtime(
None,
None,
Some(WorkerRuntimeOpts::UserWorker(Default::default())),
vec![],
None,
)
.await;
assert!(!main_rt.env_vars.is_empty());
Expand Down Expand Up @@ -1455,6 +1526,7 @@ mod test {
..Default::default()
})),
static_patterns.iter().map(|it| String::from(*it)).collect(),
None,
)
.await
}
Expand Down
2 changes: 2 additions & 0 deletions crates/base/src/macros/test_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ macro_rules! integration_test_listen_fut {
$token.clone(),
vec![],
None,
Some("https://esm.sh/preact".to_string()),
Some("jsx-runtime".to_string()),
)
.boxed()
}};
Expand Down
13 changes: 13 additions & 0 deletions crates/base/src/rt_worker/worker_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::rt_worker::worker::{Worker, WorkerHandler};
use crate::rt_worker::worker_pool::WorkerPool;
use anyhow::{anyhow, bail, Error};
use cpu_timer::CPUTimer;
use deno_config::JsxImportSourceConfig;
use deno_core::{InspectorSessionProxy, LocalInspectorSession};
use event_worker::events::{
BootEvent, ShutdownEvent, WorkerEventWithMetadata, WorkerEvents, WorkerMemoryUsed,
Expand Down Expand Up @@ -677,6 +678,7 @@ pub async fn send_user_worker_request(
Ok(res)
}

// Todo: Fix
#[allow(clippy::too_many_arguments)]
pub async fn create_main_worker(
main_worker_path: PathBuf,
Expand All @@ -687,6 +689,7 @@ pub async fn create_main_worker(
maybe_decorator: Option<DecoratorType>,
termination_token: Option<TerminationToken>,
inspector: Option<Inspector>,
jsx: Option<JsxImportSourceConfig>,
) -> Result<mpsc::UnboundedSender<WorkerRequestMsg>, Error> {
let mut service_path = main_worker_path.clone();
let mut maybe_eszip = None;
Expand All @@ -712,6 +715,7 @@ pub async fn create_main_worker(
conf: WorkerRuntimeOpts::MainWorker(runtime_opts),
env_vars: std::env::vars().collect(),
static_patterns: vec![],
maybe_jsx_import_source_config: jsx,
},
termination_token,
),
Expand Down Expand Up @@ -760,6 +764,7 @@ pub async fn create_events_worker(
maybe_module_code: None,
conf: WorkerRuntimeOpts::EventsWorker(EventWorkerRuntimeOpts {}),
static_patterns: vec![],
maybe_jsx_import_source_config: None,
},
termination_token,
),
Expand All @@ -778,6 +783,7 @@ pub async fn create_user_worker_pool(
termination_token: Option<TerminationToken>,
static_patterns: Vec<String>,
inspector: Option<Inspector>,
jsx: Option<JsxImportSourceConfig>,
request_idle_timeout: Option<u64>,
) -> Result<(SharedMetricSource, mpsc::UnboundedSender<UserWorkerMsgs>), Error> {
let metric_src = SharedMetricSource::default();
Expand Down Expand Up @@ -828,6 +834,13 @@ pub async fn create_user_worker_pool(
Some(UserWorkerMsgs::Create(worker_options, tx)) => {
worker_pool.create_user_worker(WorkerContextInitOpts {
static_patterns: static_patterns.clone(),
maybe_jsx_import_source_config: {
if worker_options.maybe_jsx_import_source_config.is_some() {
worker_options.maybe_jsx_import_source_config
} else {
jsx.clone()
}
},
..worker_options
}, tx, termination_token.as_ref().map(|it| it.child_token()));
}
Expand Down
2 changes: 2 additions & 0 deletions crates/base/src/rt_worker/worker_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ impl WorkerPool {
maybe_module_code,
maybe_entrypoint,
maybe_decorator,
maybe_jsx_import_source_config,
..
} = worker_options;

Expand All @@ -376,6 +377,7 @@ impl WorkerPool {
maybe_entrypoint,
maybe_decorator,
static_patterns: vec![],
maybe_jsx_import_source_config,
},
tx,
))
Expand Down
12 changes: 12 additions & 0 deletions crates/base/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::rt_worker::worker_ctx::{
use crate::rt_worker::worker_pool::WorkerPoolPolicy;
use crate::InspectorOption;
use anyhow::{anyhow, bail, Context, Error};
use deno_config::JsxImportSourceConfig;
use event_worker::events::WorkerEventWithMetadata;
use futures_util::future::{poll_fn, BoxFuture};
use futures_util::{FutureExt, Stream};
Expand Down Expand Up @@ -37,6 +38,7 @@ use tokio_rustls::rustls::pki_types::{CertificateDer, PrivateKeyDer};
use tokio_rustls::rustls::ServerConfig;
use tokio_rustls::TlsAcceptor;
use tokio_util::sync::CancellationToken;
use url::Url;

mod signal {
pub use tokio::signal::ctrl_c;
Expand Down Expand Up @@ -346,6 +348,8 @@ impl Server {
termination_token: Option<TerminationToken>,
static_patterns: Vec<String>,
inspector: Option<Inspector>,
jsx_specifier: Option<String>,
jsx_module: Option<String>,
) -> Result<Self, Error> {
let mut worker_events_tx: Option<mpsc::UnboundedSender<WorkerEventWithMetadata>> = None;
let maybe_events_entrypoint = entrypoints.events;
Expand Down Expand Up @@ -374,13 +378,20 @@ impl Server {
None
};

let jsx_config = jsx_module.map(|jsx_mod| JsxImportSourceConfig {
default_specifier: jsx_specifier,
module: jsx_mod,
base_url: Url::from_file_path(std::env::current_dir().unwrap()).unwrap(),
});

// Create a user worker pool
let (shared_metric_src, worker_pool_tx) = create_user_worker_pool(
maybe_user_worker_policy.unwrap_or_default(),
worker_events_tx,
Some(termination_tokens.pool.clone()),
static_patterns,
inspector.clone(),
jsx_config.clone(),
flags.request_idle_timeout_ms,
)
.await?;
Expand All @@ -407,6 +418,7 @@ impl Server {
} else {
None
},
jsx_config,
)
.await?;

Expand Down

0 comments on commit 08ff8af

Please sign in to comment.