Skip to content

Commit

Permalink
Improve API
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorTaelin committed Mar 4, 2017
1 parent 3cd4c5a commit 425dec0
Show file tree
Hide file tree
Showing 11 changed files with 246 additions and 127 deletions.
40 changes: 24 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ This library allows you to interact with the Swarm network from JavaScript. It:

## Basic usage

The simplest use case for Swarm is uploading/downloading raw data and directories.
The simplest use case for Swarm is uploading/downloading raw data and directories. First, load the lib:

```javascript
// Loads the Swarm API pointing to the official gateway
Expand All @@ -31,44 +31,52 @@ const swarm = require("swarm-js").at("http://swarm-gateways.net");

#### Upload raw data

To upload raw data, just call `swarm.upload(buffer)`. It returns a promise with the uploaded hash.

```javascript
const file = "test file";
swarm.uploadData(new Buffer(file)).then(hash => {
swarm.upload(new Buffer(file)).then(hash => {
console.log("Uploaded file. Address:", hash);
})
```

#### Downlod raw data

To download raw data, just call `swarm.download(hash)`. It returns a promise with the data buffer.

```javascript
const fileHash = "a5c10851ef054c268a2438f10a21f6efe3dc3dcdcc2ea0e6a1a7a38bf8c91e23";
swarm.downloadData(fileHash).then(buffer => {
swarm.download(fileHash).then(buffer => {
console.log("Downloaded file:", buffer.toString());
});
```

#### Upload a directory

To upload a directory, just call `swarm.upload(directory)`, where directory is an object mapping paths to entries, those containing a mime-type and the data (a buffer).

```javascript
const dir = {
"/foo.txt": "sample file",
"/bar.txt": "another file"
"/foo.txt": {type: "text/plain", data: new Buffer("sample file")},
"/bar.txt": {type: "text/plain", data: new Buffer("another file")}
};
swarm.uploadDirectory(dir).then(hash => {
swarm.upload(dir).then(hash => {
console.log("Uploaded directory. Address:", hash);
});
```

#### Download a directory

To dowwnload a directory, just call `swarm.download(hash)`. Swarm.js will return a directory instead of a buffer by detecting the existence of a manifest on that hash.

```javascript
const dirHash = "7e980476df218c05ecfcb0a2ca73597193a34c5a9d6da84d54e295ecd8e0c641";
swarm.downloadDirectory(dirHash).then(dir => {
swarm.download(dirHash).then(dir => {
console.log("Downloaded directory:");
for (let path in dir) {
console.log("-", path, ":", dir[path].toString());
console.log("-", path, ":", dir[path].data.toString());
}
}
});
```

For examples of how to upload/download from disk, please check the [`examples`](https://github.com/MaiaVictor/swarm-js/tree/master/examples) directory.
Expand All @@ -92,14 +100,14 @@ const indexHtml =
(...)

const exampleDApp = {
"" : indexHtml,
"/index.html" : indexHtml,
"/ethereum_icon.png" : ethereumIconPng,
"/foo/test_text_1.txt" : testText1,
"/foo/test_text_2.txt" : testText2
"" : {type: "text/html", data: indexHtml},
"/index.html" : {type: "text/html", data: indexHtml},
"/ethereum_icon.png" : {type: "image/png", data: ethereumIconPng},
"/foo/test_text_1.txt" : {type: "text/plain", data: testText1},
"/foo/test_text_2.txt" : {type: "text/plain", data: testText2}
}

swarm.uploadDirectory(exampleDApp)
swarm.upload(exampleDApp)
.then(console.log)
.catch(console.log);
```
Expand Down Expand Up @@ -131,7 +139,7 @@ const config = {
Swarm.local(config, swarm => new Promise((resolve, reject) => {

// Uploads data using the local node
swarm.uploadData("test").then(hash => {
swarm.upload(new Buffer("test")).then(hash => {
console.log("Uploaded data. Address:", hash);

// Closes the Swarm process.
Expand Down
25 changes: 18 additions & 7 deletions examples/dapp_download.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,27 @@
const swarm = require("./../swarm.js").at("http://swarm-gateways.net");

// The hash of the DApp we uploaded on the other example.
const exampleDAppHash = "8587c8e716bfceea12a7306d85a8a8ccad5019020916eb5a21fa47a7f1826891";
const exampleDAppHash = "379d2791624c3e3719bb28f7bfa362cc9c726ec06482b5800c8e3cefaf2b7bcf";

// Download the example DApp and print its index.html.
swarm.downloadDirectory(exampleDAppHash)
// It knows it is a DApp (not a file) by checking the existence of a manifest.
swarm.download(exampleDAppHash)
.then(console.log)
.catch(console.log);

// This script outputs:
// { '': <Buffer 3c 68 74 6d 6c 3e 0a 20 20 3c 62 6f 64 79 3e 0a 20 20 20 20 3c 68 33 3e 3c 69 6d 67 20 73 72 63 3d 22 65 74 68 65 72 65 75 6d 5f 69 63 6f 6e 2e 70 6e ... >,
// 'index.html': <Buffer 3c 68 74 6d 6c 3e 0a 20 20 3c 62 6f 64 79 3e 0a 20 20 20 20 3c 68 33 3e 3c 69 6d 67 20 73 72 63 3d 22 65 74 68 65 72 65 75 6d 5f 69 63 6f 6e 2e 70 6e ... >,/
// 'ethereum_icon.png': <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 00 10 00 00 00 10 08 04 00 00 00 b5 fa 37 ea 00 00 00 f7 49 44 41 54 28 91 63 60 40 01 0d 09 0e ... >,
// 'foo/test_text_1.txt': <Buffer 74 65 73 74 20 74 65 78 74 20 23 31>,
// 'foo/test_text_2.txt': <Buffer 74 65 73 74 20 74 65 78 74 20 23 32> }
// { 'ethereum_icon.png':
// { type: 'image/png',
// data: <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 00 10 00 00 00 10 08 04 00 00 00 b5 fa 37 ea 00 00 00 f7 49 44 41 54 28 91 63 60 40 01 0d 09 0e ... > },
// 'index.html':
// { type: 'text/html',
// data: <Buffer 3c 68 74 6d 6c 3e 0a 20 20 3c 62 6f 64 79 3e 0a 20 20 20 20 3c 68 33 3e 3c 69 6d 67 20 73 72 63 3d 22 65 74 68 65 72 65 75 6d 5f 69 63 6f 6e 2e 70 6e ... > },
// '':
// { type: 'text/html',
// data: <Buffer 3c 68 74 6d 6c 3e 0a 20 20 3c 62 6f 64 79 3e 0a 20 20 20 20 3c 68 33 3e 3c 69 6d 67 20 73 72 63 3d 22 65 74 68 65 72 65 75 6d 5f 69 63 6f 6e 2e 70 6e ... > },
// 'foo/test_text_1.txt':
// { type: 'text/plain',
// data: <Buffer 74 65 73 74 20 74 65 78 74 20 23 31> },
// 'foo/test_text_2.txt':
// { type: 'text/plain',
// data: <Buffer 74 65 73 74 20 74 65 78 74 20 23 32> } }
2 changes: 1 addition & 1 deletion examples/dapp_download_to_disk.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ const path = require("path");
const exampleDAppHash = "8587c8e716bfceea12a7306d85a8a8ccad5019020916eb5a21fa47a7f1826891";
const targetDirPath = path.join(__dirname,"example_dapp");

swarm.downloadDirectoryToDisk(exampleDAppHash)(targetDirPath)
swarm.download(exampleDAppHash, targetDirPath)
.then(dirPath => console.log(`Downloaded DApp to ${dirPath}.`))
.catch(console.log);
18 changes: 9 additions & 9 deletions examples/dapp_upload.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// This uploads a DApp to the Swarm network *without* using the filesystem.
// That allows a web application running on the browser to upload any DApp.
// Outputs the DApp address. In this case, it is:
// 8587c8e716bfceea12a7306d85a8a8ccad5019020916eb5a21fa47a7f1826891
// 379d2791624c3e3719bb28f7bfa362cc9c726ec06482b5800c8e3cefaf2b7bcf
// You can access it at:
// http://swarm-gateways.net/bzz:/8587c8e716bfceea12a7306d85a8a8ccad5019020916eb5a21fa47a7f1826891/
// http://swarm-gateways.net/bzz:/379d2791624c3e3719bb28f7bfa362cc9c726ec06482b5800c8e3cefaf2b7bcf/

const swarm = require("./../swarm.js").at("http://swarm-gateways.net");

Expand Down Expand Up @@ -49,16 +49,16 @@ const testText2 = "test text #2";
// The DApp is just an object mapping routes to contents, which can be either
// strings or buffers. Mime types are inferred from the extension. You can also
// force them by using `{"type": "application/json", "content": "[1,2,3]"}`, for
// example. Notice the empty route also poiting to index.html.
// example. Notice the empty route also pointing to index.html.
const exampleDApp = {
"" : indexHtml,
"/index.html" : indexHtml,
"/ethereum_icon.png" : ethereumIconPng,
"/foo/test_text_1.txt" : testText1,
"/foo/test_text_2.txt" : testText2
"" : {type: "text/html", data: indexHtml},
"/index.html" : {type: "text/html", data: indexHtml},
"/ethereum_icon.png" : {type: "image/png", data: ethereumIconPng},
"/foo/test_text_1.txt" : {type: "text/plain", data: testText1},
"/foo/test_text_2.txt" : {type: "text/plain", data: testText2}
}

// Now we just upload it.
swarm.uploadDirectory(exampleDApp)
swarm.upload(exampleDApp)
.then(console.log)
.catch(console.log);
6 changes: 2 additions & 4 deletions examples/dapp_upload_from_disk.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// Uploads a DApp from a local directory to the Swarm network.
// Outputs the DApp address. In this case, it is:
// 8587c8e716bfceea12a7306d85a8a8ccad5019020916eb5a21fa47a7f1826891
// 379d2791624c3e3719bb28f7bfa362cc9c726ec06482b5800c8e3cefaf2b7bcf

const swarm = require("./../swarm.js").at("http://swarm-gateways.net");
const path = require("path");

// Notice we use the `WithDefaultPath` version, which allows us to specify a
// root file that will be served on the empty path (e.g., bzz:/my_dapp.eth/).
swarm.uploadDirectoryFromDiskWithDefaultPath("/index.html")(path.join(__dirname,"example_dapp"))
swarm.uploadDirectoryFromDisk("/index.html", path.join(__dirname,"example_dapp"))
.then(console.log)
.catch(console.log);
6 changes: 3 additions & 3 deletions examples/data_download.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Downloads raw data from the Swarm network.
// Outputs the downloaded data: "test string"
// Outputs the downloaded data: "test"

const swarm = require("./../swarm.js").at("http://swarm-gateways.net");
const hash = "a5c10851ef054c268a2438f10a21f6efe3dc3dcdcc2ea0e6a1a7a38bf8c91e23";
const hash = "c9a99c7d326dcc6316f32fe2625b311f6dc49a175e6877681ded93137d3569e7";

swarm.downloadData(hash)
swarm.download(hash)
.then(buffer => console.log(buffer.toString()))
.catch(console.log);

4 changes: 2 additions & 2 deletions examples/data_upload.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Uploads raw data to the Swarm network.
// Outputs the Swarm address: a5c10851ef054c268a2438f10a21f6efe3dc3dcdcc2ea0e6a1a7a38bf8c91e23
// Outputs the Swarm address: c9a99c7d326dcc6316f32fe2625b311f6dc49a175e6877681ded93137d3569e7

const swarm = require("./../swarm.js").at("http://swarm-gateways.net");

swarm.uploadData("test string")
swarm.upload(new Buffer("test"))
.then(console.log)
.catch(console.log);
14 changes: 7 additions & 7 deletions examples/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ const swarm = require("./../swarm.js").at("http://swarm-gateways.net");

// Uploading raw data
const file = "test file";
swarm.uploadData(new Buffer(file)).then(hash => {
swarm.upload(new Buffer(file)).then(hash => {
console.log("Uploaded file. Address:", hash);
})

// Downloading raw data
const fileHash = "a5c10851ef054c268a2438f10a21f6efe3dc3dcdcc2ea0e6a1a7a38bf8c91e23";
swarm.downloadData(fileHash).then(buffer => {
swarm.download(fileHash).then(buffer => {
console.log("Downloaded file:", buffer.toString());
});

// Uploading directory
const dir = {
"/foo.txt": "sample file",
"/bar.txt": "another file"
"/foo.txt": {type: "text/plain", data: new Buffer("sample file")},
"/bar.txt": {type: "text/plain", data: new Buffer("another file")}
};
swarm.uploadDirectory(dir).then(hash => {
swarm.upload(dir).then(hash => {
console.log("Uploaded directory. Address:", hash);
});

// Downloaading a directory
const dirHash = "7e980476df218c05ecfcb0a2ca73597193a34c5a9d6da84d54e295ecd8e0c641";
swarm.downloadDirectory(dirHash).then(dir => {
swarm.download(dirHash).then(dir => {
console.log("Downloaded directory:");
for (let path in dir) {
console.log("-", path, ":", dir[path].toString());
console.log("-", path, ":", dir[path].data.toString());
}
});
8 changes: 5 additions & 3 deletions examples/runNode.js → examples/run_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ const config = {
// Magically starts a local Swarm node
// Downloads binaries if necessary
Swarm.local(config)(swarm => new Promise((resolve, reject) => {
console.log("running");
console.log(swarm);

// Uploads data using the local node
swarm.uploadData("test").then(hash => {
swarm.upload(new Buffer("test")).then(hash => {
console.log("Uploaded data. Address:", hash);

// Closes the Swarm process.
resolve();
});

})).then(() => console.log("Done!"));

}))
.then(() => console.log("Done!"));
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
{
"name": "swarm-js",
"version": "0.1.0",
"version": "0.1.3",
"description": "Swarm tools for JavaScript.",
"main": "swarm.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "MaiaVictor",
"license": "MIT"
"license": "MIT",
"dependencies": {
"bluebird": "^3.4.7",
"fs-promise": "^2.0.0",
"got": "^6.7.1",
"mimetype": "0.0.8",
"mkdirp-promise": "^5.0.1",
"tar.gz": "^1.0.5"
}
}
Loading

0 comments on commit 425dec0

Please sign in to comment.