@salesforce/storefront-next-runtime 0.3.1-alpha.1 → 0.4.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +82 -0
- package/dist/DesignComponent.js +37 -12
- package/dist/DesignComponent.js.map +1 -1
- package/dist/DesignContext.js +47 -2
- package/dist/DesignContext.js.map +1 -1
- package/dist/DesignFrame.js +1 -1
- package/dist/DesignRegion.js +1 -1
- package/dist/config.d.ts +4 -4
- package/dist/custom-global-preferences.d.ts +20 -0
- package/dist/custom-global-preferences.d.ts.map +1 -0
- package/dist/custom-global-preferences.js +28 -0
- package/dist/custom-global-preferences.js.map +1 -0
- package/dist/custom-site-preferences.d.ts +20 -0
- package/dist/custom-site-preferences.d.ts.map +1 -0
- package/dist/custom-site-preferences.js +28 -0
- package/dist/custom-site-preferences.js.map +1 -0
- package/dist/data-store-custom-global-preferences.d.ts +2 -0
- package/dist/data-store-custom-global-preferences.js +6 -0
- package/dist/data-store-custom-site-preferences.d.ts +2 -0
- package/dist/data-store-custom-site-preferences.js +6 -0
- package/dist/data-store-gcp-preferences.d.ts +2 -0
- package/dist/data-store-gcp-preferences.js +6 -0
- package/dist/data-store.d.ts +97 -0
- package/dist/data-store.d.ts.map +1 -0
- package/dist/data-store.js +42 -0
- package/dist/data-store.js.map +1 -0
- package/dist/design-data.d.ts +82 -88
- package/dist/design-data.d.ts.map +1 -1
- package/dist/design-data.js +95 -57
- package/dist/design-data.js.map +1 -1
- package/dist/design-messaging.d.ts +2 -2
- package/dist/events.d.ts +34 -6
- package/dist/events.d.ts.map +1 -1
- package/dist/events.js +6 -6
- package/dist/events.js.map +1 -1
- package/dist/gcp-preferences.d.ts +52 -0
- package/dist/gcp-preferences.d.ts.map +1 -0
- package/dist/gcp-preferences.js +61 -0
- package/dist/gcp-preferences.js.map +1 -0
- package/dist/i18n-client.d.ts +38 -0
- package/dist/i18n-client.d.ts.map +1 -0
- package/dist/i18n-client.js +72 -0
- package/dist/i18n-client.js.map +1 -0
- package/dist/i18n.d.ts +63 -0
- package/dist/i18n.d.ts.map +1 -0
- package/dist/i18n.js +98 -0
- package/dist/i18n.js.map +1 -0
- package/dist/index.d.ts +60 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/messaging-api.js +3 -1
- package/dist/messaging-api.js.map +1 -1
- package/dist/scapi.d.ts +247 -2
- package/dist/scapi.d.ts.map +1 -1
- package/dist/scapi.js +1 -1
- package/dist/scapi.js.map +1 -1
- package/dist/site-context.d.ts +94 -18
- package/dist/site-context.d.ts.map +1 -1
- package/dist/site-context.js +2 -417
- package/dist/site-context2.js +513 -0
- package/dist/site-context2.js.map +1 -0
- package/dist/types2.d.ts +210 -0
- package/dist/types2.d.ts.map +1 -1
- package/dist/utils.js +179 -0
- package/dist/utils.js.map +1 -0
- package/package.json +63 -4
- package/dist/site-context.js.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","names":["providerPromise: Promise<DataStoreProvider> | null","providerPromise","dataStoreMiddleware: MiddlewareFunction<Response>"],"sources":["../src/data-store/provider.ts","../src/data-store/utils.ts"],"sourcesContent":["/**\n * Copyright 2026 Salesforce, Inc.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { DataStore } from '@salesforce/mrt-utilities/middleware';\nimport { hasMrtEnvironment, isDevelopmentEnvironment, tryImportLocalProvider } from './utils';\n\nexport type DataStoreEntry<TValue = unknown> = {\n value?: TValue;\n};\n\nexport type DataStoreProvider = {\n kind: 'mrt' | 'local';\n getEntry: <TValue = unknown>(key: string) => Promise<DataStoreEntry<TValue> | null>;\n};\n\nlet providerPromise: Promise<DataStoreProvider> | null = null;\n\n/**\n * Reset the cached provider promise.\n *\n * Intended for tests to ensure isolated provider resolution.\n */\nexport function resetDataStoreProviderCache(): void {\n providerPromise = null;\n}\n\n/**\n * Resolve the default data-store provider based on MRT environment variables.\n *\n * Environment variables:\n * - `AWS_REGION` (required for MRT): AWS region for the data store table (e.g., \"us-east-1\")\n * - `MOBIFY_PROPERTY_ID` (required for MRT): MRT property identifier (e.g., \"abcd1234\")\n * - `DEPLOY_TARGET` (required for MRT): MRT deploy target (e.g., \"production\")\n * - `SFNEXT_DATA_STORE_ALLOW_LOCAL` (optional): allow local provider outside development (\"true\")\n * - `CI` (optional): allow local provider when set to \"true\"\n *\n * @returns Provider promise resolved for the current environment.\n * @example\n * const provider = await getDefaultDataStoreProvider();\n * const entry = await provider.getEntry('custom-global-preferences');\n */\nexport function getDefaultDataStoreProvider(): Promise<DataStoreProvider> {\n if (providerPromise) {\n return providerPromise;\n }\n\n providerPromise = hasMrtEnvironment() ? Promise.resolve(createMrtDataStoreProvider()) : resolveNonMrtProvider();\n\n return providerPromise;\n}\n\n/**\n * Create the MRT data-store provider.\n *\n * @returns MRT provider backed by `@salesforce/mrt-utilities`.\n * @example\n * const provider = createMrtDataStoreProvider();\n * await provider.getEntry('custom-global-preferences');\n */\nfunction createMrtDataStoreProvider(): DataStoreProvider {\n return {\n kind: 'mrt',\n getEntry: async <TValue = unknown>(key: string) =>\n (await DataStore.getDataStore().getEntry(key)) as DataStoreEntry<TValue> | null,\n };\n}\n\n/**\n * Load the local data-store provider for development.\n *\n * @returns Local provider loaded via dynamic import.\n * @example\n * const provider = await loadLocalDataStoreProvider();\n * await provider.getEntry('custom-global-preferences');\n */\nasync function loadLocalDataStoreProvider(): Promise<DataStoreProvider> {\n const module = await tryImportLocalProvider();\n if (typeof module.createLocalDataStoreProvider !== 'function') {\n throw new Error('Missing createLocalDataStoreProvider export.');\n }\n return module.createLocalDataStoreProvider();\n}\n\n/**\n * Resolve the non-MRT provider based on environment.\n *\n * Environment variables:\n * - `SFNEXT_DATA_STORE_ALLOW_LOCAL` (optional): allow local provider outside development (\"true\")\n * - `CI` (optional): allow local provider when set to \"true\"\n *\n * @returns Local provider in development, otherwise throws.\n * @example\n * const provider = await resolveNonMrtProvider();\n */\nasync function resolveNonMrtProvider(): Promise<DataStoreProvider> {\n const allowLocalProvider =\n isDevelopmentEnvironment() || process.env.SFNEXT_DATA_STORE_ALLOW_LOCAL === 'true' || process.env.CI === 'true';\n\n if (allowLocalProvider) {\n return loadLocalDataStoreProvider();\n }\n\n throw new Error('Data store is unavailable. Ensure AWS_REGION, MOBIFY_PROPERTY_ID, and DEPLOY_TARGET are set.');\n}\n","/**\n * Copyright 2026 Salesforce, Inc.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { type MiddlewareFunction, type RouterContextProvider, createContext } from 'react-router';\nimport {\n DataStoreNotFoundError,\n DataStoreServiceError,\n DataStoreUnavailableError,\n} from '@salesforce/mrt-utilities/middleware';\nimport { getDefaultDataStoreProvider, type DataStoreProvider } from './provider';\nimport { siteContext } from '../site-context';\n\nexport type DataStoreContextKey<T> = ReturnType<typeof createContext<T | null>>;\n\nexport type DataStoreEntryKey = string | ((context: Readonly<RouterContextProvider>) => string);\n\nexport type DataStoreMiddlewareOptions<T> = {\n entryKey: DataStoreEntryKey;\n context: DataStoreContextKey<T>;\n transform?: (value: Record<string, unknown>) => T;\n provider?: DataStoreProvider | Promise<DataStoreProvider>;\n};\n\n/**\n * Creates a typed React Router context for data store entries.\n *\n * Initializes the context with `null` so middleware can populate it during requests.\n *\n * @returns React Router context key for data store values\n */\nexport function createDataStoreContext<T>(): DataStoreContextKey<T> {\n return createContext<T | null>(null);\n}\n\n/**\n * Creates a data-store middleware that fetches site preferences from MRT data access layer\n * and stores them in the router context.\n *\n * Environment variables:\n * - `AWS_REGION` (required): AWS region for the data store table (e.g., \"us-east-1\")\n * - `MOBIFY_PROPERTY_ID` (required): MRT property identifier (e.g., \"abcd1234\")\n * - `DEPLOY_TARGET` (required): MRT deploy target (e.g., \"production\")\n *\n * @param options - Middleware options for data store entry and context\n * @returns React Router middleware for server requests\n */\nexport function createDataStoreMiddleware<T>(options: DataStoreMiddlewareOptions<T>): MiddlewareFunction<Response> {\n const { entryKey, context: contextKey } = options;\n const transform = options.transform ?? ((value: Record<string, unknown>) => value as T);\n const providerPromise = options.provider ? Promise.resolve(options.provider) : getDefaultDataStoreProvider();\n\n const dataStoreMiddleware: MiddlewareFunction<Response> = async ({ context }, next) => {\n const resolvedEntryKey = typeof entryKey === 'function' ? entryKey(context) : entryKey;\n try {\n const provider = await providerPromise;\n const entry = await provider.getEntry(resolvedEntryKey);\n\n if (!entry?.value || typeof entry.value !== 'object') {\n // eslint-disable-next-line no-console\n console.warn(`Data store entry '${resolvedEntryKey}' not found or invalid.`);\n return next();\n }\n context.set(contextKey, transform(entry.value as Record<string, unknown>));\n } catch (error) {\n if (error instanceof DataStoreUnavailableError) {\n throw new Error(\n 'Data store is unavailable. Ensure AWS_REGION, MOBIFY_PROPERTY_ID, and DEPLOY_TARGET are set.'\n );\n }\n if (error instanceof DataStoreNotFoundError) {\n // eslint-disable-next-line no-console\n console.warn(`Data store entry '${resolvedEntryKey}' not found.`);\n return next();\n }\n if (error instanceof DataStoreServiceError) {\n throw new Error(`Data store request failed for '${resolvedEntryKey}'.`);\n }\n throw error;\n }\n\n return next();\n };\n\n return dataStoreMiddleware;\n}\n\n/**\n * Check whether MRT environment variables are present.\n *\n * @returns True when all MRT environment variables are set.\n * @example\n * if (hasMrtEnvironment()) {\n * // Use MRT provider\n * }\n */\nexport function hasMrtEnvironment(): boolean {\n return Boolean(process.env.AWS_REGION && process.env.MOBIFY_PROPERTY_ID && process.env.DEPLOY_TARGET);\n}\n\n/**\n * Check whether the runtime is in a development environment.\n *\n * @returns True when NODE_ENV is not \"production\".\n * @example\n * if (isDevelopmentEnvironment()) {\n * // Load local provider\n * }\n */\nexport function isDevelopmentEnvironment(): boolean {\n return process.env.NODE_ENV !== 'production';\n}\n\n/**\n * Attempt to import the local provider from the dev package or workspace path.\n *\n * @returns Local provider module.\n * @throws Error when the provider cannot be resolved.\n * @example\n * const module = await tryImportLocalProvider();\n * const provider = module.createLocalDataStoreProvider();\n */\nexport async function tryImportLocalProvider() {\n try {\n // @ts-expect-error - resolved at runtime from dev package\n return await import(/* @vite-ignore */ '@salesforce/storefront-next-dev/data-store/local-provider');\n } catch (error) {\n throw new Error(\n 'Failed to load local data-store provider. Ensure @salesforce/storefront-next-dev is installed.',\n { cause: error }\n );\n }\n}\n\n/**\n * Creates an entryKey function that prefixes the given suffix with the current site ID.\n *\n * @param suffix - The entry key suffix (e.g., \"custom-site-preferences\")\n * @returns A function compatible with `DataStoreMiddlewareOptions.entryKey`\n */\nexport function prefixWithSiteId(suffix: string): (context: Readonly<RouterContextProvider>) => string {\n return (context) => {\n const siteId = context.get(siteContext)?.site?.id;\n if (!siteId)\n throw new Error('Site id not found. Ensure site context middleware runs before data-store middleware.');\n return `${siteId}-${suffix}`;\n };\n}\n"],"mappings":";;;;;AA4BA,IAAIA,kBAAqD;;;;;;;;;;;;;;;;AA0BzD,SAAgB,8BAA0D;AACtE,KAAI,gBACA,QAAO;AAGX,mBAAkB,mBAAmB,GAAG,QAAQ,QAAQ,4BAA4B,CAAC,GAAG,uBAAuB;AAE/G,QAAO;;;;;;;;;;AAWX,SAAS,6BAAgD;AACrD,QAAO;EACH,MAAM;EACN,UAAU,OAAyB,QAC9B,MAAM,UAAU,cAAc,CAAC,SAAS,IAAI;EACpD;;;;;;;;;;AAWL,eAAe,6B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,IAAI,cAAc,QAAQ,IAAI,sBAAsB,QAAQ,IAAI,cAAc;;;;;;;;;;;AAYzG,SAAgB,2BAAoC;AAChD,QAAO,QAAQ,IAAI,aAAa;;;;;;;;;;;AAYpC,eAAsB,yBAAyB;AAC3C,KAAI;AAEA,SAAO,MAAM;;GAA0B;;UAClC,OAAO;AACZ,QAAM,IAAI,MACN,kGACA,EAAE,OAAO,OAAO,CACnB;;;;;;;;;AAUT,SAAgB,iBAAiB,QAAsE;AACnG,SAAQ,YAAY;EAChB,MAAM,SAAS,QAAQ,IAAI,YAAY,EAAE,MAAM;AAC/C,MAAI,CAAC,OACD,OAAM,IAAI,MAAM,uFAAuF;AAC3G,SAAO,GAAG,OAAO,GAAG"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@salesforce/storefront-next-runtime",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0-alpha.0",
|
|
4
4
|
"description": "Runtime agnostic libraries for SFCC Storefront Next",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -62,6 +62,30 @@
|
|
|
62
62
|
"default": "./dist/events.js"
|
|
63
63
|
}
|
|
64
64
|
},
|
|
65
|
+
"./data-store": {
|
|
66
|
+
"import": {
|
|
67
|
+
"types": "./dist/data-store.d.ts",
|
|
68
|
+
"default": "./dist/data-store.js"
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
"./data-store/custom-site-preferences": {
|
|
72
|
+
"import": {
|
|
73
|
+
"types": "./dist/data-store-custom-site-preferences.d.ts",
|
|
74
|
+
"default": "./dist/data-store-custom-site-preferences.js"
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
"./data-store/custom-global-preferences": {
|
|
78
|
+
"import": {
|
|
79
|
+
"types": "./dist/data-store-custom-global-preferences.d.ts",
|
|
80
|
+
"default": "./dist/data-store-custom-global-preferences.js"
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
"./data-store/gcp-preferences": {
|
|
84
|
+
"import": {
|
|
85
|
+
"types": "./dist/data-store-gcp-preferences.d.ts",
|
|
86
|
+
"default": "./dist/data-store-gcp-preferences.js"
|
|
87
|
+
}
|
|
88
|
+
},
|
|
65
89
|
"./site-context": {
|
|
66
90
|
"import": {
|
|
67
91
|
"types": "./dist/site-context.d.ts",
|
|
@@ -91,6 +115,18 @@
|
|
|
91
115
|
"types": "./dist/workspace.d.ts",
|
|
92
116
|
"default": "./dist/workspace.js"
|
|
93
117
|
}
|
|
118
|
+
},
|
|
119
|
+
"./i18n": {
|
|
120
|
+
"import": {
|
|
121
|
+
"types": "./dist/i18n.d.ts",
|
|
122
|
+
"default": "./dist/i18n.js"
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
"./i18n/client": {
|
|
126
|
+
"import": {
|
|
127
|
+
"types": "./dist/i18n-client.d.ts",
|
|
128
|
+
"default": "./dist/i18n-client.js"
|
|
129
|
+
}
|
|
94
130
|
}
|
|
95
131
|
},
|
|
96
132
|
"files": [
|
|
@@ -105,6 +141,7 @@
|
|
|
105
141
|
"author": "cc-pwa-kit@salesforce.com",
|
|
106
142
|
"license": "Apache-2.0",
|
|
107
143
|
"dependencies": {
|
|
144
|
+
"@salesforce/mrt-utilities": "0.1.0",
|
|
108
145
|
"jiti": "^2.6.1",
|
|
109
146
|
"openapi-fetch": "0.15.0"
|
|
110
147
|
},
|
|
@@ -112,7 +149,6 @@
|
|
|
112
149
|
"@react-router/dev": "7.12.0",
|
|
113
150
|
"@react-router/fs-routes": "7.12.0",
|
|
114
151
|
"@testing-library/react": "^16.1.0",
|
|
115
|
-
"react-router": "7.12.0",
|
|
116
152
|
"@types/node": "^24.0.0",
|
|
117
153
|
"@types/react": "^19.0.0",
|
|
118
154
|
"@types/react-dom": "^19.0.0",
|
|
@@ -120,20 +156,43 @@
|
|
|
120
156
|
"@vitejs/plugin-react": "5.0.3",
|
|
121
157
|
"@vitest/coverage-v8": "4.0.18",
|
|
122
158
|
"happy-dom": "20.0.10",
|
|
159
|
+
"i18next": "25.5.3",
|
|
160
|
+
"i18next-browser-languagedetector": "8.2.0",
|
|
123
161
|
"openapi-typescript": "7.10.1",
|
|
124
162
|
"react": "19.2.3",
|
|
125
163
|
"react-dom": "19.2.3",
|
|
164
|
+
"react-i18next": "16.0.0",
|
|
165
|
+
"react-router": "7.12.0",
|
|
166
|
+
"remix-i18next": "7.4.2",
|
|
126
167
|
"shx": "^0.4.0",
|
|
127
168
|
"tsdown": "^0.15.4",
|
|
128
169
|
"typescript": "^5.6.0",
|
|
129
170
|
"vitest": "4.0.18"
|
|
130
171
|
},
|
|
131
172
|
"peerDependencies": {
|
|
132
|
-
"@react-router/fs-routes": ">=7.0.0",
|
|
133
173
|
"@react-router/dev": ">=7.0.0",
|
|
174
|
+
"@react-router/fs-routes": ">=7.0.0",
|
|
175
|
+
"i18next": ">=25.0.0",
|
|
176
|
+
"i18next-browser-languagedetector": ">=8.0.0",
|
|
134
177
|
"react": ">=19.0.0",
|
|
135
178
|
"react-dom": ">=19.0.0",
|
|
136
|
-
"react-
|
|
179
|
+
"react-i18next": ">=16.0.0",
|
|
180
|
+
"react-router": ">=7.0.0",
|
|
181
|
+
"remix-i18next": ">=7.0.0"
|
|
182
|
+
},
|
|
183
|
+
"peerDependenciesMeta": {
|
|
184
|
+
"i18next": {
|
|
185
|
+
"optional": true
|
|
186
|
+
},
|
|
187
|
+
"i18next-browser-languagedetector": {
|
|
188
|
+
"optional": true
|
|
189
|
+
},
|
|
190
|
+
"react-i18next": {
|
|
191
|
+
"optional": true
|
|
192
|
+
},
|
|
193
|
+
"remix-i18next": {
|
|
194
|
+
"optional": true
|
|
195
|
+
}
|
|
137
196
|
},
|
|
138
197
|
"engines": {
|
|
139
198
|
"node": ">=24.0.0"
|
package/dist/site-context.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"site-context.js","names":["result: Record<string, string>","resolvers: Record<DetectionMethod, () => Promise<string | null>>","DEFAULT_SITE_DETECTION: Required<DetectionConfig>","DEFAULT_LOCALE_DETECTION: Required<DetectionConfig>","locale: Locale | null","resolvers: Record<DetectionMethod, () => Promise<string | null>>","createContext","siteDetectionConfig: SiteSettings['siteDetectionConfig']","localeDetectionConfig: SiteSettings['localeDetectionConfig']","settings: SiteSettings","siteContextMiddleware: MiddlewareFunction<Response>"],"sources":["../src/site-context/site-context.tsx","../src/site-context/build-url.ts","../src/site-context/utils.ts","../src/site-context/site-detection.ts","../src/site-context/configs.ts","../src/site-context/cookies.ts","../src/site-context/locale-detection.ts","../src/site-context/middleware.ts"],"sourcesContent":["/**\n * Copyright 2026 Salesforce, Inc.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { createContext, useContext, type PropsWithChildren } from 'react';\nimport type { Site } from './types';\n\nconst SiteContext = createContext<Site | undefined>(undefined);\n\n/**\n * Provides the current site to the component tree.\n * Follows the same pattern as CurrencyProvider.\n *\n * Mounted in the template (e.g., app-wrapper.tsx or root.tsx) with the resolved\n * site value from the loader/middleware.\n */\nexport function SiteProvider({ value, children }: PropsWithChildren<{ value: Site }>) {\n return <SiteContext.Provider value={value}>{children}</SiteContext.Provider>;\n}\n\n/**\n * React hook to get the current site.\n * Returns undefined when no SiteProvider is mounted.\n */\n// eslint-disable-next-line react-refresh/only-export-components\nexport function useSite(): Site | undefined {\n return useContext(SiteContext);\n}\n","/**\n * Copyright 2026 Salesforce, Inc.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport type { Url } from '../config/types';\n\n/**\n * Parses search config string into key-value pairs, preserving ':param' placeholders.\n * '?lng=:localeId&site=:siteId' → { lng: ':localeId', site: ':siteId' }\n */\nexport function parseSearchConfig(search: string): Record<string, string> {\n const searchParams = new URLSearchParams(search);\n const result: Record<string, string> = {};\n for (const [key, value] of searchParams) {\n result[key] = value;\n }\n return result;\n}\n\n/**\n * Extracts parameter names from a prefix string.\n * '/:siteId/:localeId' → ['siteId', 'localeId']\n */\nexport function extractPrefixParams(prefix: string): string[] {\n const matches = prefix.match(/:(\\w+)/g);\n return matches ? matches.map((m) => m.slice(1)) : [];\n}\n\n/**\n * Splits a URL string into its component parts.\n * '/product/123?color=red#details' → { pathname: '/product/123', search: 'color=red', hash: '#details' }\n */\nexport function decomposeUrl(url: string): { pathname: string; search: string; hash: string } {\n const hashIdx = url.indexOf('#');\n const hash = hashIdx >= 0 ? url.slice(hashIdx) : '';\n const withoutHash = hashIdx >= 0 ? url.slice(0, hashIdx) : url;\n const searchIdx = withoutHash.indexOf('?');\n const search = searchIdx >= 0 ? withoutHash.slice(searchIdx + 1) : '';\n const pathname = searchIdx >= 0 ? withoutHash.slice(0, searchIdx) : withoutHash;\n return { pathname, search, hash };\n}\n\n/**\n * Resolves a prefix template by replacing parameter placeholders with values.\n * ('/:siteId/:localeId', { siteId: 'global', localeId: 'en-GB' }) → '/global/en-GB'\n */\nexport function resolvePrefix(prefix: string, params: Record<string, string>): string {\n let resolved = prefix;\n for (const paramName of extractPrefixParams(prefix)) {\n const value = params[paramName];\n if (value) {\n resolved = resolved.replace(`:${paramName}`, value);\n }\n }\n return resolved;\n}\n\n/**\n * Strips the URL prefix segments from a pathname based on a prefix pattern.\n * Since all routes are configured with the prefix baked in, segment counting is sufficient.\n *\n * @param pathname - Full pathname (e.g. '/global/en-GB/checkout')\n * @param prefixPattern - URL prefix pattern from config (e.g. '/:siteId/:localeId')\n * @returns Pathname with prefix stripped (e.g. '/checkout'), or original if\n * the pathname has fewer segments than the prefix\n *\n * @example\n * stripPathPrefix('/global/en-GB/checkout', '/:siteId/:localeId') // → '/checkout'\n * stripPathPrefix('/checkout', '/:siteId/:localeId') // → '/checkout' (fewer segments → unchanged)\n * stripPathPrefix('/checkout', '') // → '/checkout' (no prefix configured)\n * stripPathPrefix('/', '/:siteId/:localeId') // → '/'\n */\nexport function stripPathPrefix(pathname: string, prefixPattern: string): string {\n if (!prefixPattern) return pathname;\n\n const prefixSegmentCount = prefixPattern.split('/').filter(Boolean).length;\n const pathSegments = pathname.split('/').filter(Boolean);\n\n if (pathSegments.length <= prefixSegmentCount) {\n return pathSegments.length === prefixSegmentCount ? '/' : pathname;\n }\n\n return `/${pathSegments.slice(prefixSegmentCount).join('/')}`;\n}\n\n/**\n * Sanitize a resolved prefix from a pathname if present.\n * sanitizePrefix('/global/en-GB/product/123', '/global/en-GB') → '/product/123'\n * sanitizePrefix('/product/123', '/global/en-GB') → '/product/123' (no-op)\n */\nexport function sanitizePrefix(pathname: string, pathPrefix: string): string {\n if (!pathPrefix) return pathname;\n if (pathname === pathPrefix) return '';\n if (pathname.startsWith(`${pathPrefix}/`)) return pathname.slice(pathPrefix.length);\n return pathname;\n}\n\n/**\n * Builds a fully-qualified URL with site context prefix and search params.\n *\n * Only keys defined in urlConfig.search are set by site context. Any other query params\n * already present on the `to` URL (including duplicate keys) are preserved as-is.\n * e.g. to='/api/search?refine=color:blue&refine=size:M', search='?lng=:localeId'\n * → '/api/search?refine=color:blue&refine=size:M&lng=en-GB'\n *\n * @example\n * buildUrl({ to: '/product/123', urlConfig: { prefix: '/:siteId', search: '?lng=:localeId' }, params: { siteId: 'global', localeId: 'en-GB' } })\n * // → '/global/product/123?lng=en-GB'\n */\nexport function buildUrl({\n to,\n urlConfig,\n params,\n}: {\n to: string;\n urlConfig?: Url;\n params: Record<string, string>;\n}): string {\n if (!urlConfig) return to;\n if (!to || to === '#' || to.startsWith('http') || to.startsWith('//')) return to;\n\n const { pathname, search: existingSearch, hash } = decomposeUrl(to);\n\n const pathPrefix = urlConfig.prefix && urlConfig.prefix !== '/' ? resolvePrefix(urlConfig.prefix, params) : '';\n // sanitize prefix to make sure there is no prefix duplication at any case\n const path = pathPrefix ? `${pathPrefix}${sanitizePrefix(pathname, pathPrefix)}` : pathname;\n\n const searchParams = new URLSearchParams(existingSearch);\n if (urlConfig.search) {\n const searchConfig = parseSearchConfig(urlConfig.search);\n for (const [queryKey, value] of Object.entries(searchConfig)) {\n if (value.startsWith(':')) {\n const paramValue = params[value.slice(1)];\n if (paramValue) {\n searchParams.set(queryKey, paramValue);\n }\n } else {\n searchParams.set(queryKey, value);\n }\n }\n }\n\n const search = searchParams.toString();\n return `${path}${search ? `?${search}` : ''}${hash}`;\n}\n","/**\n * Copyright 2026 Salesforce, Inc.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { Cookie } from 'react-router';\n\n/**\n * Extract a string value from the URL path segment at the given index.\n */\nexport function lookupFromPath(pathname: string, pathIndex: number): string | null {\n const pathSegments = pathname.split('/').filter(Boolean);\n\n if (pathSegments.length <= pathIndex) return null;\n\n return pathSegments[pathIndex];\n}\n\n/**\n * Detect a string value from cookie using the given cookie parser.\n *\n * Returns a promise that resolves to the cookie value.\n */\nexport async function readCookieFromRequest(request: Request, cookie: Cookie): Promise<string | null> {\n const cookies = request.headers.get('Cookie');\n if (!cookies) return null;\n\n const cookieValue = await cookie.parse(cookies);\n return cookieValue;\n}\n","/**\n * Copyright 2026 Salesforce, Inc.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { Cookie } from 'react-router';\nimport type { Site, SiteSettings, DetectionMethod } from './types';\nimport { readCookieFromRequest, lookupFromPath } from './utils';\n\n/**\n * Detect site reference from cookie.\n */\nexport async function readSiteFromCookie(request: Request, cookie: Cookie): Promise<string | null> {\n return readCookieFromRequest(request, cookie);\n}\n\n/**\n * Get site object using the site id or alias\n * 1. Check siteIdentifier against each site's alias; if matched, return that site.\n * 2. Else check against each site's id; if matched, return that site.\n * 3. If no match, return null.\n */\nfunction getSiteFromIdOrAlias(siteIdentifier: string | null, sites: Site[]): Site | null {\n if (!siteIdentifier) return null;\n return sites.find((site) => site.alias === siteIdentifier || site.id === siteIdentifier) ?? null;\n}\n\n/**\n * Resolve site using the configured detection order.\n * Returns the first valid site from the first source that yields a valid value.\n */\nexport async function resolveSite(request: Request, settings: SiteSettings): Promise<Site> {\n const { sites, defaultSiteId, siteDetectionConfig, siteCookie } = settings;\n\n const requestUrl = new URL(request.url);\n\n // When a base path is configured (e.g., '/shop'), we need to skip its path segments.\n // React Router handles the base path internally for hooks like useParams or useLocation,\n // but it does not strip it from request.url. The offset is calculated dynamically from\n // the number of segments in the base path as future-proof in case we support multi-segment\n // base paths in the future.\n const basePathOffset = process.env.MRT_ENV_BASE_PATH\n ? process.env.MRT_ENV_BASE_PATH.split('/').filter(Boolean).length\n : 0;\n\n const resolvers: Record<DetectionMethod, () => Promise<string | null>> = {\n path: () =>\n Promise.resolve(\n lookupFromPath(requestUrl.pathname, siteDetectionConfig.lookupFromPathIndex + basePathOffset)\n ),\n querystring: () => Promise.resolve(requestUrl.searchParams.get(siteDetectionConfig.lookupQuerystring)),\n header: () => Promise.resolve(request.headers.get(siteDetectionConfig.lookupHeader)),\n cookie: async () => readSiteFromCookie(request, siteCookie),\n };\n\n for (const method of siteDetectionConfig.order) {\n const siteIdOrAlias = await resolvers[method]?.();\n const resolvedSite = getSiteFromIdOrAlias(siteIdOrAlias, sites);\n if (resolvedSite) return resolvedSite;\n }\n\n // If no site id was found, use the default site id\n const site = getSiteFromIdOrAlias(defaultSiteId, sites);\n\n // If default site id is invalid, throw an error\n if (!site) {\n throw new Error(`Default site ${defaultSiteId} not found.`);\n }\n\n return site;\n}\n","/**\n * Copyright 2026 Salesforce, Inc.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { DetectionConfig } from './types';\n\n/**\n * Default site detection configuration\n */\nexport const DEFAULT_SITE_DETECTION: Required<DetectionConfig> = {\n order: ['path', 'querystring', 'cookie', 'header'],\n lookupFromPathIndex: 0,\n lookupQuerystring: 'site',\n lookupCookie: 'site_id',\n lookupHeader: 'X-Site-Id',\n caches: ['cookie'],\n};\n\n/**\n * Default locale detection configuration\n */\nexport const DEFAULT_LOCALE_DETECTION: Required<DetectionConfig> = {\n order: ['path', 'querystring', 'cookie', 'header'],\n lookupFromPathIndex: 1,\n lookupQuerystring: 'lng',\n lookupCookie: 'lng',\n lookupHeader: 'Accept-Language',\n caches: ['cookie'],\n};\n","/**\n * Copyright 2026 Salesforce, Inc.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { createCookie, type Cookie } from 'react-router';\n\n/**\n * Cookie options for site context cookies\n */\nexport const COOKIE_OPTIONS = {\n path: '/',\n sameSite: 'lax' as const,\n secure: process.env.NODE_ENV === 'production',\n httpOnly: true,\n};\n\n/**\n * Creates a cookie instance with the given name.\n *\n * @param name - Cookie name\n * @returns Cookie instance configured with site context options\n */\nexport function createSiteContextCookie(name: string): Cookie {\n return createCookie(name, COOKIE_OPTIONS);\n}\n\n/**\n * WeakMap to pass resolved locale from site context middleware to i18next's findLocale.\n * WeakMap allows garbage collection when requests are done.\n * This is necessary because findLocale() only receives the Request object, not the router context.\n */\nexport const requestToLocaleMap = new WeakMap<Request, string>();\n","/**\n * Copyright 2026 Salesforce, Inc.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { Cookie } from 'react-router';\nimport type { DetectionMethod, Locale, SiteSettings, Site } from './types';\nimport { readCookieFromRequest, lookupFromPath } from './utils';\n\n/**\n * Read locale from cookie.\n */\nexport async function readLocaleFromCookie(request: Request, cookie: Cookie): Promise<string | null> {\n return readCookieFromRequest(request, cookie);\n}\n\n/**\n * Get locale object using the locale id or alias.\n * 1. Check localeIdOrAlias against each locale's alias; if matched, return that locale.\n * 2. Else check against each locale's id; if matched, return that locale.\n * 3. If no match, return null (caller should use defaultLocale).\n *\n * @param localeIdentifier - The locale id or alias to get the locale from. Null is allowed because this may come from\n * extrenal sources such as cookies, headers, or query parameters.\n * @param locales - The list of locales to search through.\n * @returns The locale object if found, otherwise null.\n */\nfunction getLocaleFromIdOrAlias(localeIdentifier: string | undefined | null, locales: Locale[]): Locale | null {\n if (!localeIdentifier) return null;\n return locales.find((locale) => locale.alias === localeIdentifier || locale.id === localeIdentifier) ?? null;\n}\n\n/**\n * Resolve locale using the configured detection order.\n * Returns the first valid locale from the first source that yields a valid value.\n */\nexport async function resolveLocale(request: Request, settings: SiteSettings, site: Site): Promise<Locale> {\n const { defaultLocale, localeDetectionConfig, localeCookie } = settings;\n const { supportedLocales } = site;\n\n let locale: Locale | null = null;\n const requestUrl = new URL(request.url);\n\n // When a base path is configured (e.g., '/shop'), we need to skip its path segments.\n // React Router handles the base path internally for hooks like useParams or useLocation,\n // but it does not strip it from request.url. The offset is calculated dynamically from\n // the number of segments in the base path as future-proof in case we support multi-segment\n // base paths in the future.\n const basePathOffset = process.env.MRT_ENV_BASE_PATH\n ? process.env.MRT_ENV_BASE_PATH.split('/').filter(Boolean).length\n : 0;\n\n const resolvers: Record<DetectionMethod, () => Promise<string | null>> = {\n path: () =>\n Promise.resolve(\n lookupFromPath(requestUrl.pathname, localeDetectionConfig.lookupFromPathIndex + basePathOffset)\n ),\n querystring: () => Promise.resolve(requestUrl.searchParams.get(localeDetectionConfig.lookupQuerystring)),\n header: () => Promise.resolve(request.headers.get(localeDetectionConfig.lookupHeader)),\n cookie: async () => readLocaleFromCookie(request, localeCookie),\n };\n\n for (const method of localeDetectionConfig.order) {\n const localeIdOrAlias = await resolvers[method]?.();\n const resolvedLocale = getLocaleFromIdOrAlias(localeIdOrAlias, supportedLocales);\n if (resolvedLocale) return resolvedLocale;\n }\n\n // If no locale was found, use the default locale\n if (!locale) {\n locale = getLocaleFromIdOrAlias(defaultLocale, supportedLocales);\n }\n\n // If default locale is invalid, throw an error\n if (!locale) {\n throw new Error(\n `Default locale ${defaultLocale} not found in the list of supported locales for site ${site.id}.`\n );\n }\n\n return locale;\n}\n","/**\n * Copyright 2026 Salesforce, Inc.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { createContext, type MiddlewareFunction, type RouterContextProvider } from 'react-router';\nimport { resolveSite } from './site-detection';\nimport type { SiteConfig, SiteContext, SiteSettings, Site, Locale } from './types';\nimport { DEFAULT_SITE_DETECTION, DEFAULT_LOCALE_DETECTION } from './configs';\nimport { createSiteContextCookie, requestToLocaleMap } from './cookies';\nimport { resolveLocale } from './locale-detection';\n\nexport const siteContext = createContext<SiteContext | null>(null);\n\ntype MiddlewareArgs = { request: Request; context: Readonly<RouterContextProvider> };\n\n/**\n * Helper function to get site context cookies from router context.\n * Useful in server actions and loaders that need to read/set cookies.\n *\n * @param context - Router context provider\n * @returns Object with siteCookie and localeCookie instances, or null if context not set\n *\n * @example\n * ```typescript\n * export const action: ActionFunction = async ({ request, context }) => {\n * const cookies = getSiteContextCookies(context);\n * if (cookies) {\n * const cookieHeader = await cookies.localeCookie.serialize(locale);\n * // ... use cookieHeader\n * }\n * };\n * ```\n */\nexport function getSiteContextCookies(context: Readonly<RouterContextProvider>) {\n const siteCtx = context.get(siteContext);\n if (!siteCtx) return null;\n return {\n siteCookie: siteCtx.siteCookie,\n localeCookie: siteCtx.localeCookie,\n };\n}\n\n/**\n * Helper function to determine if cookies should be set based on:\n * 1. Whether caching is enabled for each cookie type\n * 2. Whether the resolved value differs from the existing cookie\n * 3. Whether cookies were already set by actions/loaders in the response\n *\n * @param request - Incoming request\n * @param response - Response from next()\n * @param settings - Site context settings with cookie instances and detection config\n * @param site - Resolved site for this request\n * @param locale - Resolved locale for this request\n * @returns Object with shouldSetSiteCookie and shouldSetLocaleCookie booleans\n */\nasync function shouldSetCookies(\n request: Request,\n response: Response,\n settings: SiteSettings,\n site: Site,\n locale: Locale\n): Promise<{ shouldSetSiteCookie: boolean; shouldSetLocaleCookie: boolean }> {\n const cacheSite = settings.siteDetectionConfig.caches?.includes('cookie');\n const cacheLocale = settings.localeDetectionConfig.caches?.includes('cookie');\n\n // Early return if no cookie caching is enabled\n if (!cacheSite && !cacheLocale) {\n return { shouldSetSiteCookie: false, shouldSetLocaleCookie: false };\n }\n\n // Check if cookies were already set by actions/loaders in the response.\n // If they were, we don't want to override them.\n const responseSetCookies = response.headers.getSetCookie?.() || [];\n const isSettingSiteCookieInResponse = responseSetCookies.some((cookie) =>\n cookie.startsWith(`${settings.siteCookie.name}=`)\n );\n const isSettingLocaleCookieInResponse = responseSetCookies.some((cookie) =>\n cookie.startsWith(`${settings.localeCookie.name}=`)\n );\n\n const requestCookieHeader = request.headers.get('Cookie');\n const [existingSiteCookie, existingLocaleCookie] = await Promise.all([\n settings.siteCookie.parse(requestCookieHeader),\n settings.localeCookie.parse(requestCookieHeader),\n ]);\n\n // Set cookie if: doesn't exist yet OR resolved value differs from existing.\n // Skip if an action/loader already set it in the response.\n return {\n shouldSetSiteCookie: cacheSite && !isSettingSiteCookieInResponse && existingSiteCookie !== site.id,\n shouldSetLocaleCookie: cacheLocale && !isSettingLocaleCookieInResponse && existingLocaleCookie !== locale.id,\n };\n}\n\n/**\n * Creates a site context middleware that resolves the current site from\n * the request (path, cookie, header, query, or default) and stores the\n * result in the router context.\n *\n * Does not import or read from app config context; the consumer supplies config.\n */\nexport function createSiteContextMiddleware(config: SiteConfig): MiddlewareFunction<Response> {\n // Merge config with defaults so every detection option has a value\n const siteDetectionConfig: SiteSettings['siteDetectionConfig'] = {\n ...DEFAULT_SITE_DETECTION,\n ...config.siteDetectionConfig,\n };\n const localeDetectionConfig: SiteSettings['localeDetectionConfig'] = {\n ...DEFAULT_LOCALE_DETECTION,\n ...config.localeDetectionConfig,\n };\n\n // Create cookies based on configured names\n const siteCookie = createSiteContextCookie(siteDetectionConfig.lookupCookie);\n const localeCookie = createSiteContextCookie(localeDetectionConfig.lookupCookie);\n\n const settings: SiteSettings = {\n ...config,\n siteDetectionConfig,\n localeDetectionConfig,\n siteCookie,\n localeCookie,\n };\n\n const siteContextMiddleware: MiddlewareFunction<Response> = async (\n { request, context }: MiddlewareArgs,\n next: () => Promise<Response>\n ): Promise<Response> => {\n const site = await resolveSite(request, settings);\n const locale = await resolveLocale(request, settings, site);\n\n // Store full Site, Locale, and Cookie objects in context for downstream middlewares (currency, loaders, etc.)\n context.set(siteContext, {\n site,\n locale,\n siteCookie: settings.siteCookie,\n localeCookie: settings.localeCookie,\n });\n\n // Store locale in a WeakMap so i18next's findLocale can access it\n // This is necessary because findLocale only receives Request and cannot access the router context\n requestToLocaleMap.set(request, locale.id);\n\n const response = await next();\n\n // Determine if cookies should be set\n const { shouldSetSiteCookie, shouldSetLocaleCookie } = await shouldSetCookies(\n request,\n response,\n settings,\n site,\n locale\n );\n\n // Early return if no cookies need to be set\n if (!shouldSetSiteCookie && !shouldSetLocaleCookie) {\n return response;\n }\n\n const [siteSetCookie, localeSetCookie] = await Promise.all([\n shouldSetSiteCookie ? settings.siteCookie.serialize(site.id, { path: '/' }) : Promise.resolve(null),\n shouldSetLocaleCookie ? settings.localeCookie.serialize(locale.id, { path: '/' }) : Promise.resolve(null),\n ]);\n\n if (siteSetCookie) response.headers.append('Set-Cookie', siteSetCookie);\n if (localeSetCookie) response.headers.append('Set-Cookie', localeSetCookie);\n\n return response;\n };\n\n return siteContextMiddleware;\n}\n"],"mappings":";;;;;;AAkBA,MAAM,cAAc,cAAgC,OAAU;;;;;;;;AAS9D,SAAgB,aAAa,EAAE,OAAO,YAAgD;AAClF,QAAO,oBAAC,YAAY;EAAgB;EAAQ;GAAgC;;;;;;AAQhF,SAAgB,UAA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