chore(errors): incremental tech debt — rate-limit typings, legacy web on Next 15
- api: Align rate-limit plugin docs with actual store (in-memory unless wired in index); type errorResponseBuilder context; augment FastifyRequest with optional ingest_token_id instead of casting to any. - web: Upgrade to Next 15 / React 19; add ESLint 9 flat config, tsconfig, next-env.d.ts; outputFileTracingRoot so file tracing stays within this product tree. Paperclip-Agent: Tiago Neves (urlKey tiago-neves; id 5d084b36-f38f-42a5-a92a-520005afda4f) Paperclip-Issue: BIT-2479 Paperclip-Run: e3292ca4-a7b9-42f1-83ca-bc3d78906532
This commit is contained in:
1 parent
0bd13bac5b
commit
78003b001d
7 files changed
+5594
-15
No files matched your search
@@ -20,13 +20,21 @@
|
||||
* INGEST_MAX_BODY_BYTES max ingest request body (default: 512 KB)
|
||||
* ADMIN_MAX_BODY_BYTES max admin API request body (default: 64 KB)
|
||||
*
|
||||
* All limits use Redis (via ioredis) for distributed counting when multiple
|
||||
* API instances run. Falls back to in-memory (no Redis configured) for dev.
|
||||
* @fastify/rate-limit uses an in-memory store unless a custom store is
|
||||
* configured at registration time (see `src/index.ts`).
|
||||
*/
|
||||
|
||||
import fp from 'fastify-plugin';
|
||||
import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
|
||||
|
||||
/** Shape of the `context` argument to @fastify/rate-limit `errorResponseBuilder`. */
|
||||
interface RateLimitErrorContext {
|
||||
ban: boolean;
|
||||
after: string;
|
||||
max: number;
|
||||
ttl: number;
|
||||
}
|
||||
|
||||
const RATE_INGEST_MAX = parseInt(process.env.RATE_INGEST_MAX ?? '1000', 10);
|
||||
const RATE_INGEST_WINDOW_MS = parseInt(process.env.RATE_INGEST_WINDOW_MS ?? '60000', 10);
|
||||
const RATE_LOGIN_MAX = parseInt(process.env.RATE_LOGIN_MAX ?? '10', 10);
|
||||
@@ -41,8 +49,8 @@ export const ADMIN_MAX_BODY_BYTES = parseInt(process.env.ADMIN_MAX_BODY_BYTES
|
||||
|
||||
/** Key generator for ingest rate limiting: prefer token ID, fall back to IP. */
|
||||
function ingest_key(request: FastifyRequest): string {
|
||||
// Ingest routes set request.ingest_token_id after token validation.
|
||||
const token_id = (request as any).ingest_token_id as string | undefined;
|
||||
// Ingest routes may set `ingest_token_id` after token validation (optional augmentation).
|
||||
const token_id = request.ingest_token_id;
|
||||
return token_id ? `ingest:token:${token_id}` : `ingest:ip:${request.ip}`;
|
||||
}
|
||||
|
||||
@@ -59,7 +67,7 @@ export default fp(async function rateLimitPlugin(app: FastifyInstance) {
|
||||
max: RATE_INGEST_MAX,
|
||||
timeWindow: RATE_INGEST_WINDOW_MS,
|
||||
keyGenerator: ingest_key,
|
||||
errorResponseBuilder: (_request: FastifyRequest, context: any) => ({
|
||||
errorResponseBuilder: (_request: FastifyRequest, context: RateLimitErrorContext) => ({
|
||||
error: 'TooManyRequests',
|
||||
message: 'Ingest rate limit exceeded. Slow down.',
|
||||
retryAfter: Math.ceil(context.ttl / 1000),
|
||||
@@ -69,7 +77,7 @@ export default fp(async function rateLimitPlugin(app: FastifyInstance) {
|
||||
max: RATE_LOGIN_MAX,
|
||||
timeWindow: RATE_LOGIN_WINDOW_MS,
|
||||
keyGenerator: (request: FastifyRequest) => `login:ip:${request.ip}`,
|
||||
errorResponseBuilder: (_request: FastifyRequest, context: any) => ({
|
||||
errorResponseBuilder: (_request: FastifyRequest, context: RateLimitErrorContext) => ({
|
||||
error: 'TooManyRequests',
|
||||
message: 'Too many login attempts. Try again later.',
|
||||
retryAfter: Math.ceil(context.ttl / 1000),
|
||||
@@ -83,7 +91,7 @@ export default fp(async function rateLimitPlugin(app: FastifyInstance) {
|
||||
max: RATE_MUTATION_MAX,
|
||||
timeWindow: RATE_MUTATION_WINDOW_MS,
|
||||
keyGenerator: (request: FastifyRequest) => `errmon_mut:ip:${request.ip}`,
|
||||
errorResponseBuilder: (_request: FastifyRequest, context: any) => ({
|
||||
errorResponseBuilder: (_request: FastifyRequest, context: RateLimitErrorContext) => ({
|
||||
success: false,
|
||||
error: 'TooManyRequests',
|
||||
message: 'Error-monitoring mutation rate limit exceeded. Slow down.',
|
||||
@@ -120,6 +128,10 @@ export default fp(async function rateLimitPlugin(app: FastifyInstance) {
|
||||
}, { name: 'rate-limit-config', dependencies: ['@fastify/rate-limit'] });
|
||||
|
||||
declare module 'fastify' {
|
||||
interface FastifyRequest {
|
||||
/** When set by ingest auth, rate limiting keys by token instead of IP. */
|
||||
ingest_token_id?: string;
|
||||
}
|
||||
interface FastifyInstance {
|
||||
rateLimits: {
|
||||
ingest: Record<string, unknown>;
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import { dirname } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { FlatCompat } from "@eslint/eslintrc";
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = dirname(__filename);
|
||||
|
||||
const compat = new FlatCompat({
|
||||
baseDirectory: __dirname,
|
||||
});
|
||||
|
||||
const eslintConfig = [...compat.extends("next/core-web-vitals", "next/typescript")];
|
||||
|
||||
export default eslintConfig;
|
||||
Vendored
+6
@@ -0,0 +1,6 @@
|
||||
/// <reference types="next" />
|
||||
/// <reference types="next/image-types/global" />
|
||||
/// <reference path="./.next/types/routes.d.ts" />
|
||||
|
||||
// NOTE: This file should not be edited
|
||||
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
|
||||
@@ -0,0 +1,12 @@
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
/** @type {import('next').NextConfig} */
|
||||
const nextConfig = {
|
||||
// Repo lives under a parent tree with other lockfiles; pin tracing to this product root.
|
||||
outputFileTracingRoot: path.join(__dirname, ".."),
|
||||
};
|
||||
|
||||
export default nextConfig;
|
||||
Generated
+5494
File diff suppressed because it is too large.
Load diff
+14
-8
@@ -10,15 +10,21 @@
|
||||
"typecheck": "tsc --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
"next": "^14.2.3",
|
||||
"react": "^18.3.1",
|
||||
"react-dom": "^18.3.1",
|
||||
"axios": "^1.7.2"
|
||||
"axios": "^1.7.2",
|
||||
"next": "^15.1.0",
|
||||
"react": "^19.0.0",
|
||||
"react-dom": "^19.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^20.14.0",
|
||||
"@types/react": "^18.3.3",
|
||||
"@types/react-dom": "^18.3.0",
|
||||
"typescript": "^5.4.5"
|
||||
"@eslint/eslintrc": "^3.2.0",
|
||||
"@types/node": "^22.9.0",
|
||||
"@types/react": "^19.0.0",
|
||||
"@types/react-dom": "^19.0.0",
|
||||
"eslint": "^9.15.0",
|
||||
"eslint-config-next": "^15.1.0",
|
||||
"typescript": "^5.6.3"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=20"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"lib": [
|
||||
"dom",
|
||||
"dom.iterable",
|
||||
"esnext"
|
||||
],
|
||||
"allowJs": true,
|
||||
"skipLibCheck": true,
|
||||
"strict": false,
|
||||
"noEmit": true,
|
||||
"incremental": true,
|
||||
"module": "esnext",
|
||||
"esModuleInterop": true,
|
||||
"moduleResolution": "node",
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"jsx": "preserve",
|
||||
"plugins": [
|
||||
{
|
||||
"name": "next"
|
||||
}
|
||||
],
|
||||
"target": "ES2017"
|
||||
},
|
||||
"include": [
|
||||
"next-env.d.ts",
|
||||
".next/types/**/*.ts",
|
||||
"**/*.ts",
|
||||
"**/*.tsx"
|
||||
],
|
||||
"exclude": [
|
||||
"node_modules"
|
||||
]
|
||||
}
|
||||
Reference in new issue
Block a user