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
|
|
|
|
|
|
|
|
use crate::state::State;
|
2020-08-25 18:22:15 -04:00
|
|
|
use deno_core::ErrBox;
|
2020-09-05 20:34:02 -04:00
|
|
|
use deno_core::OpRegistry;
|
2020-07-10 15:51:24 -04:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2020-09-05 20:34:02 -04:00
|
|
|
use idna::domain_to_ascii;
|
|
|
|
use idna::domain_to_ascii_strict;
|
|
|
|
use serde_derive::Deserialize;
|
|
|
|
use serde_json::Value;
|
2020-08-18 12:30:13 -04:00
|
|
|
use std::rc::Rc;
|
2020-07-10 15:51:24 -04:00
|
|
|
|
2020-09-05 20:34:02 -04:00
|
|
|
pub fn init(s: &Rc<State>) {
|
|
|
|
s.register_op_json_sync("op_domain_to_ascii", 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,
|
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
|
|
|
}
|