mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 03:19:46 +00:00
Export PolicyWorker as a regular class
This commit is contained in:
parent
3b17fd9b45
commit
f2e2072184
3 changed files with 19 additions and 16 deletions
|
|
@ -33,7 +33,7 @@ import { getAmount } from '@/utils/bolt11.ts';
|
|||
import { errorJson } from '@/utils/log.ts';
|
||||
import { purifyEvent } from '@/utils/purify.ts';
|
||||
import { getTagSet } from '@/utils/tags.ts';
|
||||
import { policyWorker } from '@/workers/policy.ts';
|
||||
import { PolicyWorker } from '@/workers/policy.ts';
|
||||
import { verifyEventWorker } from '@/workers/verify.ts';
|
||||
import { fetchFavicon, insertFavicon, queryFavicon } from '@/utils/favicon.ts';
|
||||
import { lookupNip05 } from '@/utils/nip05.ts';
|
||||
|
|
@ -54,6 +54,7 @@ export class DittoAPIStore implements NRelay {
|
|||
private push: DittoPush;
|
||||
private encounters = new LRUCache<string, true>({ max: 5000 });
|
||||
private controller = new AbortController();
|
||||
private policyWorker: PolicyWorker;
|
||||
|
||||
private faviconCache: SimpleLRU<string, URL>;
|
||||
private nip05Cache: SimpleLRU<string, nip19.ProfilePointer>;
|
||||
|
|
@ -64,6 +65,7 @@ export class DittoAPIStore implements NRelay {
|
|||
const { conf, db } = this.opts;
|
||||
|
||||
this.push = new DittoPush(opts);
|
||||
this.policyWorker = new PolicyWorker(conf);
|
||||
|
||||
this.listen().catch((e: unknown) => {
|
||||
logi({ level: 'error', ns: this.ns, source: 'listen', error: errorJson(e) });
|
||||
|
|
@ -211,7 +213,7 @@ export class DittoAPIStore implements NRelay {
|
|||
|
||||
private async policyFilter(event: NostrEvent, signal?: AbortSignal): Promise<void> {
|
||||
try {
|
||||
const result = await policyWorker.call(event, signal);
|
||||
const result = await this.policyWorker.call(event, signal);
|
||||
const [, , ok, reason] = result;
|
||||
logi({ level: 'debug', ns: 'ditto.policy', id: event.id, kind: event.kind, ok, reason });
|
||||
policyEventsCounter.inc({ ok: String(ok) });
|
||||
|
|
|
|||
|
|
@ -1,16 +1,16 @@
|
|||
import { DittoConf } from '@ditto/conf';
|
||||
import { NostrEvent, NostrRelayOK, NPolicy } from '@nostrify/nostrify';
|
||||
import { logi } from '@soapbox/logi';
|
||||
import * as Comlink from 'comlink';
|
||||
|
||||
import { Conf } from '@/config.ts';
|
||||
import type { CustomPolicy } from '@/workers/policy.worker.ts';
|
||||
|
||||
class PolicyWorker implements NPolicy {
|
||||
export class PolicyWorker implements NPolicy {
|
||||
private worker: Comlink.Remote<CustomPolicy>;
|
||||
private ready: Promise<void>;
|
||||
private enabled = true;
|
||||
|
||||
constructor() {
|
||||
constructor(private conf: DittoConf) {
|
||||
this.worker = Comlink.wrap<CustomPolicy>(
|
||||
new Worker(
|
||||
new URL('./policy.worker.ts', import.meta.url),
|
||||
|
|
@ -19,8 +19,8 @@ class PolicyWorker implements NPolicy {
|
|||
name: 'PolicyWorker',
|
||||
deno: {
|
||||
permissions: {
|
||||
read: [Conf.denoDir, Conf.policy, Conf.dataDir],
|
||||
write: [Conf.dataDir],
|
||||
read: [conf.denoDir, conf.policy, conf.dataDir],
|
||||
write: [conf.dataDir],
|
||||
net: 'inherit',
|
||||
env: false,
|
||||
import: true,
|
||||
|
|
@ -44,18 +44,20 @@ class PolicyWorker implements NPolicy {
|
|||
}
|
||||
|
||||
private async init(): Promise<void> {
|
||||
const conf = this.conf;
|
||||
|
||||
try {
|
||||
await this.worker.init({
|
||||
path: Conf.policy,
|
||||
databaseUrl: Conf.databaseUrl,
|
||||
pubkey: await Conf.signer.getPublicKey(),
|
||||
path: conf.policy,
|
||||
databaseUrl: conf.databaseUrl,
|
||||
pubkey: await conf.signer.getPublicKey(),
|
||||
});
|
||||
|
||||
logi({
|
||||
level: 'info',
|
||||
ns: 'ditto.system.policy',
|
||||
msg: 'Using custom policy',
|
||||
path: Conf.policy,
|
||||
path: conf.policy,
|
||||
enabled: true,
|
||||
});
|
||||
} catch (e) {
|
||||
|
|
@ -76,16 +78,14 @@ class PolicyWorker implements NPolicy {
|
|||
level: 'warn',
|
||||
ns: 'ditto.system.policy',
|
||||
msg: 'Custom policies are not supported with PGlite. The policy is disabled.',
|
||||
path: Conf.policy,
|
||||
path: conf.policy,
|
||||
enabled: false,
|
||||
});
|
||||
this.enabled = false;
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Error(`DITTO_POLICY (error importing policy): ${Conf.policy}`);
|
||||
throw new Error(`DITTO_POLICY (error importing policy): ${conf.policy}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const policyWorker = new PolicyWorker();
|
||||
|
|
|
|||
|
|
@ -2,11 +2,12 @@ import { DittoConf } from '@ditto/conf';
|
|||
import { DittoPolyPg } from '@ditto/db';
|
||||
|
||||
import { DittoPgStore } from '../packages/ditto/storages/DittoPgStore.ts';
|
||||
import { policyWorker } from '../packages/ditto/workers/policy.ts';
|
||||
import { PolicyWorker } from '../packages/ditto/workers/policy.ts';
|
||||
|
||||
const conf = new DittoConf(Deno.env);
|
||||
const db = new DittoPolyPg(conf.databaseUrl);
|
||||
const relay = new DittoPgStore({ db, pubkey: await conf.signer.getPublicKey() });
|
||||
const policyWorker = new PolicyWorker(conf);
|
||||
|
||||
let count = 0;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue