etherpad-lite/src/node/hooks/express/apicalls.js

40 lines
1.5 KiB
JavaScript
Raw Normal View History

'use strict';
2020-11-23 13:24:19 -05:00
const log4js = require('log4js');
const clientLogger = log4js.getLogger('client');
const formidable = require('formidable');
const apiHandler = require('../../handler/APIHandler');
2012-02-25 15:20:31 +01:00
exports.expressCreateServer = (hookName, args, cb) => {
2020-11-23 13:24:19 -05:00
// The Etherpad client side sends information about how a disconnect happened
args.app.post('/ep/pad/connection-diagnostic-info', (req, res) => {
new formidable.IncomingForm().parse(req, (err, fields, files) => {
clientLogger.info(`DIAGNOSTIC-INFO: ${fields.diagnosticInfo}`);
res.end('OK');
2012-02-25 15:20:31 +01:00
});
});
const parseJserrorForm = async (req) => await new Promise((resolve, reject) => {
const form = new formidable.IncomingForm();
2021-10-07 19:46:38 -04:00
form.maxFileSize = 1; // Files are not expected. Not sure if 0 means unlimited, so 1 is used.
form.on('error', (err) => reject(err));
form.parse(req, (err, fields) => err != null ? reject(err) : resolve(fields.errorInfo));
});
2020-11-23 13:24:19 -05:00
// The Etherpad client side sends information about client side javscript errors
args.app.post('/jserror', (req, res, next) => {
(async () => {
const data = JSON.parse(await parseJserrorForm(req));
2020-11-23 13:24:19 -05:00
clientLogger.warn(`${data.msg} --`, data);
res.end('OK');
})().catch((err) => next(err || new Error(err)));
2012-02-25 15:20:31 +01:00
});
2018-03-23 11:17:39 +00:00
2020-11-23 13:24:19 -05:00
// Provide a possibility to query the latest available API version
args.app.get('/api', (req, res) => {
res.json({currentVersion: apiHandler.latestApiVersion});
});
return cb();
2020-11-23 13:24:19 -05:00
};