@amboras-dev/storefront-core 2026.11.11-rc.1 → 2026.11.11
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/CHANGELOG.md +3 -102
- package/README.md +3 -29
- package/bin/amboras-check.mjs +1 -2
- package/bin/checks/check-redirect-boundaries.mjs +2 -10
- package/core-manifest.json +1 -6
- package/dist/edge/hosted-checkout.d.ts +2 -5
- package/dist/edge/hosted-checkout.d.ts.map +1 -1
- package/dist/edge/hosted-checkout.js +6 -8
- package/dist/edge/hosted-checkout.js.map +1 -1
- package/dist/edge/proxy.d.ts +2 -27
- package/dist/edge/proxy.d.ts.map +1 -1
- package/dist/edge/proxy.js +56 -204
- package/dist/edge/proxy.js.map +1 -1
- package/dist/hooks/use-auth.d.ts.map +1 -1
- package/dist/hooks/use-auth.js +2 -5
- package/dist/hooks/use-auth.js.map +1 -1
- package/dist/hooks/use-variant-selection.d.ts.map +1 -1
- package/dist/hooks/use-variant-selection.js +12 -1
- package/dist/hooks/use-variant-selection.js.map +1 -1
- package/dist/lib/medusa-client.d.ts.map +1 -1
- package/dist/lib/medusa-client.js +1 -30
- package/dist/lib/medusa-client.js.map +1 -1
- package/dist/lib/redirects.d.ts +5 -13
- package/dist/lib/redirects.d.ts.map +1 -1
- package/dist/lib/redirects.js +9 -68
- package/dist/lib/redirects.js.map +1 -1
- package/dist/lib/seo.d.ts +7 -20
- package/dist/lib/seo.d.ts.map +1 -1
- package/dist/lib/seo.js +29 -157
- package/dist/lib/seo.js.map +1 -1
- package/dist/lib/site-url.d.ts +0 -8
- package/dist/lib/site-url.d.ts.map +1 -1
- package/dist/lib/site-url.js +0 -18
- package/dist/lib/site-url.js.map +1 -1
- package/dist/lib/variant-selection.d.ts +7 -3
- package/dist/lib/variant-selection.d.ts.map +1 -1
- package/dist/lib/variant-selection.js +10 -5
- package/dist/lib/variant-selection.js.map +1 -1
- package/dist/next-config/index.d.ts +0 -31
- package/dist/next-config/index.d.ts.map +1 -1
- package/dist/next-config/index.js +1 -32
- package/dist/next-config/index.js.map +1 -1
- package/package.json +1 -5
- package/bin/checks/check-market-routes.mjs +0 -184
- package/dist/edge/market.d.ts +0 -45
- package/dist/edge/market.d.ts.map +0 -1
- package/dist/edge/market.js +0 -74
- package/dist/edge/market.js.map +0 -1
- package/dist/edge/url-map.d.ts +0 -57
- package/dist/edge/url-map.d.ts.map +0 -1
- package/dist/edge/url-map.js +0 -140
- package/dist/edge/url-map.js.map +0 -1
- package/dist/lib/market.d.ts +0 -176
- package/dist/lib/market.d.ts.map +0 -1
- package/dist/lib/market.js +0 -228
- package/dist/lib/market.js.map +0 -1
- package/dist/lib/visitor-free-routes.d.ts +0 -17
- package/dist/lib/visitor-free-routes.d.ts.map +0 -1
- package/dist/lib/visitor-free-routes.js +0 -31
- package/dist/lib/visitor-free-routes.js.map +0 -1
- package/dist/shell/market-link.d.ts +0 -18
- package/dist/shell/market-link.d.ts.map +0 -1
- package/dist/shell/market-link.js +0 -39
- package/dist/shell/market-link.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amboras-dev/storefront-core",
|
|
3
|
-
"version": "2026.11.11
|
|
3
|
+
"version": "2026.11.11",
|
|
4
4
|
"description": "The platform half of every Amboras storefront: cart, checkout, payments, auth, orders, subscriptions, catalogue data, analytics, consent, plugin slots, edge proxy and the Next config. Stores keep only design; they import this. ESM, React 19, Next 16.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"type": "module",
|
|
@@ -46,10 +46,6 @@
|
|
|
46
46
|
"types": "./dist/config/index.d.ts",
|
|
47
47
|
"default": "./dist/config/index.js"
|
|
48
48
|
},
|
|
49
|
-
"./visitor-free-routes": {
|
|
50
|
-
"types": "./dist/lib/visitor-free-routes.d.ts",
|
|
51
|
-
"default": "./dist/lib/visitor-free-routes.js"
|
|
52
|
-
},
|
|
53
49
|
"./edge": {
|
|
54
50
|
"types": "./dist/edge/proxy.d.ts",
|
|
55
51
|
"default": "./dist/edge/proxy.js"
|
|
@@ -1,184 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* Guard: on a store with the `app/[m]/` tree (AMB-2174), every route file sits where `marketProxy` serves it.
|
|
4
|
-
*
|
|
5
|
-
* WHY. `marketProxy` rewrites every page request to `/{market key}{path}` (`/products/x` → `/_/products/x`), so a
|
|
6
|
-
* page, route handler or metadata route anywhere else under `app/` is never reached: it 404s at the root and under
|
|
7
|
-
* every market prefix. The other way round, a URL in core's `OUTSIDE_MARKET_TREE` (the API, `.well-known`, a file
|
|
8
|
-
* such as `/llms.txt` or `/pinterest/feed.xml`, robots, sitemaps, the manifest) is never rewritten, so its route file
|
|
9
|
-
* must stay at the top of `app/`. Metadata images (`icon`, `apple-icon`, `opengraph-image`, `twitter-image`) live at the
|
|
10
|
-
* app root only. The rule is core's own `isOutsideMarketTree`, loaded from the installed `dist/lib/market.js`, so the
|
|
11
|
-
* gate and the proxy cannot drift.
|
|
12
|
-
*
|
|
13
|
-
* Active when the store has an `app/[m]/` folder or its proxy file exports `marketProxy`; then `app/[m]/layout.*`
|
|
14
|
-
* must exist and guard its key with `assertMarketKey`, and `next.config.*` must spread `marketTreeEnv()` into its
|
|
15
|
-
* `env` and return `marketFallbackRewrites()` in its `rewrites()` `afterFiles`, alone or spread anywhere in an array
|
|
16
|
-
* (else a missing file path, `/old.pdf`, 404s instead of answering its merchant redirect; a config writer such as the
|
|
17
|
-
* Shopify cloner can drop them). A store on the old tree runs `proxy` and passes untouched.
|
|
18
|
-
*
|
|
19
|
-
* Usage: `node check-market-routes.mjs` from the store root (or AMBORAS_STORE_ROOT). One FAIL line per problem with
|
|
20
|
-
* the move to make, exit 1; `[check-market-routes] EXIT_OK` as the last line otherwise.
|
|
21
|
-
*/
|
|
22
|
-
import { existsSync, readdirSync, readFileSync, realpathSync } from 'node:fs'
|
|
23
|
-
import { dirname, join, relative, sep } from 'node:path'
|
|
24
|
-
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
25
|
-
|
|
26
|
-
const TAG = '[check-market-routes]'
|
|
27
|
-
const SCRIPT_EXT = '(?:tsx?|jsx?)'
|
|
28
|
-
const PAGE_RE = new RegExp(`^(?:page|route)\\.${SCRIPT_EXT}$`)
|
|
29
|
-
/** Metadata images: served from the app root only on the market tree (nested under `[m]` their URL doubles the prefix). */
|
|
30
|
-
const METADATA_IMAGE_RE = /^(?:icon|apple-icon|opengraph-image|twitter-image)\d*\..+$/
|
|
31
|
-
/** The other metadata routes and the URL Next serves each at. */
|
|
32
|
-
const METADATA_URL = { robots: 'robots.txt', manifest: 'manifest.webmanifest', sitemap: 'sitemap.xml' }
|
|
33
|
-
const METADATA_RE = /^(robots|manifest|sitemap)\..+$/
|
|
34
|
-
const LAYOUT_RE = new RegExp(`^layout\\.${SCRIPT_EXT}$`)
|
|
35
|
-
const PROXY_FILES = ['proxy.ts', 'proxy.js', 'src/proxy.ts']
|
|
36
|
-
|
|
37
|
-
/** True when the store is on the `app/[m]/` tree: the folder exists, or its proxy file exports `marketProxy`. */
|
|
38
|
-
export function isMarketTree(root) {
|
|
39
|
-
if (existsSync(join(root, 'app', '[m]'))) return true
|
|
40
|
-
return PROXY_FILES.some((file) => {
|
|
41
|
-
try {
|
|
42
|
-
return /\bmarketProxy\b/.test(readFileSync(join(root, file), 'utf8'))
|
|
43
|
-
} catch {
|
|
44
|
-
return false
|
|
45
|
-
}
|
|
46
|
-
})
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** What is wrong with `app/[m]/layout.*`, or null: it must exist and call `assertMarketKey`. */
|
|
50
|
-
export function layoutProblem(root) {
|
|
51
|
-
const dir = join(root, 'app', '[m]')
|
|
52
|
-
const layout = existsSync(dir) ? readdirSync(dir).find((name) => LAYOUT_RE.test(name)) : undefined
|
|
53
|
-
if (!layout) return 'app/[m]/layout.tsx is missing: it sets the market metadataBase and answers a bad key with assertMarketKey.'
|
|
54
|
-
if (!readFileSync(join(dir, layout), 'utf8').includes('assertMarketKey')) {
|
|
55
|
-
return `app/[m]/${layout} does not call assertMarketKey(m, notFound): a path no rewrite reaches (/api/missing.json) would render a page under any first segment.`
|
|
56
|
-
}
|
|
57
|
-
return null
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const NEXT_CONFIGS = ['next.config.mjs', 'next.config.js', 'next.config.ts', 'next.config.mts', 'next.config.cjs']
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* The source of the `{ … }` (or the `[ … ]` with `open` `[`) that starts at or after `from`, by bracket count (strings
|
|
64
|
-
* and comments are not special).
|
|
65
|
-
*/
|
|
66
|
-
function blockFrom(src, from, open = '{') {
|
|
67
|
-
const close = open === '[' ? ']' : '}'
|
|
68
|
-
const start = src.indexOf(open, from)
|
|
69
|
-
if (start < 0) return ''
|
|
70
|
-
let depth = 0
|
|
71
|
-
for (let i = start; i < src.length; i++) {
|
|
72
|
-
if (src[i] === open) depth++
|
|
73
|
-
if (src[i] === close && --depth === 0) return src.slice(start, i + 1)
|
|
74
|
-
}
|
|
75
|
-
return src.slice(start)
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/** Whether the `afterFiles` of a `rewrites()` body is `marketFallbackRewrites()` or an array that spreads it anywhere. */
|
|
79
|
-
function afterFilesHasFallback(body) {
|
|
80
|
-
const afterFiles = /\bafterFiles\s*:\s*/.exec(body)
|
|
81
|
-
if (!afterFiles) return false
|
|
82
|
-
const value = body.slice(afterFiles.index + afterFiles[0].length)
|
|
83
|
-
if (/^marketFallbackRewrites\s*\(/.test(value)) return true
|
|
84
|
-
return value.startsWith('[') && /\.\.\.\s*marketFallbackRewrites\s*\(/.test(blockFrom(value, 0, '['))
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/** The first match of `re` (global) that is not on a `//` comment line, or null. */
|
|
88
|
-
function declaration(src, re) {
|
|
89
|
-
for (const match of src.matchAll(re)) {
|
|
90
|
-
const lineStart = src.lastIndexOf('\n', match.index) + 1
|
|
91
|
-
if (!src.slice(lineStart, match.index).includes('//')) return match
|
|
92
|
-
}
|
|
93
|
-
return null
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* What is wrong with the store's next.config, or null: its `env` must spread `marketTreeEnv()` (the opt-in of the
|
|
98
|
-
* market tree), its `rewrites()` must return `marketFallbackRewrites()` in `afterFiles` (after the public files and
|
|
99
|
-
* static routes, before `app/[m]`; alone or spread anywhere in an array).
|
|
100
|
-
*/
|
|
101
|
-
export function nextConfigProblem(root) {
|
|
102
|
-
const file = NEXT_CONFIGS.find((name) => existsSync(join(root, name)))
|
|
103
|
-
if (!file) return `no next.config (${NEXT_CONFIGS.join(', ')}): it must set env: { ...marketTreeEnv() } and rewrites() afterFiles: marketFallbackRewrites().`
|
|
104
|
-
const src = readFileSync(join(root, file), 'utf8')
|
|
105
|
-
// The declarations themselves (`env: {`, `rewrites() {`, `rewrites: async () =>`), never a comment that names them.
|
|
106
|
-
const env = declaration(src, /\benv\s*:\s*\{/g)
|
|
107
|
-
if (!env || !/\.\.\.\s*marketTreeEnv\s*\(\s*\)/.test(blockFrom(src, env.index))) {
|
|
108
|
-
return `${file} does not spread marketTreeEnv() into its env: the market tree's static pages would read cookies on every store read and render per request.`
|
|
109
|
-
}
|
|
110
|
-
const rewrites = declaration(src, /\brewrites\s*(?:\(\s*\)\s*\{|:\s*(?:async\s*)?(?:\(\s*\)\s*=>|function\s*\())/g)
|
|
111
|
-
if (!rewrites || !afterFilesHasFallback(blockFrom(src, rewrites.index))) {
|
|
112
|
-
return `${file} does not return marketFallbackRewrites() in its rewrites() afterFiles: a missing file path (/old.pdf) would 404 instead of answering its merchant redirect.`
|
|
113
|
-
}
|
|
114
|
-
return null
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function walk(dir) {
|
|
118
|
-
return readdirSync(dir, { withFileTypes: true }).flatMap((d) => (d.isDirectory() ? walk(join(dir, d.name)) : [join(dir, d.name)]))
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/** The URL a route file serves, from its segments under `app/` (groups and `[m]` already dropped). */
|
|
122
|
-
function urlOf(dirs, name) {
|
|
123
|
-
const metadata = METADATA_RE.exec(name)?.[1]
|
|
124
|
-
return `/${[...dirs, ...(metadata ? [METADATA_URL[metadata]] : [])].join('/')}`
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Misplaced route files of `root` as `{ file, move, reason }` (store-relative, `/`-separated). `isOutside` is core's
|
|
129
|
-
* `isOutsideMarketTree`. Files in private folders (`_x`) are not routes; route groups (`(x)`) are not in the URL.
|
|
130
|
-
*/
|
|
131
|
-
export function findMarketRouteViolations(root, isOutside) {
|
|
132
|
-
const appDir = join(root, 'app')
|
|
133
|
-
if (!existsSync(appDir)) return []
|
|
134
|
-
const found = []
|
|
135
|
-
for (const file of walk(appDir).map((f) => relative(root, f).split(sep).join('/')).sort()) {
|
|
136
|
-
const segments = file.split('/').slice(1)
|
|
137
|
-
const name = segments.pop()
|
|
138
|
-
if (segments.some((s) => s.startsWith('_'))) continue
|
|
139
|
-
const isImage = METADATA_IMAGE_RE.test(name)
|
|
140
|
-
if (!isImage && !PAGE_RE.test(name) && !METADATA_RE.test(name)) continue
|
|
141
|
-
const dirs = segments.filter((s) => !/^\(.*\)$/.test(s))
|
|
142
|
-
const inTree = dirs[0] === '[m]'
|
|
143
|
-
const urlDirs = inTree ? dirs.slice(1) : dirs
|
|
144
|
-
const raw = segments.filter((s) => s !== '[m]')
|
|
145
|
-
const top = `app/${[...raw, name].join('/')}`
|
|
146
|
-
if (isImage) {
|
|
147
|
-
if (dirs.length) found.push({ file, move: `app/${name}`, reason: 'a metadata image is served from the app root only' })
|
|
148
|
-
continue
|
|
149
|
-
}
|
|
150
|
-
const outside = isOutside(PAGE_RE.test(name) ? `/${urlDirs.join('/')}` : urlOf(urlDirs, name))
|
|
151
|
-
if (outside && inTree) found.push({ file, move: top, reason: 'its URL is outside app/[m]/ (OUTSIDE_MARKET_TREE), marketProxy never rewrites it' })
|
|
152
|
-
if (!outside && !inTree) found.push({ file, move: `app/[m]/${[...raw, name].join('/')}`, reason: 'a route outside app/[m]/ is never served by marketProxy' })
|
|
153
|
-
}
|
|
154
|
-
return found
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
async function main() {
|
|
158
|
-
const root = process.env.AMBORAS_STORE_ROOT ?? process.cwd()
|
|
159
|
-
if (!isMarketTree(root)) {
|
|
160
|
-
console.log(`${TAG} OK: no app/[m]/ and no marketProxy, the store is on the old route tree (proxy), nothing to check.`)
|
|
161
|
-
console.log(`${TAG} EXIT_OK`)
|
|
162
|
-
return
|
|
163
|
-
}
|
|
164
|
-
// core's own rule, from the installed build (the same code the proxy runs).
|
|
165
|
-
const coreMarket = pathToFileURL(join(dirname(fileURLToPath(import.meta.url)), '..', '..', 'dist', 'lib', 'market.js')).href
|
|
166
|
-
const { isOutsideMarketTree } = await import(coreMarket)
|
|
167
|
-
const problems = [
|
|
168
|
-
...[layoutProblem(root), nextConfigProblem(root)].filter(Boolean),
|
|
169
|
-
...findMarketRouteViolations(root, isOutsideMarketTree).map(({ file, move, reason }) => `${file} → ${move} (${reason})`),
|
|
170
|
-
]
|
|
171
|
-
if (problems.length) {
|
|
172
|
-
for (const problem of problems) console.error(`${TAG} FAIL: ${problem}`)
|
|
173
|
-
process.exit(1)
|
|
174
|
-
}
|
|
175
|
-
console.log(`${TAG} OK: every route file is where marketProxy serves it, and app/[m]/layout guards its key.`)
|
|
176
|
-
console.log(`${TAG} EXIT_OK`)
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
if (process.argv[1] && realpathSync(process.argv[1]) === realpathSync(fileURLToPath(import.meta.url))) {
|
|
180
|
-
main().catch((err) => {
|
|
181
|
-
console.error(`${TAG} FAIL: check crashed — ${err?.message ?? err}`)
|
|
182
|
-
process.exit(1)
|
|
183
|
-
})
|
|
184
|
-
}
|
package/dist/edge/market.d.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import type { UrlMapCopy } from './url-map.js';
|
|
2
|
-
/**
|
|
3
|
-
* Which market a request URL names (AMB-2174). Pure: no I/O, the same answer for a crawler and a human.
|
|
4
|
-
*
|
|
5
|
-
* First match wins:
|
|
6
|
-
* 1. the host is a subdomain or domain presence: active → that market; inactive → its `inactiveRedirect` to the
|
|
7
|
-
* primary, else the root; pending → the root (`domain_pending`);
|
|
8
|
-
* 2. on the primary host, the first path segment is a subfolder presence's `path`: active → that market (a
|
|
9
|
-
* `/checkout*` inner path → 302 to the root checkout); inactive → its `inactiveRedirect` to the root, else the root;
|
|
10
|
-
* 2a. a previous path (`previousPaths`) → 301 to the current URL; 2b. a former default language's path
|
|
11
|
-
* (`languagePaths`) → 301 to the current URL; either only while that URL is announced (active and turned on),
|
|
12
|
-
* else a 302 to the root (`unserved`: a pending host, an inactive market or a passive URL is no place to move
|
|
13
|
-
* an indexed link to for good);
|
|
14
|
-
* 3. anything else → the root, today's behaviour.
|
|
15
|
-
*
|
|
16
|
-
* An inactive presence redirects as Medusa says (`inactiveRedirect`: `temporary` 302, null none; it can always come
|
|
17
|
-
* back), a moved URL 301s; the proxy sends
|
|
18
|
-
* every one with `no-store`, so a browser never replays an old one after a suffix is reverted. Redirect targets are
|
|
19
|
-
* built on a map origin (a presence host or the primary), never resolved against the request, so none can become an
|
|
20
|
-
* open redirect.
|
|
21
|
-
*/
|
|
22
|
-
export interface MarketResolution {
|
|
23
|
-
source: 'host' | 'subfolder' | 'root';
|
|
24
|
-
/** null on the root. */
|
|
25
|
-
marketId: string | null;
|
|
26
|
-
/** '/en-ca' on a subfolder, '' otherwise. */
|
|
27
|
-
publicPrefix: string;
|
|
28
|
-
/** The `[m]` segment: '_' root, 'en-ca' subfolder, '_<marketId>' subdomain or domain. */
|
|
29
|
-
internalKey: string;
|
|
30
|
-
/** The path without publicPrefix. */
|
|
31
|
-
innerPath: string;
|
|
32
|
-
redirect?: {
|
|
33
|
-
location: URL;
|
|
34
|
-
status: 301 | 302;
|
|
35
|
-
kind: 'previous_path' | 'language' | 'unserved' | 'inactive' | 'root_checkout';
|
|
36
|
-
};
|
|
37
|
-
fallback?: 'map_stale' | 'map_unavailable' | 'domain_pending';
|
|
38
|
-
}
|
|
39
|
-
/** The root: today's behaviour, no market. */
|
|
40
|
-
export declare function rootResolution(url: URL): MarketResolution;
|
|
41
|
-
/** `options.subfolders: false` resolves hosts only (the `proxy` export, for a store tree without `app/[m]/`). */
|
|
42
|
-
export declare function resolveMarket(url: URL, host: string, map: UrlMapCopy | null, primaryHost: string | null, { subfolders: withSubfolders }?: {
|
|
43
|
-
subfolders?: boolean;
|
|
44
|
-
}): MarketResolution;
|
|
45
|
-
//# sourceMappingURL=market.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"market.d.ts","sourceRoot":"","sources":["../../src/edge/market.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AAE9C;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,WAAW,gBAAgB;IAC/B,MAAM,EAAE,MAAM,GAAG,WAAW,GAAG,MAAM,CAAA;IACrC,wBAAwB;IACxB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAA;IACvB,6CAA6C;IAC7C,YAAY,EAAE,MAAM,CAAA;IACpB,yFAAyF;IACzF,WAAW,EAAE,MAAM,CAAA;IACnB,qCAAqC;IACrC,SAAS,EAAE,MAAM,CAAA;IACjB,QAAQ,CAAC,EAAE;QAAE,QAAQ,EAAE,GAAG,CAAC;QAAC,MAAM,EAAE,GAAG,GAAG,GAAG,CAAC;QAAC,IAAI,EAAE,eAAe,GAAG,UAAU,GAAG,UAAU,GAAG,UAAU,GAAG,eAAe,CAAA;KAAE,CAAA;IAC/H,QAAQ,CAAC,EAAE,WAAW,GAAG,iBAAiB,GAAG,gBAAgB,CAAA;CAC9D;AAgBD,8CAA8C;AAC9C,wBAAgB,cAAc,CAAC,GAAG,EAAE,GAAG,GAAG,gBAAgB,CAEzD;AAED,iHAAiH;AACjH,wBAAgB,aAAa,CAC3B,GAAG,EAAE,GAAG,EACR,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,UAAU,GAAG,IAAI,EACtB,WAAW,EAAE,MAAM,GAAG,IAAI,EAC1B,EAAE,UAAU,EAAE,cAAqB,EAAE,GAAE;IAAE,UAAU,CAAC,EAAE,OAAO,CAAA;CAAO,GACnE,gBAAgB,CAkDlB"}
|
package/dist/edge/market.js
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { isCheckoutPath } from './hosted-checkout.js';
|
|
2
|
-
import { isAnnounced, keyOfPresence, presenceOfHost, ROOT_MARKET_KEY } from '../lib/market.js';
|
|
3
|
-
/** `https://<host><path><search>`: the path is set on the origin (leading slashes folded), never resolved against it. */
|
|
4
|
-
function onHost(host, path, search) {
|
|
5
|
-
const target = new URL(`https://${host}`);
|
|
6
|
-
target.pathname = path.replace(/^[/\\]+/, '/');
|
|
7
|
-
target.search = search;
|
|
8
|
-
return target;
|
|
9
|
-
}
|
|
10
|
-
/** Whether an inactive presence 302s to the root: Medusa's `inactiveRedirect`. */
|
|
11
|
-
const redirectsWhenInactive = (presence) => presence.status === 'inactive' && presence.inactiveRedirect === 'temporary';
|
|
12
|
-
/** `/en-ca` + `/x` → `/en-ca/x`; `/en-ca` + `/` → `/en-ca`. */
|
|
13
|
-
const joinPrefix = (prefix, inner) => (inner === '/' ? prefix : prefix + inner);
|
|
14
|
-
/** The root: today's behaviour, no market. */
|
|
15
|
-
export function rootResolution(url) {
|
|
16
|
-
return { source: 'root', marketId: null, publicPrefix: '', internalKey: ROOT_MARKET_KEY, innerPath: url.pathname };
|
|
17
|
-
}
|
|
18
|
-
/** `options.subfolders: false` resolves hosts only (the `proxy` export, for a store tree without `app/[m]/`). */
|
|
19
|
-
export function resolveMarket(url, host, map, primaryHost, { subfolders: withSubfolders = true } = {}) {
|
|
20
|
-
const root = rootResolution(url);
|
|
21
|
-
if (!map)
|
|
22
|
-
return { ...root, fallback: 'map_unavailable' };
|
|
23
|
-
const fallback = map.stale ? 'map_stale' : undefined;
|
|
24
|
-
const result = (r) => (fallback ? { ...r, fallback } : r);
|
|
25
|
-
const { presences } = map.value;
|
|
26
|
-
const { search } = url;
|
|
27
|
-
const hosted = presenceOfHost(map.value, host);
|
|
28
|
-
if (hosted) {
|
|
29
|
-
if (hosted.status === 'active') {
|
|
30
|
-
return result({ source: 'host', marketId: hosted.marketId, publicPrefix: '', internalKey: keyOfPresence(hosted), innerPath: url.pathname });
|
|
31
|
-
}
|
|
32
|
-
if (hosted.status === 'pending')
|
|
33
|
-
return { ...root, fallback: fallback ?? 'domain_pending' };
|
|
34
|
-
if (!primaryHost || !redirectsWhenInactive(hosted))
|
|
35
|
-
return result(root);
|
|
36
|
-
return result({ ...root, redirect: { location: onHost(primaryHost, url.pathname, search), status: 302, kind: 'inactive' } });
|
|
37
|
-
}
|
|
38
|
-
if (!withSubfolders || !primaryHost || host !== primaryHost)
|
|
39
|
-
return result(root);
|
|
40
|
-
const slash = url.pathname.indexOf('/', 1);
|
|
41
|
-
const segment = `/${slash < 0 ? url.pathname.slice(1) : url.pathname.slice(1, slash)}`;
|
|
42
|
-
const inner = slash < 0 ? '/' : url.pathname.slice(slash);
|
|
43
|
-
const subfolders = presences.filter((p) => p.kind === 'subfolder' && p.path !== null);
|
|
44
|
-
const current = subfolders.find((p) => p.path === segment);
|
|
45
|
-
if (current?.status === 'active') {
|
|
46
|
-
const market = {
|
|
47
|
-
source: 'subfolder',
|
|
48
|
-
marketId: current.marketId,
|
|
49
|
-
publicPrefix: current.path,
|
|
50
|
-
internalKey: keyOfPresence(current),
|
|
51
|
-
innerPath: inner,
|
|
52
|
-
};
|
|
53
|
-
// The checkout runs at the root until the hosted checkout knows the market path (§13 item 4).
|
|
54
|
-
if (isCheckoutPath(inner))
|
|
55
|
-
return result({ ...market, redirect: { location: onHost(primaryHost, inner, search), status: 302, kind: 'root_checkout' } });
|
|
56
|
-
return result(market);
|
|
57
|
-
}
|
|
58
|
-
if (current && redirectsWhenInactive(current)) {
|
|
59
|
-
return result({ ...root, redirect: { location: onHost(primaryHost, inner, search), status: 302, kind: 'inactive' } });
|
|
60
|
-
}
|
|
61
|
-
if (current)
|
|
62
|
-
return result(root);
|
|
63
|
-
const moved = presences.find((p) => p.previousPaths.includes(segment));
|
|
64
|
-
const relanguaged = moved ? undefined : presences.find((p) => p.languagePaths.includes(segment));
|
|
65
|
-
const target = moved ?? relanguaged;
|
|
66
|
-
if (!target)
|
|
67
|
-
return result(root);
|
|
68
|
-
const to = isAnnounced(target) ? target : null;
|
|
69
|
-
const location = to?.path ? onHost(primaryHost, joinPrefix(to.path, inner), search) : to?.host ? onHost(to.host, inner, search) : null;
|
|
70
|
-
if (location)
|
|
71
|
-
return result({ ...root, redirect: { location, status: 301, kind: moved ? 'previous_path' : 'language' } });
|
|
72
|
-
return result({ ...root, redirect: { location: onHost(primaryHost, inner, search), status: 302, kind: 'unserved' } });
|
|
73
|
-
}
|
|
74
|
-
//# sourceMappingURL=market.js.map
|
package/dist/edge/market.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"market.js","sourceRoot":"","sources":["../../src/edge/market.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AACrD,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,cAAc,EAAE,eAAe,EAAuB,MAAM,kBAAkB,CAAA;AAqCnH,yHAAyH;AACzH,SAAS,MAAM,CAAC,IAAY,EAAE,IAAY,EAAE,MAAc;IACxD,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC,WAAW,IAAI,EAAE,CAAC,CAAA;IACzC,MAAM,CAAC,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,GAAG,CAAC,CAAA;IAC9C,MAAM,CAAC,MAAM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,EAAE,GAAG,IAAI,EAAE,QAAQ,EAAE,EAAE,QAAQ,EAAE,MAAM,CAAC,WAAW,EAAE,KAAK,EAAE,MAAM,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE,IAAI,EAAE,UAAU,EAAE,EAAE,CAAC,CAAA;AACvH,CAAC"}
|
package/dist/edge/url-map.d.ts
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { type StoreUrlMap } from '../lib/market.js';
|
|
2
|
-
/**
|
|
3
|
-
* The proxy's in-memory copy of the store's URL map (`GET /store/url-map`, AMB-2174).
|
|
4
|
-
*
|
|
5
|
-
* - Refreshed at most every 30 s per instance with `If-None-Match`; a `304` keeps the copy.
|
|
6
|
-
* - Only a cold instance (no copy yet) waits for Medusa. With a copy the request is served from it and the refresh
|
|
7
|
-
* runs in the background (`waitUntil`). One refresh is in flight per instance.
|
|
8
|
-
* - A 404 or 410 with no good copy yet (an older Medusa without the route) means the store has no URL map: the copy
|
|
9
|
-
* is an empty map (the root everywhere, no cold-start 503), retried in 30 s.
|
|
10
|
-
* - Anything else that is not a map (a 5xx, a 401/403 after a key rotation, a 429, a WAF block, a 404 after a good
|
|
11
|
-
* copy, a network error, a bad body, a 1 500 ms timeout) is a failure: the last good copy is kept and marked
|
|
12
|
-
* `stale` (counted as `map_stale`), and the next try is 30 s later. With no copy the next try is 5 s later, and until
|
|
13
|
-
* then the resolver reads `null` as the root.
|
|
14
|
-
*/
|
|
15
|
-
type Env = Record<string, string | undefined>;
|
|
16
|
-
/** The store API env, read straight from `process.env`: the edge has no `configureCore`. The one reader at the edge. */
|
|
17
|
-
export interface EdgeStoreEnv {
|
|
18
|
-
medusaUrl?: string;
|
|
19
|
-
publishableKey?: string;
|
|
20
|
-
storeId?: string;
|
|
21
|
-
}
|
|
22
|
-
export declare function edgeStoreEnv(env?: Env): EdgeStoreEnv;
|
|
23
|
-
export interface UrlMapCopy {
|
|
24
|
-
value: StoreUrlMap;
|
|
25
|
-
/** The last refresh failed: this is the last good copy. */
|
|
26
|
-
stale: boolean;
|
|
27
|
-
/** Milliseconds since Medusa last confirmed this copy (a 200 or a 304). */
|
|
28
|
-
age: number;
|
|
29
|
-
}
|
|
30
|
-
/** A copy older than this still routes, but never redirects: a 30x from a long-stale copy could send people away. */
|
|
31
|
-
export declare const MAX_REDIRECT_AGE_MS: number;
|
|
32
|
-
/**
|
|
33
|
-
* The URL map for this request, or null when none was ever read (no Medusa URL, or Medusa never answered).
|
|
34
|
-
* `waitUntil` (the proxy's `event.waitUntil`) lets a warm instance refresh in the background; without it every
|
|
35
|
-
* refresh is awaited.
|
|
36
|
-
*/
|
|
37
|
-
export declare function getUrlMap(env?: Env, waitUntil?: (promise: Promise<unknown>) => void): Promise<UrlMapCopy | null>;
|
|
38
|
-
/**
|
|
39
|
-
* The copy in memory, or null, never waiting for Medusa (a file path never waits). A refresh that is due (a cold
|
|
40
|
-
* instance's first read included) runs in the background through `waitUntil`, so a later request has the map; none
|
|
41
|
-
* without it.
|
|
42
|
-
*/
|
|
43
|
-
export declare function peekUrlMap(env?: Env, waitUntil?: (promise: Promise<unknown>) => void): UrlMapCopy | null;
|
|
44
|
-
/** A cold instance's 503 for a market-shaped path gives up after this long without a map (a revoked key, a WAF). */
|
|
45
|
-
export declare const MAX_COLD_503_MS: number;
|
|
46
|
-
/** Milliseconds since this instance's first failed read with no map yet; 0 once it has one (or before it tried). */
|
|
47
|
-
export declare function coldForMs(): number;
|
|
48
|
-
export type MarketSignalKind = 'map_stale' | 'map_unavailable' | 'inactive_redirect' | 'hop_unverified';
|
|
49
|
-
/**
|
|
50
|
-
* Counts a fallback in Medusa (`POST /store/market-signals`, a Datadog counter): at most once a minute per kind per
|
|
51
|
-
* instance, through `waitUntil` so it never delays the response. A lost beacon loses a count, never a page.
|
|
52
|
-
*/
|
|
53
|
-
export declare function sendMarketSignal(kind: MarketSignalKind, waitUntil: ((promise: Promise<unknown>) => void) | undefined, env?: Env): void;
|
|
54
|
-
/** Test seam: forget the copy and the signal clock. */
|
|
55
|
-
export declare function resetUrlMap(): void;
|
|
56
|
-
export {};
|
|
57
|
-
//# sourceMappingURL=url-map.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"url-map.d.ts","sourceRoot":"","sources":["../../src/edge/url-map.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsC,KAAK,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAEvF;;;;;;;;;;;;GAYG;AAEH,KAAK,GAAG,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA;AAE7C,wHAAwH;AACxH,MAAM,WAAW,YAAY;IAC3B,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;AAED,wBAAgB,YAAY,CAAC,GAAG,GAAE,GAAiB,GAAG,YAAY,CAMjE;AAYD,MAAM,WAAW,UAAU;IACzB,KAAK,EAAE,WAAW,CAAA;IAClB,2DAA2D;IAC3D,KAAK,EAAE,OAAO,CAAA;IACd,2EAA2E;IAC3E,GAAG,EAAE,MAAM,CAAA;CACZ;AAED,qHAAqH;AACrH,eAAO,MAAM,mBAAmB,QAAc,CAAA;AA6D9C;;;;GAIG;AACH,wBAAsB,SAAS,CAAC,GAAG,GAAE,GAAiB,EAAE,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,KAAK,IAAI,GAAG,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC,CAQnI;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,GAAG,GAAE,GAAiB,EAAE,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,KAAK,IAAI,GAAG,UAAU,GAAG,IAAI,CAIrH;AAED,oHAAoH;AACpH,eAAO,MAAM,eAAe,QAAc,CAAA;AAE1C,oHAAoH;AACpH,wBAAgB,SAAS,IAAI,MAAM,CAElC;AAED,MAAM,MAAM,gBAAgB,GAAG,WAAW,GAAG,iBAAiB,GAAG,mBAAmB,GAAG,gBAAgB,CAAA;AAKvG;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,gBAAgB,EAAE,SAAS,EAAE,CAAC,CAAC,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,KAAK,IAAI,CAAC,GAAG,SAAS,EAAE,GAAG,GAAE,GAAiB,GAAG,IAAI,CAanJ;AAED,uDAAuD;AACvD,wBAAgB,WAAW,IAAI,IAAI,CAOlC"}
|
package/dist/edge/url-map.js
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
import { isNoUrlMapStatus, parseStoreUrlMap } from '../lib/market.js';
|
|
2
|
-
export function edgeStoreEnv(env = process.env) {
|
|
3
|
-
return {
|
|
4
|
-
medusaUrl: env.NEXT_PUBLIC_MEDUSA_BACKEND_URL,
|
|
5
|
-
publishableKey: env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY,
|
|
6
|
-
storeId: env.NEXT_PUBLIC_STORE_ID,
|
|
7
|
-
};
|
|
8
|
-
}
|
|
9
|
-
/** The headers every store route needs: the publishable key and the tenant (the store's dev environment id). */
|
|
10
|
-
function storeRouteHeaders({ publishableKey, storeId }) {
|
|
11
|
-
return {
|
|
12
|
-
...(publishableKey ? { 'x-publishable-api-key': publishableKey } : {}),
|
|
13
|
-
...(storeId ? { 'X-Store-Environment-ID': storeId } : {}),
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
const storeApiBase = (store) => store.medusaUrl?.replace(/\/+$/, '') || undefined;
|
|
17
|
-
/** A copy older than this still routes, but never redirects: a 30x from a long-stale copy could send people away. */
|
|
18
|
-
export const MAX_REDIRECT_AGE_MS = 10 * 60_000;
|
|
19
|
-
const REFRESH_MS = 30_000;
|
|
20
|
-
/** After a failed read with no copy, the next try waits this long (a burst of cold requests never hammers Medusa). */
|
|
21
|
-
const COLD_RETRY_MS = 5_000;
|
|
22
|
-
const FETCH_TIMEOUT_MS = 1_500;
|
|
23
|
-
let copy = null;
|
|
24
|
-
/** The map of a store API that has none: no market URL, nothing to resolve. */
|
|
25
|
-
const NO_MAP = { version: '', hreflang: false, language: '', rootCountries: [], presences: [] };
|
|
26
|
-
let stale = false;
|
|
27
|
-
let nextTryAt = Number.NEGATIVE_INFINITY;
|
|
28
|
-
/** When the first read of a cold instance failed (null: a copy exists, or no read failed yet). */
|
|
29
|
-
let coldSince = null;
|
|
30
|
-
let refreshing = null;
|
|
31
|
-
async function refresh(base, store) {
|
|
32
|
-
try {
|
|
33
|
-
const res = await fetch(`${base}/store/url-map`, {
|
|
34
|
-
headers: { ...storeRouteHeaders(store), ...(copy?.etag ? { 'if-none-match': copy.etag } : {}) },
|
|
35
|
-
cache: 'no-store',
|
|
36
|
-
signal: AbortSignal.timeout(FETCH_TIMEOUT_MS),
|
|
37
|
-
});
|
|
38
|
-
if (res.status === 304 && copy) {
|
|
39
|
-
copy.confirmedAt = Date.now();
|
|
40
|
-
stale = false;
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
const goodCopy = copy !== null && copy.value !== NO_MAP;
|
|
44
|
-
if (isNoUrlMapStatus(res.status) && !goodCopy) {
|
|
45
|
-
copy = { value: NO_MAP, etag: null, confirmedAt: Date.now() };
|
|
46
|
-
stale = false;
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
const value = res.ok ? parseStoreUrlMap(await res.json()) : null;
|
|
50
|
-
if (!value)
|
|
51
|
-
throw new Error(`url-map: HTTP ${res.status}`);
|
|
52
|
-
copy = { value, etag: res.headers.get('etag'), confirmedAt: Date.now() };
|
|
53
|
-
stale = false;
|
|
54
|
-
}
|
|
55
|
-
catch {
|
|
56
|
-
stale = true;
|
|
57
|
-
}
|
|
58
|
-
finally {
|
|
59
|
-
coldSince = copy ? null : (coldSince ?? Date.now());
|
|
60
|
-
// With a copy the next try is in 30 s; without one, in 5 s.
|
|
61
|
-
nextTryAt = Date.now() + (copy ? REFRESH_MS : COLD_RETRY_MS);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
/** The refresh in flight, starting one when it is due; null when none is due or there is no store API. */
|
|
65
|
-
function refreshIfDue(env) {
|
|
66
|
-
const store = edgeStoreEnv(env);
|
|
67
|
-
const base = storeApiBase(store);
|
|
68
|
-
if (!base || (!refreshing && Date.now() < nextTryAt))
|
|
69
|
-
return null;
|
|
70
|
-
refreshing ??= refresh(base, store).finally(() => {
|
|
71
|
-
refreshing = null;
|
|
72
|
-
});
|
|
73
|
-
return refreshing;
|
|
74
|
-
}
|
|
75
|
-
const currentCopy = () => copy && { value: copy.value, stale, age: Date.now() - copy.confirmedAt };
|
|
76
|
-
/**
|
|
77
|
-
* The URL map for this request, or null when none was ever read (no Medusa URL, or Medusa never answered).
|
|
78
|
-
* `waitUntil` (the proxy's `event.waitUntil`) lets a warm instance refresh in the background; without it every
|
|
79
|
-
* refresh is awaited.
|
|
80
|
-
*/
|
|
81
|
-
export async function getUrlMap(env = process.env, waitUntil) {
|
|
82
|
-
if (!storeApiBase(edgeStoreEnv(env)))
|
|
83
|
-
return null;
|
|
84
|
-
const pending = refreshIfDue(env);
|
|
85
|
-
if (pending) {
|
|
86
|
-
if (copy && waitUntil)
|
|
87
|
-
waitUntil(pending);
|
|
88
|
-
else
|
|
89
|
-
await pending;
|
|
90
|
-
}
|
|
91
|
-
return currentCopy();
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* The copy in memory, or null, never waiting for Medusa (a file path never waits). A refresh that is due (a cold
|
|
95
|
-
* instance's first read included) runs in the background through `waitUntil`, so a later request has the map; none
|
|
96
|
-
* without it.
|
|
97
|
-
*/
|
|
98
|
-
export function peekUrlMap(env = process.env, waitUntil) {
|
|
99
|
-
const pending = waitUntil ? refreshIfDue(env) : null;
|
|
100
|
-
if (pending)
|
|
101
|
-
waitUntil?.(pending);
|
|
102
|
-
return currentCopy();
|
|
103
|
-
}
|
|
104
|
-
/** A cold instance's 503 for a market-shaped path gives up after this long without a map (a revoked key, a WAF). */
|
|
105
|
-
export const MAX_COLD_503_MS = 10 * 60_000;
|
|
106
|
-
/** Milliseconds since this instance's first failed read with no map yet; 0 once it has one (or before it tried). */
|
|
107
|
-
export function coldForMs() {
|
|
108
|
-
return coldSince === null ? 0 : Date.now() - coldSince;
|
|
109
|
-
}
|
|
110
|
-
const SIGNAL_EVERY_MS = 60_000;
|
|
111
|
-
const signalledAt = new Map();
|
|
112
|
-
/**
|
|
113
|
-
* Counts a fallback in Medusa (`POST /store/market-signals`, a Datadog counter): at most once a minute per kind per
|
|
114
|
-
* instance, through `waitUntil` so it never delays the response. A lost beacon loses a count, never a page.
|
|
115
|
-
*/
|
|
116
|
-
export function sendMarketSignal(kind, waitUntil, env = process.env) {
|
|
117
|
-
const store = edgeStoreEnv(env);
|
|
118
|
-
const base = storeApiBase(store);
|
|
119
|
-
const now = Date.now();
|
|
120
|
-
if (!base || now - (signalledAt.get(kind) ?? Number.NEGATIVE_INFINITY) < SIGNAL_EVERY_MS)
|
|
121
|
-
return;
|
|
122
|
-
signalledAt.set(kind, now);
|
|
123
|
-
const sent = fetch(`${base}/store/market-signals`, {
|
|
124
|
-
method: 'POST',
|
|
125
|
-
headers: { 'content-type': 'application/json', ...storeRouteHeaders(store) },
|
|
126
|
-
body: JSON.stringify({ kind }),
|
|
127
|
-
signal: AbortSignal.timeout(FETCH_TIMEOUT_MS),
|
|
128
|
-
}).catch(() => undefined);
|
|
129
|
-
waitUntil?.(sent);
|
|
130
|
-
}
|
|
131
|
-
/** Test seam: forget the copy and the signal clock. */
|
|
132
|
-
export function resetUrlMap() {
|
|
133
|
-
copy = null;
|
|
134
|
-
coldSince = null;
|
|
135
|
-
stale = false;
|
|
136
|
-
nextTryAt = Number.NEGATIVE_INFINITY;
|
|
137
|
-
refreshing = null;
|
|
138
|
-
signalledAt.clear();
|
|
139
|
-
}
|
|
140
|
-
//# sourceMappingURL=url-map.js.map
|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