diff options
Diffstat (limited to 'runtime')
-rw-r--r-- | runtime/web_worker.rs | 4 | ||||
-rw-r--r-- | runtime/worker.rs | 30 |
2 files changed, 34 insertions, 0 deletions
diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index 11bcd328a..de32e3994 100644 --- a/runtime/web_worker.rs +++ b/runtime/web_worker.rs @@ -6,6 +6,7 @@ use crate::permissions::PermissionsContainer; use crate::shared::runtime; use crate::tokio_util::create_and_run_current_thread; use crate::worker::import_meta_resolve_callback; +use crate::worker::validate_import_attributes_callback; use crate::worker::FormatJsErrorFn; use crate::BootstrapOptions; use deno_broadcast_channel::InMemoryBroadcastChannel; @@ -547,6 +548,9 @@ impl WebWorker { import_meta_resolve_callback: Some(Box::new( import_meta_resolve_callback, )), + validate_import_attributes_cb: Some(Box::new( + validate_import_attributes_callback, + )), ..Default::default() }); diff --git a/runtime/worker.rs b/runtime/worker.rs index 1fac28c6a..2cb1ab491 100644 --- a/runtime/worker.rs +++ b/runtime/worker.rs @@ -1,4 +1,5 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. +use std::collections::HashMap; use std::rc::Rc; use std::sync::atomic::AtomicBool; use std::sync::atomic::AtomicI32; @@ -62,6 +63,32 @@ pub fn import_meta_resolve_callback( ) } +// TODO(bartlomieju): temporary measurement until we start supporting more +// module types +pub fn validate_import_attributes_callback( + scope: &mut v8::HandleScope, + attributes: &HashMap<String, String>, +) { + for (key, value) in attributes { + let msg = if key != "type" { + Some(format!("\"{key}\" attribute is not supported.")) + } else if value != "json" { + Some(format!("\"{value}\" is not a valid module type.")) + } else { + None + }; + + let Some(msg) = msg else { + continue; + }; + + let message = v8::String::new(scope, &msg).unwrap(); + let exception = v8::Exception::type_error(scope, message); + scope.throw_exception(exception); + return; + } +} + #[derive(Clone, Default)] pub struct ExitCode(Arc<AtomicI32>); @@ -469,6 +496,9 @@ impl MainWorker { import_meta_resolve_callback: Some(Box::new( import_meta_resolve_callback, )), + validate_import_attributes_cb: Some(Box::new( + validate_import_attributes_callback, + )), ..Default::default() }); |