| 1 | "use strict";
|
|---|
| 2 | const usm = require("./url-state-machine");
|
|---|
| 3 | const urlencoded = require("./urlencoded");
|
|---|
| 4 | const URLSearchParams = require("./URLSearchParams");
|
|---|
| 5 |
|
|---|
| 6 | exports.implementation = class URLImpl {
|
|---|
| 7 | constructor(globalObject, constructorArgs) {
|
|---|
| 8 | const url = constructorArgs[0];
|
|---|
| 9 | const base = constructorArgs[1];
|
|---|
| 10 |
|
|---|
| 11 | let parsedBase = null;
|
|---|
| 12 | if (base !== undefined) {
|
|---|
| 13 | parsedBase = usm.basicURLParse(base);
|
|---|
| 14 | if (parsedBase === null) {
|
|---|
| 15 | throw new TypeError(`Invalid base URL: ${base}`);
|
|---|
| 16 | }
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | const parsedURL = usm.basicURLParse(url, { baseURL: parsedBase });
|
|---|
| 20 | if (parsedURL === null) {
|
|---|
| 21 | throw new TypeError(`Invalid URL: ${url}`);
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | const query = parsedURL.query !== null ? parsedURL.query : "";
|
|---|
| 25 |
|
|---|
| 26 | this._url = parsedURL;
|
|---|
| 27 |
|
|---|
| 28 | // We cannot invoke the "new URLSearchParams object" algorithm without going through the constructor, which strips
|
|---|
| 29 | // question mark by default. Therefore the doNotStripQMark hack is used.
|
|---|
| 30 | this._query = URLSearchParams.createImpl(globalObject, [query], { doNotStripQMark: true });
|
|---|
| 31 | this._query._url = this;
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | get href() {
|
|---|
| 35 | return usm.serializeURL(this._url);
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | set href(v) {
|
|---|
| 39 | const parsedURL = usm.basicURLParse(v);
|
|---|
| 40 | if (parsedURL === null) {
|
|---|
| 41 | throw new TypeError(`Invalid URL: ${v}`);
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | this._url = parsedURL;
|
|---|
| 45 |
|
|---|
| 46 | this._query._list.splice(0);
|
|---|
| 47 | const { query } = parsedURL;
|
|---|
| 48 | if (query !== null) {
|
|---|
| 49 | this._query._list = urlencoded.parseUrlencodedString(query);
|
|---|
| 50 | }
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | get origin() {
|
|---|
| 54 | return usm.serializeURLOrigin(this._url);
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | get protocol() {
|
|---|
| 58 | return `${this._url.scheme}:`;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | set protocol(v) {
|
|---|
| 62 | usm.basicURLParse(`${v}:`, { url: this._url, stateOverride: "scheme start" });
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | get username() {
|
|---|
| 66 | return this._url.username;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | set username(v) {
|
|---|
| 70 | if (usm.cannotHaveAUsernamePasswordPort(this._url)) {
|
|---|
| 71 | return;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | usm.setTheUsername(this._url, v);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | get password() {
|
|---|
| 78 | return this._url.password;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | set password(v) {
|
|---|
| 82 | if (usm.cannotHaveAUsernamePasswordPort(this._url)) {
|
|---|
| 83 | return;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | usm.setThePassword(this._url, v);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | get host() {
|
|---|
| 90 | const url = this._url;
|
|---|
| 91 |
|
|---|
| 92 | if (url.host === null) {
|
|---|
| 93 | return "";
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | if (url.port === null) {
|
|---|
| 97 | return usm.serializeHost(url.host);
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | return `${usm.serializeHost(url.host)}:${usm.serializeInteger(url.port)}`;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | set host(v) {
|
|---|
| 104 | if (this._url.cannotBeABaseURL) {
|
|---|
| 105 | return;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | usm.basicURLParse(v, { url: this._url, stateOverride: "host" });
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | get hostname() {
|
|---|
| 112 | if (this._url.host === null) {
|
|---|
| 113 | return "";
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | return usm.serializeHost(this._url.host);
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | set hostname(v) {
|
|---|
| 120 | if (this._url.cannotBeABaseURL) {
|
|---|
| 121 | return;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | usm.basicURLParse(v, { url: this._url, stateOverride: "hostname" });
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | get port() {
|
|---|
| 128 | if (this._url.port === null) {
|
|---|
| 129 | return "";
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | return usm.serializeInteger(this._url.port);
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | set port(v) {
|
|---|
| 136 | if (usm.cannotHaveAUsernamePasswordPort(this._url)) {
|
|---|
| 137 | return;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | if (v === "") {
|
|---|
| 141 | this._url.port = null;
|
|---|
| 142 | } else {
|
|---|
| 143 | usm.basicURLParse(v, { url: this._url, stateOverride: "port" });
|
|---|
| 144 | }
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | get pathname() {
|
|---|
| 148 | if (this._url.cannotBeABaseURL) {
|
|---|
| 149 | return this._url.path[0];
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | if (this._url.path.length === 0) {
|
|---|
| 153 | return "";
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | return `/${this._url.path.join("/")}`;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | set pathname(v) {
|
|---|
| 160 | if (this._url.cannotBeABaseURL) {
|
|---|
| 161 | return;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | this._url.path = [];
|
|---|
| 165 | usm.basicURLParse(v, { url: this._url, stateOverride: "path start" });
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | get search() {
|
|---|
| 169 | if (this._url.query === null || this._url.query === "") {
|
|---|
| 170 | return "";
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | return `?${this._url.query}`;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | set search(v) {
|
|---|
| 177 | const url = this._url;
|
|---|
| 178 |
|
|---|
| 179 | if (v === "") {
|
|---|
| 180 | url.query = null;
|
|---|
| 181 | this._query._list = [];
|
|---|
| 182 | return;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | const input = v[0] === "?" ? v.substring(1) : v;
|
|---|
| 186 | url.query = "";
|
|---|
| 187 | usm.basicURLParse(input, { url, stateOverride: "query" });
|
|---|
| 188 | this._query._list = urlencoded.parseUrlencodedString(input);
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | get searchParams() {
|
|---|
| 192 | return this._query;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | get hash() {
|
|---|
| 196 | if (this._url.fragment === null || this._url.fragment === "") {
|
|---|
| 197 | return "";
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | return `#${this._url.fragment}`;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | set hash(v) {
|
|---|
| 204 | if (v === "") {
|
|---|
| 205 | this._url.fragment = null;
|
|---|
| 206 | return;
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | const input = v[0] === "#" ? v.substring(1) : v;
|
|---|
| 210 | this._url.fragment = "";
|
|---|
| 211 | usm.basicURLParse(input, { url: this._url, stateOverride: "fragment" });
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | toJSON() {
|
|---|
| 215 | return this.href;
|
|---|
| 216 | }
|
|---|
| 217 | };
|
|---|