-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipfs.js
107 lines (95 loc) · 2.52 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const fs = require('fs');
const IPFS = require('ipfs');
const Room = require("ipfs-pubsub-room");
const MAIN_DIR = '/dweather'
const REPONAME = 'repo'
const NL = '\n'
let __ipfs__ = null
let ROOM = null
let ROOM_NAME = 'dweather'
let currentHash = ''
function getIpfs() {
if (!__ipfs__) {
const isRepoExists = fs.existsSync(REPONAME)
console.log('repo exists: ', isRepoExists)
const ipfs = new IPFS({
init: !isRepoExists,
repo: REPONAME,
// relay: { enabled: true, hop: { enabled: true, active: false } },
EXPERIMENTAL: {
pubsub: true
},
config: {
Addresses: {
Swarm: [
'/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star'
]
}
}
})
__ipfs__ = new Promise(resolve => {
ipfs.on('ready', () => {
ROOM = Room(ipfs, ROOM_NAME)
ROOM.on('message', message => {
const from = message.from
const data = message.data.toString()
if (data === 'currenthash' && currentHash) {
publishHash()
}
})
ROOM.on('subscribed', () => {
console.log('subscribed to ', ROOM_NAME)
resolve(ipfs)
})
})
})
}
return __ipfs__
}
async function appendFile(filename, newContent) {
const ipfs = await getIpfs()
let content = null
try {
content = await ipfs.files.read(filename)
}
catch (e) {
console.log('create a new file')
content = Buffer.from('')
}
await ipfs.files.write(filename, Buffer.concat([content, newContent]), { create: true, parents: true })
return ipfs.files.flush()
}
function publishHash() {
if (ROOM) {
ROOM.broadcast(currentHash)
}
}
async function publishNewDir() {
const ipfs = await getIpfs()
const dir = await ipfs.files.stat(MAIN_DIR)
currentHash = dir.hash
publishHash()
return currentHash
}
async function addItem(temp, humid, sensorId = 'main') {
const now = new Date()
const year = now.getUTCFullYear()
const month = (now.getUTCMonth() + 1).toString(10).padStart(2, '0')
const day = now.getUTCDate().toString(10).padStart(2, '0')
const ts = now.getTime()
const filename = `${MAIN_DIR}/${year}-${month}-${day}.txt`
const line = Buffer.from(`${ts},"${sensorId}",${temp},${humid}${NL}`)
await appendFile(filename, line)
console.log('additem - write,', Date.now())
return await publishNewDir()
}
async function stopIpfs() {
const ipfs = await getIpfs()
await ROOM.leave()
return ipfs.stop()
}
module.exports = {
addItem,
getIpfs,
stopIpfs
}