formatting pass

This commit is contained in:
Luke Hagar
2025-08-13 14:24:04 -05:00
parent b9ea464077
commit ad269409e6
4 changed files with 78 additions and 84 deletions

View File

@@ -155,18 +155,13 @@ export async function startServers({
const persisted = await storage().getAllEndpoints(); const persisted = await storage().getAllEndpoints();
for (const ep of persisted) { for (const ep of persisted) {
try { try {
openApiStore.recordEndpoint( openApiStore.recordEndpoint(ep.path, ep.method.toLowerCase(), ep.data.request, {
ep.path, status: ep.data.response?.status || 200,
ep.method.toLowerCase(), headers: ep.data.response?.headers || {},
ep.data.request, contentType: ep.data.response?.contentType || 'application/json',
{ body: '[Raw data stored]',
status: ep.data.response?.status || 200, rawData: Buffer.alloc(0),
headers: ep.data.response?.headers || {}, });
contentType: ep.data.response?.contentType || 'application/json',
body: '[Raw data stored]',
rawData: Buffer.alloc(0),
}
);
} catch {} } catch {}
} }
} catch (e) { } catch (e) {
@@ -401,15 +396,17 @@ export async function startServers({
// Add the HAR entry to the store and persist if enabled // Add the HAR entry to the store and persist if enabled
harStore.addEntry(harEntry); harStore.addEntry(harEntry);
if (dbPath) { if (dbPath) {
storage().saveHarEntry({ storage()
startedDateTime: harEntry.startedDateTime, .saveHarEntry({
time: harEntry.time, startedDateTime: harEntry.startedDateTime,
request: harEntry.request, time: harEntry.time,
response: { request: harEntry.request,
...harEntry.response, response: {
// Do not persist raw buffer reference ...harEntry.response,
}, // Do not persist raw buffer reference
}).catch(() => {}); },
})
.catch(() => {});
} }
// Extract security schemes from headers - minimal work // Extract security schemes from headers - minimal work
@@ -458,22 +455,24 @@ export async function startServers({
// Persist endpoint minimal info for reconstruction // Persist endpoint minimal info for reconstruction
if (dbPath) { if (dbPath) {
storage().upsertEndpoint(path, method.toLowerCase(), { storage()
path, .upsertEndpoint(path, method.toLowerCase(), {
method: method.toLowerCase(), path,
request: { method: method.toLowerCase(),
query: queryParams, request: {
headers: requestHeaders, query: queryParams,
contentType: requestHeaders['content-type'] || 'application/json', headers: requestHeaders,
body: requestBody, contentType: requestHeaders['content-type'] || 'application/json',
security: securitySchemes, body: requestBody,
}, security: securitySchemes,
response: { },
status: proxyRes.statusCode || 500, response: {
headers: responseHeaders, status: proxyRes.statusCode || 500,
contentType: responseHeaders['content-type'] || 'application/json', headers: responseHeaders,
}, contentType: responseHeaders['content-type'] || 'application/json',
}).catch(() => {}); },
})
.catch(() => {});
} }
if (verbose) { if (verbose) {
@@ -514,17 +513,12 @@ export async function startServers({
tempStore.setTargetUrl(target); tempStore.setTargetUrl(target);
for (const ep of persisted) { for (const ep of persisted) {
try { try {
tempStore.recordEndpoint( tempStore.recordEndpoint(ep.path, ep.method.toLowerCase(), ep.data.request, {
ep.path, status: ep.data.response?.status || 200,
ep.method.toLowerCase(), headers: ep.data.response?.headers || {},
ep.data.request, contentType: ep.data.response?.contentType || 'application/json',
{ body: '[Raw data stored]',
status: ep.data.response?.status || 200, });
headers: ep.data.response?.headers || {},
contentType: ep.data.response?.contentType || 'application/json',
body: '[Raw data stored]'
}
);
} catch {} } catch {}
} }
res.send(JSON.stringify(tempStore.getOpenAPISpec())); res.send(JSON.stringify(tempStore.getOpenAPISpec()));
@@ -543,17 +537,12 @@ export async function startServers({
tempStore.setTargetUrl(target); tempStore.setTargetUrl(target);
for (const ep of persisted) { for (const ep of persisted) {
try { try {
tempStore.recordEndpoint( tempStore.recordEndpoint(ep.path, ep.method.toLowerCase(), ep.data.request, {
ep.path, status: ep.data.response?.status || 200,
ep.method.toLowerCase(), headers: ep.data.response?.headers || {},
ep.data.request, contentType: ep.data.response?.contentType || 'application/json',
{ body: '[Raw data stored]',
status: ep.data.response?.status || 200, });
headers: ep.data.response?.headers || {},
contentType: ep.data.response?.contentType || 'application/json',
body: '[Raw data stored]'
}
);
} catch {} } catch {}
} }
res.send(tempStore.getOpenAPISpecAsYAML()); res.send(tempStore.getOpenAPISpecAsYAML());

View File

@@ -11,5 +11,3 @@ export async function initStorage(dbPath: string): Promise<StorageAdapter> {
export function storage(): StorageAdapter { export function storage(): StorageAdapter {
return storageInstance; return storageInstance;
} }

View File

@@ -75,28 +75,37 @@ export class SQLiteStorage implements StorageAdapter {
} }
async getHarLog(): Promise<any> { async getHarLog(): Promise<any> {
const empty = { log: { version: '1.2', creator: { name: 'Arbiter', version: '1.0.0' }, entries: [] as any[] } }; const empty = {
log: { version: '1.2', creator: { name: 'Arbiter', version: '1.0.0' }, entries: [] as any[] },
};
if (!this.db) return empty; if (!this.db) return empty;
try { try {
const rows: Array<{ startedDateTime: string; time: number; request: string; response: string }> = this.db const rows: Array<{
startedDateTime: string;
time: number;
request: string;
response: string;
}> = this.db
.prepare('SELECT startedDateTime, time, request, response FROM har_entries ORDER BY id ASC') .prepare('SELECT startedDateTime, time, request, response FROM har_entries ORDER BY id ASC')
.all(); .all();
const entries = rows.map((r: { startedDateTime: string; time: number; request: string; response: string }) => { const entries = rows.map(
let req: any = {}; (r: { startedDateTime: string; time: number; request: string; response: string }) => {
let res: any = {}; let req: any = {};
try { let res: any = {};
req = JSON.parse(r.request); try {
} catch {} req = JSON.parse(r.request);
try { } catch {}
res = JSON.parse(r.response); try {
} catch {} res = JSON.parse(r.response);
return { } catch {}
startedDateTime: r.startedDateTime, return {
time: r.time, startedDateTime: r.startedDateTime,
request: req, time: r.time,
response: res, request: req,
}; response: res,
}); };
}
);
return { log: { ...empty.log, entries } }; return { log: { ...empty.log, entries } };
} catch { } catch {
return empty; return empty;
@@ -124,7 +133,9 @@ export class SQLiteStorage implements StorageAdapter {
async getAllEndpoints(): Promise<Array<{ path: string; method: string; data: any }>> { async getAllEndpoints(): Promise<Array<{ path: string; method: string; data: any }>> {
if (!this.db) return []; if (!this.db) return [];
try { try {
const rows: Array<{ path: string; method: string; data: string }> = this.db.prepare('SELECT path, method, data FROM endpoints').all(); const rows: Array<{ path: string; method: string; data: string }> = this.db
.prepare('SELECT path, method, data FROM endpoints')
.all();
return rows.map((r: { path: string; method: string; data: string }) => { return rows.map((r: { path: string; method: string; data: string }) => {
let data: any = {}; let data: any = {};
try { try {
@@ -139,5 +150,3 @@ export class SQLiteStorage implements StorageAdapter {
} }
export const sqliteStorage = new SQLiteStorage(); export const sqliteStorage = new SQLiteStorage();

View File

@@ -12,5 +12,3 @@ export interface StorageAdapter {
upsertEndpoint(path: string, method: string, data: any): Promise<void>; upsertEndpoint(path: string, method: string, data: any): Promise<void>;
getAllEndpoints(): Promise<Array<{ path: string; method: string; data: any }>>; getAllEndpoints(): Promise<Array<{ path: string; method: string; data: any }>>;
} }