UNPKG

@fedify/fedify

Version:

An ActivityPub server framework

323 lines (322 loc) • 11.6 kB
import { Temporal } from "@js-temporal/polyfill"; import { URLPattern } from "urlpattern-polyfill"; import { a as createExponentialBackoffPolicy, c as buildCollectionSynchronizationHeader, d as Router, f as RouterError, i as SendActivityError, l as digest, o as respondWithObject, r as handleWebFinger, s as respondWithObjectIfAcceptable, t as createFederation, u as createFederationBuilder } from "../middleware-Ar1QOOPG.js"; import { isEqual } from "es-toolkit"; //#region src/federation/kv.ts /** * A key–value store that stores values in memory. * Do not use this in production as it does not persist values. * * @since 0.5.0 */ var MemoryKvStore = class { #values = {}; #encodeKey(key) { return JSON.stringify(key); } /** * {@inheritDoc KvStore.get} */ get(key) { const encodedKey = this.#encodeKey(key); const entry = this.#values[encodedKey]; if (entry == null) return Promise.resolve(void 0); const [value, expiration] = entry; if (expiration != null && Temporal.Now.instant().until(expiration).sign < 0) { delete this.#values[encodedKey]; return Promise.resolve(void 0); } return Promise.resolve(value); } /** * {@inheritDoc KvStore.set} */ set(key, value, options) { const encodedKey = this.#encodeKey(key); const expiration = options?.ttl == null ? null : Temporal.Now.instant().add(options.ttl.round({ largestUnit: "hour" })); this.#values[encodedKey] = [value, expiration]; return Promise.resolve(); } /** * {@inheritDoc KvStore.delete} */ delete(key) { const encodedKey = this.#encodeKey(key); delete this.#values[encodedKey]; return Promise.resolve(); } /** * {@inheritDoc KvStore.cas} */ cas(key, expectedValue, newValue, options) { const encodedKey = this.#encodeKey(key); const entry = this.#values[encodedKey]; let currentValue; if (entry == null) currentValue = void 0; else { const [value, expiration] = entry; if (expiration != null && Temporal.Now.instant().until(expiration).sign < 0) { delete this.#values[encodedKey]; currentValue = void 0; } else currentValue = value; } if (!isEqual(currentValue, expectedValue)) return Promise.resolve(false); const expiration = options?.ttl == null ? null : Temporal.Now.instant().add(options.ttl.round({ largestUnit: "hour" })); this.#values[encodedKey] = [newValue, expiration]; return Promise.resolve(true); } /** * {@inheritDoc KvStore.list} */ async *list(prefix) { const now = Temporal.Now.instant(); for (const [encodedKey, entry] of Object.entries(this.#values)) { const key = JSON.parse(encodedKey); if (prefix != null) { if (key.length < prefix.length) continue; if (!prefix.every((p, i) => key[i] === p)) continue; } const [value, expiration] = entry; if (expiration != null && now.until(expiration).sign < 0) { delete this.#values[encodedKey]; continue; } yield { key, value }; } } }; //#endregion //#region src/federation/mq.ts /** * A message queue that processes messages in the same process. * Do not use this in production as it does neither persist messages nor * distribute them across multiple processes. * * @since 0.5.0 */ var InProcessMessageQueue = class { #messages; #monitors; #pollIntervalMs; /** * Tracks which ordering keys are currently being processed to ensure * sequential processing for messages with the same key. */ #processingKeys; /** * In-process message queue does not provide native retry mechanisms. * @since 1.7.0 */ nativeRetrial = false; /** * Constructs a new {@link InProcessMessageQueue} with the given options. * @param options Additional options for the in-process message queue. */ constructor(options = {}) { this.#messages = []; this.#monitors = {}; this.#pollIntervalMs = Temporal.Duration.from(options.pollInterval ?? { seconds: 5 }).total("millisecond"); this.#processingKeys = /* @__PURE__ */ new Set(); } enqueue(message, options) { const delay = options?.delay == null ? 0 : Math.max(options.delay.total("millisecond"), 0); if (delay > 0) { setTimeout(() => this.enqueue(message, { ...options, delay: void 0 }), delay); return Promise.resolve(); } const orderingKey = options?.orderingKey ?? null; this.#messages.push({ message, orderingKey }); for (const monitorId in this.#monitors) this.#monitors[monitorId](); return Promise.resolve(); } enqueueMany(messages, options) { if (messages.length === 0) return Promise.resolve(); const delay = options?.delay == null ? 0 : Math.max(options.delay.total("millisecond"), 0); if (delay > 0) { setTimeout(() => this.enqueueMany(messages, { ...options, delay: void 0 }), delay); return Promise.resolve(); } const orderingKey = options?.orderingKey ?? null; for (const message of messages) this.#messages.push({ message, orderingKey }); for (const monitorId in this.#monitors) this.#monitors[monitorId](); return Promise.resolve(); } async listen(handler, options = {}) { const signal = options.signal; while (signal == null || !signal.aborted) { const idx = this.#messages.findIndex((m) => m.orderingKey == null || !this.#processingKeys.has(m.orderingKey)); if (idx >= 0) { const { message, orderingKey } = this.#messages.splice(idx, 1)[0]; if (orderingKey != null) this.#processingKeys.add(orderingKey); try { await handler(message); } finally { if (orderingKey != null) this.#processingKeys.delete(orderingKey); } } else if (this.#messages.length === 0) await this.#wait(this.#pollIntervalMs, signal); else await this.#wait(10, signal); } } #wait(ms, signal) { let timer = null; return Promise.any([new Promise((resolve) => { signal?.addEventListener("abort", () => { if (timer != null) clearTimeout(timer); resolve(); }, { once: true }); const monitorId = crypto.randomUUID(); this.#monitors[monitorId] = () => { delete this.#monitors[monitorId]; if (timer != null) clearTimeout(timer); resolve(); }; }), new Promise((resolve) => timer = setTimeout(resolve, ms))]); } }; /** * A message queue that processes messages in parallel. It takes another * {@link MessageQueue}, and processes messages in parallel up to a certain * number of workers. * * Actually, it's rather a decorator than a queue itself. * * Note that the workers do not run in truly parallel, in the sense that they * are not running in separate threads or processes. They are running in the * same process, but are scheduled to run in parallel. Hence, this is useful * for I/O-bound tasks, but not for CPU-bound tasks, which is okay for Fedify's * workloads. * * When using `ParallelMessageQueue`, the ordering guarantee is preserved * *only if* the underlying queue implementation delivers messages in a wrapper * format that includes the `__fedify_ordering_key__` property. Currently, * only `DenoKvMessageQueue` and `WorkersMessageQueue` use this format. * For other queue implementations (e.g., `InProcessMessageQueue`, * `RedisMessageQueue`, `PostgresMessageQueue`, `SqliteMessageQueue`, * `AmqpMessageQueue`), the ordering key cannot be detected by * `ParallelMessageQueue`, so ordering guarantees are handled by those * implementations directly rather than at the `ParallelMessageQueue` level. * * Messages with the same ordering key will never be processed concurrently * by different workers, ensuring sequential processing within each key. * Messages with different ordering keys (or no ordering key) can still be * processed in parallel. * * @since 1.0.0 */ var ParallelMessageQueue = class ParallelMessageQueue { queue; workers; /** * Inherits the native retry capability from the wrapped queue. * @since 1.7.0 */ nativeRetrial; /** * Tracks which ordering keys are currently being processed to ensure * sequential processing for messages with the same key. */ #processingKeys = /* @__PURE__ */ new Set(); /** * Pending messages waiting for their ordering key to become available. */ #pendingMessages = []; /** * Constructs a new {@link ParallelMessageQueue} with the given queue and * number of workers. * @param queue The message queue to use under the hood. Note that * {@link ParallelMessageQueue} cannot be nested. * @param workers The number of workers to process messages in parallel. * @throws {TypeError} If the given queue is an instance of * {@link ParallelMessageQueue}. */ constructor(queue, workers) { if (queue instanceof ParallelMessageQueue) throw new TypeError("Cannot nest ParallelMessageQueue."); this.queue = queue; this.workers = workers; this.nativeRetrial = queue.nativeRetrial; } enqueue(message, options) { return this.queue.enqueue(message, options); } async enqueueMany(messages, options) { if (this.queue.enqueueMany == null) { const errors = (await Promise.allSettled(messages.map((message) => this.queue.enqueue(message, options)))).filter((r) => r.status === "rejected").map((r) => r.reason); if (errors.length > 1) throw new AggregateError(errors, "Failed to enqueue messages."); else if (errors.length === 1) throw errors[0]; return; } await this.queue.enqueueMany(messages, options); } /** * Extracts ordering key from a message if present. * * This method only works for queue implementations that deliver messages * in the wrapper format with `__fedify_ordering_key__` property. Currently, * only `DenoKvMessageQueue` and `WorkersMessageQueue` use this format. * * For other queue implementations (`InProcessMessageQueue`, * `RedisMessageQueue`, `PostgresMessageQueue`, `SqliteMessageQueue`, * `AmqpMessageQueue`), messages are delivered as raw payloads without the * wrapper, so the ordering key cannot be detected here. Those * implementations handle ordering guarantees internally. */ #extractOrderingKey(message) { if (message != null && typeof message === "object") { if ("__fedify_ordering_key__" in message) return message.__fedify_ordering_key__; } } listen(handler, options = {}) { const workers = /* @__PURE__ */ new Map(); return this.queue.listen(async (message) => { while (workers.size >= this.workers) { const consumedId = await Promise.any(workers.values()); workers.delete(consumedId); } const workerId = crypto.randomUUID(); const orderingKey = this.#extractOrderingKey(message); if (orderingKey != null && this.#processingKeys.has(orderingKey)) await new Promise((resolve) => { this.#pendingMessages.push({ message, orderingKey, resolve }); }); if (orderingKey != null) this.#processingKeys.add(orderingKey); const promise = this.#work(workerId, handler, message, orderingKey); workers.set(workerId, promise); }, options); } async #work(workerId, handler, message, orderingKey) { await this.#sleep(0); try { await handler(message); } finally { if (orderingKey != null) { this.#processingKeys.delete(orderingKey); const pendingIdx = this.#pendingMessages.findIndex((p) => p.orderingKey === orderingKey); if (pendingIdx >= 0) this.#pendingMessages.splice(pendingIdx, 1)[0].resolve(); } } return workerId; } #sleep(ms) { return new Promise((resolve) => setTimeout(resolve, ms)); } }; //#endregion export { InProcessMessageQueue, MemoryKvStore, ParallelMessageQueue, Router, RouterError, SendActivityError, buildCollectionSynchronizationHeader, createExponentialBackoffPolicy, createFederation, createFederationBuilder, digest, handleWebFinger, respondWithObject, respondWithObjectIfAcceptable };