@aikidosec/firewall
Version:
Zen by Aikido is an embedded Application Firewall that autonomously protects Node.js apps against common and critical attacks, provides rate limiting, detects malicious traffic (including bots), and more.
34 lines (33 loc) • 1.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PendingEvents = void 0;
const promises_1 = require("timers/promises");
/**
* Attack events (detected_attack and detected_attack_wave) are sent as fire-and-forget promises.
*
* However, in serverless environments like Lambda and Cloud Functions,
* if the handler returns quickly, the execution environment freezes before these API calls complete, causing events to be lost.
*
* This class tracks these pending promises so they can be awaited before the function returns.
*
* Heartbeat and started events are already handled in the Lambda/Cloud Function integration and don't need tracking.
*/
class PendingEvents {
constructor() {
this.pendingPromises = new Set();
}
onAPICall(apiPromise) {
this.pendingPromises.add(apiPromise);
apiPromise.finally(() => {
this.pendingPromises.delete(apiPromise);
});
}
async waitUntilSent(timeoutInMS) {
if (this.pendingPromises.size === 0) {
return;
}
const promises = Array.from(this.pendingPromises);
await Promise.race([Promise.allSettled(promises), (0, promises_1.setTimeout)(timeoutInMS)]);
}
}
exports.PendingEvents = PendingEvents;