UNPKG

nuxi

Version:
220 lines (219 loc) 8.49 kB
String.fromCharCode; const HASH_RE = /#/g; const AMPERSAND_RE = /&/g; const SLASH_RE = /\//g; const EQUAL_RE = /=/g; const PLUS_RE = /\+/g; const ENC_CARET_RE = /%5e/gi; const ENC_BACKTICK_RE = /%60/gi; const ENC_PIPE_RE = /%7c/gi; const ENC_SPACE_RE = /%20/gi; function encode(text) { return encodeURI("" + text).replace(ENC_PIPE_RE, "|"); } function encodeQueryValue(input) { return encode(typeof input === "string" ? input : JSON.stringify(input)).replace(PLUS_RE, "%2B").replace(ENC_SPACE_RE, "+").replace(HASH_RE, "%23").replace(AMPERSAND_RE, "%26").replace(ENC_BACKTICK_RE, "`").replace(ENC_CARET_RE, "^").replace(SLASH_RE, "%2F"); } function encodeQueryKey(text) { return encodeQueryValue(text).replace(EQUAL_RE, "%3D"); } function decode(text = "") { try { return decodeURIComponent("" + text); } catch { return "" + text; } } function decodeQueryKey(text) { return decode(text.replace(PLUS_RE, " ")); } function decodeQueryValue(text) { return decode(text.replace(PLUS_RE, " ")); } function parseQuery(parametersString = "") { const object = /* @__PURE__ */ Object.create(null); if (parametersString[0] === "?") parametersString = parametersString.slice(1); for (const parameter of parametersString.split("&")) { const s = parameter.match(/([^=]+)=?(.*)/) || []; if (s.length < 2) continue; const key = decodeQueryKey(s[1]); if (key === "__proto__" || key === "constructor") continue; const value = decodeQueryValue(s[2] || ""); if (object[key] === void 0) object[key] = value; else if (Array.isArray(object[key])) object[key].push(value); else object[key] = [object[key], value]; } return object; } function encodeQueryItem(key, value) { if (typeof value === "number" || typeof value === "boolean") value = String(value); if (!value) return encodeQueryKey(key); if (Array.isArray(value)) return value.map((_value) => `${encodeQueryKey(key)}=${encodeQueryValue(_value)}`).join("&"); return `${encodeQueryKey(key)}=${encodeQueryValue(value)}`; } function stringifyQuery(query) { return Object.keys(query).filter((k) => query[k] !== void 0).map((k) => encodeQueryItem(k, query[k])).filter(Boolean).join("&"); } const PROTOCOL_STRICT_REGEX = /^[\s\w\0+.-]{2,}:([/\\]{1,2})/; const PROTOCOL_REGEX = /^[\s\w\0+.-]{2,}:([/\\]{2})?/; const PROTOCOL_RELATIVE_REGEX = /^([/\\]\s*){2,}[^/\\]/; const TRAILING_SLASH_RE = /\/$|\/\?|\/#/; const JOIN_LEADING_SLASH_RE = /^\.?\//; function hasProtocol(inputString, opts = {}) { if (typeof opts === "boolean") opts = { acceptRelative: opts }; if (opts.strict) return PROTOCOL_STRICT_REGEX.test(inputString); return PROTOCOL_REGEX.test(inputString) || (opts.acceptRelative ? PROTOCOL_RELATIVE_REGEX.test(inputString) : false); } function hasTrailingSlash(input = "", respectQueryAndFragment) { if (!respectQueryAndFragment) return input.endsWith("/"); return TRAILING_SLASH_RE.test(input); } function withoutTrailingSlash(input = "", respectQueryAndFragment) { if (!respectQueryAndFragment) return (hasTrailingSlash(input) ? input.slice(0, -1) : input) || "/"; if (!hasTrailingSlash(input, true)) return input || "/"; let path = input; let fragment = ""; const fragmentIndex = input.indexOf("#"); if (fragmentIndex !== -1) { path = input.slice(0, fragmentIndex); fragment = input.slice(fragmentIndex); } const [s0, ...s] = path.split("?"); return ((s0.endsWith("/") ? s0.slice(0, -1) : s0) || "/") + (s.length > 0 ? `?${s.join("?")}` : "") + fragment; } function withTrailingSlash(input = "", respectQueryAndFragment) { if (!respectQueryAndFragment) return input.endsWith("/") ? input : input + "/"; if (hasTrailingSlash(input, true)) return input || "/"; let path = input; let fragment = ""; const fragmentIndex = input.indexOf("#"); if (fragmentIndex !== -1) { path = input.slice(0, fragmentIndex); fragment = input.slice(fragmentIndex); if (!path) return fragment; } const [s0, ...s] = path.split("?"); return s0 + "/" + (s.length > 0 ? `?${s.join("?")}` : "") + fragment; } function withBase(input, base) { if (isEmptyURL(base) || hasProtocol(input)) return input; const _base = withoutTrailingSlash(base); if (input.startsWith(_base)) { const nextChar = input[_base.length]; if (!nextChar || nextChar === "/" || nextChar === "?") return input; } return joinURL(_base, input); } function withQuery(input, query) { const parsed = parseURL(input); parsed.search = stringifyQuery({ ...parseQuery(parsed.search), ...query }); return stringifyParsedURL(parsed); } function isEmptyURL(url) { return !url || url === "/"; } function isNonEmptyURL(url) { return url && url !== "/"; } function joinURL(base, ...input) { let url = base || ""; for (const segment of input.filter((url2) => isNonEmptyURL(url2))) if (url) { const _segment = segment.replace(JOIN_LEADING_SLASH_RE, ""); url = withTrailingSlash(url) + _segment; } else url = segment; return url; } const protocolRelative = Symbol.for("ufo:protocolRelative"); function parseURL(input = "", defaultProto) { const _specialProtoMatch = input.match(/^[\s\0]*(blob:|data:|javascript:|vbscript:)(.*)/i); if (_specialProtoMatch) { const [, _proto, _pathname = ""] = _specialProtoMatch; return { protocol: _proto.toLowerCase(), pathname: _pathname, href: _proto + _pathname, auth: "", host: "", search: "", hash: "" }; } if (!hasProtocol(input, { acceptRelative: true })) return defaultProto ? parseURL(defaultProto + input) : parsePath(input); const [, protocol = "", auth, hostAndPath = ""] = input.replace(/\\/g, "/").match(/^[\s\0]*([\w+.-]{2,}:)?\/\/([^/@]+@)?(.*)/) || []; let [, host = "", path = ""] = hostAndPath.match(/([^#/?]*)(.*)?/) || []; if (protocol === "file:") path = path.replace(/\/(?=[A-Za-z]:)/, ""); const { pathname, search, hash } = parsePath(path); return { protocol: protocol.toLowerCase(), auth: auth ? auth.slice(0, Math.max(0, auth.length - 1)) : "", host, pathname, search, hash, [protocolRelative]: !protocol }; } function parsePath(input = "") { const [pathname = "", search = "", hash = ""] = (input.match(/([^#?]*)(\?[^#]*)?(#.*)?/) || []).splice(1); return { pathname, search, hash }; } function stringifyParsedURL(parsed) { const pathname = parsed.pathname || ""; const search = parsed.search ? (parsed.search.startsWith("?") ? "" : "?") + parsed.search : ""; const hash = parsed.hash || ""; const auth = parsed.auth ? parsed.auth + "@" : ""; const host = parsed.host || ""; return (parsed.protocol || parsed[protocolRelative] ? (parsed.protocol || "") + "//" : "") + auth + host + pathname + search + hash; } //#endregion //#region ../../node_modules/.pnpm/destr@2.0.5/node_modules/destr/dist/index.mjs const suspectProtoRx = /"(?:_|\\u0{2}5[Ff]){2}(?:p|\\u0{2}70)(?:r|\\u0{2}72)(?:o|\\u0{2}6[Ff])(?:t|\\u0{2}74)(?:o|\\u0{2}6[Ff])(?:_|\\u0{2}5[Ff]){2}"\s*:/; const suspectConstructorRx = /"(?:c|\\u0063)(?:o|\\u006[Ff])(?:n|\\u006[Ee])(?:s|\\u0073)(?:t|\\u0074)(?:r|\\u0072)(?:u|\\u0075)(?:c|\\u0063)(?:t|\\u0074)(?:o|\\u006[Ff])(?:r|\\u0072)"\s*:/; const JsonSigRx = /^\s*["[{]|^\s*-?\d{1,16}(\.\d{1,17})?([Ee][+-]?\d+)?\s*$/; function jsonParseTransform(key, value) { if (key === "__proto__" || key === "constructor" && value && typeof value === "object" && "prototype" in value) { warnKeyDropped(key); return; } return value; } function warnKeyDropped(key) { console.warn(`[destr] Dropping "${key}" key to prevent prototype pollution.`); } function destr(value, options = {}) { if (typeof value !== "string") return value; if (value[0] === "\"" && value[value.length - 1] === "\"" && value.indexOf("\\") === -1) return value.slice(1, -1); const _value = value.trim(); if (_value.length <= 9) switch (_value.toLowerCase()) { case "true": return true; case "false": return false; case "undefined": return; case "null": return null; case "nan": return NaN; case "infinity": return Number.POSITIVE_INFINITY; case "-infinity": return Number.NEGATIVE_INFINITY; } if (!JsonSigRx.test(value)) { if (options.strict) throw new SyntaxError("[destr] Invalid JSON"); return value; } try { if (suspectProtoRx.test(value) || suspectConstructorRx.test(value)) { if (options.strict) throw new Error("[destr] Possible prototype pollution"); return JSON.parse(value, jsonParseTransform); } return JSON.parse(value); } catch (error) { if (options.strict) throw error; return value; } } //#endregion export { withQuery as i, joinURL as n, withBase as r, destr as t };