index.jsView |
---|
64 | 64 | 'thin-pack', |
65 | 65 | ], repo.refs(), repo.symrefs(), false) |
66 | 66 | |
67 | 67 | var lines = pktLine.decode(read, options) |
68 | | - var readHave = lines.haves() |
| 68 | + var readWantHave = lines.haves() |
69 | 69 | var acked |
70 | 70 | var commonHash |
71 | 71 | var sendPack |
72 | 72 | var wants = {} |
73 | 73 | var shallows = {} |
74 | 74 | var aborted |
| 75 | + var gotWants |
75 | 76 | |
| 77 | + function readWant(abort, cb) { |
| 78 | + if (abort) return |
| 79 | + |
| 80 | + readWantHave(null, function next(end, want) { |
| 81 | + if (end || want.type == 'flush-pkt') { |
| 82 | + gotWants = true |
| 83 | + readHave(end === true ? null : end, cb) |
| 84 | + return |
| 85 | + } |
| 86 | + if (want.type == 'want') { |
| 87 | + wants[want.hash] = true |
| 88 | + } else if (want.type == 'shallow') { |
| 89 | + shallows[want.hash] = true |
| 90 | + } else { |
| 91 | + var err = new Error("Unknown thing", want.type, want.hash) |
| 92 | + return readWantHave(err, function (e) { cb(e || err) }) |
| 93 | + } |
| 94 | + readWantHave(null, next) |
| 95 | + }) |
| 96 | + } |
| 97 | + |
| 98 | + function readHave(abort, cb) { |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + if (abort) return |
| 104 | + readWantHave(null, function next(end, have) { |
| 105 | + if (end === true) { |
| 106 | + cb(true) |
| 107 | + } else if (have.type === 'flush-pkt') { |
| 108 | + |
| 109 | + if (!acked) { |
| 110 | + cb(null, 'NAK') |
| 111 | + } else { |
| 112 | + readWantHave(null, next) |
| 113 | + } |
| 114 | + } else if (end) |
| 115 | + cb(end) |
| 116 | + else if (have.type != 'have') |
| 117 | + cb(new Error('Unknown have' + JSON.stringify(have))) |
| 118 | + else if (acked) |
| 119 | + readWantHave(null, next) |
| 120 | + else |
| 121 | + repo.hasObjectFromAny(have.hash, function (err, haveIt) { |
| 122 | + if (err) return cb(err) |
| 123 | + if (!haveIt) |
| 124 | + return readWantHave(null, next) |
| 125 | + commonHash = haveIt |
| 126 | + acked = true |
| 127 | + cb(null, 'ACK ' + have.hash) |
| 128 | + }) |
| 129 | + }) |
| 130 | + } |
| 131 | + |
76 | 132 | |
77 | 133 | return cat([ |
78 | 134 | pktLine.encode(cat([ |
79 | 135 | sendRefs, |
80 | 136 | pull.once(''), |
81 | 137 | function (abort, cb) { |
82 | | - if (abort) return |
83 | | - if (acked) return cb(true) |
84 | | - |
85 | | - |
86 | | - var readWant = lines.wants() |
87 | | - readWant(null, function (end, want) { |
88 | | - if (end === true) return cb(aborted = true) |
89 | | - else if (end) cb(end) |
90 | | - else nextWant(null, want) |
91 | | - }) |
92 | | - function nextWant(end, want) { |
93 | | - if (end) return wantsDone(end === true ? null : end) |
94 | | - if (want.type == 'want') { |
95 | | - wants[want.hash] = true |
96 | | - } else if (want.type == 'shallow') { |
97 | | - shallows[want.hash] = true |
98 | | - } else { |
99 | | - var err = new Error("Unknown thing", want.type, want.hash) |
100 | | - return readWant(err, function (e) { cb(e || err) }) |
101 | | - } |
102 | | - readWant(null, nextWant) |
103 | | - } |
104 | | - |
105 | | - function wantsDone(err) { |
106 | | - if (err) return cb(err) |
107 | | - |
108 | | - |
109 | | - |
110 | | - |
111 | | - readHave(null, function next(end, have) { |
112 | | - if (end === true) { |
113 | | - |
114 | | - acked = true |
115 | | - cb(null, 'NAK') |
116 | | - } else if (end) |
117 | | - cb(end) |
118 | | - else if (have.type != 'have') |
119 | | - cb(new Error('Unknown have' + JSON.stringify(have))) |
120 | | - else |
121 | | - repo.hasObjectFromAny(have.hash, function (err, haveIt) { |
122 | | - if (err) return cb(err) |
123 | | - if (!haveIt) |
124 | | - return readHave(null, next) |
125 | | - commonHash = haveIt |
126 | | - acked = true |
127 | | - cb(null, 'ACK ' + have.hash) |
128 | | - }) |
129 | | - }) |
130 | | - } |
131 | | - }, |
| 138 | + if (!gotWants) readWant(abort, cb) |
| 139 | + else readHave(abort, cb) |
| 140 | + } |
132 | 141 | ])), |
133 | 142 | |
134 | 143 | function havesDone(abort, cb) { |
135 | 144 | if (abort || aborted) return cb(abort || aborted) |
142 | 151 | var progress = progressObjects(options) |
143 | 152 | progress.setNumObjects(numObjects) |
144 | 153 | sendPack = pack.encode(options, numObjects, |
145 | 154 | progress(readObjects)) |
146 | | - havesDone(abort, cb) |
| 155 | + cb(null, '') |
147 | 156 | } |
148 | 157 | ) |
149 | 158 | } |
150 | 159 | ]) |