Skip to content

Commit

Permalink
Added auto.js for auto-detecting beaker and sane defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
RangerMauve committed Aug 27, 2019
1 parent 5d87df7 commit 5062a57
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
30 changes: 28 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,28 @@ The Dat SDK combines the lower level pieces of the Dat ecosystem into high level
- React-Native?
- Electron?

## API/Examples (Promise)

```js
// Auto-detects sane defaults based on your environment
// Uses Beaker's APIs if they are if they are available
// DatArchive is the same as Beaker
// https://beakerbrowser.com/docs/apis/dat
const {DatArchive} = require('dat-sdk/auto')

const archive = await DatArchive.load('dat://dat.foundation')

const someData = await DatArchive.readFile('/dat.json', 'utf8')

console.log('Dat foundation dat.json:', someData)

const myArchive = await DatArchive.create({
title: 'My Archive'
})

await myArchive.writeFile('/example.txt', 'Hello World!')
```

## API/Examples (Callbacks)

```js
Expand Down Expand Up @@ -160,6 +182,10 @@ trie.put('key', 'value', () => {
- [x] Ability to close drives
- [x] Check that hypercore replication is working
- [x] Release v0.2.0
- [x] Initial Promise API / Beaker support
- [x] Implement DatArchive API over CB based SDK
- [x] Expose 'auto' module that automatically configures the SDK
- [x] Release
- [ ] New Hyperdrive and Hyperswarm and Corestore
- [ ] Add corestore for replication
- [ ] virtual-corestore API
Expand All @@ -172,8 +198,8 @@ trie.put('key', 'value', () => {
- [ ] Test that hypercore still works using web storage / proxying
- [ ] Make sure tests work in Node / Web / Beaker
- [ ] Release
- [ ] Initial Promise API
- [ ] Draft API (Hyperdrive, Hypercore, DNS, Corestore)
- [ ] Promise API for new data types
- [ ] Draft API (Hypercore, DNS, Corestore)
- [ ] Create wrappers over Callback API
- [ ] Auto-detect presence of Beaker APIs and use those
- [ ] Release
Expand Down
10 changes: 10 additions & 0 deletions auto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const global = require('global')

if (global.DatArchive) {
module.exports = {
DatArchive: global.DatArchive,
destroy: () => void 0
}
} else {
module.exports = require('./promise')()
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"dat-dns": "^4.0.0",
"dat-encoding": "^5.0.1",
"dom-event-target": "^1.0.0",
"global": "^4.4.0",
"hex-to-32": "^2.0.0",
"hypercore": "^7.5.0",
"hypercore-crypto": "^1.0.0",
Expand Down
17 changes: 4 additions & 13 deletions promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ module.exports = function SDK (opts) {
return new Promise((resolve, reject) => {
function cb (err, result) {
// Ignore errors saying we're up to date
if(err && err.message !== 'No update available from peers') reject(err)
if (err && err.message !== 'No update available from peers') reject(err)
else resolve(result)
}
if (archive.metadata.peers.length) {
Expand Down Expand Up @@ -153,16 +153,6 @@ module.exports = function SDK (opts) {
this.url = this.url || `dat://${archive.key.toString('hex')}`
this._loadPromise = null

// if (!archive.writable && !archive.metadata.length) {
// // wait to receive a first update
// await new Promise((resolve, reject) => {
// archive.metadata.update(err => {
// if (err) reject(err)
// else resolve()
// })
// })
// }

var s = toEventTarget(pda.createNetworkActivityStream(this._archive))

s.addEventListener('network-changed', detail =>
Expand Down Expand Up @@ -462,8 +452,9 @@ module.exports = function SDK (opts) {
return archive
}

static async create ({ title, description, type, author } = {}) {
const archive = new DatArchive(null)
static async create (options = {}) {
const { title, description, type, author } = options
const archive = new DatArchive(null, options)

await archive._loadPromise

Expand Down

0 comments on commit 5062a57

Please sign in to comment.