2021-04-05 22:18:19 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { parentPort } from 'worker_threads';
|
|
|
|
|
2021-09-16 21:54:06 +00:00
|
|
|
import type { LoggerType } from '../types/Logging';
|
2022-11-22 18:43:43 +00:00
|
|
|
import * as Errors from '../types/errors';
|
2021-09-16 21:54:06 +00:00
|
|
|
import type {
|
|
|
|
WrappedWorkerRequest,
|
|
|
|
WrappedWorkerResponse,
|
|
|
|
WrappedWorkerLogEntry,
|
|
|
|
} from './main';
|
2021-04-05 22:18:19 +00:00
|
|
|
import db from './Server';
|
|
|
|
|
|
|
|
if (!parentPort) {
|
|
|
|
throw new Error('Must run as a worker thread');
|
|
|
|
}
|
|
|
|
|
|
|
|
const port = parentPort;
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
function respond(seq: number, error: Error | undefined, response?: any) {
|
|
|
|
const wrappedResponse: WrappedWorkerResponse = {
|
2021-09-16 21:54:06 +00:00
|
|
|
type: 'response',
|
2021-04-05 22:18:19 +00:00
|
|
|
seq,
|
2022-11-22 18:43:43 +00:00
|
|
|
error: error === undefined ? undefined : Errors.toLogFormat(error),
|
2021-04-05 22:18:19 +00:00
|
|
|
response,
|
|
|
|
};
|
|
|
|
port.postMessage(wrappedResponse);
|
|
|
|
}
|
|
|
|
|
2021-09-16 21:54:06 +00:00
|
|
|
const log = (
|
|
|
|
level: WrappedWorkerLogEntry['level'],
|
|
|
|
args: Array<unknown>
|
|
|
|
): void => {
|
|
|
|
const wrappedResponse: WrappedWorkerResponse = {
|
|
|
|
type: 'log',
|
|
|
|
level,
|
|
|
|
args,
|
|
|
|
};
|
|
|
|
port.postMessage(wrappedResponse);
|
|
|
|
};
|
|
|
|
|
|
|
|
const logger: LoggerType = {
|
|
|
|
fatal(...args: Array<unknown>) {
|
|
|
|
log('fatal', args);
|
|
|
|
},
|
|
|
|
error(...args: Array<unknown>) {
|
|
|
|
log('error', args);
|
|
|
|
},
|
|
|
|
warn(...args: Array<unknown>) {
|
|
|
|
log('warn', args);
|
|
|
|
},
|
|
|
|
info(...args: Array<unknown>) {
|
|
|
|
log('info', args);
|
|
|
|
},
|
|
|
|
debug(...args: Array<unknown>) {
|
|
|
|
log('debug', args);
|
|
|
|
},
|
|
|
|
trace(...args: Array<unknown>) {
|
|
|
|
log('trace', args);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-04-05 22:18:19 +00:00
|
|
|
port.on('message', async ({ seq, request }: WrappedWorkerRequest) => {
|
|
|
|
try {
|
|
|
|
if (request.type === 'init') {
|
2021-09-16 21:54:06 +00:00
|
|
|
await db.initialize({
|
|
|
|
...request.options,
|
|
|
|
logger,
|
|
|
|
});
|
2021-04-05 22:18:19 +00:00
|
|
|
|
|
|
|
respond(seq, undefined, undefined);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request.type === 'close') {
|
|
|
|
await db.close();
|
|
|
|
|
|
|
|
respond(seq, undefined, undefined);
|
|
|
|
process.exit(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-10 00:43:36 +00:00
|
|
|
if (request.type === 'removeDB') {
|
|
|
|
await db.removeDB();
|
|
|
|
|
|
|
|
respond(seq, undefined, undefined);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-05 22:18:19 +00:00
|
|
|
if (request.type === 'sqlCall') {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
const method = (db as any)[request.method];
|
|
|
|
if (typeof method !== 'function') {
|
|
|
|
throw new Error(`Invalid sql method: ${method}`);
|
|
|
|
}
|
|
|
|
|
2023-07-15 00:52:20 +00:00
|
|
|
const start = performance.now();
|
2021-04-26 22:01:22 +00:00
|
|
|
const result = await method.apply(db, request.args);
|
2023-07-15 00:52:20 +00:00
|
|
|
const end = performance.now();
|
2021-04-26 22:01:22 +00:00
|
|
|
|
|
|
|
respond(seq, undefined, { result, duration: end - start });
|
2021-04-05 22:18:19 +00:00
|
|
|
} else {
|
|
|
|
throw new Error('Unexpected request type');
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
respond(seq, error, undefined);
|
|
|
|
}
|
|
|
|
});
|