Refactor: Replace library rate limit with custom Map-based implementation

This commit is contained in:
2026-01-31 15:39:57 +00:00
parent 650931fa81
commit 933d14d1ca

View File

@@ -30,14 +30,14 @@ async function savePost(post) {
return posts;
}
// CaptchaLM Middleware
// Custom Rate Limiting
const rateLimitMap = new Map();
const RATE_LIMIT_WINDOW = 30000; // 30 seconds
// CaptchaLM Middleware (Rate limit removed)
const { protect, challenge } = createExpressMiddleware({
secret: 'ai-twitter-secret-key-123',
difficulty: 'medium',
rateLimit: {
maxAttempts: 1,
windowMs: 30000 // 30 seconds
}
});
// --- API Endpoints ---
@@ -55,8 +55,20 @@ app.get('/api/posts', async (req, res) => {
}
});
// 3. Create Post (Protected - AI Only)
// 3. Create Post (Protected - AI Only + Custom Rate Limit)
app.post('/api/posts', protect, async (req, res) => {
const ip = req.ip || req.socket.remoteAddress;
const lastPostTime = rateLimitMap.get(ip);
const now = Date.now();
if (lastPostTime && (now - lastPostTime < RATE_LIMIT_WINDOW)) {
const remaining = Math.ceil((RATE_LIMIT_WINDOW - (now - lastPostTime)) / 1000);
return res.status(429).json({
error: `Rate limited. Please wait ${remaining}s.`,
errorCode: 'RATE_LIMITED'
});
}
const { content, agentId = 'Unknown Agent' } = req.body;
if (!content) {
@@ -71,6 +83,8 @@ app.post('/api/posts', protect, async (req, res) => {
};
await savePost(newPost);
rateLimitMap.set(ip, now); // Update rate limit timestamp
console.log(`[New Log] ${agentId}: ${content.substring(0, 50)}...`);
res.json({ success: true, post: newPost });
@@ -79,4 +93,4 @@ app.post('/api/posts', protect, async (req, res) => {
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});
});