npm package: @synonymdev/slashtags-core-data
Description: Slashtags Core Data is abstraction library that encapsulates logic related to managing data on Slashtags.
Latest version: 1.0.0-alpha.11

# slashtags-core-data

Slashtags Core Data is abstraction library that encapsulates logic related to managing data on Slashtags.

## Install

```bash
npm install @synonymdev/slashtags-core-data
```

## Usage

Initialize

```js
const SlashtagsCoreData = require('@synonymdev/slashtags-core-data')
const coreData = new SlashtagsCoreData()

// Wait for instance to be ready 
await coreData.ready
const data = Buffer.from('bar')

await coreData.create('/public/foo', data)
const url = coreData.createURL('/public/foo')
// slash:<key>/public/foo
```

You can now share the URL generated from above example and read it.

```js
const SlashtagsCoreData = require('@synonymdev/slashtags-core-data')

const reader = new SlashtagsCoreData({ key }) // Key from the writer

const resolved = await reader.readRemote(url) // url shared from the writer
// <Buffer 62 61 72>
```

## API

#### `const coreData = new SlashtagsCoreData(opts)`

Create a new Slashtags Core Data instance.

`opts` is an object that includes:

- `keyPair` Optional keyPair `{secretKey: Uint8Array, publicKey: Uint8Array}` to generate local drives. keys have to be 32 bytes.
- `seeders` Optional list of seeder's public keys, where local drives will by synced with for backup and availability.
- `storage` Optinal storage path or [RandomAccessStorage](https://github.com/random-access-storage/random-access-storage) instance (defaults to [RandomAccessMemory](https://github.com/random-access-storage/random-access-memory)).
- `bootstrap` Optional [Hyperswarm](https://github.com/holepunchto/hyperswarm) bootstrapping nodes, mostly for testing in a testnet.
- `seedersTopic` Optional topic for discovery of other seeders providing remote drives that aren't announced on their own discoveryKeys, defaults to `3b9f8ccd062ca9fc0b7dd407b4cd287ca6e2d8b32f046d7958fa7bea4d78fd75`.


#### `await coreData.createURL(path)`

Create a Slashtags URL for the data stored at that path.

#### `await coreData.ready()`

Await for the instance to be ready to be used.

#### `await coreData.close()`

Gracefully close connection and instance.

#### `await coreData.create(path, data, opts)`

Same as `await coreData.update(path, data, [opts])`

`opts` is an object that includes:

- `skipSeederSync` Default to false, if set to true function will resolve without waiting for at least one seeder to reach full sync. 


#### `await coreData.update(path, data, [opts])`

Updates a file. `key` should be a string, and `data` param should be Uint8Array.

If path starts with `/public/` it will be unencrypted, otherwise it will a private encrypted drive.


`opts` is an object that includes:

- `skipSeederSync` Default to false, if set to true function will resolve without waiting for at least one seeder to reach full sync. 

#### `await coreData.delete(path, [opts])`

Deletes the value from drive.

`opts` is an object that includes:

- `skipSeederSync` Default to false, if set to true function will resolve without waiting for at least one seeder to reach full sync. 

#### `await coreData.readRemote(url, opts)`

`opts` include:
- `timeout` how long to wait to find the data before quitting. Default 10 seconds

Read the data from either a local writable drive or a remote drive shared with you.

#### `const unsubscribe = coreData.subscribe(url, onupdate)`

Watch updates to a local or a remote file, and call `onupdate(value)` function with the current value.

Call `unsubscribe()` to remove all related listeners and close resources created in `subscribe`.

## Example Scripts 
There is a bundled example showing how to read and write a JSON file in Slashtags. 

### How to use

First start by running Slashtags endpoint that will listen on a key and serve JSON data.

```sh
node ./JSONFile.js
```
Copy the `slash://` outputed by the script and open a new terminal.

```sh
node ./JSONFile.js --url <slash:// url>

```

The script should output the JSON written by the first script.