diff --git a/src/node/hooks/express/apicalls.ts b/src/node/hooks/express/apicalls.ts index 91c44e389..ee7b6d5df 100644 --- a/src/node/hooks/express/apicalls.ts +++ b/src/node/hooks/express/apicalls.ts @@ -1,16 +1,39 @@ 'use strict'; +import express from "express"; + const log4js = require('log4js'); const clientLogger = log4js.getLogger('client'); const {Formidable} = require('formidable'); const apiHandler = require('../../handler/APIHandler'); const util = require('util'); + +function objectAsString(obj) { + var output = ''; + for (var property in obj) { + if(obj.hasOwnProperty(property) && typeof obj[property] !== 'function') { + var value = obj[property]; + if(typeof value === 'object' && !Array.isArray(value) && value !== null) { + value = '{' + objectAsString(value) + '}'; + } + output += property + ': ' + value +'; '; + } + } + return output; +} + exports.expressPreSession = async (hookName:string, {app}:any) => { + app.use(express.json()); // The Etherpad client side sends information about how a disconnect happened app.post('/ep/pad/connection-diagnostic-info', async (req:any, res:any) => { - const [fields, files] = await (new Formidable({})).parse(req); - clientLogger.info(`DIAGNOSTIC-INFO: ${fields.diagnosticInfo}`); + if (!req.body ||!req.body.diagnosticInfo || typeof req.body.diagnosticInfo !== 'object') { + clientLogger.warn('DIAGNOSTIC-INFO: No diagnostic info provided'); + res.status(400).end('No diagnostic info provided'); + return; + } + + clientLogger.info(`DIAGNOSTIC-INFO: ${objectAsString(req.body.diagnosticInfo)}`); res.end('OK'); }); diff --git a/src/static/js/pad.ts b/src/static/js/pad.ts index e94611fcd..7c3fa6ca5 100644 --- a/src/static/js/pad.ts +++ b/src/static/js/pad.ts @@ -725,18 +725,17 @@ const pad = { } }, asyncSendDiagnosticInfo: () => { - window.setTimeout(() => { - $.ajax( - { - type: 'post', - url: '../ep/pad/connection-diagnostic-info', - data: { - diagnosticInfo: JSON.stringify(pad.diagnosticInfo), - }, - success: () => {}, - error: () => {}, - }); - }, 0); + fetch('../ep/pad/connection-diagnostic-info', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + diagnosticInfo: pad.diagnosticInfo, + }), + }).catch((error) => { + console.error('Error sending diagnostic info:', error); + }) }, forceReconnect: () => { $('form#reconnectform input.padId').val(pad.getPadId());