Skip to content

Commit

Permalink
Added delete, fixed create
Browse files Browse the repository at this point in the history
  • Loading branch information
RangerMauve committed Jul 24, 2019
1 parent ccf33d6 commit 2f22d03
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 23 deletions.
33 changes: 17 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
})
})
})
})
Expand All @@ -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)
})
}
}
Expand Down
16 changes: 12 additions & 4 deletions example/example.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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')
})
})
})
})

Expand All @@ -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)
})
}
}
Expand Down
14 changes: 12 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -162,6 +171,7 @@ function SDK ({ storageOpts, swarmOpts, driveOpts, coreOpts, dnsOpts } = {}) {
Hyperdrive,
Hypercore,
resolveName,
deleteStorage,
destroy
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 2f22d03

Please sign in to comment.