mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
DittoPglite: prevent starting PGlite instances in worker threads
This commit is contained in:
parent
606aeb3236
commit
350671db47
3 changed files with 52 additions and 6 deletions
|
|
@ -6,16 +6,23 @@ import { Kysely } from 'kysely';
|
||||||
import { DittoDatabase, DittoDatabaseOpts } from '@/db/DittoDatabase.ts';
|
import { DittoDatabase, DittoDatabaseOpts } from '@/db/DittoDatabase.ts';
|
||||||
import { DittoTables } from '@/db/DittoTables.ts';
|
import { DittoTables } from '@/db/DittoTables.ts';
|
||||||
import { KyselyLogger } from '@/db/KyselyLogger.ts';
|
import { KyselyLogger } from '@/db/KyselyLogger.ts';
|
||||||
|
import { isWorker } from '@/utils/worker.ts';
|
||||||
|
|
||||||
export class DittoPglite {
|
export class DittoPglite {
|
||||||
static create(databaseUrl: string, opts?: DittoDatabaseOpts): DittoDatabase {
|
static create(databaseUrl: string, opts?: DittoDatabaseOpts): DittoDatabase {
|
||||||
|
const url = new URL(databaseUrl);
|
||||||
|
|
||||||
|
if (url.protocol === 'file:' && isWorker()) {
|
||||||
|
throw new Error('PGlite is not supported in worker threads.');
|
||||||
|
}
|
||||||
|
|
||||||
|
const pglite = new PGlite(databaseUrl, {
|
||||||
|
extensions: { pg_trgm },
|
||||||
|
debug: opts?.debug,
|
||||||
|
});
|
||||||
|
|
||||||
const kysely = new Kysely<DittoTables>({
|
const kysely = new Kysely<DittoTables>({
|
||||||
dialect: new PgliteDialect({
|
dialect: new PgliteDialect({ database: pglite }),
|
||||||
database: new PGlite(databaseUrl, {
|
|
||||||
extensions: { pg_trgm },
|
|
||||||
debug: opts?.debug,
|
|
||||||
}),
|
|
||||||
}),
|
|
||||||
log: KyselyLogger,
|
log: KyselyLogger,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
30
src/utils/worker.test.ts
Normal file
30
src/utils/worker.test.ts
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
import { assertEquals } from '@std/assert';
|
||||||
|
|
||||||
|
import { isWorker } from '@/utils/worker.ts';
|
||||||
|
|
||||||
|
Deno.test('isWorker from the main thread returns false', () => {
|
||||||
|
assertEquals(isWorker(), false);
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test('isWorker from a worker thread returns true', async () => {
|
||||||
|
const script = `
|
||||||
|
import { isWorker } from '@/utils/worker.ts';
|
||||||
|
postMessage(isWorker());
|
||||||
|
self.close();
|
||||||
|
`;
|
||||||
|
|
||||||
|
const worker = new Worker(
|
||||||
|
URL.createObjectURL(new Blob([script], { type: 'application/javascript' })),
|
||||||
|
{ type: 'module' },
|
||||||
|
);
|
||||||
|
|
||||||
|
const result = await new Promise<boolean>((resolve) => {
|
||||||
|
worker.onmessage = (e) => {
|
||||||
|
resolve(e.data);
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
worker.terminate();
|
||||||
|
|
||||||
|
assertEquals(result, true);
|
||||||
|
});
|
||||||
9
src/utils/worker.ts
Normal file
9
src/utils/worker.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
/**
|
||||||
|
* Detect if this thread is running in a Worker context.
|
||||||
|
*
|
||||||
|
* https://stackoverflow.com/a/18002694
|
||||||
|
*/
|
||||||
|
export function isWorker(): boolean {
|
||||||
|
// @ts-ignore This is fine.
|
||||||
|
return typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope;
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue