diff --git a/.env.template b/.env.template index 6c73d93..2d5b27e 100644 --- a/.env.template +++ b/.env.template @@ -17,17 +17,12 @@ RPC_CONFIG='{ ... # Add more networks as needed. }' -PUBLIC_API_KEYS=string # Optional. Comma-separated list of authorized API keys. -DRIPS_API_KEY=string # Optional. API key withouth rate limit. - NODE_ENV=string # Required. 'development' or 'production'. POSTGRES_CONNECTION_STRING=string # Required. The connection string for the database. PRETEND_ALL_REPOS_EXIST=boolean # If true, app will always assume all GitHub repos exist. Used in E2E tests. Defaults to false. -RATE_LIMIT_WINDOW_IN_MINUTES=number # Optional. defaults to 2. -RATE_LIMIT_MAX_REQUESTS_PER_WINDOW=number # Optional. defaults to 1000. MAX_QUERY_DEPTH=number # Optional. defaults to 4. TIMEOUT_IN_SECONDS=number # Optional. defaults to 20. diff --git a/package-lock.json b/package-lock.json index e0f91b6..2f5f34b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24,7 +24,6 @@ "dotenv": "^16.3.1", "ethers": "^6.7.1", "express": "^4.18.2", - "express-rate-limit": "^7.1.5", "graphql": "^16.8.0", "graphql-depth-limit": "^1.1.0", "graphql-tag": "^2.12.6", @@ -8661,21 +8660,6 @@ "url": "https://opencollective.com/express" } }, - "node_modules/express-rate-limit": { - "version": "7.3.1", - "resolved": "https://registry.npmjs.org/express-rate-limit/-/express-rate-limit-7.3.1.tgz", - "integrity": "sha512-BbaryvkY4wEgDqLgD18/NSy2lDO2jTuT9Y8c1Mpx0X63Yz0sYd5zN6KPe7UvpuSVvV33T6RaE1o1IVZQjHMYgw==", - "license": "MIT", - "engines": { - "node": ">= 16" - }, - "funding": { - "url": "https://github.com/sponsors/express-rate-limit" - }, - "peerDependencies": { - "express": "4 || 5 || ^5.0.0-beta.1" - } - }, "node_modules/express/node_modules/debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", diff --git a/package.json b/package.json index 85a1ede..f6cc0fd 100644 --- a/package.json +++ b/package.json @@ -65,7 +65,6 @@ "dotenv": "^16.3.1", "ethers": "^6.7.1", "express": "^4.18.2", - "express-rate-limit": "^7.1.5", "graphql": "^16.8.0", "graphql-depth-limit": "^1.1.0", "graphql-tag": "^2.12.6", diff --git a/src/common/appSettings.ts b/src/common/appSettings.ts index 9ab41a5..9acc9e1 100644 --- a/src/common/appSettings.ts +++ b/src/common/appSettings.ts @@ -58,20 +58,10 @@ export default { rpcConfig: process.env.RPC_CONFIG ? RpcConfigSchema.parse(JSON.parse(process.env.RPC_CONFIG)) : missingEnvVar('RPC_CONFIG'), - publicApiKeys: process.env.PUBLIC_API_KEYS?.split(',') || [], - dripsApiKey: process.env.DRIPS_API_KEY, postgresConnectionString: process.env.POSTGRES_CONNECTION_STRING, pretendAllReposExist: (process.env.PRETEND_ALL_REPOS_EXIST as unknown as string) === 'true' || false, - rateLimitWindowInMinutes: parseInt( - process.env.RATE_LIMIT_WINDOW_IN_MINUTES ?? '2', - 10, - ), - rateLimitMaxRequestsPerWindow: parseInt( - process.env.RATE_LIMIT_MAX_REQUESTS_PER_WINDOW ?? '1000', - 10, - ), maxQueryDepth: parseInt(process.env.MAX_QUERY_DEPTH ?? '10', 10), timeoutInSeconds: parseInt(process.env.TIMEOUT_IN_SECONDS ?? '20', 10), ipfsGatewayUrl: diff --git a/src/server.ts b/src/server.ts index 5c4d03c..d0da18b 100644 --- a/src/server.ts +++ b/src/server.ts @@ -6,14 +6,6 @@ import { expressMiddleware } from '@apollo/server/express4'; import express from 'express'; import http from 'http'; import bodyParser from 'body-parser'; -import rateLimit from 'express-rate-limit'; -import type { - NextFunction, - ParamsDictionary, - Request, - Response, -} from 'express-serve-static-core'; -import type { ParsedQs } from 'qs'; import depthLimit from 'graphql-depth-limit'; import cors from 'cors'; import resolvers from './resolvers'; @@ -64,45 +56,11 @@ const server = new ApolloServer({ ], }); -const limiter = rateLimit({ - skipFailedRequests: true, - windowMs: appSettings.rateLimitWindowInMinutes * 60 * 1000, - limit: appSettings.rateLimitMaxRequestsPerWindow, - standardHeaders: 'draft-7', - legacyHeaders: false, - handler: (req, res /* , next */) => { - res.status(429).json({ - error: { - message: `Too many requests. Please try again at ${new Date( - (req as any).rateLimit.resetTime, - )}`, - statusCode: 429, - }, - }); - }, -}); - -const customRateLimiter = ( - req: Request>, - res: Response, number>, - next: NextFunction, -) => { - const apiKey = req.headers.authorization?.split(' ')[1]; - - if (apiKey && apiKey === appSettings.dripsApiKey) { - next(); - } else { - limiter(req, res, next); - } -}; - const startServer = async () => { await connectToDatabase(); await server.start(); - app.use(customRateLimiter); - app.use((req, res, next) => { res.setTimeout(appSettings.timeoutInSeconds * 1000, () => { res.send(408);