|
| 1 | +'use strict'; /*jslint node:true es9:true*/ |
| 2 | + |
| 3 | +// Pure helpers for the geo_fanout tool: validating the list of country exits |
| 4 | +// and turning a set of per-geo results into a single structured report where a |
| 5 | +// blocked / redirected / failed geo is a FIRST-CLASS classified result, not a |
| 6 | +// discarded error. No network or transport dependencies live here so the fanout |
| 7 | +// aggregation logic is unit-testable in isolation. |
| 8 | + |
| 9 | +import {classify_response, OUTCOME} from './retry_utils.js'; |
| 10 | + |
| 11 | +// Per-geo status the caller sees in the aggregated report. |
| 12 | +export const GEO_STATUS = { |
| 13 | + OK: 'ok', // fetched successfully |
| 14 | + BLOCKED: 'blocked', // target refused this geo (403/451) |
| 15 | + REDIRECTED: 'redirected', // 3xx to a different host/path (geo gating signal) |
| 16 | + RATE_LIMITED: 'rate_limited', |
| 17 | + ERROR: 'error', // transient/fatal failure after retries |
| 18 | +}; |
| 19 | + |
| 20 | +// Validate and normalize a list of 2-letter ISO country codes. Lowercases, |
| 21 | +// trims, dedupes (preserving first-seen order), and rejects malformed entries |
| 22 | +// loudly so a typo never becomes a silently-dropped exit. Returns the clean |
| 23 | +// array; throws Error on any invalid code (callers want fast, explicit failure). |
| 24 | +export function normalize_geos(geos){ |
| 25 | + if (!Array.isArray(geos) || geos.length===0) |
| 26 | + throw new Error('geos must be a non-empty array of 2-letter codes'); |
| 27 | + const seen = new Set(); |
| 28 | + const out = []; |
| 29 | + for (const raw of geos) |
| 30 | + { |
| 31 | + if (typeof raw!='string') |
| 32 | + throw new Error(`invalid geo (not a string): ${JSON.stringify(raw)}`); |
| 33 | + const code = raw.trim().toLowerCase(); |
| 34 | + if (!/^[a-z]{2}$/.test(code)) |
| 35 | + throw new Error(`invalid geo country code: "${raw}" ` |
| 36 | + +`(expected 2 letters, e.g. "us", "de")`); |
| 37 | + if (seen.has(code)) |
| 38 | + continue; |
| 39 | + seen.add(code); |
| 40 | + out.push(code); |
| 41 | + } |
| 42 | + return out; |
| 43 | +} |
| 44 | + |
| 45 | +// Detect whether a successful-looking response is actually a geo redirect. |
| 46 | +// A 3xx with a Location header pointing at a different host is the classic |
| 47 | +// "Belgian visitor bounced to a different storefront" signal we must capture. |
| 48 | +function detect_redirect(status, headers, request_url){ |
| 49 | + if (!(status>=300 && status<400)) |
| 50 | + return null; |
| 51 | + const location = read_header(headers, 'location'); |
| 52 | + if (!location) |
| 53 | + return {redirected: true, location: null, cross_host: false}; |
| 54 | + let cross_host = false; |
| 55 | + try { |
| 56 | + const from = new URL(request_url); |
| 57 | + const to = new URL(location, request_url); |
| 58 | + cross_host = from.host!==to.host; |
| 59 | + } catch(e){ |
| 60 | + cross_host = false; |
| 61 | + } |
| 62 | + return {redirected: true, location, cross_host}; |
| 63 | +} |
| 64 | + |
| 65 | +function read_header(headers, name){ |
| 66 | + if (!headers || typeof headers!='object') |
| 67 | + return undefined; |
| 68 | + const target = name.toLowerCase(); |
| 69 | + for (const key of Object.keys(headers)) |
| 70 | + { |
| 71 | + if (key.toLowerCase()===target) |
| 72 | + return headers[key]; |
| 73 | + } |
| 74 | + return undefined; |
| 75 | +} |
| 76 | + |
| 77 | +// Map one classified outcome to the per-geo report status. |
| 78 | +function outcome_to_geo_status(outcome){ |
| 79 | + switch (outcome) |
| 80 | + { |
| 81 | + case OUTCOME.SUCCESS: return GEO_STATUS.OK; |
| 82 | + case OUTCOME.BLOCKED: return GEO_STATUS.BLOCKED; |
| 83 | + case OUTCOME.RATE_LIMITED: return GEO_STATUS.RATE_LIMITED; |
| 84 | + default: return GEO_STATUS.ERROR; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// Build the per-geo entry for a single settled attempt. `attempt` is the |
| 89 | +// normalized shape produced by the tool's executor: |
| 90 | +// {geo, url, response:{status, headers, exit_ip?}} on a completed request, or |
| 91 | +// {geo, url, error:{code?, response?}} on a thrown failure. |
| 92 | +// `now_ms` is injectable for deterministic Retry-After math under test. |
| 93 | +export function build_geo_entry(attempt, now_ms = Date.now()){ |
| 94 | + const obj = attempt && typeof attempt=='object' ? attempt : {}; |
| 95 | + const geo = typeof obj.geo=='string' ? obj.geo : 'unknown'; |
| 96 | + const url = typeof obj.url=='string' ? obj.url : null; |
| 97 | + // Normalize to the shape classify_response understands: a thrown error keeps |
| 98 | + // its {error} envelope; a completed request passes its {status, headers}. |
| 99 | + const classify_input = obj.error ? {error: obj.error} |
| 100 | + : (obj.response || {}); |
| 101 | + const classification = classify_response(classify_input, now_ms); |
| 102 | + const status = classification.status; |
| 103 | + const headers = obj.response ? obj.response.headers |
| 104 | + : (obj.error && obj.error.response |
| 105 | + ? obj.error.response.headers : undefined); |
| 106 | + const exit_ip = obj.response |
| 107 | + && typeof obj.response.exit_ip=='string' |
| 108 | + ? obj.response.exit_ip : null; |
| 109 | + |
| 110 | + const redirect = detect_redirect(status, headers, url); |
| 111 | + let geo_status = outcome_to_geo_status(classification.outcome); |
| 112 | + if (redirect && redirect.redirected) |
| 113 | + geo_status = GEO_STATUS.REDIRECTED; |
| 114 | + |
| 115 | + return { |
| 116 | + geo, |
| 117 | + url, |
| 118 | + status: geo_status, |
| 119 | + http_status: status, |
| 120 | + exit_ip, |
| 121 | + outcome: classification.outcome, |
| 122 | + retry_after_ms: classification.retry_after_ms, |
| 123 | + reason: classification.reason, |
| 124 | + redirect: redirect || null, |
| 125 | + }; |
| 126 | +} |
| 127 | + |
| 128 | +// Aggregate all per-geo entries into one report. Every geo appears exactly once |
| 129 | +// regardless of success/failure; nothing is dropped. The summary makes the |
| 130 | +// "this geo was blocked / redirected" fact queryable at a glance. |
| 131 | +export function summarize_fanout(entries){ |
| 132 | + const list = Array.isArray(entries) ? entries : []; |
| 133 | + const summary = { |
| 134 | + total: list.length, |
| 135 | + ok: 0, |
| 136 | + blocked: 0, |
| 137 | + redirected: 0, |
| 138 | + rate_limited: 0, |
| 139 | + error: 0, |
| 140 | + }; |
| 141 | + for (const e of list) |
| 142 | + { |
| 143 | + switch (e.status) |
| 144 | + { |
| 145 | + case GEO_STATUS.OK: summary.ok++; break; |
| 146 | + case GEO_STATUS.BLOCKED: summary.blocked++; break; |
| 147 | + case GEO_STATUS.REDIRECTED: summary.redirected++; break; |
| 148 | + case GEO_STATUS.RATE_LIMITED: summary.rate_limited++; break; |
| 149 | + default: summary.error++; break; |
| 150 | + } |
| 151 | + } |
| 152 | + return { |
| 153 | + summary, |
| 154 | + any_blocked: summary.blocked>0, |
| 155 | + any_redirected: summary.redirected>0, |
| 156 | + results: list, |
| 157 | + }; |
| 158 | +} |
0 commit comments