diff options
| author | Ryan Dahl <ry@tinyclouds.org> | 2021-08-11 12:27:05 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-08-11 12:27:05 +0200 |
| commit | a0285e2eb88f6254f6494b0ecd1878db3a3b2a58 (patch) | |
| tree | 90671b004537e20f9493fd3277ffd21d30b39a0e /ext/url | |
| parent | 3a6994115176781b3a93d70794b1b81bc95e42b4 (diff) | |
Rename extensions/ directory to ext/ (#11643)
Diffstat (limited to 'ext/url')
| -rw-r--r-- | ext/url/00_url.js | 623 | ||||
| -rw-r--r-- | ext/url/Cargo.toml | 28 | ||||
| -rw-r--r-- | ext/url/README.md | 5 | ||||
| -rw-r--r-- | ext/url/benches/url_ops.rs | 27 | ||||
| -rw-r--r-- | ext/url/internal.d.ts | 14 | ||||
| -rw-r--r-- | ext/url/lib.deno_url.d.ts | 175 | ||||
| -rw-r--r-- | ext/url/lib.rs | 173 |
7 files changed, 1045 insertions, 0 deletions
diff --git a/ext/url/00_url.js b/ext/url/00_url.js new file mode 100644 index 000000000..f3c12d0c2 --- /dev/null +++ b/ext/url/00_url.js @@ -0,0 +1,623 @@ +// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. + +// @ts-check +/// <reference path="../../core/internal.d.ts" /> +/// <reference path="../../core/lib.deno_core.d.ts" /> +/// <reference path="../webidl/internal.d.ts" /> + +"use strict"; + +((window) => { + const core = window.Deno.core; + const webidl = window.__bootstrap.webidl; + const { + ArrayIsArray, + ArrayPrototypeMap, + ArrayPrototypePush, + ArrayPrototypeSome, + ArrayPrototypeSort, + ArrayPrototypeSplice, + ObjectKeys, + StringPrototypeSlice, + Symbol, + SymbolFor, + SymbolIterator, + SymbolToStringTag, + TypeError, + } = window.__bootstrap.primordials; + + const _list = Symbol("list"); + const _urlObject = Symbol("url object"); + + class URLSearchParams { + [_list]; + [_urlObject] = null; + + /** + * @param {string | [string][] | Record<string, string>} init + */ + constructor(init = "") { + const prefix = "Failed to construct 'URL'"; + init = webidl.converters + ["sequence<sequence<USVString>> or record<USVString, USVString> or USVString"]( + init, + { prefix, context: "Argument 1" }, + ); + this[webidl.brand] = webidl.brand; + + if (typeof init === "string") { + // Overload: USVString + // If init is a string and starts with U+003F (?), + // remove the first code point from init. + if (init[0] == "?") { + init = StringPrototypeSlice(init, 1); + } + this[_list] = core.opSync("op_url_parse_search_params", init); + } else if (ArrayIsArray(init)) { + // Overload: sequence<sequence<USVString>> + this[_list] = ArrayPrototypeMap(init, (pair, i) => { + if (pair.length !== 2) { + throw new TypeError( + `${prefix}: Item ${i + + 0} in the parameter list does have length 2 exactly.`, + ); + } + return [pair[0], pair[1]]; + }); + } else { + // Overload: record<USVString, USVString> + this[_list] = ArrayPrototypeMap( + ObjectKeys(init), + (key) => [key, init[key]], + ); + } + } + + #updateUrlSearch() { + const url = this[_urlObject]; + if (url === null) { + return; + } + const parts = core.opSync("op_url_parse", { + href: url.href, + setSearch: this.toString(), + }); + url[_url] = parts; + } + + /** + * @param {string} name + * @param {string} value + */ + append(name, value) { + webidl.assertBranded(this, URLSearchParams); + const prefix = "Failed to execute 'append' on 'URLSearchParams'"; + webidl.requiredArguments(arguments.length, 2, { prefix }); + name = webidl.converters.USVString(name, { + prefix, + context: "Argument 1", + }); + value = webidl.converters.USVString(value, { + prefix, + context: "Argument 2", + }); + ArrayPrototypePush(this[_list], [name, value]); + this.#updateUrlSearch(); + } + + /** + * @param {string} name + */ + delete(name) { + webidl.assertBranded(this, URLSearchParams); + const prefix = "Failed to execute 'append' on 'URLSearchParams'"; + webidl.requiredArguments(arguments.length, 1, { prefix }); + name = webidl.converters.USVString(name, { + prefix, + context: "Argument 1", + }); + const list = this[_list]; + let i = 0; + while (i < list.length) { + if (list[i][0] === name) { + ArrayPrototypeSplice(list, i, 1); + } else { + i++; + } + } + this.#updateUrlSearch(); + } + + /** + * @param {string} name + * @returns {string[]} + */ + getAll(name) { + webidl.assertBranded(this, URLSearchParams); + const prefix = "Failed to execute 'getAll' on 'URLSearchParams'"; + webidl.requiredArguments(arguments.length, 1, { prefix }); + name = webidl.converters.USVString(name, { + prefix, + context: "Argument 1", + }); + const values = []; + for (const entry of this[_list]) { + if (entry[0] === name) { + ArrayPrototypePush(values, entry[1]); + } + } + return values; + } + + /** + * @param {string} name + * @return {string | null} + */ + get(name) { + webidl.assertBranded(this, URLSearchParams); + const prefix = "Failed to execute 'get' on 'URLSearchParams'"; + webidl.requiredArguments(arguments.length, 1, { prefix }); + name = webidl.converters.USVString(name, { + prefix, + context: "Argument 1", + }); + for (const entry of this[_list]) { + if (entry[0] === name) { + return entry[1]; + } + } + return null; + } + + /** + * @param {string} name + * @return {boolean} + */ + has(name) { + webidl.assertBranded(this, URLSearchParams); + const prefix = "Failed to execute 'has' on 'URLSearchParams'"; + webidl.requiredArguments(arguments.length, 1, { prefix }); + name = webidl.converters.USVString(name, { + prefix, + context: "Argument 1", + }); + return ArrayPrototypeSome(this[_list], (entry) => entry[0] === name); + } + + /** + * @param {string} name + * @param {string} value + */ + set(name, value) { + webidl.assertBranded(this, URLSearchParams); + const prefix = "Failed to execute 'set' on 'URLSearchParams'"; + webidl.requiredArguments(arguments.length, 2, { prefix }); + name = webidl.converters.USVString(name, { + prefix, + context: "Argument 1", + }); + value = webidl.converters.USVString(value, { + prefix, + context: "Argument 2", + }); + + const list = this[_list]; + + // If there are any name-value pairs whose name is name, in list, + // set the value of the first such name-value pair to value + // and remove the others. + let found = false; + let i = 0; + while (i < list.length) { + if (list[i][0] === name) { + if (!found) { + list[i][1] = value; + found = true; + i++; + } else { + ArrayPrototypeSplice(list, i, 1); + } + } else { + i++; + } + } + + // Otherwise, append a new name-value pair whose name is name + // and value is value, to list. + if (!found) { + ArrayPrototypePush(list, [name, value]); + } + + this.#updateUrlSearch(); + } + + sort() { + webidl.assertBranded(this, URLSearchParams); + ArrayPrototypeSort( + this[_list], + (a, b) => (a[0] === b[0] ? 0 : a[0] > b[0] ? 1 : -1), + ); + this.#updateUrlSearch(); + } + + /** + * @return {string} + */ + toString() { + webidl.assertBranded(this, URLSearchParams); + return core.opSync("op_url_stringify_search_params", this[_list]); + } + + get [SymbolToStringTag]() { + return "URLSearchParams"; + } + } + + webidl.mixinPairIterable("URLSearchParams", URLSearchParams, _list, 0, 1); + + webidl.configurePrototype(URLSearchParams); + + const _url = Symbol("url"); + + class URL { + [_url]; + #queryObject = null; + + /** + * @param {string} url + * @param {string} base + */ + constructor(url, base = undefined) { + const prefix = "Failed to construct 'URL'"; + url = webidl.converters.USVString(url, { prefix, context: "Argument 1" }); + if (base !== undefined) { + base = webidl.converters.USVString(base, { + prefix, + context: "Argument 2", + }); + } + this[webidl.brand] = webidl.brand; + + const parts = core.opSync("op_url_parse", { href: url, baseHref: base }); + this[_url] = parts; + } + + [SymbolFor("Deno.privateCustomInspect")](inspect) { + const object = { + href: this.href, + origin: this.origin, + protocol: this.protocol, + username: this.username, + password: this.password, + host: this.host, + hostname: this.hostname, + port: this.port, + pathname: this.pathname, + hash: this.hash, + search: this.search, + }; + return `${this.constructor.name} ${inspect(object)}`; + } + + #updateSearchParams() { + if (this.#queryObject !== null) { + const params = this.#queryObject[_list]; + const newParams = core.opSync( + "op_url_parse_search_params", + StringPrototypeSlice(this.search, 1), + ); + ArrayPrototypeSplice(params, 0, params.length, ...newParams); + } + } + + /** @return {string} */ + get hash() { + webidl.assertBranded(this, URL); + return this[_url].hash; + } + + /** @param {string} value */ + set hash(value) { + webidl.assertBranded(this, URL); + const prefix = "Failed to set 'hash' on 'URL'"; + webidl.requiredArguments(arguments.length, 1, { prefix }); + value = webidl.converters.USVString(value, { + prefix, + context: "Argument 1", + }); + try { + this[_url] = core.opSync("op_url_parse", { + href: this[_url].href, + setHash: value, + }); + } catch { + /* pass */ + } + } + + /** @return {string} */ + get host() { + webidl.assertBranded(this, URL); + return this[_url].host; + } + + /** @param {string} value */ + set host(value) { + webidl.assertBranded(this, URL); + const prefix = "Failed to set 'host' on 'URL'"; + webidl.requiredArguments(arguments.length, 1, { prefix }); + value = webidl.converters.USVString(value, { + prefix, + context: "Argument 1", + }); + try { + this[_url] = core.opSync("op_url_parse", { + href: this[_url].href, + setHost: value, + }); + } catch { + /* pass */ + } + } + + /** @return {string} */ + get hostname() { + webidl.assertBranded(this, URL); + return this[_url].hostname; + } + + /** @param {string} value */ + set hostname(value) { + webidl.assertBranded(this, URL); + const prefix = "Failed to set 'hostname' on 'URL'"; + webidl.requiredArguments(arguments.length, 1, { prefix }); + value = webidl.converters.USVString(value, { + prefix, + context: "Argument 1", + }); + try { + this[_url] = core.opSync("op_url_parse", { + href: this[_url].href, + setHostname: value, + }); + } catch { + /* pass */ + } + } + + /** @return {string} */ + get href() { + webidl.assertBranded(this, URL); + return this[_url].href; + } + + /** @param {string} value */ + set href(value) { + webidl.assertBranded(this, URL); + const prefix = "Failed to set 'href' on 'URL'"; + webidl.requiredArguments(arguments.length, 1, { prefix }); + value = webidl.converters.USVString(value, { + prefix, + context: "Argument 1", + }); + this[_url] = core.opSync("op_url_parse", { + href: value, + }); + this.#updateSearchParams(); + } + + /** @return {string} */ + get origin() { + webidl.assertBranded(this, URL); + return this[_url].origin; + } + + /** @return {string} */ + get password() { + webidl.assertBranded(this, URL); + return this[_url].password; + } + + /** @param {string} value */ + set password(value) { + webidl.assertBranded(this, URL); + const prefix = "Failed to set 'password' on 'URL'"; + webidl.requiredArguments(arguments.length, 1, { prefix }); + value = webidl.converters.USVString(value, { + prefix, + context: "Argument 1", + }); + try { + this[_url] = core.opSync("op_url_parse", { + href: this[_url].href, + setPassword: value, + }); + } catch { + /* pass */ + } + } + + /** @return {string} */ + get pathname() { + webidl.assertBranded(this, URL); + return this[_url].pathname; + } + + /** @param {string} value */ + set pathname(value) { + webidl.assertBranded(this, URL); + const prefix = "Failed to set 'pathname' on 'URL'"; + webidl.requiredArguments(arguments.length, 1, { prefix }); + value = webidl.converters.USVString(value, { + prefix, + context: "Argument 1", + }); + try { + this[_url] = core.opSync("op_url_parse", { + href: this[_url].href, + setPathname: value, + }); + } catch { + /* pass */ + } + } + + /** @return {string} */ + get port() { + webidl.assertBranded(this, URL); + return this[_url].port; + } + + /** @param {string} value */ + set port(value) { + webidl.assertBranded(this, URL); + const prefix = "Failed to set 'port' on 'URL'"; + webidl.requiredArguments(arguments.length, 1, { prefix }); + value = webidl.converters.USVString(value, { + prefix, + context: "Argument 1", + }); + try { + this[_url] = core.opSync("op_url_parse", { + href: this[_url].href, + setPort: value, + }); + } catch { + /* pass */ + } + } + + /** @return {string} */ + get protocol() { + webidl.assertBranded(this, URL); + return this[_url].protocol; + } + + /** @param {string} value */ + set protocol(value) { + webidl.assertBranded(this, URL); + const prefix = "Failed to set 'protocol' on 'URL'"; + webidl.requiredArguments(arguments.length, 1, { prefix }); + value = webidl.converters.USVString(value, { + prefix, + context: "Argument 1", + }); + try { + this[_url] = core.opSync("op_url_parse", { + href: this[_url].href, + setProtocol: value, + }); + } catch { + /* pass */ + } + } + + /** @return {string} */ + get search() { + webidl.assertBranded(this, URL); + return this[_url].search; + } + + /** @param {string} value */ + set search(value) { + webidl.assertBranded(this, URL); + const prefix = "Failed to set 'search' on 'URL'"; + webidl.requiredArguments(arguments.length, 1, { prefix }); + value = webidl.converters.USVString(value, { + prefix, + context: "Argument 1", + }); + try { + this[_url] = core.opSync("op_url_parse", { + href: this[_url].href, + setSearch: value, + }); + this.#updateSearchParams(); + } catch { + /* pass */ + } + } + + /** @return {string} */ + get username() { + webidl.assertBranded(this, URL); + return this[_url].username; + } + + /** @param {string} value */ + set username(value) { + webidl.assertBranded(this, URL); + const prefix = "Failed to set 'username' on 'URL'"; + webidl.requiredArguments(arguments.length, 1, { prefix }); + value = webidl.converters.USVString(value, { + prefix, + context: "Argument 1", + }); + try { + this[_url] = core.opSync("op_url_parse", { + href: this[_url].href, + setUsername: value, + }); + } catch { + /* pass */ + } + } + + /** @return {string} */ + get searchParams() { + if (this.#queryObject == null) { + this.#queryObject = new URLSearchParams(this.search); + this.#queryObject[_urlObject] = this; + } + return this.#queryObject; + } + + /** @return {string} */ + toString() { + webidl.assertBranded(this, URL); + return this[_url].href; + } + + /** @return {string} */ + toJSON() { + webidl.assertBranded(this, URL); + return this[_url].href; + } + + get [SymbolToStringTag]() { + return "URL"; + } + } + + webidl.configurePrototype(URL); + + /** + * This function implements application/x-www-form-urlencoded parsing. + * https://url.spec.whatwg.org/#concept-urlencoded-parser + * @param {Uint8Array} bytes + * @returns {[string, string][]} + */ + function parseUrlEncoded(bytes) { + return core.opSync("op_url_parse_search_params", null, bytes); + } + + webidl + .converters[ + "sequence<sequence<USVString>> or record<USVString, USVString> or USVString" + ] = (V, opts) => { + // Union for (sequence<sequence<USVString>> or record<USVString, USVString> or USVString) + if (webidl.type(V) === "Object" && V !== null) { + if (V[SymbolIterator] !== undefined) { + return webidl.converters["sequence<sequence<USVString>>"](V, opts); + } + return webidl.converters["record<USVString, USVString>"](V, opts); + } + return webidl.converters.USVString(V, opts); + }; + + window.__bootstrap.url = { + URL, + URLSearchParams, + parseUrlEncoded, + }; +})(this); diff --git a/ext/url/Cargo.toml b/ext/url/Cargo.toml new file mode 100644 index 000000000..a76dac2e6 --- /dev/null +++ b/ext/url/Cargo.toml @@ -0,0 +1,28 @@ +# Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. + +[package] +name = "deno_url" +version = "0.14.0" +authors = ["the Deno authors"] +edition = "2018" +license = "MIT" +readme = "README.md" +repository = "https://github.com/denoland/deno" +description = "URL API implementation for Deno" + +[lib] +path = "lib.rs" + +[dependencies] +deno_core = { version = "0.96.0", path = "../../core" } +idna = "0.2.3" +percent-encoding = "2.1.0" +serde = { version = "1.0.126", features = ["derive"] } + +[dev-dependencies] +deno_bench_util = { version = "0.8.0", path = "../../bench_util" } +deno_webidl = { version = "0.14.0", path = "../webidl" } + +[[bench]] +name = "url_ops" +harness = false diff --git a/ext/url/README.md b/ext/url/README.md new file mode 100644 index 000000000..991dd8b20 --- /dev/null +++ b/ext/url/README.md @@ -0,0 +1,5 @@ +# deno_url + +This crate implements the URL API for Deno. + +Spec: https://url.spec.whatwg.org/ diff --git a/ext/url/benches/url_ops.rs b/ext/url/benches/url_ops.rs new file mode 100644 index 000000000..ed27b6f80 --- /dev/null +++ b/ext/url/benches/url_ops.rs @@ -0,0 +1,27 @@ +use deno_bench_util::bench_js_sync; +use deno_bench_util::bench_or_profile; +use deno_bench_util::bencher::{benchmark_group, Bencher}; + +use deno_core::Extension; + +fn setup() -> Vec<Extension> { + vec![ + deno_webidl::init(), + deno_url::init(), + Extension::builder() + .js(vec![( + "setup", + Box::new(|| { + Ok(r#"const { URL } = globalThis.__bootstrap.url;"#.to_owned()) + }), + )]) + .build(), + ] +} + +fn bench_url_parse(b: &mut Bencher) { + bench_js_sync(b, r#"new URL(`http://www.google.com/`);"#, setup); +} + +benchmark_group!(benches, bench_url_parse,); +bench_or_profile!(benches); diff --git a/ext/url/internal.d.ts b/ext/url/internal.d.ts new file mode 100644 index 000000000..ec2c2688c --- /dev/null +++ b/ext/url/internal.d.ts @@ -0,0 +1,14 @@ +// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. + +/// <reference no-default-lib="true" /> +/// <reference lib="esnext" /> + +declare namespace globalThis { + declare namespace __bootstrap { + declare var url: { + URL: typeof URL; + URLSearchParams: typeof URLSearchParams; + parseUrlEncoded(bytes: Uint8Array): [string, string][]; + }; + } +} diff --git a/ext/url/lib.deno_url.d.ts b/ext/url/lib.deno_url.d.ts new file mode 100644 index 000000000..3f9745352 --- /dev/null +++ b/ext/url/lib.deno_url.d.ts @@ -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); + static createObjectURL(blob: Blob): string; + static 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; +} diff --git a/ext/url/lib.rs b/ext/url/lib.rs new file mode 100644 index 000000000..8ccc59eb8 --- /dev/null +++ b/ext/url/lib.rs @@ -0,0 +1,173 @@ +// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. + +use deno_core::error::generic_error; +use deno_core::error::type_error; +use deno_core::error::uri_error; +use deno_core::error::AnyError; +use deno_core::include_js_files; +use deno_core::op_sync; +use deno_core::url::form_urlencoded; +use deno_core::url::quirks; +use deno_core::url::Url; +use deno_core::Extension; +use deno_core::ZeroCopyBuf; +use serde::Deserialize; +use serde::Serialize; +use std::panic::catch_unwind; +use std::path::PathBuf; + +pub fn init() -> Extension { + Extension::builder() + .js(include_js_files!( + prefix "deno:ext/url", + "00_url.js", + )) + .ops(vec![ + ("op_url_parse", op_sync(op_url_parse)), + ( + "op_url_parse_search_params", + op_sync(op_url_parse_search_params), + ), + ( + "op_url_stringify_search_params", + op_sync(op_url_stringify_search_params), + ), + ]) + .build() +} + +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct UrlParseArgs { + href: String, + base_href: Option<String>, + // If one of the following are present, this is a setter call. Apply the + // proper `Url::set_*()` method after (re)parsing `href`. + set_hash: Option<String>, + set_host: Option<String>, + set_hostname: Option<String>, + set_password: Option<String>, + set_pathname: Option<String>, + set_port: Option<String>, + set_protocol: Option<String>, + set_search: Option<String>, + set_username: Option<String>, +} + +#[derive(Serialize)] +pub struct UrlParts { + href: String, + hash: String, + host: String, + hostname: String, + origin: String, + password: String, + pathname: String, + port: String, + protocol: String, + search: String, + username: String, +} + +/// Parse `UrlParseArgs::href` with an optional `UrlParseArgs::base_href`, or an +/// optional part to "set" after parsing. Return `UrlParts`. +pub fn op_url_parse( + _state: &mut deno_core::OpState, + args: UrlParseArgs, + _: (), +) -> Result<UrlParts, AnyError> { + let base_url = args + .base_href + .as_ref() + .map(|b| Url::parse(b).map_err(|_| type_error("Invalid base URL"))) + .transpose()?; + let mut url = Url::options() + .base_url(base_url.as_ref()) + .parse(&args.href) + .map_err(|_| type_error("Invalid URL"))?; + + if let Some(hash) = args.set_hash.as_ref() { + quirks::set_hash(&mut url, hash); + } else if let Some(host) = args.set_host.as_ref() { + quirks::set_host(&mut url, host).map_err(|_| uri_error("Invalid host"))?; + } else if let Some(hostname) = args.set_hostname.as_ref() { + quirks::set_hostname(&mut url, hostname) + .map_err(|_| uri_error("Invalid hostname"))?; + } else if let Some(password) = args.set_password.as_ref() { + quirks::set_password(&mut url, password) + .map_err(|_| uri_error("Invalid password"))?; + } else if let Some(pathname) = args.set_pathname.as_ref() { + quirks::set_pathname(&mut url, pathname); + } else if let Some(port) = args.set_port.as_ref() { + quirks::set_port(&mut url, port).map_err(|_| uri_error("Invalid port"))?; + } else if let Some(protocol) = args.set_protocol.as_ref() { + quirks::set_protocol(&mut url, protocol) + .map_err(|_| uri_error("Invalid protocol"))?; + } else if let Some(search) = args.set_search.as_ref() { + quirks::set_search(&mut url, search); + } else if let Some(username) = args.set_username.as_ref() { + quirks::set_username(&mut url, username) + .map_err(|_| uri_error("Invalid username"))?; + } + + // TODO(nayeemrmn): Panic that occurs in rust-url for the `non-spec:` + // url-constructor wpt tests: https://github.com/servo/rust-url/issues/670. + let username = catch_unwind(|| quirks::username(&url)).map_err(|_| { + generic_error(format!( + "Internal error while parsing \"{}\"{}, \ + see https://github.com/servo/rust-url/issues/670", + args.href, + args + .base_href + .map(|b| format!(" against \"{}\"", b)) + .unwrap_or_default() + )) + })?; + Ok(UrlParts { + href: quirks::href(&url).to_string(), + hash: quirks::hash(&url).to_string(), + host: quirks::host(&url).to_string(), + hostname: quirks::hostname(&url).to_string(), + origin: quirks::origin(&url), + password: quirks::password(&url).to_string(), + pathname: quirks::pathname(&url).to_string(), + port: quirks::port(&url).to_string(), + protocol: quirks::protocol(&url).to_string(), + search: quirks::search(&url).to_string(), + username: username.to_string(), + }) +} + +pub fn op_url_parse_search_params( + _state: &mut deno_core::OpState, + args: Option<String>, + zero_copy: Option<ZeroCopyBuf>, +) -> Result<Vec<(String, String)>, AnyError> { + let params = match (args, zero_copy) { + (None, Some(zero_copy)) => form_urlencoded::parse(&zero_copy) + .into_iter() + .map(|(k, v)| (k.as_ref().to_owned(), v.as_ref().to_owned())) + .collect(), + (Some(args), None) => form_urlencoded::parse(args.as_bytes()) + .into_iter() + .map(|(k, v)| (k.as_ref().to_owned(), v.as_ref().to_owned())) + .collect(), + _ => return Err(type_error("invalid parameters")), + }; + Ok(params) +} + +pub fn op_url_stringify_search_params( + _state: &mut deno_core::OpState, + args: Vec<(String, String)>, + _: (), +) -> Result<String, AnyError> { + let search = form_urlencoded::Serializer::new(String::new()) + .extend_pairs(args) + .finish(); + Ok(search) +} + +pub fn get_declaration() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("lib.deno_url.d.ts") +} |
