mirror of
https://github.com/aljazceru/ditto.git
synced 2025-12-19 06:24:21 +01:00
Refactor PolicyWorker error handling
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { NostrEvent, NostrRelayOK, NPolicy } from '@nostrify/nostrify';
|
||||
import { Stickynotes } from '@soapbox/stickynotes';
|
||||
import * as Comlink from 'comlink';
|
||||
|
||||
@@ -8,35 +9,67 @@ import '@/workers/handlers/abortsignal.ts';
|
||||
|
||||
const console = new Stickynotes('ditto:policy');
|
||||
|
||||
export const policyWorker = Comlink.wrap<CustomPolicy>(
|
||||
new Worker(
|
||||
new URL('./policy.worker.ts', import.meta.url),
|
||||
{
|
||||
type: 'module',
|
||||
deno: {
|
||||
permissions: {
|
||||
read: [Conf.denoDir, Conf.policy, Conf.dataDir],
|
||||
write: [Conf.dataDir],
|
||||
net: 'inherit',
|
||||
env: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
),
|
||||
);
|
||||
class PolicyWorker implements NPolicy {
|
||||
private worker: Comlink.Remote<CustomPolicy>;
|
||||
private ready: Promise<void>;
|
||||
private enabled = true;
|
||||
|
||||
try {
|
||||
await policyWorker.init({
|
||||
path: Conf.policy,
|
||||
cwd: Deno.cwd(),
|
||||
databaseUrl: Conf.databaseUrl,
|
||||
adminPubkey: Conf.pubkey,
|
||||
});
|
||||
console.debug(`Using custom policy: ${Conf.policy}`);
|
||||
} catch (e: any) {
|
||||
if (e.message.includes('Module not found')) {
|
||||
console.debug('Custom policy not found <https://docs.soapbox.pub/ditto/policies/>');
|
||||
} else {
|
||||
throw new Error(`DITTO_POLICY (error importing policy): ${Conf.policy}`, e);
|
||||
constructor() {
|
||||
this.worker = Comlink.wrap<CustomPolicy>(
|
||||
new Worker(
|
||||
new URL('./policy.worker.ts', import.meta.url),
|
||||
{
|
||||
type: 'module',
|
||||
deno: {
|
||||
permissions: {
|
||||
read: [Conf.denoDir, Conf.policy, Conf.dataDir],
|
||||
write: [Conf.dataDir],
|
||||
net: 'inherit',
|
||||
env: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user