aostiles
aostiles3mo ago

Enabling Web APIs embedded

Hi, I'm trying to use the TextEncoder web api in an embedded scenario. Here's what I'm doing:
struct NoopTimersPermission;
impl deno_web::TimersPermission for NoopTimersPermission {
fn allow_hrtime(&mut self) -> bool {
false
}
}

let blob_store = Arc::new(deno_web::BlobStore::default());
let extensions: Vec<Extension> = vec![
deno_url::init_ops_and_esm(),
deno_webidl::deno_webidl::init_ops_and_esm(),
deno_console::deno_console::init_ops_and_esm(),
deno_web::deno_web::init_ops_and_esm::<NoopTimersPermission>(blob_store, None)
];

let options = RuntimeOptions {
module_loader: Some(loader),
extensions,
..Default::default()
};

let mut runtime = JsRuntime::new(options);

// Run the event loop to completion
runtime.run_event_loop(PollEventLoopOptions::default()).await
.map_err(|e| format!("Failed to run event loop: {}", e))?;
struct NoopTimersPermission;
impl deno_web::TimersPermission for NoopTimersPermission {
fn allow_hrtime(&mut self) -> bool {
false
}
}

let blob_store = Arc::new(deno_web::BlobStore::default());
let extensions: Vec<Extension> = vec![
deno_url::init_ops_and_esm(),
deno_webidl::deno_webidl::init_ops_and_esm(),
deno_console::deno_console::init_ops_and_esm(),
deno_web::deno_web::init_ops_and_esm::<NoopTimersPermission>(blob_store, None)
];

let options = RuntimeOptions {
module_loader: Some(loader),
extensions,
..Default::default()
};

let mut runtime = JsRuntime::new(options);

// Run the event loop to completion
runtime.run_event_loop(PollEventLoopOptions::default()).await
.map_err(|e| format!("Failed to run event loop: {}", e))?;
but I can't seem to reference TextEncoder at runtime in my JS code. Is there something I'm doing wrong? Thanks.
3 Replies
Deno AI Helper
Deno AI Helper3mo ago
I wasn't able to find the answer to your question in the official Deno documentation. However, based on the code snippet you provided, I noticed that there might be a syntax error in your Rust implementation. The allow_hrtime function is missing a semicolon at the end of the false statement. In Rust, expressions in function bodies should end with a semicolon to indicate that they are statements. Here's the corrected version of the function:
fn allow_hrtime(&mut self) -> bool {
false;
}
fn allow_hrtime(&mut self) -> bool {
false;
}
Please note that this advice is not based on the Deno documentation, and I would advise you to double-check the accuracy of this advice. If the issue persists after making this correction, it could be related to other parts of your setup or code that are not included in your message.
BST
BST3mo ago
Rust?
Deno AI Helper
Deno AI Helper3mo ago
Apologies for the confusion. In Rust, the last expression in a function is used as the return value and should not have a semicolon if it's the value you wish to return. Therefore, the original implementation of your allow_hrtime function was correct without the semicolon:
fn allow_hrtime(&mut self) -> bool {
false
}
fn allow_hrtime(&mut self) -> bool {
false
}
If TextEncoder is not available at runtime in your JavaScript code, it's possible that the necessary extension to enable TextEncoder is not being included or properly initialized in your Deno runtime. The TextEncoder API is part of the deno_web crate, which seems to be included in your extensions. Please ensure that all necessary dependencies and extensions are correctly initialized. If the problem persists, it might be beneficial to review the official Deno documentation or seek assistance from the Deno community, as there might be additional context or steps required that are not covered in the code snippet you've provided.