move worker_interface and logs and i18n to TS

pull/2239/head
Audric Ackermann 3 years ago
parent 0dfa3e35cc
commit ff43cfa593

@ -2,7 +2,7 @@
const { ipcRenderer } = require('electron'); const { ipcRenderer } = require('electron');
const url = require('url'); const url = require('url');
const i18n = require('./js/modules/i18n'); const i18n = require('./ts/util/i18n');
const config = url.parse(window.location.toString(), true).query; const config = url.parse(window.location.toString(), true).query;
const { locale } = config; const { locale } = config;
@ -18,6 +18,6 @@ window.getAppInstance = () => config.appInstance;
window.closeAbout = () => ipcRenderer.send('close-about'); window.closeAbout = () => ipcRenderer.send('close-about');
window.i18n = i18n.setup(locale, localeMessages); window.i18n = i18n.setupi18n(locale, localeMessages);
require('./ts/util/logging'); require('./ts/util/logging');

@ -2,7 +2,7 @@
const { ipcRenderer } = require('electron'); const { ipcRenderer } = require('electron');
const url = require('url'); const url = require('url');
const i18n = require('./js/modules/i18n'); const i18n = require('./ts/util/i18n');
const { DebugLogView } = require('./ts/views/DebugLogView'); const { DebugLogView } = require('./ts/views/DebugLogView');
const config = url.parse(window.location.toString(), true).query; const config = url.parse(window.location.toString(), true).query;
@ -19,7 +19,7 @@ window.ReactDOM = require('react-dom');
window.getVersion = () => config.version; window.getVersion = () => config.version;
window.theme = config.theme; window.theme = config.theme;
window.i18n = i18n.setup(locale, localeMessages); window.i18n = i18n.setupi18n(locale, localeMessages);
// got.js appears to need this to successfully submit debug logs to the cloud // got.js appears to need this to successfully submit debug logs to the cloud
window.nodeSetImmediate = setImmediate; window.nodeSetImmediate = setImmediate;

@ -1,65 +1,6 @@
"use strict"; "use strict";
/* global Whisper, Backbone, Mustache, _, $ */
/*
* Whisper.View
*
* This is the base for most of our views. The Backbone view is extended
* with some conveniences:
*
* 1. Pre-parses all our mustache templates for performance.
* https://github.com/janl/mustache.js#pre-parsing-and-caching-templates
*
* 2. Defines a default definition for render() which allows sub-classes
* to simply specify a templateName and renderAttributes which are plugged
* into Mustache.render
*
* 3. Makes all the templates available for rendering as partials.
* https://github.com/janl/mustache.js#partials
*
* 4. Provides some common functionality, e.g. confirmation dialog
*
*/
// eslint-disable-next-line func-names
(function () { (function () {
'use strict'; 'use strict';
window.Whisper = window.Whisper || {}; window.Whisper = window.Whisper || {};
// window.Whisper.View = Backbone.View.extend( })();
// { //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoid2hpc3Blcl92aWV3LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsid2hpc3Blcl92aWV3LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUF1QkEsQ0FBQztJQUNDLFlBQVksQ0FBQztJQUViLE1BQU0sQ0FBQyxPQUFPLEdBQUcsTUFBTSxDQUFDLE9BQU8sSUFBSSxFQUFFLENBQUM7QUF5Q3hDLENBQUMsQ0FBQyxFQUFFLENBQUMifQ==
// constructor(...params: any) {
// Backbone.View.call(this, ...params);
// Mustache.parse(_.result(this, 'template'));
// },
// render_attributes() {
// return _.result(this.model, 'attributes', {});
// },
// render_partials() {
// return window.Whisper.View.Templates;
// },
// template() {
// if (this.templateName) {
// return window.Whisper.View.Templates[this.templateName];
// }
// return '';
// },
// render() {
// const attrs = _.result(this, 'render_attributes', {});
// const template = _.result(this, 'template', '');
// const partials = _.result(this, 'render_partials', '');
// this.$el.html(Mustache.render(template, attrs, partials));
// return this;
// },
// },
// {
// // Class attributes
// Templates: (() => {
// const templates = {};
// $('script[type="text/x-tmpl-mustache"]').each((i, el) => {
// const $el = $(el);
// const id = $el.attr('id');
// templates[id] = $el.html();
// });
// return templates;
// })(),
// }
// );
})();

@ -2,7 +2,7 @@
const { ipcRenderer } = require('electron'); const { ipcRenderer } = require('electron');
const url = require('url'); const url = require('url');
const i18n = require('./js/modules/i18n'); const i18n = require('./ts/util/i18n');
const config = url.parse(window.location.toString(), true).query; const config = url.parse(window.location.toString(), true).query;
const { locale } = config; const { locale } = config;
@ -15,7 +15,7 @@ window.React = require('react');
window.ReactDOM = require('react-dom'); window.ReactDOM = require('react-dom');
window.theme = config.theme; window.theme = config.theme;
window.i18n = i18n.setup(locale, localeMessages); window.i18n = i18n.setupi18n(locale, localeMessages);
window.getEnvironment = () => config.environment; window.getEnvironment = () => config.environment;
window.getVersion = () => config.version; window.getVersion = () => config.version;
@ -29,8 +29,6 @@ window.Signal = {
}, },
}; };
window.Signal.Logs = require('./js/modules/logs');
window.clearLocalData = async () => { window.clearLocalData = async () => {
window.log.info('reset database'); window.log.info('reset database');
ipcRenderer.send('resetDatabase'); ipcRenderer.send('resetDatabase');

@ -183,13 +183,13 @@ if (config.proxyUrl) {
window.nodeSetImmediate = setImmediate; window.nodeSetImmediate = setImmediate;
const Signal = require('./js/modules/signal'); const Signal = require('./js/modules/signal');
const i18n = require('./js/modules/i18n'); const i18n = require('./ts/util/i18n');
window.Signal = Signal.setup(); window.Signal = Signal.setup();
window.getSwarmPollingInstance = require('./ts/session/apis/snode_api').getSwarmPollingInstance; window.getSwarmPollingInstance = require('./ts/session/apis/snode_api').getSwarmPollingInstance;
const WorkerInterface = require('./js/modules/util_worker_interface'); const WorkerInterface = require('./ts/node/util_worker_interface');
// A Worker with a 3 minute timeout // A Worker with a 3 minute timeout
const utilWorkerPath = path.join(app.getAppPath(), 'js', 'util_worker.js'); const utilWorkerPath = path.join(app.getAppPath(), 'js', 'util_worker.js');
@ -222,14 +222,12 @@ window.getSeedNodeList = () => [
]; ];
const { locale: localFromEnv } = config; const { locale: localFromEnv } = config;
window.i18n = i18n.setup(localFromEnv, localeMessages); window.i18n = i18n.setupi18n(localFromEnv, localeMessages);
window.moment = require('moment'); window.moment = require('moment');
window.libsession = require('./ts/session'); window.libsession = require('./ts/session');
window.Signal.Data = require('./ts/data/data'); window.Signal.Data = require('./ts/data/data');
window.Signal.Logs = require('./js/modules/logs');
window.addEventListener('contextmenu', e => { window.addEventListener('contextmenu', e => {
const editable = e.target.closest('textarea, input, [contenteditable="true"]'); const editable = e.target.closest('textarea, input, [contenteditable="true"]');
const link = e.target.closest('a'); const link = e.target.closest('a');

@ -11,11 +11,12 @@ import { SessionSpinner } from '../basic/SessionSpinner';
import { SessionWrapperModal } from '../SessionWrapperModal'; import { SessionWrapperModal } from '../SessionWrapperModal';
import * as Data from '../../data/data'; import * as Data from '../../data/data';
import { deleteAllLogs } from '../../node/logs';
const deleteDbLocally = async () => { const deleteDbLocally = async () => {
window?.log?.info('last message sent successfully. Deleting everything'); window?.log?.info('last message sent successfully. Deleting everything');
window.persistStore?.purge(); window.persistStore?.purge();
await window.Signal.Logs.deleteAll(); await deleteAllLogs();
await Data.removeAll(); await Data.removeAll();
await Data.close(); await Data.close();
await Data.removeDB(); await Data.removeDB();

@ -15,6 +15,7 @@ import * as Data from '../data/data';
import Backbone from 'backbone'; import Backbone from 'backbone';
import { SessionRegistrationView } from '../components/registration/SessionRegistrationView'; import { SessionRegistrationView } from '../components/registration/SessionRegistrationView';
import { SessionInboxView } from '../components/SessionInboxView'; import { SessionInboxView } from '../components/SessionInboxView';
import { deleteAllLogs } from '../node/logs';
// tslint:disable: max-classes-per-file // tslint:disable: max-classes-per-file
// Globally disable drag and drop // Globally disable drag and drop
@ -157,7 +158,7 @@ Storage.onready(async () => {
await Data.cleanupOrphanedAttachments(); await Data.cleanupOrphanedAttachments();
await window.Signal.Logs.deleteAll(); await deleteAllLogs();
} }
const themeSetting = window.Events.getThemeSetting(); const themeSetting = window.Events.getThemeSetting();

@ -2,8 +2,8 @@ import { ipcMain } from 'electron';
import rimraf from 'rimraf'; import rimraf from 'rimraf';
import * as Attachments from '../attachments/attachments'; import * as Attachments from '../attachments/attachments';
import { removeKnownAttachments } from './sql';
// tslint:disable: no-console // tslint:disable: no-console
import { sqlNode } from './sql';
let initialized = false; let initialized = false;
@ -12,7 +12,7 @@ const CLEANUP_ORPHANED_ATTACHMENTS_KEY = 'cleanup-orphaned-attachments';
export async function cleanupOrphanedAttachments(userDataPath: string) { export async function cleanupOrphanedAttachments(userDataPath: string) {
const allAttachments = await Attachments.getAllAttachments(userDataPath); const allAttachments = await Attachments.getAllAttachments(userDataPath);
const orphanedAttachments = await removeKnownAttachments(allAttachments); //sql.js const orphanedAttachments = sqlNode.removeKnownAttachments(allAttachments); //sql.js
await Attachments.deleteAll({ await Attachments.deleteAll({
userDataPath, userDataPath,
attachments: orphanedAttachments, attachments: orphanedAttachments,

@ -1,12 +1,6 @@
const { ipcRenderer } = require('electron'); import { ipcRenderer } from 'electron';
/* eslint-env node */ export async function deleteAllLogs() {
module.exports = {
deleteAll,
};
function deleteAll() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
ipcRenderer.once('delete-all-logs-complete', resolve); ipcRenderer.once('delete-all-logs-complete', resolve);

@ -63,7 +63,7 @@ function getSQLCipherIntegrityCheck(db: BetterSqlite3.Database) {
if (rows.length === 0) { if (rows.length === 0) {
return undefined; return undefined;
} }
return rows.map(row => row.cipher_integrity_check); return rows.map((row: any) => row.cipher_integrity_check);
} }
function keyDatabase(db: BetterSqlite3.Database, key: string) { function keyDatabase(db: BetterSqlite3.Database, key: string) {
@ -3024,7 +3024,7 @@ function getExternalFilesForConversation(conversation: any) {
return files; return files;
} }
export function removeKnownAttachments(allAttachments: any) { function removeKnownAttachments(allAttachments: any) {
const lookup = fromPairs(map(allAttachments, file => [file, true])); const lookup = fromPairs(map(allAttachments, file => [file, true]));
const chunkSize = 50; const chunkSize = 50;
@ -3491,7 +3491,9 @@ function fillWithTestData(numConvosToAdd: number, numMsgsToAdd: number) {
return convosIdsAdded; return convosIdsAdded;
} }
const exportedFunctions = { export type SqlNodeType = typeof sqlNode;
export const sqlNode = {
initializeSql, initializeSql,
close, close,
removeDB, removeDB,
@ -3576,14 +3578,13 @@ const exportedFunctions = {
resetAttachmentDownloadPending, resetAttachmentDownloadPending,
removeAttachmentDownloadJob, removeAttachmentDownloadJob,
removeAllAttachmentDownloadJobs, removeAllAttachmentDownloadJobs,
removeKnownAttachments,
removeAll, removeAll,
getMessagesWithVisualMediaAttachments, getMessagesWithVisualMediaAttachments,
getMessagesWithFileAttachments, getMessagesWithFileAttachments,
removeKnownAttachments,
getAllEncryptionKeyPairsForGroup, getAllEncryptionKeyPairsForGroup,
getLatestClosedGroupEncryptionKeyPair, getLatestClosedGroupEncryptionKeyPair,
addClosedGroupEncryptionKeyPair, addClosedGroupEncryptionKeyPair,
@ -3597,6 +3598,3 @@ const exportedFunctions = {
removeV2OpenGroupRoom, removeV2OpenGroupRoom,
removeOneOpenGroupV1Message, removeOneOpenGroupV1Message,
}; };
// tslint:disable-next-line: no-default-export
export const sqlNode = exportedFunctions;

@ -1,5 +1,5 @@
import { ipcMain } from 'electron'; import { ipcMain } from 'electron';
import sql from './sql'; import { sqlNode } from './sql';
import { userConfig } from './config/user_config'; import { userConfig } from './config/user_config';
import { ephemeralConfig } from './config/ephemeral_config'; import { ephemeralConfig } from './config/ephemeral_config';
@ -17,7 +17,7 @@ export function initialize() {
ipcMain.on(SQL_CHANNEL_KEY, (event, jobId, callName, ...args) => { ipcMain.on(SQL_CHANNEL_KEY, (event, jobId, callName, ...args) => {
try { try {
const fn = sql[callName]; const fn = (sqlNode as any)[callName];
if (!fn) { if (!fn) {
throw new Error(`sql channel: ${callName} is not an available function`); throw new Error(`sql channel: ${callName} is not an available function`);
} }

@ -3,7 +3,7 @@
const WORKER_TIMEOUT = 60 * 1000; // one minute const WORKER_TIMEOUT = 60 * 1000; // one minute
class TimedOutError extends Error { class TimedOutError extends Error {
constructor(message) { constructor(message: string) {
super(message); super(message);
this.name = this.constructor.name; this.name = this.constructor.name;
if (typeof Error.captureStackTrace === 'function') { if (typeof Error.captureStackTrace === 'function') {
@ -14,8 +14,14 @@ class TimedOutError extends Error {
} }
} }
class WorkerInterface { export class WorkerInterface {
constructor(path, timeout = WORKER_TIMEOUT) { private readonly timeout: number;
private readonly _DEBUG: boolean;
private _jobCounter: number;
private readonly _jobs: Record<number, any>;
private readonly _utilWorker: Worker;
constructor(path: string, timeout = WORKER_TIMEOUT) {
this._utilWorker = new Worker(path); this._utilWorker = new Worker(path);
this.timeout = timeout; this.timeout = timeout;
this._jobs = Object.create(null); this._jobs = Object.create(null);
@ -44,7 +50,25 @@ class WorkerInterface {
}; };
} }
_makeJob(fnName) { public async callWorker(fnName: string, ...args: any) {
const jobId = this._makeJob(fnName);
return new Promise((resolve, reject) => {
this._utilWorker.postMessage([jobId, fnName, ...args]);
this._updateJob(jobId, {
resolve,
reject,
args: this._DEBUG ? args : null,
});
setTimeout(() => {
reject(new TimedOutError(`Worker job ${jobId} (${fnName}) timed out`));
}, this.timeout);
});
}
private _makeJob(fnName: string): number {
this._jobCounter += 1; this._jobCounter += 1;
const id = this._jobCounter; const id = this._jobCounter;
@ -59,14 +83,14 @@ class WorkerInterface {
return id; return id;
} }
_updateJob(id, data) { private _updateJob(id: number, data: any) {
const { resolve, reject } = data; const { resolve, reject } = data;
const { fnName, start } = this._jobs[id]; const { fnName, start } = this._jobs[id];
this._jobs[id] = { this._jobs[id] = {
...this._jobs[id], ...this._jobs[id],
...data, ...data,
resolve: value => { resolve: (value: any) => {
this._removeJob(id); this._removeJob(id);
const end = Date.now(); const end = Date.now();
if (this._DEBUG) { if (this._DEBUG) {
@ -74,7 +98,7 @@ class WorkerInterface {
} }
return resolve(value); return resolve(value);
}, },
reject: error => { reject: (error: any) => {
this._removeJob(id); this._removeJob(id);
const end = Date.now(); const end = Date.now();
window.log.info(`Worker job ${id} (${fnName}) failed in ${end - start}ms`); window.log.info(`Worker job ${id} (${fnName}) failed in ${end - start}ms`);
@ -83,36 +107,16 @@ class WorkerInterface {
}; };
} }
_removeJob(id) { private _removeJob(id: number) {
if (this._DEBUG) { if (this._DEBUG) {
this._jobs[id].complete = true; this._jobs[id].complete = true;
} else { } else {
// tslint:disable-next-line: no-dynamic-delete
delete this._jobs[id]; delete this._jobs[id];
} }
} }
_getJob(id) { private _getJob(id: number) {
return this._jobs[id]; return this._jobs[id];
} }
callWorker(fnName, ...args) {
const jobId = this._makeJob(fnName);
return new Promise((resolve, reject) => {
this._utilWorker.postMessage([jobId, fnName, ...args]);
this._updateJob(jobId, {
resolve,
reject,
args: this._DEBUG ? args : null,
});
setTimeout(
() => reject(new TimedOutError(`Worker job ${jobId} (${fnName}) timed out`)),
this.timeout
);
});
}
} }
module.exports = WorkerInterface;

@ -1,7 +1,8 @@
/* eslint-env node */ // this file is a weird one as it is used by both sides of electron at the same time
/* global log */
exports.setup = (locale, messages) => { import { LocaleMessagesType } from '../node/locale';
export const setupi18n = (locale: string, messages: LocaleMessagesType) => {
if (!locale) { if (!locale) {
throw new Error('i18n: locale parameter is required'); throw new Error('i18n: locale parameter is required');
} }
@ -9,10 +10,13 @@ exports.setup = (locale, messages) => {
throw new Error('i18n: messages parameter is required'); throw new Error('i18n: messages parameter is required');
} }
function getMessage(key, substitutions) { function getMessage(key: string, substitutions: Array<string>) {
const message = messages[key]; const message = messages[key];
if (!message) { if (!message) {
log.error(`i18n: Attempted to get translation for nonexistent key '${key}'`); // tslint:disable-next-line: no-console
(window.log.error || console.log)(
`i18n: Attempted to get translation for nonexistent key '${key}'`
);
return ''; return '';
} }
Loading…
Cancel
Save