1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 23:34:47 -05:00
denoland-deno/cli/ops/idna.rs
Ryan Dahl 7c2e7c6608
Use gotham-like state for ops (#7385)
Provides a concrete state type that can be dynamically added. This is necessary for op crates.
* renames BasicState to OpState
* async ops take `Rc<RefCell<OpState>>`
* sync ops take `&mut OpState`
* removes `OpRegistry`, `OpRouter` traits
* `get_error_class_fn` moved to OpState
* ResourceTable moved to OpState
2020-09-10 09:57:45 -04:00

39 lines
1,012 B
Rust

// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
//! https://url.spec.whatwg.org/#idna
use deno_core::ErrBox;
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, ErrBox> {
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);
ErrBox::new("URIError", message)
})
.map(|domain| json!(domain))
}