Skip to content

Commit

Permalink
chore: split web op crate
Browse files Browse the repository at this point in the history
This commit starts splitting out the deno_web op crate into multiple
smaller crates. This commit splits out WebIDL and URL API, but in the
future I want to split out each spec into its own crate. That means we
will have (in rough order of loading): `webidl`, `dom`, `streams`,
`console`, `encoding`, `url`, `file`, `fetch`, `websocket`, and
`webgpu` crates.
  • Loading branch information
lucacasonato committed Mar 1, 2021
1 parent 62f33e3 commit b3c69a9
Show file tree
Hide file tree
Showing 30 changed files with 636 additions and 499 deletions.
20 changes: 18 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use deno_core::JsRuntime;
use deno_core::RuntimeOptions;
use deno_runtime::deno_crypto;
use deno_runtime::deno_fetch;
use deno_runtime::deno_url;
use deno_runtime::deno_web;
use deno_runtime::deno_webgpu;
use deno_runtime::deno_websocket;
Expand Down Expand Up @@ -61,6 +62,7 @@ fn create_compiler_snapshot(
) {
// libs that are being provided by op crates.
let mut op_crate_libs = HashMap::new();
op_crate_libs.insert("deno.url", deno_url::get_declaration());
op_crate_libs.insert("deno.web", deno_web::get_declaration());
op_crate_libs.insert("deno.fetch", deno_fetch::get_declaration());
op_crate_libs.insert("deno.webgpu", deno_webgpu::get_declaration());
Expand Down Expand Up @@ -254,6 +256,10 @@ fn main() {

println!("cargo:rustc-env=TS_VERSION={}", ts_version());
println!("cargo:rustc-env=GIT_COMMIT_HASH={}", git_commit_hash());
println!(
"cargo:rustc-env=DENO_URL_LIB_PATH={}",
deno_url::get_declaration().display()
);
println!(
"cargo:rustc-env=DENO_WEB_LIB_PATH={}",
deno_web::get_declaration().display()
Expand Down
3 changes: 2 additions & 1 deletion cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,9 @@ fn print_cache_info(

pub fn get_types(unstable: bool) -> String {
let mut types = format!(
"{}\n{}\n{}\n{}\n{}\n{}\n{}",
"{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}",
crate::tsc::DENO_NS_LIB,
crate::tsc::DENO_URL_LIB,
crate::tsc::DENO_WEB_LIB,
crate::tsc::DENO_FETCH_LIB,
crate::tsc::DENO_WEBGPU_LIB,
Expand Down
1 change: 1 addition & 0 deletions cli/tsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use std::sync::Mutex;
// Declaration files

pub static DENO_NS_LIB: &str = include_str!("dts/lib.deno.ns.d.ts");
pub static DENO_URL_LIB: &str = include_str!(env!("DENO_URL_LIB_PATH"));
pub static DENO_WEB_LIB: &str = include_str!(env!("DENO_WEB_LIB_PATH"));
pub static DENO_FETCH_LIB: &str = include_str!(env!("DENO_FETCH_LIB_PATH"));
pub static DENO_WEBGPU_LIB: &str = include_str!(env!("DENO_WEBGPU_LIB_PATH"));
Expand Down
2 changes: 1 addition & 1 deletion op_crates/crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
name = "deno_crypto"
version = "0.13.0"
edition = "2018"
description = "Collection of WebCrypto APIs"
description = "Web Cryptography API implementation for Deno"
authors = ["the Deno authors"]
license = "MIT"
readme = "README.md"
Expand Down
6 changes: 4 additions & 2 deletions op_crates/crypto/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# deno crypto
# deno_crypto

Op crate that implements crypto functions.
This crate implements the Web Cryptography API.

Spec: https://www.w3.org/TR/WebCryptoAPI/
2 changes: 1 addition & 1 deletion op_crates/fetch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
name = "deno_fetch"
version = "0.22.0"
edition = "2018"
description = "provides fetch Web API to deno_core"
description = "Fetch API implementation for Deno"
authors = ["the Deno authors"]
license = "MIT"
readme = "README.md"
Expand Down
6 changes: 5 additions & 1 deletion op_crates/fetch/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
This crate provides the web standard fetch API to `deno_core`.
# deno_fetch

This crate implements the Fetch API.

Spec: https://fetch.spec.whatwg.org/
9 changes: 3 additions & 6 deletions op_crates/web/11_url.js → op_crates/url/00_url.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,18 @@
domain,
{ beStrict = false } = {},
) {
return core.jsonOpSync("op_domain_to_ascii", { domain, beStrict });
return core.jsonOpSync("op_url_domain_to_ascii", { domain, beStrict });
}

function decodeSearchParam(p) {
const s = p.replaceAll("+", " ");
const decoder = new TextDecoder();

return s.replace(/(%[0-9a-f]{2})+/gi, (matched) => {
const buf = new Uint8Array(Math.ceil(matched.length / 3));
for (let i = 0, offset = 0; i < matched.length; i += 3, offset += 1) {
buf[offset] = parseInt(matched.slice(i + 1, i + 3), 16);
}
return decoder.decode(buf);
return core.decode(buf);
});
}

Expand Down Expand Up @@ -808,10 +807,8 @@
return charInUserinfoSet(c) || ["\u0021", "\u0024", "\u0025", "\u0026", "\u0027", "\u0028", "\u0029", "\u002B", "\u002C", "\u007E"].includes(c);
}

const encoder = new TextEncoder();

function encodeChar(c) {
return [...encoder.encode(c)]
return [...core.encode(c)]
.map((n) => `%${n.toString(16).padStart(2, "0")}`)
.join("")
.toUpperCase();
Expand Down
19 changes: 19 additions & 0 deletions op_crates/url/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

[package]
name = "deno_url"
version = "0.1.0"
edition = "2018"
description = "URL API implementation for Deno"
authors = ["the Deno authors"]
license = "MIT"
readme = "README.md"
repository = "https://github.com/denoland/deno"

[lib]
path = "lib.rs"

[dependencies]
deno_core = { version = "0.79.0", path = "../../core" }
idna = "0.2.1"
serde = { version = "1.0.123", features = ["derive"] }
5 changes: 5 additions & 0 deletions op_crates/url/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# deno_url

This crate implements the URL API for Deno.

Spec: https://url.spec.whatwg.org/
14 changes: 14 additions & 0 deletions op_crates/url/internal.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
// deno-lint-ignore-file no-explicit-any ban-types

/// <reference no-default-lib="true" />
/// <reference lib="esnext" />

declare namespace globalThis {
declare namespace __bootstrap {
declare var url: {
URL: typeof URL;
URLSearchParams: typeof URLSearchParams;
};
}
}
175 changes: 175 additions & 0 deletions op_crates/url/lib.deno_url.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.

// deno-lint-ignore-file no-explicit-any

/// <reference no-default-lib="true" />
/// <reference lib="esnext" />

declare class URLSearchParams {
constructor(
init?: string[][] | Record<string, string> | string | URLSearchParams,
);
static toString(): string;

/** Appends a specified key/value pair as a new search parameter.
*
* ```ts
* let searchParams = new URLSearchParams();
* searchParams.append('name', 'first');
* searchParams.append('name', 'second');
* ```
*/
append(name: string, value: string): void;

/** Deletes the given search parameter and its associated value,
* from the list of all search parameters.
*
* ```ts
* let searchParams = new URLSearchParams([['name', 'value']]);
* searchParams.delete('name');
* ```
*/
delete(name: string): void;

/** Returns all the values associated with a given search parameter
* as an array.
*
* ```ts
* searchParams.getAll('name');
* ```
*/
getAll(name: string): string[];

/** Returns the first value associated to the given search parameter.
*
* ```ts
* searchParams.get('name');
* ```
*/
get(name: string): string | null;

/** Returns a Boolean that indicates whether a parameter with the
* specified name exists.
*
* ```ts
* searchParams.has('name');
* ```
*/
has(name: string): boolean;

/** Sets the value associated with a given search parameter to the
* given value. If there were several matching values, this method
* deletes the others. If the search parameter doesn't exist, this
* method creates it.
*
* ```ts
* searchParams.set('name', 'value');
* ```
*/
set(name: string, value: string): void;

/** Sort all key/value pairs contained in this object in place and
* return undefined. The sort order is according to Unicode code
* points of the keys.
*
* ```ts
* searchParams.sort();
* ```
*/
sort(): void;

/** Calls a function for each element contained in this object in
* place and return undefined. Optionally accepts an object to use
* as this when executing callback as second argument.
*
* ```ts
* const params = new URLSearchParams([["a", "b"], ["c", "d"]]);
* params.forEach((value, key, parent) => {
* console.log(value, key, parent);
* });
* ```
*
*/
forEach(
callbackfn: (value: string, key: string, parent: this) => void,
thisArg?: any,
): void;

/** Returns an iterator allowing to go through all keys contained
* in this object.
*
* ```ts
* const params = new URLSearchParams([["a", "b"], ["c", "d"]]);
* for (const key of params.keys()) {
* console.log(key);
* }
* ```
*/
keys(): IterableIterator<string>;

/** Returns an iterator allowing to go through all values contained
* in this object.
*
* ```ts
* const params = new URLSearchParams([["a", "b"], ["c", "d"]]);
* for (const value of params.values()) {
* console.log(value);
* }
* ```
*/
values(): IterableIterator<string>;

/** Returns an iterator allowing to go through all key/value
* pairs contained in this object.
*
* ```ts
* const params = new URLSearchParams([["a", "b"], ["c", "d"]]);
* for (const [key, value] of params.entries()) {
* console.log(key, value);
* }
* ```
*/
entries(): IterableIterator<[string, string]>;

/** Returns an iterator allowing to go through all key/value
* pairs contained in this object.
*
* ```ts
* const params = new URLSearchParams([["a", "b"], ["c", "d"]]);
* for (const [key, value] of params) {
* console.log(key, value);
* }
* ```
*/
[Symbol.iterator](): IterableIterator<[string, string]>;

/** Returns a query string suitable for use in a URL.
*
* ```ts
* searchParams.toString();
* ```
*/
toString(): string;
}

/** The URL interface represents an object providing static methods used for creating object URLs. */
declare class URL {
constructor(url: string, base?: string | URL);
createObjectURL(object: any): string;
revokeObjectURL(url: string): void;

hash: string;
host: string;
hostname: string;
href: string;
toString(): string;
readonly origin: string;
password: string;
pathname: string;
port: string;
protocol: string;
search: string;
readonly searchParams: URLSearchParams;
username: string;
toJSON(): string;
}
Loading

0 comments on commit b3c69a9

Please sign in to comment.