Files: d456a6cc8589489e558e6164750474ed2cfc961e / lib / ssb.js
1048 bytesRaw
1 | import ssbClient from 'ssb-client'; |
2 | import pull from 'pull-stream'; |
3 | import Block from 'pull-block'; |
4 | import toStream from 'pull-stream-to-stream'; |
5 | |
6 | export default class SSB { |
7 | |
8 | async connect () { |
9 | if (this.sbot) return |
10 | return new Promise((resolve, reject) => { |
11 | ssbClient((err, sbot) => { |
12 | if (err) return reject(err) |
13 | this.sbot = sbot |
14 | resolve() |
15 | }) |
16 | }) |
17 | } |
18 | |
19 | addBlob(data, cb) { |
20 | pull( |
21 | pull.once(data), |
22 | this.sbot.blobs.add((err, id) => { |
23 | if (err) return cb(err) |
24 | cb(null, {link: id, size: data.length}) |
25 | }) |
26 | ) |
27 | } |
28 | |
29 | addBlobs(cb) { |
30 | return toStream(pull( |
31 | Block({size: 5242879, zeroPadding: false}), |
32 | pull.asyncMap(this.addBlob.bind(this)), |
33 | pull.collect(cb) |
34 | )) |
35 | } |
36 | |
37 | async close (err) { |
38 | if (!this.sbot) return err && console.trace(err) |
39 | return new Promise((resolve, reject) => { |
40 | this.sbot.close(err, (err) => { |
41 | this.sbot = null |
42 | if (err && err !== true) return reject(err) |
43 | resolve() |
44 | }) |
45 | }) |
46 | } |
47 | |
48 | } |
49 |
Built with git-ssb-web