All files / scripts riksbank-fetch.ts

77.05% Statements 131/170
64.15% Branches 68/106
72.22% Functions 13/18
78.43% Lines 120/153

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439                                                                                            2x           2x                               7x 7x     7x 6x 6x 6x 6x 6x 5x 5x 5x 4x 4x   1x     5x       5x 1x         18x 18x   1x   17x 15x 3x   12x       3x     3x     2x 2x     2x     2x   2x     4x 1x       5x       9x                             4x 4x                         6x 1x 1x                   5x 5x 5x 5x 5x 5x 5x 10x 10x 5x 5x 5x         5x     5x   5x     5x 5x 5x 5x 5x   5x                 9x 9x 10x         9x 2x 2x       2x 2x 2x 2x 2x   7x                   9x 9x 9x 9x   9x     9x 9x   2x 2x               7x   7x 1x 1x               6x 9x 9x   9x   1x                 1x 1x               1x   1x 1x         1x                       5x 2x 1x 1x             1x 1x               1x 1x 1x                         3x                 3x 3x               3x 3x 3x 3x                       9x                                                                         2x 2x 2x     2x              
#!/usr/bin/env tsx
/**
 * @module scripts/riksbank-fetch
 * @description Fetches public Riksbank web/JSON artifacts for Swedish
 * monetary-policy and fuel-price transmission context.
 */
 
import path from 'node:path';
import { pathToFileURL } from 'node:url';
 
import { persistRiksbankData } from './parliamentary-data/data-persistence.js';
 
export type RiksbankArtifactKind = 'repo-rate-path' | 'minutes' | 'fuel-price-context';
 
export interface RiksbankEconomicProvenance {
  readonly provider: 'riksbank';
  readonly dataflow: 'riksbank-web';
  readonly indicator: RiksbankArtifactKind;
  readonly url: string;
  readonly retrieved_at: string;
}
 
export interface RiksbankFetchPayload {
  readonly provider: 'riksbank';
  readonly kind: RiksbankArtifactKind;
  readonly url: string;
  readonly contentType: string;
  readonly retrievedAt: string;
  readonly status: 'ok' | 'no-data';
  readonly warning?: string;
  readonly title?: string;
  readonly json?: unknown;
  readonly text?: string;
  /** Base64-encoded payload for binary responses (e.g. PDF). Capped at PDF_MAX_BYTES. */
  readonly pdfBase64?: string;
  /** Byte length of the binary payload before base64 encoding. */
  readonly pdfBytes?: number;
  readonly economicProvenance: RiksbankEconomicProvenance;
}
 
interface ParsedArgs {
  readonly command: 'repo-rate-path' | 'minutes' | 'fuel-price-context' | 'fetch' | 'help';
  readonly flags: ReadonlyMap<string, string>;
  readonly booleans: ReadonlySet<string>;
}
 
const DEFAULT_URLS: Readonly<Record<RiksbankArtifactKind, string>> = Object.freeze({
  'repo-rate-path': 'https://www.riksbank.se/en-gb/monetary-policy/the-policy-rate/',
  minutes: 'https://www.riksbank.se/en-gb/monetary-policy/monetary-policy-minutes/',
  'fuel-price-context': 'https://www.riksbank.se/en-gb/monetary-policy/monetary-policy-reports/',
});
 
const HELP = `tsx scripts/riksbank-fetch.ts <command> [flags]
 
Commands:
  repo-rate-path      Fetch the policy-rate path source page unless --url overrides it
  minutes             Fetch monetary-policy minutes source page/PDF/JSON
  fuel-price-context  Fetch monetary-policy report context for fuel/energy assumptions
  fetch               Fetch a custom --kind and --url pair from www.riksbank.se
  help                Show this message
 
Flags:
  --url <URL>      Riksbank HTTPS URL (host must be www.riksbank.se or riksbank.se)
  --kind <KIND>    repo-rate-path | minutes | fuel-price-context (fetch command)
  --persist        Write output under analysis/data/riksbank/
`;
 
export function parseRiksbankArgs(argv: readonly string[]): ParsedArgs {
  const command = (argv[0] ?? 'help') as ParsedArgs['command'];
  const validCommands: readonly ParsedArgs['command'][] = [
    'repo-rate-path', 'minutes', 'fuel-price-context', 'fetch', 'help',
  ];
  if (!validCommands.includes(command)) throw new Error(`unknown command ${command}`);
  const flags = new Map<string, string>();
  const booleans = new Set<string>();
  for (let i = 1; i < argv.length; i++) {
    const token = argv[i];
    if (!token.startsWith('--')) throw new Error(`unexpected positional argument ${token}`);
    const key = token.slice(2);
    const next = argv[i + 1];
    if (next !== undefined && !next.startsWith('--')) {
      flags.set(key, next);
      i++;
    } else {
      booleans.add(key);
    }
  }
  return { command, flags, booleans };
}
 
export function parseRiksbankKind(value: string): RiksbankArtifactKind {
  if (value === 'repo-rate-path' || value === 'minutes' || value === 'fuel-price-context') return value;
  throw new Error(`unknown Riksbank artifact kind ${value}`);
}
 
export function assertRiksbankFetchTarget(url: string): URL {
  let parsed: URL;
  try {
    parsed = new URL(url);
  } catch {
    throw new Error(`invalid Riksbank URL: ${url}`);
  }
  if (parsed.protocol !== 'https:') throw new Error('Riksbank fetch URL must use HTTPS');
  if (parsed.hostname !== 'www.riksbank.se' && parsed.hostname !== 'riksbank.se') {
    throw new Error(`Riksbank host ${parsed.hostname} is not in allowlist`);
  }
  return parsed;
}
 
function extractTitle(text: string): string | undefined {
  const title = /<title[^>]*>(.*?)<\/title>/is.exec(text)?.[1]
    ?.replace(/\s+/g, ' ')
    .trim();
  return title && title.length > 0 ? title : undefined;
}
 
const DEFAULT_RIKSBANK_TIMEOUT_MS = 15_000;
const TEXT_MAX_BYTES = 20_000;
/** Hard cap on HTML/text body size before truncation. Prevents pathological
 *  HTML pages from exhausting memory while still allowing a generous slice. */
const TEXT_RESPONSE_MAX_BYTES = 2 * 1024 * 1024;
/** Hard cap on PDF size accepted from Riksbank. Matches a generous monetary-policy
 *  report size (~5 MB) without allowing pathological responses to exhaust memory. */
const PDF_MAX_BYTES = 5 * 1024 * 1024;
/** Maximum number of redirects to follow manually while re-validating each host. */
const MAX_REDIRECTS = 3;
 
async function safeCancel(target: ReadableStreamDefaultReader<Uint8Array> | ReadableStream<Uint8Array> | null | undefined): Promise<void> {
  if (!target) return;
  try { await target.cancel(); } catch { /* ignore cancel errors */ }
}
 
async function safeReleaseLock(reader: ReadableStreamDefaultReader<Uint8Array>): Promise<void> {
  try { reader.releaseLock(); } catch { /* ignore release errors */ }
}
 
function buildProvenance(kind: RiksbankArtifactKind, url: string, retrievedAt: string): RiksbankEconomicProvenance {
  return {
    provider: 'riksbank',
    dataflow: 'riksbank-web',
    indicator: kind,
    url,
    retrieved_at: retrievedAt,
  };
}
 
function buildOutagePayload(
  kind: RiksbankArtifactKind,
  url: string,
  contentType: string,
  warning: string,
): RiksbankFetchPayload {
  const retrievedAt = new Date().toISOString();
  return {
    provider: 'riksbank',
    kind,
    url,
    contentType,
    retrievedAt,
    status: 'no-data',
    warning,
    economicProvenance: buildProvenance(kind, url, retrievedAt),
  };
}
 
function parseContentLength(header: string | null): number | undefined {
  if (!header) return undefined;
  const value = Number.parseInt(header, 10);
  return Number.isFinite(value) && value >= 0 ? value : undefined;
}
 
/** Read a `ReadableStream<Uint8Array>` body with a hard byte cap. The returned
 *  `bytes` holds the data read so far; `exceeded === true` means the cap was
 *  hit and `bytes` should not be consumed. */
async function readBodyWithCap(
  body: ReadableStream<Uint8Array> | null,
  maxBytes: number,
): Promise<{ exceeded: boolean; bytes: Uint8Array; bytesRead: number }> {
  Iif (!body) return { exceeded: false, bytes: new Uint8Array(0), bytesRead: 0 };
  const reader = body.getReader();
  const chunks: Uint8Array[] = [];
  let total = 0;
  let exceeded = false;
  try {
    for (;;) {
      const { value, done } = await reader.read();
      if (done) break;
      Iif (!value) continue;
      total += value.byteLength;
      Iif (total > maxBytes) {
        exceeded = true;
        await safeCancel(reader);
        break;
      }
      chunks.push(value);
    }
  } finally {
    await safeReleaseLock(reader);
  }
  Iif (exceeded) {
    return { exceeded: true, bytes: new Uint8Array(0), bytesRead: total };
  }
  const out = new Uint8Array(total);
  let offset = 0;
  for (const chunk of chunks) {
    out.set(chunk, offset);
    offset += chunk.byteLength;
  }
  return { exceeded: false, bytes: out, bytesRead: total };
}
 
/** Perform a single fetch with manual-redirect handling against the Riksbank
 *  host allowlist. Returns the final 2xx Response or throws. */
async function fetchWithManualRedirects(
  target: URL,
  signal: AbortSignal,
): Promise<{ response: Response; finalUrl: URL }> {
  let current = target;
  for (let hop = 0; hop <= MAX_REDIRECTS; hop++) {
    const response = await fetch(current, {
      headers: { Accept: 'application/json, text/html, application/pdf;q=0.8, text/plain;q=0.7' },
      signal,
      redirect: 'manual',
    });
    if (response.status >= 300 && response.status < 400) {
      const location = response.headers.get('location');
      Iif (!location) {
        throw new Error(`Riksbank redirect ${response.status} without Location header`);
      }
      // Drain redirect body to free the connection.
      await safeCancel(response.body);
      const next = new URL(location, current);
      assertRiksbankFetchTarget(next.toString());
      current = next;
      continue;
    }
    return { response, finalUrl: current };
  }
  throw new Error(`Riksbank fetch exceeded ${MAX_REDIRECTS} redirects`);
}
 
export async function fetchRiksbankPayload(
  kind: RiksbankArtifactKind,
  url: string,
  options: { timeoutMs?: number } = {},
): Promise<RiksbankFetchPayload> {
  const target = assertRiksbankFetchTarget(url);
  const timeoutMs = options.timeoutMs ?? DEFAULT_RIKSBANK_TIMEOUT_MS;
  const controller = new AbortController();
  const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
 
  try {
    let response: Response;
    let finalUrl: URL;
    try {
      ({ response, finalUrl } = await fetchWithManualRedirects(target, controller.signal));
    } catch (error) {
      const detail = error instanceof Error ? error.message : String(error);
      return buildOutagePayload(
        kind,
        target.toString(),
        'application/octet-stream',
        `Riksbank fetch failed (${detail}); callers should fall back to cached analysis/data/riksbank/ artifacts.`,
      );
    }
 
    const finalUrlStr = finalUrl.toString();
 
    if (!response.ok) {
      await safeCancel(response.body);
      return buildOutagePayload(
        kind,
        finalUrlStr,
        response.headers.get('content-type') ?? 'application/octet-stream',
        `Riksbank fetch returned HTTP ${response.status} ${response.statusText}; callers should fall back to cached analysis/data/riksbank/ artifacts.`,
      );
    }
 
    const contentType = response.headers.get('content-type') ?? 'application/octet-stream';
    const contentLength = parseContentLength(response.headers.get('content-length'));
    const retrievedAt = new Date().toISOString();
 
    if (contentType.includes('json')) {
      // JSON responses are typically small; still cap to TEXT_RESPONSE_MAX_BYTES.
      Iif (contentLength !== undefined && contentLength > TEXT_RESPONSE_MAX_BYTES) {
        await safeCancel(response.body);
        return buildOutagePayload(
          kind,
          finalUrlStr,
          contentType,
          `Riksbank JSON Content-Length ${contentLength} exceeds cap ${TEXT_RESPONSE_MAX_BYTES}; persisted as no-data.`,
        );
      }
      const capped = await readBodyWithCap(response.body, TEXT_RESPONSE_MAX_BYTES);
      Iif (capped.exceeded) {
        return buildOutagePayload(
          kind,
          finalUrlStr,
          contentType,
          `Riksbank JSON body exceeded ${TEXT_RESPONSE_MAX_BYTES} bytes (read ${capped.bytesRead}); persisted as no-data.`,
        );
      }
      const jsonBytes = capped.bytes;
      let json: unknown;
      try {
        json = JSON.parse(new TextDecoder('utf-8').decode(jsonBytes));
      } catch (error) {
        const detail = error instanceof Error ? error.message : String(error);
        return buildOutagePayload(kind, finalUrlStr, contentType, `Riksbank JSON parse failed (${detail}).`);
      }
      return {
        provider: 'riksbank',
        kind,
        url: finalUrlStr,
        contentType,
        retrievedAt,
        status: 'ok',
        json,
        economicProvenance: buildProvenance(kind, finalUrlStr, retrievedAt),
      };
    }
 
    if (contentType.includes('pdf')) {
      if (contentLength !== undefined && contentLength > PDF_MAX_BYTES) {
        await safeCancel(response.body);
        return buildOutagePayload(
          kind,
          finalUrlStr,
          contentType,
          `Riksbank PDF Content-Length ${contentLength} exceeds cap ${PDF_MAX_BYTES}; persisted as no-data.`,
        );
      }
      const capped = await readBodyWithCap(response.body, PDF_MAX_BYTES);
      Iif (capped.exceeded) {
        return buildOutagePayload(
          kind,
          finalUrlStr,
          contentType,
          `Riksbank PDF body exceeded ${PDF_MAX_BYTES} bytes (read ${capped.bytesRead}); persisted as no-data.`,
        );
      }
      const pdfBytesRaw = capped.bytes;
      const pdfBase64 = Buffer.from(pdfBytesRaw).toString('base64');
      return {
        provider: 'riksbank',
        kind,
        url: finalUrlStr,
        contentType,
        retrievedAt,
        status: 'ok',
        pdfBase64,
        pdfBytes: pdfBytesRaw.byteLength,
        economicProvenance: buildProvenance(kind, finalUrlStr, retrievedAt),
      };
    }
 
    Iif (contentLength !== undefined && contentLength > TEXT_RESPONSE_MAX_BYTES) {
      await safeCancel(response.body);
      return buildOutagePayload(
        kind,
        finalUrlStr,
        contentType,
        `Riksbank text Content-Length ${contentLength} exceeds cap ${TEXT_RESPONSE_MAX_BYTES}; persisted as no-data.`,
      );
    }
    const capped = await readBodyWithCap(response.body, TEXT_RESPONSE_MAX_BYTES);
    Iif (capped.exceeded) {
      return buildOutagePayload(
        kind,
        finalUrlStr,
        contentType,
        `Riksbank text body exceeded ${TEXT_RESPONSE_MAX_BYTES} bytes (read ${capped.bytesRead}); persisted as no-data.`,
      );
    }
    const textBytes = capped.bytes;
    const text = new TextDecoder('utf-8').decode(textBytes);
    const title = extractTitle(text);
    return {
      provider: 'riksbank',
      kind,
      url: finalUrlStr,
      contentType,
      retrievedAt,
      status: 'ok',
      ...(title ? { title } : {}),
      text: text.slice(0, TEXT_MAX_BYTES),
      economicProvenance: buildProvenance(kind, finalUrlStr, retrievedAt),
    };
  } finally {
    clearTimeout(timeoutId);
  }
}
 
async function runKind(
  kind: RiksbankArtifactKind,
  url: string,
  booleans: ReadonlySet<string>,
): Promise<void> {
  const payload = await fetchRiksbankPayload(kind, url);
  process.stdout.write(`${JSON.stringify(payload, null, 2)}\n`);
  if (booleans.has('persist')) persistRiksbankData(kind, payload);
}
 
function requireRiksbankFlag(
  flags: ReadonlyMap<string, string>,
  name: string,
): string {
  const value = flags.get(name)?.trim();
  if (!value) throw new Error(`missing required flag --${name}`);
  return value;
}
 
async function main(): Promise<void> {
  const { command, flags, booleans } = parseRiksbankArgs(process.argv.slice(2));
  if (command === 'help') {
    process.stdout.write(HELP);
    return;
  }
  const kind =
    command === 'fetch' ? parseRiksbankKind(requireRiksbankFlag(flags, 'kind')) : command;
  const urlFlag = flags.get('url')?.trim();
  const url = urlFlag && urlFlag.length > 0 ? urlFlag : DEFAULT_URLS[kind];
  await runKind(kind, url, booleans);
}
 
function isDirectExecution(): boolean {
  const entry = process.argv[1];
  Iif (!entry) return false;
  return import.meta.url === pathToFileURL(path.resolve(entry)).href;
}
 
Iif (isDirectExecution()) {
  main().catch((error: unknown) => {
    const message = error instanceof Error ? error.message : String(error);
    process.stderr.write(`riksbank-fetch: ${message}\n`);
    process.exit(/^(missing|unknown|unexpected|invalid|Riksbank fetch URL)/i.test(message) ? 2 : 1);
  });
}