diff options
author | Kristóf Marussy <kristof@marussy.com> | 2022-04-23 01:59:21 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-22 23:59:21 +0000 |
commit | d02644f7c41150709795e57bfd40351b4da35a7b (patch) | |
tree | 2403fb76bd5fae1703f8b55172ffce9e0a5d2bce /src/models/Service.js | |
parent | Complete tray icons redesign for all platforms (#28) (diff) | |
download | ferdium-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/models/Service.js')
-rw-r--r-- | src/models/Service.js | 31 |
1 files changed, 15 insertions, 16 deletions
diff --git a/src/models/Service.js b/src/models/Service.js index 9f345375f..1fca034bc 100644 --- a/src/models/Service.js +++ b/src/models/Service.js | |||
@@ -14,8 +14,7 @@ import { | |||
14 | ifUndefinedNumber, | 14 | ifUndefinedNumber, |
15 | } from '../jsUtils'; | 15 | } from '../jsUtils'; |
16 | 16 | ||
17 | // TODO: Go back to 'debug' from 'console.log' when https://github.com/electron/electron/issues/31689 is fixed | 17 | const debug = require('../preload-safe-debug')('Ferdium:Service'); |
18 | // const debug = require('debug')('Ferdium:Service'); | ||
19 | 18 | ||
20 | export default class Service { | 19 | export default class Service { |
21 | id = ''; | 20 | id = ''; |
@@ -306,25 +305,25 @@ export default class Service { | |||
306 | // Send those headers to ipcMain so that it can be set in session | 305 | // Send those headers to ipcMain so that it can be set in session |
307 | if (typeof this.recipe.modifyRequestHeaders === 'function') { | 306 | if (typeof this.recipe.modifyRequestHeaders === 'function') { |
308 | const modifiedRequestHeaders = this.recipe.modifyRequestHeaders(); | 307 | const modifiedRequestHeaders = this.recipe.modifyRequestHeaders(); |
309 | console.log(this.name, 'modifiedRequestHeaders', modifiedRequestHeaders); | 308 | debug(this.name, 'modifiedRequestHeaders', modifiedRequestHeaders); |
310 | ipcRenderer.send('modifyRequestHeaders', { | 309 | ipcRenderer.send('modifyRequestHeaders', { |
311 | modifiedRequestHeaders, | 310 | modifiedRequestHeaders, |
312 | serviceId: this.id, | 311 | serviceId: this.id, |
313 | }); | 312 | }); |
314 | } else { | 313 | } else { |
315 | console.log(this.name, 'modifyRequestHeaders is not defined in the recipe'); | 314 | debug(this.name, 'modifyRequestHeaders is not defined in the recipe'); |
316 | } | 315 | } |
317 | 316 | ||
318 | // if the recipe has implemented 'knownCertificateHosts' | 317 | // if the recipe has implemented 'knownCertificateHosts' |
319 | if (typeof this.recipe.knownCertificateHosts === 'function') { | 318 | if (typeof this.recipe.knownCertificateHosts === 'function') { |
320 | const knownHosts = this.recipe.knownCertificateHosts(); | 319 | const knownHosts = this.recipe.knownCertificateHosts(); |
321 | console.log(this.name, 'knownCertificateHosts', knownHosts); | 320 | debug(this.name, 'knownCertificateHosts', knownHosts); |
322 | ipcRenderer.send('knownCertificateHosts', { | 321 | ipcRenderer.send('knownCertificateHosts', { |
323 | knownHosts, | 322 | knownHosts, |
324 | serviceId: this.id, | 323 | serviceId: this.id, |
325 | }); | 324 | }); |
326 | } else { | 325 | } else { |
327 | console.log(this.name, 'knownCertificateHosts is not defined in the recipe'); | 326 | debug(this.name, 'knownCertificateHosts is not defined in the recipe'); |
328 | } | 327 | } |
329 | 328 | ||
330 | this.webview.addEventListener('ipc-message', async e => { | 329 | this.webview.addEventListener('ipc-message', async e => { |
@@ -348,7 +347,7 @@ export default class Service { | |||
348 | this.webview.addEventListener( | 347 | this.webview.addEventListener( |
349 | 'new-window', | 348 | 'new-window', |
350 | (event, url, frameName, options) => { | 349 | (event, url, frameName, options) => { |
351 | console.log('new-window', event, url, frameName, options); | 350 | debug('new-window', event, url, frameName, options); |
352 | if (!isValidExternalURL(event.url)) { | 351 | if (!isValidExternalURL(event.url)) { |
353 | return; | 352 | return; |
354 | } | 353 | } |
@@ -372,7 +371,7 @@ export default class Service { | |||
372 | ); | 371 | ); |
373 | 372 | ||
374 | this.webview.addEventListener('did-start-loading', event => { | 373 | this.webview.addEventListener('did-start-loading', event => { |
375 | console.log('Did start load', this.name, event); | 374 | debug('Did start load', this.name, event); |
376 | 375 | ||
377 | this.hasCrashed = false; | 376 | this.hasCrashed = false; |
378 | this.isLoading = true; | 377 | this.isLoading = true; |
@@ -391,7 +390,7 @@ export default class Service { | |||
391 | this.webview.addEventListener('did-navigate', didLoad.bind(this)); | 390 | this.webview.addEventListener('did-navigate', didLoad.bind(this)); |
392 | 391 | ||
393 | this.webview.addEventListener('did-fail-load', event => { | 392 | this.webview.addEventListener('did-fail-load', event => { |
394 | console.log('Service failed to load', this.name, event); | 393 | debug('Service failed to load', this.name, event); |
395 | if ( | 394 | if ( |
396 | event.isMainFrame && | 395 | event.isMainFrame && |
397 | event.errorCode !== -21 && | 396 | event.errorCode !== -21 && |
@@ -404,33 +403,33 @@ export default class Service { | |||
404 | }); | 403 | }); |
405 | 404 | ||
406 | this.webview.addEventListener('crashed', () => { | 405 | this.webview.addEventListener('crashed', () => { |
407 | console.log('Service crashed', this.name); | 406 | debug('Service crashed', this.name); |
408 | this.hasCrashed = true; | 407 | this.hasCrashed = true; |
409 | }); | 408 | }); |
410 | 409 | ||
411 | this.webview.addEventListener('found-in-page', ({ result }) => { | 410 | this.webview.addEventListener('found-in-page', ({ result }) => { |
412 | console.log('Found in page', result); | 411 | debug('Found in page', result); |
413 | this.webview.send('found-in-page', result); | 412 | this.webview.send('found-in-page', result); |
414 | }); | 413 | }); |
415 | 414 | ||
416 | webviewWebContents.on('login', (event, request, authInfo, callback) => { | 415 | webviewWebContents.on('login', (event, request, authInfo, callback) => { |
417 | // const authCallback = callback; | 416 | // const authCallback = callback; |
418 | console.log('browser login event', authInfo); | 417 | debug('browser login event', authInfo); |
419 | event.preventDefault(); | 418 | event.preventDefault(); |
420 | 419 | ||
421 | if (authInfo.isProxy && authInfo.scheme === 'basic') { | 420 | if (authInfo.isProxy && authInfo.scheme === 'basic') { |
422 | console.log('Sending service echo ping'); | 421 | debug('Sending service echo ping'); |
423 | webviewWebContents.send('get-service-id'); | 422 | webviewWebContents.send('get-service-id'); |
424 | 423 | ||
425 | console.log('Received service id', this.id); | 424 | debug('Received service id', this.id); |
426 | 425 | ||
427 | const ps = stores.settings.proxy[this.id]; | 426 | const ps = stores.settings.proxy[this.id]; |
428 | 427 | ||
429 | if (ps) { | 428 | if (ps) { |
430 | console.log('Sending proxy auth callback for service', this.id); | 429 | debug('Sending proxy auth callback for service', this.id); |
431 | callback(ps.user, ps.password); | 430 | callback(ps.user, ps.password); |
432 | } else { | 431 | } else { |
433 | console.log('No proxy auth config found for', this.id); | 432 | debug('No proxy auth config found for', this.id); |
434 | } | 433 | } |
435 | } | 434 | } |
436 | }); | 435 | }); |