Skip to content

Commit

Permalink
replace curl with fetch, v0.1.6
Browse files Browse the repository at this point in the history
  • Loading branch information
WardCunningham committed Jun 5, 2019
1 parent 7228c97 commit 020aa62
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wiki-plugin-datalog",
"version": "0.1.5",
"version": "0.1.6",
"description": "Federated Wiki - Datalog Plugin",
"keywords": [
"datalog",
Expand Down
43 changes: 28 additions & 15 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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})
})
})

Expand Down

0 comments on commit 020aa62

Please sign in to comment.