Swarm.js: Decentralized Storage Client

0.1.42 · abandoned · verified Sun Apr 19

Swarm.js is a JavaScript library designed to facilitate interaction with the Swarm network, a decentralized storage platform. The package provides functionalities for uploading and downloading files and directories to and from Swarm, as well as hashing various data types. It specifically targets both Node.js and browser environments for these operations. The current stable version appears to be 0.1.42, indicating it never reached a 1.0 release. The library's development has ceased, with the maintainer explicitly stating it is no longer actively maintained and recommending Erebos (https://erebos.js.org/) as an alternative. Therefore, users should be aware of its abandoned status and consider alternatives for new projects, or for maintaining existing ones.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates installation, configuration with a gateway, and basic upload/download operations for both single files and directories.

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

const fileContent = "Hello Swarm! This is a test file.";

swarm.upload(fileContent)
  .then(hash => {
    console.log("Uploaded file. Address:", hash);
    return swarm.download(hash);
  })
  .then(downloadedArray => {
    console.log("Downloaded file content:", swarm.toString(downloadedArray));
  })
  .catch(error => {
    console.error("An error occurred:", error);
  });

const dir = {
  "/hello.txt": { type: "text/plain", data: "Hello from directory!" },
  "/meta.json": { type: "application/json", data: JSON.stringify({ version: "1.0" }) }
};

swarm.upload(dir)
  .then(dirHash => {
    console.log("Uploaded directory. Address:", dirHash);
    return swarm.download(dirHash);
  })
  .then(downloadedDir => {
    console.log("Downloaded directory:");
    for (let path in downloadedDir) {
      console.log(`- ${path}: ${downloadedDir[path].data.toString()}`);
    }
  })
  .catch(error => {
    console.error("Directory operation failed:", error);
  });

view raw JSON →