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/api | |
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/api')
-rw-r--r-- | src/api/server/LocalApi.ts | 9 | ||||
-rw-r--r-- | src/api/server/ServerApi.ts | 61 |
2 files changed, 34 insertions, 36 deletions
diff --git a/src/api/server/LocalApi.ts b/src/api/server/LocalApi.ts index 28028bf80..a292bc42d 100644 --- a/src/api/server/LocalApi.ts +++ b/src/api/server/LocalApi.ts | |||
@@ -4,15 +4,14 @@ import fastFolderSize from 'fast-folder-size'; | |||
4 | 4 | ||
5 | import { getServicePartitionsDirectory } from '../../helpers/service-helpers'; | 5 | import { getServicePartitionsDirectory } from '../../helpers/service-helpers'; |
6 | 6 | ||
7 | // TODO: Go back to 'debug' from 'console.log' when https://github.com/electron/electron/issues/31689 is fixed | 7 | const debug = require('../../preload-safe-debug')('Ferdium:LocalApi'); |
8 | // const debug = require('debug')('Ferdium:LocalApi'); | ||
9 | 8 | ||
10 | export default class LocalApi { | 9 | export default class LocalApi { |
11 | // Settings | 10 | // Settings |
12 | getAppSettings(type: string) { | 11 | getAppSettings(type: string) { |
13 | return new Promise(resolve => { | 12 | return new Promise(resolve => { |
14 | ipcRenderer.once('appSettings', (_event, resp) => { | 13 | ipcRenderer.once('appSettings', (_event, resp) => { |
15 | console.log('LocalApi::getAppSettings resolves', resp.type, resp.data); | 14 | debug('LocalApi::getAppSettings resolves', resp.type, resp.data); |
16 | resolve(resp); | 15 | resolve(resp); |
17 | }); | 16 | }); |
18 | 17 | ||
@@ -21,7 +20,7 @@ export default class LocalApi { | |||
21 | } | 20 | } |
22 | 21 | ||
23 | async updateAppSettings(type: string, data: any) { | 22 | async updateAppSettings(type: string, data: any) { |
24 | console.log('LocalApi::updateAppSettings resolves', type, data); | 23 | debug('LocalApi::updateAppSettings resolves', type, data); |
25 | ipcRenderer.send('updateAppSettings', { | 24 | ipcRenderer.send('updateAppSettings', { |
26 | type, | 25 | type, |
27 | data, | 26 | data, |
@@ -40,7 +39,7 @@ export default class LocalApi { | |||
40 | reject(err); | 39 | reject(err); |
41 | } | 40 | } |
42 | 41 | ||
43 | console.log('LocalApi::getAppCacheSize resolves', bytes); | 42 | debug('LocalApi::getAppCacheSize resolves', bytes); |
44 | resolve(bytes); | 43 | resolve(bytes); |
45 | }, | 44 | }, |
46 | ); | 45 | ); |
diff --git a/src/api/server/ServerApi.ts b/src/api/server/ServerApi.ts index dc29c7b89..935d7de3d 100644 --- a/src/api/server/ServerApi.ts +++ b/src/api/server/ServerApi.ts | |||
@@ -35,8 +35,7 @@ import { | |||
35 | 35 | ||
36 | import { removeServicePartitionDirectory } from '../../helpers/service-helpers'; | 36 | import { removeServicePartitionDirectory } from '../../helpers/service-helpers'; |
37 | 37 | ||
38 | // TODO: Go back to 'debug' from 'console.log' when https://github.com/electron/electron/issues/31689 is fixed | 38 | const debug = require('../../preload-safe-debug')('Ferdium:ServerApi'); |
39 | // const debug = require('debug')('Ferdium:ServerApi'); | ||
40 | 39 | ||
41 | module.paths.unshift(getDevRecipeDirectory(), getRecipeDirectory()); | 40 | module.paths.unshift(getDevRecipeDirectory(), getRecipeDirectory()); |
42 | 41 | ||
@@ -62,7 +61,7 @@ export default class ServerApi { | |||
62 | } | 61 | } |
63 | const u = await request.json(); | 62 | const u = await request.json(); |
64 | 63 | ||
65 | console.log('ServerApi::login resolves', u); | 64 | debug('ServerApi::login resolves', u); |
66 | return u.token; | 65 | return u.token; |
67 | } | 66 | } |
68 | 67 | ||
@@ -80,7 +79,7 @@ export default class ServerApi { | |||
80 | } | 79 | } |
81 | const u = await request.json(); | 80 | const u = await request.json(); |
82 | 81 | ||
83 | console.log('ServerApi::signup resolves', u); | 82 | debug('ServerApi::signup resolves', u); |
84 | return u.token; | 83 | return u.token; |
85 | } | 84 | } |
86 | 85 | ||
@@ -93,7 +92,7 @@ export default class ServerApi { | |||
93 | throw new Error(request.statusText); | 92 | throw new Error(request.statusText); |
94 | } | 93 | } |
95 | 94 | ||
96 | console.log('ServerApi::inviteUser'); | 95 | debug('ServerApi::inviteUser'); |
97 | return true; | 96 | return true; |
98 | } | 97 | } |
99 | 98 | ||
@@ -113,7 +112,7 @@ export default class ServerApi { | |||
113 | } | 112 | } |
114 | const r = await request.json(); | 113 | const r = await request.json(); |
115 | 114 | ||
116 | console.log('ServerApi::retrievePassword'); | 115 | debug('ServerApi::retrievePassword'); |
117 | return r; | 116 | return r; |
118 | } | 117 | } |
119 | 118 | ||
@@ -129,7 +128,7 @@ export default class ServerApi { | |||
129 | const data = await request.json(); | 128 | const data = await request.json(); |
130 | 129 | ||
131 | const user = new UserModel(data); | 130 | const user = new UserModel(data); |
132 | console.log('ServerApi::userInfo resolves', user); | 131 | debug('ServerApi::userInfo resolves', user); |
133 | 132 | ||
134 | return user; | 133 | return user; |
135 | } | 134 | } |
@@ -147,7 +146,7 @@ export default class ServerApi { | |||
147 | const user = Object.assign(updatedData, { | 146 | const user = Object.assign(updatedData, { |
148 | data: new UserModel(updatedData.data), | 147 | data: new UserModel(updatedData.data), |
149 | }); | 148 | }); |
150 | console.log('ServerApi::updateUserInfo resolves', user); | 149 | debug('ServerApi::updateUserInfo resolves', user); |
151 | return user; | 150 | return user; |
152 | } | 151 | } |
153 | 152 | ||
@@ -160,7 +159,7 @@ export default class ServerApi { | |||
160 | } | 159 | } |
161 | const data = await request.json(); | 160 | const data = await request.json(); |
162 | 161 | ||
163 | console.log('ServerApi::deleteAccount resolves', data); | 162 | debug('ServerApi::deleteAccount resolves', data); |
164 | return data; | 163 | return data; |
165 | } | 164 | } |
166 | 165 | ||
@@ -178,7 +177,7 @@ export default class ServerApi { | |||
178 | 177 | ||
179 | const services = await this._mapServiceModels(data); | 178 | const services = await this._mapServiceModels(data); |
180 | const filteredServices = services.filter(service => !!service); | 179 | const filteredServices = services.filter(service => !!service); |
181 | console.log('ServerApi::getServices resolves', filteredServices); | 180 | debug('ServerApi::getServices resolves', filteredServices); |
182 | return filteredServices; | 181 | return filteredServices; |
183 | } | 182 | } |
184 | 183 | ||
@@ -205,7 +204,7 @@ export default class ServerApi { | |||
205 | data: await this._prepareServiceModel(serviceData.data), | 204 | data: await this._prepareServiceModel(serviceData.data), |
206 | }); | 205 | }); |
207 | 206 | ||
208 | console.log('ServerApi::createService resolves', service); | 207 | debug('ServerApi::createService resolves', service); |
209 | return service; | 208 | return service; |
210 | } | 209 | } |
211 | 210 | ||
@@ -231,7 +230,7 @@ export default class ServerApi { | |||
231 | data: await this._prepareServiceModel(serviceData.data), | 230 | data: await this._prepareServiceModel(serviceData.data), |
232 | }); | 231 | }); |
233 | 232 | ||
234 | console.log('ServerApi::updateService resolves', service); | 233 | debug('ServerApi::updateService resolves', service); |
235 | return service; | 234 | return service; |
236 | } | 235 | } |
237 | 236 | ||
@@ -271,7 +270,7 @@ export default class ServerApi { | |||
271 | throw new Error(request.statusText); | 270 | throw new Error(request.statusText); |
272 | } | 271 | } |
273 | const serviceData = await request.json(); | 272 | const serviceData = await request.json(); |
274 | console.log('ServerApi::reorderService resolves', serviceData); | 273 | debug('ServerApi::reorderService resolves', serviceData); |
275 | return serviceData; | 274 | return serviceData; |
276 | } | 275 | } |
277 | 276 | ||
@@ -286,7 +285,7 @@ export default class ServerApi { | |||
286 | 285 | ||
287 | removeServicePartitionDirectory(id, true); | 286 | removeServicePartitionDirectory(id, true); |
288 | 287 | ||
289 | console.log('ServerApi::deleteService resolves', data); | 288 | debug('ServerApi::deleteService resolves', data); |
290 | return data; | 289 | return data; |
291 | } | 290 | } |
292 | 291 | ||
@@ -299,7 +298,7 @@ export default class ServerApi { | |||
299 | const data = await request.json(); | 298 | const data = await request.json(); |
300 | 299 | ||
301 | const features = data; | 300 | const features = data; |
302 | console.log('ServerApi::getDefaultFeatures resolves', features); | 301 | debug('ServerApi::getDefaultFeatures resolves', features); |
303 | return features; | 302 | return features; |
304 | } | 303 | } |
305 | 304 | ||
@@ -315,7 +314,7 @@ export default class ServerApi { | |||
315 | const data = await request.json(); | 314 | const data = await request.json(); |
316 | 315 | ||
317 | const features = data; | 316 | const features = data; |
318 | console.log('ServerApi::getFeatures resolves', features); | 317 | debug('ServerApi::getFeatures resolves', features); |
319 | return features; | 318 | return features; |
320 | } | 319 | } |
321 | 320 | ||
@@ -340,7 +339,7 @@ export default class ServerApi { | |||
340 | // eslint-disable-next-line unicorn/prefer-spread | 339 | // eslint-disable-next-line unicorn/prefer-spread |
341 | this.recipes = this.recipes.concat(this._getDevRecipes()); | 340 | this.recipes = this.recipes.concat(this._getDevRecipes()); |
342 | 341 | ||
343 | console.log('StubServerApi::getInstalledRecipes resolves', this.recipes); | 342 | debug('StubServerApi::getInstalledRecipes resolves', this.recipes); |
344 | return this.recipes; | 343 | return this.recipes; |
345 | } | 344 | } |
346 | 345 | ||
@@ -353,7 +352,7 @@ export default class ServerApi { | |||
353 | throw new Error(request.statusText); | 352 | throw new Error(request.statusText); |
354 | } | 353 | } |
355 | const recipes = await request.json(); | 354 | const recipes = await request.json(); |
356 | console.log('ServerApi::getRecipeUpdates resolves', recipes); | 355 | debug('ServerApi::getRecipeUpdates resolves', recipes); |
357 | return recipes; | 356 | return recipes; |
358 | } | 357 | } |
359 | 358 | ||
@@ -363,7 +362,7 @@ export default class ServerApi { | |||
363 | if (!request.ok) throw new Error(request.statusText); | 362 | if (!request.ok) throw new Error(request.statusText); |
364 | const data = await request.json(); | 363 | const data = await request.json(); |
365 | const recipePreviews = this._mapRecipePreviewModel(data); | 364 | const recipePreviews = this._mapRecipePreviewModel(data); |
366 | console.log('ServerApi::getRecipes resolves', recipePreviews); | 365 | debug('ServerApi::getRecipes resolves', recipePreviews); |
367 | return recipePreviews; | 366 | return recipePreviews; |
368 | } | 367 | } |
369 | 368 | ||
@@ -375,7 +374,7 @@ export default class ServerApi { | |||
375 | // data = this._addLocalRecipesToPreviews(data); | 374 | // data = this._addLocalRecipesToPreviews(data); |
376 | 375 | ||
377 | const recipePreviews = this._mapRecipePreviewModel(data); | 376 | const recipePreviews = this._mapRecipePreviewModel(data); |
378 | console.log('ServerApi::getFeaturedRecipes resolves', recipePreviews); | 377 | debug('ServerApi::getFeaturedRecipes resolves', recipePreviews); |
379 | return recipePreviews; | 378 | return recipePreviews; |
380 | } | 379 | } |
381 | 380 | ||
@@ -386,7 +385,7 @@ export default class ServerApi { | |||
386 | 385 | ||
387 | const data = await request.json(); | 386 | const data = await request.json(); |
388 | const recipePreviews = this._mapRecipePreviewModel(data); | 387 | const recipePreviews = this._mapRecipePreviewModel(data); |
389 | console.log('ServerApi::searchRecipePreviews resolves', recipePreviews); | 388 | debug('ServerApi::searchRecipePreviews resolves', recipePreviews); |
390 | return recipePreviews; | 389 | return recipePreviews; |
391 | } | 390 | } |
392 | 391 | ||
@@ -402,21 +401,21 @@ export default class ServerApi { | |||
402 | let archivePath: PathOrFileDescriptor; | 401 | let archivePath: PathOrFileDescriptor; |
403 | 402 | ||
404 | if (pathExistsSync(internalRecipeFile)) { | 403 | if (pathExistsSync(internalRecipeFile)) { |
405 | console.log('[ServerApi::getRecipePackage] Using internal recipe file'); | 404 | debug('[ServerApi::getRecipePackage] Using internal recipe file'); |
406 | archivePath = internalRecipeFile; | 405 | archivePath = internalRecipeFile; |
407 | } else { | 406 | } else { |
408 | console.log('[ServerApi::getRecipePackage] Downloading recipe from server'); | 407 | debug('[ServerApi::getRecipePackage] Downloading recipe from server'); |
409 | archivePath = tempArchivePath; | 408 | archivePath = tempArchivePath; |
410 | 409 | ||
411 | const packageUrl = `${apiBase()}/recipes/download/${recipeId}`; | 410 | const packageUrl = `${apiBase()}/recipes/download/${recipeId}`; |
412 | 411 | ||
413 | const res = await window.fetch(packageUrl); | 412 | const res = await window.fetch(packageUrl); |
414 | console.log('Recipe downloaded', recipeId); | 413 | debug('Recipe downloaded', recipeId); |
415 | const blob = await res.blob(); | 414 | const blob = await res.blob(); |
416 | const buffer = await blob.arrayBuffer(); | 415 | const buffer = await blob.arrayBuffer(); |
417 | writeFileSync(tempArchivePath, Buffer.from(buffer)); | 416 | writeFileSync(tempArchivePath, Buffer.from(buffer)); |
418 | } | 417 | } |
419 | console.log(archivePath); | 418 | debug(archivePath); |
420 | 419 | ||
421 | await sleep(10); | 420 | await sleep(10); |
422 | 421 | ||
@@ -427,7 +426,7 @@ export default class ServerApi { | |||
427 | preservePaths: true, | 426 | preservePaths: true, |
428 | unlink: true, | 427 | unlink: true, |
429 | preserveOwner: false, | 428 | preserveOwner: false, |
430 | onwarn: x => console.log('warn', recipeId, x), | 429 | onwarn: x => debug('warn', recipeId, x), |
431 | }); | 430 | }); |
432 | 431 | ||
433 | await sleep(10); | 432 | await sleep(10); |
@@ -457,7 +456,7 @@ export default class ServerApi { | |||
457 | if (!request.ok) { | 456 | if (!request.ok) { |
458 | throw new Error(request.statusText); | 457 | throw new Error(request.statusText); |
459 | } | 458 | } |
460 | console.log('ServerApi::healthCheck resolves'); | 459 | debug('ServerApi::healthCheck resolves'); |
461 | } | 460 | } |
462 | 461 | ||
463 | async getLegacyServices() { | 462 | async getLegacyServices() { |
@@ -484,7 +483,7 @@ export default class ServerApi { | |||
484 | }), | 483 | }), |
485 | ); | 484 | ); |
486 | 485 | ||
487 | console.log('ServerApi::getLegacyServices resolves', services); | 486 | debug('ServerApi::getLegacyServices resolves', services); |
488 | return services; | 487 | return services; |
489 | } | 488 | } |
490 | } catch { | 489 | } catch { |
@@ -517,7 +516,7 @@ export default class ServerApi { | |||
517 | 516 | ||
518 | return new ServiceModel(service, recipe); | 517 | return new ServiceModel(service, recipe); |
519 | } catch (error) { | 518 | } catch (error) { |
520 | console.log(error); | 519 | debug(error); |
521 | return null; | 520 | return null; |
522 | } | 521 | } |
523 | } | 522 | } |
@@ -540,7 +539,7 @@ export default class ServerApi { | |||
540 | 539 | ||
541 | await this.getRecipePackage(recipeId); | 540 | await this.getRecipePackage(recipeId); |
542 | 541 | ||
543 | console.log('Rerun ServerAPI::getInstalledRecipes'); | 542 | debug('Rerun ServerAPI::getInstalledRecipes'); |
544 | await this.getInstalledRecipes(); | 543 | await this.getInstalledRecipes(); |
545 | 544 | ||
546 | recipe = this.recipes.find(r => r.id === recipeId); | 545 | recipe = this.recipes.find(r => r.id === recipeId); |
@@ -605,7 +604,7 @@ export default class ServerApi { | |||
605 | 604 | ||
606 | return recipes; | 605 | return recipes; |
607 | } catch { | 606 | } catch { |
608 | console.log('Could not load dev recipes'); | 607 | debug('Could not load dev recipes'); |
609 | return false; | 608 | return false; |
610 | } | 609 | } |
611 | } | 610 | } |