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';
|
|
|
|
import type {
|
|
|
|
WrappedWorkerRequest,
|
|
|
|
WrappedWorkerResponse,
|
|
|
|
WrappedWorkerLogEntry,
|
|
|
|
} from './main';
|
2024-07-22 18:16:33 +00:00
|
|
|
import type { WritableDB } from './Interface';
|
2024-08-12 19:54:24 +00:00
|
|
|
import { initialize, DataReader, DataWriter, removeDB } from './Server';
|
2023-10-10 23:19:11 +00:00
|
|
|
import { SqliteErrorKind, parseSqliteError } from './errors';
|
2021-04-05 22:18:19 +00:00
|
|
|
|
|
|
|
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) {
|
2023-10-10 23:19:11 +00:00
|
|
|
let errorKind: SqliteErrorKind | undefined;
|
|
|
|
if (error !== undefined) {
|
|
|
|
errorKind = parseSqliteError(error);
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
if (errorKind === SqliteErrorKind.Corrupted && db != null) {
|
|
|
|
DataWriter.runCorruptionChecks(db);
|
2023-10-10 23:19:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 22:18:19 +00:00
|
|
|
const wrappedResponse: WrappedWorkerResponse = {
|
2021-09-16 21:54:06 +00:00
|
|
|
type: 'response',
|
2021-04-05 22:18:19 +00:00
|
|
|
seq,
|
2024-08-13 20:42:20 +00:00
|
|
|
error:
|
|
|
|
error == null
|
|
|
|
? undefined
|
|
|
|
: {
|
|
|
|
name: error.name,
|
|
|
|
message: error.message,
|
|
|
|
stack: error.stack,
|
|
|
|
},
|
2023-10-10 23:19:11 +00:00
|
|
|
errorKind,
|
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);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
let db: WritableDB | undefined;
|
|
|
|
let isPrimary = false;
|
|
|
|
let isRemoved = false;
|
|
|
|
|
|
|
|
port.on('message', ({ seq, request }: WrappedWorkerRequest) => {
|
2021-04-05 22:18:19 +00:00
|
|
|
try {
|
|
|
|
if (request.type === 'init') {
|
2024-07-22 18:16:33 +00:00
|
|
|
isPrimary = request.isPrimary;
|
|
|
|
isRemoved = false;
|
|
|
|
db = initialize({
|
2021-09-16 21:54:06 +00:00
|
|
|
...request.options,
|
2024-07-22 18:16:33 +00:00
|
|
|
isPrimary,
|
2021-09-16 21:54:06 +00:00
|
|
|
logger,
|
|
|
|
});
|
2021-04-05 22:18:19 +00:00
|
|
|
|
|
|
|
respond(seq, undefined, undefined);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
// 'close' is sent on shutdown, but we already removed the database.
|
|
|
|
if (isRemoved && request.type === 'close') {
|
|
|
|
respond(seq, undefined, undefined);
|
|
|
|
process.exit(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-12 19:54:24 +00:00
|
|
|
// Removing database does not require active connection.
|
|
|
|
if (request.type === 'removeDB') {
|
|
|
|
try {
|
|
|
|
if (db) {
|
|
|
|
if (isPrimary) {
|
|
|
|
DataWriter.close(db);
|
|
|
|
} else {
|
|
|
|
DataReader.close(db);
|
|
|
|
}
|
|
|
|
db = undefined;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
logger.error('Failed to close database before removal');
|
|
|
|
}
|
2024-07-22 18:16:33 +00:00
|
|
|
|
|
|
|
if (isPrimary) {
|
2024-08-12 19:54:24 +00:00
|
|
|
removeDB();
|
2024-07-22 18:16:33 +00:00
|
|
|
}
|
2024-08-12 19:54:24 +00:00
|
|
|
|
|
|
|
isRemoved = true;
|
2021-04-05 22:18:19 +00:00
|
|
|
|
|
|
|
respond(seq, undefined, undefined);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-12 19:54:24 +00:00
|
|
|
if (!db) {
|
|
|
|
throw new Error('Not initialized');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request.type === 'close') {
|
2024-07-22 18:16:33 +00:00
|
|
|
if (isPrimary) {
|
2024-08-12 19:54:24 +00:00
|
|
|
DataWriter.close(db);
|
2024-07-22 18:16:33 +00:00
|
|
|
} else {
|
|
|
|
DataReader.close(db);
|
|
|
|
}
|
|
|
|
db = undefined;
|
2021-07-10 00:43:36 +00:00
|
|
|
|
|
|
|
respond(seq, undefined, undefined);
|
2024-08-12 19:54:24 +00:00
|
|
|
process.exit(0);
|
2021-07-10 00:43:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
if (request.type === 'sqlCall:read' || request.type === 'sqlCall:write') {
|
|
|
|
const DataInterface =
|
|
|
|
request.type === 'sqlCall:read' ? DataReader : DataWriter;
|
2021-04-05 22:18:19 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2024-07-22 18:16:33 +00:00
|
|
|
const method = (DataInterface as any)[request.method];
|
2021-04-05 22:18:19 +00:00
|
|
|
if (typeof method !== 'function') {
|
2024-09-06 17:52:19 +00:00
|
|
|
throw new Error(`Invalid sql method: ${request.method} ${method}`);
|
2021-04-05 22:18:19 +00:00
|
|
|
}
|
|
|
|
|
2023-07-15 00:52:20 +00:00
|
|
|
const start = performance.now();
|
2024-07-22 18:16:33 +00:00
|
|
|
const result = method(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);
|
|
|
|
}
|
|
|
|
});
|