UNPKG

@message-in-the-middle/core

Version:

Framework-agnostic middleware pattern for message queue processing. Core package with all middlewares.

57 lines 2.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ImmediateRetryInboundMiddleware = void 0; class ImmediateRetryInboundMiddleware { options; constructor(options) { this.options = options; } async process(context, next) { const maxRetries = this.options.maxRetries; const delayMs = this.options.delayMs || 1000; const backoffMultiplier = this.options.backoffMultiplier || 2; let lastError; let attempt = 0; while (attempt <= maxRetries) { try { context.metadata.retryAttempt = attempt; await next(); return; } catch (error) { lastError = error; attempt++; if (this.options.retryableErrors && !this.options.retryableErrors.includes(error.name)) { throw error; } if (attempt <= maxRetries) { const delay = delayMs * Math.pow(backoffMultiplier, attempt - 1); context.metadata.retryDelay = delay; await this.sleep(delay); } } } context.metadata.retryExhausted = true; throw new Error(`Max retries (${maxRetries}) exceeded. Last error: ${lastError?.message || 'Unknown error'}`); } sleep(ms) { return new Promise((resolve, reject) => { const timeout = setTimeout(resolve, ms); if (this.options.signal) { const onAbort = () => { clearTimeout(timeout); reject(new Error('Retry aborted')); }; if (this.options.signal.aborted) { clearTimeout(timeout); reject(new Error('Retry aborted')); return; } this.options.signal.addEventListener('abort', onAbort, { once: true }); } }); } } exports.ImmediateRetryInboundMiddleware = ImmediateRetryInboundMiddleware; //# sourceMappingURL=immediate-retry.middleware.js.map