Files: b5f837feead2289e0e40ebb9331c756fc171e063 / lib / ssb.js
1031 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(cb) |
23 | ) |
24 | } |
25 | |
26 | addBlobs(cb) { |
27 | return toStream(pull( |
28 | Block({size: 5242879, zeroPadding: false}), |
29 | pull.asyncMap((buf, cb) => { |
30 | pull( |
31 | pull.once(buf), |
32 | this.sbot.blobs.add(cb) |
33 | ) |
34 | }), |
35 | pull.collect(cb) |
36 | )) |
37 | } |
38 | |
39 | async close (err) { |
40 | if (!this.sbot) return err && console.trace(err) |
41 | return new Promise((resolve, reject) => { |
42 | this.sbot.close(err, (err) => { |
43 | this.sbot = null |
44 | if (err && err !== true) return reject(err) |
45 | resolve() |
46 | }) |
47 | }) |
48 | } |
49 | |
50 | } |
51 |
Built with git-ssb-web