dms-report
Version:
Simple library for reporting to Dead Man's Snitch (deadmanssnitch.com)
60 lines (50 loc) • 1.27 kB
JavaScript
const fetch = require('fetch-everywhere');
exports.report = report;
exports.wrap = wrap.bind(null, false);
exports.wrapBlocking = wrap.bind(null, true);
function report(token, params = {}) {
const body = Object.keys(params)
.map(function (key) {
return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]);
})
.join('&');
return fetch('https://nosnch.in/' + token, {
method: 'post',
headers: {
'content-type': 'application/x-www-form-urlencoded',
},
body: body,
});
}
function wrap(blocking, token, func) {
return function () {
const promise = Promise.resolve(func.apply(null, arguments));
return promise.then(
function (result) {
return silentReport(blocking, token, void 0, function () {
return result;
});
},
function (error) {
const params = {
s: 1,
m: error.message,
};
return silentReport(blocking, token, params, function () {
throw error;
});
}
);
};
}
function silentReport(blocking, token, params, func) {
// give up after 5 seconds
const promise = Promise.race([
report(token, params),
new Promise(function (r) { setTimeout(r, 5000); }),
]);
if (!blocking)
return func();
// don't propagate reporting errors
return promise.then(func, func);
}