@ohhwells/bridge 0.1.54-next.148 → 0.1.54-next.149
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/pages.cjs +7 -2
- package/dist/pages.cjs.map +1 -1
- package/dist/pages.d.cts +1 -1
- package/dist/pages.d.ts +1 -1
- package/dist/pages.js +7 -2
- package/dist/pages.js.map +1 -1
- package/package.json +1 -1
package/dist/pages.cjs
CHANGED
|
@@ -33,7 +33,11 @@ module.exports = __toCommonJS(pages_exports);
|
|
|
33
33
|
|
|
34
34
|
// src/lib/site-pages.ts
|
|
35
35
|
var DELETED_PAGE_PREFIX = "/__ohw-deleted";
|
|
36
|
-
function resolveSubdomain() {
|
|
36
|
+
function resolveSubdomain(hostnameOverride) {
|
|
37
|
+
if (hostnameOverride) {
|
|
38
|
+
const parts = hostnameOverride.split(".");
|
|
39
|
+
if (parts.length >= 3 && parts[0] !== "www") return parts[0];
|
|
40
|
+
}
|
|
37
41
|
const siteUrl = process.env.NEXT_PUBLIC_SITE_URL;
|
|
38
42
|
if (!siteUrl) return "";
|
|
39
43
|
try {
|
|
@@ -93,7 +97,8 @@ async function sitePagesMiddleware(request) {
|
|
|
93
97
|
if (pathname.startsWith("/_next") || pathname.startsWith("/api") || /\.[a-zA-Z0-9]+$/.test(pathname)) {
|
|
94
98
|
return import_server.NextResponse.next();
|
|
95
99
|
}
|
|
96
|
-
const
|
|
100
|
+
const subdomain = request.nextUrl.searchParams.get("subdomain") || resolveSubdomain(request.nextUrl.hostname);
|
|
101
|
+
const pages = await fetchSitePages(subdomain || void 0);
|
|
97
102
|
if (pages.length === 0) return import_server.NextResponse.next();
|
|
98
103
|
const decision = resolvePageRewrite(pathname, pages);
|
|
99
104
|
if (!decision) return import_server.NextResponse.next();
|
package/dist/pages.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/pages.ts","../src/lib/site-pages.ts","../src/lib/site-pages-middleware.ts","../src/lib/site-pages-render.ts"],"sourcesContent":["// Server-safe entry point (no 'use client' banner) — for middleware/Edge runtime and\n// Server Components. Keep this file free of React component exports; UI goes in the\n// default `@ohhwells/bridge` entry (src/index.ts).\nexport {\n DELETED_PAGE_PREFIX,\n resolveSubdomain,\n fetchSitePages,\n resolvePageRewrite,\n resolveSitePage,\n} from './lib/site-pages'\nexport type { SitePage, PageRewriteDecision, SitePageResolution } from './lib/site-pages'\nexport { sitePagesMiddleware, sitePagesMiddlewareConfig } from './lib/site-pages-middleware'\nexport { renderCatchAllPage } from './lib/site-pages-render'\nexport type { CatchAllPageHandlers } from './lib/site-pages-render'\n","// Shared page-CRUD infra (OHH-670): fetching a site's page list and deciding how the\n// catch-all route / middleware should handle deleted, renamed, and duplicated pages.\n// Server-safe (no React/browser APIs) so it can run in middleware (Edge runtime) and\n// Server Components — kept out of the main client-bundled entry point, see `../pages.ts`.\n\n// Prefix used to route deleted-physical-page requests into the catch-all `[...page]`\n// route, which has no file of its own at that segment and calls notFound() for it.\nexport const DELETED_PAGE_PREFIX = '/__ohw-deleted'\n\nexport type SitePage = {\n id: string\n path: string\n title: string\n is_physical: boolean\n original_path: string | null\n duplicated_from: string | null\n is_deleted: boolean\n}\n\n// Mirrors OhhwellsBridge's resolveSubdomain fallback — window.location isn't available on\n// the server (middleware / RSC), so this only uses the NEXT_PUBLIC_SITE_URL env var that's\n// set per deployment.\nexport function resolveSubdomain(): string {\n const siteUrl = process.env.NEXT_PUBLIC_SITE_URL\n if (!siteUrl) return ''\n try {\n const parts = new URL(siteUrl).hostname.split('.')\n if (parts.length >= 3 && parts[0] !== 'www') return parts[0]\n } catch {\n // ignore malformed env value\n }\n return ''\n}\n\n// `subdomainOverride` is the `?subdomain=` query param the canvas editor's iframe appends\n// (see `editUrl` in fo-class-experience). It's required locally: NEXT_PUBLIC_SITE_URL is\n// `http://localhost:3000` in dev, which resolveSubdomain() can't parse a subdomain from.\nexport async function fetchSitePages(subdomainOverride?: string): Promise<SitePage[]> {\n const subdomain = subdomainOverride || resolveSubdomain()\n const apiUrl = process.env.NEXT_PUBLIC_FLOWOPS_API_URL\n if (!subdomain || !apiUrl) return []\n\n try {\n // no-store, not a timed revalidate: middleware uses this list to decide whether a\n // path is deleted/renamed, and `next start`'s persistent Data Cache would otherwise\n // keep routing a just-deleted page's URL to its live content for longer than expected.\n const res = await fetch(`${apiUrl}/api/public/sites/${subdomain}/pages`, {\n cache: 'no-store',\n })\n if (!res.ok) return []\n const { pages } = (await res.json()) as { pages: SitePage[] }\n return pages\n } catch {\n return []\n }\n}\n\nexport type PageRewriteDecision = { path: string } | null\n\n// Middleware decision logic: given the current pathname and the site's page list, decide\n// whether to rewrite to the deleted-page placeholder or to a renamed physical page's real\n// file location. Callers still own the `_next`/`api`/static-file early-return and the actual\n// `NextResponse.rewrite` call, since those are Next.js-specific.\nexport function resolvePageRewrite(pathname: string, pages: SitePage[]): PageRewriteDecision {\n const current = pages.find((page) => page.path === pathname)\n\n if (current) {\n if (current.is_deleted) return { path: `${DELETED_PAGE_PREFIX}${pathname}` }\n // Renamed physical page: the file still lives at original_path, so rewrite there\n // internally while the browser keeps showing the new canonical path.\n if (current.is_physical && current.original_path && current.original_path !== current.path) {\n return { path: current.original_path }\n }\n return null\n }\n\n // Old URL of a renamed physical page — no redirect, it just no longer resolves to\n // anything (whether or not the page has since also been deleted).\n const oldUrlOfRenamed = pages.find(\n (page) => page.is_physical && page.original_path === pathname && page.path !== pathname,\n )\n if (oldUrlOfRenamed) return { path: `${DELETED_PAGE_PREFIX}${pathname}` }\n\n return null\n}\n\nexport type SitePageResolution =\n | { kind: 'not-found' }\n | { kind: 'duplicate'; sourceSegment: string; page: SitePage }\n | { kind: 'blank'; page: SitePage }\n\n// Catch-all route lookup: given the route's path segments, resolve which page (if any) this\n// request maps to. Callers still own the dynamic `import()` for `kind: 'duplicate'` — that\n// needs a literal relative path per app for webpack's static context analysis to work, so it\n// can't live in this shared package (see the comment at the call site).\nexport async function resolveSitePage(segments: string[], subdomainOverride?: string): Promise<SitePageResolution> {\n if (`/${segments[0]}` === DELETED_PAGE_PREFIX) return { kind: 'not-found' }\n\n const path = `/${segments.join('/')}`\n const pages = await fetchSitePages(subdomainOverride)\n const page = pages.find((entry) => entry.path === path && !entry.is_deleted)\n if (!page) return { kind: 'not-found' }\n\n if (page.duplicated_from) {\n const sourceSegment = page.duplicated_from === '/' ? '' : page.duplicated_from.replace(/^\\//, '')\n return { kind: 'duplicate', sourceSegment, page }\n }\n\n return { kind: 'blank', page }\n}\n","import { NextRequest, NextResponse } from 'next/server'\nimport { fetchSitePages, resolvePageRewrite } from './site-pages'\n\n// Drop-in Next.js middleware for the OHH-670 page-CRUD system: rewrites deleted/renamed\n// pages to the right place before the catch-all route (see `resolveSitePage`) ever runs.\n// Apps that need extra middleware logic of their own can still compose `fetchSitePages`/\n// `resolvePageRewrite` directly instead of using this wrapper.\nexport async function sitePagesMiddleware(request: NextRequest): Promise<NextResponse> {\n const { pathname } = request.nextUrl\n\n if (pathname.startsWith('/_next') || pathname.startsWith('/api') || /\\.[a-zA-Z0-9]+$/.test(pathname)) {\n return NextResponse.next()\n }\n\n const pages = await fetchSitePages(request.nextUrl.searchParams.get('subdomain') ?? undefined)\n if (pages.length === 0) return NextResponse.next()\n\n const decision = resolvePageRewrite(pathname, pages)\n if (!decision) return NextResponse.next()\n return NextResponse.rewrite(new URL(decision.path, request.url))\n}\n\n// Mirrors the `_next`/`api` early-return above so apps that re-export this directly as\n// their middleware `config` don't invoke the function needlessly on asset requests.\nexport const sitePagesMiddlewareConfig = {\n matcher: ['/((?!_next|api).*)'],\n}\n","import { notFound } from 'next/navigation'\nimport { createElement, type ComponentType, type ReactElement } from 'react'\nimport { resolveSitePage, type SitePage } from './site-pages'\n\nexport interface CatchAllPageHandlers {\n // Called for a page that's a duplicate of another. Must contain the actual `import(...)`\n // call itself (relative, not the `@` alias) — webpack can only build a static context\n // module (bundling every matching src/app/*/page.tsx) when the prefix is a real relative\n // path, so this one expression has to stay in the calling app's own file, resolved against\n // its own src/app tree, not this shared package's. Everything else about turning the\n // resolved module into a rendered element lives here instead.\n renderDuplicate: (sourceSegment: string) => Promise<{ default: ComponentType }>\n // Called for a page with no section content of its own yet.\n renderBlank: (page: SitePage) => ReactElement\n}\n\n// Full catch-all route resolution for the OHH-670 page-CRUD system: looks the page up,\n// calls `notFound()` for anything unresolved, and delegates to the two things every\n// consuming app still owns (the duplicate-source import and its blank-page chrome).\nexport async function renderCatchAllPage(\n segments: string[],\n subdomainOverride: string | undefined,\n handlers: CatchAllPageHandlers,\n): Promise<ReactElement> {\n const result = await resolveSitePage(segments, subdomainOverride)\n\n if (result.kind === 'not-found') notFound()\n\n if (result.kind === 'duplicate') {\n try {\n const sourceModule = await handlers.renderDuplicate(result.sourceSegment)\n return createElement(sourceModule.default)\n } catch {\n notFound()\n }\n }\n\n return handlers.renderBlank(result.page)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACOO,IAAM,sBAAsB;AAe5B,SAAS,mBAA2B;AACzC,QAAM,UAAU,QAAQ,IAAI;AAC5B,MAAI,CAAC,QAAS,QAAO;AACrB,MAAI;AACF,UAAM,QAAQ,IAAI,IAAI,OAAO,EAAE,SAAS,MAAM,GAAG;AACjD,QAAI,MAAM,UAAU,KAAK,MAAM,CAAC,MAAM,MAAO,QAAO,MAAM,CAAC;AAAA,EAC7D,QAAQ;AAAA,EAER;AACA,SAAO;AACT;AAKA,eAAsB,eAAe,mBAAiD;AACpF,QAAM,YAAY,qBAAqB,iBAAiB;AACxD,QAAM,SAAS,QAAQ,IAAI;AAC3B,MAAI,CAAC,aAAa,CAAC,OAAQ,QAAO,CAAC;AAEnC,MAAI;AAIF,UAAM,MAAM,MAAM,MAAM,GAAG,MAAM,qBAAqB,SAAS,UAAU;AAAA,MACvE,OAAO;AAAA,IACT,CAAC;AACD,QAAI,CA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AAsB,oBAAoB,SAA6C;AACrF,QAAM,EAAE,SAAS,IAAI,QAAQ;AAE7B,MAAI,SAAS,WAAW,QAAQ,KAAK,SAAS,WAAW,MAAM,KAAK,kBAAkB,KAAK,QAAQ,GAAG;AACpG,WAAO,2BAAa,KAAK;AAAA,EAC3B;AAEA,QAAM,QAAQ,MAAM,eAAe,QAAQ,QAAQ,aAAa,IAAI,WAAW,KAAK,MAAS;AAC7F,MAAI,MAAM,WAAW,EAAG,QAAO,2BAAa,KAAK;AAEjD,QAAM,WAAW,mBAAmB,UAAU,KAAK;AACnD,MAAI,CAAC,SAAU,QAAO,2BAAa,KAAK;AACxC,SAAO,2BAAa,QAAQ,IAAI,IAAI,SAAS,MAAM,QAAQ,GAAG,CAAC;AACjE;AAIO,IAAM,4BAA4B;AAAA,EACvC,SAAS,CAAC,oBAAoB;AAChC;;;AC1BA,wBAAyB;AACzB,mBAAqE;AAkBrE,eAAsB,mBACpB,UACA,mBACA,UACuB;AACvB,QAAM,SAAS,MAAM,gBAAgB,UAAU,iBAAiB;AAEhE,MAAI,OAAO,SAAS,YAAa,iCAAS;AAE1C,MAAI,OAAO,SAAS,aAAa;AAC/B,QAAI;AACF,YAAM,eAAe,MAAM,SAAS,gBAAgB,OAAO,aAAa;AACxE,iBAAO,4BAAc,aAAa,OAAO;AAAA,IAC3C,QAAQ;AACN,sCAAS;AAAA,IACX;AAAA,EACF;AAEA,SAAO,SAAS,YAAY,OAAO,IAAI;AACzC;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/pages.ts","../src/lib/site-pages.ts","../src/lib/site-pages-middleware.ts","../src/lib/site-pages-render.ts"],"sourcesContent":["// Server-safe entry point (no 'use client' banner) — for middleware/Edge runtime and\n// Server Components. Keep this file free of React component exports; UI goes in the\n// default `@ohhwells/bridge` entry (src/index.ts).\nexport {\n DELETED_PAGE_PREFIX,\n resolveSubdomain,\n fetchSitePages,\n resolvePageRewrite,\n resolveSitePage,\n} from './lib/site-pages'\nexport type { SitePage, PageRewriteDecision, SitePageResolution } from './lib/site-pages'\nexport { sitePagesMiddleware, sitePagesMiddlewareConfig } from './lib/site-pages-middleware'\nexport { renderCatchAllPage } from './lib/site-pages-render'\nexport type { CatchAllPageHandlers } from './lib/site-pages-render'\n","// Shared page-CRUD infra (OHH-670): fetching a site's page list and deciding how the\n// catch-all route / middleware should handle deleted, renamed, and duplicated pages.\n// Server-safe (no React/browser APIs) so it can run in middleware (Edge runtime) and\n// Server Components — kept out of the main client-bundled entry point, see `../pages.ts`.\n\n// Prefix used to route deleted-physical-page requests into the catch-all `[...page]`\n// route, which has no file of its own at that segment and calls notFound() for it.\nexport const DELETED_PAGE_PREFIX = '/__ohw-deleted'\n\nexport type SitePage = {\n id: string\n path: string\n title: string\n is_physical: boolean\n original_path: string | null\n duplicated_from: string | null\n is_deleted: boolean\n}\n\n// Mirrors OhhwellsBridge's client-side resolveSubdomain: prefer the real request hostname\n// (window.location isn't available on the server, so callers pass the Host header/nextUrl\n// hostname instead) and only fall back to the NEXT_PUBLIC_SITE_URL env var when no hostname\n// is known — the only case that's true in local dev, where the host is just localhost.\nexport function resolveSubdomain(hostnameOverride?: string): string {\n if (hostnameOverride) {\n const parts = hostnameOverride.split('.')\n if (parts.length >= 3 && parts[0] !== 'www') return parts[0]\n }\n\n const siteUrl = process.env.NEXT_PUBLIC_SITE_URL\n if (!siteUrl) return ''\n try {\n const parts = new URL(siteUrl).hostname.split('.')\n if (parts.length >= 3 && parts[0] !== 'www') return parts[0]\n } catch {\n // ignore malformed env value\n }\n return ''\n}\n\n// `subdomainOverride` is the `?subdomain=` query param the canvas editor's iframe appends\n// (see `editUrl` in fo-class-experience). It's required locally: NEXT_PUBLIC_SITE_URL is\n// `http://localhost:3000` in dev, which resolveSubdomain() can't parse a subdomain from.\nexport async function fetchSitePages(subdomainOverride?: string): Promise<SitePage[]> {\n const subdomain = subdomainOverride || resolveSubdomain()\n const apiUrl = process.env.NEXT_PUBLIC_FLOWOPS_API_URL\n if (!subdomain || !apiUrl) return []\n\n try {\n // no-store, not a timed revalidate: middleware uses this list to decide whether a\n // path is deleted/renamed, and `next start`'s persistent Data Cache would otherwise\n // keep routing a just-deleted page's URL to its live content for longer than expected.\n const res = await fetch(`${apiUrl}/api/public/sites/${subdomain}/pages`, {\n cache: 'no-store',\n })\n if (!res.ok) return []\n const { pages } = (await res.json()) as { pages: SitePage[] }\n return pages\n } catch {\n return []\n }\n}\n\nexport type PageRewriteDecision = { path: string } | null\n\n// Middleware decision logic: given the current pathname and the site's page list, decide\n// whether to rewrite to the deleted-page placeholder or to a renamed physical page's real\n// file location. Callers still own the `_next`/`api`/static-file early-return and the actual\n// `NextResponse.rewrite` call, since those are Next.js-specific.\nexport function resolvePageRewrite(pathname: string, pages: SitePage[]): PageRewriteDecision {\n const current = pages.find((page) => page.path === pathname)\n\n if (current) {\n if (current.is_deleted) return { path: `${DELETED_PAGE_PREFIX}${pathname}` }\n // Renamed physical page: the file still lives at original_path, so rewrite there\n // internally while the browser keeps showing the new canonical path.\n if (current.is_physical && current.original_path && current.original_path !== current.path) {\n return { path: current.original_path }\n }\n return null\n }\n\n // Old URL of a renamed physical page — no redirect, it just no longer resolves to\n // anything (whether or not the page has since also been deleted).\n const oldUrlOfRenamed = pages.find(\n (page) => page.is_physical && page.original_path === pathname && page.path !== pathname,\n )\n if (oldUrlOfRenamed) return { path: `${DELETED_PAGE_PREFIX}${pathname}` }\n\n return null\n}\n\nexport type SitePageResolution =\n | { kind: 'not-found' }\n | { kind: 'duplicate'; sourceSegment: string; page: SitePage }\n | { kind: 'blank'; page: SitePage }\n\n// Catch-all route lookup: given the route's path segments, resolve which page (if any) this\n// request maps to. Callers still own the dynamic `import()` for `kind: 'duplicate'` — that\n// needs a literal relative path per app for webpack's static context analysis to work, so it\n// can't live in this shared package (see the comment at the call site).\nexport async function resolveSitePage(segments: string[], subdomainOverride?: string): Promise<SitePageResolution> {\n if (`/${segments[0]}` === DELETED_PAGE_PREFIX) return { kind: 'not-found' }\n\n const path = `/${segments.join('/')}`\n const pages = await fetchSitePages(subdomainOverride)\n const page = pages.find((entry) => entry.path === path && !entry.is_deleted)\n if (!page) return { kind: 'not-found' }\n\n if (page.duplicated_from) {\n const sourceSegment = page.duplicated_from === '/' ? '' : page.duplicated_from.replace(/^\\//, '')\n return { kind: 'duplicate', sourceSegment, page }\n }\n\n return { kind: 'blank', page }\n}\n","import { NextRequest, NextResponse } from 'next/server'\nimport { fetchSitePages, resolvePageRewrite, resolveSubdomain } from './site-pages'\n\n// Drop-in Next.js middleware for the OHH-670 page-CRUD system: rewrites deleted/renamed\n// pages to the right place before the catch-all route (see `resolveSitePage`) ever runs.\n// Apps that need extra middleware logic of their own can still compose `fetchSitePages`/\n// `resolvePageRewrite` directly instead of using this wrapper.\nexport async function sitePagesMiddleware(request: NextRequest): Promise<NextResponse> {\n const { pathname } = request.nextUrl\n\n if (pathname.startsWith('/_next') || pathname.startsWith('/api') || /\\.[a-zA-Z0-9]+$/.test(pathname)) {\n return NextResponse.next()\n }\n\n // `?subdomain=` is the canvas editor iframe's override (its own origin isn't the site's\n // real domain). Every other request — every real visitor — hits the site's actual\n // `*.ohhwells.site`/custom domain, so the request's own hostname is the source of truth\n // there; without this fallback the middleware silently no-ops on every production request.\n const subdomain =\n request.nextUrl.searchParams.get('subdomain') || resolveSubdomain(request.nextUrl.hostname)\n const pages = await fetchSitePages(subdomain || undefined)\n if (pages.length === 0) return NextResponse.next()\n\n const decision = resolvePageRewrite(pathname, pages)\n if (!decision) return NextResponse.next()\n return NextResponse.rewrite(new URL(decision.path, request.url))\n}\n\n// Mirrors the `_next`/`api` early-return above so apps that re-export this directly as\n// their middleware `config` don't invoke the function needlessly on asset requests.\nexport const sitePagesMiddlewareConfig = {\n matcher: ['/((?!_next|api).*)'],\n}\n","import { notFound } from 'next/navigation'\nimport { createElement, type ComponentType, type ReactElement } from 'react'\nimport { resolveSitePage, type SitePage } from './site-pages'\n\nexport interface CatchAllPageHandlers {\n // Called for a page that's a duplicate of another. Must contain the actual `import(...)`\n // call itself (relative, not the `@` alias) — webpack can only build a static context\n // module (bundling every matching src/app/*/page.tsx) when the prefix is a real relative\n // path, so this one expression has to stay in the calling app's own file, resolved against\n // its own src/app tree, not this shared package's. Everything else about turning the\n // resolved module into a rendered element lives here instead.\n renderDuplicate: (sourceSegment: string) => Promise<{ default: ComponentType }>\n // Called for a page with no section content of its own yet.\n renderBlank: (page: SitePage) => ReactElement\n}\n\n// Full catch-all route resolution for the OHH-670 page-CRUD system: looks the page up,\n// calls `notFound()` for anything unresolved, and delegates to the two things every\n// consuming app still owns (the duplicate-source import and its blank-page chrome).\nexport async function renderCatchAllPage(\n segments: string[],\n subdomainOverride: string | undefined,\n handlers: CatchAllPageHandlers,\n): Promise<ReactElement> {\n const result = await resolveSitePage(segments, subdomainOverride)\n\n if (result.kind === 'not-found') notFound()\n\n if (result.kind === 'duplicate') {\n try {\n const sourceModule = await handlers.renderDuplicate(result.sourceSegment)\n return createElement(sourceModule.default)\n } catch {\n notFound()\n }\n }\n\n return handlers.renderBlank(result.page)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACOO,IAAM,sBAAsB;AAgB5B,SAAS,iBAAiB,kBAAmC;AAClE,MAAI,kBAAkB;AACpB,UAAM,QAAQ,iBAAiB,MAAM,GAAG;AACxC,QAAI,MAAM,UAAU,KAAK,MAAM,CAAC,MAAM,MAAO,QAAO,MAAM,CAAC;AAAA,EAC7D;AAEA,QAAM,UAAU,QAAQ,IAAI;AAC5B,MAAI,CAAC,QAAS,QAAO;AACrB,MAAI;AACF,UAAM,QAAQ,IAAI,IAAI,OAAO,EAAE,SAAS,MAAM,GAAG;AACjD,QAAI,MAAM,UAAU,KAAK,MAAM,CAAC,MAAM,MAAO,QAAO,MAAM,CAAC;AAAA,EAC7D,QAAQ;AAAA,EAER;AACA,SAAO;AACT;AAKA,eAA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|
package/dist/pages.d.cts
CHANGED
|
@@ -11,7 +11,7 @@ type SitePage = {
|
|
|
11
11
|
duplicated_from: string | null;
|
|
12
12
|
is_deleted: boolean;
|
|
13
13
|
};
|
|
14
|
-
declare function resolveSubdomain(): string;
|
|
14
|
+
declare function resolveSubdomain(hostnameOverride?: string): string;
|
|
15
15
|
declare function fetchSitePages(subdomainOverride?: string): Promise<SitePage[]>;
|
|
16
16
|
type PageRewriteDecision = {
|
|
17
17
|
path: string;
|
package/dist/pages.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ type SitePage = {
|
|
|
11
11
|
duplicated_from: string | null;
|
|
12
12
|
is_deleted: boolean;
|
|
13
13
|
};
|
|
14
|
-
declare function resolveSubdomain(): string;
|
|
14
|
+
declare function resolveSubdomain(hostnameOverride?: string): string;
|
|
15
15
|
declare function fetchSitePages(subdomainOverride?: string): Promise<SitePage[]>;
|
|
16
16
|
type PageRewriteDecision = {
|
|
17
17
|
path: string;
|
package/dist/pages.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
// src/lib/site-pages.ts
|
|
2
2
|
var DELETED_PAGE_PREFIX = "/__ohw-deleted";
|
|
3
|
-
function resolveSubdomain() {
|
|
3
|
+
function resolveSubdomain(hostnameOverride) {
|
|
4
|
+
if (hostnameOverride) {
|
|
5
|
+
const parts = hostnameOverride.split(".");
|
|
6
|
+
if (parts.length >= 3 && parts[0] !== "www") return parts[0];
|
|
7
|
+
}
|
|
4
8
|
const siteUrl = process.env.NEXT_PUBLIC_SITE_URL;
|
|
5
9
|
if (!siteUrl) return "";
|
|
6
10
|
try {
|
|
@@ -60,7 +64,8 @@ async function sitePagesMiddleware(request) {
|
|
|
60
64
|
if (pathname.startsWith("/_next") || pathname.startsWith("/api") || /\.[a-zA-Z0-9]+$/.test(pathname)) {
|
|
61
65
|
return NextResponse.next();
|
|
62
66
|
}
|
|
63
|
-
const
|
|
67
|
+
const subdomain = request.nextUrl.searchParams.get("subdomain") || resolveSubdomain(request.nextUrl.hostname);
|
|
68
|
+
const pages = await fetchSitePages(subdomain || void 0);
|
|
64
69
|
if (pages.length === 0) return NextResponse.next();
|
|
65
70
|
const decision = resolvePageRewrite(pathname, pages);
|
|
66
71
|
if (!decision) return NextResponse.next();
|
package/dist/pages.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/lib/site-pages.ts","../src/lib/site-pages-middleware.ts","../src/lib/site-pages-render.ts"],"sourcesContent":["// Shared page-CRUD infra (OHH-670): fetching a site's page list and deciding how the\n// catch-all route / middleware should handle deleted, renamed, and duplicated pages.\n// Server-safe (no React/browser APIs) so it can run in middleware (Edge runtime) and\n// Server Components — kept out of the main client-bundled entry point, see `../pages.ts`.\n\n// Prefix used to route deleted-physical-page requests into the catch-all `[...page]`\n// route, which has no file of its own at that segment and calls notFound() for it.\nexport const DELETED_PAGE_PREFIX = '/__ohw-deleted'\n\nexport type SitePage = {\n id: string\n path: string\n title: string\n is_physical: boolean\n original_path: string | null\n duplicated_from: string | null\n is_deleted: boolean\n}\n\n// Mirrors OhhwellsBridge's resolveSubdomain fallback — window.location isn't available on\n// the server (middleware / RSC), so this only uses the NEXT_PUBLIC_SITE_URL env var that's\n// set per deployment.\nexport function resolveSubdomain(): string {\n const siteUrl = process.env.NEXT_PUBLIC_SITE_URL\n if (!siteUrl) return ''\n try {\n const parts = new URL(siteUrl).hostname.split('.')\n if (parts.length >= 3 && parts[0] !== 'www') return parts[0]\n } catch {\n // ignore malformed env value\n }\n return ''\n}\n\n// `subdomainOverride` is the `?subdomain=` query param the canvas editor's iframe appends\n// (see `editUrl` in fo-class-experience). It's required locally: NEXT_PUBLIC_SITE_URL is\n// `http://localhost:3000` in dev, which resolveSubdomain() can't parse a subdomain from.\nexport async function fetchSitePages(subdomainOverride?: string): Promise<SitePage[]> {\n const subdomain = subdomainOverride || resolveSubdomain()\n const apiUrl = process.env.NEXT_PUBLIC_FLOWOPS_API_URL\n if (!subdomain || !apiUrl) return []\n\n try {\n // no-store, not a timed revalidate: middleware uses this list to decide whether a\n // path is deleted/renamed, and `next start`'s persistent Data Cache would otherwise\n // keep routing a just-deleted page's URL to its live content for longer than expected.\n const res = await fetch(`${apiUrl}/api/public/sites/${subdomain}/pages`, {\n cache: 'no-store',\n })\n if (!res.ok) return []\n const { pages } = (await res.json()) as { pages: SitePage[] }\n return pages\n } catch {\n return []\n }\n}\n\nexport type PageRewriteDecision = { path: string } | null\n\n// Middleware decision logic: given the current pathname and the site's page list, decide\n// whether to rewrite to the deleted-page placeholder or to a renamed physical page's real\n// file location. Callers still own the `_next`/`api`/static-file early-return and the actual\n// `NextResponse.rewrite` call, since those are Next.js-specific.\nexport function resolvePageRewrite(pathname: string, pages: SitePage[]): PageRewriteDecision {\n const current = pages.find((page) => page.path === pathname)\n\n if (current) {\n if (current.is_deleted) return { path: `${DELETED_PAGE_PREFIX}${pathname}` }\n // Renamed physical page: the file still lives at original_path, so rewrite there\n // internally while the browser keeps showing the new canonical path.\n if (current.is_physical && current.original_path && current.original_path !== current.path) {\n return { path: current.original_path }\n }\n return null\n }\n\n // Old URL of a renamed physical page — no redirect, it just no longer resolves to\n // anything (whether or not the page has since also been deleted).\n const oldUrlOfRenamed = pages.find(\n (page) => page.is_physical && page.original_path === pathname && page.path !== pathname,\n )\n if (oldUrlOfRenamed) return { path: `${DELETED_PAGE_PREFIX}${pathname}` }\n\n return null\n}\n\nexport type SitePageResolution =\n | { kind: 'not-found' }\n | { kind: 'duplicate'; sourceSegment: string; page: SitePage }\n | { kind: 'blank'; page: SitePage }\n\n// Catch-all route lookup: given the route's path segments, resolve which page (if any) this\n// request maps to. Callers still own the dynamic `import()` for `kind: 'duplicate'` — that\n// needs a literal relative path per app for webpack's static context analysis to work, so it\n// can't live in this shared package (see the comment at the call site).\nexport async function resolveSitePage(segments: string[], subdomainOverride?: string): Promise<SitePageResolution> {\n if (`/${segments[0]}` === DELETED_PAGE_PREFIX) return { kind: 'not-found' }\n\n const path = `/${segments.join('/')}`\n const pages = await fetchSitePages(subdomainOverride)\n const page = pages.find((entry) => entry.path === path && !entry.is_deleted)\n if (!page) return { kind: 'not-found' }\n\n if (page.duplicated_from) {\n const sourceSegment = page.duplicated_from === '/' ? '' : page.duplicated_from.replace(/^\\//, '')\n return { kind: 'duplicate', sourceSegment, page }\n }\n\n return { kind: 'blank', page }\n}\n","import { NextRequest, NextResponse } from 'next/server'\nimport { fetchSitePages, resolvePageRewrite } from './site-pages'\n\n// Drop-in Next.js middleware for the OHH-670 page-CRUD system: rewrites deleted/renamed\n// pages to the right place before the catch-all route (see `resolveSitePage`) ever runs.\n// Apps that need extra middleware logic of their own can still compose `fetchSitePages`/\n// `resolvePageRewrite` directly instead of using this wrapper.\nexport async function sitePagesMiddleware(request: NextRequest): Promise<NextResponse> {\n const { pathname } = request.nextUrl\n\n if (pathname.startsWith('/_next') || pathname.startsWith('/api') || /\\.[a-zA-Z0-9]+$/.test(pathname)) {\n return NextResponse.next()\n }\n\n const pages = await fetchSitePages(request.nextUrl.searchParams.get('subdomain') ?? undefined)\n if (pages.length === 0) return NextResponse.next()\n\n const decision = resolvePageRewrite(pathname, pages)\n if (!decision) return NextResponse.next()\n return NextResponse.rewrite(new URL(decision.path, request.url))\n}\n\n// Mirrors the `_next`/`api` early-return above so apps that re-export this directly as\n// their middleware `config` don't invoke the function needlessly on asset requests.\nexport const sitePagesMiddlewareConfig = {\n matcher: ['/((?!_next|api).*)'],\n}\n","import { notFound } from 'next/navigation'\nimport { createElement, type ComponentType, type ReactElement } from 'react'\nimport { resolveSitePage, type SitePage } from './site-pages'\n\nexport interface CatchAllPageHandlers {\n // Called for a page that's a duplicate of another. Must contain the actual `import(...)`\n // call itself (relative, not the `@` alias) — webpack can only build a static context\n // module (bundling every matching src/app/*/page.tsx) when the prefix is a real relative\n // path, so this one expression has to stay in the calling app's own file, resolved against\n // its own src/app tree, not this shared package's. Everything else about turning the\n // resolved module into a rendered element lives here instead.\n renderDuplicate: (sourceSegment: string) => Promise<{ default: ComponentType }>\n // Called for a page with no section content of its own yet.\n renderBlank: (page: SitePage) => ReactElement\n}\n\n// Full catch-all route resolution for the OHH-670 page-CRUD system: looks the page up,\n// calls `notFound()` for anything unresolved, and delegates to the two things every\n// consuming app still owns (the duplicate-source import and its blank-page chrome).\nexport async function renderCatchAllPage(\n segments: string[],\n subdomainOverride: string | undefined,\n handlers: CatchAllPageHandlers,\n): Promise<ReactElement> {\n const result = await resolveSitePage(segments, subdomainOverride)\n\n if (result.kind === 'not-found') notFound()\n\n if (result.kind === 'duplicate') {\n try {\n const sourceModule = await handlers.renderDuplicate(result.sourceSegment)\n return createElement(sourceModule.default)\n } catch {\n notFound()\n }\n }\n\n return handlers.renderBlank(result.page)\n}\n"],"mappings":";AAOO,IAAM,sBAAsB;AAe5B,SAAS,mBAA2B;AACzC,QAAM,UAAU,QAAQ,IAAI;AAC5B,MAAI,CAAC,QAAS,QAAO;AACrB,MAAI;AACF,UAAM,QAAQ,IAAI,IAAI,OAAO,EAAE,SAAS,MAAM,GAAG;AACjD,QAAI,MAAM,UAAU,KAAK,MAAM,CAAC,MAAM,MAAO,QAAO,MAAM,CAAC;AAAA,EAC7D,QAAQ;AAAA,EAER;AACA,SAAO;AACT;AAKA,eAAsB,eAAe,mBAAiD;AACpF,QAAM,YAAY,qBAAqB,iBAAiB;AACxD,QAAM,SAAS,QAAQ,IAAI;AAC3B,MAAI,CAAC,aAAa,CAAC,OAAQ,QAAO,CAAC;AAEnC,MAAI;AAIF,UAAM,MAAM,MAAM,MAAM,GAAG,MAAM,qBAAqB,SAAS,UAAU;AAAA,MACvE,OAAO;AAAA,IACT,CAAC;AACD,QAAI,CAAC,IAAI,GAAI,QAAO,CAAC;AACrB,UAAM,EAAE,MAAM,IAAK,MAAM,IAAI,KAAK;AAClC,WAAO;AAAA,EACT,QAAQ;AACN,WAAO,CAAC;AAAA,EACV;AACF;AAQO,SAAS,mBAAmB,UAAkB,OAAwC;AAC3F,QAAM,UAAU,MAAM,KA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AAM,QAAQ,MAAM,eAAe,QAAQ,QAAQ,aAAa,IAAI,WAAW,KAAK,MAAS;AAC7F,MAAI,MAAM,WAAW,EAAG,QAAO,aAAa,KAAK;AAEjD,QAAM,WAAW,mBAAmB,UAAU,KAAK;AACnD,MAAI,CAAC,SAAU,QAAO,aAAa,KAAK;AACxC,SAAO,aAAa,QAAQ,IAAI,IAAI,SAAS,MAAM,QAAQ,GAAG,CAAC;AACjE;AAIO,IAAM,4BAA4B;AAAA,EACvC,SAAS,CAAC,oBAAoB;AAChC;;;AC1BA,SAAS,gBAAgB;AACzB,SAAS,qBAA4D;AAkBrE,eAAsB,mBACpB,UACA,mBACA,UACuB;AACvB,QAAM,SAAS,MAAM,gBAAgB,UAAU,iBAAiB;AAEhE,MAAI,OAAO,SAAS,YAAa,UAAS;AAE1C,MAAI,OAAO,SAAS,aAAa;AAC/B,QAAI;AACF,YAAM,eAAe,MAAM,SAAS,gBAAgB,OAAO,aAAa;AACxE,aAAO,cAAc,aAAa,OAAO;AAAA,IAC3C,QAAQ;AACN,eAAS;AAAA,IACX;AAAA,EACF;AAEA,SAAO,SAAS,YAAY,OAAO,IAAI;AACzC;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/lib/site-pages.ts","../src/lib/site-pages-middleware.ts","../src/lib/site-pages-render.ts"],"sourcesContent":["// Shared page-CRUD infra (OHH-670): fetching a site's page list and deciding how the\n// catch-all route / middleware should handle deleted, renamed, and duplicated pages.\n// Server-safe (no React/browser APIs) so it can run in middleware (Edge runtime) and\n// Server Components — kept out of the main client-bundled entry point, see `../pages.ts`.\n\n// Prefix used to route deleted-physical-page requests into the catch-all `[...page]`\n// route, which has no file of its own at that segment and calls notFound() for it.\nexport const DELETED_PAGE_PREFIX = '/__ohw-deleted'\n\nexport type SitePage = {\n id: string\n path: string\n title: string\n is_physical: boolean\n original_path: string | null\n duplicated_from: string | null\n is_deleted: boolean\n}\n\n// Mirrors OhhwellsBridge's client-side resolveSubdomain: prefer the real request hostname\n// (window.location isn't available on the server, so callers pass the Host header/nextUrl\n// hostname instead) and only fall back to the NEXT_PUBLIC_SITE_URL env var when no hostname\n// is known — the only case that's true in local dev, where the host is just localhost.\nexport function resolveSubdomain(hostnameOverride?: string): string {\n if (hostnameOverride) {\n const parts = hostnameOverride.split('.')\n if (parts.length >= 3 && parts[0] !== 'www') return parts[0]\n }\n\n const siteUrl = process.env.NEXT_PUBLIC_SITE_URL\n if (!siteUrl) return ''\n try {\n const parts = new URL(siteUrl).hostname.split('.')\n if (parts.length >= 3 && parts[0] !== 'www') return parts[0]\n } catch {\n // ignore malformed env value\n }\n return ''\n}\n\n// `subdomainOverride` is the `?subdomain=` query param the canvas editor's iframe appends\n// (see `editUrl` in fo-class-experience). It's required locally: NEXT_PUBLIC_SITE_URL is\n// `http://localhost:3000` in dev, which resolveSubdomain() can't parse a subdomain from.\nexport async function fetchSitePages(subdomainOverride?: string): Promise<SitePage[]> {\n const subdomain = subdomainOverride || resolveSubdomain()\n const apiUrl = process.env.NEXT_PUBLIC_FLOWOPS_API_URL\n if (!subdomain || !apiUrl) return []\n\n try {\n // no-store, not a timed revalidate: middleware uses this list to decide whether a\n // path is deleted/renamed, and `next start`'s persistent Data Cache would otherwise\n // keep routing a just-deleted page's URL to its live content for longer than expected.\n const res = await fetch(`${apiUrl}/api/public/sites/${subdomain}/pages`, {\n cache: 'no-store',\n })\n if (!res.ok) return []\n const { pages } = (await res.json()) as { pages: SitePage[] }\n return pages\n } catch {\n return []\n }\n}\n\nexport type PageRewriteDecision = { path: string } | null\n\n// Middleware decision logic: given the current pathname and the site's page list, decide\n// whether to rewrite to the deleted-page placeholder or to a renamed physical page's real\n// file location. Callers still own the `_next`/`api`/static-file early-return and the actual\n// `NextResponse.rewrite` call, since those are Next.js-specific.\nexport function resolvePageRewrite(pathname: string, pages: SitePage[]): PageRewriteDecision {\n const current = pages.find((page) => page.path === pathname)\n\n if (current) {\n if (current.is_deleted) return { path: `${DELETED_PAGE_PREFIX}${pathname}` }\n // Renamed physical page: the file still lives at original_path, so rewrite there\n // internally while the browser keeps showing the new canonical path.\n if (current.is_physical && current.original_path && current.original_path !== current.path) {\n return { path: current.original_path }\n }\n return null\n }\n\n // Old URL of a renamed physical page — no redirect, it just no longer resolves to\n // anything (whether or not the page has since also been deleted).\n const oldUrlOfRenamed = pages.find(\n (page) => page.is_physical && page.original_path === pathname && page.path !== pathname,\n )\n if (oldUrlOfRenamed) return { path: `${DELETED_PAGE_PREFIX}${pathname}` }\n\n return null\n}\n\nexport type SitePageResolution =\n | { kind: 'not-found' }\n | { kind: 'duplicate'; sourceSegment: string; page: SitePage }\n | { kind: 'blank'; page: SitePage }\n\n// Catch-all route lookup: given the route's path segments, resolve which page (if any) this\n// request maps to. Callers still own the dynamic `import()` for `kind: 'duplicate'` — that\n// needs a literal relative path per app for webpack's static context analysis to work, so it\n// can't live in this shared package (see the comment at the call site).\nexport async function resolveSitePage(segments: string[], subdomainOverride?: string): Promise<SitePageResolution> {\n if (`/${segments[0]}` === DELETED_PAGE_PREFIX) return { kind: 'not-found' }\n\n const path = `/${segments.join('/')}`\n const pages = await fetchSitePages(subdomainOverride)\n const page = pages.find((entry) => entry.path === path && !entry.is_deleted)\n if (!page) return { kind: 'not-found' }\n\n if (page.duplicated_from) {\n const sourceSegment = page.duplicated_from === '/' ? '' : page.duplicated_from.replace(/^\\//, '')\n return { kind: 'duplicate', sourceSegment, page }\n }\n\n return { kind: 'blank', page }\n}\n","import { NextRequest, NextResponse } from 'next/server'\nimport { fetchSitePages, resolvePageRewrite, resolveSubdomain } from './site-pages'\n\n// Drop-in Next.js middleware for the OHH-670 page-CRUD system: rewrites deleted/renamed\n// pages to the right place before the catch-all route (see `resolveSitePage`) ever runs.\n// Apps that need extra middleware logic of their own can still compose `fetchSitePages`/\n// `resolvePageRewrite` directly instead of using this wrapper.\nexport async function sitePagesMiddleware(request: NextRequest): Promise<NextResponse> {\n const { pathname } = request.nextUrl\n\n if (pathname.startsWith('/_next') || pathname.startsWith('/api') || /\\.[a-zA-Z0-9]+$/.test(pathname)) {\n return NextResponse.next()\n }\n\n // `?subdomain=` is the canvas editor iframe's override (its own origin isn't the site's\n // real domain). Every other request — every real visitor — hits the site's actual\n // `*.ohhwells.site`/custom domain, so the request's own hostname is the source of truth\n // there; without this fallback the middleware silently no-ops on every production request.\n const subdomain =\n request.nextUrl.searchParams.get('subdomain') || resolveSubdomain(request.nextUrl.hostname)\n const pages = await fetchSitePages(subdomain || undefined)\n if (pages.length === 0) return NextResponse.next()\n\n const decision = resolvePageRewrite(pathname, pages)\n if (!decision) return NextResponse.next()\n return NextResponse.rewrite(new URL(decision.path, request.url))\n}\n\n// Mirrors the `_next`/`api` early-return above so apps that re-export this directly as\n// their middleware `config` don't invoke the function needlessly on asset requests.\nexport const sitePagesMiddlewareConfig = {\n matcher: ['/((?!_next|api).*)'],\n}\n","import { notFound } from 'next/navigation'\nimport { createElement, type ComponentType, type ReactElement } from 'react'\nimport { resolveSitePage, type SitePage } from './site-pages'\n\nexport interface CatchAllPageHandlers {\n // Called for a page that's a duplicate of another. Must contain the actual `import(...)`\n // call itself (relative, not the `@` alias) — webpack can only build a static context\n // module (bundling every matching src/app/*/page.tsx) when the prefix is a real relative\n // path, so this one expression has to stay in the calling app's own file, resolved against\n // its own src/app tree, not this shared package's. Everything else about turning the\n // resolved module into a rendered element lives here instead.\n renderDuplicate: (sourceSegment: string) => Promise<{ default: ComponentType }>\n // Called for a page with no section content of its own yet.\n renderBlank: (page: SitePage) => ReactElement\n}\n\n// Full catch-all route resolution for the OHH-670 page-CRUD system: looks the page up,\n// calls `notFound()` for anything unresolved, and delegates to the two things every\n// consuming app still owns (the duplicate-source import and its blank-page chrome).\nexport async function renderCatchAllPage(\n segments: string[],\n subdomainOverride: string | undefined,\n handlers: CatchAllPageHandlers,\n): Promise<ReactElement> {\n const result = await resolveSitePage(segments, subdomainOverride)\n\n if (result.kind === 'not-found') notFound()\n\n if (result.kind === 'duplicate') {\n try {\n const sourceModule = await handlers.renderDuplicate(result.sourceSegment)\n return createElement(sourceModule.default)\n } catch {\n notFound()\n }\n }\n\n return handlers.renderBlank(result.page)\n}\n"],"mappings":";AAOO,IAAM,sBAAsB;AAgB5B,SAAS,iBAAiB,kBAAmC;AAClE,MAAI,kBAAkB;AACpB,UAAM,QAAQ,iBAAiB,MAAM,GAAG;AACxC,QAAI,MAAM,UAAU,KAAK,MAAM,CAAC,MAAM,MAAO,QAAO,MAAM,CAAC;AAAA,EAC7D;AAEA,QAAM,UAAU,QAAQ,IAAI;AAC5B,MAAI,CAAC,QAAS,QAAO;AACrB,MAAI;AACF,UAAM,QAAQ,IAAI,IAAI,OAAO,EAAE,SAAS,MAAM,GAAG;AACjD,QAAI,MAAM,UAAU,KAAK,MAAM,CAAC,MAAM,MAAO,QAAO,MAAM,CAAC;AAAA,EAC7D,QAAQ;AAAA,EAER;AACA,SAAO;AACT;AAKA,eAAsB,eAAe,mBAAiD;AACpF,QAAM,YAAY,qBAAqB,iBAAiB;AACxD,QAAM,SAAS,QAAQ,IAAI;AAC3B,MAAI,CAAC,aAAa,CAAC,OAAQ,QAAO,CAAC;AAEnC,MAAI;AAIF,UAAM,MAAM,MAAM,MAAM,GAAG,MAAM,qBAAqB,SAAS,UAAU;AAAA,MACvE,OAAO;AAAA,IACT,CAAC;AACD,QAAI,CAAC,IAAI,GAAI,QAAO,CAAC;AACrB,UAAM,EAAE,MAAM,IAAK,MAAM,IAAI,KAAK;AAClC,WAAO;AAAA,EACT,QAAQ;AACN,WAAO,CAAC;AAAA,EACV;AACF;AAQO,SAAS,mBAAmB,UAAkB,OAAwC;AAC3F,QAAM,UAAU,MAAM,KAAK,CAAC,SAAS,KAAK,SAAS,QAAQ;AAE3D,MAAI,SAAS;AACX,QAAI,QAAQ,WAAY,QAAO,EAAE,MAAM,GAAG,mBAAmB,GAAG,QAAQ,GAAG;AAG3E,QAAI,QAAQ,eAAe,QAAQ,iBAAiB,QAAQ,kBAAkB,QAAQ,MAAM;AAC1F,a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