Skip to content

Commit

Permalink
Add an assertion when updating conversations; update cleanData
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanHahn-Signal committed Feb 4, 2021
1 parent 73a304f commit bc37b5c
Show file tree
Hide file tree
Showing 23 changed files with 749 additions and 79 deletions.
8 changes: 7 additions & 1 deletion about_preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@
const { ipcRenderer } = require('electron');
const url = require('url');
const i18n = require('./js/modules/i18n');
const {
getEnvironment,
setEnvironment,
parseEnvironment,
} = require('./ts/environment');

const config = url.parse(window.location.toString(), true).query;
const { locale } = config;
const localeMessages = ipcRenderer.sendSync('locale-data');
setEnvironment(parseEnvironment(config.environment));

window.getEnvironment = () => config.environment;
window.getEnvironment = getEnvironment;
window.getVersion = () => config.version;
window.getAppInstance = () => config.appInstance;

Expand Down
21 changes: 13 additions & 8 deletions app/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@

const path = require('path');
const { app } = require('electron');

let environment;
const {
Environment,
getEnvironment,
setEnvironment,
parseEnvironment,
} = require('../ts/environment');

// In production mode, NODE_ENV cannot be customized by the user
if (!app.isPackaged) {
environment = process.env.NODE_ENV || 'development';
if (app.isPackaged) {
setEnvironment(Environment.Production);
} else {
environment = 'production';
setEnvironment(parseEnvironment(process.env.NODE_ENV || 'development'));
}

// Set environment vars to configure node-config before requiring it
process.env.NODE_ENV = environment;
process.env.NODE_ENV = getEnvironment();
process.env.NODE_CONFIG_DIR = path.join(__dirname, '..', 'config');

if (environment === 'production') {
if (getEnvironment() === Environment.Production) {
// harden production config against the local env
process.env.NODE_CONFIG = '';
process.env.NODE_CONFIG_STRICT_MODE = true;
Expand All @@ -30,9 +34,10 @@ if (environment === 'production') {
}

// We load config after we've made our modifications to NODE_ENV
// eslint-disable-next-line import/order
const config = require('config');

config.environment = environment;
config.environment = getEnvironment();
config.enableHttp = process.env.SIGNAL_ENABLE_HTTP;

// Log resulting env vars in use by config
Expand Down
8 changes: 7 additions & 1 deletion debug_log_preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ const { ipcRenderer } = require('electron');
const url = require('url');
const copyText = require('copy-text-to-clipboard');
const i18n = require('./js/modules/i18n');
const {
getEnvironment,
setEnvironment,
parseEnvironment,
} = require('./ts/environment');

const config = url.parse(window.location.toString(), true).query;
const { locale } = config;
const localeMessages = ipcRenderer.sendSync('locale-data');
setEnvironment(parseEnvironment(config.environment));

window.getVersion = () => config.version;
window.theme = config.theme;
Expand All @@ -21,7 +27,7 @@ window.copyText = copyText;
window.nodeSetImmediate = setImmediate;

window.getNodeVersion = () => config.node_version;
window.getEnvironment = () => config.environment;
window.getEnvironment = getEnvironment;

require('./ts/logging/set_up_renderer_logging');

Expand Down
5 changes: 3 additions & 2 deletions js/modules/backup.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const rimraf = require('rimraf');
const electronRemote = require('electron').remote;

const crypto = require('../../ts/Crypto');
const { getEnvironment } = require('../../ts/environment');

const { dialog, BrowserWindow } = electronRemote;

Expand Down Expand Up @@ -1198,7 +1199,7 @@ function deleteAll(pattern) {
const ARCHIVE_NAME = 'messages.tar.gz';

async function exportToDirectory(directory, options) {
const env = window.getEnvironment();
const env = getEnvironment();
if (env !== 'test') {
throw new Error('export is only supported in test mode');
}
Expand Down Expand Up @@ -1266,7 +1267,7 @@ async function importFromDirectory(directory, options) {

const archivePath = path.join(directory, ARCHIVE_NAME);
if (fs.existsSync(archivePath)) {
const env = window.getEnvironment();
const env = getEnvironment();
if (env !== 'test') {
throw new Error('import is only supported in test mode');
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"publish-to-apt": "NAME=$npm_package_name VERSION=$npm_package_version ./aptly.sh",
"test": "yarn test-node && yarn test-electron",
"test-electron": "yarn grunt test",
"test-node": "electron-mocha --require test/setup-test-node.js --recursive test/app test/modules ts/test-node ts/test-both",
"test-node": "electron-mocha --file test/setup-test-node.js --recursive test/app test/modules ts/test-node ts/test-both",
"test-node-coverage": "nyc --reporter=lcov --reporter=text mocha --recursive test/app test/modules ts/test-node ts/test-both",
"eslint": "eslint .",
"lint": "yarn format --list-different && yarn eslint",
Expand Down
8 changes: 7 additions & 1 deletion permissions_popup_preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@ const url = require('url');
const i18n = require('./js/modules/i18n');
const { ConfirmationModal } = require('./ts/components/ConfirmationModal');
const { makeGetter, makeSetter } = require('./preload_utils');
const {
getEnvironment,
setEnvironment,
parseEnvironment,
} = require('./ts/environment');

const { nativeTheme } = remote.require('electron');

const config = url.parse(window.location.toString(), true).query;
const { locale } = config;
const localeMessages = ipcRenderer.sendSync('locale-data');
setEnvironment(parseEnvironment(config.environment));

window.getEnvironment = () => config.environment;
window.getEnvironment = getEnvironment;
window.getVersion = () => config.version;
window.theme = config.theme;
window.i18n = i18n.setup(locale, localeMessages);
Expand Down
14 changes: 11 additions & 3 deletions preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ try {
const client = require('libsignal-client');
const _ = require('lodash');
const { installGetter, installSetter } = require('./preload_utils');
const {
getEnvironment,
setEnvironment,
parseEnvironment,
Environment,
} = require('./ts/environment');

const { remote } = electron;
const { app } = remote;
Expand All @@ -19,9 +25,11 @@ try {
window.PROTO_ROOT = 'protos';
const config = require('url').parse(window.location.toString(), true).query;

setEnvironment(parseEnvironment(config.environment));

let title = config.name;
if (config.environment !== 'production') {
title += ` - ${config.environment}`;
if (getEnvironment() !== Environment.Production) {
title += ` - ${getEnvironment()}`;
}
if (config.appInstance) {
title += ` - ${config.appInstance}`;
Expand All @@ -37,7 +45,7 @@ try {

window.platform = process.platform;
window.getTitle = () => title;
window.getEnvironment = () => config.environment;
window.getEnvironment = getEnvironment;
window.getAppInstance = () => config.appInstance;
window.getVersion = () => config.version;
window.getExpiration = () => {
Expand Down
8 changes: 7 additions & 1 deletion settings_preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ const { ipcRenderer, remote } = require('electron');

const url = require('url');
const i18n = require('./js/modules/i18n');
const {
getEnvironment,
setEnvironment,
parseEnvironment,
} = require('./ts/environment');

const config = url.parse(window.location.toString(), true).query;
const { locale } = config;
const localeMessages = ipcRenderer.sendSync('locale-data');
setEnvironment(parseEnvironment(config.environment));

const { nativeTheme } = remote.require('electron');

Expand All @@ -33,7 +39,7 @@ window.subscribeToSystemThemeChange = fn => {
});
};

window.getEnvironment = () => config.environment;
window.getEnvironment = getEnvironment;
window.getVersion = () => config.version;
window.getAppInstance = () => config.appInstance;

Expand Down
9 changes: 8 additions & 1 deletion sticker-creator/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ const config = require('url').parse(window.location.toString(), true).query;
const { noop, uniqBy } = require('lodash');
const pMap = require('p-map');
const { deriveStickerPackKey } = require('../ts/Crypto');
const {
getEnvironment,
setEnvironment,
parseEnvironment,
} = require('../ts/environment');
const { makeGetter } = require('../preload_utils');

const { dialog } = remote;
Expand All @@ -21,9 +26,11 @@ const MAX_STICKER_DIMENSION = STICKER_SIZE;
const MAX_WEBP_STICKER_BYTE_LENGTH = 100 * 1024;
const MAX_ANIMATED_STICKER_BYTE_LENGTH = 300 * 1024;

setEnvironment(parseEnvironment(config.environment));

window.ROOT_PATH = window.location.href.startsWith('file') ? '../../' : '/';
window.PROTO_ROOT = '../../protos';
window.getEnvironment = () => config.environment;
window.getEnvironment = getEnvironment;
window.getVersion = () => config.version;
window.getGuid = require('uuid/v4');
window.PQueue = require('p-queue').default;
Expand Down
6 changes: 6 additions & 0 deletions test/setup-test-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

/* eslint-disable no-console */

const { setEnvironment, Environment } = require('../ts/environment');

before(() => {
setEnvironment(Environment.Test);
});

// To replicate logic we have on the client side
global.window = {
log: {
Expand Down
49 changes: 49 additions & 0 deletions ts/environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only

// Many places rely on this enum being a string.
export enum Environment {
Development = 'development',
Production = 'production',
Staging = 'staging',
Test = 'test',
TestLib = 'test-lib',
}

let environment: undefined | Environment;

export function getEnvironment(): Environment {
if (environment === undefined) {
// This should never happen—we should always have initialized the environment by this
// point. It'd be nice to log here but the logger depends on the environment and we
// can't have circular dependencies.
return Environment.Production;
}
return environment;
}

/**
* Sets the current environment. Should be called early in a process's life, and can only
* be called once.
*/
export function setEnvironment(env: Environment): void {
if (environment !== undefined) {
throw new Error('Environment has already been set');
}
environment = env;
}

const ENVIRONMENTS_BY_STRING = new Map<string, Environment>([
['development', Environment.Development],
['production', Environment.Production],
['staging', Environment.Staging],
['test', Environment.Test],
['test-lib', Environment.TestLib],
]);
export function parseEnvironment(value: unknown): Environment {
if (typeof value !== 'string') {
return Environment.Production;
}
const result = ENVIRONMENTS_BY_STRING.get(value);
return result || Environment.Production;
}
33 changes: 33 additions & 0 deletions ts/logging/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only

import { noop } from 'lodash';
import { LogLevel } from './shared';

type LogAtLevelFnType = (
level: LogLevel,
...args: ReadonlyArray<unknown>
) => void;

let logAtLevel: LogAtLevelFnType = noop;
let hasInitialized = false;

type LogFn = (...args: ReadonlyArray<unknown>) => void;
export const fatal: LogFn = (...args) => logAtLevel(LogLevel.Fatal, ...args);
export const error: LogFn = (...args) => logAtLevel(LogLevel.Error, ...args);
export const warn: LogFn = (...args) => logAtLevel(LogLevel.Warn, ...args);
export const info: LogFn = (...args) => logAtLevel(LogLevel.Info, ...args);
export const debug: LogFn = (...args) => logAtLevel(LogLevel.Debug, ...args);
export const trace: LogFn = (...args) => logAtLevel(LogLevel.Trace, ...args);

/**
* Sets the low-level logging interface. Should be called early in a process's life, and
* can only be called once.
*/
export function setLogAtLevel(log: LogAtLevelFnType): void {
if (hasInitialized) {
throw new Error('Logger has already been initialized');
}
logAtLevel = log;
hasInitialized = true;
}
19 changes: 11 additions & 8 deletions ts/logging/set_up_renderer_logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
getLogLevelString,
isLogEntry,
} from './shared';
import * as log from './log';
import { reallyJsonStringify } from '../util/reallyJsonStringify';

// To make it easier to visually scan logs, we make all levels the same length
Expand All @@ -33,13 +34,13 @@ function now() {
return date.toJSON();
}

function log(...args: ReadonlyArray<unknown>) {
function consoleLog(...args: ReadonlyArray<unknown>) {
logAtLevel(LogLevel.Info, ...args);
}

if (window.console) {
console._log = console.log;
console.log = log;
console.log = consoleLog;
}

// The mechanics of preparing a log for publish
Expand Down Expand Up @@ -126,13 +127,15 @@ function logAtLevel(level: LogLevel, ...args: ReadonlyArray<unknown>): void {
});
}

log.setLogAtLevel(logAtLevel);

window.log = {
fatal: _.partial(logAtLevel, LogLevel.Fatal),
error: _.partial(logAtLevel, LogLevel.Error),
warn: _.partial(logAtLevel, LogLevel.Warn),
info: _.partial(logAtLevel, LogLevel.Info),
debug: _.partial(logAtLevel, LogLevel.Debug),
trace: _.partial(logAtLevel, LogLevel.Trace),
fatal: log.fatal,
error: log.error,
warn: log.warn,
info: log.info,
debug: log.debug,
trace: log.trace,
fetch,
publish,
};
Expand Down
Loading

0 comments on commit bc37b5c

Please sign in to comment.