Merge remote-tracking branch 'origin/main' into refactor-subs

This commit is contained in:
Alex Gleason 2024-04-25 19:04:01 -05:00
commit b04d0c42e6
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 13 additions and 12 deletions

View File

@ -1,8 +1,7 @@
import { NostrEvent, NostrFilter } from '@nostrify/nostrify'; import { NostrEvent, NostrFilter, NSchema as n } from '@nostrify/nostrify';
import { relayInfoController } from '@/controllers/nostr/relay-info.ts'; import { relayInfoController } from '@/controllers/nostr/relay-info.ts';
import { eventsDB } from '@/storages.ts'; import { eventsDB } from '@/storages.ts';
import * as pipeline from '@/pipeline.ts'; import * as pipeline from '@/pipeline.ts';
import { jsonSchema } from '@/schema.ts';
import { import {
type ClientCLOSE, type ClientCLOSE,
type ClientCOUNT, type ClientCOUNT,
@ -14,6 +13,7 @@ import {
import { Storages } from '@/storages.ts'; import { Storages } from '@/storages.ts';
import type { AppController } from '@/app.ts'; import type { AppController } from '@/app.ts';
import { Conf } from '@/config.ts';
/** Limit of initial events returned for a subscription. */ /** Limit of initial events returned for a subscription. */
const FILTER_LIMIT = 100; const FILTER_LIMIT = 100;
@ -31,7 +31,7 @@ function connectStream(socket: WebSocket) {
const controllers = new Map<string, AbortController>(); const controllers = new Map<string, AbortController>();
socket.onmessage = (e) => { socket.onmessage = (e) => {
const result = jsonSchema.pipe(clientMsgSchema).safeParse(e.data); const result = n.json().pipe(clientMsgSchema).safeParse(e.data);
if (result.success) { if (result.success) {
handleMsg(result.data); handleMsg(result.data);
} else { } else {
@ -129,11 +129,12 @@ function connectStream(socket: WebSocket) {
/** Enforce the filters with certain criteria. */ /** Enforce the filters with certain criteria. */
function prepareFilters(filters: ClientREQ[2][]): NostrFilter[] { function prepareFilters(filters: ClientREQ[2][]): NostrFilter[] {
return filters.map((filter) => ({ return filters.map((filter) => {
...filter, const narrow = Boolean(filter.ids?.length || filter.authors?.length);
const search = narrow ? filter.search : `domain:${Conf.url.host} ${filter.search ?? ''}`;
// Return only local events unless the query is already narrow. // Return only local events unless the query is already narrow.
local: (filter.ids?.length || filter.authors?.length) ? undefined : true, return { ...filter, search };
})); });
} }
const relayController: AppController = (c, next) => { const relayController: AppController = (c, next) => {

View File

@ -45,14 +45,14 @@ const searchStore = new SearchStore({
}); });
export class Storages { export class Storages {
private static _subsub: InternalRelay | undefined; private static _pubsub: InternalRelay | undefined;
static get pubsub(): InternalRelay { static get pubsub(): InternalRelay {
if (!this._subsub) { if (!this._pubsub) {
this._subsub = new InternalRelay(); this._pubsub = new InternalRelay();
} }
return this._subsub; return this._pubsub;
} }
} }

View File

@ -23,7 +23,7 @@ export class InternalRelay implements NRelay {
async *req( async *req(
filters: NostrFilter[], filters: NostrFilter[],
opts: { signal?: AbortSignal }, opts?: { signal?: AbortSignal },
): AsyncGenerator<NostrRelayEVENT | NostrRelayEOSE | NostrRelayCLOSED> { ): AsyncGenerator<NostrRelayEVENT | NostrRelayEOSE | NostrRelayCLOSED> {
const id = crypto.randomUUID(); const id = crypto.randomUUID();
const machina = new Machina<NostrEvent>(opts?.signal); const machina = new Machina<NostrEvent>(opts?.signal);