deno.land / x / deno@v1.28.2 / ext / url / 00_url.js
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804// Copyright 2018-2022 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 ops = core.ops; const webidl = window.__bootstrap.webidl; const { ArrayIsArray, ArrayPrototypeMap, ArrayPrototypePush, ArrayPrototypeSome, ArrayPrototypeSort, ArrayPrototypeSplice, ObjectKeys, Uint32Array, SafeArrayIterator, StringPrototypeSlice, Symbol, SymbolFor, SymbolIterator, TypeError, } = window.__bootstrap.primordials;
const _list = Symbol("list"); const _urlObject = Symbol("url object");
// WARNING: must match rust code's UrlSetter::* const SET_HASH = 0; const SET_HOST = 1; const SET_HOSTNAME = 2; const SET_PASSWORD = 3; const SET_PATHNAME = 4; const SET_PORT = 5; const SET_PROTOCOL = 6; const SET_SEARCH = 7; const SET_USERNAME = 8;
// Helper functions function opUrlReparse(href, setter, value) { const status = ops.op_url_reparse( href, setter, value, componentsBuf.buffer, ); return getSerialization(status, href); }
function opUrlParse(href, maybeBase) { let status; if (maybeBase === undefined) { status = ops.op_url_parse(href, componentsBuf.buffer); } else { status = core.ops.op_url_parse_with_base( href, maybeBase, componentsBuf.buffer, ); } return getSerialization(status, href); }
function getSerialization(status, href) { if (status === 0) { return href; } else if (status === 1) { return core.ops.op_url_get_serialization(); } else { throw new TypeError("Invalid URL"); } }
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 (!init) { // if there is no query string, return early this[_list] = []; return; }
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] = ops.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; } url[_updateUrlSearch](this.toString()); }
/** * @param {string} name * @param {string} value */ append(name, value) { webidl.assertBranded(this, URLSearchParamsPrototype); 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, URLSearchParamsPrototype); 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, URLSearchParamsPrototype); 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, URLSearchParamsPrototype); 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, URLSearchParamsPrototype); 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, URLSearchParamsPrototype); 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, URLSearchParamsPrototype); ArrayPrototypeSort( this[_list], (a, b) => (a[0] === b[0] ? 0 : a[0] > b[0] ? 1 : -1), ); this.#updateUrlSearch(); }
/** * @return {string} */ toString() { webidl.assertBranded(this, URLSearchParamsPrototype); return ops.op_url_stringify_search_params(this[_list]); } }
webidl.mixinPairIterable("URLSearchParams", URLSearchParams, _list, 0, 1);
webidl.configurePrototype(URLSearchParams); const URLSearchParamsPrototype = URLSearchParams.prototype;
webidl.converters["URLSearchParams"] = webidl.createInterfaceConverter( "URLSearchParams", URLSearchParamsPrototype, );
const _updateUrlSearch = Symbol("updateUrlSearch");
function trim(s) { if (s.length === 1) return ""; return s; }
// Represents a "no port" value. A port in URL cannot be greater than 2^16 − 1 const NO_PORT = 65536;
const componentsBuf = new Uint32Array(8); class URL { #queryObject = null; #serialization; #schemeEnd; #usernameEnd; #hostStart; #hostEnd; #port; #pathStart; #queryStart; #fragmentStart;
[_updateUrlSearch](value) { this.#serialization = opUrlReparse( this.#serialization, SET_SEARCH, value, ); this.#updateComponents(); }
/** * @param {string} url * @param {string} base */ constructor(url, base = undefined) { const prefix = "Failed to construct 'URL'"; url = webidl.converters.DOMString(url, { prefix, context: "Argument 1" }); if (base !== undefined) { base = webidl.converters.DOMString(base, { prefix, context: "Argument 2", }); } this[webidl.brand] = webidl.brand; this.#serialization = opUrlParse(url, base); this.#updateComponents(); }
#updateComponents() { [ this.#schemeEnd, this.#usernameEnd, this.#hostStart, this.#hostEnd, this.#port, this.#pathStart, this.#queryStart, this.#fragmentStart, ] = componentsBuf; }
[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) { 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, inspectOptions)}`; }
#updateSearchParams() { if (this.#queryObject !== null) { const params = this.#queryObject[_list]; const newParams = ops.op_url_parse_search_params( StringPrototypeSlice(this.search, 1), ); ArrayPrototypeSplice( params, 0, params.length, ...new SafeArrayIterator(newParams), ); } }
#hasAuthority() { // https://github.com/servo/rust-url/blob/1d307ae51a28fecc630ecec03380788bfb03a643/url/src/lib.rs#L824 return this.#serialization.slice(this.#schemeEnd).startsWith("://"); }
/** @return {string} */ get hash() { webidl.assertBranded(this, URLPrototype); // https://github.com/servo/rust-url/blob/1d307ae51a28fecc630ecec03380788bfb03a643/url/src/quirks.rs#L263 return this.#fragmentStart ? trim(this.#serialization.slice(this.#fragmentStart)) : ""; }
/** @param {string} value */ set hash(value) { webidl.assertBranded(this, URLPrototype); const prefix = "Failed to set 'hash' on 'URL'"; webidl.requiredArguments(arguments.length, 1, { prefix }); value = webidl.converters.DOMString(value, { prefix, context: "Argument 1", }); try { this.#serialization = opUrlReparse( this.#serialization, SET_HASH, value, ); this.#updateComponents(); } catch { /* pass */ } }
/** @return {string} */ get host() { webidl.assertBranded(this, URLPrototype); // https://github.com/servo/rust-url/blob/1d307ae51a28fecc630ecec03380788bfb03a643/url/src/quirks.rs#L101 return this.#serialization.slice(this.#hostStart, this.#pathStart); }
/** @param {string} value */ set host(value) { webidl.assertBranded(this, URLPrototype); const prefix = "Failed to set 'host' on 'URL'"; webidl.requiredArguments(arguments.length, 1, { prefix }); value = webidl.converters.DOMString(value, { prefix, context: "Argument 1", }); try { this.#serialization = opUrlReparse( this.#serialization, SET_HOST, value, ); this.#updateComponents(); } catch { /* pass */ } }
/** @return {string} */ get hostname() { webidl.assertBranded(this, URLPrototype); // https://github.com/servo/rust-url/blob/1d307ae51a28fecc630ecec03380788bfb03a643/url/src/lib.rs#L988 return this.#serialization.slice(this.#hostStart, this.#hostEnd); }
/** @param {string} value */ set hostname(value) { webidl.assertBranded(this, URLPrototype); const prefix = "Failed to set 'hostname' on 'URL'"; webidl.requiredArguments(arguments.length, 1, { prefix }); value = webidl.converters.DOMString(value, { prefix, context: "Argument 1", }); try { this.#serialization = opUrlReparse( this.#serialization, SET_HOSTNAME, value, ); this.#updateComponents(); } catch { /* pass */ } }
/** @return {string} */ get href() { webidl.assertBranded(this, URLPrototype); return this.#serialization; }
/** @param {string} value */ set href(value) { webidl.assertBranded(this, URLPrototype); const prefix = "Failed to set 'href' on 'URL'"; webidl.requiredArguments(arguments.length, 1, { prefix }); value = webidl.converters.DOMString(value, { prefix, context: "Argument 1", }); this.#serialization = opUrlParse(value); this.#updateComponents(); this.#updateSearchParams(); }
/** @return {string} */ get origin() { webidl.assertBranded(this, URLPrototype); // https://github.com/servo/rust-url/blob/1d307ae51a28fecc630ecec03380788bfb03a643/url/src/origin.rs#L14 const scheme = this.#serialization.slice(0, this.#schemeEnd); if ( scheme === "http" || scheme === "https" || scheme === "ftp" || scheme === "ws" || scheme === "wss" ) { return `${scheme}://${this.host}`; }
if (scheme === "blob") { // TODO(@littledivy): Fast path. try { return new URL(this.pathname).origin; } catch { return "null"; } }
return "null"; }
/** @return {string} */ get password() { webidl.assertBranded(this, URLPrototype); // https://github.com/servo/rust-url/blob/1d307ae51a28fecc630ecec03380788bfb03a643/url/src/lib.rs#L914 if ( this.#hasAuthority() && this.#usernameEnd !== this.#serialization.length && this.#serialization[this.#usernameEnd] === ":" ) { return this.#serialization.slice( this.#usernameEnd + 1, this.#hostStart - 1, ); } return ""; }
/** @param {string} value */ set password(value) { webidl.assertBranded(this, URLPrototype); const prefix = "Failed to set 'password' on 'URL'"; webidl.requiredArguments(arguments.length, 1, { prefix }); value = webidl.converters.DOMString(value, { prefix, context: "Argument 1", }); try { this.#serialization = opUrlReparse( this.#serialization, SET_PASSWORD, value, ); this.#updateComponents(); } catch { /* pass */ } }
/** @return {string} */ get pathname() { webidl.assertBranded(this, URLPrototype); // https://github.com/servo/rust-url/blob/1d307ae51a28fecc630ecec03380788bfb03a643/url/src/lib.rs#L1203 if (!this.#queryStart && !this.#fragmentStart) { return this.#serialization.slice(this.#pathStart); }
const nextComponentStart = this.#queryStart || this.#fragmentStart; return this.#serialization.slice(this.#pathStart, nextComponentStart); }
/** @param {string} value */ set pathname(value) { webidl.assertBranded(this, URLPrototype); const prefix = "Failed to set 'pathname' on 'URL'"; webidl.requiredArguments(arguments.length, 1, { prefix }); value = webidl.converters.DOMString(value, { prefix, context: "Argument 1", }); try { this.#serialization = opUrlReparse( this.#serialization, SET_PATHNAME, value, ); this.#updateComponents(); } catch { /* pass */ } }
/** @return {string} */ get port() { webidl.assertBranded(this, URLPrototype); // https://github.com/servo/rust-url/blob/1d307ae51a28fecc630ecec03380788bfb03a643/url/src/quirks.rs#L196 if (this.#port === NO_PORT) { return this.#serialization.slice(this.#hostEnd, this.#pathStart); } else { return this.#serialization.slice( this.#hostEnd + 1, /* : */ this.#pathStart, ); } }
/** @param {string} value */ set port(value) { webidl.assertBranded(this, URLPrototype); const prefix = "Failed to set 'port' on 'URL'"; webidl.requiredArguments(arguments.length, 1, { prefix }); value = webidl.converters.DOMString(value, { prefix, context: "Argument 1", }); try { this.#serialization = opUrlReparse( this.#serialization, SET_PORT, value, ); this.#updateComponents(); } catch { /* pass */ } }
/** @return {string} */ get protocol() { webidl.assertBranded(this, URLPrototype); // https://github.com/servo/rust-url/blob/1d307ae51a28fecc630ecec03380788bfb03a643/url/src/quirks.rs#L56 return this.#serialization.slice(0, this.#schemeEnd + 1 /* : */); }
/** @param {string} value */ set protocol(value) { webidl.assertBranded(this, URLPrototype); const prefix = "Failed to set 'protocol' on 'URL'"; webidl.requiredArguments(arguments.length, 1, { prefix }); value = webidl.converters.DOMString(value, { prefix, context: "Argument 1", }); try { this.#serialization = opUrlReparse( this.#serialization, SET_PROTOCOL, value, ); this.#updateComponents(); } catch { /* pass */ } }
/** @return {string} */ get search() { webidl.assertBranded(this, URLPrototype); // https://github.com/servo/rust-url/blob/1d307ae51a28fecc630ecec03380788bfb03a643/url/src/quirks.rs#L249 const afterPath = this.#queryStart || this.#fragmentStart || this.#serialization.length; const afterQuery = this.#fragmentStart || this.#serialization.length; return trim(this.#serialization.slice(afterPath, afterQuery)); }
/** @param {string} value */ set search(value) { webidl.assertBranded(this, URLPrototype); const prefix = "Failed to set 'search' on 'URL'"; webidl.requiredArguments(arguments.length, 1, { prefix }); value = webidl.converters.DOMString(value, { prefix, context: "Argument 1", }); try { this.#serialization = opUrlReparse( this.#serialization, SET_SEARCH, value, ); this.#updateComponents(); this.#updateSearchParams(); } catch { /* pass */ } }
/** @return {string} */ get username() { webidl.assertBranded(this, URLPrototype); // https://github.com/servo/rust-url/blob/1d307ae51a28fecc630ecec03380788bfb03a643/url/src/lib.rs#L881 const schemeSeperatorLen = 3; /* :// */ if ( this.#hasAuthority() && this.#usernameEnd > this.#schemeEnd + schemeSeperatorLen ) { return this.#serialization.slice( this.#schemeEnd + schemeSeperatorLen, this.#usernameEnd, ); } else { return ""; } }
/** @param {string} value */ set username(value) { webidl.assertBranded(this, URLPrototype); const prefix = "Failed to set 'username' on 'URL'"; webidl.requiredArguments(arguments.length, 1, { prefix }); value = webidl.converters.DOMString(value, { prefix, context: "Argument 1", }); try { this.#serialization = opUrlReparse( this.#serialization, SET_USERNAME, value, ); this.#updateComponents(); } 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, URLPrototype); return this.#serialization; }
/** @return {string} */ toJSON() { webidl.assertBranded(this, URLPrototype); return this.#serialization; } }
webidl.configurePrototype(URL); const URLPrototype = URL.prototype;
/** * 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 ops.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, URLPrototype, URLSearchParams, URLSearchParamsPrototype, parseUrlEncoded, };})(this);
Version Info