118 lines
No EOL
3.3 KiB
JavaScript
118 lines
No EOL
3.3 KiB
JavaScript
import { t as lazyInherit } from "./_inherit.mjs";
|
|
|
|
//#region src/_url.ts
|
|
/**
|
|
* URL wrapper with fast paths to access to the following props:
|
|
*
|
|
* - `url.pathname`
|
|
* - `url.search`
|
|
* - `url.searchParams`
|
|
* - `url.protocol`
|
|
*
|
|
* **NOTES:**
|
|
*
|
|
* - It is assumed that the input URL is **already encoded** and formatted from an HTTP request and contains no hash.
|
|
* - Triggering the setters or getters on other props will deoptimize to full URL parsing.
|
|
* - Changes to `searchParams` will be discarded as we don't track them.
|
|
*/
|
|
const FastURL = /* @__PURE__ */ (() => {
|
|
const NativeURL = globalThis.URL;
|
|
const FastURL$1 = class URL {
|
|
#url;
|
|
#href;
|
|
#protocol;
|
|
#host;
|
|
#pathname;
|
|
#search;
|
|
#searchParams;
|
|
#pos;
|
|
constructor(url) {
|
|
if (typeof url === "string") this.#href = url;
|
|
else {
|
|
this.#protocol = url.protocol;
|
|
this.#host = url.host;
|
|
this.#pathname = url.pathname;
|
|
this.#search = url.search;
|
|
}
|
|
}
|
|
static [Symbol.hasInstance](val) {
|
|
return val instanceof NativeURL;
|
|
}
|
|
get _url() {
|
|
if (this.#url) return this.#url;
|
|
this.#url = new NativeURL(this.href);
|
|
this.#href = void 0;
|
|
this.#protocol = void 0;
|
|
this.#host = void 0;
|
|
this.#pathname = void 0;
|
|
this.#search = void 0;
|
|
this.#searchParams = void 0;
|
|
this.#pos = void 0;
|
|
return this.#url;
|
|
}
|
|
get href() {
|
|
if (this.#url) return this.#url.href;
|
|
if (!this.#href) this.#href = `${this.#protocol || "http:"}//${this.#host || "localhost"}${this.#pathname || "/"}${this.#search || ""}`;
|
|
return this.#href;
|
|
}
|
|
#getPos() {
|
|
if (!this.#pos) {
|
|
const url = this.href;
|
|
const protoIndex = url.indexOf("://");
|
|
const pathnameIndex = protoIndex === -1 ? -1 : url.indexOf("/", protoIndex + 4);
|
|
this.#pos = [
|
|
protoIndex,
|
|
pathnameIndex,
|
|
pathnameIndex === -1 ? -1 : url.indexOf("?", pathnameIndex)
|
|
];
|
|
}
|
|
return this.#pos;
|
|
}
|
|
get pathname() {
|
|
if (this.#url) return this.#url.pathname;
|
|
if (this.#pathname === void 0) {
|
|
const [, pathnameIndex, queryIndex] = this.#getPos();
|
|
if (pathnameIndex === -1) return this._url.pathname;
|
|
this.#pathname = this.href.slice(pathnameIndex, queryIndex === -1 ? void 0 : queryIndex);
|
|
}
|
|
return this.#pathname;
|
|
}
|
|
get search() {
|
|
if (this.#url) return this.#url.search;
|
|
if (this.#search === void 0) {
|
|
const [, pathnameIndex, queryIndex] = this.#getPos();
|
|
if (pathnameIndex === -1) return this._url.search;
|
|
const url = this.href;
|
|
this.#search = queryIndex === -1 || queryIndex === url.length - 1 ? "" : url.slice(queryIndex);
|
|
}
|
|
return this.#search;
|
|
}
|
|
get searchParams() {
|
|
if (this.#url) return this.#url.searchParams;
|
|
if (!this.#searchParams) this.#searchParams = new URLSearchParams(this.search);
|
|
return this.#searchParams;
|
|
}
|
|
get protocol() {
|
|
if (this.#url) return this.#url.protocol;
|
|
if (this.#protocol === void 0) {
|
|
const [protocolIndex] = this.#getPos();
|
|
if (protocolIndex === -1) return this._url.protocol;
|
|
this.#protocol = this.href.slice(0, protocolIndex + 1);
|
|
}
|
|
return this.#protocol;
|
|
}
|
|
toString() {
|
|
return this.href;
|
|
}
|
|
toJSON() {
|
|
return this.href;
|
|
}
|
|
};
|
|
lazyInherit(FastURL$1.prototype, NativeURL.prototype, "_url");
|
|
Object.setPrototypeOf(FastURL$1.prototype, NativeURL.prototype);
|
|
Object.setPrototypeOf(FastURL$1, NativeURL);
|
|
return FastURL$1;
|
|
})();
|
|
|
|
//#endregion
|
|
export { FastURL as t }; |