diff --git a/package.json b/package.json index 4e9911a..f9ae596 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wiki-plugin-datalog", - "version": "0.1.5", + "version": "0.1.6", "description": "Federated Wiki - Datalog Plugin", "keywords": [ "datalog", diff --git a/server/server.js b/server/server.js index ea8d3b3..58405b4 100644 --- a/server/server.js +++ b/server/server.js @@ -87,6 +87,13 @@ return offset*year } + function timeout(duration) { + // https://stackoverflow.com/a/49857905 + return new Promise((_, reject) => + setTimeout(() => reject(new Error(`timeout after ${duration} msec`)), duration)) + } + + function activate(slugitem) { console.log('activate', slugitem) @@ -103,12 +110,6 @@ // return `${assets}/plugins/datalog/${slug}/${utc(new Date(clock),chunk)}.log` // } - function timeout(duration) { - // https://stackoverflow.com/a/49857905 - return new Promise((_, reject) => - setTimeout(() => reject(new Error(`timeout after ${duration} msec`)), duration)) - } - function sample() { let clock = Date.now() @@ -241,17 +242,29 @@ }) app.get('/plugin/datalog/curl', cors, (req, res) => { - console.log(req.query) - let curl = `curl -m 3 -s '${req.query.url}'` + let url = /^http/.test(req.query.url) ? req.query.url : `http://${req.query.url}` let t0 = Date.now() - exec(curl, (err, stdout, stderr) => { - let sample = { - exit: err ? err.code : 0, - time: Date.now() - t0, - stdout: stdout.length, - stderr: stderr.length - } + + function send(sample) { + sample.time = Date.now() - t0 res.send(JSON.stringify(sample)) + } + + Promise.race([ + fetch(url,{redirect:'manual'}), + timeout(3000) + ]) + .then(response => { + if (!response.ok && !response.status==302) { + return send({exit: 1, error: response.statusText, code: response.status}) + } + return response.text() + }) + .then(data => { + return send({exit: 0, stdout:data.length}) + }) + .catch(error => { + return send({exit: 2, error: error.message}) }) })