//#region ../../node_modules/.pnpm/p-timeout@7.0.1/node_modules/p-timeout/index.js
var TimeoutError = class TimeoutError extends Error {
	name = "TimeoutError";
	constructor(message, options) {
		super(message, options);
		Error.captureStackTrace?.(this, TimeoutError);
	}
};
const getAbortedReason = (signal) => signal.reason ?? new DOMException("This operation was aborted.", "AbortError");
function pTimeout(promise, options) {
	const { milliseconds, fallback, message, customTimers = {
		setTimeout,
		clearTimeout
	}, signal } = options;
	let timer;
	let abortHandler;
	const cancelablePromise = new Promise((resolve, reject) => {
		if (typeof milliseconds !== "number" || Math.sign(milliseconds) !== 1) throw new TypeError(`Expected \`milliseconds\` to be a positive number, got \`${milliseconds}\``);
		if (signal?.aborted) {
			reject(getAbortedReason(signal));
			return;
		}
		if (signal) {
			abortHandler = () => {
				reject(getAbortedReason(signal));
			};
			signal.addEventListener("abort", abortHandler, { once: true });
		}
		promise.then(resolve, reject);
		if (milliseconds === Number.POSITIVE_INFINITY) return;
		const timeoutError = new TimeoutError();
		timer = customTimers.setTimeout.call(void 0, () => {
			if (fallback) {
				try {
					resolve(fallback());
				} catch (error) {
					reject(error);
				}
				return;
			}
			if (typeof promise.cancel === "function") promise.cancel();
			if (message === false) resolve();
			else if (message instanceof Error) reject(message);
			else {
				timeoutError.message = message ?? `Promise timed out after ${milliseconds} milliseconds`;
				reject(timeoutError);
			}
		}, milliseconds);
	}).finally(() => {
		cancelablePromise.clear();
		if (abortHandler && signal) signal.removeEventListener("abort", abortHandler);
	});
	cancelablePromise.clear = () => {
		customTimers.clearTimeout.call(void 0, timer);
		timer = void 0;
	};
	return cancelablePromise;
}
//#endregion
export { TimeoutError, pTimeout as default };

//# sourceMappingURL=index.js.map