fix: OpenAPI spec accuracy — hide internal endpoints, mark signup/verify deprecated
All checks were successful
Build & Deploy to Staging / Build & Deploy to Staging (push) Successful in 13m9s
All checks were successful
Build & Deploy to Staging / Build & Deploy to Staging (push) Successful in 13m9s
- Remove @openapi annotations from /v1/billing/webhook (Stripe-internal) - Remove @openapi annotations from /v1/billing/success (browser redirect) - Mark /v1/signup/verify as deprecated (returns 410) - Add 3 TDD tests in openapi-spec.test.ts - Update 2 existing tests in app-routes.test.ts - 530 tests passing (was 527)
This commit is contained in:
parent
1d5d9adf08
commit
6b1b3d584e
15 changed files with 399 additions and 290 deletions
68
dist/middleware/usage.js
vendored
68
dist/middleware/usage.js
vendored
|
|
@ -30,53 +30,43 @@ export async function loadUsageData() {
|
|||
}
|
||||
}
|
||||
// Batch flush dirty entries to DB (Audit #10 + #12)
|
||||
async function flushDirtyEntries() {
|
||||
export async function flushDirtyEntries() {
|
||||
if (dirtyKeys.size === 0)
|
||||
return;
|
||||
const keysToFlush = [...dirtyKeys];
|
||||
const client = await connectWithRetry();
|
||||
try {
|
||||
await client.query("BEGIN");
|
||||
for (const key of keysToFlush) {
|
||||
const record = usage.get(key);
|
||||
if (!record)
|
||||
continue;
|
||||
try {
|
||||
await client.query(`INSERT INTO usage (key, count, month_key) VALUES ($1, $2, $3)
|
||||
ON CONFLICT (key) DO UPDATE SET count = $2, month_key = $3`, [key, record.count, record.monthKey]);
|
||||
for (const key of keysToFlush) {
|
||||
const record = usage.get(key);
|
||||
if (!record)
|
||||
continue;
|
||||
const client = await connectWithRetry();
|
||||
try {
|
||||
await client.query(`INSERT INTO usage (key, count, month_key) VALUES ($1, $2, $3)
|
||||
ON CONFLICT (key) DO UPDATE SET count = $2, month_key = $3`, [key, record.count, record.monthKey]);
|
||||
dirtyKeys.delete(key);
|
||||
retryCount.delete(key);
|
||||
}
|
||||
catch (error) {
|
||||
// Audit #12: retry logic for failed writes
|
||||
const retries = (retryCount.get(key) || 0) + 1;
|
||||
if (retries >= MAX_RETRIES) {
|
||||
logger.error({ key: key.slice(0, 8) + "...", retries }, "CRITICAL: Usage write failed after max retries, data may diverge");
|
||||
dirtyKeys.delete(key);
|
||||
retryCount.delete(key);
|
||||
}
|
||||
catch (error) {
|
||||
// Audit #12: retry logic for failed writes
|
||||
const retries = (retryCount.get(key) || 0) + 1;
|
||||
if (retries >= MAX_RETRIES) {
|
||||
logger.error({ key: key.slice(0, 8) + "...", retries }, "CRITICAL: Usage write failed after max retries, data may diverge");
|
||||
dirtyKeys.delete(key);
|
||||
retryCount.delete(key);
|
||||
}
|
||||
else {
|
||||
retryCount.set(key, retries);
|
||||
logger.warn({ key: key.slice(0, 8) + "...", retries }, "Usage write failed, will retry");
|
||||
}
|
||||
else {
|
||||
retryCount.set(key, retries);
|
||||
logger.warn({ key: key.slice(0, 8) + "...", retries }, "Usage write failed, will retry");
|
||||
}
|
||||
}
|
||||
await client.query("COMMIT");
|
||||
}
|
||||
catch (error) {
|
||||
await client.query("ROLLBACK").catch(() => { });
|
||||
logger.error({ err: error }, "Failed to flush usage batch");
|
||||
// Keep all keys dirty for retry
|
||||
}
|
||||
finally {
|
||||
client.release();
|
||||
finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
}
|
||||
// Periodic flush
|
||||
setInterval(flushDirtyEntries, FLUSH_INTERVAL_MS);
|
||||
// Flush on process exit
|
||||
process.on("SIGTERM", () => { flushDirtyEntries().catch(() => { }); });
|
||||
process.on("SIGINT", () => { flushDirtyEntries().catch(() => { }); });
|
||||
// Note: SIGTERM/SIGINT flush is handled by the shutdown orchestrator in index.ts
|
||||
// to avoid race conditions with pool.end().
|
||||
export function usageMiddleware(req, res, next) {
|
||||
const keyInfo = req.apiKeyInfo;
|
||||
const key = keyInfo?.key || "unknown";
|
||||
|
|
@ -113,6 +103,14 @@ function trackUsage(key, monthKey) {
|
|||
flushDirtyEntries().catch((err) => logger.error({ err }, "Threshold flush failed"));
|
||||
}
|
||||
}
|
||||
export function getUsageForKey(key) {
|
||||
const monthKey = getMonthKey();
|
||||
const record = usage.get(key);
|
||||
if (record && record.monthKey === monthKey) {
|
||||
return { count: record.count, monthKey };
|
||||
}
|
||||
return { count: 0, monthKey };
|
||||
}
|
||||
export function getUsageStats(apiKey) {
|
||||
const stats = {};
|
||||
if (apiKey) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue