git ssb

0+

cel / pull-git-remote-helper



Commit ef07dd5bda4ebb6d6e9f63ebf742df7e23029f5f

Read all objects

Charles Lehner committed on 2/8/2016, 4:37:22 AM
Parent: 95a067249e90995c9d34e46ae4aa937180c1d2f6

Files changed

pack.jschanged
package.jsonchanged
pack.jsView
@@ -1,9 +1,9 @@
1-var zlib = require('zlib')
21 var buffered = require('pull-buffered')
32 var crypto = require('crypto')
43 var pull = require('pull-stream')
54 var toPull = require('stream-to-pull-stream')
5+var Inflate = require('pako/lib/inflate').Inflate
66
77 exports.decode = decodePack
88
99 var objectTypes = [
@@ -18,13 +18,48 @@
1818 }
1919
2020 function createHash(type) {
2121 var hash = crypto.createHash(type)
22- return pull.through(function (data) {
23- hash.update(data)
24- })
22+ var hasher = pull.through(hash.update.bind(hash))
23+ hasher.digest = hash.digest.bind(hash)
24+ return hasher
2525 }
2626
27+function inflateBytes(read) {
28+ var inflate = new Inflate()
29+ var ended, dataOut
30+
31+ inflate.onData = function (data) {
32+ dataOut = new Buffer(data)
33+ // console.error('inflated data', data.length)
34+ }
35+
36+ inflate.onEnd = function (status) {
37+ ended = (status === 0) ? true : new Error(inflate.msg)
38+ // console.error('inflated end', status, ended)
39+ }
40+
41+ return function (abort, cb) {
42+ if (ended) return cb(ended)
43+ read(abort, function next(end, data) {
44+ if (end === true) {
45+ end = null
46+ data = []
47+ }
48+ if (ended = end) return cb(end)
49+ if (data.length > 1) return cb(new Error('got more than one byte'))
50+ dataOut = null
51+ inflate.push(data, end === true)
52+ if (dataOut)
53+ cb(null, dataOut)
54+ else if (ended)
55+ cb(ended)
56+ else
57+ read(null, next)
58+ })
59+ }
60+}
61+
2762 function decodePack(onEnd, read) {
2863 if (read === undefined)
2964 return decodePack.bind(this, onEnd)
3065
@@ -35,8 +70,11 @@
3570 var readByte = b.chunks(1)
3671 var readWord = b.chunks(4)
3772 var readChecksum = b.chunks(20)
3873 var expectChecksum = false
74+ var opts = {
75+ verbosity: 2
76+ }
3977
4078 function readHeader(cb) {
4179 readWord(null, function (end, header) {
4280 if (ended = end) return cb(end)
@@ -61,8 +99,10 @@
6199 function readNumObjects(cb) {
62100 readWord(null, function (end, word) {
63101 if (ended = end) return cb(end)
64102 numObjects = word.readUInt32BE()
103+ if (opts.verbosity >= 1)
104+ console.error(numObjects + ' objects')
65105 readObject(null, cb)
66106 })
67107 }
68108
@@ -71,43 +111,41 @@
71111 // https://codewords.recurse.com/images/three/varint.svg
72112 readByte(null, function (end, buf) {
73113 if (ended = end) return cb(end)
74114 var firstByte = buf[0]
75- type = objectTypes[(firstByte & 0b01110000) >> 4]
76- value = firstByte & 0b00001111
115+ type = objectTypes[(firstByte >> 4) & 7]
116+ value = firstByte & 15
77117 console.error('byte1', firstByte, firstByte.toString(2), value, value.toString(2))
78118 shift = 4
79119 checkByte(firstByte)
80120 })
81121
82122 function checkByte(byte) {
83- if (byte & 0b10000000)
123+ if (byte & 0x80)
84124 readByte(null, gotByte)
85125 else
86126 cb(null, type, value)
87127 }
88128
89129 function gotByte(end, buf) {
90130 if (ended = end) return cb(end)
91131 var byte = buf[0]
92- value |= ((byte & 0b01111111) << shift)
132+ value += (byte & 0x7f) << shift
93133 shift += 7
94134 console.error('byte', byte, byte.toString(2), value, value.toString(2))
95135 checkByte(byte)
96136 }
97137 }
98138
99139 function getObject(cb) {
100140 readVarInt(function (end, type, length) {
141+ console.error('read var int', end, type, length)
101142 if (end === true && expectChecksum)
102143 onEnd(new Error('Missing checksum'))
103144 if (ended = end) return cb(end)
104- console.error('length', length)
105145 numObjects--
106- cb(null, type, pull(
107- b.take(length),
108- toPull(zlib.createInflate())
109- ))
146+ // TODO: verify that the inflated data is the correct length
147+ cb(null, type, inflateBytes(readByte))
110148 })
111149 }
112150
113151 function readTrailer(cb) {
package.jsonView
@@ -20,8 +20,9 @@
2020 "bugs": {
2121 "url": "https://github.com/clehner/pull-git-remote-helper/issues"
2222 },
2323 "dependencies": {
24+ "pako": "^0.2.8",
2425 "pull-buffered": "^0.3.0",
2526 "pull-cat": "^1.1.8",
2627 "pull-stream": "^3.1.0",
2728 "stream-to-pull-stream": "^1.6.6"

Built with git-ssb-web