Skip to content

Commit

Permalink
Merge pull request #35 from supabase/fix-deno-namespace
Browse files Browse the repository at this point in the history
fix: missing values in global scope
  • Loading branch information
laktek authored Apr 8, 2023
2 parents 9c7d7b2 + 3668471 commit 5406d6e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
13 changes: 0 additions & 13 deletions crates/base/src/js_worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,6 @@ impl MainWorker {
stream: UnixStream,
shutdown_tx: oneshot::Sender<()>,
) -> Result<(), Error> {
// set bootstrap options
// TODO: Migrate this to `supacore`
let script = format!(r#"globalThis.__build_target = "{}""#, env!("TARGET"));
self.js_runtime
.execute_script::<String>(located_script_name!(), script.into())
.expect("Failed to execute bootstrap script");

let (unix_stream_tx, unix_stream_rx) = mpsc::unbounded_channel::<UnixStream>();
if let Err(e) = unix_stream_tx.send(stream) {
bail!(e)
Expand Down Expand Up @@ -329,12 +322,6 @@ impl UserWorker {
});

// set bootstrap options
// TODO: Move to `supacore`
let script = format!("globalThis.__build_target = \"{}\"", env!("TARGET"));
self.js_runtime
.execute_script::<String>(&located_script_name!(), script.into())
.expect("Failed to execute bootstrap script");

let (unix_stream_tx, unix_stream_rx) = mpsc::unbounded_channel::<UnixStream>();
if let Err(e) = unix_stream_tx.send(stream) {
bail!(e)
Expand Down
7 changes: 7 additions & 0 deletions crates/sb_core/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// build script
use std::env;

fn main() {
println!("cargo:rustc-env=TARGET={}", env::var("TARGET").unwrap());
println!("cargo:rustc-env=PROFILE={}", env::var("PROFILE").unwrap());
}
13 changes: 11 additions & 2 deletions crates/sb_core/js/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import * as tls from "ext:deno_net/02_tls.js";
import * as net from "ext:deno_net/01_net.js";
import * as response from "ext:deno_fetch/23_response.js";
import * as request from "ext:deno_fetch/23_request.js";
import * as globalInterfaces from "ext:deno_web/04_global_interfaces.js";
import { SUPABASE_USER_WORKERS } from "ext:sb_user_workers/user_workers.js";
import { SUPABASE_ENV } from "ext:sb_env/env.js";

Expand Down Expand Up @@ -537,6 +538,15 @@ delete globalThis.bootstrap;

ObjectDefineProperties(globalThis, globalScope);

const globalProperties = {
Window: globalInterfaces.windowConstructorDescriptor,
window: getterOnly(() => globalThis),
self: getterOnly(() => globalThis),
};

ObjectDefineProperties(globalThis, globalProperties);
ObjectSetPrototypeOf(globalThis, Window.prototype);

// TODO: figure out if this is needed
globalThis[webidl.brand] = webidl.brand;

Expand All @@ -556,9 +566,8 @@ runtimeStart({
tsVersion: "NA",
noColor: true,
isTty: false,
target: "supabase",
target: ops.op_build_target(),
});
delete globalThis.__build_target;

// set these overrides after runtimeStart
ObjectDefineProperties(Deno, {
Expand Down
10 changes: 8 additions & 2 deletions crates/sb_core/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::path::Path;
#[op]
fn op_main_module(state: &mut OpState) -> Result<String, AnyError> {
let main = state.borrow::<ModuleSpecifier>().to_string();
let main_url = deno_core::resolve_url_or_path(&main, Path::new("./steve-jobs"))?;
let main_url = deno_core::resolve_url_or_path(&main, std::env::current_dir()?.as_path())?;
if main_url.scheme() == "file" {
let main_path = std::env::current_dir()
.context("Failed to get current working directory")?
Expand All @@ -24,8 +24,14 @@ fn op_main_module(state: &mut OpState) -> Result<String, AnyError> {
Ok(main)
}

#[op]
fn op_build_target(_state: &mut OpState) -> String {
let target = env!("TARGET").to_string();
target
}

deno_core::extension!(sb_core_runtime,
ops = [op_main_module],
ops = [op_main_module, op_build_target],
options = {
main_module: Option<ModuleSpecifier>
},
Expand Down

0 comments on commit 5406d6e

Please sign in to comment.