aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/app.js19
-rw-r--r--lib/git.js4
-rw-r--r--lib/util.js28
3 files changed, 43 insertions, 8 deletions
diff --git a/lib/app.js b/lib/app.js
index 1daf064..8ef859f 100644
--- a/lib/app.js
+++ b/lib/app.js
@@ -177,10 +177,7 @@ App.prototype.addBlob = function (cb) {
done(function (err, hash, add) {
cb(err, hash)
})
- return pull(
- hasher(hashCb),
- this.sbot.blobs.add(addCb)
- )
+ return sink
}
App.prototype.pushBlob = function (id, cb) {
@@ -188,15 +185,25 @@ App.prototype.pushBlob = function (id, cb) {
this.sbot.blobs.push(id, cb)
}
-App.prototype.readBlob = function (link, opts) {
+App.prototype.readBlob = function (link) {
link = u.toLink(link)
- opts = opts || {}
return this.sbot.blobs.get({
hash: link.link,
size: link.size,
+ })
+}
+
+App.prototype.readBlobSlice = function (link, opts) {
+ if (this.sbot.blobs.getSlice) return this.sbot.blobs.getSlice({
+ hash: link.link,
+ size: link.size,
start: opts.start,
end: opts.end,
})
+ return pull(
+ this.readBlob(link),
+ u.pullSlice(opts.start, opts.end)
+ )
}
App.prototype.ensureHasBlobs = function (links, cb) {
diff --git a/lib/git.js b/lib/git.js
index 07061e2..1360a6f 100644
--- a/lib/git.js
+++ b/lib/git.js
@@ -82,7 +82,7 @@ Git.prototype.openObject = function (opts, cb) {
Git.prototype.readObject = function (obj) {
return pull(
- this.app.readBlob(obj.packLink, {start: obj.offset, end: obj.next}),
+ this.app.readBlobSlice(obj.packLink, {start: obj.offset, end: obj.next}),
this.decodeObject({
type: obj.type,
length: obj.length,
@@ -310,7 +310,7 @@ Git.prototype.decodeObject = function (opts) {
if (!offset) return cb(null, 'missing source object ' +
sourcehash.toString('hex'))
var readSource = pull(
- self.app.readBlob(opts.packLink, {
+ self.app.readBlobSlice(opts.packLink, {
start: offset.offset,
end: offset.next
}),
diff --git a/lib/util.js b/lib/util.js
index e8fc435..d25ecaf 100644
--- a/lib/util.js
+++ b/lib/util.js
@@ -141,3 +141,31 @@ u.escapeHTML = function (html) {
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
}
+
+u.pullSlice = function (start, end) {
+ if (end == null) end = Infinity
+ var offset = 0
+ return function (read) {
+ return function (abort, cb) {
+ if (abort) read(abort, cb)
+ else if (offset >= end) read(true, function (err) {
+ cb(err || true)
+ })
+ else if (offset < start) read(null, function next(err, data) {
+ if (err) return cb(err)
+ offset += data.length
+ if (offset <= start) read(null, next)
+ else if (offset < end) cb(null,
+ data.slice(data.length - (offset - start)))
+ else cb(null, data.slice(data.length - (offset - start),
+ data.length - (offset - end)))
+ })
+ else read(null, function (err, data) {
+ if (err) return cb(err)
+ offset += data.length
+ if (offset <= end) cb(null, data)
+ else cb(null, data.slice(0, data.length - (offset - end)))
+ })
+ }
+ }
+}