@aikidosec/firewall
Version:
Zen by Aikido is an embedded Web Application Firewall that autonomously protects Node.js apps against common and critical attacks
50 lines (49 loc) • 1.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReportingAPINodeHTTP = void 0;
const fetch_1 = require("../../helpers/fetch");
class ReportingAPINodeHTTP {
constructor(reportingUrl) {
this.reportingUrl = reportingUrl;
}
toAPIResponse(statusCode, data) {
if (statusCode === 429) {
return { success: false, error: "rate_limited" };
}
if (statusCode === 401) {
return { success: false, error: "invalid_token" };
}
if (statusCode === 200) {
try {
return JSON.parse(data);
}
catch {
// Fall through
}
}
return { success: false, error: "unknown_error" };
}
async report(token, event, timeoutInMS) {
let response;
try {
response = await (0, fetch_1.fetch)({
url: new URL(`${this.reportingUrl.toString()}api/runtime/events`),
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: token.asString(),
},
body: JSON.stringify(event),
timeoutInMS,
});
}
catch (error) {
if (error.message.includes("timed out")) {
return { success: false, error: "timeout" };
}
throw error;
}
return this.toAPIResponse(response.statusCode, response.body);
}
}
exports.ReportingAPINodeHTTP = ReportingAPINodeHTTP;