Merge pull request #1179 from Mikunj/window-fix-2

Change back to old window syntax and add stubbing method.
pull/1180/head
Mikunj Varsani 4 years ago committed by GitHub
commit 50d27d1143
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

71
ts/global.d.ts vendored

@ -1,74 +1,3 @@
// TODO: Delete this and depend on window.ts instead
interface Window {
CONSTANTS: any;
versionInfo: any;
Events: any;
Lodash: any;
clearLocalData: any;
getAccountManager: any;
getConversations: any;
getFriendsFromContacts: any;
mnemonic: any;
clipboard: any;
attemptConnection: any;
passwordUtil: any;
userConfig: any;
shortenPubkey: any;
dcodeIO: any;
libsignal: any;
libloki: any;
displayNameRegex: any;
Signal: any;
Whisper: any;
ConversationController: any;
MessageController: any;
StringView: any;
// Following function needs to be written in background.js
// getMemberList: any;
onLogin: any;
setPassword: any;
textsecure: any;
Session: any;
log: any;
i18n: any;
friends: any;
generateID: any;
storage: any;
pushToast: any;
confirmationDialog: any;
showQRDialog: any;
showSeedDialog: any;
showPasswordDialog: any;
showEditProfileDialog: any;
deleteAccount: any;
toggleTheme: any;
toggleMenuBar: any;
toggleSpellCheck: any;
toggleLinkPreview: any;
toggleMediaPermissions: any;
getSettingValue: any;
setSettingValue: any;
lokiFeatureFlags: any;
resetDatabase: any;
restart: () => void;
lokiFileServerAPI: any;
WebAPI: any;
SenderKeyAPI: any;
}
interface Promise<T> {
ignore(): void;
}

@ -1,6 +1,5 @@
import { EncryptionType } from '../types/EncryptionType';
import { SignalService } from '../../protobuf';
import { libloki, libsignal, Signal, textsecure } from '../../window';
import { UserUtil } from '../../util';
import { CipherTextObject } from '../../../libtextsecure/libsignal-protocol';
@ -46,7 +45,7 @@ export async function encrypt(
cipherText: Uint8Array;
}> {
const plainText = padPlainTextBuffer(plainTextBuffer);
const address = new libsignal.SignalProtocolAddress(device, 1);
const address = new window.libsignal.SignalProtocolAddress(device, 1);
if (encryptionType === EncryptionType.MediumGroup) {
// TODO: Do medium group stuff here
@ -55,11 +54,11 @@ export async function encrypt(
let innerCipherText: CipherTextObject;
if (encryptionType === EncryptionType.SessionRequest) {
const cipher = new libloki.crypto.FallBackSessionCipher(address);
const cipher = new window.libloki.crypto.FallBackSessionCipher(address);
innerCipherText = await cipher.encrypt(plainText.buffer);
} else {
const cipher = new libsignal.SessionCipher(
textsecure.storage.protocol,
const cipher = new window.libsignal.SessionCipher(
window.textsecure.storage.protocol,
address
);
innerCipherText = await cipher.encrypt(plainText.buffer);
@ -85,8 +84,8 @@ async function encryptUsingSealedSender(
senderDevice: 1,
});
const cipher = new Signal.Metadata.SecretSessionCipher(
textsecure.storage.protocol
const cipher = new window.Signal.Metadata.SecretSessionCipher(
window.textsecure.storage.protocol
);
const cipherTextBuffer = await cipher.encrypt(
device,

@ -1,7 +1,6 @@
import { SessionRequestMessage } from '../messages/outgoing';
// import { MessageSender } from '../sending';
import { createOrUpdateItem, getItemById } from '../../../js/modules/data';
import { libloki, libsignal, textsecure } from '../../window';
import { MessageSender } from '../sending';
import { MessageUtils } from '../utils';
import { PubKey } from '../types';
@ -50,9 +49,9 @@ export class SessionProtocol {
/** Returns true if we already have a session with that device */
public static async hasSession(pubkey: PubKey): Promise<boolean> {
// Session does not use the concept of a deviceId, thus it's always 1
const address = new libsignal.SignalProtocolAddress(pubkey.key, 1);
const sessionCipher = new libsignal.SessionCipher(
textsecure.storage.protocol,
const address = new window.libsignal.SignalProtocolAddress(pubkey.key, 1);
const sessionCipher = new window.libsignal.SessionCipher(
window.textsecure.storage.protocol,
address
);
@ -90,7 +89,7 @@ export class SessionProtocol {
return;
}
const preKeyBundle = await libloki.storage.getPreKeyBundleForContact(
const preKeyBundle = await window.libloki.storage.getPreKeyBundleForContact(
pubkey.key
);
@ -102,11 +101,7 @@ export class SessionProtocol {
try {
await SessionProtocol.sendSessionRequest(sessionReset, pubkey);
} catch (error) {
window.console.warn(
'Failed to send session request to:',
pubkey.key,
error
);
console.warn('Failed to send session request to:', pubkey.key, error);
}
}

@ -5,7 +5,6 @@ import { OpenGroupMessage } from '../messages/outgoing';
import { SignalService } from '../../protobuf';
import { UserUtil } from '../../util';
import { MessageEncrypter } from '../crypto';
import { lokiMessageAPI, lokiPublicChatAPI } from '../../window';
import pRetry from 'p-retry';
// ================ Regular ================
@ -15,7 +14,7 @@ import pRetry from 'p-retry';
*/
export function canSendToSnode(): boolean {
// Seems like lokiMessageAPI is not always guaranteed to be initialized
return Boolean(lokiMessageAPI);
return Boolean(window.lokiMessageAPI);
}
/**
@ -42,7 +41,7 @@ export async function send(
const data = wrapEnvelope(envelope);
return pRetry(
async () => lokiMessageAPI.sendMessage(device, data, timestamp, ttl),
async () => window.lokiMessageAPI.sendMessage(device, data, timestamp, ttl),
{
retries: Math.max(attempts - 1, 0),
factor: 1,
@ -102,7 +101,7 @@ export async function sendToOpenGroup(
This should be fixed and we shouldn't rely on returning true/false, rather return nothing (success) or throw an error (failure)
*/
const { group, quote, attachments, preview, body } = message;
const channelAPI = await lokiPublicChatAPI.findOrCreateChannel(
const channelAPI = await window.lokiPublicChatAPI.findOrCreateChannel(
group.server,
group.channel,
group.conversationId

@ -1,8 +1,7 @@
import { ConversationController } from '../../window';
import { PubKey } from '../types';
export async function getGroupMembers(groupId: PubKey): Promise<Array<PubKey>> {
const groupConversation = ConversationController.get(groupId.key);
const groupConversation = window.ConversationController.get(groupId.key);
const groupMembers = groupConversation
? groupConversation.attributes.members
: undefined;
@ -15,7 +14,7 @@ export async function getGroupMembers(groupId: PubKey): Promise<Array<PubKey>> {
}
export function isMediumGroup(groupId: PubKey): boolean {
const conversation = ConversationController.get(groupId.key);
const conversation = window.ConversationController.get(groupId.key);
if (!conversation) {
return false;

@ -4,7 +4,6 @@ import {
getAllConversations,
getPrimaryDeviceFor,
} from '../../../js/modules/data';
import { ConversationController, Whisper } from '../../window';
import { ContentMessage, SyncMessage } from '../messages/outgoing';
@ -32,7 +31,7 @@ export async function getSyncContacts(): Promise<Array<any> | undefined> {
const primaryDevice = await getPrimaryDeviceFor(thisDevice);
const conversations = await getAllConversations({
ConversationCollection: Whisper.ConversationCollection,
ConversationCollection: window.Whisper.ConversationCollection,
});
// We are building a set of all contacts
@ -54,7 +53,7 @@ export async function getSyncContacts(): Promise<Array<any> | undefined> {
);
const seondaryContactsPromise = secondaryContactsPartial.map(async c =>
ConversationController.getOrCreateAndWait(
window.ConversationController.getOrCreateAndWait(
c.getPrimaryDevicePubKey(),
'private'
)

@ -4,10 +4,10 @@ import * as window from '../../window';
import * as DataShape from '../../../js/modules/data';
import { v4 as uuid } from 'uuid';
import { ImportMock } from 'ts-mock-imports';
import { PubKey } from '../../../ts/session/types';
import { ChatMessage } from '../../session/messages/outgoing';
const globalAny: any = global;
const sandbox = sinon.createSandbox();
// We have to do this in a weird way because Data uses module.exports
@ -26,7 +26,7 @@ export function stubData(fn: keyof DataFunction): sinon.SinonStub {
return sandbox.stub(Data, fn);
}
type WindowFunction = typeof window;
type WindowValue<K extends keyof Window> = Partial<Window[K]> | undefined;
/**
* Stub a window object.
@ -34,15 +34,33 @@ type WindowFunction = typeof window;
* Note: This uses a custom sandbox.
* Please call `restoreStubs()` or `stub.restore()` to restore original functionality.
*/
export function stubWindow<K extends keyof WindowFunction>(
export function stubWindow<K extends keyof Window>(
fn: K,
replaceWith?: Partial<WindowFunction[K]>
value: WindowValue<K>
) {
return ImportMock.mockOther(window, fn, replaceWith);
// tslint:disable-next-line: no-typeof-undefined
if (typeof globalAny.window === 'undefined') {
globalAny.window = {};
}
const set = (newValue: WindowValue<K>) => {
globalAny.window[fn] = newValue;
};
const get = () => {
return globalAny.window[fn] as WindowValue<K>;
};
globalAny.window[fn] = value;
return {
get,
set,
};
}
export function restoreStubs() {
ImportMock.restore();
globalAny.window = undefined;
sandbox.restore();
}

76
ts/window.d.ts vendored

@ -0,0 +1,76 @@
import { LocalizerType } from '../types/Util';
import LokiMessageAPI from '../../js/modules/loki_message_api';
import LokiPublicChatFactoryAPI from '../../js/modules/loki_public_chat_api';
import { LibsignalProtocol } from '../../libtextsecure/libsignal-protocol';
import { SignalInterface } from '../../js/modules/signal';
/*
We declare window stuff here instead of global.d.ts because we are importing other declarations.
If you import anything in global.d.ts, the type system won't work correctly.
*/
declare global {
interface Window {
CONSTANTS: any;
ConversationController: any;
Events: any;
Lodash: any;
LokiAppDotNetServerAPI: any;
LokiFileServerAPI: any;
LokiPublicChatAPI: any;
LokiRssAPI: any;
LokiSnodeAPI: any;
MessageController: any;
SenderKeyAPI: any;
Session: any;
Signal: SignalInterface;
StringView: any;
StubAppDotNetApi: any;
StubMessageAPI: any;
WebAPI: any;
Whisper: any;
attemptConnection: any;
clearLocalData: any;
clipboard: any;
confirmationDialog: any;
dcodeIO: any;
deleteAccount: any;
displayNameRegex: any;
friends: any;
generateID: any;
getAccountManager: any;
getConversations: any;
getFriendsFromContacts: any;
getSettingValue: any;
i18n: LocalizerType;
libloki: any;
libsignal: LibsignalProtocol;
log: any;
lokiFeatureFlags: any;
lokiFileServerAPI: any;
lokiMessageAPI: LokiMessageAPI;
lokiPublicChatAPI: LokiPublicChatFactoryAPI;
mnemonic: any;
onLogin: any;
passwordUtil: any;
pushToast: any;
resetDatabase: any;
restart: any;
seedNodeList: any;
setPassword: any;
setSettingValue: any;
shortenPubkey: any;
showEditProfileDialog: any;
showPasswordDialog: any;
showQRDialog: any;
showSeedDialog: any;
storage: any;
textsecure: any;
toggleLinkPreview: any;
toggleMediaPermissions: any;
toggleMenuBar: any;
toggleSpellCheck: any;
toggleTheme: any;
userConfig: any;
versionInfo: any;
}
}

@ -1,142 +0,0 @@
import { LocalizerType } from '../types/Util';
import LokiMessageAPI from '../../js/modules/loki_message_api';
import LokiPublicChatFactoryAPI from '../../js/modules/loki_public_chat_api';
import { LibsignalProtocol } from '../../libtextsecure/libsignal-protocol';
import { SignalInterface } from '../../js/modules/signal';
interface WindowInterface extends Window {
seedNodeList: any;
WebAPI: any;
LokiSnodeAPI: any;
SenderKeyAPI: any;
StubMessageAPI: any;
StubAppDotNetApi: any;
LokiPublicChatAPI: any;
LokiAppDotNetServerAPI: any;
LokiFileServerAPI: any;
LokiRssAPI: any;
CONSTANTS: any;
versionInfo: any;
Events: any;
Lodash: any;
clearLocalData: any;
getAccountManager: any;
getConversations: any;
getFriendsFromContacts: any;
mnemonic: any;
clipboard: any;
attemptConnection: any;
passwordUtil: any;
userConfig: any;
shortenPubkey: any;
dcodeIO: any;
libsignal: LibsignalProtocol;
libloki: any;
displayNameRegex: any;
Signal: SignalInterface;
Whisper: any;
ConversationController: any;
onLogin: any;
setPassword: any;
textsecure: any;
Session: any;
log: any;
i18n: LocalizerType;
friends: any;
generateID: any;
storage: any;
pushToast: any;
confirmationDialog: any;
showQRDialog: any;
showSeedDialog: any;
showPasswordDialog: any;
showEditProfileDialog: any;
deleteAccount: any;
toggleTheme: any;
toggleMenuBar: any;
toggleSpellCheck: any;
toggleLinkPreview: any;
toggleMediaPermissions: any;
getSettingValue: any;
setSettingValue: any;
lokiFeatureFlags: any;
resetDatabase: any;
lokiMessageAPI: LokiMessageAPI;
lokiPublicChatAPI: LokiPublicChatFactoryAPI;
}
// In the case for tests
// tslint:disable-next-line: no-typeof-undefined
if (typeof window === 'undefined') {
const globalAny: any = global;
globalAny.window = {};
}
declare const window: WindowInterface;
// TODO: Is there an easier way to dynamically export these?
// Utilities
export const WebAPI = window.WebAPI;
export const Events = window.Events;
export const Signal = window.Signal;
export const Whisper = window.Whisper;
export const ConversationController = window.ConversationController;
export const passwordUtil = window.passwordUtil;
// Values
export const CONSTANTS = window.CONSTANTS;
export const versionInfo = window.versionInfo;
export const mnemonic = window.mnemonic;
export const lokiFeatureFlags = window.lokiFeatureFlags;
// Getters
export const getAccountManager = window.getAccountManager;
export const getConversations = window.getConversations;
export const getFriendsFromContacts = window.getFriendsFromContacts;
export const getSettingValue = window.getSettingValue;
// Setters
export const setPassword = window.setPassword;
export const setSettingValue = window.setSettingValue;
// UI Events
export const pushToast = window.pushToast;
export const confirmationDialog = window.confirmationDialog;
export const showQRDialog = window.showQRDialog;
export const showSeedDialog = window.showSeedDialog;
export const showPasswordDialog = window.showPasswordDialog;
export const showEditProfileDialog = window.showEditProfileDialog;
export const toggleTheme = window.toggleTheme;
export const toggleMenuBar = window.toggleMenuBar;
export const toggleSpellCheck = window.toggleSpellCheck;
export const toggleLinkPreview = window.toggleLinkPreview;
export const toggleMediaPermissions = window.toggleMediaPermissions;
// Actions
export const clearLocalData = window.clearLocalData;
export const deleteAccount = window.deleteAccount;
export const resetDatabase = window.resetDatabase;
export const attemptConnection = window.attemptConnection;
export const libloki = window.libloki;
export const libsignal = window.libsignal;
export const textsecure = window.textsecure;
export const lokiMessageAPI = window.lokiMessageAPI;
export const lokiPublicChatAPI = window.lokiPublicChatAPI;
Loading…
Cancel
Save