import ssbClient from 'ssb-client'; import pull from 'pull-stream'; import Block from 'pull-block'; import toStream from 'pull-stream-to-stream'; export default class SSB { async connect () { if (this.sbot) return return new Promise((resolve, reject) => { ssbClient((err, sbot) => { if (err) return reject(err) this.sbot = sbot resolve() }) }) } addBlob(data, cb) { pull( pull.once(data), this.sbot.blobs.add((err, id) => { if (err) return cb(err) cb(null, {link: id, size: data.length}) }) ) } addBlobs(cb) { return toStream(pull( Block({size: 5242879, zeroPadding: false}), pull.asyncMap(this.addBlob.bind(this)), pull.collect(cb) )) } async close (err) { if (!this.sbot) return err && console.trace(err) return new Promise((resolve, reject) => { this.sbot.close(err, (err) => { this.sbot = null if (err && err !== true) return reject(err) resolve() }) }) } }