2020-07-10 20:51:24 +01:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
//! https://url.spec.whatwg.org/#idna
|
|
|
|
|
|
|
|
use crate::state::State;
|
2020-08-26 00:22:15 +02:00
|
|
|
use deno_core::ErrBox;
|
2020-09-06 02:34:02 +02:00
|
|
|
use deno_core::OpRegistry;
|
2020-07-10 20:51:24 +01:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2020-09-06 02:34:02 +02:00
|
|
|
use idna::domain_to_ascii;
|
|
|
|
use idna::domain_to_ascii_strict;
|
|
|
|
use serde_derive::Deserialize;
|
|
|
|
use serde_json::Value;
|
2020-08-18 18:30:13 +02:00
|
|
|
use std::rc::Rc;
|
2020-07-10 20:51:24 +01:00
|
|
|
|
2020-09-06 02:34:02 +02:00
|
|
|
pub fn init(s: &Rc<State>) {
|
|
|
|
s.register_op_json_sync("op_domain_to_ascii", op_domain_to_ascii);
|
2020-07-10 20:51:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct DomainToAscii {
|
|
|
|
domain: String,
|
|
|
|
be_strict: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn op_domain_to_ascii(
|
2020-08-28 17:08:24 +02:00
|
|
|
_state: &State,
|
2020-07-10 20:51:24 +01:00
|
|
|
args: Value,
|
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-08-28 17:08:24 +02:00
|
|
|
) -> Result<Value, ErrBox> {
|
2020-07-10 20:51:24 +01:00
|
|
|
let args: DomainToAscii = serde_json::from_value(args)?;
|
2020-08-26 00:22:15 +02:00
|
|
|
if args.be_strict {
|
2020-07-10 20:51:24 +01:00
|
|
|
domain_to_ascii_strict(args.domain.as_str())
|
|
|
|
} else {
|
2020-08-07 22:47:18 +02:00
|
|
|
domain_to_ascii(args.domain.as_str())
|
2020-08-26 00:22:15 +02:00
|
|
|
}
|
|
|
|
.map_err(|err| {
|
|
|
|
let message = format!("Invalid IDNA encoded domain name: {:?}", err);
|
|
|
|
ErrBox::new("URIError", message)
|
|
|
|
})
|
2020-08-28 17:08:24 +02:00
|
|
|
.map(|domain| json!(domain))
|
2020-07-10 20:51:24 +01:00
|
|
|
}
|