Skip to content

Commit

Permalink
fix(sb_workers): allow extra context to be passed in from js land (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyannyacha authored Nov 9, 2024
1 parent 8095acf commit 1bb7f70
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 5 deletions.
20 changes: 17 additions & 3 deletions crates/base/src/deno_runtime.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::inspector_server::Inspector;
use crate::rt_worker::supervisor::{CPUUsage, CPUUsageMetrics};
use crate::rt_worker::worker::DuplexStreamEntry;
use crate::utils::json;
use crate::utils::path::find_up;
use crate::utils::units::{bytes_to_display, mib_to_bytes};

Expand Down Expand Up @@ -610,7 +611,20 @@ where
.copied()
.unwrap_or_default(),
]),
serde_json::json!(RuntimeContext::get_runtime_context())
{
let mut runtime_context = serde_json::json!(RuntimeContext::get_runtime_context());

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

runtime_context
}
);

if let Some(inspector) = maybe_inspector.clone() {
Expand Down Expand Up @@ -1344,7 +1358,7 @@ mod test {
impl GetRuntimeContext for WithSyncFileAPI {
fn get_runtime_context() -> impl Serialize {
serde_json::json!({
"useSyncFileAPI": true,
"useReadSyncFileAPI": true,
})
}
}
Expand Down Expand Up @@ -2063,7 +2077,7 @@ mod test {
impl GetRuntimeContext for Ctx {
fn get_runtime_context() -> impl Serialize {
serde_json::json!({
"useSyncFileAPI": true,
"useReadSyncFileAPI": true,
"shouldBootstrapMockFnThrowError": true,
})
}
Expand Down
12 changes: 12 additions & 0 deletions crates/base/src/utils/json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use deno_core::serde_json::Value;

pub fn merge_object(a: &mut Value, b: &Value) {
match (a, b) {
(Value::Object(a), Value::Object(b)) => {
for (k, v) in b {
merge_object(a.entry(k.clone()).or_insert(Value::Null), v);
}
}
(a, b) => *a = b.clone(),
}
}
1 change: 1 addition & 0 deletions crates/base/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod json;
pub mod path;
pub mod units;
2 changes: 1 addition & 1 deletion crates/sb_core/js/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ globalThis.bootstrapSBEdge = (opts, extraCtx) => {
'memoryUsage': () => ops.op_runtime_memory_usage(),
};

if (extraCtx?.useSyncFileAPI) {
if (extraCtx?.useReadSyncFileAPI) {
apisToBeOverridden['readFileSync'] = true;
apisToBeOverridden['readTextFileSync'] = true;
}
Expand Down
4 changes: 4 additions & 0 deletions crates/sb_workers/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ pub struct UserWorkerRuntimeOpts {
pub allow_net: Option<Vec<String>>,
pub allow_remote_modules: bool,
pub custom_module_root: Option<String>,

pub context: Option<crate::JsonMap>,
}

impl Default for UserWorkerRuntimeOpts {
Expand All @@ -94,6 +96,8 @@ impl Default for UserWorkerRuntimeOpts {
allow_remote_modules: true,
custom_module_root: None,
service_path: None,

context: None,
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion crates/sb_workers/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use deno_config::JsxImportSourceConfig;
use deno_core::error::{custom_error, type_error, AnyError};
use deno_core::futures::stream::Peekable;
use deno_core::futures::{FutureExt, Stream, StreamExt};
use deno_core::{op2, ModuleSpecifier};
use deno_core::{op2, serde_json, ModuleSpecifier};
use deno_core::{
AsyncRefCell, AsyncResult, BufView, ByteString, CancelFuture, CancelHandle, CancelTryFuture,
JsBuffer, OpState, RcRef, Resource, ResourceId, WriteOutcome,
Expand Down Expand Up @@ -58,6 +58,8 @@ pub struct JsxImportBaseConfig {
base_url: String,
}

pub type JsonMap = serde_json::Map<String, serde_json::Value>;

#[derive(Deserialize, Serialize, Default, Debug)]
#[serde(rename_all = "camelCase")]
pub struct UserWorkerCreateOptions {
Expand Down Expand Up @@ -87,6 +89,8 @@ pub struct UserWorkerCreateOptions {

s3_fs_config: Option<S3FsConfig>,
tmp_fs_config: Option<TmpFsConfig>,

context: Option<JsonMap>,
}

#[op2(async)]
Expand Down Expand Up @@ -125,6 +129,8 @@ pub async fn op_user_worker_create(

s3_fs_config: maybe_s3_fs_config,
tmp_fs_config: maybe_tmp_fs_config,

context,
} = opts;

let user_worker_options = WorkerContextInitOpts {
Expand Down Expand Up @@ -153,6 +159,8 @@ pub async fn op_user_worker_create(
allow_remote_modules,
custom_module_root,

context,

..Default::default()
}
}),
Expand Down

0 comments on commit 1bb7f70

Please sign in to comment.