reqmeister: middleware/cache, reqmeister, nip05, unfurl, refactor some code
This commit is contained in:
parent
e121a8805e
commit
2fc9988c06
|
@ -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 };
|
|
@ -30,7 +30,7 @@ const tagConditions: Record<string, TagCondition> = {
|
|||
|
||||
/** Insert an event (and its tags) into the database. */
|
||||
function insertEvent(event: Event, data: EventData): Promise<void> {
|
||||
debug('insertEvent', event);
|
||||
debug('insertEvent', JSON.stringify(event));
|
||||
|
||||
return db.transaction().execute(async (trx) => {
|
||||
/** Insert the event into the database. */
|
||||
|
|
|
@ -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<UserRow>) {
|
||||
debug('insertUser', JSON.stringify(user));
|
||||
return db.insertInto('users').values(user).execute();
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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<K extends number> {
|
||||
/** Signal to abort the request. */
|
||||
|
|
|
@ -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 };
|
||||
|
|
|
@ -29,7 +29,7 @@ async function updateStats<K extends number>(event: Event<K>) {
|
|||
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));
|
||||
|
|
|
@ -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<string, Promise<string | null>>({ ttl: Time.hours(1), max: 5000 });
|
||||
|
||||
const NIP05_REGEX = /^(?:([\w.+-]+)@)?([\w.-]+)$/;
|
||||
|
@ -46,7 +48,7 @@ function lookupNip05Cached(value: string): Promise<string | null> {
|
|||
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);
|
||||
|
||||
|
|
|
@ -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<PreviewCard | null> {
|
||||
console.log(`Unfurling ${url}...`);
|
||||
debug(`Unfurling ${url}...`);
|
||||
try {
|
||||
const result = await unfurl(url, {
|
||||
fetch: (url) => fetchWorker(url, { signal }),
|
||||
|
|
Loading…
Reference in New Issue