import { ServerRuntimeClient, applySdkMetadata, debug } from '@sentry/core'; import { DEBUG_BUILD } from './debug-build.js'; class CloudflareClient extends ServerRuntimeClient { /** * Creates a new Cloudflare SDK instance. * @param options Configuration options for this SDK. */ constructor(options) { applySdkMetadata(options, "cloudflare"); options._metadata = options._metadata || {}; const { flushLock, ...serverOptions } = options; const clientOptions = { ...serverOptions, platform: "javascript", // TODO: Grab version information runtime: { name: "cloudflare" }, // TODO: Add server name _flushInterval: 0 }; super(clientOptions); this._pendingSpans = /* @__PURE__ */ new Set(); this._spanCompletionPromise = null; this._resolveSpanCompletion = null; this._unsubscribeSpanStart = null; this._unsubscribeSpanEnd = null; this._flushLock = flushLock; this._unsubscribeSpanStart = this.on("spanStart", (span) => { const spanId = span.spanContext().spanId; DEBUG_BUILD && debug.log("[CloudflareClient] Span started:", spanId); this._pendingSpans.add(spanId); if (!this._spanCompletionPromise) { this._spanCompletionPromise = new Promise((resolve) => { this._resolveSpanCompletion = resolve; }); } }); this._unsubscribeSpanEnd = this.on("spanEnd", (span) => { const spanId = span.spanContext().spanId; DEBUG_BUILD && debug.log("[CloudflareClient] Span ended:", spanId); this._pendingSpans.delete(spanId); if (this._pendingSpans.size === 0 && this._resolveSpanCompletion) { DEBUG_BUILD && debug.log("[CloudflareClient] All spans completed, resolving promise"); this._resolveSpanCompletion(); this._resetSpanCompletionPromise(); } }); } /** * Flushes pending operations and ensures all data is processed. * If a timeout is provided, the operation will be completed within the specified time limit. * * It will wait for all pending spans to complete before flushing. * * @param {number} [timeout] - Optional timeout in milliseconds to force the completion of the flush operation. * @return {Promise} A promise that resolves to a boolean indicating whether the flush operation was successful. */ async flush(timeout) { if (this._flushLock) { await this._flushLock.finalize(); } if (this._pendingSpans.size > 0 && this._spanCompletionPromise) { DEBUG_BUILD && debug.log("[CloudflareClient] Waiting for", this._pendingSpans.size, "pending spans to complete..."); const timeoutMs = timeout ?? 5e3; const spanCompletionRace = Promise.race([ this._spanCompletionPromise, new Promise( (resolve) => setTimeout(() => { DEBUG_BUILD && debug.log("[CloudflareClient] Span completion timeout after", timeoutMs, "ms, flushing anyway"); resolve(void 0); }, timeoutMs) ) ]); await spanCompletionRace; } return super.flush(timeout); } /** * Disposes of the client and releases all resources. * * This method clears all Cloudflare-specific state in addition to the base client cleanup. * It unsubscribes from span lifecycle events and clears pending span tracking. * * Call this method after flushing to allow the client to be garbage collected. * After calling dispose(), the client should not be used anymore. */ dispose() { DEBUG_BUILD && debug.log("[CloudflareClient] Disposing client..."); super.dispose(); if (this._unsubscribeSpanStart) { this._unsubscribeSpanStart(); this._unsubscribeSpanStart = null; } if (this._unsubscribeSpanEnd) { this._unsubscribeSpanEnd(); this._unsubscribeSpanEnd = null; } this._resetSpanCompletionPromise(); this._flushLock = void 0; } /** * Resets the span completion promise and resolve function. */ _resetSpanCompletionPromise() { this._pendingSpans.clear(); this._spanCompletionPromise = null; this._resolveSpanCompletion = null; } } export { CloudflareClient }; //# sourceMappingURL=client.js.map