payload-live-preview 2.0.1 → 2.0.2

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.
Files changed (76) hide show
  1. package/README.md +36 -31
  2. package/dist/adapters/astro/RichText.astro +1 -1
  3. package/dist/adapters/astro/index.js +1 -1
  4. package/dist/adapters/astro/index.js.map +1 -1
  5. package/dist/adapters/astro/middleware-entry.js +1 -1
  6. package/dist/adapters/astro/middleware-entry.js.map +1 -1
  7. package/dist/adapters/nextjs/index.js +1 -1
  8. package/dist/adapters/nextjs/index.js.map +1 -1
  9. package/dist/adapters/nuxt/index.js +1 -1
  10. package/dist/adapters/nuxt/index.js.map +1 -1
  11. package/dist/adapters/nuxt/module.d.ts +12 -4
  12. package/dist/adapters/nuxt/module.js.map +1 -1
  13. package/dist/adapters/react/index.d.ts +6 -5
  14. package/dist/adapters/react/index.js.map +1 -1
  15. package/dist/adapters/sveltekit/index.js +1 -1
  16. package/dist/adapters/sveltekit/index.js.map +1 -1
  17. package/dist/adapters/vue/index.d.ts +3 -3
  18. package/dist/adapters/vue/index.js.map +1 -1
  19. package/dist/annotate.js +1 -1
  20. package/dist/annotate.js.map +1 -1
  21. package/dist/client.cjs +1 -1
  22. package/dist/client.cjs.map +1 -1
  23. package/dist/client.js +1 -1
  24. package/dist/client.js.map +1 -1
  25. package/dist/codegen-astro.d.ts +4 -1
  26. package/dist/codegen-astro.js.map +1 -1
  27. package/dist/codegen-cli.js +1 -1
  28. package/dist/codegen-cli.js.map +1 -1
  29. package/dist/codegen.cjs +1 -1
  30. package/dist/codegen.cjs.map +1 -1
  31. package/dist/codegen.js +1 -1
  32. package/dist/codegen.js.map +1 -1
  33. package/dist/core.cjs +1 -1
  34. package/dist/core.cjs.map +1 -1
  35. package/dist/core.d.cts +1 -1
  36. package/dist/core.d.ts +1 -1
  37. package/dist/core.js +1 -1
  38. package/dist/core.js.map +1 -1
  39. package/dist/doctor-cli.d.ts +1 -1
  40. package/dist/doctor-cli.js +1 -1
  41. package/dist/doctor-cli.js.map +1 -1
  42. package/dist/doctor.d.ts +2 -2
  43. package/dist/doctor.js +1 -1
  44. package/dist/doctor.js.map +1 -1
  45. package/dist/fragment.cjs.map +1 -1
  46. package/dist/fragment.d.cts +6 -3
  47. package/dist/fragment.d.ts +6 -3
  48. package/dist/fragment.js.map +1 -1
  49. package/dist/index.cjs +1 -1
  50. package/dist/index.cjs.map +1 -1
  51. package/dist/index.d.cts +8 -8
  52. package/dist/index.d.ts +8 -8
  53. package/dist/index.js +1 -1
  54. package/dist/index.js.map +1 -1
  55. package/dist/lean.cjs +1 -1
  56. package/dist/lean.cjs.map +1 -1
  57. package/dist/lean.d.cts +3 -3
  58. package/dist/lean.d.ts +3 -3
  59. package/dist/lean.js +1 -1
  60. package/dist/lean.js.map +1 -1
  61. package/dist/lexical.cjs +1 -1
  62. package/dist/lexical.cjs.map +1 -1
  63. package/dist/lexical.d.cts +11 -1
  64. package/dist/lexical.d.ts +11 -1
  65. package/dist/lexical.js +1 -1
  66. package/dist/lexical.js.map +1 -1
  67. package/dist/plugins.cjs +1 -1
  68. package/dist/plugins.cjs.map +1 -1
  69. package/dist/plugins.js +1 -1
  70. package/dist/plugins.js.map +1 -1
  71. package/dist/{probe-wmvfVEsI.d.ts → probe-DqaItcwX.d.ts} +2 -1
  72. package/dist/structural.cjs +2 -2
  73. package/dist/structural.cjs.map +1 -1
  74. package/dist/structural.js +2 -2
  75. package/dist/structural.js.map +1 -1
  76. package/package.json +2 -3
@@ -7,7 +7,11 @@ import { P as PreviewAdapterOptions, a as PreviewRequestLike } from '../../optio
7
7
  * ```ts
8
8
  * export default defineNuxtConfig({
9
9
  * modules: ['payload-live-preview/nuxt-module'],
10
- * livePreview: { allowedOrigins: [process.env.PUBLIC_PAYLOAD_ADMIN_ORIGIN] },
10
+ * livePreview: {
11
+ * // The module cannot carry authorizePreview (below), so it runs on the 1.x profile.
12
+ * defaults: 'v1',
13
+ * allowedOrigins: [process.env.PUBLIC_PAYLOAD_ADMIN_ORIGIN!],
14
+ * },
11
15
  * });
12
16
  * ```
13
17
  *
@@ -25,9 +29,13 @@ import { P as PreviewAdapterOptions, a as PreviewRequestLike } from '../../optio
25
29
  * `srcDir`, so a bare specifier would be looked for under `server/`).
26
30
  *
27
31
  * That the options are serialized is why `authorizePreview` and `shouldInject`
28
- * are not part of this type: a function does not survive `JSON.stringify`. A
29
- * preview that needs either registers `livePreviewNitroPlugin()` by hand,
30
- * exactly as before (docs/nuxt.md).
32
+ * are not part of this type: a function does not survive `JSON.stringify`. It
33
+ * is also why the example sets `defaults: 'v1'`: the generated plugin builds
34
+ * its policy when Nitro loads it, and the strict 2.0 default throws there
35
+ * without `authorizePreview`. Under `'v1'` (or `strict: false`) the plugin
36
+ * injects on client-controlled intent alone. A preview that needs either
37
+ * function registers `livePreviewNitroPlugin()` by hand, exactly as before
38
+ * (docs/nuxt.md).
31
39
  */
32
40
 
33
41
  /** What the module accepts, from `livePreview` in `nuxt.config.ts` or from `modules: [[..., options]]`. */
@@ -1 +1 @@
1
- {"version":3,"file":"module.js","names":["PLUGIN_FILENAME","pluginSource","options","JSON","stringify","join","livePreviewModule","inlineOptions","nuxt","livePreview","build","templates","push","filename","write","getContents","plugin","buildDir","hook","config","plugins","includes","meta","name","configKey"],"sources":["../../../src/adapters/nuxt/module.ts"],"sourcesContent":["/**\n * The Nuxt module: one line in `nuxt.config.ts` instead of a hand-written\n * Nitro plugin.\n *\n * ```ts\n * export default defineNuxtConfig({\n * modules: ['payload-live-preview/nuxt-module'],\n * livePreview: { allowedOrigins: [process.env.PUBLIC_PAYLOAD_ADMIN_ORIGIN] },\n * });\n * ```\n *\n * A Nuxt module is a plain function of `(inlineOptions, nuxt)`; `defineNuxtModule`\n * from `@nuxt/kit` is sugar around exactly that. Writing the function keeps\n * `@nuxt/kit` out of the dependency list — this package adapts Nuxt, it does not\n * build with it — and keeps the module readable as the two things it does:\n * take the options, and register a Nitro plugin written from them.\n *\n * The plugin is generated rather than shipped. The module runs at build time and\n * the plugin runs per request in Nitro's own bundle, so the options cannot travel\n * as a closure — they have to be written as source. Nuxt already has the\n * mechanism: a build template, written into `.nuxt/` and registered by its path,\n * which is what `resolveNitroPath` wants (it resolves a plugin entry against\n * `srcDir`, so a bare specifier would be looked for under `server/`).\n *\n * That the options are serialized is why `authorizePreview` and `shouldInject`\n * are not part of this type: a function does not survive `JSON.stringify`. A\n * preview that needs either registers `livePreviewNitroPlugin()` by hand,\n * exactly as before (docs/nuxt.md).\n */\n\nimport { join } from 'node:path';\nimport type { PreviewAdapterOptions } from '@adapters/shared/options';\nimport type { PreviewRequestLike } from '@adapters/shared/preview-request';\n\n/** What the module accepts, from `livePreview` in `nuxt.config.ts` or from `modules: [[..., options]]`. */\nexport type LivePreviewModuleOptions = Omit<\n PreviewAdapterOptions<PreviewRequestLike>,\n 'authorizePreview' | 'shouldInject'\n>;\n\n/** The Nitro config slice this module writes into. */\nexport interface NitroConfigLike {\n plugins?: string[];\n}\n\n/** The one build template shape this module adds; Nuxt writes it to `buildDir/filename`. */\nexport interface NuxtTemplateLike {\n readonly filename: string;\n readonly write: boolean;\n readonly getContents: () => string;\n}\n\n/**\n * The two config slots and the one hook this module touches, duck-typed. Nuxt's\n * own types would mean a build-time dependency on `nuxt` for everyone who\n * installs this package.\n */\nexport interface NuxtLike {\n readonly options: {\n readonly buildDir: string;\n readonly build: { templates: NuxtTemplateLike[] };\n livePreview?: LivePreviewModuleOptions;\n };\n readonly hook: (name: 'nitro:config', handler: (config: NitroConfigLike) => void) => void;\n}\n\n/** The generated plugin's name in `.nuxt/`, distinctive enough to recognise in a build listing. @internal */\nexport const PLUGIN_FILENAME = 'payload-live-preview-nitro-plugin.mjs';\n\n/**\n * The generated plugin: an import of the public adapter entry and a call with\n * the options. Nothing framework-private, so a reader who opens the file in\n * `.nuxt/` sees the hand-written setup they would otherwise have typed.\n * @internal\n */\nexport function pluginSource(options: LivePreviewModuleOptions): string {\n return [\n \"import { livePreviewNitroPlugin } from 'payload-live-preview/nuxt';\",\n '',\n `export default livePreviewNitroPlugin(${JSON.stringify(options)});`,\n '',\n ].join('\\n');\n}\n\n/**\n * The module itself. Options come from `livePreview` in the Nuxt config, from\n * the module's own inline options, or from both — the inline ones win, because\n * they are the more specific place to write them.\n */\nexport default function livePreviewModule(\n inlineOptions: LivePreviewModuleOptions | undefined,\n nuxt: NuxtLike,\n): void {\n const options: LivePreviewModuleOptions = {\n ...(nuxt.options.livePreview ?? {}),\n ...(inlineOptions ?? {}),\n };\n nuxt.options.build.templates.push({\n filename: PLUGIN_FILENAME,\n // Nitro reads the plugin from disk, not from Nuxt's virtual file system.\n write: true,\n getContents: () => pluginSource(options),\n });\n const plugin = join(nuxt.options.buildDir, PLUGIN_FILENAME);\n nuxt.hook('nitro:config', (config) => {\n const plugins = (config.plugins ??= []);\n // Nuxt re-runs modules on a config reload; registering twice would inject\n // the runtime twice into every page.\n if (!plugins.includes(plugin)) plugins.push(plugin);\n });\n}\n\n/** Nuxt reads this off the module function to name it in build output and errors. */\nlivePreviewModule.meta = {\n name: 'payload-live-preview',\n configKey: 'livePreview',\n};\n"],"mappings":"4BAmEO,IAAMA,EAAkB,wCAQxB,SAASC,EAAaC,GAC3B,MAAO,CACL,sEACA,GACA,yCAAyCC,KAAKC,UAAUF,OACxD,IACAG,KAAK,KACT,CAOe,SAARC,EACLC,EACAC,GAEA,MAAMN,EAAoC,IACpCM,EAAKN,QAAQO,aAAe,MAC5BF,GAAiB,IAEvBC,EAAKN,QAAQQ,MAAMC,UAAUC,KAAK,CAChCC,SAAUb,EAEVc,OAAO,EACPC,YAAa,IAAMd,EAAaC,KAElC,MAAMc,EAASX,EAAKG,EAAKN,QAAQe,SAAUjB,GAC3CQ,EAAKU,KAAK,eAAiBC,IACzB,MAAMC,EAAWD,EAAOC,UAAY,GAG/BA,EAAQC,SAASL,IAASI,EAAQR,KAAKI,IAEhD,CAGAV,EAAkBgB,KAAO,CACvBC,KAAM,uBACNC,UAAW,sB","ignoreList":[]}
1
+ {"version":3,"file":"module.js","names":["PLUGIN_FILENAME","pluginSource","options","JSON","stringify","join","livePreviewModule","inlineOptions","nuxt","livePreview","build","templates","push","filename","write","getContents","plugin","buildDir","hook","config","plugins","includes","meta","name","configKey"],"sources":["../../../src/adapters/nuxt/module.ts"],"sourcesContent":["/**\n * The Nuxt module: one line in `nuxt.config.ts` instead of a hand-written\n * Nitro plugin.\n *\n * ```ts\n * export default defineNuxtConfig({\n * modules: ['payload-live-preview/nuxt-module'],\n * livePreview: {\n * // The module cannot carry authorizePreview (below), so it runs on the 1.x profile.\n * defaults: 'v1',\n * allowedOrigins: [process.env.PUBLIC_PAYLOAD_ADMIN_ORIGIN!],\n * },\n * });\n * ```\n *\n * A Nuxt module is a plain function of `(inlineOptions, nuxt)`; `defineNuxtModule`\n * from `@nuxt/kit` is sugar around exactly that. Writing the function keeps\n * `@nuxt/kit` out of the dependency list — this package adapts Nuxt, it does not\n * build with it — and keeps the module readable as the two things it does:\n * take the options, and register a Nitro plugin written from them.\n *\n * The plugin is generated rather than shipped. The module runs at build time and\n * the plugin runs per request in Nitro's own bundle, so the options cannot travel\n * as a closure — they have to be written as source. Nuxt already has the\n * mechanism: a build template, written into `.nuxt/` and registered by its path,\n * which is what `resolveNitroPath` wants (it resolves a plugin entry against\n * `srcDir`, so a bare specifier would be looked for under `server/`).\n *\n * That the options are serialized is why `authorizePreview` and `shouldInject`\n * are not part of this type: a function does not survive `JSON.stringify`. It\n * is also why the example sets `defaults: 'v1'`: the generated plugin builds\n * its policy when Nitro loads it, and the strict 2.0 default throws there\n * without `authorizePreview`. Under `'v1'` (or `strict: false`) the plugin\n * injects on client-controlled intent alone. A preview that needs either\n * function registers `livePreviewNitroPlugin()` by hand, exactly as before\n * (docs/nuxt.md).\n */\n\nimport { join } from 'node:path';\nimport type { PreviewAdapterOptions } from '@adapters/shared/options';\nimport type { PreviewRequestLike } from '@adapters/shared/preview-request';\n\n/** What the module accepts, from `livePreview` in `nuxt.config.ts` or from `modules: [[..., options]]`. */\nexport type LivePreviewModuleOptions = Omit<\n PreviewAdapterOptions<PreviewRequestLike>,\n 'authorizePreview' | 'shouldInject'\n>;\n\n/** The Nitro config slice this module writes into. */\nexport interface NitroConfigLike {\n plugins?: string[];\n}\n\n/** The one build template shape this module adds; Nuxt writes it to `buildDir/filename`. */\nexport interface NuxtTemplateLike {\n readonly filename: string;\n readonly write: boolean;\n readonly getContents: () => string;\n}\n\n/**\n * The two config slots and the one hook this module touches, duck-typed. Nuxt's\n * own types would mean a build-time dependency on `nuxt` for everyone who\n * installs this package.\n */\nexport interface NuxtLike {\n readonly options: {\n readonly buildDir: string;\n readonly build: { templates: NuxtTemplateLike[] };\n livePreview?: LivePreviewModuleOptions;\n };\n readonly hook: (name: 'nitro:config', handler: (config: NitroConfigLike) => void) => void;\n}\n\n/** The generated plugin's name in `.nuxt/`, distinctive enough to recognise in a build listing. @internal */\nexport const PLUGIN_FILENAME = 'payload-live-preview-nitro-plugin.mjs';\n\n/**\n * The generated plugin: an import of the public adapter entry and a call with\n * the options. Nothing framework-private, so a reader who opens the file in\n * `.nuxt/` sees the hand-written setup they would otherwise have typed.\n * @internal\n */\nexport function pluginSource(options: LivePreviewModuleOptions): string {\n return [\n \"import { livePreviewNitroPlugin } from 'payload-live-preview/nuxt';\",\n '',\n `export default livePreviewNitroPlugin(${JSON.stringify(options)});`,\n '',\n ].join('\\n');\n}\n\n/**\n * The module itself. Options come from `livePreview` in the Nuxt config, from\n * the module's own inline options, or from both — the inline ones win, because\n * they are the more specific place to write them.\n */\nexport default function livePreviewModule(\n inlineOptions: LivePreviewModuleOptions | undefined,\n nuxt: NuxtLike,\n): void {\n const options: LivePreviewModuleOptions = {\n ...(nuxt.options.livePreview ?? {}),\n ...(inlineOptions ?? {}),\n };\n nuxt.options.build.templates.push({\n filename: PLUGIN_FILENAME,\n // Nitro reads the plugin from disk, not from Nuxt's virtual file system.\n write: true,\n getContents: () => pluginSource(options),\n });\n const plugin = join(nuxt.options.buildDir, PLUGIN_FILENAME);\n nuxt.hook('nitro:config', (config) => {\n const plugins = (config.plugins ??= []);\n // Nuxt re-runs modules on a config reload; registering twice would inject\n // the runtime twice into every page.\n if (!plugins.includes(plugin)) plugins.push(plugin);\n });\n}\n\n/** Nuxt reads this off the module function to name it in build output and errors. */\nlivePreviewModule.meta = {\n name: 'payload-live-preview',\n configKey: 'livePreview',\n};\n"],"mappings":"4BA2EO,IAAMA,EAAkB,wCAQxB,SAASC,EAAaC,GAC3B,MAAO,CACL,sEACA,GACA,yCAAyCC,KAAKC,UAAUF,OACxD,IACAG,KAAK,KACT,CAOe,SAARC,EACLC,EACAC,GAEA,MAAMN,EAAoC,IACpCM,EAAKN,QAAQO,aAAe,MAC5BF,GAAiB,IAEvBC,EAAKN,QAAQQ,MAAMC,UAAUC,KAAK,CAChCC,SAAUb,EAEVc,OAAO,EACPC,YAAa,IAAMd,EAAaC,KAElC,MAAMc,EAASX,EAAKG,EAAKN,QAAQe,SAAUjB,GAC3CQ,EAAKU,KAAK,eAAiBC,IACzB,MAAMC,EAAWD,EAAOC,UAAY,GAG/BA,EAAQC,SAASL,IAASI,EAAQR,KAAKI,IAEhD,CAGAV,EAAkBgB,KAAO,CACvBC,KAAM,uBACNC,UAAW,sB","ignoreList":[]}
@@ -69,9 +69,9 @@ declare function registerRouteRefresh(refresh: RouteRefresh): () => void;
69
69
 
70
70
  /**
71
71
  * The React hook: Payload's `useLivePreview` shape, with this package's merge
72
- * underneath — one request at a time, the newer one winning, an HTTP error
73
- * refused instead of becoming the document, and a cache that belongs to this
74
- * hook alone (ADR 0002).
72
+ * underneath — one request per accepted update, a newer one aborting the one in
73
+ * flight, an HTTP error refused instead of becoming the document, and a session
74
+ * that belongs to this hook alone (ADR 0002).
75
75
  *
76
76
  * It re-renders your component tree, so conditional sections and derived values
77
77
  * are as correct as a server render. What it cannot do is keep the visitor's
@@ -82,8 +82,9 @@ declare function registerRouteRefresh(refresh: RouteRefresh): () => void;
82
82
  * loads it, and `react-dom/server`, only at the first render that needs them.
83
83
  *
84
84
  * The published file starts with `'use client'`. It is not written here because
85
- * esbuild drops a module directive when it bundles; the build adds it back as a
86
- * banner (`DIRECTIVE_ENTRIES` in scripts/package-entries.ts).
85
+ * esbuild drops a module directive when it bundles, and a banner with it; the
86
+ * build writes it back through terser's `preamble` (`DIRECTIVE_ENTRIES` in
87
+ * scripts/package-entries.ts, applied in scripts/build-dist.ts).
87
88
  */
88
89
 
89
90
  interface UseLivePreviewDocumentOptions<T> extends DocumentSessionOptions {
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["observeThenableResult","value","then","Promise","resolve","catch","noopDiagnostic","isPathSegment","maxLength","length","includes","character","codePoint","codePointAt","hasControlCharacter","isSafeSlug","DataMerger","serverURL","apiRoute","depth","fetchFn","log","inflight","attempt","destroyDepth","constructor","options","this","end","endsWith","slice","withoutTrailingSlashes","route","diagnostic","startsWith","args","canMerge","request","endpointOf","globalSlug","encodeURIComponent","collectionSlug","id","data","Number","isFinite","String","merge","status","previous","controller","superseded","abort","endpoint","fetch","AbortController","url","response","method","credentials","headers","body","JSON","stringify","flattenLocales","locale","signal","ok","merged","json","Array","isArray","doc","error","destroy","parseFieldSchema","raw","out","entry","parsed","parseFieldEntry","push","record","name","type","fields","blocks","slug","parseBlockSchema","result","optionalFieldOk","check","optionalTypeOk","isPlainObject","prototype","Object","getPrototypeOf","NULL_AS_ABSENT_FIELDS","MessageBus","matcher","handlers","boundListener","attachedTarget","generation","revision","queueHead","queueTail","attach","target","window","committed","listener","event","receive","origin","reportInvalid","resetQueue","addEventListener","removeEventListener","detach","advanceGeneration","sendReady","targets","origins","payload","ready","protocolVersion","postMessage","matchesOrigin","matchesSource","v","x","isLivePreviewMessage","dispatchUpdate","message","normalized","field","Reflect","get","deleteProperty","fieldSchemaJSON","normalizeLivePreviewMessage","isDocumentEventMessage","invokeHandler","onDocumentEvent","focusFieldOf","onFocusField","hasUpdateData","isCurrentGeneration","messageRevision","validator","validateToken","commitUpdate","pending","verdict","next","enqueue","previewToken","settleValidation","approved","drainQueue","dequeue","onUpdate","reason","onInvalid","sourcePolicy","source","parent","opener","handler","expected","LOCALHOST_PATTERN","VALID_ORIGIN_PATTERN","LOCALHOST_HANDSHAKE_PORTS","OriginDetector","state","explicit","Set","additionalOrigins","normalised","normaliseOrigin","add","referrerOrigin","enableReferrerDetection","referrer","document","URL","allowLocalhost","enableLocalhostMatching","forceDevMode","isDevMode","nodeEnv","process","env","readNodeEnv","host","location","hostname","matches","has","size","test","lockOrigin","unlockOrigin","lockedOrigin","enumerate","port","referrerWasAvailable","isProductionUnconfigured","isInIframe","self","top","isReferrerOnlyTrust","trimmed","trim","protocol","reasonFrom","line","label","detail","Error","DocumentSession","merger","bus","detector","listeners","initial","snapshot","lastLog","attached","initialData","allowedOrigins","handle","eventSourcePolicy","isLoading","subscribe","delete","getSnapshot","getServerSnapshot","readyTargets","publish","current","SLOT","registerRouteRefresh","refresh","settle","waiting","settled","LivePreviewRouteRefresh","startTransition","useTransition","useRef","useEffect","undo","useLivePreviewDocument","session","key","join","held","useSessionFor","useSyncExternalStore"],"sources":["../../../src/core/thenable.ts","../../../src/core/diagnostics.ts","../../../src/core/data-merger.ts","../../../src/schema/parser.ts","../../../src/core/message-guards.ts","../../../src/core/message-bus.ts","../../../src/core/protocol-version.ts","../../../src/detection/origin.ts","../../../src/detection/environment.ts","../../../src/adapters/shared/document-session.ts","../../../src/core/route-refresh.ts","../../../src/adapters/react/route-refresh.ts","../../../src/adapters/react/index.ts"],"sourcesContent":["/**\n * Detect an invalid async result in a synchronous extension point.\n *\n * Rejected native Promises otherwise become unhandled after the caller falls\n * back synchronously. Assimilating the result through a handled Promise keeps\n * that contract violation observable only through the runtime's explicit\n * error channel.\n */\nexport function observeThenableResult(value: unknown): boolean {\n if ((typeof value !== 'object' || value === null) && typeof value !== 'function') return false;\n if (typeof (value as { readonly then?: unknown }).then !== 'function') return false;\n void Promise.resolve(value).catch(() => undefined);\n return true;\n}\n","import { observeThenableResult } from './thenable';\n\n/**\n * Diagnostics are observability, never control flow: a consumer logger may\n * throw or return a rejected thenable, and reporting a failure must not become\n * a second failure. Invocation stays synchronous for ordinary loggers.\n */\nexport function isolateDiagnostic(\n diagnostic: (...args: unknown[]) => unknown,\n): (...args: unknown[]) => void {\n return (...args): void => {\n try {\n observeThenableResult(diagnostic(...args));\n } catch {\n // Diagnostics must not become a second failure boundary.\n }\n };\n}\n\n/** Shared allocation-free default for disabled diagnostic channels. */\nexport function noopDiagnostic(): void {\n // Intentionally empty.\n}\n\n// Access, invocation and thenable inspection all happen inside the wrapper, so\n// a missing or hostile console cannot escape.\nexport const safeConsoleError = isolateDiagnostic((...args) =>\n (console.error as (...values: unknown[]) => unknown)(...args),\n);\n\nexport const safeConsoleWarn = isolateDiagnostic((...args) =>\n (console.warn as (...values: unknown[]) => unknown)(...args),\n);\n\nexport const safeConsoleDebug = isolateDiagnostic((...args) =>\n // eslint-disable-next-line no-console -- best-effort development diagnostics\n (console.debug as (...values: unknown[]) => unknown)(...args),\n);\n","/**\n * Server-side merge for Payload 3.x, whose admin posts raw form values with\n * relationships as bare ids. Like the official client, the update is re-fetched\n * through the REST API with `X-Payload-HTTP-Method-Override: GET`, which\n * merges the form values into the stored document and returns it populated.\n * Only the newest request matters: a newer merge aborts the one in flight.\n */\n\nimport { isolateDiagnostic, noopDiagnostic } from './diagnostics';\n\nexport interface DataMergerOptions {\n /** Payload server origin, e.g. `https://cms.example.com`. */\n readonly serverURL: string;\n /** REST route prefix. Defaults to `/api`. */\n readonly apiRoute?: string;\n /** Population depth requested from the server. Defaults to `1`. */\n readonly depth?: number;\n readonly fetchFn?: typeof fetch;\n readonly log?: (...args: unknown[]) => void;\n}\n\nexport interface MergeRequest {\n readonly collectionSlug?: string | undefined;\n readonly globalSlug?: string | undefined;\n readonly data: Record<string, unknown>;\n readonly locale?: string | undefined;\n}\n\n/** `superseded`: a newer merge replaced this one. `unavailable`: fall back to the raw values. */\nexport type MergeResult =\n | { readonly status: 'merged'; readonly doc: Record<string, unknown> }\n | { readonly status: 'superseded' }\n | { readonly status: 'unavailable' };\n\nfunction hasControlCharacter(value: string): boolean {\n for (const character of value) {\n // An iterated character always has a code point; 0 keeps the type and fails closed.\n const codePoint = character.codePointAt(0) ?? 0;\n if (codePoint <= 0x1f || codePoint === 0x7f) return true;\n }\n return false;\n}\n\n/** One path segment: non-empty, bounded, not a dot segment, without separators or control characters. */\nfunction isPathSegment(value: string, maxLength: number): boolean {\n return (\n value.length > 0 &&\n value.length <= maxLength &&\n value !== '.' &&\n value !== '..' &&\n !value.includes('/') &&\n !value.includes('\\\\') &&\n !hasControlCharacter(value)\n );\n}\n\n/** A slug is one of Payload's own names; `?` and `#` never belong in one. */\nfunction isSafeSlug(value: string): boolean {\n return isPathSegment(value, 128) && !value.includes('?') && !value.includes('#');\n}\n\n/** An id comes from the form values; a string one is encoded, so `?` and `#` may appear. */\nfunction isSafeId(value: unknown): value is string | number {\n if (typeof value === 'number') return Number.isFinite(value);\n return typeof value === 'string' && isPathSegment(value, 512);\n}\n\n/**\n * Trailing slashes off a base URL, without a regular expression: `/\\/+$/`\n * backtracks over a long run of slashes and takes quadratic time on input the\n * page supplies (CodeQL js/polynomial-redos).\n */\nfunction withoutTrailingSlashes(value: string): string {\n let end = value.length;\n while (value.endsWith('/', end)) end -= 1;\n return value.slice(0, end);\n}\n\nexport class DataMerger {\n private readonly serverURL: string;\n private readonly apiRoute: string;\n private readonly depth: number;\n private readonly fetchFn: typeof fetch | undefined;\n private readonly log: (...args: unknown[]) => void;\n private inflight: AbortController | null = null;\n private attempt = 0;\n private destroyDepth = 0;\n\n constructor(options: DataMergerOptions) {\n this.serverURL = withoutTrailingSlashes(options.serverURL);\n const route = options.apiRoute ?? '/api';\n this.apiRoute = route.startsWith('/') ? route : `/${route}`;\n this.depth = options.depth ?? 1;\n this.fetchFn = options.fetchFn;\n this.log = isolateDiagnostic(options.log ?? noopDiagnostic);\n }\n\n /** Collections need an `id` in the form values; globals only their slug. */\n canMerge(request: MergeRequest): boolean {\n return this.endpointOf(request) !== null;\n }\n\n /**\n * The path under `apiRoute`, or `null` when no safe one exists. A request\n * accessor that throws counts as none, and so does a lone surrogate, which\n * `encodeURIComponent` refuses with a `URIError`.\n */\n private endpointOf(request: MergeRequest): string | null {\n try {\n // An empty global slug means \"collection document\" on the wire.\n const globalSlug = request.globalSlug;\n if (globalSlug) {\n return isSafeSlug(globalSlug) ? `globals/${encodeURIComponent(globalSlug)}` : null;\n }\n const collectionSlug = request.collectionSlug ?? '';\n const id: unknown = request.data['id'];\n if (!isSafeSlug(collectionSlug) || !isSafeId(id)) return null;\n return `${encodeURIComponent(collectionSlug)}/${encodeURIComponent(String(id))}`;\n } catch {\n return null;\n }\n }\n\n async merge(request: MergeRequest): Promise<MergeResult> {\n if (this.destroyDepth > 0) return { status: 'superseded' };\n // Claim the attempt before aborting: abort listeners and fetch shims are\n // reentrant, and a newer merge they start must win. Nothing but this class\n // aborts the signal, and only after the counter moved, so the counter\n // alone decides who was superseded.\n const attempt = (this.attempt += 1);\n const previous = this.inflight;\n this.inflight = null;\n let controller: AbortController | null = null;\n const superseded = (): boolean => this.attempt !== attempt;\n try {\n previous?.abort();\n if (superseded()) return { status: 'superseded' };\n // The endpoint reads the request's own values, which may be consumer\n // getters that start a newer merge before throwing.\n const endpoint = this.endpointOf(request);\n if (endpoint === null) {\n return superseded() ? { status: 'superseded' } : { status: 'unavailable' };\n }\n const fetchFn = this.fetchFn ?? (typeof fetch === 'function' ? fetch : undefined);\n if (fetchFn === undefined) return { status: 'unavailable' };\n controller = new AbortController();\n this.inflight = controller;\n const url = `${this.serverURL}${this.apiRoute}/${endpoint}`;\n const response = await fetchFn(url, {\n method: 'POST',\n credentials: 'include',\n headers: {\n 'Content-Type': 'application/json',\n 'X-Payload-HTTP-Method-Override': 'GET',\n },\n // The admin already flattens locales before posting; an absent locale\n // is left out of the JSON.\n body: JSON.stringify({\n data: request.data,\n depth: this.depth,\n flattenLocales: false,\n locale: request.locale,\n }),\n signal: controller.signal,\n });\n // A fetch shim may ignore the signal; the attempt is authoritative.\n if (superseded()) return { status: 'superseded' };\n if (!response.ok) {\n this.log('merge HTTP', response.status, url);\n return { status: 'unavailable' };\n }\n const merged: unknown = await response.json();\n if (superseded()) return { status: 'superseded' };\n if (merged === null || typeof merged !== 'object' || Array.isArray(merged)) {\n this.log('merge invalid', url);\n return { status: 'unavailable' };\n }\n return { status: 'merged', doc: merged as Record<string, unknown> };\n } catch (error) {\n if (superseded()) return { status: 'superseded' };\n this.log('merge exception', error);\n return { status: 'unavailable' };\n } finally {\n // Only the newest attempt's controller is ever in flight: a superseded\n // attempt's is gone already, and its successor's is left alone.\n if (this.inflight === controller) this.inflight = null;\n }\n }\n\n /** Abort any in-flight merge. The merger stays usable across a stop/start cycle. */\n destroy(): void {\n this.attempt += 1;\n const inflight = this.inflight;\n this.inflight = null;\n this.destroyDepth += 1;\n try {\n inflight?.abort();\n } finally {\n this.destroyDepth -= 1;\n }\n }\n}\n","/**\n * Validates the `fieldSchemaJSON` the admin sends. Entries that fail\n * validation are dropped, never thrown: a Payload/runtime version mismatch\n * must not take the preview down.\n */\n\nimport type { PayloadBlockSchema, PayloadFieldSchema } from '@/types/payload-protocol';\n\nexport function parseFieldSchema(raw: unknown): readonly PayloadFieldSchema[] {\n if (!Array.isArray(raw)) return [];\n const out: PayloadFieldSchema[] = [];\n for (const entry of raw) {\n const parsed = parseFieldEntry(entry);\n if (parsed) out.push(parsed);\n }\n return out;\n}\n\nfunction parseFieldEntry(value: unknown): PayloadFieldSchema | undefined {\n if (typeof value !== 'object' || value === null) return undefined;\n const record = value as Record<string, unknown>;\n const name = record['name'];\n const type = record['type'];\n if (typeof name !== 'string' || name.length === 0) return undefined;\n if (typeof type !== 'string' || type.length === 0) return undefined;\n\n const fields = parseFieldSchema(record['fields']);\n const blocks = parseBlockSchema(record['blocks']);\n\n const result: Record<string, unknown> = { ...record, name, type };\n if (fields.length > 0) result['fields'] = fields;\n else delete result['fields'];\n if (blocks.length > 0) result['blocks'] = blocks;\n else delete result['blocks'];\n\n return result as unknown as PayloadFieldSchema;\n}\n\nfunction parseBlockSchema(raw: unknown): readonly PayloadBlockSchema[] {\n if (!Array.isArray(raw)) return [];\n const out: PayloadBlockSchema[] = [];\n for (const entry of raw) {\n if (typeof entry !== 'object' || entry === null) continue;\n const record = entry as Record<string, unknown>;\n const slug = record['slug'];\n if (typeof slug !== 'string' || slug.length === 0) continue;\n const fields = parseFieldSchema(record['fields']);\n out.push({ ...record, slug, fields });\n }\n return out;\n}\n","/** Shape guards for the messages the bus accepts. `null` counts as absent, as JSON bridges send it. */\n\nimport type {\n PayloadDocumentEventMessage,\n PayloadFieldSchema,\n PayloadLivePreviewMessage,\n} from '@/types/payload-protocol';\nimport { parseFieldSchema } from '@schema/parser';\n\nexport function isObjectMessage(value: unknown): value is { type: string } {\n return (\n typeof value === 'object' && value !== null && 'type' in value && typeof value.type === 'string'\n );\n}\n\n/** A `payload-live-preview` message: `data` a plain object when present, scalars of the right type or absent. */\nexport function isLivePreviewMessage(value: { type: string }): value is PayloadLivePreviewMessage {\n const v = value as Record<string, unknown>;\n if (v['data'] !== undefined && v['data'] !== null && !isPlainObject(v['data'])) return false;\n return (\n optionalTypeOk(v['fieldSchemaJSON'], (x) => Array.isArray(x)) &&\n optionalTypeOk(v['globalSlug'], (x) => typeof x === 'string') &&\n optionalTypeOk(v['collectionSlug'], (x) => typeof x === 'string') &&\n optionalTypeOk(v['locale'], (x) => typeof x === 'string') &&\n optionalTypeOk(v['ready'], (x) => typeof x === 'boolean') &&\n optionalTypeOk(v['previewToken'], (x) => typeof x === 'string') &&\n optionalTypeOk(v['protocolVersion'], (x) => typeof x === 'number')\n );\n}\n\n/** The non-empty field name of a `payload-live-preview-focus` message, else `undefined`. */\nexport function focusFieldOf(value: { type: string }): string | undefined {\n const field = (value as Record<string, unknown>)['field'];\n return typeof field === 'string' && field.length > 0 ? field : undefined;\n}\n\n/** Stock Payload sends only the `type`; custom senders may add the typed fields (omitted, never `null`). */\nexport function isDocumentEventMessage(value: {\n type: string;\n}): value is PayloadDocumentEventMessage {\n const v = value as Record<string, unknown>;\n return (\n optionalFieldOk(v['action'], (x) => x === 'updated' || x === 'created' || x === 'deleted') &&\n optionalFieldOk(v['slug'], (x) => typeof x === 'string') &&\n optionalFieldOk(v['id'], (x) => typeof x === 'string' || Number.isFinite(x))\n );\n}\n\nfunction optionalFieldOk(value: unknown, check: (v: unknown) => boolean): boolean {\n return value === undefined || check(value);\n}\n\nfunction optionalTypeOk(value: unknown, check: (v: unknown) => boolean): boolean {\n return value === undefined || value === null || check(value);\n}\n\nexport function isPlainObject(value: unknown): value is Record<string, unknown> {\n if (typeof value !== 'object' || value === null) return false;\n const prototype = Object.getPrototypeOf(value) as object | null;\n return prototype === null || Object.getPrototypeOf(prototype) === null;\n}\n\nconst NULL_AS_ABSENT_FIELDS = [\n 'data',\n 'fieldSchemaJSON',\n 'globalSlug',\n 'collectionSlug',\n 'locale',\n 'ready',\n 'previewToken',\n 'protocolVersion',\n] as const satisfies readonly (keyof PayloadLivePreviewMessage)[];\n\n/**\n * Drop `null` optionals so downstream sees the strict public type, and parse\n * the field schema entry by entry (malformed entries are dropped). Unknown\n * fields stay, for protocol evolution.\n */\nexport function normalizeLivePreviewMessage(\n message: PayloadLivePreviewMessage,\n): PayloadLivePreviewMessage {\n const normalized = { ...message };\n for (const field of NULL_AS_ABSENT_FIELDS) {\n const wireValue: unknown = Reflect.get(normalized, field);\n if (wireValue === null) Reflect.deleteProperty(normalized, field);\n }\n if (!Array.isArray(normalized.fieldSchemaJSON)) return normalized;\n let fieldSchemaJSON: readonly PayloadFieldSchema[];\n try {\n fieldSchemaJSON = parseFieldSchema(normalized.fieldSchemaJSON);\n } catch {\n fieldSchemaJSON = [];\n }\n return { ...normalized, fieldSchemaJSON };\n}\n","/**\n * postMessage ingress: origin and source checks, per-type shape guards, the\n * ordered preview-token validation queue, and the `ready` handshake. The bus\n * never throws; a rejected message is dropped or reported via `onInvalid`.\n * Consumer callbacks may detach or re-attach the bus synchronously, so\n * anything that runs after one re-checks the generation it started in.\n * See ADR 0004.\n */\n\nimport type {\n PayloadDocumentEventMessage,\n PayloadLivePreviewMessage,\n} from '@/types/payload-protocol';\nimport {\n focusFieldOf,\n isDocumentEventMessage,\n isLivePreviewMessage,\n isObjectMessage,\n isPlainObject,\n normalizeLivePreviewMessage,\n} from './message-guards';\nimport { LIBRARY_PROTOCOL_VERSION } from './protocol-version';\nimport { observeThenableResult } from './thenable';\n\nexport type OriginMatcher = (origin: string) => boolean;\n\n/** Identity of a data-bearing update: `revision` is monotonic, `generation` names the listener attachment. */\nexport interface MessageRevision {\n readonly revision: number;\n readonly generation: number;\n}\nexport function sameRevision(a: MessageRevision, b: MessageRevision): boolean {\n return a.generation === b.generation && a.revision === b.revision;\n}\n\nexport type InvalidReason = 'origin' | 'shape' | 'type' | 'token' | 'source';\n\nexport interface MessageHandlers {\n /** A validated update; data-bearing ones carry their revision, ready handshakes do not. */\n readonly onUpdate: (\n msg: PayloadLivePreviewMessage,\n origin: string,\n messageRevision?: MessageRevision,\n ) => void;\n readonly onDocumentEvent: (msg: PayloadDocumentEventMessage, origin: string) => void;\n /** The admin reports the cursor moved into a field (reveal tier 2). */\n readonly onFocusField?: (field: string, origin: string) => void;\n readonly onInvalid?: (reason: InvalidReason, origin: string) => void;\n /** `'parent-or-opener'` refuses every window but the framing/opening one, even from an allowed origin. */\n readonly sourcePolicy?: 'any' | 'parent-or-opener';\n /**\n * Gate for data-bearing updates. Verdicts commit in arrival order, so an\n * earlier update is dispatched before a later one even when its validation\n * resolves later; an error counts as rejection.\n */\n readonly validateToken?: (\n token: string | undefined,\n origin: string,\n ) => boolean | Promise<boolean>;\n}\n\n/** One entry of the validation queue: a singly linked list in arrival order. */\ninterface PendingValidation {\n readonly generation: number;\n readonly message: PayloadLivePreviewMessage;\n readonly origin: string;\n readonly messageRevision: MessageRevision | undefined;\n /** `undefined` until the validator settled; only literal `true` approves. */\n verdict: boolean | undefined;\n next: PendingValidation | undefined;\n}\n\nexport class MessageBus {\n private readonly matcher: OriginMatcher;\n private readonly handlers: MessageHandlers;\n private boundListener: ((event: MessageEvent) => void) | undefined = undefined;\n private attachedTarget: Window | undefined = undefined;\n private generation = 0;\n private revision = 0;\n private queueHead: PendingValidation | undefined = undefined;\n private queueTail: PendingValidation | undefined = undefined;\n\n constructor(matcher: OriginMatcher, handlers: MessageHandlers) {\n this.matcher = matcher;\n this.handlers = handlers;\n }\n\n /** Start listening on `target`. Idempotent. */\n attach(target: Window = window): void {\n if (this.attachedTarget !== undefined) return;\n let committed = false;\n const listener = (event: MessageEvent): void => {\n // Each attachment owns its listener; a stale one must not borrow the new generation.\n if (!committed || this.boundListener !== listener) return;\n const generation = this.generation;\n try {\n this.receive(event, generation, target);\n } catch {\n // A synthetic event can carry throwing accessors; that is a shape failure.\n let origin: string;\n try {\n origin = event.origin;\n } catch {\n origin = '';\n }\n this.reportInvalid('shape', origin, generation);\n }\n };\n this.generation += 1;\n this.resetQueue();\n this.attachedTarget = target;\n this.boundListener = listener;\n try {\n target.addEventListener('message', listener);\n } catch (error) {\n // Invalidate before rollback: a hook may already have attached a newer generation.\n if (this.boundListener === listener) {\n this.attachedTarget = undefined;\n this.boundListener = undefined;\n this.resetQueue();\n }\n try {\n target.removeEventListener('message', listener);\n } catch {\n // Keep the original failure.\n }\n throw error;\n }\n if (this.boundListener === listener) {\n committed = true;\n } else {\n try {\n target.removeEventListener('message', listener);\n } catch {\n // The listener is identity-gated above even if removal fails.\n }\n }\n }\n\n /** Stop listening. Idempotent. */\n detach(): void {\n const target = this.attachedTarget;\n const listener = this.boundListener;\n if (target === undefined || listener === undefined) return;\n // Invalidate first so a listener already on the stack sees an obsolete generation.\n this.attachedTarget = undefined;\n this.boundListener = undefined;\n this.resetQueue();\n target.removeEventListener('message', listener);\n }\n\n /** Invalidate pending validations while staying attached (heartbeat expiry). */\n advanceGeneration(): boolean {\n if (this.attachedTarget === undefined) return false;\n this.generation += 1;\n this.resetQueue();\n return true;\n }\n\n /** Post the `ready` handshake to every target for every origin. */\n static sendReady(targets: readonly Window[], origins: readonly string[]): void {\n const payload: PayloadLivePreviewMessage = {\n type: 'payload-live-preview',\n ready: true,\n protocolVersion: LIBRARY_PROTOCOL_VERSION,\n };\n for (const target of targets) {\n for (const origin of origins) {\n try {\n target.postMessage(payload, origin);\n } catch {\n // A malformed origin is the consumer's configuration problem.\n }\n }\n }\n }\n\n private receive(event: MessageEvent, generation: number, target: Window): void {\n const origin = event.origin;\n if (!this.matchesOrigin(origin, generation)) {\n this.reportInvalid('origin', origin, generation);\n return;\n }\n if (!this.matchesSource(event, target)) {\n this.reportInvalid('source', origin, generation);\n return;\n }\n const data: unknown = event.data;\n if (!isObjectMessage(data)) {\n this.reportInvalid('shape', origin, generation);\n return;\n }\n switch (data.type) {\n case 'payload-live-preview':\n if (!isLivePreviewMessage(data)) {\n this.reportInvalid('shape', origin, generation);\n return;\n }\n this.dispatchUpdate(normalizeLivePreviewMessage(data), origin, generation);\n return;\n case 'payload-document-event':\n if (!isDocumentEventMessage(data)) {\n this.reportInvalid('shape', origin, generation);\n return;\n }\n this.invokeHandler(generation, this.handlers.onDocumentEvent, data, origin);\n return;\n case 'payload-live-preview-focus': {\n const field = focusFieldOf(data);\n if (field === undefined) {\n this.reportInvalid('shape', origin, generation);\n return;\n }\n this.invokeHandler(generation, this.handlers.onFocusField, field, origin);\n return;\n }\n default:\n this.reportInvalid('type', origin, generation);\n }\n }\n\n private dispatchUpdate(\n message: PayloadLivePreviewMessage,\n origin: string,\n generation: number,\n ): void {\n const hasUpdateData = isPlainObject(message.data);\n if (!this.isCurrentGeneration(generation)) return;\n // A shape-valid data update consumes its revision before validation; a rejected token leaves a gap.\n const messageRevision = hasUpdateData\n ? { generation, revision: (this.revision += 1) }\n : undefined;\n const validator = this.handlers.validateToken;\n if (!this.isCurrentGeneration(generation)) return;\n if (validator === undefined) {\n this.commitUpdate(message, origin, generation, messageRevision);\n return;\n }\n // The data-less ready handshake carries no token and passes, so the admin learns we listen.\n if (message.ready === true && message.data === undefined) {\n this.commitUpdate(message, origin, generation, undefined);\n return;\n }\n const pending: PendingValidation = {\n generation,\n message,\n origin,\n messageRevision,\n verdict: undefined,\n next: undefined,\n };\n this.enqueue(pending);\n let verdict: unknown;\n try {\n // Invoked eagerly; only the commit waits for earlier queue entries.\n verdict = validator(message.previewToken, origin);\n } catch {\n this.settleValidation(pending, false);\n return;\n }\n if (typeof verdict === 'boolean') {\n this.settleValidation(pending, verdict);\n return;\n }\n // Assimilate any thenable; only literal `true` approves.\n void Promise.resolve(verdict).then(\n (approved) => {\n this.settleValidation(pending, approved === true);\n },\n () => {\n this.settleValidation(pending, false);\n },\n );\n }\n\n private settleValidation(pending: PendingValidation, approved: boolean): void {\n pending.verdict = approved;\n this.drainQueue(pending.generation);\n }\n\n private drainQueue(generation: number): void {\n while (this.isCurrentGeneration(generation)) {\n const pending = this.queueHead;\n if (pending === undefined) return;\n // The matcher may have narrowed since ingress (origin lock); an obsolete\n // origin must not hold the queue head and block the locked origin's work.\n if (!this.matchesOrigin(pending.origin, generation)) {\n if (!this.dequeue(pending)) continue;\n this.reportInvalid('origin', pending.origin, pending.generation);\n continue;\n }\n if (pending.verdict === undefined) return;\n if (!this.dequeue(pending)) continue;\n if (pending.verdict) {\n this.commitUpdate(\n pending.message,\n pending.origin,\n pending.generation,\n pending.messageRevision,\n );\n } else {\n this.reportInvalid('token', pending.origin, pending.generation);\n }\n }\n }\n\n private commitUpdate(\n message: PayloadLivePreviewMessage,\n origin: string,\n generation: number,\n messageRevision: MessageRevision | undefined,\n ): void {\n if (messageRevision === undefined) {\n this.invokeHandler(generation, this.handlers.onUpdate, message, origin);\n } else {\n this.invokeHandler(generation, this.handlers.onUpdate, message, origin, messageRevision);\n }\n }\n\n private reportInvalid(reason: InvalidReason, origin: string, generation: number): void {\n this.invokeHandler(generation, this.handlers.onInvalid, reason, origin);\n }\n\n /** Under `'parent-or-opener'`, whether the event came from the attached window's parent or opener. */\n private matchesSource(event: MessageEvent, target: Window): boolean {\n if (this.handlers.sourcePolicy !== 'parent-or-opener') return true;\n try {\n // Only a window is a source: a missing one matches nothing, not even a missing opener.\n const source: unknown = event.source;\n if (typeof source !== 'object' || source === null) return false;\n const parent = target.parent;\n if (parent !== target && source === parent) return true;\n const opener: unknown = target.opener;\n return source === opener;\n } catch {\n return false;\n }\n }\n\n /** Fail-closed: only literal `true` from the matcher, and only within the generation it ran in. */\n private matchesOrigin(origin: string, generation: number): boolean {\n try {\n const approved: unknown = this.isCurrentGeneration(generation) && this.matcher(origin);\n return approved === true && this.isCurrentGeneration(generation);\n } catch {\n return false;\n }\n }\n\n /** Callbacks run only in a current generation and must not unwind the listener or the drain. */\n private invokeHandler<TArgs extends unknown[]>(\n generation: number,\n handler: ((...args: TArgs) => unknown) | undefined,\n ...args: TArgs\n ): void {\n if (handler === undefined || !this.isCurrentGeneration(generation)) return;\n try {\n observeThenableResult(handler(...args));\n } catch {\n // Isolated by contract.\n }\n }\n\n private isCurrentGeneration(generation: number): boolean {\n return this.attachedTarget !== undefined && this.generation === generation;\n }\n\n private enqueue(pending: PendingValidation): void {\n if (this.queueTail === undefined) this.queueHead = pending;\n else this.queueTail.next = pending;\n this.queueTail = pending;\n }\n\n /** Advance the head past `expected`, unless a matcher reset the queue meanwhile. */\n private dequeue(expected: PendingValidation): boolean {\n if (this.queueHead !== expected) return false;\n this.queueHead = expected.next;\n if (this.queueHead === undefined) this.queueTail = undefined;\n return true;\n }\n\n private resetQueue(): void {\n this.queueHead = undefined;\n this.queueTail = undefined;\n }\n}\n","/**\n * Protocol negotiation. A capability is granted either by the version a peer\n * announces or by observation, since the stock Payload admin announces none:\n * what it can do is read off the messages it sends. See ADR 0010.\n */\n\n/** Bumped only when this library's understanding of the wire format changes. @internal */\nexport const LIBRARY_PROTOCOL_VERSION = 4;\n\n/** How a capability becomes active. @internal */\nexport type CapabilitySource = 'version' | 'observed';\n\n/** @internal */\nexport interface CapabilityDeclaration {\n /** Enabled once the negotiated version reaches this (for announced peers). */\n readonly since: number;\n /** The wire signal that grants this capability to a peer announcing no version. */\n readonly observed?:\n 'schema' | 'locale' | 'preview-token' | 'document-event' | 'relationship-event';\n}\n\n/** What activates each capability; the prose lives in `CAPABILITY_DOCUMENTATION`. */\nexport const CAPABILITY_DECLARATIONS = {\n basic: { since: 1 },\n 'schema-json': { since: 2, observed: 'schema' },\n locale: { since: 2, observed: 'locale' },\n 'preview-token': { since: 3, observed: 'preview-token' },\n 'nested-arrays': { since: 4 },\n 'recursive-diffs': { since: 4 },\n 'document-events': { since: 4, observed: 'document-event' },\n 'relationship-events': { since: 4, observed: 'relationship-event' },\n} as const satisfies Record<string, CapabilityDeclaration>;\n\nexport type ProtocolCapability = keyof typeof CAPABILITY_DECLARATIONS;\n\n/** Every declared capability, in declaration order. @internal */\nexport const PROTOCOL_CAPABILITIES = Object.keys(\n CAPABILITY_DECLARATIONS,\n) as readonly ProtocolCapability[];\n\nexport interface ProtocolNegotiation {\n /** This library's protocol version. */\n readonly ours: number;\n /** The remote party's version, when it announced one. */\n readonly theirs: number | undefined;\n /** `min(ours, theirs ?? 1)` — the version both sides actually share. */\n readonly negotiated: number;\n /** Capabilities active at the negotiated version or seen on the wire. */\n readonly capabilities: ReadonlySet<ProtocolCapability>;\n /** The subset that was observed on the wire rather than granted by version. */\n readonly observed: ReadonlySet<ProtocolCapability>;\n}\n\n/** Deterministic; the runtime re-runs it whenever either input changes. */\nexport function negotiateProtocol(\n theirs: number | undefined,\n observed: Iterable<ProtocolCapability> = [],\n): ProtocolNegotiation {\n const sanitisedTheirs =\n typeof theirs === 'number' && Number.isFinite(theirs) && theirs >= 1\n ? Math.floor(theirs)\n : undefined;\n const effectiveTheirs = sanitisedTheirs ?? 1;\n const negotiated = Math.min(LIBRARY_PROTOCOL_VERSION, effectiveTheirs);\n const seen = new Set<ProtocolCapability>(observed);\n const capabilities = new Set<ProtocolCapability>();\n for (const capability of PROTOCOL_CAPABILITIES) {\n if (negotiated >= CAPABILITY_DECLARATIONS[capability].since || seen.has(capability)) {\n capabilities.add(capability);\n }\n }\n return {\n ours: LIBRARY_PROTOCOL_VERSION,\n ...(sanitisedTheirs !== undefined ? { theirs: sanitisedTheirs } : { theirs: undefined }),\n negotiated,\n capabilities,\n observed: seen,\n };\n}\n\n/** @internal */\nexport function hasCapability(\n negotiation: ProtocolNegotiation,\n capability: ProtocolCapability,\n): boolean {\n return negotiation.capabilities.has(capability);\n}\n\n/** The capabilities a single message demonstrates, by its shape alone. */\nexport function observeCapabilities(message: {\n readonly fieldSchemaJSON?: unknown;\n readonly locale?: unknown;\n readonly previewToken?: unknown;\n readonly externallyUpdatedRelationship?: unknown;\n}): readonly ProtocolCapability[] {\n const seen: ProtocolCapability[] = [];\n if (Array.isArray(message.fieldSchemaJSON)) seen.push('schema-json');\n if (typeof message.locale === 'string') seen.push('locale');\n if (typeof message.previewToken === 'string') seen.push('preview-token');\n if (\n typeof message.externallyUpdatedRelationship === 'object' &&\n message.externallyUpdatedRelationship !== null\n ) {\n seen.push('relationship-events');\n }\n return seen;\n}\n","/**\n * Which admin origins the runtime trusts: explicit ones always, the\n * `document.referrer` origin only when none is configured, any localhost port\n * in development. See docs/security.md §1.\n */\n\nimport { isDevMode, isInIframe } from './environment';\n\nconst LOCALHOST_PATTERN = /^https?:\\/\\/(localhost|127\\.0\\.0\\.1)(?::\\d+)?$/i;\nconst VALID_ORIGIN_PATTERN = /^https?:\\/\\/[^/?#\\s]+$/i;\n\n/** Ports the handshake broadcasts to when the localhost pattern is on; matching itself is unrestricted. */\nconst LOCALHOST_HANDSHAKE_PORTS: readonly number[] = [\n 3000, 3001, 3333, 4000, 4321, 5000, 5173, 5174, 8000, 8080, 8888, 9000,\n];\n\nexport interface OriginDetectorOptions {\n /** Explicit origins to trust. Always allowed. */\n readonly additionalOrigins?: readonly string[];\n /** Set to `false` to ignore `document.referrer`. */\n readonly enableReferrerDetection?: boolean;\n /** Set to `false` to ignore the localhost pattern even in development. */\n readonly enableLocalhostMatching?: boolean;\n /** Override dev-mode detection. */\n readonly forceDevMode?: boolean;\n /** Override `document.referrer` for tests. */\n readonly referrer?: string;\n}\n\nconst enum OriginSlot {\n ExplicitOrigins,\n ReferrerOrigin,\n AllowLocalhost,\n ReferrerWasAvailable,\n LockedOrigin,\n}\n\ntype OriginState = [\n explicitOrigins: ReadonlySet<string>,\n referrerOrigin: string | undefined,\n allowLocalhost: boolean,\n referrerWasAvailable: boolean,\n lockedOrigin: string | undefined,\n];\n\n/** The trusted-origin policy: the union of the candidate sources, narrowed to one by `lockOrigin()`. */\nexport class OriginDetector {\n readonly #state: OriginState;\n\n constructor(options: OriginDetectorOptions = {}) {\n const explicit = new Set<string>();\n for (const origin of options.additionalOrigins ?? []) {\n const normalised = normaliseOrigin(origin);\n if (normalised !== undefined) explicit.add(normalised);\n }\n let referrerOrigin: string | undefined;\n if (options.enableReferrerDetection ?? true) {\n const referrer =\n options.referrer ?? (typeof document !== 'undefined' ? document.referrer : '');\n if (referrer.length > 0) {\n try {\n const origin = new URL(referrer).origin;\n if (origin && origin !== 'null') referrerOrigin = origin;\n } catch {\n // opaque referrer\n }\n }\n }\n const allowLocalhost =\n (options.enableLocalhostMatching ?? true) && (options.forceDevMode ?? isDevMode());\n this.#state = [\n explicit,\n referrerOrigin,\n allowLocalhost,\n referrerOrigin !== undefined,\n undefined,\n ];\n }\n\n /** Whether `origin` is trusted: the locked origin alone once locked, otherwise the policy above. */\n matches(origin: string): boolean {\n if (origin.length === 0 || origin === 'null') return false;\n if (this.#state[OriginSlot.LockedOrigin] !== undefined) {\n return origin === this.#state[OriginSlot.LockedOrigin];\n }\n if (this.#state[OriginSlot.ExplicitOrigins].has(origin)) return true;\n // The referrer is a fallback, not a union member: any embedder lands there.\n if (\n this.#state[OriginSlot.ExplicitOrigins].size === 0 &&\n this.#state[OriginSlot.ReferrerOrigin] !== undefined &&\n origin === this.#state[OriginSlot.ReferrerOrigin]\n ) {\n return true;\n }\n if (this.#state[OriginSlot.AllowLocalhost] && LOCALHOST_PATTERN.test(origin)) return true;\n return false;\n }\n\n /** Narrow trust to `origin`; refused (`false`) when it is not currently trusted. */\n lockOrigin(origin: string): boolean {\n if (!this.matches(origin)) return false;\n this.#state[OriginSlot.LockedOrigin] = origin;\n return true;\n }\n\n /** Release the lock (heartbeat timeout) so another allow-listed origin may reconnect. Returns the previous lock. */\n unlockOrigin(): string | undefined {\n const previous = this.#state[OriginSlot.LockedOrigin];\n this.#state[OriginSlot.LockedOrigin] = undefined;\n return previous;\n }\n\n get lockedOrigin(): string | undefined {\n return this.#state[OriginSlot.LockedOrigin];\n }\n\n /** Every origin the `ready` handshake is broadcast to; the localhost pattern expands to the common dev ports. */\n enumerate(): string[] {\n if (this.#state[OriginSlot.LockedOrigin] !== undefined) {\n return [this.#state[OriginSlot.LockedOrigin]];\n }\n const result = new Set<string>(this.#state[OriginSlot.ExplicitOrigins]);\n if (\n this.#state[OriginSlot.ExplicitOrigins].size === 0 &&\n this.#state[OriginSlot.ReferrerOrigin] !== undefined\n ) {\n result.add(this.#state[OriginSlot.ReferrerOrigin]);\n }\n if (this.#state[OriginSlot.AllowLocalhost]) {\n for (const port of LOCALHOST_HANDSHAKE_PORTS) {\n result.add(`http://localhost:${String(port)}`);\n result.add(`http://127.0.0.1:${String(port)}`);\n }\n }\n return [...result];\n }\n\n /** Whether `document.referrer` contributed an origin (absent under `Referrer-Policy: no-referrer`). */\n get referrerWasAvailable(): boolean {\n return this.#state[OriginSlot.ReferrerWasAvailable];\n }\n\n /** Framed, outside development, with no trusted origin at all: the host should warn (LP0101). */\n get isProductionUnconfigured(): boolean {\n if (!isInIframe()) return false;\n if (this.#state[OriginSlot.ExplicitOrigins].size > 0) return false;\n if (this.#state[OriginSlot.AllowLocalhost]) return false;\n if (this.#state[OriginSlot.ReferrerOrigin] !== undefined) return false;\n return true;\n }\n\n /** The referrer is the only trust source, so any framing site may drive the preview (LP0102). */\n get isReferrerOnlyTrust(): boolean {\n if (this.#state[OriginSlot.ExplicitOrigins].size > 0) return false;\n if (this.#state[OriginSlot.AllowLocalhost]) return false;\n return this.#state[OriginSlot.ReferrerOrigin] !== undefined;\n }\n}\n\n/** The canonical `http(s)://host[:port]` form of `value`, or `undefined` when it is not one. */\nexport function normaliseOrigin(value: string): string | undefined {\n const trimmed = value.trim();\n if (trimmed.length === 0) return undefined;\n try {\n const url = new URL(trimmed);\n if (url.protocol !== 'http:' && url.protocol !== 'https:') return undefined;\n if (!VALID_ORIGIN_PATTERN.test(url.origin)) return undefined;\n return url.origin;\n } catch {\n return undefined;\n }\n}\n","/**\n * Where the runtime is running: framed, opened, or on a developer's machine.\n * Nothing reads bundler-injected variables; a Vite consumer passes\n * `import.meta.env.X` explicitly (docs/astro.md).\n * @internal\n */\n\nexport function isInIframe(): boolean {\n if (typeof window === 'undefined') return false;\n try {\n return window.self !== window.top;\n } catch {\n // A cross-origin parent throws on the comparison; that is an iframe.\n return true;\n }\n}\n\n/** @internal */\nexport function isInPopup(): boolean {\n if (typeof window === 'undefined') return false;\n return window.opener != null;\n}\n\n/** An iframe or a popup; top-level navigation never starts the preview. @internal */\nexport function isInPreviewContext(): boolean {\n return isInIframe() || isInPopup();\n}\n\n/** Under Node, `NODE_ENV !== 'production'`; in a browser the hostname, which is the only signal a bundle has. @internal */\nexport function isDevMode(): boolean {\n const nodeEnv = readNodeEnv();\n if (nodeEnv !== undefined) return nodeEnv !== 'production';\n if (typeof window !== 'undefined') {\n const host = window.location.hostname;\n return host === 'localhost' || host === '127.0.0.1';\n }\n return false;\n}\n\nfunction readNodeEnv(): string | undefined {\n if (typeof process === 'undefined') return undefined;\n const value = process.env['NODE_ENV'];\n return typeof value === 'string' ? value : undefined;\n}\n","/**\n * The document a framework hook exposes, kept outside any framework: the\n * message bus decides which windows may speak, `DataMerger` turns the admin's\n * raw form values back into a populated document, and this class holds the one\n * snapshot a `useSyncExternalStore`-shaped API reads.\n *\n * It touches no DOM. A hook that re-renders a component tree needs the data,\n * not the bindings — everything about elements lives in the runtime instead.\n *\n * The session is per instance (ADR 0002): two hooks on one page have two\n * mergers, two revisions and two snapshots, and neither can hand the other a\n * document it never asked for.\n */\n\nimport { DataMerger } from '@core/data-merger';\nimport { MessageBus } from '@core/message-bus';\nimport { OriginDetector } from '@detection/origin';\nimport type { PayloadLivePreviewMessage } from '@/types/payload-protocol';\n\n/**\n * `idle` — connected, nothing received yet. `live` — the newest update was\n * merged and is in `data`. `unavailable` — it could not be, and `data` is the\n * last good document rather than a half-merged one.\n */\nexport type DocumentStatus = 'idle' | 'live' | 'unavailable';\n\nexport interface DocumentSnapshot<T> {\n /** The newest document that merged; never a partially applied one. */\n readonly data: T;\n /** No update has settled yet, or a merge is in flight. */\n readonly isLoading: boolean;\n readonly status: DocumentStatus;\n /** Why the last update did not merge; `undefined` while `status` is not `unavailable`. */\n readonly error: Error | undefined;\n}\n\nexport interface DocumentSessionOptions {\n /** Payload server origin the update is re-fetched from. */\n readonly serverURL: string;\n /** REST route prefix. Default `/api`. */\n readonly apiRoute?: string;\n /** Population depth for the merge. Default `1`, as Payload's own hook. */\n readonly depth?: number;\n /**\n * Admin origins allowed to post updates. Without one the session falls back\n * to the localhost matcher in development and trusts nothing in production.\n */\n readonly allowedOrigins?: readonly string[];\n /** `'parent-or-opener'` (default) refuses every window but the framing or opening one. */\n readonly eventSourcePolicy?: 'any' | 'parent-or-opener';\n /** Trust `document.referrer` as an origin source. Default `false`. */\n readonly enableReferrerDetection?: boolean;\n /** Match `localhost` origins in development. Default `true`. */\n readonly enableLocalhostMatching?: boolean;\n /** Injected for tests. */\n readonly fetchFn?: typeof fetch;\n /** Window to listen on. Default the global one. */\n readonly target?: Window;\n}\n\ntype Listener = () => void;\n\n/** What the merger logged for the attempt that failed, turned into one sentence. */\nfunction reasonFrom(line: readonly unknown[]): string {\n const [label, detail] = line;\n if (label === 'merge HTTP') return `the server answered ${String(detail)}`;\n if (label === 'merge invalid') return 'the server answered with something other than a document';\n if (label === 'merge exception') {\n return detail instanceof Error ? detail.message : 'the request failed';\n }\n return 'the update could not be merged';\n}\n\nexport class DocumentSession<T> {\n readonly #merger: DataMerger;\n readonly #bus: MessageBus;\n readonly #detector: OriginDetector;\n readonly #target: Window | undefined;\n readonly #listeners = new Set<Listener>();\n readonly #initial: DocumentSnapshot<T>;\n #snapshot: DocumentSnapshot<T>;\n /** Newest handled message; an older merge that settles later is ignored. */\n #revision = 0;\n #lastLog: readonly unknown[] = [];\n #attached = false;\n\n constructor(initialData: T, options: DocumentSessionOptions) {\n this.#detector = new OriginDetector({\n ...(options.allowedOrigins !== undefined\n ? { additionalOrigins: options.allowedOrigins }\n : {}),\n enableReferrerDetection: options.enableReferrerDetection ?? false,\n ...(options.enableLocalhostMatching !== undefined\n ? { enableLocalhostMatching: options.enableLocalhostMatching }\n : {}),\n });\n this.#merger = new DataMerger({\n serverURL: options.serverURL,\n ...(options.apiRoute !== undefined ? { apiRoute: options.apiRoute } : {}),\n ...(options.depth !== undefined ? { depth: options.depth } : {}),\n ...(options.fetchFn !== undefined ? { fetchFn: options.fetchFn } : {}),\n log: (...args: unknown[]) => {\n this.#lastLog = args;\n },\n });\n this.#bus = new MessageBus((origin) => this.#detector.matches(origin), {\n onUpdate: (message) => {\n void this.#handle(message);\n },\n onDocumentEvent: () => {\n // A save is the admin's business; the document itself arrives as an\n // update like any other.\n },\n sourcePolicy: options.eventSourcePolicy ?? 'parent-or-opener',\n });\n this.#target = options.target;\n this.#initial = { data: initialData, isLoading: true, status: 'idle', error: undefined };\n this.#snapshot = this.#initial;\n }\n\n /**\n * Attach on the first subscriber and detach with the last, so a component\n * that mounts twice (React's strict mode) listens once and a page that\n * unmounts the hook stops listening at all.\n */\n subscribe = (listener: Listener): (() => void) => {\n this.#listeners.add(listener);\n if (!this.#attached) this.#attach();\n return () => {\n this.#listeners.delete(listener);\n if (this.#listeners.size === 0) this.#detach();\n };\n };\n\n getSnapshot = (): DocumentSnapshot<T> => this.#snapshot;\n\n /** Server rendering has no window and no messages: the initial snapshot a client render starts from. */\n getServerSnapshot = (): DocumentSnapshot<T> => this.#initial;\n\n #attach(): void {\n if (typeof window === 'undefined' && this.#target === undefined) return;\n this.#attached = true;\n this.#bus.attach(this.#target ?? window);\n MessageBus.sendReady(this.#readyTargets(), this.#detector.enumerate());\n }\n\n #detach(): void {\n if (!this.#attached) return;\n this.#attached = false;\n this.#bus.detach();\n this.#merger.destroy();\n }\n\n #readyTargets(): Window[] {\n const host = this.#target ?? (typeof window === 'undefined' ? undefined : window);\n if (host === undefined) return [];\n const targets: Window[] = [];\n if (host.parent !== host) targets.push(host.parent);\n const opener: unknown = host.opener;\n if (opener !== null && opener !== undefined && opener !== host) targets.push(opener as Window);\n return targets;\n }\n\n async #handle(message: PayloadLivePreviewMessage): Promise<void> {\n const data = message.data;\n if (data === undefined) return;\n const revision = (this.#revision += 1);\n this.#publish({ ...this.#snapshot, isLoading: true });\n this.#lastLog = [];\n const result = await this.#merger.merge({\n data,\n ...(message.collectionSlug !== undefined ? { collectionSlug: message.collectionSlug } : {}),\n ...(message.globalSlug !== undefined ? { globalSlug: message.globalSlug } : {}),\n ...(message.locale !== undefined ? { locale: message.locale } : {}),\n });\n // A newer message owns the state now, whatever this one turned out to be.\n if (revision !== this.#revision) return;\n if (result.status === 'superseded') return;\n if (result.status === 'unavailable') {\n this.#publish({\n ...this.#snapshot,\n isLoading: false,\n status: 'unavailable',\n error: new Error(`live preview: ${reasonFrom(this.#lastLog)}`),\n });\n return;\n }\n // The server decides the document's shape; `T` is what the caller says it is.\n this.#publish({\n data: result.doc as T,\n isLoading: false,\n status: 'live',\n error: undefined,\n });\n }\n\n #publish(next: DocumentSnapshot<T>): void {\n const current = this.#snapshot;\n if (\n current.data === next.data &&\n current.isLoading === next.isLoading &&\n current.status === next.status &&\n current.error === next.error\n ) {\n return;\n }\n this.#snapshot = next;\n for (const listener of [...this.#listeners]) listener();\n }\n}\n","/**\n * The seam between a host framework's own router and the route strategy.\n *\n * The route strategy answers a revision by fetching the route and morphing it\n * into the living page. On a page whose DOM belongs to a reconciler that is\n * working against the framework, and it was seen to break once: `removeChild`\n * on `null` inside React's commit phase, over markup this package had patched.\n * A host that re-renders the route itself does the same job without the morph\n * and without the second HTML request, so it is given the chance to.\n *\n * Why a named slot on `window` and not a reference: the runtime is an inline\n * script that builds its strategy before any application code has hydrated, so\n * there is nothing to hand a component; `window.__livePreview`, the only other\n * public surface, is created later and not at all outside a preview; and the\n * two richer extension points — plugins and events — are reachable only through\n * the programmatic client, which a page using an adapter does not have. One\n * slot, written by whoever mounts and read at the moment of a refresh, is the\n * smallest thing that survives both orders of arrival.\n */\n\n/**\n * A host's own route refresh. It must settle once the fresh markup is\n * committed: the runtime re-applies the revision on top of it, and doing that\n * against the markup being replaced would write into nodes about to be dropped.\n */\nexport type RouteRefresh = () => void | Promise<void>;\n\n/** Read late, so it does not matter whether the runtime or the component came first. */\nconst SLOT = '__livePreviewRouteRefresh';\n\ntype WindowSlot = Record<string, RouteRefresh | undefined>;\n\nfunction slot(): WindowSlot | undefined {\n return typeof window === 'undefined' ? undefined : (window as unknown as WindowSlot);\n}\n\n/**\n * Lend the runtime the host's route refresh; the return value undoes it, for a\n * component's cleanup. A later registration wins, and undoing one that has\n * already been replaced does nothing — two components on one page would\n * otherwise take the slot from each other on unmount.\n */\nexport function registerRouteRefresh(refresh: RouteRefresh): () => void {\n const target = slot();\n if (target === undefined) return () => undefined;\n target[SLOT] = refresh;\n return () => {\n // Cleared rather than deleted: the slot is read through `?.` anyway, and\n // removing the key buys nothing a lint rule would not have to be argued out of.\n if (target[SLOT] === refresh) target[SLOT] = undefined;\n };\n}\n\n/** The registered refresh, or `undefined` on a page that registered none. */\nexport function readRouteRefresh(): RouteRefresh | undefined {\n return slot()?.[SLOT];\n}\n","/**\n * `<LivePreviewRouteRefresh />` — lends the runtime the host router's own\n * refresh, so a route update is a re-render the framework performs rather than\n * fresh HTML this package morphs over the reconciler's nodes (see\n * `src/core/route-refresh.ts` for why that is worth avoiding).\n *\n * It takes the refresh as a prop instead of reaching for `next/navigation`,\n * because `next` is not a dependency of this package and must not become one\n * for a component this small. In Next's App Router that is one client\n * component:\n *\n * ```tsx\n * 'use client';\n * import { useRouter } from 'next/navigation';\n * import { LivePreviewRouteRefresh } from 'payload-live-preview/react';\n *\n * export function LivePreviewRefresh() {\n * return <LivePreviewRouteRefresh refresh={useRouter().refresh} />;\n * }\n * ```\n *\n * The same component serves any router with a refresh of that shape.\n */\n\nimport { useEffect, useRef, useTransition } from 'react';\nimport { registerRouteRefresh } from '@core/route-refresh';\n\nexport interface LivePreviewRouteRefreshProps {\n /** The host router's refresh — `useRouter().refresh` in Next's App Router. */\n readonly refresh: () => void;\n}\n\n/** Resolve everyone waiting on the transition that has just finished. */\nfunction settle(waiting: { current: (() => void)[] }): void {\n const settled = waiting.current;\n waiting.current = [];\n for (const resolve of settled) resolve();\n}\n\n/**\n * Renders nothing. Mount it once inside the preview tree; a second one takes\n * the registration from the first, and unmounting gives it back.\n */\nexport function LivePreviewRouteRefresh({ refresh }: LivePreviewRouteRefreshProps): null {\n const [pending, startTransition] = useTransition();\n const waiting = useRef<(() => void)[]>([]);\n\n // The runtime re-applies the revision on top of the fresh markup, so the\n // refresh it is given may not resolve before that markup is committed. The\n // transition is what makes that observable: it is pending until React has\n // rendered the new server output, and this effect runs on the commit after.\n useEffect(() => {\n if (!pending) settle(waiting);\n });\n\n useEffect(() => {\n const undo = registerRouteRefresh(\n () =>\n new Promise<void>((resolve) => {\n waiting.current.push(resolve);\n startTransition(() => {\n refresh();\n });\n }),\n );\n // A tree that unmounts mid-refresh will never commit one, so the waiting\n // runtime is released here rather than left holding a promise for good.\n return () => {\n undo();\n settle(waiting);\n };\n }, [refresh, startTransition]);\n\n return null;\n}\n","/**\n * The React hook: Payload's `useLivePreview` shape, with this package's merge\n * underneath — one request at a time, the newer one winning, an HTTP error\n * refused instead of becoming the document, and a cache that belongs to this\n * hook alone (ADR 0002).\n *\n * It re-renders your component tree, so conditional sections and derived values\n * are as correct as a server render. What it cannot do is keep the visitor's\n * state: React re-renders the subtree, and focus, scroll and typed values go\n * with it. The DOM runtime is the other side of that trade (docs/react.md).\n *\n * `react` is an optional peer. This entry imports it at module scope; `./nextjs`\n * loads it, and `react-dom/server`, only at the first render that needs them.\n *\n * The published file starts with `'use client'`. It is not written here because\n * esbuild drops a module directive when it bundles; the build adds it back as a\n * banner (`DIRECTIVE_ENTRIES` in scripts/package-entries.ts).\n */\n\nimport { useRef, useSyncExternalStore } from 'react';\nimport {\n DocumentSession,\n type DocumentSessionOptions,\n type DocumentSnapshot,\n type DocumentStatus,\n} from '@adapters/shared/document-session';\n\nexport type { DocumentSessionOptions, DocumentSnapshot, DocumentStatus };\n\nexport { LivePreviewRouteRefresh, type LivePreviewRouteRefreshProps } from './route-refresh';\nexport { registerRouteRefresh, type RouteRefresh } from '@core/route-refresh';\n\nexport interface UseLivePreviewDocumentOptions<T> extends DocumentSessionOptions {\n /** The document the page was rendered from; returned until an update merges. */\n readonly initialData: T;\n}\n\n/**\n * Subscribe to the admin's updates and return the merged document.\n *\n * ```tsx\n * const { data, status } = useLivePreviewDocument<Page>({\n * serverURL: process.env.NEXT_PUBLIC_PAYLOAD_URL!,\n * allowedOrigins: [process.env.NEXT_PUBLIC_PAYLOAD_URL!],\n * initialData: page,\n * depth: 1,\n * });\n * ```\n *\n * Returns `{ data, isLoading }` as Payload's hook does, plus `status` and\n * `error`: a merge that fails keeps the last good document instead of leaving\n * the page on a stale one with no way to tell.\n */\nexport function useLivePreviewDocument<T>(\n options: UseLivePreviewDocumentOptions<T>,\n): DocumentSnapshot<T> {\n const session = useSessionFor(options);\n return useSyncExternalStore(session.subscribe, session.getSnapshot, session.getServerSnapshot);\n}\n\n/**\n * One session per hook instance, rebuilt only when a connection option changes.\n * `initialData` is deliberately not one of them: a re-render with a fresh object\n * identity must not throw away a document that already merged.\n */\nfunction useSessionFor<T>(options: UseLivePreviewDocumentOptions<T>): DocumentSession<T> {\n const {\n initialData,\n serverURL,\n apiRoute,\n depth,\n allowedOrigins,\n eventSourcePolicy,\n enableReferrerDetection,\n enableLocalhostMatching,\n fetchFn,\n target,\n } = options;\n const key = [\n serverURL,\n apiRoute ?? '',\n String(depth ?? ''),\n (allowedOrigins ?? []).join(' '),\n eventSourcePolicy ?? '',\n String(enableReferrerDetection ?? ''),\n String(enableLocalhostMatching ?? ''),\n // A separator no origin, route prefix or policy name can contain.\n ].join(' | ');\n const held = useRef<{ key: string; session: DocumentSession<T> } | null>(null);\n if (held.current?.key !== key) {\n held.current = {\n key,\n session: new DocumentSession<T>(initialData, {\n serverURL,\n ...(apiRoute !== undefined ? { apiRoute } : {}),\n ...(depth !== undefined ? { depth } : {}),\n ...(allowedOrigins !== undefined ? { allowedOrigins } : {}),\n ...(eventSourcePolicy !== undefined ? { eventSourcePolicy } : {}),\n ...(enableReferrerDetection !== undefined ? { enableReferrerDetection } : {}),\n ...(enableLocalhostMatching !== undefined ? { enableLocalhostMatching } : {}),\n ...(fetchFn !== undefined ? { fetchFn } : {}),\n ...(target !== undefined ? { target } : {}),\n }),\n };\n }\n return held.current.session;\n}\n"],"mappings":";2FAQO,SAASA,EAAsBC,GACpC,OAAsB,iBAAVA,GAAgC,OAAVA,GAAoC,mBAAVA,IACD,mBAA/CA,EAAsCC,OAC7CC,QAAQC,QAAQH,GAAOI,MAAM,SAC3B,EACT,CCOO,SAASC,IAEhB,CCsBA,SAASC,EAAcN,EAAeO,GACpC,OACEP,EAAMQ,OAAS,GACfR,EAAMQ,QAAUD,GACN,MAAVP,GACU,OAAVA,IACCA,EAAMS,SAAS,OACfT,EAAMS,SAAS,QAjBpB,SAA6BT,GAC3B,UAAWU,KAAaV,EAAO,CAE7B,MAAMW,EAAYD,EAAUE,YAAY,IAAM,EAC9C,GAAID,GAAa,IAAsB,MAAdA,EAAoB,OAAO,CACtD,CACA,OAAO,CACT,CAWKE,CAAoBb,EAE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AEA,2BAA4B,WAC1C,IAA5BK,EAAwC,CAAEA,2BAA4B,WAC1D,IAAZxK,EAAwB,CAAEA,WAAY,WAC3B,IAAXwF,EAAuB,CAAEA,UAAW,OAIvCsJ,EAAKjB,QAAQc,OACtB,CAlDkBI,CAAczO,GAC9B,OAAO0O,EAAqBL,EAAQpB,UAAWoB,EAAQlB,YAAakB,EAAQjB,kBAC9E,Q","ignoreList":[]}
1
+ {"version":3,"file":"index.js","names":["observeThenableResult","value","then","Promise","resolve","catch","noopDiagnostic","isPathSegment","maxLength","length","includes","character","codePoint","codePointAt","hasControlCharacter","isSafeSlug","DataMerger","serverURL","apiRoute","depth","fetchFn","log","inflight","attempt","destroyDepth","constructor","options","this","end","endsWith","slice","withoutTrailingSlashes","route","diagnostic","startsWith","args","canMerge","request","endpointOf","globalSlug","encodeURIComponent","collectionSlug","id","data","Number","isFinite","String","merge","status","previous","controller","superseded","abort","endpoint","fetch","AbortController","url","response","method","credentials","headers","body","JSON","stringify","flattenLocales","locale","signal","ok","merged","json","Array","isArray","doc","error","destroy","parseFieldSchema","raw","out","entry","parsed","parseFieldEntry","push","record","name","type","fields","blocks","slug","parseBlockSchema","result","optionalFieldOk","check","optionalTypeOk","isPlainObject","prototype","Object","getPrototypeOf","NULL_AS_ABSENT_FIELDS","MessageBus","matcher","handlers","boundListener","attachedTarget","generation","revision","queueHead","queueTail","attach","target","window","committed","listener","event","receive","origin","reportInvalid","resetQueue","addEventListener","removeEventListener","detach","advanceGeneration","sendReady","targets","origins","payload","ready","protocolVersion","postMessage","matchesOrigin","matchesSource","v","x","isLivePreviewMessage","dispatchUpdate","message","normalized","field","Reflect","get","deleteProperty","fieldSchemaJSON","normalizeLivePreviewMessage","isDocumentEventMessage","invokeHandler","onDocumentEvent","focusFieldOf","onFocusField","hasUpdateData","isCurrentGeneration","messageRevision","validator","validateToken","commitUpdate","pending","verdict","next","enqueue","previewToken","settleValidation","approved","drainQueue","dequeue","onUpdate","reason","onInvalid","sourcePolicy","source","parent","opener","handler","expected","LOCALHOST_PATTERN","VALID_ORIGIN_PATTERN","LOCALHOST_HANDSHAKE_PORTS","OriginDetector","state","explicit","Set","additionalOrigins","normalised","normaliseOrigin","add","referrerOrigin","enableReferrerDetection","referrer","document","URL","allowLocalhost","enableLocalhostMatching","forceDevMode","isDevMode","nodeEnv","process","env","readNodeEnv","host","location","hostname","matches","has","size","test","lockOrigin","unlockOrigin","lockedOrigin","enumerate","port","referrerWasAvailable","isProductionUnconfigured","isInIframe","self","top","isReferrerOnlyTrust","trimmed","trim","protocol","reasonFrom","line","label","detail","Error","DocumentSession","merger","bus","detector","listeners","initial","snapshot","lastLog","attached","initialData","allowedOrigins","handle","eventSourcePolicy","isLoading","subscribe","delete","getSnapshot","getServerSnapshot","readyTargets","publish","current","SLOT","registerRouteRefresh","refresh","settle","waiting","settled","LivePreviewRouteRefresh","startTransition","useTransition","useRef","useEffect","undo","useLivePreviewDocument","session","key","join","held","useSessionFor","useSyncExternalStore"],"sources":["../../../src/core/thenable.ts","../../../src/core/diagnostics.ts","../../../src/core/data-merger.ts","../../../src/schema/parser.ts","../../../src/core/message-guards.ts","../../../src/core/message-bus.ts","../../../src/core/protocol-version.ts","../../../src/detection/origin.ts","../../../src/detection/environment.ts","../../../src/adapters/shared/document-session.ts","../../../src/core/route-refresh.ts","../../../src/adapters/react/route-refresh.ts","../../../src/adapters/react/index.ts"],"sourcesContent":["/**\n * Detect an invalid async result in a synchronous extension point.\n *\n * Rejected native Promises otherwise become unhandled after the caller falls\n * back synchronously. Assimilating the result through a handled Promise keeps\n * that contract violation observable only through the runtime's explicit\n * error channel.\n */\nexport function observeThenableResult(value: unknown): boolean {\n if ((typeof value !== 'object' || value === null) && typeof value !== 'function') return false;\n if (typeof (value as { readonly then?: unknown }).then !== 'function') return false;\n void Promise.resolve(value).catch(() => undefined);\n return true;\n}\n","import { observeThenableResult } from './thenable';\n\n/**\n * Diagnostics are observability, never control flow: a consumer logger may\n * throw or return a rejected thenable, and reporting a failure must not become\n * a second failure. Invocation stays synchronous for ordinary loggers.\n */\nexport function isolateDiagnostic(\n diagnostic: (...args: unknown[]) => unknown,\n): (...args: unknown[]) => void {\n return (...args): void => {\n try {\n observeThenableResult(diagnostic(...args));\n } catch {\n // Diagnostics must not become a second failure boundary.\n }\n };\n}\n\n/** Shared allocation-free default for disabled diagnostic channels. */\nexport function noopDiagnostic(): void {\n // Intentionally empty.\n}\n\n// Access, invocation and thenable inspection all happen inside the wrapper, so\n// a missing or hostile console cannot escape.\nexport const safeConsoleError = isolateDiagnostic((...args) =>\n (console.error as (...values: unknown[]) => unknown)(...args),\n);\n\nexport const safeConsoleWarn = isolateDiagnostic((...args) =>\n (console.warn as (...values: unknown[]) => unknown)(...args),\n);\n\nexport const safeConsoleDebug = isolateDiagnostic((...args) =>\n // eslint-disable-next-line no-console -- best-effort development diagnostics\n (console.debug as (...values: unknown[]) => unknown)(...args),\n);\n","/**\n * Server-side merge for Payload 3.x, whose admin posts raw form values with\n * relationships as bare ids. Like the official client, the update is re-fetched\n * through the REST API with `X-Payload-HTTP-Method-Override: GET`, which\n * merges the form values into the stored document and returns it populated.\n * Only the newest request matters: a newer merge aborts the one in flight.\n */\n\nimport { isolateDiagnostic, noopDiagnostic } from './diagnostics';\n\nexport interface DataMergerOptions {\n /** Payload server origin, e.g. `https://cms.example.com`. */\n readonly serverURL: string;\n /** REST route prefix. Defaults to `/api`. */\n readonly apiRoute?: string;\n /** Population depth requested from the server. Defaults to `1`. */\n readonly depth?: number;\n readonly fetchFn?: typeof fetch;\n readonly log?: (...args: unknown[]) => void;\n}\n\nexport interface MergeRequest {\n readonly collectionSlug?: string | undefined;\n readonly globalSlug?: string | undefined;\n readonly data: Record<string, unknown>;\n readonly locale?: string | undefined;\n}\n\n/** `superseded`: a newer merge replaced this one. `unavailable`: fall back to the raw values. */\nexport type MergeResult =\n | { readonly status: 'merged'; readonly doc: Record<string, unknown> }\n | { readonly status: 'superseded' }\n | { readonly status: 'unavailable' };\n\nfunction hasControlCharacter(value: string): boolean {\n for (const character of value) {\n // An iterated character always has a code point; 0 keeps the type and fails closed.\n const codePoint = character.codePointAt(0) ?? 0;\n if (codePoint <= 0x1f || codePoint === 0x7f) return true;\n }\n return false;\n}\n\n/** One path segment: non-empty, bounded, not a dot segment, without separators or control characters. */\nfunction isPathSegment(value: string, maxLength: number): boolean {\n return (\n value.length > 0 &&\n value.length <= maxLength &&\n value !== '.' &&\n value !== '..' &&\n !value.includes('/') &&\n !value.includes('\\\\') &&\n !hasControlCharacter(value)\n );\n}\n\n/** A slug is one of Payload's own names; `?` and `#` never belong in one. */\nfunction isSafeSlug(value: string): boolean {\n return isPathSegment(value, 128) && !value.includes('?') && !value.includes('#');\n}\n\n/** An id comes from the form values; a string one is encoded, so `?` and `#` may appear. */\nfunction isSafeId(value: unknown): value is string | number {\n if (typeof value === 'number') return Number.isFinite(value);\n return typeof value === 'string' && isPathSegment(value, 512);\n}\n\n/**\n * Trailing slashes off a base URL, without a regular expression: `/\\/+$/`\n * backtracks over a long run of slashes and takes quadratic time on input the\n * page supplies (CodeQL js/polynomial-redos).\n */\nfunction withoutTrailingSlashes(value: string): string {\n let end = value.length;\n while (value.endsWith('/', end)) end -= 1;\n return value.slice(0, end);\n}\n\nexport class DataMerger {\n private readonly serverURL: string;\n private readonly apiRoute: string;\n private readonly depth: number;\n private readonly fetchFn: typeof fetch | undefined;\n private readonly log: (...args: unknown[]) => void;\n private inflight: AbortController | null = null;\n private attempt = 0;\n private destroyDepth = 0;\n\n constructor(options: DataMergerOptions) {\n this.serverURL = withoutTrailingSlashes(options.serverURL);\n const route = options.apiRoute ?? '/api';\n this.apiRoute = route.startsWith('/') ? route : `/${route}`;\n this.depth = options.depth ?? 1;\n this.fetchFn = options.fetchFn;\n this.log = isolateDiagnostic(options.log ?? noopDiagnostic);\n }\n\n /** Collections need an `id` in the form values; globals only their slug. */\n canMerge(request: MergeRequest): boolean {\n return this.endpointOf(request) !== null;\n }\n\n /**\n * The path under `apiRoute`, or `null` when no safe one exists. A request\n * accessor that throws counts as none, and so does a lone surrogate, which\n * `encodeURIComponent` refuses with a `URIError`.\n */\n private endpointOf(request: MergeRequest): string | null {\n try {\n // An empty global slug means \"collection document\" on the wire.\n const globalSlug = request.globalSlug;\n if (globalSlug) {\n return isSafeSlug(globalSlug) ? `globals/${encodeURIComponent(globalSlug)}` : null;\n }\n const collectionSlug = request.collectionSlug ?? '';\n const id: unknown = request.data['id'];\n if (!isSafeSlug(collectionSlug) || !isSafeId(id)) return null;\n return `${encodeURIComponent(collectionSlug)}/${encodeURIComponent(String(id))}`;\n } catch {\n return null;\n }\n }\n\n async merge(request: MergeRequest): Promise<MergeResult> {\n if (this.destroyDepth > 0) return { status: 'superseded' };\n // Claim the attempt before aborting: abort listeners and fetch shims are\n // reentrant, and a newer merge they start must win. Nothing but this class\n // aborts the signal, and only after the counter moved, so the counter\n // alone decides who was superseded.\n const attempt = (this.attempt += 1);\n const previous = this.inflight;\n this.inflight = null;\n let controller: AbortController | null = null;\n const superseded = (): boolean => this.attempt !== attempt;\n try {\n previous?.abort();\n if (superseded()) return { status: 'superseded' };\n // The endpoint reads the request's own values, which may be consumer\n // getters that start a newer merge before throwing.\n const endpoint = this.endpointOf(request);\n if (endpoint === null) {\n return superseded() ? { status: 'superseded' } : { status: 'unavailable' };\n }\n const fetchFn = this.fetchFn ?? (typeof fetch === 'function' ? fetch : undefined);\n if (fetchFn === undefined) return { status: 'unavailable' };\n controller = new AbortController();\n this.inflight = controller;\n const url = `${this.serverURL}${this.apiRoute}/${endpoint}`;\n const response = await fetchFn(url, {\n method: 'POST',\n credentials: 'include',\n headers: {\n 'Content-Type': 'application/json',\n 'X-Payload-HTTP-Method-Override': 'GET',\n },\n // The admin already flattens locales before posting; an absent locale\n // is left out of the JSON.\n body: JSON.stringify({\n data: request.data,\n depth: this.depth,\n flattenLocales: false,\n locale: request.locale,\n }),\n signal: controller.signal,\n });\n // A fetch shim may ignore the signal; the attempt is authoritative.\n if (superseded()) return { status: 'superseded' };\n if (!response.ok) {\n this.log('merge HTTP', response.status, url);\n return { status: 'unavailable' };\n }\n const merged: unknown = await response.json();\n if (superseded()) return { status: 'superseded' };\n if (merged === null || typeof merged !== 'object' || Array.isArray(merged)) {\n this.log('merge invalid', url);\n return { status: 'unavailable' };\n }\n return { status: 'merged', doc: merged as Record<string, unknown> };\n } catch (error) {\n if (superseded()) return { status: 'superseded' };\n this.log('merge exception', error);\n return { status: 'unavailable' };\n } finally {\n // Only the newest attempt's controller is ever in flight: a superseded\n // attempt's is gone already, and its successor's is left alone.\n if (this.inflight === controller) this.inflight = null;\n }\n }\n\n /** Abort any in-flight merge. The merger stays usable across a stop/start cycle. */\n destroy(): void {\n this.attempt += 1;\n const inflight = this.inflight;\n this.inflight = null;\n this.destroyDepth += 1;\n try {\n inflight?.abort();\n } finally {\n this.destroyDepth -= 1;\n }\n }\n}\n","/**\n * Validates the `fieldSchemaJSON` the admin sends. Entries that fail\n * validation are dropped, never thrown: a Payload/runtime version mismatch\n * must not take the preview down.\n */\n\nimport type { PayloadBlockSchema, PayloadFieldSchema } from '@/types/payload-protocol';\n\nexport function parseFieldSchema(raw: unknown): readonly PayloadFieldSchema[] {\n if (!Array.isArray(raw)) return [];\n const out: PayloadFieldSchema[] = [];\n for (const entry of raw) {\n const parsed = parseFieldEntry(entry);\n if (parsed) out.push(parsed);\n }\n return out;\n}\n\nfunction parseFieldEntry(value: unknown): PayloadFieldSchema | undefined {\n if (typeof value !== 'object' || value === null) return undefined;\n const record = value as Record<string, unknown>;\n const name = record['name'];\n const type = record['type'];\n if (typeof name !== 'string' || name.length === 0) return undefined;\n if (typeof type !== 'string' || type.length === 0) return undefined;\n\n const fields = parseFieldSchema(record['fields']);\n const blocks = parseBlockSchema(record['blocks']);\n\n const result: Record<string, unknown> = { ...record, name, type };\n if (fields.length > 0) result['fields'] = fields;\n else delete result['fields'];\n if (blocks.length > 0) result['blocks'] = blocks;\n else delete result['blocks'];\n\n return result as unknown as PayloadFieldSchema;\n}\n\nfunction parseBlockSchema(raw: unknown): readonly PayloadBlockSchema[] {\n if (!Array.isArray(raw)) return [];\n const out: PayloadBlockSchema[] = [];\n for (const entry of raw) {\n if (typeof entry !== 'object' || entry === null) continue;\n const record = entry as Record<string, unknown>;\n const slug = record['slug'];\n if (typeof slug !== 'string' || slug.length === 0) continue;\n const fields = parseFieldSchema(record['fields']);\n out.push({ ...record, slug, fields });\n }\n return out;\n}\n","/** Shape guards for the messages the bus accepts. `null` counts as absent, as JSON bridges send it. */\n\nimport type {\n PayloadDocumentEventMessage,\n PayloadFieldSchema,\n PayloadLivePreviewMessage,\n} from '@/types/payload-protocol';\nimport { parseFieldSchema } from '@schema/parser';\n\nexport function isObjectMessage(value: unknown): value is { type: string } {\n return (\n typeof value === 'object' && value !== null && 'type' in value && typeof value.type === 'string'\n );\n}\n\n/** A `payload-live-preview` message: `data` a plain object when present, scalars of the right type or absent. */\nexport function isLivePreviewMessage(value: { type: string }): value is PayloadLivePreviewMessage {\n const v = value as Record<string, unknown>;\n if (v['data'] !== undefined && v['data'] !== null && !isPlainObject(v['data'])) return false;\n return (\n optionalTypeOk(v['fieldSchemaJSON'], (x) => Array.isArray(x)) &&\n optionalTypeOk(v['globalSlug'], (x) => typeof x === 'string') &&\n optionalTypeOk(v['collectionSlug'], (x) => typeof x === 'string') &&\n optionalTypeOk(v['locale'], (x) => typeof x === 'string') &&\n optionalTypeOk(v['ready'], (x) => typeof x === 'boolean') &&\n optionalTypeOk(v['previewToken'], (x) => typeof x === 'string') &&\n optionalTypeOk(v['protocolVersion'], (x) => typeof x === 'number')\n );\n}\n\n/** The non-empty field name of a `payload-live-preview-focus` message, else `undefined`. */\nexport function focusFieldOf(value: { type: string }): string | undefined {\n const field = (value as Record<string, unknown>)['field'];\n return typeof field === 'string' && field.length > 0 ? field : undefined;\n}\n\n/** Stock Payload sends only the `type`; custom senders may add the typed fields (omitted, never `null`). */\nexport function isDocumentEventMessage(value: {\n type: string;\n}): value is PayloadDocumentEventMessage {\n const v = value as Record<string, unknown>;\n return (\n optionalFieldOk(v['action'], (x) => x === 'updated' || x === 'created' || x === 'deleted') &&\n optionalFieldOk(v['slug'], (x) => typeof x === 'string') &&\n optionalFieldOk(v['id'], (x) => typeof x === 'string' || Number.isFinite(x))\n );\n}\n\nfunction optionalFieldOk(value: unknown, check: (v: unknown) => boolean): boolean {\n return value === undefined || check(value);\n}\n\nfunction optionalTypeOk(value: unknown, check: (v: unknown) => boolean): boolean {\n return value === undefined || value === null || check(value);\n}\n\nexport function isPlainObject(value: unknown): value is Record<string, unknown> {\n if (typeof value !== 'object' || value === null) return false;\n const prototype = Object.getPrototypeOf(value) as object | null;\n return prototype === null || Object.getPrototypeOf(prototype) === null;\n}\n\nconst NULL_AS_ABSENT_FIELDS = [\n 'data',\n 'fieldSchemaJSON',\n 'globalSlug',\n 'collectionSlug',\n 'locale',\n 'ready',\n 'previewToken',\n 'protocolVersion',\n] as const satisfies readonly (keyof PayloadLivePreviewMessage)[];\n\n/**\n * Drop `null` optionals so downstream sees the strict public type, and parse\n * the field schema entry by entry (malformed entries are dropped). Unknown\n * fields stay, for protocol evolution.\n */\nexport function normalizeLivePreviewMessage(\n message: PayloadLivePreviewMessage,\n): PayloadLivePreviewMessage {\n const normalized = { ...message };\n for (const field of NULL_AS_ABSENT_FIELDS) {\n const wireValue: unknown = Reflect.get(normalized, field);\n if (wireValue === null) Reflect.deleteProperty(normalized, field);\n }\n if (!Array.isArray(normalized.fieldSchemaJSON)) return normalized;\n let fieldSchemaJSON: readonly PayloadFieldSchema[];\n try {\n fieldSchemaJSON = parseFieldSchema(normalized.fieldSchemaJSON);\n } catch {\n fieldSchemaJSON = [];\n }\n return { ...normalized, fieldSchemaJSON };\n}\n","/**\n * postMessage ingress: origin and source checks, per-type shape guards, the\n * ordered preview-token validation queue, and the `ready` handshake. The bus\n * never throws; a rejected message is dropped or reported via `onInvalid`.\n * Consumer callbacks may detach or re-attach the bus synchronously, so\n * anything that runs after one re-checks the generation it started in.\n * See ADR 0004.\n */\n\nimport type {\n PayloadDocumentEventMessage,\n PayloadLivePreviewMessage,\n} from '@/types/payload-protocol';\nimport {\n focusFieldOf,\n isDocumentEventMessage,\n isLivePreviewMessage,\n isObjectMessage,\n isPlainObject,\n normalizeLivePreviewMessage,\n} from './message-guards';\nimport { LIBRARY_PROTOCOL_VERSION } from './protocol-version';\nimport { observeThenableResult } from './thenable';\n\nexport type OriginMatcher = (origin: string) => boolean;\n\n/** Identity of a data-bearing update: `revision` is monotonic, `generation` names the listener attachment. */\nexport interface MessageRevision {\n readonly revision: number;\n readonly generation: number;\n}\nexport function sameRevision(a: MessageRevision, b: MessageRevision): boolean {\n return a.generation === b.generation && a.revision === b.revision;\n}\n\nexport type InvalidReason = 'origin' | 'shape' | 'type' | 'token' | 'source';\n\nexport interface MessageHandlers {\n /** A validated update; data-bearing ones carry their revision, ready handshakes do not. */\n readonly onUpdate: (\n msg: PayloadLivePreviewMessage,\n origin: string,\n messageRevision?: MessageRevision,\n ) => void;\n readonly onDocumentEvent: (msg: PayloadDocumentEventMessage, origin: string) => void;\n /** The admin reports the cursor moved into a field (reveal tier 2). */\n readonly onFocusField?: (field: string, origin: string) => void;\n readonly onInvalid?: (reason: InvalidReason, origin: string) => void;\n /** `'parent-or-opener'` refuses every window but the framing/opening one, even from an allowed origin. */\n readonly sourcePolicy?: 'any' | 'parent-or-opener';\n /**\n * Gate for data-bearing updates. Verdicts commit in arrival order, so an\n * earlier update is dispatched before a later one even when its validation\n * resolves later; an error counts as rejection.\n */\n readonly validateToken?: (\n token: string | undefined,\n origin: string,\n ) => boolean | Promise<boolean>;\n}\n\n/** One entry of the validation queue: a singly linked list in arrival order. */\ninterface PendingValidation {\n readonly generation: number;\n readonly message: PayloadLivePreviewMessage;\n readonly origin: string;\n readonly messageRevision: MessageRevision | undefined;\n /** `undefined` until the validator settled; only literal `true` approves. */\n verdict: boolean | undefined;\n next: PendingValidation | undefined;\n}\n\nexport class MessageBus {\n private readonly matcher: OriginMatcher;\n private readonly handlers: MessageHandlers;\n private boundListener: ((event: MessageEvent) => void) | undefined = undefined;\n private attachedTarget: Window | undefined = undefined;\n private generation = 0;\n private revision = 0;\n private queueHead: PendingValidation | undefined = undefined;\n private queueTail: PendingValidation | undefined = undefined;\n\n constructor(matcher: OriginMatcher, handlers: MessageHandlers) {\n this.matcher = matcher;\n this.handlers = handlers;\n }\n\n /** Start listening on `target`. Idempotent. */\n attach(target: Window = window): void {\n if (this.attachedTarget !== undefined) return;\n let committed = false;\n const listener = (event: MessageEvent): void => {\n // Each attachment owns its listener; a stale one must not borrow the new generation.\n if (!committed || this.boundListener !== listener) return;\n const generation = this.generation;\n try {\n this.receive(event, generation, target);\n } catch {\n // A synthetic event can carry throwing accessors; that is a shape failure.\n let origin: string;\n try {\n origin = event.origin;\n } catch {\n origin = '';\n }\n this.reportInvalid('shape', origin, generation);\n }\n };\n this.generation += 1;\n this.resetQueue();\n this.attachedTarget = target;\n this.boundListener = listener;\n try {\n target.addEventListener('message', listener);\n } catch (error) {\n // Invalidate before rollback: a hook may already have attached a newer generation.\n if (this.boundListener === listener) {\n this.attachedTarget = undefined;\n this.boundListener = undefined;\n this.resetQueue();\n }\n try {\n target.removeEventListener('message', listener);\n } catch {\n // Keep the original failure.\n }\n throw error;\n }\n if (this.boundListener === listener) {\n committed = true;\n } else {\n try {\n target.removeEventListener('message', listener);\n } catch {\n // The listener is identity-gated above even if removal fails.\n }\n }\n }\n\n /** Stop listening. Idempotent. */\n detach(): void {\n const target = this.attachedTarget;\n const listener = this.boundListener;\n if (target === undefined || listener === undefined) return;\n // Invalidate first so a listener already on the stack sees an obsolete generation.\n this.attachedTarget = undefined;\n this.boundListener = undefined;\n this.resetQueue();\n target.removeEventListener('message', listener);\n }\n\n /** Invalidate pending validations while staying attached (heartbeat expiry). */\n advanceGeneration(): boolean {\n if (this.attachedTarget === undefined) return false;\n this.generation += 1;\n this.resetQueue();\n return true;\n }\n\n /** Post the `ready` handshake to every target for every origin. */\n static sendReady(targets: readonly Window[], origins: readonly string[]): void {\n const payload: PayloadLivePreviewMessage = {\n type: 'payload-live-preview',\n ready: true,\n protocolVersion: LIBRARY_PROTOCOL_VERSION,\n };\n for (const target of targets) {\n for (const origin of origins) {\n try {\n target.postMessage(payload, origin);\n } catch {\n // A malformed origin is the consumer's configuration problem.\n }\n }\n }\n }\n\n private receive(event: MessageEvent, generation: number, target: Window): void {\n const origin = event.origin;\n if (!this.matchesOrigin(origin, generation)) {\n this.reportInvalid('origin', origin, generation);\n return;\n }\n if (!this.matchesSource(event, target)) {\n this.reportInvalid('source', origin, generation);\n return;\n }\n const data: unknown = event.data;\n if (!isObjectMessage(data)) {\n this.reportInvalid('shape', origin, generation);\n return;\n }\n switch (data.type) {\n case 'payload-live-preview':\n if (!isLivePreviewMessage(data)) {\n this.reportInvalid('shape', origin, generation);\n return;\n }\n this.dispatchUpdate(normalizeLivePreviewMessage(data), origin, generation);\n return;\n case 'payload-document-event':\n if (!isDocumentEventMessage(data)) {\n this.reportInvalid('shape', origin, generation);\n return;\n }\n this.invokeHandler(generation, this.handlers.onDocumentEvent, data, origin);\n return;\n case 'payload-live-preview-focus': {\n const field = focusFieldOf(data);\n if (field === undefined) {\n this.reportInvalid('shape', origin, generation);\n return;\n }\n this.invokeHandler(generation, this.handlers.onFocusField, field, origin);\n return;\n }\n default:\n this.reportInvalid('type', origin, generation);\n }\n }\n\n private dispatchUpdate(\n message: PayloadLivePreviewMessage,\n origin: string,\n generation: number,\n ): void {\n const hasUpdateData = isPlainObject(message.data);\n if (!this.isCurrentGeneration(generation)) return;\n // A shape-valid data update consumes its revision before validation; a rejected token leaves a gap.\n const messageRevision = hasUpdateData\n ? { generation, revision: (this.revision += 1) }\n : undefined;\n const validator = this.handlers.validateToken;\n if (!this.isCurrentGeneration(generation)) return;\n if (validator === undefined) {\n this.commitUpdate(message, origin, generation, messageRevision);\n return;\n }\n // The data-less ready handshake carries no token and passes, so the admin learns we listen.\n if (message.ready === true && message.data === undefined) {\n this.commitUpdate(message, origin, generation, undefined);\n return;\n }\n const pending: PendingValidation = {\n generation,\n message,\n origin,\n messageRevision,\n verdict: undefined,\n next: undefined,\n };\n this.enqueue(pending);\n let verdict: unknown;\n try {\n // Invoked eagerly; only the commit waits for earlier queue entries.\n verdict = validator(message.previewToken, origin);\n } catch {\n this.settleValidation(pending, false);\n return;\n }\n if (typeof verdict === 'boolean') {\n this.settleValidation(pending, verdict);\n return;\n }\n // Assimilate any thenable; only literal `true` approves.\n void Promise.resolve(verdict).then(\n (approved) => {\n this.settleValidation(pending, approved === true);\n },\n () => {\n this.settleValidation(pending, false);\n },\n );\n }\n\n private settleValidation(pending: PendingValidation, approved: boolean): void {\n pending.verdict = approved;\n this.drainQueue(pending.generation);\n }\n\n private drainQueue(generation: number): void {\n while (this.isCurrentGeneration(generation)) {\n const pending = this.queueHead;\n if (pending === undefined) return;\n // The matcher may have narrowed since ingress (origin lock); an obsolete\n // origin must not hold the queue head and block the locked origin's work.\n if (!this.matchesOrigin(pending.origin, generation)) {\n if (!this.dequeue(pending)) continue;\n this.reportInvalid('origin', pending.origin, pending.generation);\n continue;\n }\n if (pending.verdict === undefined) return;\n if (!this.dequeue(pending)) continue;\n if (pending.verdict) {\n this.commitUpdate(\n pending.message,\n pending.origin,\n pending.generation,\n pending.messageRevision,\n );\n } else {\n this.reportInvalid('token', pending.origin, pending.generation);\n }\n }\n }\n\n private commitUpdate(\n message: PayloadLivePreviewMessage,\n origin: string,\n generation: number,\n messageRevision: MessageRevision | undefined,\n ): void {\n if (messageRevision === undefined) {\n this.invokeHandler(generation, this.handlers.onUpdate, message, origin);\n } else {\n this.invokeHandler(generation, this.handlers.onUpdate, message, origin, messageRevision);\n }\n }\n\n private reportInvalid(reason: InvalidReason, origin: string, generation: number): void {\n this.invokeHandler(generation, this.handlers.onInvalid, reason, origin);\n }\n\n /** Under `'parent-or-opener'`, whether the event came from the attached window's parent or opener. */\n private matchesSource(event: MessageEvent, target: Window): boolean {\n if (this.handlers.sourcePolicy !== 'parent-or-opener') return true;\n try {\n // Only a window is a source: a missing one matches nothing, not even a missing opener.\n const source: unknown = event.source;\n if (typeof source !== 'object' || source === null) return false;\n const parent = target.parent;\n if (parent !== target && source === parent) return true;\n const opener: unknown = target.opener;\n return source === opener;\n } catch {\n return false;\n }\n }\n\n /** Fail-closed: only literal `true` from the matcher, and only within the generation it ran in. */\n private matchesOrigin(origin: string, generation: number): boolean {\n try {\n const approved: unknown = this.isCurrentGeneration(generation) && this.matcher(origin);\n return approved === true && this.isCurrentGeneration(generation);\n } catch {\n return false;\n }\n }\n\n /** Callbacks run only in a current generation and must not unwind the listener or the drain. */\n private invokeHandler<TArgs extends unknown[]>(\n generation: number,\n handler: ((...args: TArgs) => unknown) | undefined,\n ...args: TArgs\n ): void {\n if (handler === undefined || !this.isCurrentGeneration(generation)) return;\n try {\n observeThenableResult(handler(...args));\n } catch {\n // Isolated by contract.\n }\n }\n\n private isCurrentGeneration(generation: number): boolean {\n return this.attachedTarget !== undefined && this.generation === generation;\n }\n\n private enqueue(pending: PendingValidation): void {\n if (this.queueTail === undefined) this.queueHead = pending;\n else this.queueTail.next = pending;\n this.queueTail = pending;\n }\n\n /** Advance the head past `expected`, unless a matcher reset the queue meanwhile. */\n private dequeue(expected: PendingValidation): boolean {\n if (this.queueHead !== expected) return false;\n this.queueHead = expected.next;\n if (this.queueHead === undefined) this.queueTail = undefined;\n return true;\n }\n\n private resetQueue(): void {\n this.queueHead = undefined;\n this.queueTail = undefined;\n }\n}\n","/**\n * Protocol negotiation. A capability is granted either by the version a peer\n * announces or by observation, since the stock Payload admin announces none:\n * what it can do is read off the messages it sends. See ADR 0010.\n */\n\n/** Bumped only when this library's understanding of the wire format changes. @internal */\nexport const LIBRARY_PROTOCOL_VERSION = 4;\n\n/** How a capability becomes active. @internal */\nexport type CapabilitySource = 'version' | 'observed';\n\n/** @internal */\nexport interface CapabilityDeclaration {\n /** Enabled once the negotiated version reaches this (for announced peers). */\n readonly since: number;\n /** The wire signal that grants this capability to a peer announcing no version. */\n readonly observed?:\n 'schema' | 'locale' | 'preview-token' | 'document-event' | 'relationship-event';\n}\n\n/** What activates each capability; the prose lives in `CAPABILITY_DOCUMENTATION`. */\nexport const CAPABILITY_DECLARATIONS = {\n basic: { since: 1 },\n 'schema-json': { since: 2, observed: 'schema' },\n locale: { since: 2, observed: 'locale' },\n 'preview-token': { since: 3, observed: 'preview-token' },\n 'nested-arrays': { since: 4 },\n 'recursive-diffs': { since: 4 },\n 'document-events': { since: 4, observed: 'document-event' },\n 'relationship-events': { since: 4, observed: 'relationship-event' },\n} as const satisfies Record<string, CapabilityDeclaration>;\n\nexport type ProtocolCapability = keyof typeof CAPABILITY_DECLARATIONS;\n\n/** Every declared capability, in declaration order. @internal */\nexport const PROTOCOL_CAPABILITIES = Object.keys(\n CAPABILITY_DECLARATIONS,\n) as readonly ProtocolCapability[];\n\nexport interface ProtocolNegotiation {\n /** This library's protocol version. */\n readonly ours: number;\n /** The remote party's version, when it announced one. */\n readonly theirs: number | undefined;\n /** `min(ours, theirs ?? 1)` — the version both sides actually share. */\n readonly negotiated: number;\n /** Capabilities active at the negotiated version or seen on the wire. */\n readonly capabilities: ReadonlySet<ProtocolCapability>;\n /** The subset that was observed on the wire rather than granted by version. */\n readonly observed: ReadonlySet<ProtocolCapability>;\n}\n\n/** Deterministic; the runtime re-runs it whenever either input changes. */\nexport function negotiateProtocol(\n theirs: number | undefined,\n observed: Iterable<ProtocolCapability> = [],\n): ProtocolNegotiation {\n const sanitisedTheirs =\n typeof theirs === 'number' && Number.isFinite(theirs) && theirs >= 1\n ? Math.floor(theirs)\n : undefined;\n const effectiveTheirs = sanitisedTheirs ?? 1;\n const negotiated = Math.min(LIBRARY_PROTOCOL_VERSION, effectiveTheirs);\n const seen = new Set<ProtocolCapability>(observed);\n const capabilities = new Set<ProtocolCapability>();\n for (const capability of PROTOCOL_CAPABILITIES) {\n if (negotiated >= CAPABILITY_DECLARATIONS[capability].since || seen.has(capability)) {\n capabilities.add(capability);\n }\n }\n return {\n ours: LIBRARY_PROTOCOL_VERSION,\n ...(sanitisedTheirs !== undefined ? { theirs: sanitisedTheirs } : { theirs: undefined }),\n negotiated,\n capabilities,\n observed: seen,\n };\n}\n\n/** @internal */\nexport function hasCapability(\n negotiation: ProtocolNegotiation,\n capability: ProtocolCapability,\n): boolean {\n return negotiation.capabilities.has(capability);\n}\n\n/** The capabilities a single message demonstrates, by its shape alone. */\nexport function observeCapabilities(message: {\n readonly fieldSchemaJSON?: unknown;\n readonly locale?: unknown;\n readonly previewToken?: unknown;\n readonly externallyUpdatedRelationship?: unknown;\n}): readonly ProtocolCapability[] {\n const seen: ProtocolCapability[] = [];\n if (Array.isArray(message.fieldSchemaJSON)) seen.push('schema-json');\n if (typeof message.locale === 'string') seen.push('locale');\n if (typeof message.previewToken === 'string') seen.push('preview-token');\n if (\n typeof message.externallyUpdatedRelationship === 'object' &&\n message.externallyUpdatedRelationship !== null\n ) {\n seen.push('relationship-events');\n }\n return seen;\n}\n","/**\n * Which admin origins the runtime trusts: explicit ones always, the\n * `document.referrer` origin only when none is configured, any localhost port\n * in development. See docs/security.md §1.\n */\n\nimport { isDevMode, isInIframe } from './environment';\n\nconst LOCALHOST_PATTERN = /^https?:\\/\\/(localhost|127\\.0\\.0\\.1)(?::\\d+)?$/i;\nconst VALID_ORIGIN_PATTERN = /^https?:\\/\\/[^/?#\\s]+$/i;\n\n/** Ports the handshake broadcasts to when the localhost pattern is on; matching itself is unrestricted. */\nconst LOCALHOST_HANDSHAKE_PORTS: readonly number[] = [\n 3000, 3001, 3333, 4000, 4321, 5000, 5173, 5174, 8000, 8080, 8888, 9000,\n];\n\nexport interface OriginDetectorOptions {\n /** Explicit origins to trust. Always allowed. */\n readonly additionalOrigins?: readonly string[];\n /** Set to `false` to ignore `document.referrer`. */\n readonly enableReferrerDetection?: boolean;\n /** Set to `false` to ignore the localhost pattern even in development. */\n readonly enableLocalhostMatching?: boolean;\n /** Override dev-mode detection. */\n readonly forceDevMode?: boolean;\n /** Override `document.referrer` for tests. */\n readonly referrer?: string;\n}\n\nconst enum OriginSlot {\n ExplicitOrigins,\n ReferrerOrigin,\n AllowLocalhost,\n ReferrerWasAvailable,\n LockedOrigin,\n}\n\ntype OriginState = [\n explicitOrigins: ReadonlySet<string>,\n referrerOrigin: string | undefined,\n allowLocalhost: boolean,\n referrerWasAvailable: boolean,\n lockedOrigin: string | undefined,\n];\n\n/** The trusted-origin policy: the union of the candidate sources, narrowed to one by `lockOrigin()`. */\nexport class OriginDetector {\n readonly #state: OriginState;\n\n constructor(options: OriginDetectorOptions = {}) {\n const explicit = new Set<string>();\n for (const origin of options.additionalOrigins ?? []) {\n const normalised = normaliseOrigin(origin);\n if (normalised !== undefined) explicit.add(normalised);\n }\n let referrerOrigin: string | undefined;\n if (options.enableReferrerDetection ?? true) {\n const referrer =\n options.referrer ?? (typeof document !== 'undefined' ? document.referrer : '');\n if (referrer.length > 0) {\n try {\n const origin = new URL(referrer).origin;\n if (origin && origin !== 'null') referrerOrigin = origin;\n } catch {\n // opaque referrer\n }\n }\n }\n const allowLocalhost =\n (options.enableLocalhostMatching ?? true) && (options.forceDevMode ?? isDevMode());\n this.#state = [\n explicit,\n referrerOrigin,\n allowLocalhost,\n referrerOrigin !== undefined,\n undefined,\n ];\n }\n\n /** Whether `origin` is trusted: the locked origin alone once locked, otherwise the policy above. */\n matches(origin: string): boolean {\n if (origin.length === 0 || origin === 'null') return false;\n if (this.#state[OriginSlot.LockedOrigin] !== undefined) {\n return origin === this.#state[OriginSlot.LockedOrigin];\n }\n if (this.#state[OriginSlot.ExplicitOrigins].has(origin)) return true;\n // The referrer is a fallback, not a union member: any embedder lands there.\n if (\n this.#state[OriginSlot.ExplicitOrigins].size === 0 &&\n this.#state[OriginSlot.ReferrerOrigin] !== undefined &&\n origin === this.#state[OriginSlot.ReferrerOrigin]\n ) {\n return true;\n }\n if (this.#state[OriginSlot.AllowLocalhost] && LOCALHOST_PATTERN.test(origin)) return true;\n return false;\n }\n\n /** Narrow trust to `origin`; refused (`false`) when it is not currently trusted. */\n lockOrigin(origin: string): boolean {\n if (!this.matches(origin)) return false;\n this.#state[OriginSlot.LockedOrigin] = origin;\n return true;\n }\n\n /** Release the lock (heartbeat timeout) so another allow-listed origin may reconnect. Returns the previous lock. */\n unlockOrigin(): string | undefined {\n const previous = this.#state[OriginSlot.LockedOrigin];\n this.#state[OriginSlot.LockedOrigin] = undefined;\n return previous;\n }\n\n get lockedOrigin(): string | undefined {\n return this.#state[OriginSlot.LockedOrigin];\n }\n\n /** Every origin the `ready` handshake is broadcast to; the localhost pattern expands to the common dev ports. */\n enumerate(): string[] {\n if (this.#state[OriginSlot.LockedOrigin] !== undefined) {\n return [this.#state[OriginSlot.LockedOrigin]];\n }\n const result = new Set<string>(this.#state[OriginSlot.ExplicitOrigins]);\n if (\n this.#state[OriginSlot.ExplicitOrigins].size === 0 &&\n this.#state[OriginSlot.ReferrerOrigin] !== undefined\n ) {\n result.add(this.#state[OriginSlot.ReferrerOrigin]);\n }\n if (this.#state[OriginSlot.AllowLocalhost]) {\n for (const port of LOCALHOST_HANDSHAKE_PORTS) {\n result.add(`http://localhost:${String(port)}`);\n result.add(`http://127.0.0.1:${String(port)}`);\n }\n }\n return [...result];\n }\n\n /** Whether `document.referrer` contributed an origin (absent under `Referrer-Policy: no-referrer`). */\n get referrerWasAvailable(): boolean {\n return this.#state[OriginSlot.ReferrerWasAvailable];\n }\n\n /** Framed, outside development, with no trusted origin at all: the host should warn (LP0101). */\n get isProductionUnconfigured(): boolean {\n if (!isInIframe()) return false;\n if (this.#state[OriginSlot.ExplicitOrigins].size > 0) return false;\n if (this.#state[OriginSlot.AllowLocalhost]) return false;\n if (this.#state[OriginSlot.ReferrerOrigin] !== undefined) return false;\n return true;\n }\n\n /** The referrer is the only trust source, so any framing site may drive the preview (LP0102). */\n get isReferrerOnlyTrust(): boolean {\n if (this.#state[OriginSlot.ExplicitOrigins].size > 0) return false;\n if (this.#state[OriginSlot.AllowLocalhost]) return false;\n return this.#state[OriginSlot.ReferrerOrigin] !== undefined;\n }\n}\n\n/** The canonical `http(s)://host[:port]` form of `value`, or `undefined` when it is not one. */\nexport function normaliseOrigin(value: string): string | undefined {\n const trimmed = value.trim();\n if (trimmed.length === 0) return undefined;\n try {\n const url = new URL(trimmed);\n if (url.protocol !== 'http:' && url.protocol !== 'https:') return undefined;\n if (!VALID_ORIGIN_PATTERN.test(url.origin)) return undefined;\n return url.origin;\n } catch {\n return undefined;\n }\n}\n","/**\n * Where the runtime is running: framed, opened, or on a developer's machine.\n * Nothing reads bundler-injected variables; a Vite consumer passes\n * `import.meta.env.X` explicitly (docs/astro.md).\n * @internal\n */\n\nexport function isInIframe(): boolean {\n if (typeof window === 'undefined') return false;\n try {\n return window.self !== window.top;\n } catch {\n // A cross-origin parent throws on the comparison; that is an iframe.\n return true;\n }\n}\n\n/** @internal */\nexport function isInPopup(): boolean {\n if (typeof window === 'undefined') return false;\n return window.opener != null;\n}\n\n/** An iframe or a popup; top-level navigation never starts the preview. @internal */\nexport function isInPreviewContext(): boolean {\n return isInIframe() || isInPopup();\n}\n\n/** Under Node, `NODE_ENV !== 'production'`; in a browser the hostname, which is the only signal a bundle has. @internal */\nexport function isDevMode(): boolean {\n const nodeEnv = readNodeEnv();\n if (nodeEnv !== undefined) return nodeEnv !== 'production';\n if (typeof window !== 'undefined') {\n const host = window.location.hostname;\n return host === 'localhost' || host === '127.0.0.1';\n }\n return false;\n}\n\nfunction readNodeEnv(): string | undefined {\n if (typeof process === 'undefined') return undefined;\n const value = process.env['NODE_ENV'];\n return typeof value === 'string' ? value : undefined;\n}\n","/**\n * The document a framework hook exposes, kept outside any framework: the\n * message bus decides which windows may speak, `DataMerger` turns the admin's\n * raw form values back into a populated document, and this class holds the one\n * snapshot a `useSyncExternalStore`-shaped API reads.\n *\n * It touches no DOM. A hook that re-renders a component tree needs the data,\n * not the bindings — everything about elements lives in the runtime instead.\n *\n * The session is per instance (ADR 0002): two hooks on one page have two\n * mergers, two revisions and two snapshots, and neither can hand the other a\n * document it never asked for.\n */\n\nimport { DataMerger } from '@core/data-merger';\nimport { MessageBus } from '@core/message-bus';\nimport { OriginDetector } from '@detection/origin';\nimport type { PayloadLivePreviewMessage } from '@/types/payload-protocol';\n\n/**\n * `idle` — connected, nothing received yet. `live` — the newest update was\n * merged and is in `data`. `unavailable` — it could not be, and `data` is the\n * last good document rather than a half-merged one.\n */\nexport type DocumentStatus = 'idle' | 'live' | 'unavailable';\n\nexport interface DocumentSnapshot<T> {\n /** The newest document that merged; never a partially applied one. */\n readonly data: T;\n /** No update has settled yet, or a merge is in flight. */\n readonly isLoading: boolean;\n readonly status: DocumentStatus;\n /** Why the last update did not merge; `undefined` while `status` is not `unavailable`. */\n readonly error: Error | undefined;\n}\n\nexport interface DocumentSessionOptions {\n /** Payload server origin the update is re-fetched from. */\n readonly serverURL: string;\n /** REST route prefix. Default `/api`. */\n readonly apiRoute?: string;\n /** Population depth for the merge. Default `1`, as Payload's own hook. */\n readonly depth?: number;\n /**\n * Admin origins allowed to post updates. Without one the session falls back\n * to the localhost matcher in development and trusts nothing in production.\n */\n readonly allowedOrigins?: readonly string[];\n /** `'parent-or-opener'` (default) refuses every window but the framing or opening one. */\n readonly eventSourcePolicy?: 'any' | 'parent-or-opener';\n /** Trust `document.referrer` as an origin source. Default `false`. */\n readonly enableReferrerDetection?: boolean;\n /** Match `localhost` origins in development. Default `true`. */\n readonly enableLocalhostMatching?: boolean;\n /** Injected for tests. */\n readonly fetchFn?: typeof fetch;\n /** Window to listen on. Default the global one. */\n readonly target?: Window;\n}\n\ntype Listener = () => void;\n\n/** What the merger logged for the attempt that failed, turned into one sentence. */\nfunction reasonFrom(line: readonly unknown[]): string {\n const [label, detail] = line;\n if (label === 'merge HTTP') return `the server answered ${String(detail)}`;\n if (label === 'merge invalid') return 'the server answered with something other than a document';\n if (label === 'merge exception') {\n return detail instanceof Error ? detail.message : 'the request failed';\n }\n return 'the update could not be merged';\n}\n\nexport class DocumentSession<T> {\n readonly #merger: DataMerger;\n readonly #bus: MessageBus;\n readonly #detector: OriginDetector;\n readonly #target: Window | undefined;\n readonly #listeners = new Set<Listener>();\n readonly #initial: DocumentSnapshot<T>;\n #snapshot: DocumentSnapshot<T>;\n /** Newest handled message; an older merge that settles later is ignored. */\n #revision = 0;\n #lastLog: readonly unknown[] = [];\n #attached = false;\n\n constructor(initialData: T, options: DocumentSessionOptions) {\n this.#detector = new OriginDetector({\n ...(options.allowedOrigins !== undefined\n ? { additionalOrigins: options.allowedOrigins }\n : {}),\n enableReferrerDetection: options.enableReferrerDetection ?? false,\n ...(options.enableLocalhostMatching !== undefined\n ? { enableLocalhostMatching: options.enableLocalhostMatching }\n : {}),\n });\n this.#merger = new DataMerger({\n serverURL: options.serverURL,\n ...(options.apiRoute !== undefined ? { apiRoute: options.apiRoute } : {}),\n ...(options.depth !== undefined ? { depth: options.depth } : {}),\n ...(options.fetchFn !== undefined ? { fetchFn: options.fetchFn } : {}),\n log: (...args: unknown[]) => {\n this.#lastLog = args;\n },\n });\n this.#bus = new MessageBus((origin) => this.#detector.matches(origin), {\n onUpdate: (message) => {\n void this.#handle(message);\n },\n onDocumentEvent: () => {\n // A save is the admin's business; the document itself arrives as an\n // update like any other.\n },\n sourcePolicy: options.eventSourcePolicy ?? 'parent-or-opener',\n });\n this.#target = options.target;\n this.#initial = { data: initialData, isLoading: true, status: 'idle', error: undefined };\n this.#snapshot = this.#initial;\n }\n\n /**\n * Attach on the first subscriber and detach with the last, so a component\n * that mounts twice (React's strict mode) listens once and a page that\n * unmounts the hook stops listening at all.\n */\n subscribe = (listener: Listener): (() => void) => {\n this.#listeners.add(listener);\n if (!this.#attached) this.#attach();\n return () => {\n this.#listeners.delete(listener);\n if (this.#listeners.size === 0) this.#detach();\n };\n };\n\n getSnapshot = (): DocumentSnapshot<T> => this.#snapshot;\n\n /** Server rendering has no window and no messages: the initial snapshot a client render starts from. */\n getServerSnapshot = (): DocumentSnapshot<T> => this.#initial;\n\n #attach(): void {\n if (typeof window === 'undefined' && this.#target === undefined) return;\n this.#attached = true;\n this.#bus.attach(this.#target ?? window);\n MessageBus.sendReady(this.#readyTargets(), this.#detector.enumerate());\n }\n\n #detach(): void {\n if (!this.#attached) return;\n this.#attached = false;\n this.#bus.detach();\n this.#merger.destroy();\n }\n\n #readyTargets(): Window[] {\n const host = this.#target ?? (typeof window === 'undefined' ? undefined : window);\n if (host === undefined) return [];\n const targets: Window[] = [];\n if (host.parent !== host) targets.push(host.parent);\n const opener: unknown = host.opener;\n if (opener !== null && opener !== undefined && opener !== host) targets.push(opener as Window);\n return targets;\n }\n\n async #handle(message: PayloadLivePreviewMessage): Promise<void> {\n const data = message.data;\n if (data === undefined) return;\n const revision = (this.#revision += 1);\n this.#publish({ ...this.#snapshot, isLoading: true });\n this.#lastLog = [];\n const result = await this.#merger.merge({\n data,\n ...(message.collectionSlug !== undefined ? { collectionSlug: message.collectionSlug } : {}),\n ...(message.globalSlug !== undefined ? { globalSlug: message.globalSlug } : {}),\n ...(message.locale !== undefined ? { locale: message.locale } : {}),\n });\n // A newer message owns the state now, whatever this one turned out to be.\n if (revision !== this.#revision) return;\n if (result.status === 'superseded') return;\n if (result.status === 'unavailable') {\n this.#publish({\n ...this.#snapshot,\n isLoading: false,\n status: 'unavailable',\n error: new Error(`live preview: ${reasonFrom(this.#lastLog)}`),\n });\n return;\n }\n // The server decides the document's shape; `T` is what the caller says it is.\n this.#publish({\n data: result.doc as T,\n isLoading: false,\n status: 'live',\n error: undefined,\n });\n }\n\n #publish(next: DocumentSnapshot<T>): void {\n const current = this.#snapshot;\n if (\n current.data === next.data &&\n current.isLoading === next.isLoading &&\n current.status === next.status &&\n current.error === next.error\n ) {\n return;\n }\n this.#snapshot = next;\n for (const listener of [...this.#listeners]) listener();\n }\n}\n","/**\n * The seam between a host framework's own router and the route strategy.\n *\n * The route strategy answers a revision by fetching the route and morphing it\n * into the living page. On a page whose DOM belongs to a reconciler that is\n * working against the framework, and it was seen to break once: `removeChild`\n * on `null` inside React's commit phase, over markup this package had patched.\n * A host that re-renders the route itself does the same job without the morph\n * and without the second HTML request, so it is given the chance to.\n *\n * Why a named slot on `window` and not a reference: the runtime is an inline\n * script that builds its strategy before any application code has hydrated, so\n * there is nothing to hand a component; `window.__livePreview`, the only other\n * public surface, is created later and not at all outside a preview; and the\n * two richer extension points — plugins and events — are reachable only through\n * the programmatic client, which a page using an adapter does not have. One\n * slot, written by whoever mounts and read at the moment of a refresh, is the\n * smallest thing that survives both orders of arrival.\n */\n\n/**\n * A host's own route refresh. It must settle once the fresh markup is\n * committed: the runtime re-applies the revision on top of it, and doing that\n * against the markup being replaced would write into nodes about to be dropped.\n */\nexport type RouteRefresh = () => void | Promise<void>;\n\n/** Read late, so it does not matter whether the runtime or the component came first. */\nconst SLOT = '__livePreviewRouteRefresh';\n\ntype WindowSlot = Record<string, RouteRefresh | undefined>;\n\nfunction slot(): WindowSlot | undefined {\n return typeof window === 'undefined' ? undefined : (window as unknown as WindowSlot);\n}\n\n/**\n * Lend the runtime the host's route refresh; the return value undoes it, for a\n * component's cleanup. A later registration wins, and undoing one that has\n * already been replaced does nothing — two components on one page would\n * otherwise take the slot from each other on unmount.\n */\nexport function registerRouteRefresh(refresh: RouteRefresh): () => void {\n const target = slot();\n if (target === undefined) return () => undefined;\n target[SLOT] = refresh;\n return () => {\n // Cleared rather than deleted: the slot is read through `?.` anyway, and\n // removing the key buys nothing a lint rule would not have to be argued out of.\n if (target[SLOT] === refresh) target[SLOT] = undefined;\n };\n}\n\n/** The registered refresh, or `undefined` on a page that registered none. */\nexport function readRouteRefresh(): RouteRefresh | undefined {\n return slot()?.[SLOT];\n}\n","/**\n * `<LivePreviewRouteRefresh />` — lends the runtime the host router's own\n * refresh, so a route update is a re-render the framework performs rather than\n * fresh HTML this package morphs over the reconciler's nodes (see\n * `src/core/route-refresh.ts` for why that is worth avoiding).\n *\n * It takes the refresh as a prop instead of reaching for `next/navigation`,\n * because `next` is not a dependency of this package and must not become one\n * for a component this small. In Next's App Router that is one client\n * component:\n *\n * ```tsx\n * 'use client';\n * import { useRouter } from 'next/navigation';\n * import { LivePreviewRouteRefresh } from 'payload-live-preview/react';\n *\n * export function LivePreviewRefresh() {\n * return <LivePreviewRouteRefresh refresh={useRouter().refresh} />;\n * }\n * ```\n *\n * The same component serves any router with a refresh of that shape.\n */\n\nimport { useEffect, useRef, useTransition } from 'react';\nimport { registerRouteRefresh } from '@core/route-refresh';\n\nexport interface LivePreviewRouteRefreshProps {\n /** The host router's refresh — `useRouter().refresh` in Next's App Router. */\n readonly refresh: () => void;\n}\n\n/** Resolve everyone waiting on the transition that has just finished. */\nfunction settle(waiting: { current: (() => void)[] }): void {\n const settled = waiting.current;\n waiting.current = [];\n for (const resolve of settled) resolve();\n}\n\n/**\n * Renders nothing. Mount it once inside the preview tree; a second one takes\n * the registration from the first, and unmounting gives it back.\n */\nexport function LivePreviewRouteRefresh({ refresh }: LivePreviewRouteRefreshProps): null {\n const [pending, startTransition] = useTransition();\n const waiting = useRef<(() => void)[]>([]);\n\n // The runtime re-applies the revision on top of the fresh markup, so the\n // refresh it is given may not resolve before that markup is committed. The\n // transition is what makes that observable: it is pending until React has\n // rendered the new server output, and this effect runs on the commit after.\n useEffect(() => {\n if (!pending) settle(waiting);\n });\n\n useEffect(() => {\n const undo = registerRouteRefresh(\n () =>\n new Promise<void>((resolve) => {\n waiting.current.push(resolve);\n startTransition(() => {\n refresh();\n });\n }),\n );\n // A tree that unmounts mid-refresh will never commit one, so the waiting\n // runtime is released here rather than left holding a promise for good.\n return () => {\n undo();\n settle(waiting);\n };\n }, [refresh, startTransition]);\n\n return null;\n}\n","/**\n * The React hook: Payload's `useLivePreview` shape, with this package's merge\n * underneath — one request per accepted update, a newer one aborting the one in\n * flight, an HTTP error refused instead of becoming the document, and a session\n * that belongs to this hook alone (ADR 0002).\n *\n * It re-renders your component tree, so conditional sections and derived values\n * are as correct as a server render. What it cannot do is keep the visitor's\n * state: React re-renders the subtree, and focus, scroll and typed values go\n * with it. The DOM runtime is the other side of that trade (docs/react.md).\n *\n * `react` is an optional peer. This entry imports it at module scope; `./nextjs`\n * loads it, and `react-dom/server`, only at the first render that needs them.\n *\n * The published file starts with `'use client'`. It is not written here because\n * esbuild drops a module directive when it bundles, and a banner with it; the\n * build writes it back through terser's `preamble` (`DIRECTIVE_ENTRIES` in\n * scripts/package-entries.ts, applied in scripts/build-dist.ts).\n */\n\nimport { useRef, useSyncExternalStore } from 'react';\nimport {\n DocumentSession,\n type DocumentSessionOptions,\n type DocumentSnapshot,\n type DocumentStatus,\n} from '@adapters/shared/document-session';\n\nexport type { DocumentSessionOptions, DocumentSnapshot, DocumentStatus };\n\nexport { LivePreviewRouteRefresh, type LivePreviewRouteRefreshProps } from './route-refresh';\nexport { registerRouteRefresh, type RouteRefresh } from '@core/route-refresh';\n\nexport interface UseLivePreviewDocumentOptions<T> extends DocumentSessionOptions {\n /** The document the page was rendered from; returned until an update merges. */\n readonly initialData: T;\n}\n\n/**\n * Subscribe to the admin's updates and return the merged document.\n *\n * ```tsx\n * const { data, status } = useLivePreviewDocument<Page>({\n * serverURL: process.env.NEXT_PUBLIC_PAYLOAD_URL!,\n * allowedOrigins: [process.env.NEXT_PUBLIC_PAYLOAD_URL!],\n * initialData: page,\n * depth: 1,\n * });\n * ```\n *\n * Returns `{ data, isLoading }` as Payload's hook does, plus `status` and\n * `error`: a merge that fails keeps the last good document instead of leaving\n * the page on a stale one with no way to tell.\n */\nexport function useLivePreviewDocument<T>(\n options: UseLivePreviewDocumentOptions<T>,\n): DocumentSnapshot<T> {\n const session = useSessionFor(options);\n return useSyncExternalStore(session.subscribe, session.getSnapshot, session.getServerSnapshot);\n}\n\n/**\n * One session per hook instance, rebuilt only when a connection option changes.\n * `initialData` is deliberately not one of them: a re-render with a fresh object\n * identity must not throw away a document that already merged.\n */\nfunction useSessionFor<T>(options: UseLivePreviewDocumentOptions<T>): DocumentSession<T> {\n const {\n initialData,\n serverURL,\n apiRoute,\n depth,\n allowedOrigins,\n eventSourcePolicy,\n enableReferrerDetection,\n enableLocalhostMatching,\n fetchFn,\n target,\n } = options;\n const key = [\n serverURL,\n apiRoute ?? '',\n String(depth ?? ''),\n (allowedOrigins ?? []).join(' '),\n eventSourcePolicy ?? '',\n String(enableReferrerDetection ?? ''),\n String(enableLocalhostMatching ?? ''),\n // A separator no origin, route prefix or policy name can contain.\n ].join(' | ');\n const held = useRef<{ key: string; session: DocumentSession<T> } | null>(null);\n if (held.current?.key !== key) {\n held.current = {\n key,\n session: new DocumentSession<T>(initialData, {\n serverURL,\n ...(apiRoute !== undefined ? { apiRoute } : {}),\n ...(depth !== undefined ? { depth } : {}),\n ...(allowedOrigins !== undefined ? { allowedOrigins } : {}),\n ...(eventSourcePolicy !== undefined ? { eventSourcePolicy } : {}),\n ...(enableReferrerDetection !== undefined ? { enableReferrerDetection } : {}),\n ...(enableLocalhostMatching !== undefined ? { enableLocalhostMatching } : {}),\n ...(fetchFn !== undefined ? { fetchFn } : {}),\n ...(target !== undefined ? { target } : {}),\n }),\n };\n }\n return held.current.session;\n}\n"],"mappings":";2FAQO,SAASA,EAAsBC,GACpC,OAAsB,iBAAVA,GAAgC,OAAVA,GAAoC,mBAAVA,IACD,mBAA/CA,EAAsCC,OAC7CC,QAAQC,QAAQH,GAAOI,MAAM,SAC3B,EACT,CCOO,SAASC,IAEhB,CCsBA,SAASC,EAAcN,EAAeO,GACpC,OACEP,EAAMQ,OAAS,GACfR,EAAMQ,QAAUD,GACN,MAAVP,GACU,OAAVA,IACCA,EAAMS,SAAS,OACfT,EAAMS,SAAS,QAjBpB,SAA6BT,GAC3B,UAAWU,KAAaV,EAAO,CAE7B,MAAMW,EAAYD,EAAUE,YAAY,IAAM,EAC9C,GAAID,GAAa,IAAsB,MAAdA,EAAoB,OAAO,CACtD,CACA,OAAO,CACT,CAWKE,CAAoBb,EAEzB,CAGA,SAASc,EAAWd,GAClB,OAAOM,EAAcN,E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