This repository has been archived by the owner on Dec 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublisher.js
150 lines (138 loc) · 3.65 KB
/
publisher.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* prefix [publishe] to all logs
*/
const pLog = function() {
args = [];
args.push('[publishe] ');
// Note: arguments is part of the prototype
for(let i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
console.log.apply(console, args);
};
/*
* pin file to network
*/
async function pin(filedata) {
const content = ipfs.Buffer.from(filedata);
return ipfs.add(content);
}
/*
* get hash of file
*/
async function hash(filedata) {
const content = ipfs.Buffer.from(filedata);
const r = await ipfs.add(content, {onlyHash: true});
return r[0].hash;
}
/*
* unpin file from network
*/
async function unpin(hash) {
return ipfs.pin.rm(hash);
}
/*
* pin all files to the network
*/
async function pinAll() {
pLog('Pinning all files ...');
const contents = fs.readdirSync(`./${ipfs.host}/`);
for (i in contents) {
const path = `./${ipfs.host}/${contents[i]}`;
if (fs.statSync(path).isDirectory()) {
const files = fs.readdirSync(path);
pLog(`Pinning ${files.length} in ${path}`);
// only add 200 entries at a time, because the socket might not be able to handle all at once
for (let i = Math.ceil(files.length/200)-1; i >= 0; i--) {
await Promise.all(files.slice(i*200, i*200+200).map(async (file) => {
const filedata = fs.readFileSync(`${path}/${file}`, 'utf8');
await pin(filedata);
}));
}
}
}
pLog('Pinned all files.');
}
/*
* Remove all pins
*/
async function unpinAll() {
await ipfs.pin.ls()
.then(async (pinset) => {
if (pinset.length > 0) {
pLog(`Removing pinset of length ${pinset.length}`);
const a = [];
pinset.forEach((p) => {
if (p.type != 'indirect') {
a.push(ipfs.pin.rm(p.hash));
}
});
await Promise.all(a);
}
})
.catch((err) => {log(err);});
pLog('Removed pinset.');
}
/*
* get a file from the network
*/
async function get(address, filepath) {
return ipfs.cat(address)
.then((content) => {
fs.writeFileSync(filepath, content.toString('utf8'), (e) => {soLog(e);})
})
.catch((err) => {pLog(err);});
}
/*
* publish a message to network
*/
function pub(channel, event, query, payload) {
const msgEncoded = ipfs.Buffer.from(JSON.stringify({ event, query, payload }));
return ipfs.pubsub.publish(`${ipfsearch.topic}${channel}`, msgEncoded)
.catch(err => {
pLog(`Error: Failed to publish ${event} to channel ${ipfsearch.topic}${channel}`);
pLog(err);
});
}
/*
* publish query to network
*/
function pubQuery(topic, query) {
pLog(`(${topic}) Asking for ${query}...`);
stats.searchSent += 1;
return pub(topic, 'query', query, '')
}
/*
* publish answer to network
*/
function pubAnswer(topic, query, results) {
pLog(`(${topic}) Answering with ${results.length} to ${query}...`);
stats.searchSent += 1;
return pub(topic, 'answer', query, results)
}
/*
* publish request for files to network
*/
function pubFileReq(topic, request) {
pLog(`(${topic}) Requesting files for ${request}...`);
stats.soSent += 1;
return pub(topic, 'fileReq', request, '')
}
/*
* publish response to request for files to network
*/
function pubFileRes(topic, request, results) {
pLog(`(${topic}) ${results.length} files for ${request}...`);
stats.soSent += 1;
return pub(topic, 'fileRes', request, results)
}
module.exports.pubQuery = pubQuery;
module.exports.pubAnswer = pubAnswer;
module.exports.pubFileReq = pubFileReq;
module.exports.pubFileRes = pubFileRes;
module.exports.get = get;
module.exports.hash = hash;
module.exports.pin = pin;
module.exports.unpin = unpin;
module.exports.pinAll = pinAll;
module.exports.unpinAll = unpinAll;