Skip to content

Commit 020aa62

Browse files
replace curl with fetch, v0.1.6
1 parent 7228c97 commit 020aa62

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "wiki-plugin-datalog",
3-
"version": "0.1.5",
3+
"version": "0.1.6",
44
"description": "Federated Wiki - Datalog Plugin",
55
"keywords": [
66
"datalog",

server/server.js

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@
8787
return offset*year
8888
}
8989

90+
function timeout(duration) {
91+
// https://stackoverflow.com/a/49857905
92+
return new Promise((_, reject) =>
93+
setTimeout(() => reject(new Error(`timeout after ${duration} msec`)), duration))
94+
}
95+
96+
9097
function activate(slugitem) {
9198

9299
console.log('activate', slugitem)
@@ -103,12 +110,6 @@
103110
// return `${assets}/plugins/datalog/${slug}/${utc(new Date(clock),chunk)}.log`
104111
// }
105112

106-
function timeout(duration) {
107-
// https://stackoverflow.com/a/49857905
108-
return new Promise((_, reject) =>
109-
setTimeout(() => reject(new Error(`timeout after ${duration} msec`)), duration))
110-
}
111-
112113
function sample() {
113114

114115
let clock = Date.now()
@@ -241,17 +242,29 @@
241242
})
242243

243244
app.get('/plugin/datalog/curl', cors, (req, res) => {
244-
console.log(req.query)
245-
let curl = `curl -m 3 -s '${req.query.url}'`
245+
let url = /^http/.test(req.query.url) ? req.query.url : `http://${req.query.url}`
246246
let t0 = Date.now()
247-
exec(curl, (err, stdout, stderr) => {
248-
let sample = {
249-
exit: err ? err.code : 0,
250-
time: Date.now() - t0,
251-
stdout: stdout.length,
252-
stderr: stderr.length
253-
}
247+
248+
function send(sample) {
249+
sample.time = Date.now() - t0
254250
res.send(JSON.stringify(sample))
251+
}
252+
253+
Promise.race([
254+
fetch(url,{redirect:'manual'}),
255+
timeout(3000)
256+
])
257+
.then(response => {
258+
if (!response.ok && !response.status==302) {
259+
return send({exit: 1, error: response.statusText, code: response.status})
260+
}
261+
return response.text()
262+
})
263+
.then(data => {
264+
return send({exit: 0, stdout:data.length})
265+
})
266+
.catch(error => {
267+
return send({exit: 2, error: error.message})
255268
})
256269
})
257270

0 commit comments

Comments
 (0)