diff --git a/src/common.ts b/src/common.ts deleted file mode 100644 index 0424b52..0000000 --- a/src/common.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { Reqmeister } from '@/reqmeister.ts'; -import { Time } from '@/utils/time.ts'; - -const reqmeister = new Reqmeister({ - delay: Time.seconds(1), - signal: AbortSignal.timeout(Time.seconds(1)), -}); - -export { reqmeister }; diff --git a/src/db/events.ts b/src/db/events.ts index 6eae0f0..83551fb 100644 --- a/src/db/events.ts +++ b/src/db/events.ts @@ -30,7 +30,7 @@ const tagConditions: Record = { /** Insert an event (and its tags) into the database. */ function insertEvent(event: Event, data: EventData): Promise { - debug('insertEvent', event); + debug('insertEvent', JSON.stringify(event)); return db.transaction().execute(async (trx) => { /** Insert the event into the database. */ diff --git a/src/db/users.ts b/src/db/users.ts index 0e8d9a0..7d56e1c 100644 --- a/src/db/users.ts +++ b/src/db/users.ts @@ -1,7 +1,9 @@ -import { type Insertable } from '@/deps.ts'; +import { Debug, type Insertable } from '@/deps.ts'; import { db, type UserRow } from '../db.ts'; +const debug = Debug('ditto:users'); + interface User { pubkey: string; username: string; @@ -11,6 +13,7 @@ interface User { /** Adds a user to the database. */ function insertUser(user: Insertable) { + debug('insertUser', JSON.stringify(user)); return db.insertInto('users').values(user).execute(); } diff --git a/src/middleware/cache.ts b/src/middleware/cache.ts index 932632b..87de611 100644 --- a/src/middleware/cache.ts +++ b/src/middleware/cache.ts @@ -1,6 +1,7 @@ +import { Debug, type MiddlewareHandler } from '@/deps.ts'; import ExpiringCache from '@/utils/expiring-cache.ts'; -import type { MiddlewareHandler } from '@/deps.ts'; +const debug = Debug('ditto:middleware:cache'); export const cache = (options: { cacheName: string; @@ -11,14 +12,14 @@ export const cache = (options: { const cache = new ExpiringCache(await caches.open(options.cacheName)); const response = await cache.match(key); if (!response) { - console.debug('Building cache for page', c.req.url); + debug('Building cache for page', c.req.url); await next(); const response = c.res.clone(); if (response.status < 500) { await cache.putExpiring(key, response, options.expires ?? 0); } } else { - console.debug('Serving page from cache', c.req.url); + debug('Serving page from cache', c.req.url); return response; } }; diff --git a/src/pipeline.ts b/src/pipeline.ts index 2919573..3f4993b 100644 --- a/src/pipeline.ts +++ b/src/pipeline.ts @@ -1,4 +1,3 @@ -import { reqmeister } from '@/common.ts'; import { Conf } from '@/config.ts'; import * as eventsDB from '@/db/events.ts'; import { addRelays } from '@/db/relays.ts'; @@ -9,6 +8,7 @@ import { isEphemeralKind } from '@/kinds.ts'; import * as mixer from '@/mixer.ts'; import { publish } from '@/pool.ts'; import { isLocallyFollowed } from '@/queries.ts'; +import { reqmeister } from '@/reqmeister.ts'; import { updateStats } from '@/stats.ts'; import { Sub } from '@/subs.ts'; import { getTagSet } from '@/tags.ts'; diff --git a/src/queries.ts b/src/queries.ts index 1ecff7b..3af2253 100644 --- a/src/queries.ts +++ b/src/queries.ts @@ -2,7 +2,7 @@ import * as eventsDB from '@/db/events.ts'; import { type Event, findReplyTag } from '@/deps.ts'; import { type DittoFilter, type Relation } from '@/filter.ts'; import * as mixer from '@/mixer.ts'; -import { reqmeister } from '@/common.ts'; +import { reqmeister } from '@/reqmeister.ts'; interface GetEventOpts { /** Signal to abort the request. */ diff --git a/src/reqmeister.ts b/src/reqmeister.ts index 7830b84..811c6cc 100644 --- a/src/reqmeister.ts +++ b/src/reqmeister.ts @@ -1,7 +1,9 @@ import * as client from '@/client.ts'; -import { type Event, EventEmitter, type Filter } from '@/deps.ts'; - +import { Debug, type Event, EventEmitter, type Filter } from '@/deps.ts'; import { eventToMicroFilter, getFilterId, type MicroFilter } from '@/filter.ts'; +import { Time } from '@/utils/time.ts'; + +const debug = Debug('ditto:reqmeister'); interface ReqmeisterOpts { delay?: number; @@ -20,11 +22,11 @@ class Reqmeister extends EventEmitter<{ [filterId: string]: (event: Event) => an constructor(opts: ReqmeisterOpts = {}) { super(); this.#opts = opts; - this.#cycle(); + this.#tick(); this.#perform(); } - #cycle() { + #tick() { this.#resolve?.(); this.#promise = new Promise((resolve) => { this.#resolve = resolve; @@ -55,13 +57,16 @@ class Reqmeister extends EventEmitter<{ [filterId: string]: (event: Event) => an if (wantedEvents.size) filters.push({ ids: [...wantedEvents] }); if (wantedAuthors.size) filters.push({ kinds: [0], authors: [...wantedAuthors] }); - const events = await client.getFilters(filters, { signal: this.#opts.signal }); + if (filters.length) { + debug(JSON.stringify(filters)); + const events = await client.getFilters(filters, { signal: this.#opts.signal }); - for (const event of events) { - this.encounter(event); + for (const event of events) { + this.encounter(event); + } } - this.#cycle(); + this.#tick(); this.#perform(); } @@ -86,4 +91,9 @@ class Reqmeister extends EventEmitter<{ [filterId: string]: (event: Event) => an } } -export { Reqmeister }; +const reqmeister = new Reqmeister({ + delay: Time.seconds(1), + signal: AbortSignal.timeout(Time.seconds(1)), +}); + +export { reqmeister }; diff --git a/src/stats.ts b/src/stats.ts index 0e9c707..b462021 100644 --- a/src/stats.ts +++ b/src/stats.ts @@ -29,7 +29,7 @@ async function updateStats(event: Event) { const eventDiffs = statDiffs.filter(([table]) => table === 'event_stats') as EventStatDiff[]; if (statDiffs.length) { - debug({ id: event.id, pubkey: event.pubkey, kind: event.kind, tags: event.tags, statDiffs }); + debug(JSON.stringify({ id: event.id, pubkey: event.pubkey, kind: event.kind, tags: event.tags, statDiffs })); } if (pubkeyDiffs.length) queries.push(authorStatsQuery(pubkeyDiffs)); diff --git a/src/utils/nip05.ts b/src/utils/nip05.ts index df08150..1364c38 100644 --- a/src/utils/nip05.ts +++ b/src/utils/nip05.ts @@ -1,7 +1,9 @@ -import { TTLCache, z } from '@/deps.ts'; +import { Debug, TTLCache, z } from '@/deps.ts'; import { Time } from '@/utils/time.ts'; import { fetchWorker } from '@/workers/fetch.ts'; +const debug = Debug('ditto:nip05'); + const nip05Cache = new TTLCache>({ ttl: Time.hours(1), max: 5000 }); const NIP05_REGEX = /^(?:([\w.+-]+)@)?([\w.-]+)$/; @@ -46,7 +48,7 @@ function lookupNip05Cached(value: string): Promise { const cached = nip05Cache.get(value); if (cached !== undefined) return cached; - console.log(`Looking up NIP-05 for ${value}`); + debug(`Looking up NIP-05 for ${value}`); const result = lookup(value); nip05Cache.set(value, result); diff --git a/src/utils/unfurl.ts b/src/utils/unfurl.ts index 3088750..9f03c0f 100644 --- a/src/utils/unfurl.ts +++ b/src/utils/unfurl.ts @@ -1,7 +1,9 @@ -import { TTLCache, unfurl } from '@/deps.ts'; +import { Debug, TTLCache, unfurl } from '@/deps.ts'; import { Time } from '@/utils/time.ts'; import { fetchWorker } from '@/workers/fetch.ts'; +const debug = Debug('ditto:unfurl'); + interface PreviewCard { url: string; title: string; @@ -20,7 +22,7 @@ interface PreviewCard { } async function unfurlCard(url: string, signal: AbortSignal): Promise { - console.log(`Unfurling ${url}...`); + debug(`Unfurling ${url}...`); try { const result = await unfurl(url, { fetch: (url) => fetchWorker(url, { signal }),