-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
190 lines (160 loc) · 6.22 KB
/
Copy pathserver.js
File metadata and controls
190 lines (160 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
'use strict';
const express = require('express');
const fetch = require('node-fetch');
const { LRUCache } = require('lru-cache');
const crypto = require('crypto');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
const START_TIME = Date.now();
const VERSION = '1.0.0';
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
// In-memory LRU cache: max 500 entries, no global TTL (TTL is per-entry)
const cache = new LRUCache({ max: 500 });
// In-memory usage counters keyed by API key
const usageMap = new Map();
function getCacheKey(url) {
return crypto.createHash('sha256').update(url).digest('hex');
}
function isValidUrl(str) {
try {
const u = new URL(str);
return u.protocol === 'http:' || u.protocol === 'https:';
} catch {
return false;
}
}
function getUsage(apiKey) {
if (!usageMap.has(apiKey)) {
usageMap.set(apiKey, { fresh_scrapes: 0, cache_hits: 0 });
}
return usageMap.get(apiKey);
}
function requireApiKey(req, res, next) {
const key = req.headers['x-api-key'];
if (!key || key.trim() === '') {
return res.status(401).json({ error: 'Missing X-API-Key header' });
}
req.apiKey = key.trim();
next();
}
// ── GET /health ────────────────────────────────────────────────────────────
app.get('/health', (req, res) => {
res.json({
status: 'ok',
version: VERSION,
uptime_seconds: Math.floor((Date.now() - START_TIME) / 1000)
});
});
// ── POST /scrape ───────────────────────────────────────────────────────────
app.post('/scrape', requireApiKey, async (req, res) => {
const { url, ttl_seconds = 3600 } = req.body || {};
if (!url) {
return res.status(400).json({ error: 'url is required' });
}
if (!isValidUrl(url)) {
return res.status(400).json({ error: 'Invalid URL' });
}
const cacheKey = getCacheKey(url);
const now = new Date();
const entry = cache.get(cacheKey);
if (entry) {
const ttlRemaining = Math.max(0, Math.floor((entry.expiresAt - Date.now()) / 1000));
if (ttlRemaining > 0) {
getUsage(req.apiKey).cache_hits += 1;
return res.json({
url,
html: entry.html,
cached: true,
fetched_at: entry.fetched_at,
ttl_remaining_sec: ttlRemaining,
credits_used: 0,
cache_key: cacheKey
});
}
}
// Cache miss or TTL expired — perform fresh fetch
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 10000);
try {
const response = await fetch(url, {
signal: controller.signal,
follow: 10,
headers: { 'User-Agent': 'CacheSnap/1.0 (+https://cachesnap.io)' }
});
clearTimeout(timeout);
const html = await response.text();
const fetchedAt = now.toISOString();
const expiresAt = Date.now() + ttl_seconds * 1000;
cache.set(cacheKey, { html, fetched_at: fetchedAt, expiresAt }, { ttl: ttl_seconds * 1000 });
getUsage(req.apiKey).fresh_scrapes += 1;
return res.json({
url,
html,
cached: false,
fetched_at: fetchedAt,
ttl_remaining_sec: ttl_seconds,
credits_used: 1,
cache_key: cacheKey
});
} catch (err) {
clearTimeout(timeout);
if (err.name === 'AbortError') {
return res.status(500).json({ error: 'Fetch timed out', url });
}
return res.status(500).json({ error: 'Fetch failed', url, detail: err.message });
}
});
// ── GET /cache/status ──────────────────────────────────────────────────────
app.get('/cache/status', requireApiKey, (req, res) => {
const { url } = req.query;
if (!url) return res.status(400).json({ error: 'url query parameter is required' });
const cacheKey = getCacheKey(url);
const entry = cache.get(cacheKey);
if (!entry) {
return res.json({ url, cached: false, ttl_remaining_sec: 0, fetched_at: null });
}
const ttlRemaining = Math.max(0, Math.floor((entry.expiresAt - Date.now()) / 1000));
return res.json({
url,
cached: ttlRemaining > 0,
ttl_remaining_sec: ttlRemaining,
fetched_at: entry.fetched_at
});
});
// ── DELETE /cache ──────────────────────────────────────────────────────────
app.delete('/cache', requireApiKey, (req, res) => {
const { url } = req.query;
if (!url) return res.status(400).json({ error: 'url query parameter is required' });
const cacheKey = getCacheKey(url);
cache.delete(cacheKey);
return res.json({ message: 'Cache invalidated', url });
});
// ── GET /usage ─────────────────────────────────────────────────────────────
app.get('/usage', requireApiKey, (req, res) => {
const usage = getUsage(req.apiKey);
const now = new Date();
const period = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}`;
const planLimit = 1000;
return res.json({
period,
fresh_scrapes: usage.fresh_scrapes,
cache_hits: usage.cache_hits,
credits_used: usage.fresh_scrapes,
credits_remaining: Math.max(0, planLimit - usage.fresh_scrapes),
plan: 'Free',
plan_limit: planLimit
});
});
// ── GET /docs ──────────────────────────────────────────────────────────────
app.get('/docs', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'docs.html'));
});
// ── Serve openapi.json ─────────────────────────────────────────────────────
app.get('/openapi.json', (req, res) => {
res.sendFile(path.join(__dirname, 'openapi.json'));
});
app.listen(PORT, '0.0.0.0', () => {
console.log(`CacheSnap API v${VERSION} running on port ${PORT}`);
});