aquakilo
aquakilo5mo ago

How can I execute DOM-reliant javascript in deno_core?

I just want to execute some javascript in the background of my rust app, but I need a virtual dom to use some modules. I'm brand new to deno/rust, so apologies if I'm missing something obvious. But I don't understand if what I'm trying to do is even possible lol. I'm trying to get linkedom to work with deno_core, but I get the error
error: ReferenceError: window is not defined
at https://esm.sh/v135/node_perf_hooks.js:1:109
error: ReferenceError: window is not defined
at https://esm.sh/v135/node_perf_hooks.js:1:109
when I run the JsRuntime eventloop that imports linkedom. This is my javascript file, following the example from https://docs.deno.com/runtime/manual/advanced/jsx_dom/linkedom/
import { parseHTML } from "https://esm.sh/linkedom@0.18.4";

function JSDOM(html) {
return parseHTML(html);
}

const { document, window } = new JSDOM("<h1>hi</h1>");
import { parseHTML } from "https://esm.sh/linkedom@0.18.4";

function JSDOM(html) {
return parseHTML(html);
}

const { document, window } = new JSDOM("<h1>hi</h1>");
this is my rust function to execute the js file
async fn run_js(file_path: &str, current_dir: &Path) -> Result<(), AnyError> {
let mut js_runtime = deno_core::JsRuntime::new(deno_core::RuntimeOptions {
module_loader: Some(Rc::new(SimpleModuleLoader)),
..Default::default()
});

let main_mod_specifier = deno_core::resolve_path(file_path, current_dir)?;
let main_mod_id = js_runtime.load_main_es_module(&main_mod_specifier).await?;
let main_result = js_runtime.mod_evaluate(main_mod_id);

js_runtime
.run_event_loop(PollEventLoopOptions {
..Default::default()
})
.await?;

main_result.await
}
async fn run_js(file_path: &str, current_dir: &Path) -> Result<(), AnyError> {
let mut js_runtime = deno_core::JsRuntime::new(deno_core::RuntimeOptions {
module_loader: Some(Rc::new(SimpleModuleLoader)),
..Default::default()
});

let main_mod_specifier = deno_core::resolve_path(file_path, current_dir)?;
let main_mod_id = js_runtime.load_main_es_module(&main_mod_specifier).await?;
let main_result = js_runtime.mod_evaluate(main_mod_id);

js_runtime
.run_event_loop(PollEventLoopOptions {
..Default::default()
})
.await?;

main_result.await
}
8 Replies
Deno AI Helper
Deno AI Helper5mo ago
I'm sorry, but I wasn't able to find the answer to your question in the official Deno documentation. However, based on the error message you've provided and the code snippet, it seems like the issue is related to the window object not being defined in the Deno environment you are running. Deno does not provide a window object by default as it is not a browser environment. The window object is specific to browser environments, and Deno is a runtime for executing JavaScript outside of the browser, similar to how Node.js operates. The linkedom library you are trying to use is designed to work with a DOM environment, and it seems like it's expecting a window object to be present, which is not the case in Deno's default runtime. Please note that this advice is not based on the Deno documentation, and I would advise you to double-check the accuracy of the advice given. You may need to look for a library or a way to create a virtual DOM environment within Deno that can provide a window object or find an alternative approach that does not rely on window being defined. For executing DOM-reliant JavaScript in Deno, you might need to use a library that can simulate the browser environment, such as jsdom or another virtual DOM implementation that is compatible with Deno. However, integrating such a library with deno_core and Rust might require additional setup and compatibility checks. Remember to check if there's a Deno-compatible version of linkedom or any other virtual DOM library you wish to use, and ensure that it can work without relying on browser-specific objects like window.
baphomet_the_traveller
What's the error you're getting? You'll probably need the web extension at least
aquakilo
aquakiloOP5mo ago
If it’s unclear I’m trying to use the deno_core crate in a rust desktop application, not a browser or web app.
baphomet_the_traveller
Yes I figured So youll need to set up window yourself in the global scope, the runtime doesn't do that for you You can find an example on the deno GitHub, in runtime/js
aquakilo
aquakiloOP5mo ago
Thanks!
baphomet_the_traveller
Lemme know if you have more questions
aquakilo
aquakiloOP5mo ago
where can I read more about how to use the deno_web extension? I updated my code to use a MainWorker like the runtime/js example did. So I've least progressed to another error, LocalStorage is not supported in this context. But again I'm not really sure what I'm doing or why I'm doing it with these snippets, I'm not seeing a guide but maybe I'm not looking hard enough
baphomet_the_traveller
You may find this of some use as a reference