@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
96 lines (95 loc) • 3.26 kB
JavaScript
import { DEBUG_BUILD } from "../debug-build.js";
import { debug } from "../utils/debug-logger.js";
import { envelopeContainsItemType } from "../utils/envelope.js";
import { isThenable } from "../utils/is.js";
import { safeDateNow } from "../utils/randomSafeContext.js";
import { parseRetryAfterHeader } from "../utils/ratelimit.js";
import { safeUnref } from "../utils/timer.js";
const START_DELAY = 5e3;
const MAX_DELAY = 36e5;
/**
* Wraps a transport and stores and retries events when they fail to send.
*
* @param createTransport The transport to wrap.
*/
function makeOfflineTransport(createTransport) {
function log(...args) {
DEBUG_BUILD && debug.log("[Offline]:", ...args);
}
return (options) => {
const transport = createTransport(options);
if (!options.createStore) throw new Error("No `createStore` function was provided");
const store = options.createStore(options);
let retryDelay = START_DELAY;
let flushTimer;
function shouldQueue(env, error, retryDelay) {
if (envelopeContainsItemType(env, ["client_report"])) return false;
if (options.shouldStore) return options.shouldStore(env, error, retryDelay);
return true;
}
function flushIn(delay) {
if (flushTimer) clearTimeout(flushTimer);
flushTimer = safeUnref(setTimeout(async () => {
flushTimer = void 0;
const found = await store.shift();
if (found) {
log("Attempting to send previously queued event");
found[0].sent_at = new Date(safeDateNow()).toISOString();
send(found, true).catch((e) => {
log("Failed to retry sending", e);
});
}
}, delay));
}
function flushWithBackOff() {
if (flushTimer) return;
flushIn(retryDelay);
retryDelay = Math.min(retryDelay * 2, MAX_DELAY);
}
async function send(envelope, isRetry = false) {
if (!isRetry && envelopeContainsItemType(envelope, ["replay_event", "replay_recording"])) {
await store.push(envelope);
flushIn(100);
return {};
}
try {
if (options.shouldSend) {
const decision = options.shouldSend(envelope);
if ((isThenable(decision) ? await decision : decision) === false) throw new Error("Envelope not sent because `shouldSend` callback returned false");
}
const result = await transport.send(envelope);
let delay = 100;
if (result) {
if (result.headers?.["retry-after"]) delay = parseRetryAfterHeader(result.headers["retry-after"]);
else if (result.headers?.["x-sentry-rate-limits"]) delay = 6e4;
else if ((result.statusCode || 0) >= 400) return result;
}
flushIn(delay);
retryDelay = START_DELAY;
return result;
} catch (e) {
const decision = shouldQueue(envelope, e, retryDelay);
if (isThenable(decision) ? await decision : decision) {
if (isRetry) await store.unshift(envelope);
else await store.push(envelope);
flushWithBackOff();
log("Error sending. Event queued.", e);
return {};
} else throw e;
}
}
if (options.flushAtStartup) flushWithBackOff();
return {
send,
flush: (timeout) => {
if (timeout === void 0) {
retryDelay = START_DELAY;
flushIn(100);
}
return transport.flush(timeout);
}
};
};
}
export { START_DELAY, makeOfflineTransport };
//# sourceMappingURL=offline.js.map