Skip to content

Commit

Permalink
feat(electron): add window.plebbitRpcAuthKey
Browse files Browse the repository at this point in the history
  • Loading branch information
estebanabaroa committed Apr 18, 2024
1 parent 97c2d0c commit 328e3a0
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion electron/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import util from 'util';
import fs from 'fs-extra';
import path from 'path';
import EnvPaths from 'env-paths';
const envPaths = EnvPaths.default('plebbit', { suffix: false });
const envPaths = EnvPaths('plebbit', { suffix: false });

// previous version created a file instead of folder
// we should remove this at some point
Expand Down
9 changes: 8 additions & 1 deletion electron/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import './log.js';
import { app, BrowserWindow, Menu, MenuItem, Tray, screen as electronScreen, shell, dialog, nativeTheme } from 'electron';
import { app, BrowserWindow, Menu, MenuItem, Tray, screen as electronScreen, shell, dialog, nativeTheme, ipcMain } from 'electron';
import isDev from 'electron-is-dev';
import fs from 'fs';
import path from 'path';
import EnvPaths from 'env-paths';
import startIpfs from './start-ipfs.js';
import './start-plebbit-rpc.js';
import { URL, fileURLToPath } from 'node:url';
Expand All @@ -19,6 +21,11 @@ startIpfs.onError = (error) => {
}
};

// send plebbit rpc auth key to renderer
const plebbitDataPath = !isDev ? EnvPaths('plebbit', { suffix: false }).data : path.join(dirname, '..', '.plebbit');
const plebbitRpcAuthKey = fs.readFileSync(path.join(plebbitDataPath, 'auth-key'), 'utf8');
ipcMain.on('get-plebbit-rpc-auth-key', (event) => event.reply('plebbit-rpc-auth-key', plebbitRpcAuthKey));

// use common user agent instead of electron so img, video, audio, iframe elements don't get blocked
// https://www.whatismybrowser.com/guides/the-latest-version/chrome
// https://www.whatismybrowser.com/guides/the-latest-user-agent/chrome
Expand Down
6 changes: 5 additions & 1 deletion electron/preload.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { contextBridge } = require('electron');
const { contextBridge, ipcRenderer } = require('electron');

// dev uses http://localhost, prod uses file://...index.html
const isDev = window.location.protocol === 'http:';
Expand All @@ -10,5 +10,9 @@ const defaultPlebbitOptions = {
contextBridge.exposeInMainWorld('defaultPlebbitOptions', defaultPlebbitOptions);
contextBridge.exposeInMainWorld('defaultMediaIpfsGatewayUrl', 'http://localhost:6473');

// receive plebbit rpc auth key from main
ipcRenderer.on('plebbit-rpc-auth-key', (event, plebbitRpcAuthKey) => contextBridge.exposeInMainWorld('plebbitRpcAuthKey', plebbitRpcAuthKey));
ipcRenderer.send('get-plebbit-rpc-auth-key');

// uncomment for logs
// localStorage.debug = 'plebbit-js:*,plebbit-react-hooks:*,seedit:*'
2 changes: 1 addition & 1 deletion electron/start-ipfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import tcpPortUsed from 'tcp-port-used';
import EnvPaths from 'env-paths';
import { fileURLToPath } from 'url';
const dirname = path.join(path.dirname(fileURLToPath(import.meta.url)));
const envPaths = EnvPaths.default('plebbit', { suffix: false });
const envPaths = EnvPaths('plebbit', { suffix: false });

// use this custom function instead of spawnSync for better logging
// also spawnSync might have been causing crash on start on windows
Expand Down
2 changes: 1 addition & 1 deletion electron/start-plebbit-rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import path from 'path';
import { fileURLToPath } from 'url';
import isDev from 'electron-is-dev';
const dirname = path.join(path.dirname(fileURLToPath(import.meta.url)));
const envPaths = EnvPaths.default('plebbit', { suffix: false });
const envPaths = EnvPaths('plebbit', { suffix: false });

// PLEB, always run plebbit rpc on this port so all clients can use it
const port = 9138;
Expand Down

0 comments on commit 328e3a0

Please sign in to comment.