UNPKG

@westacks/vortex

Version:

Server-based routing for SPAs

198 lines (197 loc) 6.75 kB
import "axios"; import { e as c, a as d, u as p } from "../../form-6EHLr-hJ.js"; import { p as a } from "../../page-DkBioJVC.js"; const i = { key: "historyKey", iv: "historyIv" }; function f() { sessionStorage.removeItem(i.key), sessionStorage.removeItem(i.iv); } async function w(e) { const t = m(), r = await u(); e = JSON.stringify(e); const o = new Uint8Array(e.length * 3), n = new TextEncoder().encodeInto(e, o); return await crypto.subtle.encrypt( { name: "AES-GCM", iv: t }, r, o.subarray(0, n.written) ); } async function b(e) { const t = m(), r = await u(), o = await crypto.subtle.decrypt( { name: "AES-GCM", iv: t }, r, e ); return JSON.parse(new TextDecoder().decode(o)); } async function u() { const e = JSON.parse(sessionStorage.getItem(i.key) || "null"), t = (n) => crypto.subtle.importKey( "raw", new Uint8Array(n), { name: "AES-GCM", length: 256 }, !0, ["encrypt", "decrypt"] ); if (e) return await t(e); const r = await crypto.subtle.generateKey( { name: "AES-GCM", length: 256 }, !0, ["encrypt", "decrypt"] ), o = await crypto.subtle.exportKey("raw", r); return sessionStorage.setItem(i.key, JSON.stringify(Array.from(new Uint8Array(o)))), r; } function m() { const e = JSON.parse(sessionStorage.getItem(i.iv) || "null"); if (e) return new Uint8Array(e); const t = crypto.getRandomValues(new Uint8Array(12)); return sessionStorage.setItem(i.iv, JSON.stringify(Array.from(t))), t; } const S = (e) => e instanceof ArrayBuffer ? b(e) : Promise.resolve(e), l = (e, t = {}) => e.encryptHistory ? w({ page: e, remember: t }) : Promise.resolve({ page: e, remember: t }); async function h(e, t = !1) { const r = t ? e : a.get(); if (c.size > 0) { const n = Object.fromEntries(c); if (history.replaceState(await l(r, n), "", window.location.href), t) return; } const o = t ? "replaceState" : "pushState"; c.clear(), window.history[o](await l(e), "", e.url); } async function y(e) { if (!e.state) return; let t; try { t = await S(e.state); } catch (r) { return console.warn("Failed to resolve state", { cause: r }), d.get(window.location.href, { vortex: { preserveScroll: !0, replaceHistory: !0 } }); } Object.entries(t.remember).forEach(([r, o]) => c.set(r, o)), a.set(t.page); } function A(e) { if (!e.vortex) return e; const t = a.get(), r = typeof e.vortex == "object" ? e.vortex : {}; return e.headers.accept = "text/html, application/xhtml+xml, application/json", e.headers["x-inertia"] = !0, e.headers["x-inertia-version"] = t?.version, e.headers["x-inertia-partial-component"] = t?.component, Array.isArray(r.only) && r.only.length > 0 && (e.headers["x-inertia-partial-data"] = r.only.join(",")), Array.isArray(r.except) && r.except.length > 0 && (e.headers["x-inertia-partial-except"] = r.except.join(",")), Array.isArray(r.reset) && r.reset.length > 0 && (e.headers["x-inertia-reset"] = r.reset.join(",")), e; } function v(e) { if (e.config.prefetch || !e.config.vortex || !e.headers["x-inertia"]) return e; const t = a.get(), r = typeof e.config.vortex == "object" ? e.config.vortex : {}; if (Array.isArray(r.only) && r.only.length > 0) { const o = {}; for (const n of r.only) o[n] = e.data.props[n]; e.data.props = { ...t.props, ...o }; } if (Array.isArray(r.except) && r.except.length > 0) { const o = {}; for (const n of r.except) o[n] = t.props[n]; e.data.props = { ...e.data.props, ...o }; } return e.data = E(e.data, t), r?.preserveHistory ? e.data.url = t?.url || e.data.url : h(e.data, r?.replaceHistory || t?.url === e.data.url && r?.replaceHistory !== !1), a.set(e.data), t.clearHistory && f(), g(e.data), r?.preserveScroll || window.scrollTo(0, 0), e; } function j(e) { if (!e.config?.vortex) return Promise.reject(e); if (e.response?.headers["x-inertia-location"]) return window.location.href = e.response.headers["x-inertia-location"], Promise.resolve(e.response); if (!e.response?.headers["x-inertia"]) O(e.response); else return v(e.response); return Promise.reject(e); } function g(e) { if (e.deferredProps) for (const t of Object.values(e.deferredProps)) d.reload({ vortex: { only: t, preserveHistory: !0, preserveScroll: !0 } }); } function E(e, t) { if (e.deepMergeProps) for (const r of e.deepMergeProps) e.props[r] = x(t.props[r], e.props[r]); if (e.mergeProps) for (const r of e.mergeProps) { const o = e.props[r]; Array.isArray(o) ? e.props[r] = [...t.props[r] || [], ...o] : typeof o == "object" && o !== null && (e.props[r] = { ...t.props[r] || [], ...o }); } return e; } function O(e) { const t = document.createElement("dialog"); Object.assign(t.style, { position: "fixed", inset: "0", maxWidth: "none", maxHeight: "none", width: "100%", height: "100%", display: "flex", justifyContent: "center", alignItems: "center", background: "transparent", backdropFilter: "blur(3px)", outline: "none" }), t.addEventListener("keydown", (s) => { s.key === "Escape" && t.close(); }); const r = document.createElement("form"); r.method = "dialog", Object.assign(r.style, { position: "absolute", inset: "0", zIndex: "-1", margin: "0", padding: "0", border: "none" }); const o = document.createElement("button"); o.type = "submit", o.innerText = "close", Object.assign(o.style, { width: "100%", height: "100%", opacity: "0", cursor: "pointer" }); const n = document.createElement("iframe"); n.src = URL.createObjectURL( new Blob([e.data], { type: e.headers["content-type"] }) ), Object.assign(n.style, { width: "90vw", height: "90vh" }), r.appendChild(o), t.appendChild(n), t.appendChild(r), t.onclose = () => { URL.revokeObjectURL(n.src), document.body.removeChild(t), document.body.style.overflow = ""; }, document.body.appendChild(t), document.body.style.overflow = "hidden", t.showModal(), t.focus(); } const x = (e, t) => Array.isArray(t) ? [...Array.isArray(e) ? e : [], ...t] : typeof t == "object" && t !== null ? Object.keys(t).reduce( (r, o) => (r[o] = x(e ? e[o] : void 0, t[o]), r), { ...e } ) : t, k = (e) => ({ request: t, response: r }) => { h(e, !0), window.addEventListener("popstate", y), p.resolveErrors = (s) => s?.data?.props?.errors ?? {}; const o = t.use(A), n = r.use(v, j); return g(e), () => { t.eject(o), r.eject(n), p.resolveErrors = (s) => ({}), window.removeEventListener("popstate", y); }; }; k.clearHistory = f; export { k as default };