1
- import { create , Options , CID } from 'ipfs-core'
1
+ import type { Options , IPFS , CID } from 'ipfs-core'
2
2
import { bootstrapNodes } from './config'
3
3
4
4
export interface IPFSInterface {
@@ -24,13 +24,23 @@ const ipfsConfig: Options = {
24
24
} ,
25
25
}
26
26
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
+
27
34
/**
28
35
* 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.
29
36
* 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.
30
37
*/
31
- async function createIPFSInterface ( ) : Promise < IPFSInterface > {
38
+ function createIPFSInterface ( ) : IPFSInterface {
32
39
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 ( )
34
44
35
45
const promiseCache : Array < {
36
46
func : ( ...args : any [ ] ) => Promise < any >
@@ -71,6 +81,9 @@ async function createIPFSInterface(): Promise<IPFSInterface> {
71
81
}
72
82
73
83
async function ensureConnectedToBootstrapNodes ( ) {
84
+ if ( ! node ) {
85
+ throw new Error ( `Not initialised!` )
86
+ }
74
87
// get a list of all addresses for all of the peers we're currently connected to
75
88
const peerAddrs = new Set < string > ( )
76
89
try {
@@ -95,6 +108,9 @@ async function createIPFSInterface(): Promise<IPFSInterface> {
95
108
}
96
109
97
110
const getData = async ( cid : string ) => {
111
+ if ( ! node ) {
112
+ throw new Error ( `Not initialised!` )
113
+ }
98
114
const content : Buffer [ ] = [ ]
99
115
for await ( const chunk of node . cat ( cid ) ) {
100
116
content . push ( Buffer . from ( chunk ) )
@@ -103,29 +119,45 @@ async function createIPFSInterface(): Promise<IPFSInterface> {
103
119
}
104
120
105
121
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 ) )
107
126
if ( ! res . value ) {
108
127
throw new Error ( `No data found!` )
109
128
}
110
129
return res . value as T
111
130
}
112
131
113
132
const sendData = async ( content : string | ArrayBuffer ) => {
133
+ if ( ! node ) {
134
+ throw new Error ( `Not initialised!` )
135
+ }
114
136
const { cid } = await node . add ( content )
115
137
return cid . toString ( )
116
138
}
117
139
118
140
const sendJSONData = async < T > ( content : T ) => {
141
+ if ( ! node ) {
142
+ throw new Error ( `Not initialised!` )
143
+ }
119
144
const cid = await node . dag . put ( content )
120
145
return cid . toString ( )
121
146
}
122
147
123
148
const getNodes = async ( ) => {
149
+ if ( ! node ) {
150
+ throw new Error ( `Not initialised!` )
151
+ }
124
152
const peers = await node . swarm . peers ( )
125
153
return peers . length
126
154
}
127
155
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 ( )
129
161
ipfsInitialised = true
130
162
_maintainConnection ( )
131
163
_resolveCachedPromises ( )
@@ -143,12 +175,12 @@ async function createIPFSInterface(): Promise<IPFSInterface> {
143
175
144
176
let _ipfs : IPFSInterface | null = null
145
177
146
- export async function initIPFS ( ) {
178
+ export function initIPFS ( ) {
147
179
if ( _ipfs ) {
148
180
return _ipfs
149
181
}
150
182
151
- _ipfs = await createIPFSInterface ( )
183
+ _ipfs = createIPFSInterface ( )
152
184
return _ipfs
153
185
}
154
186
0 commit comments