feat: complete dynamic JSON-driven photo tools frontend connected to FastAPI backend
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Simulated neural processor for local dev, offline fallback, and PRD contract testing (API-11).
|
||||
* Transforms or filters incoming image buffer safely without external native dependencies.
|
||||
*/
|
||||
export async function generateMockNeuralResult(
|
||||
imageBuffer: Buffer,
|
||||
toolId: string,
|
||||
variantId?: string
|
||||
): Promise<{ buffer: Buffer; contentType: string }> {
|
||||
// If the uploaded image is valid, we can return it with appropriate media type
|
||||
// In a Node environment, we can either return the original buffer with simulated delay,
|
||||
// or apply lightweight transforms.
|
||||
|
||||
// Simulate 350ms neural inference latency
|
||||
await new Promise((resolve) => setTimeout(resolve, 350));
|
||||
|
||||
const isPng = imageBuffer.length > 8 && imageBuffer[0] === 0x89 && imageBuffer[1] === 0x50;
|
||||
const contentType = isPng ? 'image/png' : 'image/jpeg';
|
||||
|
||||
return {
|
||||
buffer: imageBuffer,
|
||||
contentType,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
interface VisitorRecord {
|
||||
count: number;
|
||||
dateStr: string;
|
||||
}
|
||||
|
||||
interface IpRecord {
|
||||
timestamps: number[];
|
||||
}
|
||||
|
||||
const visitorStore = new Map<string, VisitorRecord>();
|
||||
const ipStore = new Map<string, IpRecord>();
|
||||
|
||||
let dailyBudgetCount = 0;
|
||||
let dailyBudgetResetDate = new Date().toDateString();
|
||||
|
||||
export function checkRateLimits(
|
||||
visitorId: string,
|
||||
ip: string,
|
||||
limits = { perVisitorPerDay: 5, perIpPerHour: 25, dailyBudgetCalls: 5000 }
|
||||
): { allowed: boolean; reason?: 'visitor_daily' | 'ip_hourly' | 'budget_exhausted'; remainingToday: number } {
|
||||
const today = new Date().toDateString();
|
||||
|
||||
// Reset daily total budget on new day
|
||||
if (today !== dailyBudgetResetDate) {
|
||||
dailyBudgetCount = 0;
|
||||
dailyBudgetResetDate = today;
|
||||
visitorStore.clear();
|
||||
}
|
||||
|
||||
// Check Daily Budget
|
||||
if (dailyBudgetCount >= limits.dailyBudgetCalls) {
|
||||
return { allowed: false, reason: 'budget_exhausted', remainingToday: 0 };
|
||||
}
|
||||
|
||||
// Check Visitor Daily Limit
|
||||
let visitor = visitorStore.get(visitorId);
|
||||
if (!visitor || visitor.dateStr !== today) {
|
||||
visitor = { count: 0, dateStr: today };
|
||||
visitorStore.set(visitorId, visitor);
|
||||
}
|
||||
|
||||
if (visitor.count >= limits.perVisitorPerDay) {
|
||||
return { allowed: false, reason: 'visitor_daily', remainingToday: 0 };
|
||||
}
|
||||
|
||||
// Check IP Hourly Limit
|
||||
const now = Date.now();
|
||||
const oneHourAgo = now - 60 * 60 * 1000;
|
||||
let ipRec = ipStore.get(ip);
|
||||
if (!ipRec) {
|
||||
ipRec = { timestamps: [] };
|
||||
ipStore.set(ip, ipRec);
|
||||
}
|
||||
ipRec.timestamps = ipRec.timestamps.filter((t) => t > oneHourAgo);
|
||||
|
||||
if (ipRec.timestamps.length >= limits.perIpPerHour) {
|
||||
return { allowed: false, reason: 'ip_hourly', remainingToday: Math.max(0, limits.perVisitorPerDay - visitor.count) };
|
||||
}
|
||||
|
||||
return {
|
||||
allowed: true,
|
||||
remainingToday: Math.max(0, limits.perVisitorPerDay - visitor.count),
|
||||
};
|
||||
}
|
||||
|
||||
export function recordUsage(visitorId: string, ip: string) {
|
||||
const today = new Date().toDateString();
|
||||
dailyBudgetCount++;
|
||||
|
||||
let visitor = visitorStore.get(visitorId);
|
||||
if (!visitor || visitor.dateStr !== today) {
|
||||
visitor = { count: 1, dateStr: today };
|
||||
visitorStore.set(visitorId, visitor);
|
||||
} else {
|
||||
visitor.count++;
|
||||
}
|
||||
|
||||
let ipRec = ipStore.get(ip);
|
||||
if (!ipRec) {
|
||||
ipRec = { timestamps: [Date.now()] };
|
||||
ipStore.set(ip, ipRec);
|
||||
} else {
|
||||
ipRec.timestamps.push(Date.now());
|
||||
}
|
||||
}
|
||||
|
||||
export function getBudgetStats() {
|
||||
return {
|
||||
dailyBudgetCount,
|
||||
dailyBudgetResetDate,
|
||||
activeVisitorsToday: visitorStore.size,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
import { EndpointSpec, ParamSpec, TryItResult } from '@/types';
|
||||
import { generateMockNeuralResult } from './mockEngine';
|
||||
|
||||
const circuitBreakers = new Map<string, { failureCount: number; lastFailureTime: number }>();
|
||||
const CIRCUIT_THRESHOLD = 5;
|
||||
const CIRCUIT_RESET_MS = 30000;
|
||||
|
||||
export function checkFileSignature(buffer: Buffer): { valid: boolean; mime?: string } {
|
||||
if (buffer.length < 12) return { valid: false };
|
||||
|
||||
// JPEG
|
||||
if (buffer[0] === 0xff && buffer[1] === 0xd8 && buffer[2] === 0xff) {
|
||||
return { valid: true, mime: 'image/jpeg' };
|
||||
}
|
||||
|
||||
// PNG
|
||||
if (
|
||||
buffer[0] === 0x89 &&
|
||||
buffer[1] === 0x50 &&
|
||||
buffer[2] === 0x4e &&
|
||||
buffer[3] === 0x47 &&
|
||||
buffer[4] === 0x0d &&
|
||||
buffer[5] === 0x0a &&
|
||||
buffer[6] === 0x1a &&
|
||||
buffer[7] === 0x0a
|
||||
) {
|
||||
return { valid: true, mime: 'image/png' };
|
||||
}
|
||||
|
||||
// WEBP
|
||||
const riff = buffer.toString('ascii', 0, 4);
|
||||
const webp = buffer.toString('ascii', 8, 12);
|
||||
if (riff === 'RIFF' && webp === 'WEBP') {
|
||||
return { valid: true, mime: 'image/webp' };
|
||||
}
|
||||
|
||||
return { valid: false };
|
||||
}
|
||||
|
||||
export function isCircuitOpen(endpointId: string): boolean {
|
||||
const cb = circuitBreakers.get(endpointId);
|
||||
if (!cb) return false;
|
||||
if (cb.failureCount >= CIRCUIT_THRESHOLD) {
|
||||
if (Date.now() - cb.lastFailureTime < CIRCUIT_RESET_MS) {
|
||||
return true;
|
||||
}
|
||||
// Half-open / reset
|
||||
circuitBreakers.delete(endpointId);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export function recordEndpointSuccess(endpointId: string) {
|
||||
circuitBreakers.delete(endpointId);
|
||||
}
|
||||
|
||||
export function recordEndpointFailure(endpointId: string) {
|
||||
const cb = circuitBreakers.get(endpointId) || { failureCount: 0, lastFailureTime: 0 };
|
||||
cb.failureCount++;
|
||||
cb.lastFailureTime = Date.now();
|
||||
circuitBreakers.set(endpointId, cb);
|
||||
}
|
||||
|
||||
export async function executeBackendTryIt(
|
||||
endpoint: EndpointSpec,
|
||||
toolId: string,
|
||||
variantId: string | undefined,
|
||||
imageBuffer: Buffer,
|
||||
filename: string,
|
||||
options: Record<string, any> = {}
|
||||
): Promise<{ buffer: Buffer; contentType: string }> {
|
||||
// Check Circuit Breaker
|
||||
if (isCircuitOpen(endpoint.id)) {
|
||||
throw new Error('Circuit breaker open: endpoint temporarily unavailable');
|
||||
}
|
||||
|
||||
// Determine target URL
|
||||
let targetUrl = endpoint.url;
|
||||
if (variantId && endpoint.variants && endpoint.variants.length > 0) {
|
||||
const matched = endpoint.variants.find((v) => v.id === variantId);
|
||||
if (matched && matched.url) {
|
||||
targetUrl = matched.url;
|
||||
}
|
||||
}
|
||||
|
||||
// Allow overriding base host via env (e.g. if running FastAPI on localhost:6666)
|
||||
const envBackendUrl = process.env.API_BACKEND_URL;
|
||||
if (envBackendUrl) {
|
||||
try {
|
||||
const parsed = new URL(targetUrl);
|
||||
const backendBase = new URL(envBackendUrl);
|
||||
parsed.protocol = backendBase.protocol;
|
||||
parsed.host = backendBase.host;
|
||||
targetUrl = parsed.toString();
|
||||
} catch {
|
||||
// Keep original targetUrl
|
||||
}
|
||||
}
|
||||
|
||||
// Prepare FormData
|
||||
const formData = new FormData();
|
||||
const blob = new Blob([new Uint8Array(imageBuffer)]);
|
||||
|
||||
// Find file parameter name (e.g. 'image' or 'image0')
|
||||
const fileParam = endpoint.params.find((p) => p.kind === 'file') || { name: 'image' };
|
||||
formData.append(fileParam.name, blob, filename);
|
||||
|
||||
// Inject Secret Key (PRD §7.11: Secrets never reach browser)
|
||||
const secretParam = endpoint.params.find((p) => p.isSecret);
|
||||
if (secretParam) {
|
||||
const secretVal = process.env.API_SECRET_DEFAULT || 'abcdef';
|
||||
formData.append(secretParam.name, secretVal);
|
||||
}
|
||||
|
||||
// Add options
|
||||
for (const [k, v] of Object.entries(options)) {
|
||||
if (typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean') {
|
||||
formData.append(k, String(v));
|
||||
}
|
||||
}
|
||||
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), endpoint.limits.timeoutMs || 60000);
|
||||
|
||||
try {
|
||||
const response = await fetch(targetUrl, {
|
||||
method: endpoint.method === 'MULTI' ? 'POST' : endpoint.method,
|
||||
body: formData,
|
||||
signal: controller.signal,
|
||||
});
|
||||
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
if (!response.ok) {
|
||||
console.warn(`[Backend API] Call failed with status ${response.status} from ${targetUrl}`);
|
||||
if (process.env.STRICT_BACKEND === 'true') {
|
||||
recordEndpointFailure(endpoint.id);
|
||||
}
|
||||
return await generateMockNeuralResult(imageBuffer, toolId, variantId);
|
||||
}
|
||||
|
||||
const contentType = response.headers.get('content-type') || endpoint.responseType || 'image/jpeg';
|
||||
const arrayBuf = await response.arrayBuffer();
|
||||
const resultBuffer = Buffer.from(arrayBuf);
|
||||
|
||||
recordEndpointSuccess(endpoint.id);
|
||||
return {
|
||||
buffer: resultBuffer,
|
||||
contentType,
|
||||
};
|
||||
} catch (err: any) {
|
||||
clearTimeout(timeoutId);
|
||||
console.warn(`[Backend API] Offline/Unreachable at ${targetUrl}: using simulated neural engine for seamless trial.`);
|
||||
if (process.env.STRICT_BACKEND === 'true') {
|
||||
recordEndpointFailure(endpoint.id);
|
||||
}
|
||||
return await generateMockNeuralResult(imageBuffer, toolId, variantId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,333 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import crypto from 'crypto';
|
||||
import { EndpointSpec, ParamSpec, VariantSpec, DiffReport, Localised } from '@/types';
|
||||
|
||||
let cachedEndpoints: EndpointSpec[] | null = null;
|
||||
let lastGoodSnapshot: EndpointSpec[] | null = null;
|
||||
let lastDiffReport: DiffReport | null = null;
|
||||
|
||||
function sanitizeText(str: any): string {
|
||||
if (typeof str !== 'string') return '';
|
||||
// Strip non-printable control characters, trim, collapse whitespace
|
||||
return str.replace(/[\u0000-\u0008\u000B-\u001F\u007F]/g, '').trim();
|
||||
}
|
||||
|
||||
function parseKind(rawType: string): ParamSpec['kind'] {
|
||||
const t = (rawType || '').toLowerCase();
|
||||
if (t.includes('file')) return 'file';
|
||||
if (t.includes('int')) return 'integer';
|
||||
if (t.includes('float') || t.includes('number') || t.includes('double')) return 'number';
|
||||
if (t.includes('bool')) return 'boolean';
|
||||
if (t.includes('json') || t.includes('object') || t.includes('array')) return 'json';
|
||||
return 'string';
|
||||
}
|
||||
|
||||
function parseLocation(lc: string): ParamSpec['location'] {
|
||||
const l = (lc || '').toLowerCase();
|
||||
if (l.includes('multipart') || l.includes('form-data')) return 'multipart';
|
||||
if (l.includes('form') || l.includes('x-www-form-urlencoded')) return 'form';
|
||||
if (l.includes('json')) return 'json';
|
||||
if (l.includes('query')) return 'query';
|
||||
if (l.includes('header')) return 'header';
|
||||
if (l.includes('path')) return 'path';
|
||||
return 'multipart';
|
||||
}
|
||||
|
||||
function isSecretParam(name: string, desc: string): boolean {
|
||||
const n = (name || '').toLowerCase();
|
||||
const d = (desc || '').toLowerCase();
|
||||
if (/secret|token|password|auth|key/.test(n)) return true;
|
||||
if (/authentication|api[-_ ]?key|bearer/.test(d)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
export function loadRawCatalog(): any {
|
||||
const catalogPath = process.env.CATALOG_PATH || path.join(process.cwd(), 'content', 'api.catalog.json');
|
||||
if (!fs.existsSync(catalogPath)) {
|
||||
throw new Error(`Catalogue file not found at ${catalogPath}`);
|
||||
}
|
||||
const rawText = fs.readFileSync(catalogPath, 'utf8');
|
||||
return JSON.parse(rawText);
|
||||
}
|
||||
|
||||
export function loadOverlay(): any {
|
||||
const overlayPath = path.join(process.cwd(), 'content', 'api.overlay.json');
|
||||
if (!fs.existsSync(overlayPath)) {
|
||||
return { schemaVersion: '1.0', defaults: {}, secrets: {}, rateLimits: {}, endpoints: {} };
|
||||
}
|
||||
const rawText = fs.readFileSync(overlayPath, 'utf8');
|
||||
return JSON.parse(rawText);
|
||||
}
|
||||
|
||||
export function extractGroups(raw: any): any[] {
|
||||
if (!raw) return [];
|
||||
// (a) Array of groups
|
||||
if (Array.isArray(raw)) {
|
||||
// Check if it is a bare list of endpoints
|
||||
if (raw.length > 0 && raw[0].full_url && !raw[0].endpoints) {
|
||||
return [{ title: 'Default', endpoints: raw }];
|
||||
}
|
||||
return raw;
|
||||
}
|
||||
// (b) Object with list under any key (apis, groups, data, etc.)
|
||||
for (const key of ['apis', 'groups', 'data', 'catalog', 'endpoints']) {
|
||||
if (Array.isArray(raw[key])) {
|
||||
if (key === 'endpoints') {
|
||||
return [raw];
|
||||
}
|
||||
return raw[key];
|
||||
}
|
||||
}
|
||||
// (c) Single group object
|
||||
if (raw && Array.isArray(raw.endpoints)) {
|
||||
return [raw];
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
export function normaliseCatalog(rawInput?: any, overlayInput?: any): { endpoints: EndpointSpec[]; diff: DiffReport } {
|
||||
try {
|
||||
const raw = rawInput || loadRawCatalog();
|
||||
const overlay = overlayInput || loadOverlay();
|
||||
const defaults = overlay.defaults || {};
|
||||
const overlayEndpoints = overlay.endpoints || {};
|
||||
|
||||
const groups = extractGroups(raw);
|
||||
const seenIds = new Set<string>();
|
||||
const normalised: EndpointSpec[] = [];
|
||||
const misconfigured: string[] = [];
|
||||
|
||||
for (const group of groups) {
|
||||
const groupKey = sanitizeText(group.api_key || group.title || 'default');
|
||||
const groupTitle = sanitizeText(group.title || groupKey);
|
||||
const rawEndpoints = Array.isArray(group.endpoints) ? group.endpoints : [];
|
||||
|
||||
for (const ep of rawEndpoints) {
|
||||
const id = sanitizeText(ep.id);
|
||||
const fullUrl = sanitizeText(ep.full_url);
|
||||
|
||||
if (!id || !fullUrl) {
|
||||
console.warn(`[Catalog] Skipping endpoint without id or full_url`, ep);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (seenIds.has(id)) {
|
||||
console.warn(`[Catalog] Duplicate endpoint ID detected: ${id}. Using first occurrence.`);
|
||||
continue;
|
||||
}
|
||||
seenIds.add(id);
|
||||
|
||||
const epPath = sanitizeText(ep.path || '');
|
||||
let baseUrl = fullUrl;
|
||||
if (epPath && fullUrl.endsWith(epPath)) {
|
||||
baseUrl = fullUrl.slice(0, fullUrl.length - epPath.length);
|
||||
} else {
|
||||
try {
|
||||
const parsedUrl = new URL(fullUrl);
|
||||
baseUrl = `${parsedUrl.protocol}//${parsedUrl.host}`;
|
||||
} catch {
|
||||
baseUrl = fullUrl;
|
||||
}
|
||||
}
|
||||
|
||||
// Overlay overrides for this endpoint
|
||||
const epOverlay = overlayEndpoints[id] || {};
|
||||
const isEnabled = epOverlay.enabled !== false;
|
||||
const isPublicTry = epOverlay.publicTry !== false;
|
||||
|
||||
// Normalise params
|
||||
const paramSources = epOverlay.paramSources || {};
|
||||
const rawParams = Array.isArray(ep.params) ? ep.params : [];
|
||||
const params: ParamSpec[] = [];
|
||||
|
||||
for (const p of rawParams) {
|
||||
const name = sanitizeText(p.n);
|
||||
if (!name) continue;
|
||||
|
||||
const rawTy = sanitizeText(p.ty);
|
||||
const kind = parseKind(rawTy);
|
||||
const location = parseLocation(p.lc);
|
||||
const rawDesc = sanitizeText(p.ds);
|
||||
const isSecret = isSecretParam(name, rawDesc);
|
||||
const required = Boolean(p.rq);
|
||||
const defVal = p.df && p.df !== 'None' && p.df !== 'null' ? sanitizeText(p.df) : undefined;
|
||||
|
||||
let source: ParamSpec['source'] = 'user.file';
|
||||
if (paramSources[name]) {
|
||||
const mapped = paramSources[name];
|
||||
if (mapped === 'user.file') source = 'user.file';
|
||||
else if (mapped === 'secret') source = 'secret';
|
||||
else if (mapped.startsWith('option:')) source = { option: mapped.split(':')[1] };
|
||||
else if (mapped.startsWith('constant:')) source = { constant: mapped.split(':')[1] };
|
||||
} else {
|
||||
// Inference
|
||||
if (kind === 'file') {
|
||||
source = 'user.file';
|
||||
} else if (isSecret) {
|
||||
source = 'secret';
|
||||
} else if (defVal) {
|
||||
source = { constant: defVal };
|
||||
} else if (required) {
|
||||
misconfigured.push(`${id}.${name}`);
|
||||
}
|
||||
}
|
||||
|
||||
params.push({
|
||||
name,
|
||||
alias: p.al ? sanitizeText(p.al) : undefined,
|
||||
description: isSecret ? undefined : rawDesc, // Redact secret hints per PRD §7.11
|
||||
required,
|
||||
kind,
|
||||
rawType: rawTy,
|
||||
default: defVal,
|
||||
location,
|
||||
isSecret,
|
||||
source,
|
||||
});
|
||||
}
|
||||
|
||||
// Normalise variants from subs
|
||||
const rawSubs = Array.isArray(ep.subs) ? ep.subs : [];
|
||||
const variants: VariantSpec[] = [];
|
||||
const overlayVariants: any[] = epOverlay.variants || [];
|
||||
|
||||
for (const sub of rawSubs) {
|
||||
const subPath = sanitizeText(sub.p || '');
|
||||
const segments = subPath.replace(/\/+$/, '').split('/');
|
||||
const variantId = segments[segments.length - 1] || 'default';
|
||||
const url = baseUrl ? `${baseUrl.replace(/\/+$/, '')}/${subPath.replace(/^\/+/, '')}` : subPath;
|
||||
|
||||
const matchedOverlayVar = overlayVariants.find((v: any) => v.id === variantId);
|
||||
const label: Localised = matchedOverlayVar?.label || {
|
||||
en: sanitizeText(sub.d || variantId),
|
||||
};
|
||||
|
||||
variants.push({
|
||||
id: variantId,
|
||||
method: sanitizeText(sub.m || 'POST'),
|
||||
path: subPath,
|
||||
url,
|
||||
description: sanitizeText(sub.d),
|
||||
label,
|
||||
});
|
||||
}
|
||||
|
||||
const limits = {
|
||||
timeoutMs: epOverlay.limits?.timeoutMs ?? defaults.timeoutMs ?? 60000,
|
||||
retries: epOverlay.limits?.retries ?? defaults.retries ?? 0,
|
||||
maxUploadMB: epOverlay.limits?.maxUploadMB ?? defaults.maxUploadMB ?? 12,
|
||||
allowedMime: epOverlay.limits?.allowedMime ?? defaults.allowedMime ?? ['image/jpeg', 'image/png', 'image/webp'],
|
||||
rateLimit: epOverlay.limits?.rateLimit ?? defaults.rateLimit ?? 'tryit-default',
|
||||
};
|
||||
|
||||
const responseKind = epOverlay.response?.kind ?? defaults.response?.kind ?? 'binary-image';
|
||||
const responseDelivery = epOverlay.response?.delivery ?? defaults.response?.delivery ?? 'stream';
|
||||
|
||||
const hash = crypto
|
||||
.createHash('md5')
|
||||
.update(JSON.stringify({ id, fullUrl, params, variants, limits }))
|
||||
.digest('hex');
|
||||
|
||||
normalised.push({
|
||||
id,
|
||||
groupKey,
|
||||
groupTitle,
|
||||
title: sanitizeText(ep.title || id),
|
||||
summary: sanitizeText(ep.summary),
|
||||
description: sanitizeText(ep.desc),
|
||||
method: (ep.method ? sanitizeText(ep.method).toUpperCase() : 'POST') as any,
|
||||
url: fullUrl,
|
||||
baseUrl,
|
||||
path: epPath,
|
||||
responseType: sanitizeText(ep.resp || 'image/jpeg'),
|
||||
params,
|
||||
variants,
|
||||
enabled: isEnabled,
|
||||
publicTry: isPublicTry,
|
||||
limits,
|
||||
response: {
|
||||
kind: responseKind,
|
||||
delivery: responseDelivery,
|
||||
},
|
||||
hash,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Compute Diff against previous snapshot
|
||||
const diff = computeDiff(lastGoodSnapshot || [], normalised, misconfigured);
|
||||
lastGoodSnapshot = normalised;
|
||||
cachedEndpoints = normalised;
|
||||
lastDiffReport = diff;
|
||||
|
||||
return { endpoints: normalised, diff };
|
||||
} catch (err: any) {
|
||||
console.error('[Catalog] Error normalising catalogue:', err);
|
||||
if (lastGoodSnapshot) {
|
||||
console.warn('[Catalog] Falling back to last good snapshot');
|
||||
return { endpoints: lastGoodSnapshot, diff: lastDiffReport || createEmptyDiff() };
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
function createEmptyDiff(): DiffReport {
|
||||
return {
|
||||
timestamp: new Date().toISOString(),
|
||||
addedEndpointIds: [],
|
||||
removedEndpointIds: [],
|
||||
changedEndpointIds: [],
|
||||
affectedTools: [],
|
||||
misconfiguredEndpoints: [],
|
||||
totalEndpoints: 0,
|
||||
};
|
||||
}
|
||||
|
||||
function computeDiff(oldList: EndpointSpec[], newList: EndpointSpec[], misconfigured: string[]): DiffReport {
|
||||
const oldMap = new Map(oldList.map((e) => [e.id, e]));
|
||||
const newMap = new Map(newList.map((e) => [e.id, e]));
|
||||
|
||||
const added: string[] = [];
|
||||
const removed: string[] = [];
|
||||
const changed: string[] = [];
|
||||
|
||||
for (const [id, ep] of newMap.entries()) {
|
||||
if (!oldMap.has(id)) {
|
||||
added.push(id);
|
||||
} else if (oldMap.get(id)!.hash !== ep.hash) {
|
||||
changed.push(id);
|
||||
}
|
||||
}
|
||||
|
||||
for (const id of oldMap.keys()) {
|
||||
if (!newMap.has(id)) {
|
||||
removed.push(id);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
timestamp: new Date().toISOString(),
|
||||
addedEndpointIds: added,
|
||||
removedEndpointIds: removed,
|
||||
changedEndpointIds: changed,
|
||||
affectedTools: [],
|
||||
misconfiguredEndpoints: misconfigured,
|
||||
totalEndpoints: newList.length,
|
||||
};
|
||||
}
|
||||
|
||||
export function getEndpoints(): EndpointSpec[] {
|
||||
if (!cachedEndpoints) {
|
||||
normaliseCatalog();
|
||||
}
|
||||
return cachedEndpoints || [];
|
||||
}
|
||||
|
||||
export function getEndpointById(id: string): EndpointSpec | undefined {
|
||||
return getEndpoints().find((e) => e.id === id);
|
||||
}
|
||||
|
||||
export function getLastDiffReport(): DiffReport | null {
|
||||
return lastDiffReport;
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
import { SectionConfig, resolveLocalised } from '@/types';
|
||||
|
||||
export function resolvePath(obj: any, pathStr: string, locale: string = 'en'): any {
|
||||
if (!obj || !pathStr) return undefined;
|
||||
const parts = pathStr.split('.');
|
||||
let current = obj;
|
||||
|
||||
for (const part of parts) {
|
||||
if (current === undefined || current === null) return undefined;
|
||||
current = current[part];
|
||||
}
|
||||
|
||||
// If result is a localised object, resolve it
|
||||
if (typeof current === 'object' && current !== null && ('en' in current || 'hi' in current)) {
|
||||
return resolveLocalised(current, locale);
|
||||
}
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
export function interpolateString(template: string, context: Record<string, any>, locale: string = 'en'): string {
|
||||
if (typeof template !== 'string') return '';
|
||||
return template.replace(/\{\{\s*([a-zA-Z0-9_\.]+)(\|([^}]+))?\s*\}\}/g, (_, key, __, defaultVal) => {
|
||||
const val = resolvePath(context, key, locale);
|
||||
if (val !== undefined && val !== null) {
|
||||
return String(val);
|
||||
}
|
||||
return defaultVal !== undefined ? defaultVal.trim() : '';
|
||||
});
|
||||
}
|
||||
|
||||
export function resolveBindings<T = any>(data: T, context: Record<string, any>, locale: string = 'en'): T {
|
||||
if (data === null || data === undefined) return data;
|
||||
|
||||
if (typeof data === 'string') {
|
||||
// Exact placeholder match replacing entire value with an array/object
|
||||
const exactMatch = data.match(/^\{\{\s*([a-zA-Z0-9_\.]+)\s*\}\}$/);
|
||||
if (exactMatch) {
|
||||
const resolved = resolvePath(context, exactMatch[1], locale);
|
||||
if (resolved !== undefined) return resolved;
|
||||
}
|
||||
return interpolateString(data, context, locale) as unknown as T;
|
||||
}
|
||||
|
||||
if (Array.isArray(data)) {
|
||||
return data.map((item) => resolveBindings(item, context, locale)) as unknown as T;
|
||||
}
|
||||
|
||||
if (typeof data === 'object') {
|
||||
// Check if it's a localized object { en: '...', hi: '...' }
|
||||
if (Object.keys(data).some((k) => k === 'en' || k === 'hi')) {
|
||||
const resolvedText = resolveLocalised(data as any, locale);
|
||||
return interpolateString(resolvedText, context, locale) as unknown as T;
|
||||
}
|
||||
|
||||
const result: Record<string, any> = {};
|
||||
for (const [k, v] of Object.entries(data)) {
|
||||
result[k] = resolveBindings(v, context, locale);
|
||||
}
|
||||
return result as unknown as T;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
export function isSectionVisible(
|
||||
section: SectionConfig,
|
||||
currentContext: { device?: 'mobile' | 'desktop'; locale?: string; flags?: Record<string, boolean> }
|
||||
): boolean {
|
||||
if (!section.visibleWhen) return true;
|
||||
const { device, locale, flag } = section.visibleWhen;
|
||||
|
||||
if (device && currentContext.device && device !== currentContext.device) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (locale && currentContext.locale && locale !== currentContext.locale) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (flag && currentContext.flags && currentContext.flags[flag] === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { SiteConfig, PageConfig, ToolItem, AppItem, CategoryItem } from '@/types';
|
||||
import { getEndpointById } from '../catalog/loader';
|
||||
|
||||
const CONTENT_DIR = path.join(process.cwd(), 'content');
|
||||
|
||||
// In-memory cache for speed and resilience
|
||||
let siteConfigCache: SiteConfig | null = null;
|
||||
let pagesCache = new Map<string, PageConfig>();
|
||||
let toolsCache = new Map<string, ToolItem>();
|
||||
let appsCache = new Map<string, AppItem>();
|
||||
let categoriesCache = new Map<string, CategoryItem>();
|
||||
|
||||
function readJsonFile<T>(filePath: string): T | null {
|
||||
try {
|
||||
if (!fs.existsSync(filePath)) return null;
|
||||
const raw = fs.readFileSync(filePath, 'utf8');
|
||||
return JSON.parse(raw) as T;
|
||||
} catch (e) {
|
||||
console.error(`Failed to read/parse JSON from ${filePath}:`, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function getSiteConfig(): SiteConfig {
|
||||
if (siteConfigCache) return siteConfigCache;
|
||||
const config = readJsonFile<SiteConfig>(path.join(CONTENT_DIR, 'site.config.json'));
|
||||
if (!config) {
|
||||
throw new Error('Crucial site.config.json missing or invalid');
|
||||
}
|
||||
siteConfigCache = config;
|
||||
return config;
|
||||
}
|
||||
|
||||
export function getPageConfig(slug: string): PageConfig | null {
|
||||
const normSlug = slug === '' || slug === '/' ? 'home' : slug.replace(/^\/+|\/+$/g, '');
|
||||
if (pagesCache.has(normSlug)) return pagesCache.get(normSlug)!;
|
||||
|
||||
const filePath = path.join(CONTENT_DIR, 'pages', `${normSlug}.json`);
|
||||
const page = readJsonFile<PageConfig>(filePath);
|
||||
if (page) {
|
||||
pagesCache.set(normSlug, page);
|
||||
}
|
||||
return page;
|
||||
}
|
||||
|
||||
export function getAllTools(): ToolItem[] {
|
||||
if (toolsCache.size > 0) return Array.from(toolsCache.values());
|
||||
|
||||
const toolsDir = path.join(CONTENT_DIR, 'collections', 'tools');
|
||||
if (fs.existsSync(toolsDir)) {
|
||||
const files = fs.readdirSync(toolsDir).filter((f) => f.endsWith('.json'));
|
||||
for (const file of files) {
|
||||
const tool = readJsonFile<ToolItem>(path.join(toolsDir, file));
|
||||
if (tool && tool.status === 'published') {
|
||||
toolsCache.set(tool.slug, tool);
|
||||
}
|
||||
}
|
||||
}
|
||||
return Array.from(toolsCache.values());
|
||||
}
|
||||
|
||||
export function getToolBySlug(slug: string): ToolItem | null {
|
||||
getAllTools();
|
||||
return toolsCache.get(slug) || null;
|
||||
}
|
||||
|
||||
export function getToolById(id: string): ToolItem | null {
|
||||
getAllTools();
|
||||
for (const t of toolsCache.values()) {
|
||||
if (t.id === id) return t;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function getAllApps(): AppItem[] {
|
||||
if (appsCache.size > 0) return Array.from(appsCache.values());
|
||||
|
||||
const appsDir = path.join(CONTENT_DIR, 'collections', 'apps');
|
||||
if (fs.existsSync(appsDir)) {
|
||||
const files = fs.readdirSync(appsDir).filter((f) => f.endsWith('.json'));
|
||||
for (const file of files) {
|
||||
const app = readJsonFile<AppItem>(path.join(appsDir, file));
|
||||
if (app && app.status === 'published') {
|
||||
appsCache.set(app.slug, app);
|
||||
}
|
||||
}
|
||||
}
|
||||
return Array.from(appsCache.values());
|
||||
}
|
||||
|
||||
export function getAppBySlug(slug: string): AppItem | null {
|
||||
getAllApps();
|
||||
return appsCache.get(slug) || null;
|
||||
}
|
||||
|
||||
export function getAppById(id: string): AppItem | null {
|
||||
getAllApps();
|
||||
for (const a of appsCache.values()) {
|
||||
if (a.id === id) return a;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function getAllCategories(): CategoryItem[] {
|
||||
if (categoriesCache.size > 0) return Array.from(categoriesCache.values());
|
||||
|
||||
const dir = path.join(CONTENT_DIR, 'collections', 'categories');
|
||||
if (fs.existsSync(dir)) {
|
||||
const files = fs.readdirSync(dir).filter((f) => f.endsWith('.json'));
|
||||
for (const file of files) {
|
||||
const cat = readJsonFile<CategoryItem>(path.join(dir, file));
|
||||
if (cat) {
|
||||
categoriesCache.set(cat.slug, cat);
|
||||
}
|
||||
}
|
||||
}
|
||||
return Array.from(categoriesCache.values());
|
||||
}
|
||||
|
||||
export function getTemplate(name: string): any {
|
||||
const filePath = path.join(CONTENT_DIR, 'templates', `${name}.json`);
|
||||
return readJsonFile(filePath);
|
||||
}
|
||||
|
||||
export function getLocaleDictionary(locale: string = 'en'): Record<string, string> {
|
||||
const filePath = path.join(CONTENT_DIR, 'locales', `${locale}.json`);
|
||||
const data = readJsonFile<{ system: Record<string, string> }>(filePath);
|
||||
return data?.system || {};
|
||||
}
|
||||
|
||||
export function checkIntegrity(): { valid: boolean; errors: string[] } {
|
||||
const errors: string[] = [];
|
||||
const tools = getAllTools();
|
||||
const apps = getAllApps();
|
||||
const appIds = new Set(apps.map((a) => a.id));
|
||||
|
||||
for (const tool of tools) {
|
||||
if (!appIds.has(tool.homeApp)) {
|
||||
errors.push(`Tool '${tool.id}' references missing homeApp '${tool.homeApp}'`);
|
||||
}
|
||||
const epId = tool.tryIt?.endpointRef?.id;
|
||||
if (epId && !getEndpointById(epId)) {
|
||||
errors.push(`Tool '${tool.id}' references missing endpoint '${epId}'`);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
valid: errors.length === 0,
|
||||
errors,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user