Skip to content

Commit dc20283

Browse files
authored
Merge pull request #36 from supabase/fix-edge-runtime-global
Fix edge runtime global
2 parents 5406d6e + d7eb441 commit dc20283

File tree

5 files changed

+46
-13
lines changed

5 files changed

+46
-13
lines changed

crates/base/src/js_worker.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use sb_core::http_start::sb_core_http;
2828
use sb_core::net::sb_core_net;
2929
use sb_core::permissions::{sb_core_permissions, Permissions};
3030
use sb_core::runtime::sb_core_runtime;
31-
use sb_core::sb_core_main;
31+
use sb_core::{sb_core_main_js, sb_core_user_js};
3232
use sb_env::sb_env;
3333
use sb_worker_context::essentials::UserWorkerMsgs;
3434
use sb_workers::sb_user_workers;
@@ -124,7 +124,7 @@ impl MainWorker {
124124
deno_tls::deno_tls::init_ops_and_esm(),
125125
sb_env::init_ops_and_esm(),
126126
sb_user_workers::init_ops_and_esm(),
127-
sb_core_main::init_ops_and_esm(),
127+
sb_core_main_js::init_ops_and_esm(),
128128
sb_core_net::init_ops_and_esm(),
129129
sb_core_http::init_ops_and_esm(),
130130
sb_core_runtime::init_ops_and_esm(Some(main_module_url.clone())),
@@ -264,7 +264,7 @@ impl UserWorker {
264264
deno_tls::deno_tls::init_ops_and_esm(),
265265
sb_env::init_ops_and_esm(),
266266
sb_user_workers::init_ops_and_esm(),
267-
sb_core_main::init_ops_and_esm(),
267+
sb_core_user_js::init_ops_and_esm(),
268268
sb_core_net::init_ops_and_esm(),
269269
sb_core_http::init_ops_and_esm(),
270270
sb_core_runtime::init_ops_and_esm(Some(main_module_url.clone())),

crates/sb_core/js/bootstrap.js

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import * as net from "ext:deno_net/01_net.js";
2323
import * as response from "ext:deno_fetch/23_response.js";
2424
import * as request from "ext:deno_fetch/23_request.js";
2525
import * as globalInterfaces from "ext:deno_web/04_global_interfaces.js";
26-
import { SUPABASE_USER_WORKERS } from "ext:sb_user_workers/user_workers.js";
2726
import { SUPABASE_ENV } from "ext:sb_env/env.js";
2827

2928

@@ -390,6 +389,28 @@ class NotSupported extends Error {
390389
}
391390
}
392391

392+
const errors = {
393+
NotFound,
394+
PermissionDenied,
395+
ConnectionRefused,
396+
ConnectionReset,
397+
ConnectionAborted,
398+
NotConnected,
399+
AddrInUse,
400+
AddrNotAvailable,
401+
BrokenPipe,
402+
AlreadyExists,
403+
InvalidData,
404+
TimedOut,
405+
Interrupted: core.Interrupted,
406+
WriteZero,
407+
UnexpectedEof,
408+
BadResource: core.BadResource,
409+
Http,
410+
Busy,
411+
NotSupported,
412+
}
413+
393414
function registerErrors() {
394415
core.registerErrorClass("NotFound", NotFound);
395416
core.registerErrorClass("PermissionDenied", PermissionDenied);
@@ -526,12 +547,6 @@ Deno.startTls = tls.startTls;
526547
Deno.resolveDns = net.resolveDns;
527548
Deno.serveHttp = serveHttp;
528549

529-
// EdgeRuntime namespace
530-
// FIXME: Make the object read-only
531-
globalThis.EdgeRuntime = {
532-
userWorkers: SUPABASE_USER_WORKERS
533-
};
534-
535550
const __bootstrap = globalThis.__bootstrap;
536551
delete globalThis.__bootstrap;
537552
delete globalThis.bootstrap;
@@ -575,7 +590,9 @@ ObjectDefineProperties(Deno, {
575590
env: readOnly(SUPABASE_ENV),
576591
pid: readOnly(globalThis.__pid),
577592
args: readOnly([]), // args are set to be empty
578-
mainModule: getterOnly(opMainModule)
593+
mainModule: getterOnly(opMainModule),
594+
errors,
595+
579596
});
580597

581598
// TODO: Abstract this file into multiple files. There's too much boilerplate

crates/sb_core/js/main_worker.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { SUPABASE_USER_WORKERS } from "ext:sb_user_workers/user_workers.js";
2+
3+
// EdgeRuntime namespace
4+
// FIXME: Make the object read-only
5+
globalThis.EdgeRuntime = {
6+
userWorkers: SUPABASE_USER_WORKERS
7+
};

crates/sb_core/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,10 @@ pub mod http_start;
22
pub mod net;
33
pub mod permissions;
44
pub mod runtime;
5-
deno_core::extension!(sb_core_main, esm = ["js/bootstrap.js"]);
5+
6+
deno_core::extension!(
7+
sb_core_main_js,
8+
esm = ["js/bootstrap.js", "js/main_worker.js"]
9+
);
10+
11+
deno_core::extension!(sb_core_user_js, esm = ["js/bootstrap.js"]);

examples/main/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ serve(async (req: Request) => {
2323
const workerTimeoutMs = 5 * 60 * 1000;
2424
const noModuleCache = false;
2525
const importMapPath = null;
26-
const envVars = [];
26+
const envVars = [
27+
["STRIPE_API_KEY", Deno.env.get("STRIPE_API_KEY")]
28+
];
29+
2730
try {
2831
const worker = await EdgeRuntime.userWorkers.create({
2932
servicePath,

0 commit comments

Comments
 (0)