0.8.0 • Published 4 years ago

@sammacbeth/dat-protocol-handler v0.8.0

Weekly downloads
23
License
MIT
Repository
github
Last release
4 years ago

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/dat-archive: Factory for the DatArchive API.
  • @sammacbeth/dat-protocol-handler: Resolves dat URLs to Node Streams, backed by the core API.

Usage

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

0.8.0

4 years ago

0.7.0

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago

0.5.2-alpha.14

4 years ago

0.6.0-alpha.0

4 years ago

0.5.0

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.11

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago