Giter Site home page Giter Site logo

maiavictor--swarm-js's Introduction

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.

maiavictor--swarm-js's People

Contributors

evertonfraga avatar victortaelin avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.