mirror of
https://github.com/devkitshq/notifkit.git
synced 2026-10-19 18:24:52 +02:00
feat(provider-console): add mock latency support with jitter to ConsoleTransport
- Support latencyMs in ConsoleTransportOptions as a fixed number or range string (e.g. '60-150')\n- Apply randomized jitter between bounds to simulate real-world vendor network response times
This commit is contained in:
@@ -10,20 +10,39 @@ export interface ConsoleTransportOptions {
|
||||
channel?: NotificationChannel;
|
||||
logger?: Logger;
|
||||
limits?: { limit: number; windowSeconds: number };
|
||||
latencyMs?: number | string;
|
||||
}
|
||||
|
||||
export class ConsoleTransport implements Transport {
|
||||
readonly channel: NotificationChannel;
|
||||
readonly limits?: { limit: number; windowSeconds: number };
|
||||
readonly latencyMs?: number | string;
|
||||
private readonly logger?: Logger;
|
||||
|
||||
constructor({ channel = "push", logger, limits }: ConsoleTransportOptions = {}) {
|
||||
constructor({ channel = "push", logger, limits, latencyMs }: ConsoleTransportOptions = {}) {
|
||||
this.channel = channel;
|
||||
this.logger = logger;
|
||||
this.limits = limits;
|
||||
this.latencyMs = latencyMs;
|
||||
}
|
||||
|
||||
async send(task: NotificationDispatchedPayload): Promise<DeliveryResult> {
|
||||
if (this.latencyMs) {
|
||||
let delay = 0;
|
||||
if (typeof this.latencyMs === "number" && this.latencyMs > 0) {
|
||||
delay = this.latencyMs;
|
||||
} else if (typeof this.latencyMs === "string" && this.latencyMs.includes("-")) {
|
||||
const [minStr, maxStr] = this.latencyMs.split("-");
|
||||
const min = parseInt(minStr!, 10) || 0;
|
||||
const max = parseInt(maxStr!, 10) || min;
|
||||
delay = min === max ? min : Math.floor(Math.random() * (max - min + 1)) + min;
|
||||
} else if (typeof this.latencyMs === "string") {
|
||||
delay = parseInt(this.latencyMs, 10) || 0;
|
||||
}
|
||||
if (delay > 0) {
|
||||
await new Promise((resolve) => setTimeout(resolve, delay));
|
||||
}
|
||||
}
|
||||
const providerMessageId = `console-${Date.now()}`;
|
||||
|
||||
console.log(
|
||||
|
||||
Reference in New Issue
Block a user