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