| 1 | const {clipboard, shell, contextBridge} = require('electron'); |
| 2 | const fs = require('fs'); |
| 3 | const internalIP = require('internal-ip'); |
| 4 | |
| 5 | // Expose protected methods so that render process does not need unsafe node integration |
| 6 | contextBridge.exposeInMainWorld('api', { |
| 7 | electron: {clipboard, shell}, |
| 8 | ip: {address: internalIP.v4.sync}, |
| 9 | getDevTools() { |
| 10 | let devtools; |
| 11 | try { |
| 12 | devtools = require('react-devtools-core/standalone').default; |
| 13 | } catch (err) { |
| 14 | alert( |
| 15 | err.toString() + |
| 16 | '\n\nDid you run `yarn` and `yarn run build` in packages/react-devtools-core?', |
| 17 | ); |
| 18 | } |
| 19 | return devtools; |
| 20 | }, |
| 21 | readEnv() { |
| 22 | let options; |
| 23 | let useHttps = false; |
| 24 | try { |
| 25 | if (process.env.KEY && process.env.CERT) { |
| 26 | options = { |
| 27 | key: fs.readFileSync(process.env.KEY), |
| 28 | cert: fs.readFileSync(process.env.CERT), |
| 29 | }; |
| 30 | useHttps = true; |
| 31 | } |
| 32 | } catch (err) { |
| 33 | console.error('Failed to process SSL options - ', err); |
| 34 | options = undefined; |
| 35 | } |
| 36 | const host = process.env.HOST || 'localhost'; |
| 37 | const protocol = useHttps ? 'https' : 'http'; |
| 38 | const port = +process.env.REACT_DEVTOOLS_PORT || +process.env.PORT || 8097; |
| 39 | const path = process.env.REACT_DEVTOOLS_PATH || undefined; |
| 40 | const clientHost = process.env.REACT_DEVTOOLS_CLIENT_HOST || undefined; |
| 41 | const clientPort = process.env.REACT_DEVTOOLS_CLIENT_PORT |
| 42 | ? +process.env.REACT_DEVTOOLS_CLIENT_PORT |
| 43 | : undefined; |
| 44 | const clientUseHttps = |
| 45 | process.env.REACT_DEVTOOLS_CLIENT_USE_HTTPS === 'true' ? true : undefined; |
| 46 | return { |
| 47 | options, |
| 48 | useHttps, |
| 49 | host, |
| 50 | protocol, |
| 51 | port, |
| 52 | path, |
| 53 | clientHost, |
| 54 | clientPort, |
| 55 | clientUseHttps, |
| 56 | }; |
| 57 | }, |
| 58 | }); |