2020-07-10 15:51:24 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
//! https://url.spec.whatwg.org/#idna
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
use super::dispatch_json::{Deserialize, Value};
|
2020-07-10 15:51:24 -04:00
|
|
|
use crate::state::State;
|
|
|
|
use deno_core::CoreIsolate;
|
2020-08-25 18:22:15 -04:00
|
|
|
use deno_core::ErrBox;
|
2020-08-28 11:08:24 -04:00
|
|
|
use deno_core::ResourceTable;
|
2020-07-10 15:51:24 -04:00
|
|
|
use deno_core::ZeroCopyBuf;
|
|
|
|
use idna::{domain_to_ascii, domain_to_ascii_strict};
|
2020-08-18 12:30:13 -04:00
|
|
|
use std::rc::Rc;
|
2020-07-10 15:51:24 -04:00
|
|
|
|
2020-08-18 12:30:13 -04:00
|
|
|
pub fn init(i: &mut CoreIsolate, s: &Rc<State>) {
|
2020-08-28 11:08:24 -04:00
|
|
|
let t = &CoreIsolate::state(i).borrow().resource_table.clone();
|
|
|
|
i.register_op(
|
|
|
|
"op_domain_to_ascii",
|
|
|
|
s.stateful_json_op_sync(t, op_domain_to_ascii),
|
|
|
|
);
|
2020-07-10 15:51:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct DomainToAscii {
|
|
|
|
domain: String,
|
|
|
|
be_strict: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn op_domain_to_ascii(
|
2020-08-28 11:08:24 -04:00
|
|
|
_state: &State,
|
|
|
|
_resource_table: &mut ResourceTable,
|
2020-07-10 15:51:24 -04:00
|
|
|
args: Value,
|
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-08-28 11:08:24 -04:00
|
|
|
) -> Result<Value, ErrBox> {
|
2020-07-10 15:51:24 -04:00
|
|
|
let args: DomainToAscii = serde_json::from_value(args)?;
|
2020-08-25 18:22:15 -04:00
|
|
|
if args.be_strict {
|
2020-07-10 15:51:24 -04:00
|
|
|
domain_to_ascii_strict(args.domain.as_str())
|
|
|
|
} else {
|
2020-08-07 16:47:18 -04:00
|
|
|
domain_to_ascii(args.domain.as_str())
|
2020-08-25 18:22:15 -04:00
|
|
|
}
|
|
|
|
.map_err(|err| {
|
|
|
|
let message = format!("Invalid IDNA encoded domain name: {:?}", err);
|
|
|
|
ErrBox::new("URIError", message)
|
|
|
|
})
|
2020-08-28 11:08:24 -04:00
|
|
|
.map(|domain| json!(domain))
|
2020-07-10 15:51:24 -04:00
|
|
|
}
|