-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
348 lines (297 loc) · 12.4 KB
/
Copy pathserver.js
File metadata and controls
348 lines (297 loc) · 12.4 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const path = require('path');
const fs = require('fs');
const multer = require('multer');
const rateLimit = require('express-rate-limit');
const { UPLOADS_DIR } = require('./lib/config');
const { isUploadAllowed, UPLOAD_ALLOWED, BROWSER_ACCEPT_STRING } = require('./lib/formatCapabilities');
const app = express();
app.use(express.json());
app.set('trust proxy', 1);
// ── Rate Limiter ──
const { tieredApiLimiter } = require('./lib/http/rateLimitHeaders');
const apiLimiter = tieredApiLimiter;
// ── CORS ──
const PRODUCTION_ORIGINS = ['https://2md.traylinx.com', 'https://www.2md.traylinx.com'];
const ALLOWED_ORIGINS = process.env.CORS_ORIGINS
? process.env.CORS_ORIGINS.split(',')
: process.env.NODE_ENV === 'production'
? PRODUCTION_ORIGINS
: ['http://localhost:3000', 'http://localhost:5173'];
// Handle standard CORS for non-API routes (strict)
app.use((req, res, next) => {
if (req.path.startsWith('/api/')) return next(); // Handled separately
cors({ origin: ALLOWED_ORIGINS })(req, res, next);
});
// Permissive CORS for API routes so developers can use them from any domain
app.use('/api', cors({ origin: '*', methods: ['GET', 'POST', 'DELETE', 'OPTIONS'], allowedHeaders: ['Content-Type', 'Authorization', 'x-client-id'] }));
// ── Multer Upload ──
const upload = multer({
dest: UPLOADS_DIR,
limits: { fileSize: 100 * 1024 * 1024 },
fileFilter: (req, file, cb) => {
const ext = path.extname(file.originalname).toLowerCase();
if (isUploadAllowed(ext)) {
cb(null, true);
} else {
cb(new Error(`Unsupported file type: ${ext}. Allowed: ${BROWSER_ACCEPT_STRING}`), false);
}
}
});
const PORT = process.env.PORT || 8222;
app.get('/api/health', (req, res) => {
res.json({
status: 'ok',
api: 'html2md',
version: '2.0.0',
time: new Date().toISOString()
});
});
app.get('/api/ping', async (req, res) => {
const targetUrl = req.query.url;
if (!targetUrl) return res.status(400).json({ error: 'Missing url' });
try {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 8000);
const response = await fetch(targetUrl, {
method: 'GET',
signal: controller.signal,
headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36' }
});
clearTimeout(timeout);
// Aggressively destroy the incoming HTTP byte stream since we only care if it's a 200
if (response.body && typeof response.body.cancel === 'function') {
response.body.cancel().catch(() => {});
}
if (response.ok) {
return res.status(200).json({ status: response.status });
} else {
return res.status(response.status).json({ error: `HTTP ${response.status}` });
}
} catch (err) {
return res.status(400).json({ error: err.message });
}
});
app.get('/api/upload-info', (req, res) => {
res.json({
extensions: UPLOAD_ALLOWED.map(f => `.${f.ext}`),
families: [...new Set(UPLOAD_ALLOWED.map(f => f.family))],
acceptString: BROWSER_ACCEPT_STRING,
maxFileSizeMb: 100
});
});
// Dynamically serve llms.txt files with the correct runtime host
const serveDynamicLlms = (req, res, filename) => {
const filePath = path.join(__dirname, 'public', filename);
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) return res.status(500).send('File not found');
const baseUrl = `${req.headers['x-forwarded-proto'] || req.protocol}://${req.get('host')}`;
const dynamicContent = data.replace(/\{\{BASE_URL\}\}/g, baseUrl);
res.type('text/plain; charset=utf-8').send(dynamicContent);
});
};
app.get('/llms.txt', (req, res) => serveDynamicLlms(req, res, 'llms.txt'));
app.get('/llms-full.txt', (req, res) => serveDynamicLlms(req, res, 'llms-full.txt'));
app.get('/skills/:skillName.md', (req, res) => {
const skillName = req.params.skillName;
if (!/^[a-zA-Z0-9_-]+$/.test(skillName)) {
return res.status(400).send('Invalid skill name');
}
const filePath = path.join(__dirname, '.agents', 'skills', skillName, 'SKILL.md');
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) return res.status(404).send('Skill not found');
res.type('text/markdown; charset=utf-8').send(data);
});
});
// Agent discovery files (served from built frontend dist)
app.get('/.well-known/ai-plugin.json', (req, res) => {
res.sendFile(path.join(__dirname, 'frontend', 'dist', '.well-known', 'ai-plugin.json'));
});
app.get('/agents.json', (req, res) => {
res.sendFile(path.join(__dirname, 'frontend', 'dist', 'agents.json'));
});
// ── Global Middleware ──
const { requestIdMiddleware } = require('./lib/http/requestId');
const { backPressureMiddleware } = require('./lib/http/backPressure');
app.use(requestIdMiddleware);
app.use('/api', backPressureMiddleware());
// ── Route Modules ──
const deps = { apiLimiter, upload };
require('./routes/convert')(app);
require('./routes/batch')(app, deps);
require('./routes/crawl')(app, deps);
require('./routes/agentify')(app, deps);
require('./routes/file2md')(app, deps);
require('./routes/video2md')(app, deps);
require('./routes/download')(app);
require('./routes/jobs')(app);
// ── Crawl URL-Prepend Zero-Friction Route ──
// e.g. GET /crawl/https://docs.example.com?depth=2&email=you@example.com
app.use((req, res, next) => {
if (req.method !== 'GET') return next();
if (!req.path.startsWith('/crawl/')) return next();
const targetUrl = req.path.substring('/crawl/'.length);
if (!/^https?:\/\//i.test(targetUrl)) return next();
// Rewrite to POST /api/crawl in async mode
req.method = 'POST';
req.body = {
url: targetUrl,
depth: parseInt(req.query.depth, 10) || 3,
maxPages: parseInt(req.query.maxPages, 10) || 50,
treeOnly: false,
async: true,
...(req.query.email ? { email: req.query.email } : {}),
...(req.query.webhook_url ? { webhook_url: req.query.webhook_url } : {}),
...(req.query.apiKey ? { apiKey: req.query.apiKey } : {}),
};
req.url = '/api/crawl';
return app.handle(req, res);
});
// ── File2MD URL-Prepend Zero-Friction Route ──
// e.g. GET /file2md/https://example.com/document.pdf
app.use((req, res, next) => {
if (req.method !== 'GET') return next();
if (!req.path.startsWith('/file2md/')) return next();
const targetUrl = req.path.substring('/file2md/'.length);
if (!/^https?:\/\//i.test(targetUrl)) return next();
req.method = 'POST';
req.body = {
url: targetUrl,
apiKey: req.query.apiKey,
enhance: req.query.enhance,
model: req.query.model,
format: req.query.format || 'markdown'
};
req.url = '/api/file2md';
if (app.file2mdHandler) {
return app.file2mdHandler(req, res);
}
return res.status(500).json({ error: 'File2md handler not initialized' });
});
// ── URL-Prepend Zero-Friction Route ──
// e.g. GET /https://example.com -> routes to convert with format=markdown
// For file URLs, routes to file2md. Supports extension-less file URLs
// via Content-Type probing (like Firecrawl).
app.use(async (req, res, next) => {
if (req.method !== 'GET') return next();
// Extract path without leading slash
const targetUrl = req.path.substring(1);
// Basic validation to see if this resembles an absolute URL.
// If not, pass it on to the SPA fallback below.
if (!/^https?:\/\//i.test(targetUrl)) {
return next();
}
const { URL_ALLOWED, getFamily } = require('./lib/formatCapabilities');
const allowedExts = URL_ALLOWED.map(f => f.ext).join('|');
const fileMatch = targetUrl.match(new RegExp(`\\.(${allowedExts})(\\?.*)?$`, 'i'));
let isFileUrl = !!fileMatch;
let detectedExt = fileMatch ? fileMatch[1].toLowerCase() : null;
// ── Content-Type probe for extension-less URLs ──
// URLs like https://arxiv.org/pdf/2406.19314 serve PDFs without a .pdf extension.
// We do a quick HEAD probe to detect the actual content type.
const FILE_CONTENT_TYPES = {
'application/pdf': 'pdf',
'image/png': 'png', 'image/jpeg': 'jpg', 'image/gif': 'gif', 'image/webp': 'webp',
'text/csv': 'csv',
'application/json': 'json',
'audio/mpeg': 'mp3', 'audio/wav': 'wav', 'audio/mp4': 'm4a',
'video/mp4': 'mp4', 'video/quicktime': 'mov', 'video/webm': 'webm',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': 'docx',
'application/vnd.openxmlformats-officedocument.presentationml.presentation': 'pptx',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': 'xlsx',
'application/octet-stream': null, // needs further inspection — route to file2md
};
if (!isFileUrl) {
try {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 4000);
const probe = await fetch(targetUrl, {
method: 'HEAD',
signal: controller.signal,
headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36' },
redirect: 'follow',
});
clearTimeout(timeout);
const ct = (probe.headers.get('content-type') || '').split(';')[0].trim().toLowerCase();
// If it's NOT text/html, check if it's a known file type
if (ct && !ct.startsWith('text/html') && ct in FILE_CONTENT_TYPES) {
detectedExt = FILE_CONTENT_TYPES[ct];
isFileUrl = true;
console.log(`[URL-Prepend] Content-Type probe: ${ct} → routing to file2md`);
}
} catch (e) {
// Probe failed or timed out — fall through to web converter
}
}
req.method = 'POST';
if (isFileUrl) {
const ext = detectedExt;
const family = ext ? getFamily(ext) : null;
const isPremium = family && ['image', 'image-partial', 'media'].includes(family);
if (isPremium && !req.query.apiKey) {
return res.status(401).json({ success: false, error: `File conversion for ${(ext || 'media').toUpperCase()} media requires an API key. Append ?apiKey=sk-... to your URL.` });
}
req.body = {
url: targetUrl,
apiKey: req.query.apiKey,
enhance: req.query.enhance,
model: req.query.model,
format: req.query.format || 'markdown',
declaredExt: detectedExt
};
req.url = '/api/file2md';
if (app.file2mdHandler) {
return app.file2mdHandler(req, res);
}
return res.status(500).json({ error: 'File2md handler not initialized' });
}
// Fake what a JSON POST to /api/convert would look like
req.body = { url: targetUrl, format: req.query.format || 'markdown' };
req.url = '/api/convert';
// Call the registered convert handler directly
if (app.convertHandler) {
return app.convertHandler(req, res);
} else {
// Fallback if not loaded
return res.status(500).json({ error: 'Convert handler not initialized' });
}
});
// ── Static / SPA Fallback ──
if (process.env.NODE_ENV !== 'production') {
app.use(express.static(path.join(__dirname, 'frontend', 'dist')));
app.use((req, res, next) => {
if (req.method !== 'GET') return next();
if (req.path.startsWith('/api/')) return next();
res.sendFile(path.join(__dirname, 'frontend', 'dist', 'index.html'));
});
} else {
// In production, the API server relies on Netlify/Vercel for the frontend.
app.use((req, res, next) => {
if (req.method !== 'GET') return next();
if (req.path.startsWith('/api/')) return next();
// Redirect direct browser visits on the API domain to the frontend website
if (req.path === '/' || req.path === '') {
return res.redirect('https://2md.traylinx.com');
}
// Return standard JSON 404 for unhandled API requests
res.status(404).json({ error: 'Endpoint not found or method not allowed' });
});
}
// ── Start ──
if (require.main === module) {
const server = app.listen(PORT, '0.0.0.0', () => {
console.log(`HTML2MD API running on port ${PORT}`);
});
// Increase Node's global socket timeout to 20 minutes for long-running video transcriptions
server.setTimeout(1200000);
// Start the background job expiration sweeper
require('./lib/jobs/jobSweeper').startJobSweeper();
// Start proactive memory watchdog (triggers GC when RSS is high between jobs)
const { startMemoryWatchdog, getMemoryMB } = require('./lib/memoryGC');
startMemoryWatchdog();
const mem = getMemoryMB();
console.log(` Memory watchdog active (RSS: ${mem.rss}MB, ceiling: ${process.env.RSS_CEILING_MB || 768}MB)`);
}
module.exports = app;