Skip to content

Commit

Permalink
chore: Enable default and CLI options
Browse files Browse the repository at this point in the history
  • Loading branch information
andreespirela committed Apr 11, 2024
1 parent 53c97c3 commit 7014052
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 23 deletions.
4 changes: 4 additions & 0 deletions crates/base/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,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 @@ -37,6 +39,8 @@ pub async fn start_server(
termination_token,
static_patterns,
inspector_option.map(Inspector::from_option),
jsx_specifier,
jsx_module,
)
.await?;

Expand Down
4 changes: 3 additions & 1 deletion crates/base/src/macros/test_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ macro_rules! integration_test {
},
termination_token.clone(),
vec![],
None
None,
Some("https://esm.sh/preact".to_string()),
Some("jsx-runtime".to_string())
)
.boxed();

Expand Down
14 changes: 13 additions & 1 deletion crates/base/src/rt_worker/worker_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,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 @@ -627,6 +628,8 @@ 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,
import_map_path: Option<String>,
Expand All @@ -635,6 +638,7 @@ pub async fn create_main_worker(
maybe_entrypoint: Option<String>,
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 @@ -659,7 +663,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: None,
maybe_jsx_import_source_config: jsx,
},
termination_token,
),
Expand Down Expand Up @@ -723,6 +727,7 @@ pub async fn create_user_worker_pool(
termination_token: Option<TerminationToken>,
static_patterns: Vec<String>,
inspector: Option<Inspector>,
jsx: Option<JsxImportSourceConfig>,
) -> Result<(SharedMetricSource, mpsc::UnboundedSender<UserWorkerMsgs>), Error> {
let metric_src = SharedMetricSource::default();
let (user_worker_msgs_tx, mut user_worker_msgs_rx) =
Expand Down Expand Up @@ -771,6 +776,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
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::Stream;
use hyper::{server::conn::Http, service::Service, Body, Request, Response};
Expand Down Expand Up @@ -35,6 +36,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;

pub enum ServerEvent {
ConnectionError(hyper::Error),
Expand Down Expand Up @@ -328,6 +330,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 @@ -355,13 +359,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(),
)
.await?;

Expand All @@ -386,6 +397,7 @@ impl Server {
} else {
None
},
jsx_config,
)
.await?;

Expand Down
1 change: 1 addition & 0 deletions crates/base/src/utils/integration_test_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ impl TestBedBuilder {
Some(token.clone()),
vec![],
None,
None,
)
.await
.unwrap(),
Expand Down
55 changes: 55 additions & 0 deletions crates/base/test_cases/jsx-2/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { serve } from 'https://deno.land/[email protected]/http/server.ts';

console.log('main function started');

serve(async (req: Request) => {
const url = new URL(req.url);
const { pathname } = url;
const path_parts = pathname.split('/');
const service_name = path_parts[1];

if (!service_name || service_name === '') {
const error = { msg: 'missing function name in request' };
return new Response(
JSON.stringify(error),
{ status: 400, headers: { 'Content-Type': 'application/json' } },
);
}

const servicePath = `./test_cases/${service_name}`;
console.error(`serving the request with ${servicePath}`);

const createWorker = async () => {
const memoryLimitMb = 150;
const workerTimeoutMs = 1 * 60 * 1000;
const noModuleCache = false;
const importMapPath = null;
const envVarsObj = Deno.env.toObject();
const envVars = Object.keys(envVarsObj).map((k) => [k, envVarsObj[k]]);

return await EdgeRuntime.userWorkers.create({
servicePath,
memoryLimitMb,
workerTimeoutMs,
noModuleCache,
importMapPath,
envVars
});
};

const callWorker = async () => {
try {
const worker = await createWorker();
return await worker.fetch(req);
} catch (e) {
console.error(e);
const error = { msg: e.toString() };
return new Response(
JSON.stringify(error),
{ status: 500, headers: { 'Content-Type': 'application/json' } },
);
}
};

return callWorker();
});
45 changes: 25 additions & 20 deletions crates/base/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ async fn test_not_trigger_pku_sigsegv_due_to_jit_compilation_non_cli() {
Some(pool_termination_token.clone()),
vec![],
None,
None,
)
.await
.unwrap();
Expand Down Expand Up @@ -318,6 +319,7 @@ async fn test_main_worker_boot_error() {
Some(pool_termination_token.clone()),
vec![],
None,
None,
)
.await
.unwrap();
Expand Down Expand Up @@ -404,26 +406,29 @@ async fn test_main_worker_abort_request() {
#[tokio::test]
#[serial]
async fn test_main_worker_with_jsx_function() {
integration_test!(
"./test_cases/jsx",
NON_SECURE_PORT,
"jsx-server",
None,
None,
None,
None,
(|resp: Result<reqwest::Response, reqwest::Error>| async {
let res = resp.unwrap();
assert!(res.status().as_u16() == 200);

let body_bytes = res.bytes().await.unwrap();
assert_eq!(
body_bytes,
r#"{"type":"div","props":{"children":"Hello"},"__k":null,"__":null,"__b":0,"__e":null,"__c":null,"__v":-1,"__i":-1,"__u":0}"#
);
}),
TerminationToken::new()
);
let jsx_tests: Vec<&str> = vec!["./test_cases/jsx", "./test_cases/jsx-2"];
for test_path in jsx_tests {
integration_test!(
test_path,
NON_SECURE_PORT,
"jsx-server",
None,
None,
None,
None,
(|resp: Result<reqwest::Response, reqwest::Error>| async {
let res = resp.unwrap();
assert!(res.status().as_u16() == 200);

let body_bytes = res.bytes().await.unwrap();
assert_eq!(
body_bytes,
r#"{"type":"div","props":{"children":"Hello"},"__k":null,"__":null,"__b":0,"__e":null,"__c":null,"__v":-1,"__i":-1,"__u":0}"#
);
}),
TerminationToken::new()
);
}
}

//#[tokio::test]
Expand Down
9 changes: 8 additions & 1 deletion crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ fn cli() -> Command {
.action(ArgAction::SetTrue)
)
.arg(arg!(--"static" <Path> "Glob pattern for static files to be included"))
.arg(arg!(--"jsx-specifier" <Path> "A valid JSX specifier"))
.arg(arg!(--"jsx-module" <Path> "A valid JSX module: jsx-runtime | jsx-dev-runtime | precompile | react"))
)
.subcommand(
Command::new("bundle")
Expand Down Expand Up @@ -236,6 +238,9 @@ fn main() -> Result<(), anyhow::Error> {
vec![]
};

let jsx_specifier = sub_matches.get_one::<String>("jsx-specifier").cloned();
let jsx_module = sub_matches.get_one::<String>("jsx-module").cloned();

let static_patterns: Vec<String> =
static_patterns.into_iter().map(|s| s.to_string()).collect();

Expand Down Expand Up @@ -298,7 +303,9 @@ fn main() -> Result<(), anyhow::Error> {
},
None,
static_patterns,
maybe_inspector_option
maybe_inspector_option,
jsx_specifier,
jsx_module
)
.await?;
}
Expand Down

0 comments on commit 7014052

Please sign in to comment.