swarm-js
Swarm tools for JavaScript.
Last updated 4 years ago by maiavictor .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install swarm-js 
SYNC missed versions from official npm registry.

Disclaimer

This library isn't activelly maintained as I moved on to other things. If you'd like to maintain it, please let me know. For now, I think I can point to Erebos: https://erebos.js.org/

Swarm.js

This library allows you to interact with the Swarm network from JavaScript.

Getting started

  1. Install

    npm install swarm-js
    
  2. Import

    // Loads the Swarm API pointing to the official gateway
    const swarm = require("swarm-js").at("http://swarm-gateways.net");
    

Examples

Uploads

  • With JSON:

    • Raw data:

      const file = "test file"; // could also be an Uint8Array of binary data
      swarm.upload(file).then(hash => {
        console.log("Uploaded file. Address:", hash);
      })
      
    • 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 (Uint8Array or UTF-8 String).

      const dir = {
        "/foo.txt": {type: "text/plain", data: "file 0"},
        "/bar.txt": {type: "text/plain", data: "file 1"}
      };
      swarm.upload(dir).then(hash => {
        console.log("Uploaded directory. Address:", hash);
      });
      
  • From disk:

    • On Node.js:

      swarm.upload({
        path: "/path/to/thing",      // path to data / file / directory
        kind: "directory",           // could also be "file" or "data"
        defaultFile: "/index.html"}) // optional, and only for kind === "directory"
        .then(console.log)
        .catch(console.log);
      
    • On browsers:

      // only works inside an event
      document.onClick = function() {
        swarm.upload({pick: "file"}) // could also be "directory" or "data"
          .then(alert);
      };
      

Downloads

  • With JSON:

    • Raw data:

      const fileHash = "a5c10851ef054c268a2438f10a21f6efe3dc3dcdcc2ea0e6a1a7a38bf8c91e23";
      swarm.download(fileHash).then(array => {
        console.log("Downloaded file:", swarm.toString(array));
      });
      
    • Directory:

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

    • On Node.js:

      swarm.download("DAPP_HASH", "/target/dir")
        .then(path => console.log(`Downloaded DApp to ${path}.`))
        .catch(console.log);
      
    • On browser:

      (Just link the Swarm URL.)

SwarmHash

console.log(swarm.hash("unicode string áéíóú λ"));
console.log(swarm.hash("0x41414141"));
console.log(swarm.hash([65, 65, 65, 65]));
console.log(swarm.hash(new Uint8Array([65, 65, 65, 65])));

More

For more examples, check out examples.

Current Tags

  • 0.1.42                                ...           latest (4 years ago)

39 Versions

  • 0.1.42                                ...           4 years ago
  • 0.1.40                                ...           6 years ago
  • 0.1.39                                ...           8 years ago
  • 0.1.38                                ...           8 years ago
  • 0.1.37                                ...           8 years ago
  • 0.1.36                                ...           9 years ago
  • 0.1.35                                ...           9 years ago
  • 0.1.34                                ...           9 years ago
  • 0.1.33                                ...           9 years ago
  • 0.1.30                                ...           9 years ago
  • 0.1.29                                ...           9 years ago
  • 0.1.28                                ...           9 years ago
  • 0.1.27                                ...           9 years ago
  • 0.1.26                                ...           9 years ago
  • 0.1.25                                ...           9 years ago
  • 0.1.24                                ...           9 years ago
  • 0.1.23                                ...           9 years ago
  • 0.1.22                                ...           9 years ago
  • 0.1.21                                ...           9 years ago
  • 0.1.20                                ...           9 years ago
  • 0.1.19                                ...           9 years ago
  • 0.1.18                                ...           9 years ago
  • 0.1.17                                ...           9 years ago
  • 0.1.16                                ...           9 years ago
  • 0.1.15                                ...           9 years ago
  • 0.1.14                                ...           9 years ago
  • 0.1.13                                ...           9 years ago
  • 0.1.12                                ...           9 years ago
  • 0.1.11                                ...           9 years ago
  • 0.1.10                                ...           9 years ago
  • 0.1.9                                ...           9 years ago
  • 0.1.8                                ...           9 years ago
  • 0.1.7                                ...           9 years ago
  • 0.1.6                                ...           9 years ago
  • 0.1.5                                ...           9 years ago
  • 0.1.4                                ...           9 years ago
  • 0.1.3                                ...           9 years ago
  • 0.1.1                                ...           9 years ago
  • 0.1.0                                ...           9 years ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (11)
Dev Dependencies (5)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |