git ssb

0+

cel-desktop / ssb-pkg



Tree: efcf9fd06b02fa9bcd28681b4c777224e19702bb

Files: efcf9fd06b02fa9bcd28681b4c777224e19702bb / lib / add-blobs.js

1690 bytesRaw
1const proc = require('child_process');
2const Transform = require('stream').Transform;
3
4module.exports = function (cb) {
5 var ids = []
6 var maxBlobSize = 5242879
7 var add, len = 0
8 var onAddDone
9 function startAdd() {
10 return proc.execFile('sbotc', ['blobs.add'], (err, stdout, stderr) => {
11 if (err) return console.error(stderr), cb(err)
12 var id = stdout.trim()
13 ids.push(id)
14 if (onAddDone) onAddDone(id), onAddDone = null
15 else console.trace(id)
16 })
17 }
18 return new Transform({
19 transform: function transform(data, encoding, cb) {
20 if (!add) add = startAdd()
21 var buf = Buffer.from(data, encoding)
22 if (buf.length === 0) return cb()
23 var remainingSpace = maxBlobSize - len
24 if (buf.length < remainingSpace) {
25 len += buf.length
26 return add.stdin.write(buf, cb)
27 }
28 var endBuf = buf.slice(0, remainingSpace)
29 var nextBuf = buf.slice(remainingSpace)
30 len += endBuf.length
31 var waiting = 2
32 onAddDone = function (id) {
33 if (!--waiting) next()
34 }
35 var stdin = add.stdin
36 add = null
37 len = 0
38 stdin.end(endBuf, function (err) {
39 if (err) return cb(err)
40 if (!--waiting) next()
41 })
42 function next() {
43 transform(nextBuf, null, cb)
44 }
45 },
46 flush: (_cb) => {
47 if (!add) {
48 _cb()
49 return cb(null, ids)
50 }
51 var waiting = 2
52 onAddDone = function (id) {
53 if (!--waiting) next()
54 }
55 add.stdin.end(function (err) {
56 if (err) return cb(err)
57 if (!--waiting) next()
58 })
59 function next() {
60 _cb()
61 cb(null, ids)
62 }
63 }
64 })
65}
66

Built with git-ssb-web