feat(electron): add http routers to electron

This commit is contained in:
Esteban Abaroa
2024-11-19 18:58:24 +00:00
parent 420bc36084
commit 521d26bbe3
6 changed files with 81 additions and 104 deletions
+66 -66
View File
@@ -1,15 +1,15 @@
// download the ipfs binaries before building the electron clients // download the ipfs binaries before building the electron clients
import fs from 'fs-extra' import fs from 'fs-extra';
import ProgressBar from 'progress' import ProgressBar from 'progress';
import https from 'https' import https from 'https';
import decompress from 'decompress' import decompress from 'decompress';
import path from 'path' import path from 'path';
import {fileURLToPath} from 'url' import { fileURLToPath } from 'url';
const ipfsClientsPath = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'bin') const ipfsClientsPath = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'bin');
const ipfsClientWindowsPath = path.join(ipfsClientsPath, 'win') const ipfsClientWindowsPath = path.join(ipfsClientsPath, 'win');
const ipfsClientMacPath = path.join(ipfsClientsPath, 'mac') const ipfsClientMacPath = path.join(ipfsClientsPath, 'mac');
const ipfsClientLinuxPath = path.join(ipfsClientsPath, 'linux') const ipfsClientLinuxPath = path.join(ipfsClientsPath, 'linux');
// plebbit kubu download links https://github.com/plebbit/kubo/releases // plebbit kubu download links https://github.com/plebbit/kubo/releases
// const ipfsClientVersion = '0.20.0' // const ipfsClientVersion = '0.20.0'
@@ -18,93 +18,93 @@ const ipfsClientLinuxPath = path.join(ipfsClientsPath, 'linux')
// const ipfsClientLinuxUrl = `https://github.com/plebbit/kubo/releases/download/v${ipfsClientVersion}/ipfs-linux-amd64` // const ipfsClientLinuxUrl = `https://github.com/plebbit/kubo/releases/download/v${ipfsClientVersion}/ipfs-linux-amd64`
// official kubo download links https://docs.ipfs.tech/install/command-line/#install-official-binary-distributions // official kubo download links https://docs.ipfs.tech/install/command-line/#install-official-binary-distributions
const ipfsClientVersion = '0.28.0' const ipfsClientVersion = '0.32.1';
const ipfsClientWindowsUrl = `https://dist.ipfs.io/kubo/v${ipfsClientVersion}/kubo_v${ipfsClientVersion}_windows-amd64.zip` const ipfsClientWindowsUrl = `https://dist.ipfs.io/kubo/v${ipfsClientVersion}/kubo_v${ipfsClientVersion}_windows-amd64.zip`;
const ipfsClientMacUrl = `https://dist.ipfs.io/kubo/v${ipfsClientVersion}/kubo_v${ipfsClientVersion}_darwin-amd64.tar.gz` const ipfsClientMacUrl = `https://dist.ipfs.io/kubo/v${ipfsClientVersion}/kubo_v${ipfsClientVersion}_darwin-amd64.tar.gz`;
const ipfsClientLinuxUrl = `https://dist.ipfs.io/kubo/v${ipfsClientVersion}/kubo_v${ipfsClientVersion}_linux-amd64.tar.gz` const ipfsClientLinuxUrl = `https://dist.ipfs.io/kubo/v${ipfsClientVersion}/kubo_v${ipfsClientVersion}_linux-amd64.tar.gz`;
const downloadWithProgress = (url) => const downloadWithProgress = (url) =>
new Promise((resolve) => { new Promise((resolve) => {
const split = url.split('/') const split = url.split('/');
const fileName = split[split.length - 1] const fileName = split[split.length - 1];
const chunks = [] const chunks = [];
const req = https.request(url) const req = https.request(url);
req.on('response', (res) => { req.on('response', (res) => {
// handle redirects // handle redirects
if (res.statusCode == 301 || res.statusCode === 302) { if (res.statusCode == 301 || res.statusCode === 302) {
resolve(downloadWithProgress(res.headers.location)) resolve(downloadWithProgress(res.headers.location));
return return;
} }
const len = parseInt(res.headers['content-length'], 10) const len = parseInt(res.headers['content-length'], 10);
console.log() console.log();
const bar = new ProgressBar(` ${fileName} [:bar] :rate/bps :percent :etas`, { const bar = new ProgressBar(` ${fileName} [:bar] :rate/bps :percent :etas`, {
complete: '=', complete: '=',
incomplete: ' ', incomplete: ' ',
width: 20, width: 20,
total: len, total: len,
}) });
res.on('data', (chunk) => { res.on('data', (chunk) => {
chunks.push(chunk) chunks.push(chunk);
bar.tick(chunk.length) bar.tick(chunk.length);
}) });
res.on('end', () => { res.on('end', () => {
console.log('\n') console.log('\n');
resolve(Buffer.concat(chunks)) resolve(Buffer.concat(chunks));
}) });
}) });
req.end() req.end();
}) });
// plebbit kubo downloads dont need to be extracted // plebbit kubo downloads dont need to be extracted
const download = async (url, destinationPath) => { const download = async (url, destinationPath) => {
let binName = 'ipfs' let binName = 'ipfs';
if (destinationPath.endsWith('win')) { if (destinationPath.endsWith('win')) {
binName += '.exe' binName += '.exe';
} }
const binPath = path.join(destinationPath, binName) const binPath = path.join(destinationPath, binName);
// already downloaded, don't download again // already downloaded, don't download again
if (fs.pathExistsSync(binPath)) { if (fs.pathExistsSync(binPath)) {
return return;
} }
const split = url.split('/') const split = url.split('/');
const fileName = split[split.length - 1] const fileName = split[split.length - 1];
const dowloadPath = path.join(destinationPath, fileName) const dowloadPath = path.join(destinationPath, fileName);
const file = await downloadWithProgress(url) const file = await downloadWithProgress(url);
fs.ensureDirSync(destinationPath) fs.ensureDirSync(destinationPath);
await fs.writeFile(binPath, file) await fs.writeFile(binPath, file);
} };
// official kubo downloads need to be extracted // official kubo downloads need to be extracted
const downloadAndExtract = async (url, destinationPath) => { const downloadAndExtract = async (url, destinationPath) => {
let binName = 'ipfs' let binName = 'ipfs';
if (destinationPath.endsWith('win')) { if (destinationPath.endsWith('win')) {
binName += '.exe' binName += '.exe';
} }
const binPath = path.join(destinationPath, binName) const binPath = path.join(destinationPath, binName);
if (fs.pathExistsSync(binPath)) { if (fs.pathExistsSync(binPath)) {
return return;
} }
const split = url.split('/') const split = url.split('/');
const fileName = split[split.length - 1] const fileName = split[split.length - 1];
const dowloadPath = path.join(destinationPath, fileName) const dowloadPath = path.join(destinationPath, fileName);
const file = await downloadWithProgress(url) const file = await downloadWithProgress(url);
fs.ensureDirSync(destinationPath) fs.ensureDirSync(destinationPath);
await fs.writeFile(dowloadPath, file) await fs.writeFile(dowloadPath, file);
await decompress(dowloadPath, destinationPath) await decompress(dowloadPath, destinationPath);
const extractedPath = path.join(destinationPath, 'kubo') const extractedPath = path.join(destinationPath, 'kubo');
const extractedBinPath = path.join(extractedPath, binName) const extractedBinPath = path.join(extractedPath, binName);
fs.moveSync(extractedBinPath, binPath) fs.moveSync(extractedBinPath, binPath);
fs.removeSync(extractedPath) fs.removeSync(extractedPath);
fs.removeSync(dowloadPath) fs.removeSync(dowloadPath);
} };
export const downloadIpfsClients = async () => { export const downloadIpfsClients = async () => {
await downloadAndExtract(ipfsClientWindowsUrl, ipfsClientWindowsPath) await downloadAndExtract(ipfsClientWindowsUrl, ipfsClientWindowsPath);
await downloadAndExtract(ipfsClientMacUrl, ipfsClientMacPath) await downloadAndExtract(ipfsClientMacUrl, ipfsClientMacPath);
await downloadAndExtract(ipfsClientLinuxUrl, ipfsClientLinuxPath) await downloadAndExtract(ipfsClientLinuxUrl, ipfsClientLinuxPath);
} };
export default async (context) => { export default async (context) => {
await downloadIpfsClients() await downloadIpfsClients();
} };
+3 -1
View File
@@ -20,8 +20,10 @@ proxy.on('proxyReq', function (proxyReq, req, res, options) {
proxyReq.removeHeader('sec-fetch-dest'); proxyReq.removeHeader('sec-fetch-dest');
proxyReq.removeHeader('referer'); proxyReq.removeHeader('referer');
}); });
proxy.on('error', (e) => { proxy.on('error', (e, req, res) => {
console.error(e); console.error(e);
// if not ended, will hang forever
res.end();
}); });
// start server // start server
+1 -1
View File
@@ -116,7 +116,7 @@ const start = async () => {
} }
pendingStart = true; pendingStart = true;
try { try {
const started = await tcpPortUsed.check(5001, '127.0.0.1'); const started = await tcpPortUsed.check(isDev ? 5002 : 5001, '127.0.0.1');
if (started) { if (started) {
return; return;
} }
+2 -2
View File
@@ -15,8 +15,7 @@ const defaultPlebbitOptions = {
// find the user's OS data path // find the user's OS data path
dataPath: !isDev ? envPaths.data : path.join(dirname, '..', '.plebbit'), dataPath: !isDev ? envPaths.data : path.join(dirname, '..', '.plebbit'),
ipfsHttpClientsOptions: ['http://localhost:5001/api/v0'], ipfsHttpClientsOptions: ['http://localhost:5001/api/v0'],
// TODO: having to define pubsubHttpClientsOptions and ipfsHttpClientsOptions is a bug with plebbit-js httpRoutersOptions: ['https://routing.lol', 'https://peers.pleb.bot'],
pubsubHttpClientsOptions: ['http://localhost:5001/api/v0'],
}; };
// generate plebbit rpc auth key if doesn't exist // generate plebbit rpc auth key if doesn't exist
@@ -42,6 +41,7 @@ const start = async () => {
return; return;
} }
const plebbitWebSocketServer = await PlebbitRpc.PlebbitWsServer({ port, plebbitOptions: defaultPlebbitOptions, authKey: plebbitRpcAuthKey }); const plebbitWebSocketServer = await PlebbitRpc.PlebbitWsServer({ port, plebbitOptions: defaultPlebbitOptions, authKey: plebbitRpcAuthKey });
plebbitWebSocketServer.on('error', (e) => console.log('plebbit rpc error', e));
console.log(`plebbit rpc: listening on ws://localhost:${port} (local connections only)`); console.log(`plebbit rpc: listening on ws://localhost:${port} (local connections only)`);
console.log(`plebbit rpc: listening on ws://localhost:${port}/${plebbitRpcAuthKey} (secret auth key for remote connections)`); console.log(`plebbit rpc: listening on ws://localhost:${port}/${plebbitRpcAuthKey} (secret auth key for remote connections)`);
+1 -1
View File
@@ -8,7 +8,7 @@
"dependencies": { "dependencies": {
"@capacitor/app": "6.0.1", "@capacitor/app": "6.0.1",
"@floating-ui/react": "0.26.1", "@floating-ui/react": "0.26.1",
"@plebbit/plebbit-react-hooks": "https://github.com/plebbit/plebbit-react-hooks.git#0d4219ad95c3322eac78db23091eb57239be8776", "@plebbit/plebbit-react-hooks": "https://github.com/plebbit/plebbit-react-hooks.git#be1a1f317db7c4096f0d1c79452e13452bf9df78",
"@testing-library/jest-dom": "5.14.1", "@testing-library/jest-dom": "5.14.1",
"@testing-library/react": "13.0.0", "@testing-library/react": "13.0.0",
"@testing-library/user-event": "13.2.1", "@testing-library/user-event": "13.2.1",
+8 -33
View File
@@ -3262,9 +3262,9 @@
resolved "https://registry.yarnpkg.com/@pkgjs/parseargs/-/parseargs-0.11.0.tgz#a77ea742fab25775145434eb1d2328cf5013ac33" resolved "https://registry.yarnpkg.com/@pkgjs/parseargs/-/parseargs-0.11.0.tgz#a77ea742fab25775145434eb1d2328cf5013ac33"
integrity sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg== integrity sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg==
"@plebbit/plebbit-js@https://github.com/plebbit/plebbit-js.git#20b3c91b30a589ea5728f9a20f61e63700fe0dd4": "@plebbit/plebbit-js@https://github.com/plebbit/plebbit-js.git#cae33a888fc23248d2f226d9d14b67c80dee7f28":
version "0.0.4" version "0.0.4"
resolved "https://github.com/plebbit/plebbit-js.git#20b3c91b30a589ea5728f9a20f61e63700fe0dd4" resolved "https://github.com/plebbit/plebbit-js.git#cae33a888fc23248d2f226d9d14b67c80dee7f28"
dependencies: dependencies:
"@bonfida/spl-name-service" "2.4.2" "@bonfida/spl-name-service" "2.4.2"
"@chainsafe/libp2p-gossipsub" "12.0.0" "@chainsafe/libp2p-gossipsub" "12.0.0"
@@ -3338,11 +3338,11 @@
dependencies: dependencies:
debug "4.3.4" debug "4.3.4"
"@plebbit/plebbit-react-hooks@https://github.com/plebbit/plebbit-react-hooks.git#0d4219ad95c3322eac78db23091eb57239be8776": "@plebbit/plebbit-react-hooks@https://github.com/plebbit/plebbit-react-hooks.git#be1a1f317db7c4096f0d1c79452e13452bf9df78":
version "0.0.1" version "0.0.1"
resolved "https://github.com/plebbit/plebbit-react-hooks.git#0d4219ad95c3322eac78db23091eb57239be8776" resolved "https://github.com/plebbit/plebbit-react-hooks.git#be1a1f317db7c4096f0d1c79452e13452bf9df78"
dependencies: dependencies:
"@plebbit/plebbit-js" "https://github.com/plebbit/plebbit-js.git#20b3c91b30a589ea5728f9a20f61e63700fe0dd4" "@plebbit/plebbit-js" "https://github.com/plebbit/plebbit-js.git#cae33a888fc23248d2f226d9d14b67c80dee7f28"
"@plebbit/plebbit-logger" "https://github.com/plebbit/plebbit-logger.git" "@plebbit/plebbit-logger" "https://github.com/plebbit/plebbit-logger.git"
assert "2.0.0" assert "2.0.0"
ethers "5.6.9" ethers "5.6.9"
@@ -15670,16 +15670,7 @@ string-split-by@^1.0.0:
dependencies: dependencies:
parenthesis "^3.1.5" parenthesis "^3.1.5"
"string-width-cjs@npm:string-width@^4.2.0": "string-width-cjs@npm:string-width@^4.2.0", "string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
dependencies:
emoji-regex "^8.0.0"
is-fullwidth-code-point "^3.0.0"
strip-ansi "^6.0.1"
"string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
version "4.2.3" version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
@@ -15783,14 +15774,7 @@ stringify-object@^3.3.0:
is-obj "^1.0.1" is-obj "^1.0.1"
is-regexp "^1.0.0" is-regexp "^1.0.0"
"strip-ansi-cjs@npm:strip-ansi@^6.0.1": "strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
dependencies:
ansi-regex "^5.0.1"
strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1" version "6.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9" resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A== integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
@@ -17471,7 +17455,7 @@ workbox-window@6.6.1:
"@types/trusted-types" "^2.0.2" "@types/trusted-types" "^2.0.2"
workbox-core "6.6.1" workbox-core "6.6.1"
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0": "wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
version "7.0.0" version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43" resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q== integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
@@ -17489,15 +17473,6 @@ wrap-ansi@^6.2.0:
string-width "^4.1.0" string-width "^4.1.0"
strip-ansi "^6.0.0" strip-ansi "^6.0.0"
wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
dependencies:
ansi-styles "^4.0.0"
string-width "^4.1.0"
strip-ansi "^6.0.0"
wrap-ansi@^8.1.0: wrap-ansi@^8.1.0:
version "8.1.0" version "8.1.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214" resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"