diff --git a/README.md b/README.md index 927d983..3733488 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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 diff --git a/auto.js b/auto.js new file mode 100644 index 0000000..eeaecc5 --- /dev/null +++ b/auto.js @@ -0,0 +1,10 @@ +const global = require('global') + +if (global.DatArchive) { + module.exports = { + DatArchive: global.DatArchive, + destroy: () => void 0 + } +} else { + module.exports = require('./promise')() +} diff --git a/package.json b/package.json index 06d1ab0..38bb035 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/promise.js b/promise.js index 1cfbbfa..a7e5499 100644 --- a/promise.js +++ b/promise.js @@ -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) { @@ -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 => @@ -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