forked from lucksus/perspectivism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IPFS.js
43 lines (35 loc) · 1.18 KB
/
IPFS.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const IPFS = require('ipfs')
const { app, BrowserWindow, ipcMain } = require('electron')
const _appendBuffer = function(buffer1, buffer2) {
var tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
tmp.set(new Uint8Array(buffer1), 0);
tmp.set(new Uint8Array(buffer2), buffer1.byteLength);
return tmp.buffer;
};
const uint8ArrayConcat = function(chunks) {
return chunks.reduce(_appendBuffer)
}
export async function init () {
const node = await IPFS.create({
EXPERIMENTAL: {
pubsub: true
}
})
const version = await node.version()
console.log('Version:', version.version)
ipcMain.handle('ipfs-add', async (event, data) => {
const fileAdded = await node.add(data)
console.debug('IPFS: Added file:', fileAdded.path, fileAdded.cid)
return fileAdded
})
ipcMain.handle('ipfs-cat', async (event, cid) => {
const chunks = []
for await (const chunk of node.cat(cid)) {
chunks.push(chunk)
}
const fileString = uint8ArrayConcat(chunks).toString();
console.debug('IPFS: Read file contents:', fileString)
return fileString
})
return node
}