blob: 60812015f6bc71769ce1a1607f22d63f271ea213 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
//! https://url.spec.whatwg.org/#idna
use deno_core::error::uri_error;
use deno_core::error::AnyError;
use deno_core::ZeroCopyBuf;
use idna::domain_to_ascii;
use idna::domain_to_ascii_strict;
use serde_derive::Deserialize;
use serde_json::Value;
pub fn init(rt: &mut deno_core::JsRuntime) {
super::reg_json_sync(rt, "op_domain_to_ascii", op_domain_to_ascii);
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct DomainToAscii {
domain: String,
be_strict: bool,
}
fn op_domain_to_ascii(
_state: &mut deno_core::OpState,
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
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))
}
|