Skip to content

Commit

Permalink
Merge pull request #608 from plebbit/master
Browse files Browse the repository at this point in the history
feat(electron): add http routers to electron
  • Loading branch information
plebeius-eth authored Nov 21, 2024
2 parents ab85514 + 521d26b commit f1737df
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 104 deletions.
132 changes: 66 additions & 66 deletions electron/before-pack.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// download the ipfs binaries before building the electron clients

import fs from 'fs-extra'
import ProgressBar from 'progress'
import https from 'https'
import decompress from 'decompress'
import path from 'path'
import {fileURLToPath} from 'url'
const ipfsClientsPath = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'bin')
const ipfsClientWindowsPath = path.join(ipfsClientsPath, 'win')
const ipfsClientMacPath = path.join(ipfsClientsPath, 'mac')
const ipfsClientLinuxPath = path.join(ipfsClientsPath, 'linux')
import fs from 'fs-extra';
import ProgressBar from 'progress';
import https from 'https';
import decompress from 'decompress';
import path from 'path';
import { fileURLToPath } from 'url';
const ipfsClientsPath = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'bin');
const ipfsClientWindowsPath = path.join(ipfsClientsPath, 'win');
const ipfsClientMacPath = path.join(ipfsClientsPath, 'mac');
const ipfsClientLinuxPath = path.join(ipfsClientsPath, 'linux');

// plebbit kubu download links https://github.com/plebbit/kubo/releases
// const ipfsClientVersion = '0.20.0'
Expand All @@ -18,93 +18,93 @@ const ipfsClientLinuxPath = path.join(ipfsClientsPath, 'linux')
// 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
const ipfsClientVersion = '0.28.0'
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 ipfsClientLinuxUrl = `https://dist.ipfs.io/kubo/v${ipfsClientVersion}/kubo_v${ipfsClientVersion}_linux-amd64.tar.gz`
const ipfsClientVersion = '0.32.1';
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 ipfsClientLinuxUrl = `https://dist.ipfs.io/kubo/v${ipfsClientVersion}/kubo_v${ipfsClientVersion}_linux-amd64.tar.gz`;

const downloadWithProgress = (url) =>
new Promise((resolve) => {
const split = url.split('/')
const fileName = split[split.length - 1]
const chunks = []
const req = https.request(url)
const split = url.split('/');
const fileName = split[split.length - 1];
const chunks = [];
const req = https.request(url);
req.on('response', (res) => {
// handle redirects
if (res.statusCode == 301 || res.statusCode === 302) {
resolve(downloadWithProgress(res.headers.location))
return
resolve(downloadWithProgress(res.headers.location));
return;
}

const len = parseInt(res.headers['content-length'], 10)
console.log()
const len = parseInt(res.headers['content-length'], 10);
console.log();
const bar = new ProgressBar(` ${fileName} [:bar] :rate/bps :percent :etas`, {
complete: '=',
incomplete: ' ',
width: 20,
total: len,
})
});
res.on('data', (chunk) => {
chunks.push(chunk)
bar.tick(chunk.length)
})
chunks.push(chunk);
bar.tick(chunk.length);
});
res.on('end', () => {
console.log('\n')
resolve(Buffer.concat(chunks))
})
})
req.end()
})
console.log('\n');
resolve(Buffer.concat(chunks));
});
});
req.end();
});

// plebbit kubo downloads dont need to be extracted
const download = async (url, destinationPath) => {
let binName = 'ipfs'
let binName = 'ipfs';
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
if (fs.pathExistsSync(binPath)) {
return
return;
}
const split = url.split('/')
const fileName = split[split.length - 1]
const dowloadPath = path.join(destinationPath, fileName)
const file = await downloadWithProgress(url)
fs.ensureDirSync(destinationPath)
await fs.writeFile(binPath, file)
}
const split = url.split('/');
const fileName = split[split.length - 1];
const dowloadPath = path.join(destinationPath, fileName);
const file = await downloadWithProgress(url);
fs.ensureDirSync(destinationPath);
await fs.writeFile(binPath, file);
};

// official kubo downloads need to be extracted
const downloadAndExtract = async (url, destinationPath) => {
let binName = 'ipfs'
let binName = 'ipfs';
if (destinationPath.endsWith('win')) {
binName += '.exe'
binName += '.exe';
}
const binPath = path.join(destinationPath, binName)
const binPath = path.join(destinationPath, binName);
if (fs.pathExistsSync(binPath)) {
return
return;
}
const split = url.split('/')
const fileName = split[split.length - 1]
const dowloadPath = path.join(destinationPath, fileName)
const file = await downloadWithProgress(url)
fs.ensureDirSync(destinationPath)
await fs.writeFile(dowloadPath, file)
await decompress(dowloadPath, destinationPath)
const extractedPath = path.join(destinationPath, 'kubo')
const extractedBinPath = path.join(extractedPath, binName)
fs.moveSync(extractedBinPath, binPath)
fs.removeSync(extractedPath)
fs.removeSync(dowloadPath)
}
const split = url.split('/');
const fileName = split[split.length - 1];
const dowloadPath = path.join(destinationPath, fileName);
const file = await downloadWithProgress(url);
fs.ensureDirSync(destinationPath);
await fs.writeFile(dowloadPath, file);
await decompress(dowloadPath, destinationPath);
const extractedPath = path.join(destinationPath, 'kubo');
const extractedBinPath = path.join(extractedPath, binName);
fs.moveSync(extractedBinPath, binPath);
fs.removeSync(extractedPath);
fs.removeSync(dowloadPath);
};

export const downloadIpfsClients = async () => {
await downloadAndExtract(ipfsClientWindowsUrl, ipfsClientWindowsPath)
await downloadAndExtract(ipfsClientMacUrl, ipfsClientMacPath)
await downloadAndExtract(ipfsClientLinuxUrl, ipfsClientLinuxPath)
}
await downloadAndExtract(ipfsClientWindowsUrl, ipfsClientWindowsPath);
await downloadAndExtract(ipfsClientMacUrl, ipfsClientMacPath);
await downloadAndExtract(ipfsClientLinuxUrl, ipfsClientLinuxPath);
};

export default async (context) => {
await downloadIpfsClients()
}
await downloadIpfsClients();
};
4 changes: 3 additions & 1 deletion electron/proxy-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ proxy.on('proxyReq', function (proxyReq, req, res, options) {
proxyReq.removeHeader('sec-fetch-dest');
proxyReq.removeHeader('referer');
});
proxy.on('error', (e) => {
proxy.on('error', (e, req, res) => {
console.error(e);
// if not ended, will hang forever
res.end();
});

// start server
Expand Down
2 changes: 1 addition & 1 deletion electron/start-ipfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ const start = async () => {
}
pendingStart = true;
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) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions electron/start-plebbit-rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ const defaultPlebbitOptions = {
// find the user's OS data path
dataPath: !isDev ? envPaths.data : path.join(dirname, '..', '.plebbit'),
ipfsHttpClientsOptions: ['http://localhost:5001/api/v0'],
// TODO: having to define pubsubHttpClientsOptions and ipfsHttpClientsOptions is a bug with plebbit-js
pubsubHttpClientsOptions: ['http://localhost:5001/api/v0'],
httpRoutersOptions: ['https://routing.lol', 'https://peers.pleb.bot'],
};

// generate plebbit rpc auth key if doesn't exist
Expand All @@ -42,6 +41,7 @@ const start = async () => {
return;
}
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}/${plebbitRpcAuthKey} (secret auth key for remote connections)`);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"dependencies": {
"@capacitor/app": "6.0.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/react": "13.0.0",
"@testing-library/user-event": "13.2.1",
Expand Down
41 changes: 8 additions & 33 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3262,9 +3262,9 @@
resolved "https://registry.yarnpkg.com/@pkgjs/parseargs/-/parseargs-0.11.0.tgz#a77ea742fab25775145434eb1d2328cf5013ac33"
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"
resolved "https://github.com/plebbit/plebbit-js.git#20b3c91b30a589ea5728f9a20f61e63700fe0dd4"
resolved "https://github.com/plebbit/plebbit-js.git#cae33a888fc23248d2f226d9d14b67c80dee7f28"
dependencies:
"@bonfida/spl-name-service" "2.4.2"
"@chainsafe/libp2p-gossipsub" "12.0.0"
Expand Down Expand Up @@ -3338,11 +3338,11 @@
dependencies:
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"
resolved "https://github.com/plebbit/plebbit-react-hooks.git#0d4219ad95c3322eac78db23091eb57239be8776"
resolved "https://github.com/plebbit/plebbit-react-hooks.git#be1a1f317db7c4096f0d1c79452e13452bf9df78"
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"
assert "2.0.0"
ethers "5.6.9"
Expand Down Expand Up @@ -15670,16 +15670,7 @@ string-split-by@^1.0.0:
dependencies:
parenthesis "^3.1.5"

"string-width-cjs@npm:string-width@^4.2.0":
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:
"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==
Expand Down Expand Up @@ -15783,14 +15774,7 @@ stringify-object@^3.3.0:
is-obj "^1.0.1"
is-regexp "^1.0.0"

"strip-ansi-cjs@npm: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:
"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==
Expand Down Expand Up @@ -17471,7 +17455,7 @@ [email protected]:
"@types/trusted-types" "^2.0.2"
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"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
Expand All @@ -17489,15 +17473,6 @@ wrap-ansi@^6.2.0:
string-width "^4.1.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:
version "8.1.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"
Expand Down

0 comments on commit f1737df

Please sign in to comment.