Skip to content

Commit 29a28f4

Browse files
authored
feat: load ipfs asynchronously (#1005)
1 parent 7692b2e commit 29a28f4

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

.eslintrc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = {
1111
'prettier', // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier
1212
],
1313
"rules": {
14-
"no-shadow": ["error", { "allow": ["state"] }],
14+
"no-shadow": 0,
1515
"vue/attribute-hyphenation": 0,
1616
"vue/no-v-html": 0,
1717
"vue/html-self-closing": 0,

src/backend/utilities/ipfs.ts

+39-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { create, Options, CID } from 'ipfs-core'
1+
import type { Options, IPFS, CID } from 'ipfs-core'
22
import { bootstrapNodes } from './config'
33

44
export interface IPFSInterface {
@@ -24,13 +24,23 @@ const ipfsConfig: Options = {
2424
},
2525
}
2626

27+
async function loadAndInitIPFS() {
28+
const { create, CID: CIDObj } = await import(`ipfs-core`)
29+
const ipfs = await create(ipfsConfig)
30+
31+
return { ipfs, CIDObj }
32+
}
33+
2734
/**
2835
* An IPFS interface is created, but the node doesn't start right away. Generally we observed that the node starting procedure is slow and blocks the loading of the whole app.
2936
* Hence, we made the loading of the node async and we basically keep a cache of all the requests to IPFS while the node is initialising to dispatch them after the fact.
3037
*/
31-
async function createIPFSInterface(): Promise<IPFSInterface> {
38+
function createIPFSInterface(): IPFSInterface {
3239
let ipfsInitialised = false
33-
const node = await create(ipfsConfig)
40+
let node: IPFS | null = null
41+
let CIDClass: typeof CID | null = null
42+
43+
const promise = loadAndInitIPFS()
3444

3545
const promiseCache: Array<{
3646
func: (...args: any[]) => Promise<any>
@@ -71,6 +81,9 @@ async function createIPFSInterface(): Promise<IPFSInterface> {
7181
}
7282

7383
async function ensureConnectedToBootstrapNodes() {
84+
if (!node) {
85+
throw new Error(`Not initialised!`)
86+
}
7487
// get a list of all addresses for all of the peers we're currently connected to
7588
const peerAddrs = new Set<string>()
7689
try {
@@ -95,6 +108,9 @@ async function createIPFSInterface(): Promise<IPFSInterface> {
95108
}
96109

97110
const getData = async (cid: string) => {
111+
if (!node) {
112+
throw new Error(`Not initialised!`)
113+
}
98114
const content: Buffer[] = []
99115
for await (const chunk of node.cat(cid)) {
100116
content.push(Buffer.from(chunk))
@@ -103,29 +119,45 @@ async function createIPFSInterface(): Promise<IPFSInterface> {
103119
}
104120

105121
const getJSONData = async <T>(cid: string) => {
106-
const res = await node.dag.get(CID.parse(cid))
122+
if (!node || !CIDClass) {
123+
throw new Error(`Not initialised!`)
124+
}
125+
const res = await node.dag.get(CIDClass.parse(cid))
107126
if (!res.value) {
108127
throw new Error(`No data found!`)
109128
}
110129
return res.value as T
111130
}
112131

113132
const sendData = async (content: string | ArrayBuffer) => {
133+
if (!node) {
134+
throw new Error(`Not initialised!`)
135+
}
114136
const { cid } = await node.add(content)
115137
return cid.toString()
116138
}
117139

118140
const sendJSONData = async <T>(content: T) => {
141+
if (!node) {
142+
throw new Error(`Not initialised!`)
143+
}
119144
const cid = await node.dag.put(content)
120145
return cid.toString()
121146
}
122147

123148
const getNodes = async () => {
149+
if (!node) {
150+
throw new Error(`Not initialised!`)
151+
}
124152
const peers = await node.swarm.peers()
125153
return peers.length
126154
}
127155

128-
const initResult = node.start().then(() => {
156+
const initResult = promise.then(async ({ ipfs, CIDObj }) => {
157+
node = ipfs
158+
CIDClass = CIDObj
159+
160+
await node.start()
129161
ipfsInitialised = true
130162
_maintainConnection()
131163
_resolveCachedPromises()
@@ -143,12 +175,12 @@ async function createIPFSInterface(): Promise<IPFSInterface> {
143175

144176
let _ipfs: IPFSInterface | null = null
145177

146-
export async function initIPFS() {
178+
export function initIPFS() {
147179
if (_ipfs) {
148180
return _ipfs
149181
}
150182

151-
_ipfs = await createIPFSInterface()
183+
_ipfs = createIPFSInterface()
152184
return _ipfs
153185
}
154186

0 commit comments

Comments
 (0)