D
Deno

help

deno_core Extension with Global Access

Oorvit11/14/2022
Is there a way to make a Deno Extension be accessed by Extension.op() instead of Deno.core.ops.op()?
DDoctor11/14/2022
Does this work?
const Extension = Deno.core.ops
Extension.op()
const Extension = Deno.core.ops
Extension.op()
Oorvit11/14/2022
Of course that does work, but that'd require asking everyone to add that, I'm looking for a way to make it so whenever deno_core::JsRuntime::execute_script is run, it'll just work.
Bbartlomieju11/14/2022
currently it's not possible OOTB, you need to handle it yourself
Oorvit11/14/2022
So how do extensions like console and fetch have their stuff globally? I'm willing to add a JS file to the extension.
Bbartlomieju11/14/2022
the global scope is assembled in a file called 99_main.js that is executed after all extensions have been loaded and added their relevant stuff to different window.__bootstrap namespaces
Oorvit11/14/2022
So is there no way to implement a similar functionality as that into my own project without forking?
Bbartlomieju11/14/2022
you certainly can, which crates do you use? do you use deno_runtime crate?
Oorvit11/14/2022
Yes, runtime.
Bbartlomieju11/14/2022
if you use deno_runtime it will be harder right now - @crowlkats and I are working on a PR that will make it much easier (https://github.com/denoland/deno/pull/16597) that will allows to extend global scope as you see fit
Bbartlomieju11/14/2022
all in all you should still be able to assign whatever you want to globalThis after all JS code from crates executes
Oorvit11/14/2022
What's the goal for that pr? (If any)
Bbartlomieju11/14/2022
to allow creating V8 snapshots from existing V8 snapshots so you can extend what's already in deno_runtime and change it as you see fit mainly useful for a windowing project Leo is working on
Oorvit11/14/2022
Around how long should I expect to wait for that
Bbartlomieju11/14/2022
a week probably
Oorvit11/14/2022
So next patch? Alright. I appreciate all of this work. Thanks for the knowledge.
Bbartlomieju11/14/2022
yes, next patch release
Oorvit11/25/2022
@.bartlomieju i see it was released, how would i properly add to the globalThis?
Bbartlomieju11/25/2022
@crowlkats can you give a quick rundown?
CcrowlKats11/25/2022
a live example of this is the cli in the deno repo. first is the build script: first off you need to define a path for the snapshot and path for where you have your js files https://github.com/denoland/deno/blob/main/cli/build.rs#L460-L463 and then the actual snapshot creation https://github.com/denoland/deno/blob/main/cli/build.rs#L271-L317 then you need to get the snapshot during execution https://github.com/denoland/deno/blob/main/cli/js.rs and pass it to your WorkerOptions's startup_snapshot. then, in a js file in the folder you defined as js folder you do whatever you need to do, inside the usual iief; at the end of the iief you add to the global scope whatever you want with window.__bootstrap.globalScope.windowOrWorkerGlobalScope.myThing = myThing
Oorvit12/3/2022
can this be done with extensions instead of everything in the build.rs? I dont exactly follow correctly
CcrowlKats12/3/2022
I am not sure I understand
Oorvit12/5/2022
How would I do this with an extension? Do I have to put everything in the js folder, or can I integrate extensions into this as well?
CcrowlKats12/5/2022
ah, at the time it wasnt possible, but now it is i can write you up how in a bit
Oorvit12/12/2022
Could I have this please?
CcrowlKats12/12/2022
my bad, forgot about it. will do it now
Oorvit12/12/2022
Thank you 😄
CcrowlKats12/12/2022
sadly there is no live example. first instead of doing https://github.com/denoland/deno/blob/main/cli/build.rs#L474-L475, just use a vec with deno_runtime::js::get_99_main() in it. Then, in deno_core::snapshot_util::CreateSnapshotOptions there is a new field called extensions_with_js; to that you pass a vec of extensions you want to use to add additional js files. that should be all
Oorvit1/5/2023
If I want all the default extensions in the cli, would I have to add those myself?
error: failed to run custom build command for `runtime v0.1.0 (/workspaces/runtime)`

Caused by:
process didn't exit successfully: `/workspaces/runtime/target/debug/build/runtime-7279b5b14e8d927f/build-script-build` (signal: 5, SIGTRAP: trace/breakpoint trap)
--- stderr
Unknown external reference 0x21.
<unresolved>
error: failed to run custom build command for `runtime v0.1.0 (/workspaces/runtime)`

Caused by:
process didn't exit successfully: `/workspaces/runtime/target/debug/build/runtime-7279b5b14e8d927f/build-script-build` (signal: 5, SIGTRAP: trace/breakpoint trap)
--- stderr
Unknown external reference 0x21.
<unresolved>
hmm
use deno_core::snapshot_util;
use std::path::PathBuf;

fn main() {
let snapshot_path = PathBuf::from(std::env::var_os("OUT_DIR").unwrap()).join("EVO_SNAPSHOT.bin");
create_snapshot(snapshot_path.clone());

println!("cargo:rustc-env=EVO_SNAPSHOT_PATH={}", snapshot_path.to_str().unwrap());
}

fn create_snapshot(snapshot_path: PathBuf) {
snapshot_util::create_snapshot(snapshot_util::CreateSnapshotOptions {
cargo_manifest_dir: env!("CARGO_MANIFEST_DIR"),
snapshot_path,
startup_snapshot: Some(deno_runtime::js::deno_isolate_init()),
extensions: vec![],
extensions_with_js: vec![],
additional_files: vec![deno_runtime::js::get_99_main()],
compression_cb: None,
});
}
use deno_core::snapshot_util;
use std::path::PathBuf;

fn main() {
let snapshot_path = PathBuf::from(std::env::var_os("OUT_DIR").unwrap()).join("EVO_SNAPSHOT.bin");
create_snapshot(snapshot_path.clone());

println!("cargo:rustc-env=EVO_SNAPSHOT_PATH={}", snapshot_path.to_str().unwrap());
}

fn create_snapshot(snapshot_path: PathBuf) {
snapshot_util::create_snapshot(snapshot_util::CreateSnapshotOptions {
cargo_manifest_dir: env!("CARGO_MANIFEST_DIR"),
snapshot_path,
startup_snapshot: Some(deno_runtime::js::deno_isolate_init()),
extensions: vec![],
extensions_with_js: vec![],
additional_files: vec![deno_runtime::js::get_99_main()],
compression_cb: None,
});
}
@crowlkats it errors out if you leave extensions as a vec. Is this intended? It seems you have to include every single extension the runtime uses for a successful build. Seems kinda frustrating, and would make maintaining the list frustrating.
CcrowlKats1/6/2023
unsure how we could improve that really @.bartlomieju thoughts?
Oorvit1/6/2023
I mean it wouldn't be challenging to add all the extensions to my project, it's just maintaining that list would be frustrating. The only solution I could think of for improving maintainability is making a function to get all default extensions built into deno_runtime, but that seems like an unnecessary solution to something that isn't much of a problem. Yet again, the cli also includes all those extensions, so a function would allow one list for the runtime and cli to use. runtime build script cant access runtime nvm
Bbartlomieju1/6/2023
well we had this one idea, that each Extension would specify its name in the builder and then we could add .depends_on(&["name1", "name2"]) and panic if one of the extensions hasn't been yet registered that would make debugging problems much easier in such case
CcrowlKats1/6/2023
ah yea, though that still seems not perfect maybe make a function that returns a vec of extensions in deno_runtime?
Bbartlomieju1/6/2023
that's also an option
Oorvit1/6/2023
That was the idea I had, but unless you want to do that in deno_core or made a new package for that, you couldn't access it in the runtime build script
CcrowlKats1/6/2023
yea thats the one problem with it... though would still be better than nothing
Oorvit1/7/2023
The only that would work with everything would be to create an ext module that re-exports everything but I don't see that being a good solution to a small problem Also, any specific reason why runtime defines all the default runtime extensions as extensions_with_js instead of just extensions like in the cli?
Bbartlomieju1/7/2023
Yes, if you put them in extensions_with_js the JS code defined in extension will be executed, it only need to happen once so that why they are passed in extensions in the cli
Oorvit1/7/2023
ah
use deno_core::snapshot_util;
use deno_runtime::{permissions::Permissions, *};

use std::{env, path::PathBuf};

fn main() {
if env::var("HOST").unwrap() != env::var("TARGET").unwrap() {
panic!("Cross-compiling is not supported");
}

let evo_snapshot_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("EVO_SNAPSHOT.bin");
create_evo_snapshot(evo_snapshot_path.clone());

println!(
"cargo:rustc-env=EVO_SNAPSHOT_PATH={}",
evo_snapshot_path.to_str().unwrap()
);
}

fn create_evo_snapshot(snapshot_path: PathBuf) {
// from github.com/denoland/deno/blob/master/cli/build.rs (formatted + comments removed)
let extensions = vec![
deno_webidl::init(),
deno_console::init(),
deno_url::init(),
deno_tls::init(),
deno_web::init::<Permissions>(deno_web::BlobStore::default(), Default::default()),
deno_fetch::init::<Permissions>(Default::default()),
deno_cache::init::<deno_cache::SqliteBackedCache>(None),
deno_websocket::init::<Permissions>("".to_owned(), None, None),
deno_webstorage::init(None),
deno_crypto::init(None),
deno_webgpu::init(false),
deno_broadcast_channel::init(
deno_broadcast_channel::InMemoryBroadcastChannel::default(),
false,
),
deno_node::init::<Permissions>(None),
deno_ffi::init::<Permissions>(false),
deno_net::init::<Permissions>(None, false, None),
deno_napi::init::<Permissions>(false),
deno_http::init(),
deno_flash::init::<Permissions>(false),
];

snapshot_util::create_snapshot(snapshot_util::CreateSnapshotOptions {
cargo_manifest_dir: env!("CARGO_MANIFEST_DIR"),
snapshot_path,
startup_snapshot: Some(js::deno_isolate_init()),
extensions,
extensions_with_js: vec![],
additional_files: vec![js::get_99_main()],
compression_cb: None,
});
}
use deno_core::snapshot_util;
use deno_runtime::{permissions::Permissions, *};

use std::{env, path::PathBuf};

fn main() {
if env::var("HOST").unwrap() != env::var("TARGET").unwrap() {
panic!("Cross-compiling is not supported");
}

let evo_snapshot_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("EVO_SNAPSHOT.bin");
create_evo_snapshot(evo_snapshot_path.clone());

println!(
"cargo:rustc-env=EVO_SNAPSHOT_PATH={}",
evo_snapshot_path.to_str().unwrap()
);
}

fn create_evo_snapshot(snapshot_path: PathBuf) {
// from github.com/denoland/deno/blob/master/cli/build.rs (formatted + comments removed)
let extensions = vec![
deno_webidl::init(),
deno_console::init(),
deno_url::init(),
deno_tls::init(),
deno_web::init::<Permissions>(deno_web::BlobStore::default(), Default::default()),
deno_fetch::init::<Permissions>(Default::default()),
deno_cache::init::<deno_cache::SqliteBackedCache>(None),
deno_websocket::init::<Permissions>("".to_owned(), None, None),
deno_webstorage::init(None),
deno_crypto::init(None),
deno_webgpu::init(false),
deno_broadcast_channel::init(
deno_broadcast_channel::InMemoryBroadcastChannel::default(),
false,
),
deno_node::init::<Permissions>(None),
deno_ffi::init::<Permissions>(false),
deno_net::init::<Permissions>(None, false, None),
deno_napi::init::<Permissions>(false),
deno_http::init(),
deno_flash::init::<Permissions>(false),
];

snapshot_util::create_snapshot(snapshot_util::CreateSnapshotOptions {
cargo_manifest_dir: env!("CARGO_MANIFEST_DIR"),
snapshot_path,
startup_snapshot: Some(js::deno_isolate_init()),
extensions,
extensions_with_js: vec![],
additional_files: vec![js::get_99_main()],
compression_cb: None,
});
}
it works but it looks like it'd be annoying to maintain

Looking for more? Join the community!

Recommended Posts
How to stop reading from a ReadableStream?```js let user for await (const data of readable) { if (data.user.id === 'user-id') { user = Deno.inspect, how to log class instances with gettersim trying to log a class instance that has getters in it, with `Deno.inspect` ```ts Deno.inspect(clhow to ssr preact (without fresh)i tried with preact render to string but it wouldnt ship any jsSynchronous read single line from Deno.stdinHi, how would I go about reading a single line from `Deno.stdin` synchronously?Closing a websocket server in a unit testHello, I'm an author of [rpc lib](https://github.com/deno-libs/rpc) and I'm currently writing unit tHow to cancel or abort .pipeTo()?I prefer not asking, but after I found (what I consider) an MDN easter egg, I realized this is probais it possible to change how tsx bundles to js?So I’m wondering if it’s possible to get Deno to convert ```jsx <div class="test">Potato</div> ``` tcan't load external modules when run as child process with Deno.spawnChildHi! This is really weird, but I'm trying to run deno scripts under another deno process with Deno.spFile line operationsHello, How to perform the following file manipulations in Deno ? - Read the nth line - Remove the FFI how to pass (or get) 128-bit bigints to (or from) Rust functions?I want to pass bigints from Deno and get i128 or u128 in Rust. Do I have to use typed arrays for thaUnable to view logs to debug edge function for Deno Fresh + Deno DeployThanks for making Deno, and Deploy, and Fresh. I'm a huge fan. I'm building a feature to allow userTypeScript enumsSo enums are objects with all keys being values and values being keys. Most of the time when using eHow to get complete Deno RAM usage?Do we use `Deno.memoryUsage().rss` or should it be `rss+heapUsed+external`?Deno NPM VSCode IntellisenseIs there a work around for this at the moment? I want to try out Deno's NPM support but I lose IntelNeed help making fresh, rutt and preact base path awareI'm currently trying to make fresh base path aware. I already patched rutt to work with base paths. How do I render react?I'm using the configuration as shown in the Deno manual with preact, but how would I server-side renResolving 'dynamically imported module evaluation pending, but no pending ops'I've been getting this error quite a lot, but I haven't found a single way to know *which* unresolvehttp serve and serveTls using async iterator cannot serve multiple domains```javascript async f_serveTls() { var o_self = this // check if ssl cert existsHow to test if a file was created?I'm making a deno library that creates some folders and files, how can i check if it generates then Deno bundle duplicating variablesUsing Deno 1.26.2 I am trying to bundle https://github.com/Savory/Danet-Starter using `deno bundle