# swarm-js

> Swarm tools for JavaScript.

Latest version **0.1.42** (published 2022-09-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install swarm-js
pnpm add swarm-js
yarn add swarm-js
bun add swarm-js
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.42 |
| Published | 2022-09-08 |
| First published | 2017-03-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 11 |
| Unpacked size | 156 KB |
| Known vulnerabilities | 0 (+13 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 47 |
| Author | MaiaVictor |
| Maintainers | maiavictor, evertonfraga |

## Links

- npm: https://www.npmjs.com/package/swarm-js
- Repository: https://github.com/maiavictor/swarm-js
- Homepage: https://github.com/maiavictor/swarm-js#readme
- Issues: https://github.com/maiavictor/swarm-js/issues
- npm.io page: https://npm.io/package/swarm-js

## Dependencies (11)

- [got](https://npm.io/package/got.md) ^11.8.5
- [tar](https://npm.io/package/tar.md) ^4.0.2
- [buffer](https://npm.io/package/buffer.md) ^5.0.5
- [eth-lib](https://npm.io/package/eth-lib.md) ^0.1.26
- [mock-fs](https://npm.io/package/mock-fs.md) ^4.1.0
- [bluebird](https://npm.io/package/bluebird.md) ^3.5.0
- [fs-extra](https://npm.io/package/fs-extra.md) ^4.0.2
- [mime-types](https://npm.io/package/mime-types.md) ^2.1.16
- [xhr-request](https://npm.io/package/xhr-request.md) ^1.0.1
- [setimmediate](https://npm.io/package/setimmediate.md) ^1.0.5
- [mkdirp-promise](https://npm.io/package/mkdirp-promise.md) ^5.0.1

## Recent versions

- 0.1.42 (latest) — 2022-09-08
- 0.1.40 — 2020-03-04
- 0.1.39 — 2018-08-31
- 0.1.38 — 2018-08-30
- 0.1.37 — 2017-10-22
- 0.1.36 — 2017-08-09
- 0.1.35 — 2017-08-01
- 0.1.34 — 2017-07-27
- 0.1.33 — 2017-07-26
- 0.1.30 — 2017-07-17
- 0.1.29 — 2017-07-17
- 0.1.28 — 2017-07-17
- 0.1.27 — 2017-07-17
- 0.1.26 — 2017-07-17
- 0.1.25 — 2017-07-17
- … 24 more at https://npm.io/package/swarm-js/versions

## README

## 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

    ```bash
    npm install swarm-js
    ```

2. Import

    ```javascript
    // 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:

        ```javascript
        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).

        ```javascript
        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:

        ```javascript
        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:

        ```javascript
        // 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:

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

    - Directory:

        ```javascript
        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:

        ```javascript
        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

```javascript
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](/examples).

---
_Source: https://npm.io/package/swarm-js · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
