aboutsummaryrefslogtreecommitdiffstats
path: root/src/features
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2022-04-23 01:59:21 +0200
committerLibravatar GitHub <noreply@github.com>2022-04-22 23:59:21 +0000
commitd02644f7c41150709795e57bfd40351b4da35a7b (patch)
tree2403fb76bd5fae1703f8b55172ffce9e0a5d2bce /src/features
parentComplete tray icons redesign for all platforms (#28) (diff)
downloadferdium-app-d02644f7c41150709795e57bfd40351b4da35a7b.tar.gz
ferdium-app-d02644f7c41150709795e57bfd40351b4da35a7b.tar.zst
ferdium-app-d02644f7c41150709795e57bfd40351b4da35a7b.zip
Preload safe debug shim (#29)
In https://github.com/ferdium/ferdium-app/pull/23 we removed usages of the debug package due to an electron bug. This patch aims to restore some debug functionality by introducing a shim. The shim detect whether if it is being introduced in a preload script where the electron but would be triggered, and falls back to a simple replacement for debug. However, in the main and renderer processes, where a preload script is not being used, we still get full debug functionality. In this way, a module can be used both in a preload script and outside of it, while still preserving debug functionality whenever possible. Signed-off-by: Kristóf Marussy <kristof@marussy.com>
Diffstat (limited to 'src/features')
-rw-r--r--src/features/basicAuth/index.ts2
-rw-r--r--src/features/basicAuth/mainIpcHandler.ts2
-rw-r--r--src/features/basicAuth/store.ts2
-rw-r--r--src/features/communityRecipes/store.ts7
-rw-r--r--src/features/publishDebugInfo/Component.js2
-rw-r--r--src/features/publishDebugInfo/index.ts2
-rw-r--r--src/features/quickSwitch/index.ts2
-rw-r--r--src/features/serviceProxy/index.ts11
-rw-r--r--src/features/todos/preload.ts7
-rw-r--r--src/features/todos/store.js23
-rw-r--r--src/features/workspaces/api.ts19
-rw-r--r--src/features/workspaces/store.js9
12 files changed, 41 insertions, 47 deletions
diff --git a/src/features/basicAuth/index.ts b/src/features/basicAuth/index.ts
index dd02a3bdc..ae698cba8 100644
--- a/src/features/basicAuth/index.ts
+++ b/src/features/basicAuth/index.ts
@@ -2,7 +2,7 @@ import { AuthInfo, BrowserWindow, ipcRenderer } from 'electron';
2 2
3import { state as ModalState } from './store'; 3import { state as ModalState } from './store';
4 4
5const debug = require('debug')('Ferdium:feature:basicAuth'); 5const debug = require('../../preload-safe-debug')('Ferdium:feature:basicAuth');
6 6
7const state = ModalState; 7const state = ModalState;
8 8
diff --git a/src/features/basicAuth/mainIpcHandler.ts b/src/features/basicAuth/mainIpcHandler.ts
index 2f78b1497..5d320df5c 100644
--- a/src/features/basicAuth/mainIpcHandler.ts
+++ b/src/features/basicAuth/mainIpcHandler.ts
@@ -1,6 +1,6 @@
1import { BrowserWindow } from 'electron'; 1import { BrowserWindow } from 'electron';
2 2
3const debug = require('debug')('Ferdium:feature:basicAuth:main'); 3const debug = require('../../preload-safe-debug')('Ferdium:feature:basicAuth:main');
4 4
5export default function mainIpcHandler(mainWindow: BrowserWindow, authInfo) { 5export default function mainIpcHandler(mainWindow: BrowserWindow, authInfo) {
6 debug('Sending basic auth call', authInfo); 6 debug('Sending basic auth call', authInfo);
diff --git a/src/features/basicAuth/store.ts b/src/features/basicAuth/store.ts
index 4b71d32fd..e0ae8ba17 100644
--- a/src/features/basicAuth/store.ts
+++ b/src/features/basicAuth/store.ts
@@ -1,7 +1,7 @@
1import { observable } from 'mobx'; 1import { observable } from 'mobx';
2import { ipcRenderer } from 'electron'; 2import { ipcRenderer } from 'electron';
3 3
4const debug = require('debug')('Ferdium:feature:basicAuth'); 4const debug = require('../../preload-safe-debug')('Ferdium:feature:basicAuth');
5 5
6const defaultState = { 6const defaultState = {
7 isModalVisible: true, 7 isModalVisible: true,
diff --git a/src/features/communityRecipes/store.ts b/src/features/communityRecipes/store.ts
index afd7d0f01..73eaae8b4 100644
--- a/src/features/communityRecipes/store.ts
+++ b/src/features/communityRecipes/store.ts
@@ -1,8 +1,7 @@
1import { computed } from 'mobx'; 1import { computed } from 'mobx';
2import { FeatureStore } from '../utils/FeatureStore'; 2import { FeatureStore } from '../utils/FeatureStore';
3 3
4// TODO: Go back to 'debug' from 'console.log' when https://github.com/electron/electron/issues/31689 is fixed 4const debug = require('../../preload-safe-debug')('Ferdium:feature:communityRecipes:store');
5// const debug = require('debug')('Ferdium:feature:communityRecipes:store');
6 5
7export class CommunityRecipesStore extends FeatureStore { 6export class CommunityRecipesStore extends FeatureStore {
8 stores: any; 7 stores: any;
@@ -10,13 +9,13 @@ export class CommunityRecipesStore extends FeatureStore {
10 actions: any; 9 actions: any;
11 10
12 start(stores: any, actions: any) { 11 start(stores: any, actions: any) {
13 console.log('start'); 12 debug('start');
14 this.stores = stores; 13 this.stores = stores;
15 this.actions = actions; 14 this.actions = actions;
16 } 15 }
17 16
18 stop() { 17 stop() {
19 console.log('stop'); 18 debug('stop');
20 super.stop(); 19 super.stop();
21 } 20 }
22 21
diff --git a/src/features/publishDebugInfo/Component.js b/src/features/publishDebugInfo/Component.js
index 3d4e85dbf..ff052a050 100644
--- a/src/features/publishDebugInfo/Component.js
+++ b/src/features/publishDebugInfo/Component.js
@@ -14,7 +14,7 @@ import { DEBUG_API } from '../../config';
14import AppStore from '../../stores/AppStore'; 14import AppStore from '../../stores/AppStore';
15import ServicesStore from '../../stores/ServicesStore'; 15import ServicesStore from '../../stores/ServicesStore';
16 16
17const debug = require('debug')('Ferdium:feature:publishDebugInfo'); 17const debug = require('../../preload-safe-debug')('Ferdium:feature:publishDebugInfo');
18 18
19const messages = defineMessages({ 19const messages = defineMessages({
20 title: { 20 title: {
diff --git a/src/features/publishDebugInfo/index.ts b/src/features/publishDebugInfo/index.ts
index 33b8eb6f5..597bcdc12 100644
--- a/src/features/publishDebugInfo/index.ts
+++ b/src/features/publishDebugInfo/index.ts
@@ -3,7 +3,7 @@ import { state as ModalState } from './store';
3export { default as Component } from './Component'; 3export { default as Component } from './Component';
4 4
5const state = ModalState; 5const state = ModalState;
6const debug = require('debug')('Ferdium:feature:publishDebugInfo'); 6const debug = require('../../preload-safe-debug')('Ferdium:feature:publishDebugInfo');
7 7
8export default function initialize() { 8export default function initialize() {
9 debug('Initialize publishDebugInfo feature'); 9 debug('Initialize publishDebugInfo feature');
diff --git a/src/features/quickSwitch/index.ts b/src/features/quickSwitch/index.ts
index 9d584dc62..9d53d0b2f 100644
--- a/src/features/quickSwitch/index.ts
+++ b/src/features/quickSwitch/index.ts
@@ -3,7 +3,7 @@ import { state as ModalState } from './store';
3export { default as Component } from './Component'; 3export { default as Component } from './Component';
4const state = ModalState; 4const state = ModalState;
5 5
6const debug = require('debug')('Ferdium:feature:quickSwitch'); 6const debug = require('../../preload-safe-debug')('Ferdium:feature:quickSwitch');
7 7
8export default function initialize() { 8export default function initialize() {
9 debug('Initialize quickSwitch feature'); 9 debug('Initialize quickSwitch feature');
diff --git a/src/features/serviceProxy/index.ts b/src/features/serviceProxy/index.ts
index b3705b190..e0d667a72 100644
--- a/src/features/serviceProxy/index.ts
+++ b/src/features/serviceProxy/index.ts
@@ -1,8 +1,7 @@
1import { autorun, observable } from 'mobx'; 1import { autorun, observable } from 'mobx';
2import { session } from '@electron/remote'; 2import { session } from '@electron/remote';
3 3
4// TODO: Go back to 'debug' from 'console.log' when https://github.com/electron/electron/issues/31689 is fixed 4const debug = require('../../preload-safe-debug')('Ferdium:feature:serviceProxy');
5// const debug = require('debug')('Ferdium:feature:serviceProxy');
6 5
7export const config = observable({ 6export const config = observable({
8 isEnabled: true, 7 isEnabled: true,
@@ -12,7 +11,7 @@ export default function init(stores: {
12 services: { enabled: any }; 11 services: { enabled: any };
13 settings: { proxy: any }; 12 settings: { proxy: any };
14}) { 13}) {
15 console.log('Initializing `serviceProxy` feature'); 14 debug('Initializing `serviceProxy` feature');
16 15
17 autorun(() => { 16 autorun(() => {
18 config.isEnabled = true; 17 config.isEnabled = true;
@@ -20,7 +19,7 @@ export default function init(stores: {
20 const services = stores.services.enabled; 19 const services = stores.services.enabled;
21 const proxySettings = stores.settings.proxy; 20 const proxySettings = stores.settings.proxy;
22 21
23 console.log('Service Proxy autorun'); 22 debug('Service Proxy autorun');
24 23
25 for (const service of services) { 24 for (const service of services) {
26 const s = session.fromPartition(`persist:service-${service.id}`); 25 const s = session.fromPartition(`persist:service-${service.id}`);
@@ -36,14 +35,14 @@ export default function init(stores: {
36 const proxyHost = `${serviceProxyConfig.host}${ 35 const proxyHost = `${serviceProxyConfig.host}${
37 serviceProxyConfig.port ? `:${serviceProxyConfig.port}` : '' 36 serviceProxyConfig.port ? `:${serviceProxyConfig.port}` : ''
38 }`; 37 }`;
39 console.log( 38 debug(
40 `Setting proxy config from service settings for "${service.name}" (${service.id}) to`, 39 `Setting proxy config from service settings for "${service.name}" (${service.id}) to`,
41 proxyHost, 40 proxyHost,
42 ); 41 );
43 42
44 s.setProxy({ proxyRules: proxyHost }) 43 s.setProxy({ proxyRules: proxyHost })
45 .then(() => { 44 .then(() => {
46 console.log( 45 debug(
47 `Using proxy "${proxyHost}" for "${service.name}" (${service.id})`, 46 `Using proxy "${proxyHost}" for "${service.name}" (${service.id})`,
48 ); 47 );
49 }) 48 })
diff --git a/src/features/todos/preload.ts b/src/features/todos/preload.ts
index 4ccee39a0..6c8bc1aea 100644
--- a/src/features/todos/preload.ts
+++ b/src/features/todos/preload.ts
@@ -1,10 +1,9 @@
1import { ipcRenderer } from 'electron'; 1import { ipcRenderer } from 'electron';
2import { IPC } from './constants'; 2import { IPC } from './constants';
3 3
4// TODO: Go back to 'debug' from 'console.log' when https://github.com/electron/electron/issues/31689 is fixed 4const debug = require('../../preload-safe-debug')('Ferdium:feature:todos:preload');
5// const debug = require('debug')('Ferdium:feature:todos:preload');
6 5
7console.log('Preloading Todos Webview'); 6debug('Preloading Todos Webview');
8 7
9let hostMessageListener = ({ action }) => { 8let hostMessageListener = ({ action }) => {
10 switch (action) { 9 switch (action) {
@@ -28,7 +27,7 @@ window['ferdium'] = {
28}; 27};
29 28
30ipcRenderer.on(IPC.TODOS_HOST_CHANNEL, (event, message) => { 29ipcRenderer.on(IPC.TODOS_HOST_CHANNEL, (event, message) => {
31 console.log('Received host message', event, message); 30 debug('Received host message', event, message);
32 hostMessageListener(message); 31 hostMessageListener(message);
33}); 32});
34 33
diff --git a/src/features/todos/store.js b/src/features/todos/store.js
index 41e632b49..9ece76327 100644
--- a/src/features/todos/store.js
+++ b/src/features/todos/store.js
@@ -18,8 +18,7 @@ import { createActionBindings } from '../utils/ActionBinding';
18import { IPC, TODOS_ROUTES } from './constants'; 18import { IPC, TODOS_ROUTES } from './constants';
19import UserAgent from '../../models/UserAgent'; 19import UserAgent from '../../models/UserAgent';
20 20
21// TODO: Go back to 'debug' from 'console.log' when https://github.com/electron/electron/issues/31689 is fixed 21const debug = require('../../preload-safe-debug')('Ferdium:feature:todos:store');
22// const debug = require('debug')('Ferdium:feature:todos:store');
23 22
24export default class TodoStore extends FeatureStore { 23export default class TodoStore extends FeatureStore {
25 @observable stores = null; 24 @observable stores = null;
@@ -97,7 +96,7 @@ export default class TodoStore extends FeatureStore {
97 // ========== PUBLIC API ========= // 96 // ========== PUBLIC API ========= //
98 97
99 @action start(stores, actions) { 98 @action start(stores, actions) {
100 console.log('TodoStore::start'); 99 debug('TodoStore::start');
101 this.stores = stores; 100 this.stores = stores;
102 this.actions = actions; 101 this.actions = actions;
103 102
@@ -134,7 +133,7 @@ export default class TodoStore extends FeatureStore {
134 133
135 @action stop() { 134 @action stop() {
136 super.stop(); 135 super.stop();
137 console.log('TodoStore::stop'); 136 debug('TodoStore::stop');
138 this.reset(); 137 this.reset();
139 this.isFeatureActive = false; 138 this.isFeatureActive = false;
140 } 139 }
@@ -163,7 +162,7 @@ export default class TodoStore extends FeatureStore {
163 }; 162 };
164 163
165 @action _setTodosWebview = ({ webview }) => { 164 @action _setTodosWebview = ({ webview }) => {
166 console.log('_setTodosWebview', webview); 165 debug('_setTodosWebview', webview);
167 if (this.webview !== webview) { 166 if (this.webview !== webview) {
168 this.webview = webview; 167 this.webview = webview;
169 this.userAgentModel.setWebviewReference(webview); 168 this.userAgentModel.setWebviewReference(webview);
@@ -171,14 +170,14 @@ export default class TodoStore extends FeatureStore {
171 }; 170 };
172 171
173 @action _handleHostMessage = message => { 172 @action _handleHostMessage = message => {
174 console.log('_handleHostMessage', message); 173 debug('_handleHostMessage', message);
175 if (message.action === 'todos:create') { 174 if (message.action === 'todos:create') {
176 this.webview.send(IPC.TODOS_HOST_CHANNEL, message); 175 this.webview.send(IPC.TODOS_HOST_CHANNEL, message);
177 } 176 }
178 }; 177 };
179 178
180 @action _handleClientMessage = ({ channel, message = {} }) => { 179 @action _handleClientMessage = ({ channel, message = {} }) => {
181 console.log('_handleClientMessage', channel, message); 180 debug('_handleClientMessage', channel, message);
182 switch (message.action) { 181 switch (message.action) {
183 case 'todos:initialized': 182 case 'todos:initialized':
184 this._onTodosClientInitialized(); 183 this._onTodosClientInitialized();
@@ -187,7 +186,7 @@ export default class TodoStore extends FeatureStore {
187 this._goToService(message.data); 186 this._goToService(message.data);
188 break; 187 break;
189 default: 188 default:
190 console.log('Other message received', channel, message); 189 debug('Other message received', channel, message);
191 if (this.stores.services.isTodosServiceAdded) { 190 if (this.stores.services.isTodosServiceAdded) {
192 this.actions.service.handleIPCMessage({ 191 this.actions.service.handleIPCMessage({
193 serviceId: this.stores.services.isTodosServiceAdded.id, 192 serviceId: this.stores.services.isTodosServiceAdded.id,
@@ -203,7 +202,7 @@ export default class TodoStore extends FeatureStore {
203 }; 202 };
204 203
205 @action _toggleTodosFeatureVisibility = () => { 204 @action _toggleTodosFeatureVisibility = () => {
206 console.log('_toggleTodosFeatureVisibility'); 205 debug('_toggleTodosFeatureVisibility');
207 206
208 this._updateSettings({ 207 this._updateSettings({
209 isFeatureEnabledByUser: !this.settings.isFeatureEnabledByUser, 208 isFeatureEnabledByUser: !this.settings.isFeatureEnabledByUser,
@@ -211,14 +210,14 @@ export default class TodoStore extends FeatureStore {
211 }; 210 };
212 211
213 _openDevTools = () => { 212 _openDevTools = () => {
214 console.log('_openDevTools'); 213 debug('_openDevTools');
215 214
216 const webview = document.querySelector('#todos-panel webview'); 215 const webview = document.querySelector('#todos-panel webview');
217 if (webview) webview.openDevTools(); 216 if (webview) webview.openDevTools();
218 }; 217 };
219 218
220 _reload = () => { 219 _reload = () => {
221 console.log('_reload'); 220 debug('_reload');
222 221
223 const webview = document.querySelector('#todos-panel webview'); 222 const webview = document.querySelector('#todos-panel webview');
224 if (webview) webview.reload(); 223 if (webview) webview.reload();
@@ -286,7 +285,7 @@ export default class TodoStore extends FeatureStore {
286 const { pathname } = this.stores.router.location; 285 const { pathname } = this.stores.router.location;
287 286
288 if (pathname === TODOS_ROUTES.TARGET) { 287 if (pathname === TODOS_ROUTES.TARGET) {
289 console.log('Router is on todos route, show todos panel'); 288 debug('Router is on todos route, show todos panel');
290 // todosStore.start(stores, actions); 289 // todosStore.start(stores, actions);
291 this.stores.router.push('/'); 290 this.stores.router.push('/');
292 291
diff --git a/src/features/workspaces/api.ts b/src/features/workspaces/api.ts
index 582433527..fb752c565 100644
--- a/src/features/workspaces/api.ts
+++ b/src/features/workspaces/api.ts
@@ -4,15 +4,14 @@ import Request from '../../stores/lib/Request';
4import Workspace from './models/Workspace'; 4import Workspace from './models/Workspace';
5import apiBase from '../../api/apiBase'; 5import apiBase from '../../api/apiBase';
6 6
7// TODO: Go back to 'debug' from 'console.log' when https://github.com/electron/electron/issues/31689 is fixed 7const debug = require('../../preload-safe-debug')('Ferdium:feature:workspaces:api');
8// const debug = require('debug')('Ferdium:feature:workspaces:api');
9 8
10export const workspaceApi = { 9export const workspaceApi = {
11 getUserWorkspaces: async () => { 10 getUserWorkspaces: async () => {
12 const url = `${apiBase()}/workspace`; 11 const url = `${apiBase()}/workspace`;
13 console.log('getUserWorkspaces GET', url); 12 debug('getUserWorkspaces GET', url);
14 const result = await sendAuthRequest(url, { method: 'GET' }); 13 const result = await sendAuthRequest(url, { method: 'GET' });
15 console.log('getUserWorkspaces RESULT', result); 14 debug('getUserWorkspaces RESULT', result);
16 if (!result.ok) { 15 if (!result.ok) {
17 throw new Error("Couldn't getUserWorkspaces"); 16 throw new Error("Couldn't getUserWorkspaces");
18 } 17 }
@@ -26,9 +25,9 @@ export const workspaceApi = {
26 method: 'POST', 25 method: 'POST',
27 body: JSON.stringify({ name }), 26 body: JSON.stringify({ name }),
28 }; 27 };
29 console.log('createWorkspace POST', url, options); 28 debug('createWorkspace POST', url, options);
30 const result = await sendAuthRequest(url, options); 29 const result = await sendAuthRequest(url, options);
31 console.log('createWorkspace RESULT', result); 30 debug('createWorkspace RESULT', result);
32 if (!result.ok) { 31 if (!result.ok) {
33 throw new Error("Couldn't createWorkspace"); 32 throw new Error("Couldn't createWorkspace");
34 } 33 }
@@ -37,9 +36,9 @@ export const workspaceApi = {
37 36
38 deleteWorkspace: async workspace => { 37 deleteWorkspace: async workspace => {
39 const url = `${apiBase()}/workspace/${workspace.id}`; 38 const url = `${apiBase()}/workspace/${workspace.id}`;
40 console.log('deleteWorkspace DELETE', url); 39 debug('deleteWorkspace DELETE', url);
41 const result = await sendAuthRequest(url, { method: 'DELETE' }); 40 const result = await sendAuthRequest(url, { method: 'DELETE' });
42 console.log('deleteWorkspace RESULT', result); 41 debug('deleteWorkspace RESULT', result);
43 if (!result.ok) { 42 if (!result.ok) {
44 throw new Error("Couldn't deleteWorkspace"); 43 throw new Error("Couldn't deleteWorkspace");
45 } 44 }
@@ -52,9 +51,9 @@ export const workspaceApi = {
52 method: 'PUT', 51 method: 'PUT',
53 body: JSON.stringify(pick(workspace, ['name', 'services'])), 52 body: JSON.stringify(pick(workspace, ['name', 'services'])),
54 }; 53 };
55 console.log('updateWorkspace UPDATE', url, options); 54 debug('updateWorkspace UPDATE', url, options);
56 const result = await sendAuthRequest(url, options); 55 const result = await sendAuthRequest(url, options);
57 console.log('updateWorkspace RESULT', result); 56 debug('updateWorkspace RESULT', result);
58 if (!result.ok) { 57 if (!result.ok) {
59 throw new Error("Couldn't updateWorkspace"); 58 throw new Error("Couldn't updateWorkspace");
60 } 59 }
diff --git a/src/features/workspaces/store.js b/src/features/workspaces/store.js
index d2ccfeccf..20d32df67 100644
--- a/src/features/workspaces/store.js
+++ b/src/features/workspaces/store.js
@@ -15,8 +15,7 @@ import { createActionBindings } from '../utils/ActionBinding';
15 15
16import { KEEP_WS_LOADED_USID } from '../../config'; 16import { KEEP_WS_LOADED_USID } from '../../config';
17 17
18// TODO: Go back to 'debug' from 'console.log' when https://github.com/electron/electron/issues/31689 is fixed 18const debug = require('../../preload-safe-debug')('Ferdium:feature:workspaces:store');
19// const debug = require('debug')('Ferdium:feature:workspaces:store');
20 19
21export default class WorkspacesStore extends FeatureStore { 20export default class WorkspacesStore extends FeatureStore {
22 @observable isFeatureActive = false; 21 @observable isFeatureActive = false;
@@ -70,7 +69,7 @@ export default class WorkspacesStore extends FeatureStore {
70 // ========== PUBLIC API ========= // 69 // ========== PUBLIC API ========= //
71 70
72 @action start(stores, actions) { 71 @action start(stores, actions) {
73 console.log('WorkspacesStore::start'); 72 debug('WorkspacesStore::start');
74 this.stores = stores; 73 this.stores = stores;
75 this.actions = actions; 74 this.actions = actions;
76 75
@@ -116,7 +115,7 @@ export default class WorkspacesStore extends FeatureStore {
116 115
117 @action stop() { 116 @action stop() {
118 super.stop(); 117 super.stop();
119 console.log('WorkspacesStore::stop'); 118 debug('WorkspacesStore::stop');
120 this.reset(); 119 this.reset();
121 this.isFeatureActive = false; 120 this.isFeatureActive = false;
122 } 121 }
@@ -274,7 +273,7 @@ export default class WorkspacesStore extends FeatureStore {
274 }; 273 };
275 274
276 _activateLastUsedWorkspaceReaction = () => { 275 _activateLastUsedWorkspaceReaction = () => {
277 console.log('_activateLastUsedWorkspaceReaction'); 276 debug('_activateLastUsedWorkspaceReaction');
278 if (!this.activeWorkspace && this.userHasWorkspaces) { 277 if (!this.activeWorkspace && this.userHasWorkspaces) {
279 const { lastActiveWorkspace } = this.settings; 278 const { lastActiveWorkspace } = this.settings;
280 if (lastActiveWorkspace) { 279 if (lastActiveWorkspace) {