diff --git a/README.md b/README.md index 5d9afaf..5e2c896 100644 --- a/README.md +++ b/README.md @@ -21,8 +21,8 @@ The Dat SDK combines the lower level pieces of the Dat ecosystem into high level ## API/Examples ``` -const SDK = require('dat-sdk') -const { Hypercore, Hyperdrive, resolveName, destroy } = SDK() +const SDK = require('../') +const { Hypercore, Hyperdrive, resolveName, deleteStorage, destroy } = SDK() const archive = Hyperdrive(null, { // This archive will disappear after the process exits @@ -50,6 +50,14 @@ resolveName('dat://beakerbrowser.com', (err, url) => { archive.readFile('/dat.json', 'utf8', (err, data) => { if (err) throw err console.log(`Beaker's dat.json is ${data}`) + + archive.close((err) => { + if (err) throw err + deleteStorage(archive.key, (e) => { + if (e) throw e + console.log('Deleted beaker storage') + }) + }) }) }) @@ -61,21 +69,14 @@ reallyReady(someArchive, () => { someArchive.readdir('/', console.log) }) +// This make sure you sync up with peers before trying to do anything with the archive function reallyReady (archive, cb) { - let wasReady = false - archive.metadata.once('sync', tryReady) - archive.readdir('/', function (e) { - if (e) return - console.log('Already loaded metadata?') - wasReady = true - cb() - }) - - function tryReady () { - if (wasReady) return - console.log('Got an append event so it must be loaded') - wasReady = true - cb() + if (archive.metadata.peers.length) { + archive.metadata.update({ ifAvailable: true }, cb) + } else { + archive.metadata.once('peer-add', () => { + archive.metadata.update({ ifAvailable: true }, cb) + }) } } diff --git a/example/example.js b/example/example.js index e773bda..09be432 100644 --- a/example/example.js +++ b/example/example.js @@ -1,5 +1,5 @@ const SDK = require('../') -const { Hypercore, Hyperdrive, resolveName, destroy } = SDK() +const { Hypercore, Hyperdrive, resolveName, deleteStorage, destroy } = SDK() const archive = Hyperdrive(null, { // This archive will disappear after the process exits @@ -27,6 +27,14 @@ resolveName('dat://beakerbrowser.com', (err, url) => { archive.readFile('/dat.json', 'utf8', (err, data) => { if (err) throw err console.log(`Beaker's dat.json is ${data}`) + + archive.close((err) => { + if (err) throw err + deleteStorage(archive.key, (e) => { + if (e) throw e + console.log('Deleted beaker storage') + }) + }) }) }) @@ -40,11 +48,11 @@ reallyReady(someArchive, () => { // This make sure you sync up with peers before trying to do anything with the archive function reallyReady (archive, cb) { - if(archive.metadata.peers.length) { - archive.metadata.update({ifAvailable: true}, cb) + if (archive.metadata.peers.length) { + archive.metadata.update({ ifAvailable: true }, cb) } else { archive.metadata.once('peer-add', () => { - archive.metadata.update({ifAvailable: true}, cb) + archive.metadata.update({ ifAvailable: true }, cb) }) } } diff --git a/index.js b/index.js index d8b946a..9880188 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ const DatEncoding = require('dat-encoding') const crypto = require('hypercore-crypto') const RAM = require('random-access-memory') const fs = require('fs') +const path = require('path') const datDNS = require('dat-dns') const hyperdrive = require('hyperdrive') @@ -56,6 +57,10 @@ function SDK ({ storageOpts, swarmOpts, driveOpts, coreOpts, dnsOpts } = {}) { return dns.resolveName(url, cb) } + function deleteStorage (key, cb) { + storage.delete(key, cb) + } + function Hyperdrive (location, opts) { opts = Object.assign({}, DEFAULT_DRIVE_OPTS, driveOpts, opts) @@ -85,13 +90,17 @@ function SDK ({ storageOpts, swarmOpts, driveOpts, coreOpts, dnsOpts } = {}) { let driveStorage = null try { driveStorage = persist ? storage.getDrive(location) : RAM - } catch(e) { - if(e.message !== 'Unable to create storage') throw e + } catch (e) { + if (e.message !== 'Unable to create storage') throw e + + // If the folder isn't a dat archive. Turn it into one. const { publicKey, secretKey } = crypto.keyPair() fs.writeFileSync(path.join(location, '.dat'), publicKey) key = publicKey location = DatEncoding.encode(publicKey) opts.secretKey = secretKey + + driveStorage = persist ? storage.getDrive(location) : RAM } const drive = hyperdrive(driveStorage, key, opts) @@ -162,6 +171,7 @@ function SDK ({ storageOpts, swarmOpts, driveOpts, coreOpts, dnsOpts } = {}) { Hyperdrive, Hypercore, resolveName, + deleteStorage, destroy } } diff --git a/package.json b/package.json index cc5349a..66052da 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "hyperdiscovery": "^10.0.0", "hyperdrive": "^9.15.0", "random-access-memory": "^3.1.1", - "universal-dat-storage": "^1.2.1" + "universal-dat-storage": "^1.3.1" }, "devDependencies": { "browserify": "^16.2.3",