Skip to content

Commit

Permalink
feat: ipfs startup improvements + more
Browse files Browse the repository at this point in the history
  • Loading branch information
vaultec81 committed Jun 4, 2023
1 parent bd2dfcc commit 4d2a632
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/common/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const IPFS_SELF_MULTIADDR = '/ip4/127.0.0.1/tcp/5004'
export const IPFS_HOST = '127.0.0.1'
export const IPFS_HOST = 'http://127.0.0.1:5004'
2 changes: 1 addition & 1 deletion src/components/DHTProviders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { IPFS_HOST } from '../common/constants';

let ipfsClient;
try {
ipfsClient = IPFSHTTPClient.create({ host: IPFS_HOST });
ipfsClient = IPFSHTTPClient.create({ url: IPFS_HOST });
} catch (error) {
console.error(`Error creating IPFS client in watch.tsx: `, error);
throw error;
Expand Down
25 changes: 19 additions & 6 deletions src/main/core/components/ipfsHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import { IPFS_HOST, IPFS_SELF_MULTIADDR } from '../../../common/constants'
const waIpfs = require('wa-go-ipfs')
const toUri = require('multiaddr-to-uri')

const IPFSPORTS = [
'5001',
'5004'
]

const defaultIpfsConfig = {
API: {
HTTPHeaders: {
Expand Down Expand Up @@ -56,6 +61,7 @@ const defaultIpfsConfig = {
}
export class IpfsHandler {
static events: any
static isReady: boolean
static get ready() {
return new Promise(async (resolve, reject) => {
let ipfsInfo = await IpfsHandler.getIpfs()
Expand All @@ -70,6 +76,9 @@ export class IpfsHandler {
})
}
static async start(appPath) {
IpfsHandler.events.once('ready', async () => {
this.isReady = true
})
console.log(`4`)
let ipfsInfo = await IpfsHandler.getIpfs()
if (!ipfsInfo.exists) {
Expand All @@ -83,7 +92,7 @@ export class IpfsHandler {
IpfsHandler.events.emit('ready')
} else {
console.log(`6`)
if (ipfsInfo.ipfs) {
if (ipfsInfo.isRunning) {
IpfsHandler.events.emit('ready')
} else {
fs.writeFileSync(
Expand All @@ -107,6 +116,7 @@ export class IpfsHandler {
const goIpfsPath = await waIpfs.getPath(
waIpfs.getDefaultPath({ dev: process.env.NODE_ENV === 'development' }),
)
console.log('repoPath', {repoPath, goIpfsPath})
await execa(goIpfsPath, ['init'], {
env: {
IPFS_Path: repoPath,
Expand Down Expand Up @@ -156,13 +166,12 @@ export class IpfsHandler {
})
}
static async getIpfs() {
const AppPath = Path.join(os.homedir(), '.blasio-app')

let ipfsPath: string
if (process.env.IPFS_Path) {
ipfsPath = process.env.IPFS_Path
} else {
ipfsPath = Path.join(os.homedir(), '.ipfs-3speak')
ipfsPath = Path.join(os.homedir(), '.ipfs-3speak2')
}

let exists
Expand All @@ -188,20 +197,23 @@ export class IpfsHandler {
}*/
}

let isRunning = false;
if (apiAddr) {
ipfs = IPFSHTTPClient.create({ host: IPFS_HOST })
ipfs = IPFSHTTPClient.create({ url: IPFS_HOST })
try {
await ipfs.config.get('Addresses')
isRunning = true
} catch (ex) {
console.error(`Error getting IPFS address config`)
console.error(ex)
ipfs = null
// ipfs = null
}
} else {
console.error(`***** API addr is null!!******`)
}

if (ipfs !== null && ipfs) {
if (ipfs !== null && isRunning) {

const gma = await ipfs.config.get('Addresses.Gateway')
gateway = toUri(gma) + '/ipfs/'
} else {
Expand All @@ -210,6 +222,7 @@ export class IpfsHandler {

return {
isLocked,
isRunning,
exists,
ipfsPath,
ipfs,
Expand Down
40 changes: 32 additions & 8 deletions src/main/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IPFSHTTPClient } from 'ipfs-http-client'
import winston from 'winston'
import execa from 'execa'
import Components from './components'
import DistillerDB from './components/DistillerDB'
import { EncoderService } from './components/EncoderService'
Expand Down Expand Up @@ -37,14 +38,19 @@ export class CoreService {
async install() {
this.start_progress.message = 'Installing IPFS'
await waIpfs.install({
version: 'v0.19.0',
version: 'v0.19.2',
dev: process.env.NODE_ENV === 'development',
recursive: true,
})
await new Promise<void>((resolve) => {
setTimeout(async () => {
const ipfsInfo = await IpfsHandler.getIpfs()
this.start_progress.message = 'Initializing IPFS'
await IpfsHandler.init(undefined)
try {
await IpfsHandler.init(ipfsInfo.ipfsPath)
} catch {

}
this.start_progress.message = null
resolve()
}, 5000)
Expand Down Expand Up @@ -87,8 +93,19 @@ export class CoreService {
fs.chmodSync(ipfsPath, 755)
}
}
const output = await execa(ipfsPath, ['version', '-n'])
console.log(output)
if(output.stdout !== "0.16.2") {
console.log('Ipfs not up to date')
throw new Error('Ipfs not up to date')
}
} catch {
await this.install()
console.log('installing ipfs update')
try {
await this.install()
} catch(ex) {
console.log(ex)
}
}

this.config = new Components.Config(this._options.path)
Expand Down Expand Up @@ -119,12 +136,19 @@ export class CoreService {
this.events.emit('ready')
this.start_progress.ready = true
this.start_progress.message = null

console.log('starting message LOOP')
setInterval(async () => {
const peerIds = (await this.ipfs.config.get('Bootstrap')) as any
for (const peerId of peerIds) {
try {
await this.ipfs.swarm.connect(peerId)
} catch {}
console.log(IpfsHandler.isReady)
try {
const peerIds = (await this.ipfs.config.get('Bootstrap')) as any
for (const peerId of peerIds) {
try {
await this.ipfs.swarm.connect(peerId)
} catch {}
}
} catch {

}
}, 60000)
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ async function startup(): Promise<void> {
void startup()

process.on('unhandledRejection', (err: Error) => {
console.log(err)
core.logger.error(`Unhandled rejection!`)
core.logger.error(err.message)
core.logger.error(err.stack)
Expand All @@ -109,6 +110,7 @@ process.on('unhandledRejection', (err: Error) => {
})

process.on('uncaughtException', (err: Error) => {
console.log('err', err)
core.logger.error(`Uncaught exception!`)
core.logger.error(err.message)
core.logger.error(err.stack)
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/views/UploaderView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function UploaderView() {
const [blockedGlobalMessage, setBlockedGlobalMessage] = useState('')

useEffect(() => {
ipfs.current = IPFSHTTPClient.create({ host: IPFS_HOST })
ipfs.current = IPFSHTTPClient.create({ url: IPFS_HOST })
}, [])

const handlePublish = async () => {
Expand Down
8 changes: 2 additions & 6 deletions src/renderer/views/WatchView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,8 @@ import { PinLocally } from './WatchView/watchViewHelpers/PinLocally';
import { showDebug } from './WatchView/watchViewHelpers/showDebug';
const Finder = ArraySearch.Finder

let ipfsClient
try {
ipfsClient = IPFSHTTPClient.create({ host: IPFS_HOST })
} catch (error) {
throw error
}
let ipfsClient = IPFSHTTPClient.create({ url: IPFS_HOST })


export function WatchView(props: any) {
const player = useRef<any>()
Expand Down

0 comments on commit 4d2a632

Please sign in to comment.