2
0
mirror of https://github.com/frappe/books.git synced 2024-12-22 02:49:03 +00:00

incr: enable context isolation

This commit is contained in:
18alantom 2023-07-10 14:42:20 +05:30
parent e2b9ea1f49
commit 92e8dfb6d7
7 changed files with 78 additions and 59 deletions

View File

@ -50,6 +50,7 @@ export type BespokeFunction = (
db: DatabaseCore,
...args: unknown[]
) => Promise<unknown>;
export type SingleValue<T> = {
fieldname: string;
parent: string;

View File

@ -1,7 +1,5 @@
import { AuthDemuxBase } from 'utils/auth/types';
import { IPC_ACTIONS } from 'utils/messages';
import { Creds } from 'utils/types';
const { ipcRenderer } = require('electron');
export class AuthDemux extends AuthDemuxBase {
#isElectron = false;
@ -12,7 +10,7 @@ export class AuthDemux extends AuthDemuxBase {
async getCreds(): Promise<Creds> {
if (this.#isElectron) {
return (await ipcRenderer.invoke(IPC_ACTIONS.GET_CREDS)) as Creds;
return await ipc.getCreds();
} else {
return { errorLogUrl: '', tokenString: '', telemetryUrl: '' };
}

View File

@ -1,26 +1,12 @@
import type Store from 'electron-store';
import { ConfigMap } from 'fyo/core/types';
import type { IPC } from 'main/preload';
export class Config {
config: Map<string, unknown> | Store;
config: Map<string, unknown> | IPC['store'];
constructor(isElectron: boolean) {
this.config = new Map();
if (isElectron) {
const Config = require('electron-store') as typeof Store;
this.config = new Config();
}
}
get store() {
if (this.config instanceof Map) {
const store: Record<string, unknown> = {};
for (const key of this.config.keys()) {
store[key] = this.config.get(key);
}
return store;
} else {
return this.config;
this.config = ipc.store;
}
}
@ -39,8 +25,4 @@ export class Config {
delete(key: keyof ConfigMap) {
this.config.delete(key);
}
clear() {
this.config.clear();
}
}

View File

@ -1,9 +1,7 @@
const { ipcRenderer } = require('electron');
import { DatabaseError, NotImplemented } from 'fyo/utils/errors';
import { SchemaMap } from 'schemas/types';
import { DatabaseDemuxBase, DatabaseMethod } from 'utils/db/types';
import { BackendResponse } from 'utils/ipc/types';
import { IPC_ACTIONS } from 'utils/messages';
export class DatabaseDemux extends DatabaseDemuxBase {
#isElectron = false;
@ -32,9 +30,7 @@ export class DatabaseDemux extends DatabaseDemuxBase {
}
return (await this.#handleDBCall(async () => {
return (await ipcRenderer.invoke(
IPC_ACTIONS.DB_SCHEMA
)) as BackendResponse;
return await ipc.db.getSchema();
})) as SchemaMap;
}
@ -47,11 +43,7 @@ export class DatabaseDemux extends DatabaseDemuxBase {
}
return (await this.#handleDBCall(async () => {
return (await ipcRenderer.invoke(
IPC_ACTIONS.DB_CREATE,
dbPath,
countryCode
)) as BackendResponse;
return ipc.db.create(dbPath, countryCode);
})) as string;
}
@ -64,11 +56,7 @@ export class DatabaseDemux extends DatabaseDemuxBase {
}
return (await this.#handleDBCall(async () => {
return (await ipcRenderer.invoke(
IPC_ACTIONS.DB_CONNECT,
dbPath,
countryCode
)) as BackendResponse;
return ipc.db.connect(dbPath, countryCode);
})) as string;
}
@ -78,11 +66,7 @@ export class DatabaseDemux extends DatabaseDemuxBase {
}
return await this.#handleDBCall(async () => {
return (await ipcRenderer.invoke(
IPC_ACTIONS.DB_CALL,
method,
...args
)) as BackendResponse;
return await ipc.db.call(method, ...args);
});
}
@ -92,11 +76,7 @@ export class DatabaseDemux extends DatabaseDemuxBase {
}
return await this.#handleDBCall(async () => {
return (await ipcRenderer.invoke(
IPC_ACTIONS.DB_BESPOKE,
method,
...args
)) as BackendResponse;
return await ipc.db.bespoke(method, ...args);
});
}
}

View File

@ -96,7 +96,7 @@ export class Fyo {
setIsElectron() {
try {
this.isElectron = Boolean(require('electron'));
this.isElectron = !!window?.ipc;
} catch {
this.isElectron = false;
}

View File

@ -4,6 +4,7 @@ require('source-map-support').install({
environment: 'node',
});
import { emitMainProcessError } from 'backend/helpers';
import {
app,
BrowserWindow,
@ -12,7 +13,6 @@ import {
ProtocolRequest,
ProtocolResponse,
} from 'electron';
import Store from 'electron-store';
import { autoUpdater } from 'electron-updater';
import fs from 'fs';
import path from 'path';
@ -21,7 +21,6 @@ import registerAutoUpdaterListeners from './main/registerAutoUpdaterListeners';
import registerIpcMainActionListeners from './main/registerIpcMainActionListeners';
import registerIpcMainMessageListeners from './main/registerIpcMainMessageListeners';
import registerProcessListeners from './main/registerProcessListeners';
import { emitMainProcessError } from 'backend/helpers';
export class Main {
title = 'Frappe Books';
@ -54,8 +53,6 @@ export class Main {
'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0',
};
Store.initRenderer();
this.registerListeners();
if (this.isMac && this.isDevelopment) {
app.dock.setIcon(this.icon);
@ -95,7 +92,7 @@ export class Main {
titleBarStyle: 'hidden',
trafficLightPosition: { x: 16, y: 16 },
webPreferences: {
contextIsolation: false, // TODO: Switch this off
contextIsolation: true,
nodeIntegration: true,
preload,
},

View File

@ -4,11 +4,15 @@ import type {
SaveDialogOptions,
SaveDialogReturnValue,
} from 'electron';
import { ipcRenderer } from 'electron';
import { BackendResponse } from 'utils/ipc/types';
import { contextBridge, ipcRenderer } from 'electron';
import type { ConfigMap } from 'fyo/core/types';
import config from 'utils/config';
import type { DatabaseMethod } from 'utils/db/types';
import type { BackendResponse } from 'utils/ipc/types';
import { IPC_ACTIONS, IPC_CHANNELS, IPC_MESSAGES } from 'utils/messages';
import type {
ConfigFilesWithModified,
Creds,
LanguageMap,
SelectFileOptions,
SelectFileReturn,
@ -23,6 +27,10 @@ const ipc = {
return ipcRenderer.send(IPC_MESSAGES.RELOAD_MAIN_WINDOW);
},
async getCreds() {
return (await ipcRenderer.invoke(IPC_ACTIONS.GET_CREDS)) as Creds;
},
async getLanguageMap(code: string) {
return (await ipcRenderer.invoke(IPC_ACTIONS.GET_LANGUAGE_MAP, code)) as {
languageMap: LanguageMap;
@ -136,8 +144,61 @@ const ipc = {
registerConsoleLogListener(listener: IPCRendererListener) {
ipcRenderer.on(IPC_CHANNELS.CONSOLE_LOG, listener);
},
db: {
async getSchema() {
return (await ipcRenderer.invoke(
IPC_ACTIONS.DB_SCHEMA
)) as BackendResponse;
},
async create(dbPath: string, countryCode?: string) {
return (await ipcRenderer.invoke(
IPC_ACTIONS.DB_CREATE,
dbPath,
countryCode
)) as BackendResponse;
},
async connect(dbPath: string, countryCode?: string) {
return (await ipcRenderer.invoke(
IPC_ACTIONS.DB_CONNECT,
dbPath,
countryCode
)) as BackendResponse;
},
async call(method: DatabaseMethod, ...args: unknown[]) {
return (await ipcRenderer.invoke(
IPC_ACTIONS.DB_CALL,
method,
...args
)) as BackendResponse;
},
async bespoke(method: string, ...args: unknown[]) {
return (await ipcRenderer.invoke(
IPC_ACTIONS.DB_BESPOKE,
method,
...args
)) as BackendResponse;
},
},
store: {
get<K extends keyof ConfigMap>(key: K) {
return config.get(key);
},
set<K extends keyof ConfigMap>(key: K, value: ConfigMap[K]) {
return config.set(key, value);
},
delete(key: keyof ConfigMap) {
return config.delete(key);
},
},
} as const;
// contextBridge.exposeInMainWorld('api', ipc);
window.ipc = ipc;
contextBridge.exposeInMainWorld('ipc', ipc);
export type IPC = typeof ipc;