2020-08-07 10:55:02 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2020-09-17 13:13:20 -04:00
|
|
|
use deno_core::error::uri_error;
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_core::serde_json::json;
|
|
|
|
use deno_core::serde_json::Value;
|
2020-09-06 15:44:29 -04:00
|
|
|
use deno_core::JsRuntime;
|
2020-09-17 13:13:20 -04:00
|
|
|
use deno_core::ZeroCopyBuf;
|
|
|
|
use idna::domain_to_ascii;
|
|
|
|
use idna::domain_to_ascii_strict;
|
|
|
|
use serde::Deserialize;
|
2020-09-30 10:51:01 -04:00
|
|
|
use std::path::PathBuf;
|
2020-08-07 10:55:02 -04:00
|
|
|
|
2020-09-17 13:13:20 -04:00
|
|
|
pub fn op_domain_to_ascii(
|
|
|
|
_state: &mut deno_core::OpState,
|
|
|
|
args: Value,
|
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
|
|
|
) -> Result<Value, AnyError> {
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct DomainToAscii {
|
|
|
|
domain: String,
|
|
|
|
be_strict: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
let args: DomainToAscii = serde_json::from_value(args)?;
|
|
|
|
if args.be_strict {
|
|
|
|
domain_to_ascii_strict(args.domain.as_str())
|
|
|
|
} else {
|
|
|
|
domain_to_ascii(args.domain.as_str())
|
|
|
|
}
|
|
|
|
.map_err(|err| {
|
|
|
|
let message = format!("Invalid IDNA encoded domain name: {:?}", err);
|
|
|
|
uri_error(message)
|
|
|
|
})
|
|
|
|
.map(|domain| json!(domain))
|
|
|
|
}
|
|
|
|
|
2020-09-30 10:51:01 -04:00
|
|
|
/// Load and execute the javascript code.
|
2020-09-06 15:44:29 -04:00
|
|
|
pub fn init(isolate: &mut JsRuntime) {
|
2020-09-01 16:32:07 -04:00
|
|
|
let files = vec![
|
2020-09-30 10:51:01 -04:00
|
|
|
(
|
|
|
|
"deno:op_crates/web/00_dom_exception.js",
|
|
|
|
include_str!("00_dom_exception.js"),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"deno:op_crates/web/01_event.js",
|
|
|
|
include_str!("01_event.js"),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"deno:op_crates/web/02_abort_signal.js",
|
|
|
|
include_str!("02_abort_signal.js"),
|
|
|
|
),
|
2020-10-20 00:05:42 -04:00
|
|
|
(
|
|
|
|
"deno:op_crates/web/03_global_interfaces.js",
|
|
|
|
include_str!("03_global_interfaces.js"),
|
|
|
|
),
|
2020-09-30 10:51:01 -04:00
|
|
|
(
|
|
|
|
"deno:op_crates/web/08_text_encoding.js",
|
|
|
|
include_str!("08_text_encoding.js"),
|
|
|
|
),
|
|
|
|
("deno:op_crates/web/11_url.js", include_str!("11_url.js")),
|
2021-01-07 13:06:08 -05:00
|
|
|
(
|
|
|
|
"deno:op_crates/web/12_location.js",
|
|
|
|
include_str!("12_location.js"),
|
|
|
|
),
|
2020-09-30 10:51:01 -04:00
|
|
|
(
|
|
|
|
"deno:op_crates/web/21_filereader.js",
|
|
|
|
include_str!("21_filereader.js"),
|
|
|
|
),
|
2020-09-01 16:32:07 -04:00
|
|
|
];
|
2020-09-30 10:51:01 -04:00
|
|
|
for (url, source_code) in files {
|
|
|
|
isolate.execute(url, source_code).unwrap();
|
2020-09-01 16:32:07 -04:00
|
|
|
}
|
2020-08-07 10:55:02 -04:00
|
|
|
}
|
|
|
|
|
2020-09-01 16:32:07 -04:00
|
|
|
pub fn get_declaration() -> PathBuf {
|
2020-09-09 08:23:57 -04:00
|
|
|
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("lib.deno_web.d.ts")
|
2020-08-07 10:55:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-09-06 15:44:29 -04:00
|
|
|
use deno_core::JsRuntime;
|
2020-08-07 10:55:02 -04:00
|
|
|
use futures::future::lazy;
|
|
|
|
use futures::task::Context;
|
|
|
|
use futures::task::Poll;
|
|
|
|
|
|
|
|
fn run_in_task<F>(f: F)
|
|
|
|
where
|
|
|
|
F: FnOnce(&mut Context) + Send + 'static,
|
|
|
|
{
|
|
|
|
futures::executor::block_on(lazy(move |cx| f(cx)));
|
|
|
|
}
|
|
|
|
|
2020-09-06 15:44:29 -04:00
|
|
|
fn setup() -> JsRuntime {
|
2020-09-11 09:18:49 -04:00
|
|
|
let mut isolate = JsRuntime::new(Default::default());
|
2020-09-01 16:32:07 -04:00
|
|
|
crate::init(&mut isolate);
|
2020-08-07 10:55:02 -04:00
|
|
|
isolate
|
|
|
|
}
|
|
|
|
|
2020-08-19 08:43:20 -04:00
|
|
|
#[test]
|
|
|
|
fn test_abort_controller() {
|
|
|
|
run_in_task(|mut cx| {
|
|
|
|
let mut isolate = setup();
|
2020-09-22 17:30:03 -04:00
|
|
|
isolate
|
|
|
|
.execute(
|
|
|
|
"abort_controller_test.js",
|
|
|
|
include_str!("abort_controller_test.js"),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-10-11 07:20:40 -04:00
|
|
|
if let Poll::Ready(Err(_)) = isolate.poll_event_loop(&mut cx) {
|
2020-08-19 08:43:20 -04:00
|
|
|
unreachable!();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-07 10:55:02 -04:00
|
|
|
#[test]
|
|
|
|
fn test_event() {
|
|
|
|
run_in_task(|mut cx| {
|
|
|
|
let mut isolate = setup();
|
2020-09-22 17:30:03 -04:00
|
|
|
isolate
|
|
|
|
.execute("event_test.js", include_str!("event_test.js"))
|
|
|
|
.unwrap();
|
2020-10-11 07:20:40 -04:00
|
|
|
if let Poll::Ready(Err(_)) = isolate.poll_event_loop(&mut cx) {
|
2020-08-07 10:55:02 -04:00
|
|
|
unreachable!();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-09 08:23:57 -04:00
|
|
|
#[test]
|
|
|
|
fn test_event_error() {
|
|
|
|
run_in_task(|mut cx| {
|
|
|
|
let mut isolate = setup();
|
|
|
|
let result = isolate.execute("foo.js", "new Event()");
|
|
|
|
if let Err(error) = result {
|
|
|
|
let error_string = error.to_string();
|
|
|
|
// Test that the script specifier is a URL: `deno:<repo-relative path>`.
|
2020-09-22 17:30:03 -04:00
|
|
|
assert!(error_string.contains("deno:op_crates/web/01_event.js"));
|
|
|
|
assert!(error_string.contains("TypeError"));
|
2020-09-09 08:23:57 -04:00
|
|
|
} else {
|
|
|
|
unreachable!();
|
|
|
|
}
|
2020-10-11 07:20:40 -04:00
|
|
|
if let Poll::Ready(Err(_)) = isolate.poll_event_loop(&mut cx) {
|
2020-09-09 08:23:57 -04:00
|
|
|
unreachable!();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-07 10:55:02 -04:00
|
|
|
#[test]
|
|
|
|
fn test_event_target() {
|
|
|
|
run_in_task(|mut cx| {
|
|
|
|
let mut isolate = setup();
|
2020-09-22 17:30:03 -04:00
|
|
|
isolate
|
|
|
|
.execute("event_target_test.js", include_str!("event_target_test.js"))
|
|
|
|
.unwrap();
|
2020-10-11 07:20:40 -04:00
|
|
|
if let Poll::Ready(Err(_)) = isolate.poll_event_loop(&mut cx) {
|
2020-08-07 10:55:02 -04:00
|
|
|
unreachable!();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_text_encoding() {
|
|
|
|
run_in_task(|mut cx| {
|
|
|
|
let mut isolate = setup();
|
2020-09-22 17:30:03 -04:00
|
|
|
isolate
|
|
|
|
.execute(
|
|
|
|
"text_encoding_test.js",
|
|
|
|
include_str!("text_encoding_test.js"),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-10-11 07:20:40 -04:00
|
|
|
if let Poll::Ready(Err(_)) = isolate.poll_event_loop(&mut cx) {
|
2020-08-07 10:55:02 -04:00
|
|
|
unreachable!();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|