|
| 1 | +/* |
| 2 | + * Copyright 2026 Adobe. All rights reserved. |
| 3 | + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | + * of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | + * OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | + * governing permissions and limitations under the License. |
| 11 | + */ |
| 12 | +/** |
| 13 | + * Parses dashboard arrays, JSON arrays or comma/newline-separated migration entries. |
| 14 | + * @param {string[]|string|undefined} value Migration-variable value. |
| 15 | + * @param {string} name Variable name used in configuration errors. |
| 16 | + * @returns {unknown[]} Parsed entries, or an empty array for an unset variable. |
| 17 | + * @throws {Error} If a provided value has an unsupported type or malformed JSON array. |
| 18 | + */ |
| 19 | +function entries(value, name) { |
| 20 | + if (!value) return []; |
| 21 | + if (Array.isArray(value)) return value; |
| 22 | + if (typeof value !== 'string') { |
| 23 | + throw new Error(`Invalid ${name}`); |
| 24 | + } |
| 25 | + const trimmed = value.trim(); |
| 26 | + if (trimmed.startsWith('{')) { |
| 27 | + throw new Error(`Invalid ${name}`); |
| 28 | + } |
| 29 | + if (!trimmed.startsWith('[')) return value.split(/[,\n]/); |
| 30 | + try { |
| 31 | + return JSON.parse(trimmed); |
| 32 | + } catch { |
| 33 | + throw new Error(`Invalid JSON in ${name}`); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Normalizes a single org/site entry, ignoring values that do not identify exactly two segments. |
| 39 | + * @param {string} value Candidate org/site entry. |
| 40 | + * @returns {string|undefined} Lowercase org/site key, or undefined for an invalid entry. |
| 41 | + */ |
| 42 | +function orgSite(value) { |
| 43 | + const parts = value.trim().toLowerCase().split('/').filter(Boolean); |
| 44 | + return parts.length === 2 ? parts.join('/') : undefined; |
| 45 | +} |
| 46 | + |
| 47 | +let migratedCache = { raw: undefined, sites: new Set() }; |
| 48 | +let dualWriteCache = { raw: undefined, sources: new Set() }; |
| 49 | + |
| 50 | +/** |
| 51 | + * Parses and caches migrated org/site entries keyed by the raw configuration value. |
| 52 | + * @param {string[]|string|undefined} raw Raw HLX6_MIGRATED_SITES configuration. |
| 53 | + * @returns {Set<string>} Normalized migrated org/site keys. |
| 54 | + * @throws {Error} If HLX6_MIGRATED_SITES cannot be parsed. |
| 55 | + */ |
| 56 | +function getMigratedSites(raw) { |
| 57 | + if (migratedCache.raw === raw) return migratedCache.sites; |
| 58 | + const sites = new Set(); |
| 59 | + entries(raw, 'HLX6_MIGRATED_SITES').forEach((entry) => { |
| 60 | + if (typeof entry === 'string' && !entry.includes('->')) { |
| 61 | + const parsed = orgSite(entry); |
| 62 | + if (parsed) sites.add(parsed); |
| 63 | + } |
| 64 | + }); |
| 65 | + migratedCache = { raw, sites }; |
| 66 | + return sites; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Parses and caches valid dual-write source entries keyed by the raw configuration value. |
| 71 | + * @param {string[]|string|undefined} raw Raw HLX6_DUAL_WRITES configuration. |
| 72 | + * @returns {Set<string>} Normalized dual-write source org/site keys. |
| 73 | + * @throws {Error} If HLX6_DUAL_WRITES cannot be parsed. |
| 74 | + */ |
| 75 | +function getDualWriteSources(raw) { |
| 76 | + if (dualWriteCache.raw === raw) return dualWriteCache.sources; |
| 77 | + const sources = new Set(); |
| 78 | + entries(raw, 'HLX6_DUAL_WRITES').forEach((entry) => { |
| 79 | + if (typeof entry !== 'string') return; |
| 80 | + const [from, to, ...rest] = entry.split('->'); |
| 81 | + const source = orgSite(from); |
| 82 | + if (!rest.length && source && (to === undefined || !!orgSite(to))) { |
| 83 | + sources.add(source); |
| 84 | + } |
| 85 | + }); |
| 86 | + dualWriteCache = { raw, sources }; |
| 87 | + return sources; |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Selects migrated sources for admin routing while excluding valid dual-write source entries. |
| 92 | + * A mapped dual-write target does not exempt that target from migrated routing. |
| 93 | + * @param {Object} env Worker migration configuration. |
| 94 | + * @param {string[]|string} [env.HLX6_MIGRATED_SITES] Migrated org/site entries. |
| 95 | + * @param {string[]|string} [env.HLX6_DUAL_WRITES] Source entries with optional target mappings. |
| 96 | + * @param {string} org Content organization. |
| 97 | + * @param {string} site Content site. |
| 98 | + * @returns {boolean} Whether requests must use admin rather than direct R2. |
| 99 | + * @throws {Error} If either configured migration list cannot be parsed. |
| 100 | + */ |
| 101 | +export default function isHlx6Site(env, org, site) { |
| 102 | + const key = `${org}/${site}`.toLowerCase(); |
| 103 | + const migrated = getMigratedSites(env.HLX6_MIGRATED_SITES); |
| 104 | + const dualWrite = getDualWriteSources(env.HLX6_DUAL_WRITES); |
| 105 | + return migrated.has(key) && !dualWrite.has(key); |
| 106 | +} |
0 commit comments