refactor: Move URL to op_crates/web (#7544)

This commit is contained in:
Bartek Iwańczuk 2020-09-17 19:13:20 +02:00 committed by GitHub
parent a6f4559174
commit 6453cb7567
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 75 additions and 83 deletions

3
Cargo.lock generated
View File

@ -414,7 +414,6 @@ dependencies = [
"futures",
"fwdansi",
"http",
"idna",
"indexmap",
"jsonc-parser",
"lazy_static",
@ -506,6 +505,8 @@ version = "0.8.0"
dependencies = [
"deno_core",
"futures",
"idna",
"serde",
]
[[package]]

View File

@ -31,6 +31,7 @@ winapi = "0.3.9"
deno_core = { path = "../core", version = "0.57.0" }
deno_doc = "0.1.9"
deno_lint = { version = "0.2.0", features = ["json"] }
deno_web = { path = "../op_crates/web", version = "0.8.0" }
atty = "0.2.14"
base64 = "0.12.3"
@ -44,7 +45,6 @@ dprint-plugin-typescript = "0.31.3"
futures = "0.3.5" # TODO(ry) Remove and use deno_core::futures
filetime = "0.2.12"
http = "0.2.1"
idna = "0.2.0"
indexmap = "1.6.0"
jsonc-parser = "0.14.0"
lazy_static = "1.4.0"

View File

@ -1,40 +0,0 @@
// 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::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))
}

View File

@ -8,7 +8,6 @@ pub mod errors;
pub mod fetch;
pub mod fs;
pub mod fs_events;
pub mod idna;
pub mod io;
pub mod net;
#[cfg(unix)]

View File

@ -41,19 +41,6 @@
return Object.prototype.hasOwnProperty.call(obj, v);
}
/** Returns whether o is iterable. */
function isIterable(
o,
) {
// checks for null and undefined
if (o == null) {
return false;
}
return (
typeof (o)[Symbol.iterator] === "function"
);
}
const objectCloneMemo = new WeakMap();
function cloneArrayBuffer(
@ -192,7 +179,6 @@
requiredArguments,
immutableDefine,
hasOwnProperty,
isIterable,
cloneValue,
defineEnumerableProps,
getHeaderValueParams,

View File

@ -4,9 +4,6 @@
((window) => {
const core = window.Deno.core;
const { log } = window.__bootstrap.util;
/*
import { blobURLMap } from "./web/url.ts";
*/
function createWorker(
specifier,
@ -68,22 +65,6 @@
const hasSourceCode = false;
const sourceCode = decoder.decode(new Uint8Array());
/* TODO(bartlomieju):
// Handle blob URL.
if (specifier.startsWith("blob:")) {
hasSourceCode = true;
const b = blobURLMap.get(specifier);
if (!b) {
throw new Error("No Blob associated with the given URL is found");
}
const blobBytes = blobBytesWeakMap.get(b!);
if (!blobBytes) {
throw new Error("Invalid Blob");
}
sourceCode = blobBytes!;
}
*/
const useDenoNamespace = options ? !!options.deno : false;
const { id } = createWorker(

View File

@ -115,7 +115,11 @@ impl WebWorker {
let handle = web_worker.thread_safe_handle();
ops::web_worker::init(&mut web_worker.worker, sender, handle);
ops::worker_host::init(&mut web_worker.worker);
ops::idna::init(&mut web_worker.worker);
ops::reg_json_sync(
&mut web_worker.worker,
"op_domain_to_ascii",
deno_web::op_domain_to_ascii,
);
ops::io::init(&mut web_worker.worker);
ops::reg_json_sync(
&mut web_worker.worker,

View File

@ -277,7 +277,11 @@ impl MainWorker {
ops::websocket::init(&mut worker);
ops::fs::init(&mut worker);
ops::fs_events::init(&mut worker);
ops::idna::init(&mut worker);
ops::reg_json_sync(
&mut worker,
"op_domain_to_ascii",
deno_web::op_domain_to_ascii,
);
ops::io::init(&mut worker);
ops::plugin::init(&mut worker);
ops::net::init(&mut worker);

View File

@ -2,7 +2,31 @@
((window) => {
const core = window.Deno.core;
const { isIterable, requiredArguments } = window.__bootstrap.webUtil;
function requiredArguments(
name,
length,
required,
) {
if (length < required) {
const errMsg = `${name} requires at least ${required} argument${
required === 1 ? "" : "s"
}, but only ${length} present`;
throw new TypeError(errMsg);
}
}
function isIterable(
o,
) {
// checks for null and undefined
if (o == null) {
return false;
}
return (
typeof (o)[Symbol.iterator] === "function"
);
}
/** https://url.spec.whatwg.org/#idna */
function domainToAscii(
@ -383,9 +407,6 @@
return parts;
}
// Keep it outside of URL to avoid any attempts of access.
const blobURLMap = new Map();
// Resolves `.`s and `..`s where possible.
// Preserves repeating and trailing `/`s by design.
// Assumes drive letter file paths will have a leading slash.
@ -872,6 +893,5 @@
window.__bootstrap.url = {
URL,
URLSearchParams,
blobURLMap,
};
})(this);

View File

@ -15,6 +15,8 @@ path = "lib.rs"
[dependencies]
deno_core = { version = "0.57.0", path = "../../core" }
idna = "0.2.0"
serde = { version = "1.0.116", features = ["derive"] }
[dev-dependencies]
futures = "0.3.5"

View File

@ -1,9 +1,43 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use deno_core::error::uri_error;
use deno_core::error::AnyError;
use deno_core::js_check;
use deno_core::serde_json;
use deno_core::serde_json::json;
use deno_core::serde_json::Value;
use deno_core::JsRuntime;
use deno_core::ZeroCopyBuf;
use idna::domain_to_ascii;
use idna::domain_to_ascii_strict;
use serde::Deserialize;
use std::path::{Path, PathBuf};
pub fn op_domain_to_ascii(
_state: &mut deno_core::OpState,
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct DomainToAscii {
domain: String,
be_strict: bool,
}
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))
}
pub fn init(isolate: &mut JsRuntime) {
let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
let files = vec![
@ -11,6 +45,7 @@ pub fn init(isolate: &mut JsRuntime) {
manifest_dir.join("01_event.js"),
manifest_dir.join("02_abort_signal.js"),
manifest_dir.join("08_text_encoding.js"),
manifest_dir.join("11_url.js"),
];
// TODO(nayeemrmn): https://github.com/rust-lang/cargo/issues/3946 to get the
// workspace root.