-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhttp.js
435 lines (358 loc) · 13.2 KB
/
http.js
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
'use strict';
const fs = require('fs');
const path = require('path');
const { PREFIX, scopedRedisClient } = require('./util');
const config = require('config');
const Redis = require('ioredis');
const mustache = require('mustache');
const { nanoid } = require('nanoid');
const { Readable } = require('stream');
const { finished } = require('stream/promises');
const { ESLint } = require('eslint');
const { renderTemplate, templatesLoad, getTemplates } = require('./http/common');
const mimeTypes = require('mime-types');
const { expiryFromOptions } = require('./lib/expiry');
const { requestCounter, notFoundCounter, responseCounter } = require('./http/promMetrics');
const app = require('fastify')({
logger: true
});
require('./logger')('http');
const redisListener = new Redis(config.app.redis);
const registered = {
get: {}
};
const renderCache = {};
const linter = new ESLint({
useEslintrc: false,
overrideConfig: {
extends: ['eslint:recommended'],
parserOptions: {
sourceType: 'module',
ecmaVersion: 'latest'
},
env: {
node: true
}
}
});
process.on('SIGUSR1', () => {
console.log('SIGUSR1 received, reloading templates');
templatesLoad(true);
});
async function createShrtned (fromUrl) {
if (!config.http.shrtnHost) {
return null;
}
const headers = {
Accept: 'application/json'
};
if (config.http.shrtnCreds?.user && config.http.shrtnCreds?.pass) {
const { user, pass } = config.http.shrtnCreds;
headers.Authorization = `Basic ${Buffer.from(`${user}:${pass}`, 'utf-8').toString('base64')}`;
}
const response = await fetch(config.http.shrtnHost + '/add', {
method: 'POST',
body: fromUrl,
headers
});
if (!response.ok) {
console.error(`Shrtn request failed: ${response.status} "${response.statusText}"`);
return null;
}
const { redirect } = await response.json();
return `${config.http.shrtnHost}/${redirect}`;
}
function mkdirSyncIgnoreExist (dirPath) {
try {
fs.mkdirSync(dirPath);
} catch (e) {
if (!['EACCES', 'EEXIST'].includes(e.code)) {
throw e;
}
}
}
async function renderAndCache (handler) {
const { parsed: { data: { name, renderType } } } = handler;
const type = ['http', 'get-req', name].join(':');
// the 'get-req' message informs the creator of this endpoint that the
// data is now needed to complete the request, and...
await scopedRedisClient((reqPubClient, PREFIX) =>
reqPubClient.publish(PREFIX, JSON.stringify({ type })));
// ...(await handler.promise) waits for it to arrive
const { body, renderObj } = renderTemplate(renderType, (await handler.promise), handler.exp);
renderCache[name] = { renderType, renderObj };
return body;
}
redisListener.subscribe(PREFIX, (err) => {
if (err) {
throw err;
}
console.log('Connected to Redis');
const PutAllowedIds = {};
const reqPubClient = new Redis(config.redis.url);
mkdirSyncIgnoreExist(config.http.staticDir);
console.log(`Using static path: ${config.http.staticDir}`);
if (config.http.attachmentsDir) {
const { attachmentsDir } = config.http;
mkdirSyncIgnoreExist(attachmentsDir);
console.log(`Using attachments path: ${attachmentsDir}`);
app.get('/attachments/:name', async (req, res) => {
const attachmentPath = path.join(attachmentsDir, req.params.name);
try {
console.log(`serving ${attachmentPath}`);
const mimeType = mimeTypes.lookup(path.parse(attachmentPath).ext || 'application/octet-stream');
return res.type(mimeType).send(await fs.promises.readFile(attachmentPath));
} catch (e) {
console.error(`failed to send ${attachmentPath}:`, e);
return res.redirect(config.http.rootRedirectUrl);
}
});
}
async function staticServe (res, p) {
const allowed = {
'.js': 'text/javascript',
'.css': 'text/css',
'.map': 'application/json'
};
const { ext } = path.parse(p);
if (!allowed[ext]) {
return res.redirect(config.http.rootRedirectUrl);
}
return res.type(allowed[ext]).send(await fs.promises.readFile(p));
}
app.get('/vendored/monaco/*', async (req, res) => {
return staticServe(res, path.join(__dirname, 'node_modules', 'monaco-editor', 'min', 'vs', req.params['*']));
});
app.get('/min-maps/*', async (req, res) => {
return staticServe(res, path.join(__dirname, 'node_modules', 'monaco-editor', 'min-maps', req.params['*']));
});
app.get('/js/*', async (req, res) => {
return staticServe(res, path.join(__dirname, 'http', 'js', req.params['*']));
});
app.get('/static/:name', async (req, res) => {
const assetPath = path.join(config.http.staticDir, req.params.name);
const mimeType = mimeTypes.lookup(path.parse(assetPath).ext || 'application/octet-stream');
try {
return res.type(mimeType).send(await fs.promises.readFile(assetPath));
} catch (e) {
console.error(`failed to send ${assetPath}:`, e);
return res.redirect(config.http.rootRedirectUrl);
}
});
function checkForExpiry (req) {
const handler = registered.get[req.params.id];
if (!handler) {
console.debug('Bad handler!', req.params);
return true;
}
if (handler.exp && Number(new Date()) > handler.exp) {
console.debug('expiring!', req.params);
delete registered.get[req.params.id];
delete PutAllowedIds[req.params.id];
return true;
}
return false;
}
app.get('/:id', async (req, res) => {
if (checkForExpiry(req)) {
return res.redirect(config.http.rootRedirectUrl);
}
if (renderCache[req.params.id]) {
console.debug('using cached render obj for', req.params.id);
const { renderType, renderObj } = renderCache[req.params.id];
res.type('text/html; charset=utf-8').send(mustache.render(getTemplates()[renderType](), renderObj));
return;
}
try {
const handler = registered.get[req.params.id];
res.type('text/html; charset=utf-8');
res.send(await renderAndCache(handler));
} catch (err) {
console.error(err);
res.redirect(config.http.rootRedirectUrl);
}
});
// gets script state
app.get('/:id/:keyComponent/:snippetName', async (req, res) => {
if (checkForExpiry(req)) {
return res.redirect(config.http.rootRedirectUrl);
}
if (!PutAllowedIds[req.params.id] || !req.params.snippetName || !req.params.keyComponent) {
return res.redirect(config.http.rootRedirectUrl);
}
const { name, keyComponent } = PutAllowedIds[req.params.id];
if (keyComponent !== req.params.keyComponent || name !== req.params.snippetName) {
return res.redirect(config.http.rootRedirectUrl);
}
// this REALLY needs to be a proper IPC!!!!!!!
const RKEY = `${PREFIX}:${req.params.keyComponent}:state`;
return res.type('application/json').send(
await scopedRedisClient((r) => r.hget(RKEY, req.params.snippetName))
);
});
// linter
app.patch('/:id/:keyComponent/:snippetName', async (req, res) => {
if (checkForExpiry(req)) {
return res.redirect(config.http.rootRedirectUrl);
}
if (!PutAllowedIds[req.params.id] || !req.params.snippetName || !req.params.keyComponent) {
return res.redirect(config.http.rootRedirectUrl);
}
const src = '(async function () {\n' + req.body + '\n})();';
const linted = await linter.lintText(src);
return res.send({
linted,
formatted: {
html: (await linter.loadFormatter('html')).format(linted),
json: (await linter.loadFormatter('json')).format(linted)
}
});
});
app.put('/:id/:keyComponent/:snippetName', async (req, res) => {
if (checkForExpiry(req)) {
return res.redirect(config.http.rootRedirectUrl);
}
if (!PutAllowedIds[req.params.id] || !req.params.snippetName || !req.params.keyComponent) {
return res.redirect(config.http.rootRedirectUrl);
}
// this REALLY needs to be a proper IPC!!!!!!!
const RKEY = `${PREFIX}:${req.params.keyComponent}`;
await scopedRedisClient((r) => r.hset(RKEY, req.params.snippetName, req.body));
return res.code(204).send();
});
app.get('/', async (req, res) => {
res.redirect(config.http.rootRedirectUrl);
});
app.setNotFoundHandler((req, _res) => {
console.warn('404 Not Found', { method: req.method, path: req.url });
notFoundCounter.inc({ method: req.method, path: req.url });
});
app.addHook('onRequest', (req, _res, done) => {
const { method, url } = req.context.config;
if (!method || !url) {
return done();
}
requestCounter.inc({ method, path: url });
done();
});
app.addHook('onResponse', (req, res, done) => {
const { method, url } = req.context.config;
responseCounter.inc({ method, path: url, code: res.statusCode });
done();
});
process.on('SIGINT', () => {
console.log('Exiting...');
redisListener.disconnect();
app.close();
process.exit(0);
});
app.listen({ host: config.http.host, port: config.http.port }, (err, addr) => {
if (err) {
throw err;
}
console.log(`Listening on ${addr}, using FQDN ${config.http.fqdn}`);
redisListener.on('message', (chan, msg) => {
try {
const parsed = JSON.parse(msg);
const [type, subType, subSubType] = parsed.type.split(':');
if (type === 'http') {
if (subType === 'get-res') {
const handler = registered.get[subSubType];
if (!handler) {
throw new Error('bad handler');
}
if (!parsed.data) {
handler.reject(new Error('no data'));
return;
}
if (PutAllowedIds[subSubType] === true) {
const { name, keyComponent } = parsed.data;
PutAllowedIds[subSubType] = { name, keyComponent };
}
handler.resolve(parsed.data);
}
}
if (subType === 'createGetEndpoint') {
if (!parsed.data.name) {
throw new Error('bad args for createGetEndpoint');
}
const { name, options } = parsed.data;
const exp = expiryFromOptions(options);
let rr;
const promise = new Promise((resolve, reject) => {
rr = { resolve, reject };
});
registered.get[name] = {
exp: expiryFromOptions(options),
parsed,
promise,
...rr
};
// force immediate render if no expiry to generate the static file
if (!exp) {
const cachePath = path.join(config.http.staticDir, name + '.html');
renderAndCache(registered.get[name])
.then((renderedBody) =>
fs.promises.writeFile(cachePath, renderedBody))
.then(() => console.log(`Persisted unexpiring ${cachePath}`))
.catch((err) => console.log('renderAndCache failed', err));
}
if (parsed.data.allowPut) {
PutAllowedIds[name] = true; // clean this up on expiry of `name` (id)!
}
} else if (subType === 'isHTTPRunningRequest' && type === 'isXRunning') {
const { reqId } = parsed.data;
console.log('isHTTPRunningRequest reqId', reqId);
reqPubClient.publish(PREFIX, JSON.stringify({
type: 'isXRunning:isHTTPRunningResponse',
data: {
reqId,
listenAddr: addr,
fqdn: config.http.fqdn
}
}));
} else if (subType === 'cacheMessageAttachementRequest' && type === 'discord') {
const { attachmentURL } = parsed.data;
console.log('cacheMessageAttachement attachmentURL', attachmentURL);
const innerHandler = async () => {
const data = { attachmentURL, enabled: !!config.http.attachmentsDir, error: null };
if (data.enabled) {
try {
const { ext } = path.parse((new URL(attachmentURL)).pathname);
const fetchRes = await fetch(attachmentURL, { // eslint-disable-line no-undef
headers: {
Accept: '*/*'
}
});
data.attachmentURLShort = await createShrtned(attachmentURL);
if (!fetchRes.ok) {
throw new Error(fetchRes.statusText);
}
const newId = nanoid() + ext;
const outPath = path.join(config.http.attachmentsDir, newId);
const outStream = fs.createWriteStream(outPath);
await finished(Readable.fromWeb(fetchRes.body).pipe(outStream));
console.log(`Cached attachment ${newId} from source ${attachmentURL}`);
data.cachedURL = config.http.proto + '://' + config.http.fqdn + '/attachments/' + newId;
data.cachedURLShort = await createShrtned(data.cachedURL);
} catch (e) {
console.error(`Fetching or persisting ${attachmentURL} failed:`, e.message);
console.error(e);
data.error = e.message;
}
}
return data;
};
innerHandler().then((data) => {
reqPubClient.publish(PREFIX, JSON.stringify({
type: 'http:cacheMessageAttachementResponse',
data
}));
});
}
} catch (e) {
console.error(e);
}
});
});
});