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 super::dispatch_json::{Deserialize, JsonOp, Value};
|
2020-08-07 16:47:18 -04:00
|
|
|
use crate::op_error::OpError;
|
2020-07-10 15:51:24 -04:00
|
|
|
use crate::state::State;
|
|
|
|
use deno_core::CoreIsolate;
|
|
|
|
use deno_core::ZeroCopyBuf;
|
|
|
|
use idna::{domain_to_ascii, domain_to_ascii_strict};
|
|
|
|
|
|
|
|
pub fn init(i: &mut CoreIsolate, s: &State) {
|
|
|
|
i.register_op("op_domain_to_ascii", s.stateful_json_op(op_domain_to_ascii));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct DomainToAscii {
|
|
|
|
domain: String,
|
|
|
|
be_strict: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn op_domain_to_ascii(
|
|
|
|
_state: &State,
|
|
|
|
args: Value,
|
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
|
|
|
) -> Result<JsonOp, OpError> {
|
|
|
|
let args: DomainToAscii = serde_json::from_value(args)?;
|
|
|
|
let domain = if args.be_strict {
|
|
|
|
domain_to_ascii_strict(args.domain.as_str())
|
2020-08-07 16:47:18 -04:00
|
|
|
.map_err(|_| OpError::invalid_domain_error())?
|
2020-07-10 15:51:24 -04:00
|
|
|
} else {
|
2020-08-07 16:47:18 -04:00
|
|
|
domain_to_ascii(args.domain.as_str())
|
|
|
|
.map_err(|_| OpError::invalid_domain_error())?
|
2020-07-10 15:51:24 -04:00
|
|
|
};
|
|
|
|
Ok(JsonOp::Sync(json!(domain)))
|
|
|
|
}
|