All files / src/utils weeklyDataCache.ts

20% Statements 10/50
30% Branches 6/20
16.66% Functions 2/12
22.22% Lines 10/45

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                      6x                       6x           6x           6x           6x                 6x                               6x         3x 3x           3x                                                                                                                                                            
import { readFile } from 'node:fs/promises';
import { createHash } from 'node:crypto';
import { fileURLToPath } from 'node:url';
import path from 'node:path';
import { z } from 'zod';
import {
  CommitteeSchema,
  MEPDetailsSchema,
  MEPSchema,
} from '../schemas/europeanParliament.js';
 
export const CacheMetadataSchema = z.object({
  schemaVersion: z.number().int().min(1),
  generatedAt: z.string(),
  weekKey: z.string().optional(),
  source: z.string(),
  dataset: z.enum(['meps', 'corporate-bodies', 'controlled-vocabularies']).optional(),
  scope: z.enum(['current', 'all']).optional(),
  complete: z.boolean().optional(),
  recordCount: z.number().int().min(0).optional(),
  detailCount: z.number().int().min(0).optional(),
});
 
export const WeeklyMEPCacheSchema = z.object({
  metadata: CacheMetadataSchema,
  meps: z.array(MEPSchema),
  mepDetails: z.record(z.string(), MEPDetailsSchema),
});
 
export const WeeklyCorporateBodiesCacheSchema = z.object({
  metadata: CacheMetadataSchema,
  corporateBodies: z.array(CommitteeSchema),
  corporateBodyDetails: z.record(z.string(), CommitteeSchema).optional(),
});
 
export const WeeklyVocabulariesCacheSchema = z.object({
  metadata: CacheMetadataSchema,
  vocabularies: z.array(z.record(z.string(), z.unknown())),
  vocabularyDetails: z.record(z.string(), z.record(z.string(), z.unknown())).optional(),
});
 
const CacheManifestEntrySchema = z.object({
  file: z.string(),
  generatedAt: z.string(),
  scope: z.enum(['current', 'all']),
  recordCount: z.number().int().min(0),
  detailCount: z.number().int().min(0),
  sha256: z.string().regex(/^[a-f0-9]{64}$/u),
});
 
export const CacheManifestSchema = z.object({
  schemaVersion: z.literal(1),
  generatedAt: z.string(),
  source: z.string(),
  datasets: z.object({
    meps: CacheManifestEntrySchema,
    'corporate-bodies': CacheManifestEntrySchema,
    'controlled-vocabularies': CacheManifestEntrySchema,
  }),
});
 
export type WeeklyMEPCache = z.infer<typeof WeeklyMEPCacheSchema>;
export type WeeklyCorporateBodiesCache = z.infer<typeof WeeklyCorporateBodiesCacheSchema>;
export type WeeklyVocabulariesCache = z.infer<typeof WeeklyVocabulariesCacheSchema>;
export type CacheManifest = z.infer<typeof CacheManifestSchema>;
 
const validatedFileCache = new Map<string, Promise<unknown>>();
 
type WeeklyDataset = 'meps' | 'corporate-bodies' | 'controlled-vocabularies';
 
export function getCacheRoot(): string {
  const configuredRoot = process.env['EP_CACHE_DIR'] ?? process.env['EP_WEEKLY_CACHE_DIR'];
  return configuredRoot === undefined || configuredRoot.trim() === ''
    ? fileURLToPath(new URL('../../data/cache/', import.meta.url))
    : path.resolve(configuredRoot);
}
 
export function getWeeklyCachePath(dataset: WeeklyDataset): string {
  return path.join(getCacheRoot(), `${dataset}.json`);
}
 
export function getCacheManifestPath(): string {
  return path.join(getCacheRoot(), 'manifest.json');
}
 
function sha256(raw: Buffer): string {
  return createHash('sha256').update(raw).digest('hex');
}
 
export async function loadCacheManifest(): Promise<CacheManifest | null> {
  return loadAndValidateFile(getCacheManifestPath(), CacheManifestSchema);
}
 
async function loadAndValidateFile<T>(filePath: string, schema: z.ZodType<T>): Promise<T | null> {
  const existing = validatedFileCache.get(filePath);
  if (existing !== undefined) return await existing as T | null;
 
  const loading = (async (): Promise<T | null> => {
    try {
      const raw = await readFile(filePath, 'utf-8');
      const parsed: unknown = JSON.parse(raw);
      const validated = schema.safeParse(parsed);
      return validated.success ? validated.data : null;
    } catch {
      return null;
    }
  })();
  validatedFileCache.set(filePath, loading);
  return loading;
}
 
async function loadAndValidate<T extends { metadata: { generatedAt: string } }>(
  dataset: WeeklyDataset,
  schema: z.ZodType<T>,
): Promise<T | null> {
  const filePath = getWeeklyCachePath(dataset);
  const cacheKey = `${filePath}#verified`;
  const existing = validatedFileCache.get(cacheKey);
  if (existing !== undefined) return await existing as T | null;
 
  const loading = (async (): Promise<T | null> => {
    try {
      const manifest = await loadCacheManifest();
      const entry = manifest?.datasets[dataset];
      if (entry?.file !== path.basename(filePath)) return null;
      const raw = await readFile(filePath);
      if (sha256(raw) !== entry.sha256) return null;
      const parsed: unknown = JSON.parse(raw.toString('utf-8'));
      const validated = schema.safeParse(parsed);
      if (!validated.success) return null;
      return validated.data.metadata.generatedAt === entry.generatedAt ? validated.data : null;
    } catch {
      return null;
    }
  })();
  validatedFileCache.set(cacheKey, loading);
  return loading;
}
 
export async function loadWeeklyMEPCache(): Promise<WeeklyMEPCache | null> {
  return loadAndValidate('meps', WeeklyMEPCacheSchema);
}
 
export async function loadWeeklyCorporateBodiesCache(): Promise<WeeklyCorporateBodiesCache | null> {
  return loadAndValidate(
    'corporate-bodies',
    WeeklyCorporateBodiesCacheSchema,
  );
}
 
export async function loadWeeklyVocabulariesCache(): Promise<WeeklyVocabulariesCache | null> {
  return loadAndValidate(
    'controlled-vocabularies',
    WeeklyVocabulariesCacheSchema,
  );
}