mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
Refactor PolicyWorker error handling
This commit is contained in:
parent
350671db47
commit
f50b41f22b
3 changed files with 67 additions and 46 deletions
|
|
@ -81,7 +81,7 @@ async function policyFilter(event: NostrEvent, signal: AbortSignal): Promise<voi
|
||||||
try {
|
try {
|
||||||
const result = await policyWorker.call(event, signal);
|
const result = await policyWorker.call(event, signal);
|
||||||
policyEventsCounter.inc({ ok: String(result[2]) });
|
policyEventsCounter.inc({ ok: String(result[2]) });
|
||||||
console.log(JSON.stringify(result));
|
console.debug(JSON.stringify(result));
|
||||||
RelayError.assert(result);
|
RelayError.assert(result);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof RelayError) {
|
if (e instanceof RelayError) {
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { NostrEvent, NostrRelayOK, NPolicy } from '@nostrify/nostrify';
|
||||||
import { Stickynotes } from '@soapbox/stickynotes';
|
import { Stickynotes } from '@soapbox/stickynotes';
|
||||||
import * as Comlink from 'comlink';
|
import * as Comlink from 'comlink';
|
||||||
|
|
||||||
|
|
@ -8,35 +9,67 @@ import '@/workers/handlers/abortsignal.ts';
|
||||||
|
|
||||||
const console = new Stickynotes('ditto:policy');
|
const console = new Stickynotes('ditto:policy');
|
||||||
|
|
||||||
export const policyWorker = Comlink.wrap<CustomPolicy>(
|
class PolicyWorker implements NPolicy {
|
||||||
new Worker(
|
private worker: Comlink.Remote<CustomPolicy>;
|
||||||
new URL('./policy.worker.ts', import.meta.url),
|
private ready: Promise<void>;
|
||||||
{
|
private enabled = true;
|
||||||
type: 'module',
|
|
||||||
deno: {
|
|
||||||
permissions: {
|
|
||||||
read: [Conf.denoDir, Conf.policy, Conf.dataDir],
|
|
||||||
write: [Conf.dataDir],
|
|
||||||
net: 'inherit',
|
|
||||||
env: false,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
try {
|
constructor() {
|
||||||
await policyWorker.init({
|
this.worker = Comlink.wrap<CustomPolicy>(
|
||||||
path: Conf.policy,
|
new Worker(
|
||||||
cwd: Deno.cwd(),
|
new URL('./policy.worker.ts', import.meta.url),
|
||||||
databaseUrl: Conf.databaseUrl,
|
{
|
||||||
adminPubkey: Conf.pubkey,
|
type: 'module',
|
||||||
});
|
deno: {
|
||||||
console.debug(`Using custom policy: ${Conf.policy}`);
|
permissions: {
|
||||||
} catch (e: any) {
|
read: [Conf.denoDir, Conf.policy, Conf.dataDir],
|
||||||
if (e.message.includes('Module not found')) {
|
write: [Conf.dataDir],
|
||||||
console.debug('Custom policy not found <https://docs.soapbox.pub/ditto/policies/>');
|
net: 'inherit',
|
||||||
} else {
|
env: false,
|
||||||
throw new Error(`DITTO_POLICY (error importing policy): ${Conf.policy}`, e);
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
this.ready = this.init();
|
||||||
|
}
|
||||||
|
|
||||||
|
async call(event: NostrEvent, signal?: AbortSignal): Promise<NostrRelayOK> {
|
||||||
|
await this.ready;
|
||||||
|
|
||||||
|
if (!this.enabled) {
|
||||||
|
return ['OK', event.id, true, ''];
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.worker.call(event, signal);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async init(): Promise<void> {
|
||||||
|
try {
|
||||||
|
await this.worker.init({
|
||||||
|
path: Conf.policy,
|
||||||
|
databaseUrl: Conf.databaseUrl,
|
||||||
|
adminPubkey: Conf.pubkey,
|
||||||
|
});
|
||||||
|
|
||||||
|
console.warn(`Using custom policy: ${Conf.policy}`);
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof Error && e.message.includes('Module not found')) {
|
||||||
|
console.warn('Custom policy not found <https://docs.soapbox.pub/ditto/policies/>');
|
||||||
|
this.enabled = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (e instanceof Error && e.message.includes('PGlite is not supported in worker threads')) {
|
||||||
|
console.warn('Custom policies are not supported with PGlite. The policy is disabled.');
|
||||||
|
this.enabled = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(`DITTO_POLICY (error importing policy): ${Conf.policy}`, e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const policyWorker = new PolicyWorker();
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import 'deno-safe-fetch/load';
|
import 'deno-safe-fetch/load';
|
||||||
import { NostrEvent, NostrRelayOK, NPolicy } from '@nostrify/nostrify';
|
import { NostrEvent, NostrRelayOK, NPolicy } from '@nostrify/nostrify';
|
||||||
import { NoOpPolicy, ReadOnlyPolicy } from '@nostrify/policies';
|
import { ReadOnlyPolicy } from '@nostrify/policies';
|
||||||
import * as Comlink from 'comlink';
|
import * as Comlink from 'comlink';
|
||||||
|
|
||||||
import { DittoDB } from '@/db/DittoDB.ts';
|
import { DittoDB } from '@/db/DittoDB.ts';
|
||||||
|
|
@ -15,8 +15,6 @@ Deno.env = new Map<string, string>();
|
||||||
interface PolicyInit {
|
interface PolicyInit {
|
||||||
/** Path to the policy module (https, jsr, file, etc) */
|
/** Path to the policy module (https, jsr, file, etc) */
|
||||||
path: string;
|
path: string;
|
||||||
/** Current working directory. */
|
|
||||||
cwd: string;
|
|
||||||
/** Database URL to connect to. */
|
/** Database URL to connect to. */
|
||||||
databaseUrl: string;
|
databaseUrl: string;
|
||||||
/** Admin pubkey to use for EventsDB checks. */
|
/** Admin pubkey to use for EventsDB checks. */
|
||||||
|
|
@ -31,10 +29,8 @@ export class CustomPolicy implements NPolicy {
|
||||||
return this.policy.call(event, signal);
|
return this.policy.call(event, signal);
|
||||||
}
|
}
|
||||||
|
|
||||||
async init({ path, cwd, databaseUrl, adminPubkey }: PolicyInit): Promise<void> {
|
async init({ path, databaseUrl, adminPubkey }: PolicyInit): Promise<void> {
|
||||||
// HACK: PGlite uses `path.resolve`, which requires read permission on Deno (which we don't want to give).
|
const Policy = (await import(path)).default;
|
||||||
// We can work around this getting the cwd from the caller and overwriting `Deno.cwd`.
|
|
||||||
Deno.cwd = () => cwd;
|
|
||||||
|
|
||||||
const { kysely } = DittoDB.create(databaseUrl, { poolSize: 1 });
|
const { kysely } = DittoDB.create(databaseUrl, { poolSize: 1 });
|
||||||
|
|
||||||
|
|
@ -44,15 +40,7 @@ export class CustomPolicy implements NPolicy {
|
||||||
timeout: 1_000,
|
timeout: 1_000,
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
this.policy = new Policy({ store });
|
||||||
const Policy = (await import(path)).default;
|
|
||||||
this.policy = new Policy({ store });
|
|
||||||
} catch (e: any) {
|
|
||||||
if (e.message.includes('Module not found')) {
|
|
||||||
this.policy = new NoOpPolicy();
|
|
||||||
}
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue