Skip to content

Commit

Permalink
Handle env
Browse files Browse the repository at this point in the history
  • Loading branch information
max-lt committed Mar 23, 2024
1 parent cf21566 commit 9fa0d2a
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "openworkers-runtime"
version = "0.1.2"
version = "0.1.3"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
1 change: 1 addition & 0 deletions examples/scheduled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ async fn main() -> Result<(), ()> {
let script = Script {
specifier: module_url(file_path.as_str()),
code: None,
env: None,
};

let time = std::time::SystemTime::now()
Expand Down
1 change: 1 addition & 0 deletions examples/serve-new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ async fn handle_request(data: Data<AppState>, req: HttpRequest) -> HttpResponse
let script = Script {
specifier: url.clone(),
code: None,
env: None,
};

let (res_tx, res_rx) = channel::<http_v02::Response<Bytes>>();
Expand Down
1 change: 1 addition & 0 deletions examples/serve-same.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ async fn main() -> std::io::Result<()> {
let script = Script {
specifier: url.clone(),
code: None,
env: None,
};

let (task_tx, mut task_rx) = tokio::sync::mpsc::channel(1);
Expand Down
9 changes: 8 additions & 1 deletion src/ext/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ import * as eventSource from "ext:deno_fetch/27_eventsource.js";
}
}

globalThis.bootstrap = (agent) => {
globalThis.bootstrap = (agent, env) => {
if (hasBootstrapped) {
throw new Error("Worker runtime already bootstrapped");
}
Expand Down Expand Up @@ -402,6 +402,13 @@ import * as eventSource from "ext:deno_fetch/27_eventsource.js";
configurable: true,
});

ObjectDefineProperty(globalThis, "env", {
value: env,
writable: false,
enumerable: true,
configurable: false,
});

event.setEventTargetData(globalThis);
event.saveGlobalThisReference(globalThis);

Expand Down
5 changes: 3 additions & 2 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ pub(crate) fn extensions(for_snapshot: bool) -> Vec<deno_core::Extension> {

pub struct Script {
pub specifier: deno_core::ModuleSpecifier,
pub code: Option<deno_core::ModuleCodeString>
pub code: Option<deno_core::ModuleCodeString>,
pub env: Option<String>,
}

pub struct Worker {
Expand Down Expand Up @@ -118,7 +119,7 @@ impl Worker {

// Bootstrap
{
let script = format!("globalThis.bootstrap('{}')", user_agent());
let script = format!("globalThis.bootstrap('{}', {})", user_agent(), script.env.unwrap_or("undefined".to_string()));
let script = deno_core::ModuleCodeString::from(script);

match js_runtime.execute_script(deno_core::located_script_name!(), script) {
Expand Down

0 comments on commit 9fa0d2a

Please sign in to comment.