2021-04-05 22:18:19 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-04-26 22:01:22 +00:00
|
|
|
/* eslint-disable no-console */
|
|
|
|
|
2021-04-05 22:18:19 +00:00
|
|
|
import { join } from 'path';
|
|
|
|
import { Worker } from 'worker_threads';
|
|
|
|
|
2021-09-08 20:39:14 +00:00
|
|
|
import { explodePromise } from '../util/explodePromise';
|
|
|
|
import { isCorruptionError } from './errors';
|
|
|
|
|
2021-04-14 19:43:11 +00:00
|
|
|
const ASAR_PATTERN = /app\.asar$/;
|
2021-08-09 22:04:17 +00:00
|
|
|
const MIN_TRACE_DURATION = 40;
|
2021-04-14 19:43:11 +00:00
|
|
|
|
2021-04-05 22:18:19 +00:00
|
|
|
export type InitializeOptions = {
|
|
|
|
readonly configDir: string;
|
|
|
|
readonly key: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type WorkerRequest =
|
|
|
|
| {
|
|
|
|
readonly type: 'init';
|
|
|
|
readonly options: InitializeOptions;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
readonly type: 'close';
|
|
|
|
}
|
2021-07-10 00:43:36 +00:00
|
|
|
| {
|
|
|
|
readonly type: 'removeDB';
|
|
|
|
}
|
2021-04-05 22:18:19 +00:00
|
|
|
| {
|
|
|
|
readonly type: 'sqlCall';
|
|
|
|
readonly method: string;
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
readonly args: ReadonlyArray<any>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type WrappedWorkerRequest = {
|
|
|
|
readonly seq: number;
|
|
|
|
readonly request: WorkerRequest;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type WrappedWorkerResponse = {
|
|
|
|
readonly seq: number;
|
|
|
|
readonly error: string | undefined;
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
readonly response: any;
|
|
|
|
};
|
|
|
|
|
|
|
|
type PromisePair<T> = {
|
|
|
|
resolve: (response: T) => void;
|
|
|
|
reject: (error: Error) => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
export class MainSQL {
|
|
|
|
private readonly worker: Worker;
|
|
|
|
|
2021-04-08 23:27:41 +00:00
|
|
|
private isReady = false;
|
|
|
|
|
|
|
|
private onReady: Promise<void> | undefined;
|
|
|
|
|
2021-04-05 22:18:19 +00:00
|
|
|
private readonly onExit: Promise<void>;
|
|
|
|
|
2021-09-08 20:39:14 +00:00
|
|
|
// This promise is resolved when any of the queries that we run against the
|
|
|
|
// database reject with a corruption error (see `isCorruptionError`)
|
|
|
|
private readonly onCorruption: Promise<Error>;
|
|
|
|
|
2021-04-05 22:18:19 +00:00
|
|
|
private seq = 0;
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
private onResponse = new Map<number, PromisePair<any>>();
|
|
|
|
|
|
|
|
constructor() {
|
2021-04-14 19:43:11 +00:00
|
|
|
let appDir = join(__dirname, '..', '..');
|
|
|
|
let isBundled = false;
|
2021-04-05 22:18:19 +00:00
|
|
|
|
2021-04-14 19:43:11 +00:00
|
|
|
if (ASAR_PATTERN.test(appDir)) {
|
|
|
|
appDir = appDir.replace(ASAR_PATTERN, 'app.asar.unpacked');
|
|
|
|
isBundled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const scriptDir = join(appDir, 'ts', 'sql');
|
|
|
|
this.worker = new Worker(
|
|
|
|
join(scriptDir, isBundled ? 'mainWorker.bundle.js' : 'mainWorker.js')
|
|
|
|
);
|
2021-04-05 22:18:19 +00:00
|
|
|
|
2021-09-08 20:39:14 +00:00
|
|
|
const {
|
|
|
|
promise: onCorruption,
|
|
|
|
resolve: resolveCorruption,
|
|
|
|
} = explodePromise<Error>();
|
|
|
|
this.onCorruption = onCorruption;
|
|
|
|
|
2021-04-05 22:18:19 +00:00
|
|
|
this.worker.on('message', (wrappedResponse: WrappedWorkerResponse) => {
|
|
|
|
const { seq, error, response } = wrappedResponse;
|
|
|
|
|
|
|
|
const pair = this.onResponse.get(seq);
|
|
|
|
this.onResponse.delete(seq);
|
|
|
|
if (!pair) {
|
|
|
|
throw new Error(`Unexpected worker response with seq: ${seq}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
2021-09-08 20:39:14 +00:00
|
|
|
const errorObj = new Error(error);
|
|
|
|
if (isCorruptionError(errorObj)) {
|
|
|
|
resolveCorruption(errorObj);
|
|
|
|
}
|
|
|
|
|
|
|
|
pair.reject(errorObj);
|
2021-04-05 22:18:19 +00:00
|
|
|
} else {
|
|
|
|
pair.resolve(response);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.onExit = new Promise<void>(resolve => {
|
|
|
|
this.worker.once('exit', resolve);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public async initialize(options: InitializeOptions): Promise<void> {
|
2021-04-08 23:27:41 +00:00
|
|
|
if (this.isReady || this.onReady) {
|
|
|
|
throw new Error('Already initialized');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.onReady = this.send({ type: 'init', options });
|
|
|
|
|
|
|
|
await this.onReady;
|
|
|
|
|
|
|
|
this.onReady = undefined;
|
|
|
|
this.isReady = true;
|
2021-04-05 22:18:19 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 20:39:14 +00:00
|
|
|
public whenCorrupted(): Promise<Error> {
|
|
|
|
return this.onCorruption;
|
|
|
|
}
|
|
|
|
|
2021-04-05 22:18:19 +00:00
|
|
|
public async close(): Promise<void> {
|
2021-04-08 23:27:41 +00:00
|
|
|
if (!this.isReady) {
|
|
|
|
throw new Error('Not initialized');
|
|
|
|
}
|
|
|
|
|
2021-04-05 22:18:19 +00:00
|
|
|
await this.send({ type: 'close' });
|
|
|
|
await this.onExit;
|
|
|
|
}
|
|
|
|
|
2021-07-10 00:43:36 +00:00
|
|
|
public async removeDB(): Promise<void> {
|
|
|
|
await this.send({ type: 'removeDB' });
|
|
|
|
}
|
|
|
|
|
2021-04-05 22:18:19 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
public async sqlCall(method: string, args: ReadonlyArray<any>): Promise<any> {
|
2021-04-08 23:27:41 +00:00
|
|
|
if (this.onReady) {
|
|
|
|
await this.onReady;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.isReady) {
|
|
|
|
throw new Error('Not initialized');
|
|
|
|
}
|
|
|
|
|
2021-04-26 22:01:22 +00:00
|
|
|
const { result, duration } = await this.send({
|
|
|
|
type: 'sqlCall',
|
|
|
|
method,
|
|
|
|
args,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (duration > MIN_TRACE_DURATION) {
|
|
|
|
console.log(`ts/sql/main: slow query ${method} duration=${duration}ms`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2021-04-05 22:18:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private async send<Response>(request: WorkerRequest): Promise<Response> {
|
|
|
|
const { seq } = this;
|
|
|
|
this.seq += 1;
|
|
|
|
|
|
|
|
const result = new Promise<Response>((resolve, reject) => {
|
|
|
|
this.onResponse.set(seq, { resolve, reject });
|
|
|
|
});
|
|
|
|
|
|
|
|
const wrappedRequest: WrappedWorkerRequest = {
|
|
|
|
seq,
|
|
|
|
request,
|
|
|
|
};
|
|
|
|
this.worker.postMessage(wrappedRequest);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|