127 lines
3.6 KiB
JavaScript
127 lines
3.6 KiB
JavaScript
function lazyInherit(target, source, sourceKey) {
|
|
for (const key of [...Object.getOwnPropertyNames(source), ...Object.getOwnPropertySymbols(source)]) {
|
|
if (key === "constructor") continue;
|
|
const targetDesc = Object.getOwnPropertyDescriptor(target, key);
|
|
const desc = Object.getOwnPropertyDescriptor(source, key);
|
|
let modified = false;
|
|
if (desc.get) {
|
|
modified = true;
|
|
desc.get = targetDesc?.get || function() {
|
|
return this[sourceKey][key];
|
|
};
|
|
}
|
|
if (desc.set) {
|
|
modified = true;
|
|
desc.set = targetDesc?.set || function(value) {
|
|
this[sourceKey][key] = value;
|
|
};
|
|
}
|
|
if (!targetDesc?.value && typeof desc.value === "function") {
|
|
modified = true;
|
|
desc.value = function(...args) {
|
|
return this[sourceKey][key](...args);
|
|
};
|
|
}
|
|
if (modified) Object.defineProperty(target, key, desc);
|
|
}
|
|
}
|
|
const FastURL = /* @__PURE__ */ (() => {
|
|
const NativeURL = globalThis.URL;
|
|
const FastURL = 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.prototype, NativeURL.prototype, "_url");
|
|
Object.setPrototypeOf(FastURL.prototype, NativeURL.prototype);
|
|
Object.setPrototypeOf(FastURL, NativeURL);
|
|
return FastURL;
|
|
})();
|
|
export { lazyInherit as n, FastURL as t };
|