
          const { Temporal } = require("@js-temporal/polyfill");
          const { URLPattern } = require("urlpattern-polyfill");
        
const require_chunk = require('./chunk-DqRYRqnO.cjs');
const require_http = require('./http-ClCl_dis.cjs');
const __logtape_logtape = require_chunk.__toESM(require("@logtape/logtape"));
const __fedify_vocab_runtime = require_chunk.__toESM(require("@fedify/vocab-runtime"));
const es_toolkit = require_chunk.__toESM(require("es-toolkit"));

//#region src/utils/docloader.ts
const logger$1 = (0, __logtape_logtape.getLogger)([
	"fedify",
	"utils",
	"docloader"
]);
/**
* Gets an authenticated {@link DocumentLoader} for the given identity.
* Note that an authenticated document loader intentionally does not cache
* the fetched documents.
* @param identity The identity to get the document loader for.
*                 The actor's key pair.
* @param options The options for the document loader.
* @returns The authenticated document loader.
* @throws {TypeError} If the key is invalid or unsupported.
* @since 0.4.0
*/
function getAuthenticatedDocumentLoader(identity, { allowPrivateAddress, userAgent, specDeterminer, tracerProvider } = {}) {
	require_http.validateCryptoKey(identity.privateKey);
	async function load(url, options) {
		if (!allowPrivateAddress) try {
			await (0, __fedify_vocab_runtime.validatePublicUrl)(url);
		} catch (error) {
			if (error instanceof __fedify_vocab_runtime.UrlError) logger$1.error("Disallowed private URL: {url}", {
				url,
				error
			});
			throw error;
		}
		const originalRequest = (0, __fedify_vocab_runtime.createActivityPubRequest)(url, { userAgent });
		const response = await require_http.doubleKnock(originalRequest, identity, {
			specDeterminer,
			log: (0, es_toolkit.curry)(__fedify_vocab_runtime.logRequest)(logger$1),
			tracerProvider,
			signal: options?.signal
		});
		return (0, __fedify_vocab_runtime.getRemoteDocument)(url, response, load);
	}
	return load;
}
const _fetchDocumentLoader = (0, __fedify_vocab_runtime.getDocumentLoader)();
const _fetchDocumentLoader_allowPrivateAddress = (0, __fedify_vocab_runtime.getDocumentLoader)({ allowPrivateAddress: true });

//#endregion
//#region src/utils/kv-cache.ts
const logger = (0, __logtape_logtape.getLogger)([
	"fedify",
	"utils",
	"kv-cache"
]);
/**
* Decorates a {@link DocumentLoader} with a cache backed by a {@link Deno.Kv}.
* @param parameters The parameters for the cache.
* @returns The decorated document loader which is cache-enabled.
*/
function kvCache({ loader, kv, prefix, rules }) {
	const keyPrefix = prefix ?? ["_fedify", "remoteDocument"];
	rules ??= [[new URLPattern({}), Temporal.Duration.from({ minutes: 5 })]];
	for (const [p, duration] of rules) if (Temporal.Duration.compare(duration, { days: 30 }) > 0) throw new TypeError("The maximum cache duration is 30 days: " + (p instanceof URLPattern ? `${p.protocol}://${p.username}:${p.password}@${p.hostname}:${p.port}/${p.pathname}?${p.search}#${p.hash}` : p.toString()));
	return async (url, options) => {
		if (url in __fedify_vocab_runtime.preloadedContexts) {
			logger.debug("Using preloaded context: {url}.", { url });
			return {
				contextUrl: null,
				document: __fedify_vocab_runtime.preloadedContexts[url],
				documentUrl: url
			};
		}
		const match = matchRule(url, rules);
		if (match == null) return await loader(url, options);
		const key = [...keyPrefix, url];
		let cache = void 0;
		try {
			cache = await kv.get(key);
		} catch (error) {
			if (error instanceof Error) logger.warn("Failed to get the document of {url} from the KV cache: {error}", {
				url,
				error
			});
		}
		if (cache == null) {
			const remoteDoc = await loader(url, options);
			try {
				await kv.set(key, remoteDoc, { ttl: match });
			} catch (error) {
				logger.warn("Failed to save the document of {url} to the KV cache: {error}", {
					url,
					error
				});
			}
			return remoteDoc;
		}
		return cache;
	};
}
function matchRule(url, rules) {
	for (const [pattern, d] of rules) {
		const duration = d instanceof Temporal.Duration ? d : Temporal.Duration.from(d);
		if (typeof pattern === "string") {
			if (url === pattern) return duration;
			continue;
		}
		if (pattern instanceof URL) {
			if (pattern.href == url) return duration;
			continue;
		}
		if (pattern.test(url)) return duration;
	}
	return null;
}

//#endregion
Object.defineProperty(exports, 'getAuthenticatedDocumentLoader', {
  enumerable: true,
  get: function () {
    return getAuthenticatedDocumentLoader;
  }
});
Object.defineProperty(exports, 'kvCache', {
  enumerable: true,
  get: function () {
    return kvCache;
  }
});