# @sammacbeth/dat-util

> Utility functions for working with DAT

Latest version **0.8.0** (published 2020-05-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install @sammacbeth/dat-util
pnpm add @sammacbeth/dat-util
yarn add @sammacbeth/dat-util
bun add @sammacbeth/dat-util
```

## 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.8.0 |
| Published | 2020-05-26 |
| First published | 2020-01-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 4.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 11 |
| Author | Sam Macbeth |
| Maintainers | sammacbeth |

## Links

- npm: https://www.npmjs.com/package/@sammacbeth/dat-util
- Repository: https://github.com/sammacbeth/sams-dat-api
- Homepage: https://github.com/sammacbeth/sams-dat-api#readme
- Issues: https://github.com/sammacbeth/sams-dat-api/issues
- npm.io page: https://npm.io/package/@sammacbeth/dat-util

## Dependencies (2)

- [rimraf](https://npm.io/package/rimraf.md) ^3.0.0
- [@sammacbeth/dat-types](https://npm.io/package/@sammacbeth/dat-types.md) ^0.8.0

## Recent versions

- 0.8.0 (latest) — 2020-05-26
- 0.7.0 — 2020-05-19
- 0.6.1 — 2020-05-15
- 0.6.0 — 2020-04-21
- 0.5.0 — 2020-02-29
- 0.0.4 — 2020-01-25
- 0.0.3 — 2020-01-23
- 0.0.2 — 2020-01-15

## README

# sams-dat-api

This repository contains a collection of modules for working with Dat's hyper*
ecosystem in a unified manner. It is written in Typescript and includes full
type annotations for Hyperdrive and Hypercore.

It currently contains the following modules:
 * `@sammacbeth/dat-types`: Typescript definitions used by other modules.
 * `@sammacbeth/dat-api-core`: Core API.
 * `@sammacbeth/dat-network-hyperdiscovery`: Dat discovery swarm using the [hyperdiscovery] module.
 * `@sammacbeth/dat-network-hyperwebrtc`: Dat discovery swarm using [hyperdiscovery] plus [discovery-swarm-webrtc].
 * `@sammacbeth/dat-api-v1`: Dat API for v1 Dats, i.e. Hyperdrive 9.x with hyperdiscovery.
 * `@sammacbeth/dat-api-v1wrtc`: As above but using `@sammacbeth/dat-network-hyperwebrtc`.
 * `@sammacbeth/dat2-api`: Dat API for v2 Dats (experimental).
 * `@sammacbeth/dat-archive`: Factory for the `DatArchive` API.
 * `@sammacbeth/dat-protocol-handler`: Resolves dat URLs to Node Streams, backed by the core API.
 * `@sammacbeth/dat-publisher`: Command line tool for creating and publishing Dats.
 * `@sammacbeth/dat-util`: Utility functions for dat (secret key import and export).

## Usage

```typescript
import apiFactory from '@sammacbeth/dat-api-v1';
import raf = require('random-access-file');

// create an API using file persistence
const api = apiFactory({
  persistantStorageFactory: (address) => Promise.resolve((file) => raf(`data/${address}/${file}`)),
});

(async () => {
  // create a dat and work with it's hyperdrive
  const dat = await api.createDat({ persist: true });
  await dat.ready;
  console.log('Created a Dat at address', dat.drive.key.toString('hex'));
  dat.drive.writeFile('file.txt', Buffer.from('hello world', 'utf8'), () =>
    console.log('wrote some data into the dat!'),
  );

  // join and leave the network
  dat.joinSwarm();
  dat.leaveSwarm();

  // load an existing dat in memory
  const existing = await api.getDat(
    '41f8a987cfeba80a037e51cc8357d513b62514de36f2f9b3d3eeec7a8fb3b5a5', {
      persist: false,
      driveOptions: {
        sparse: true,
      },
    },
  );
  // wait for data
  await existing.ready;
  console.log('Loaded remote dat');
  const files = await new Promise((resolve, reject) => {
    existing.drive.readdir('/', (err, files) => {
      if (err) {
        return reject('error listing directory');
      }
      resolve(files);
    });
  });
  console.log('Dat has files:', files);

  // close all dats and cleanup
  api.shutdown();
})();
```

## License

MIT

  [hyperdiscovery]: https://github.com/datproject/hyperdiscovery
  [discovery-swarm-webrtc]: https://github.com/geut/discovery-swarm-webrtc

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