-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathsimple_usage.js
31 lines (26 loc) · 993 Bytes
/
simple_usage.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
const swarm = require("./..").at("http://swarm-gateways.net");
(async () => {
try {
// Uploading raw data
const file = "this is a test";
const fileHash = await swarm.upload(new Buffer(file))
console.log("Uploaded file. SwarmHash:", fileHash);
//// Downloading raw data
const fileBuffer = await swarm.download(fileHash);
console.log("Downloaded file. Contents:", swarm.toString(fileBuffer));
// Uploading directory
const dir = {
"/foo.txt": {type: "text/plain", data: "this is foo.txt"},
"/bar.txt": {type: "text/plain", data: "this is bar.txt"}
};
const dirHash = await swarm.upload(dir);
console.log("Uploaded directory. SwarmHash:", dirHash);
//// Downloaading a directory
const dirObj = await swarm.download(dirHash);
console.log("Downloaded directory. Contents:");
for (let path in dirObj)
console.log("-", path, ":", swarm.toString(dirObj[path].data));
} catch (e) {
console.log(e);
}
})();