MCPcopy
hub / github.com/creationix/js-git / encodePack

Function encodePack

lib/pack-codec.js:245–281  ·  view source on GitHub ↗
(emit)

Source from the content-addressed store, hash-verified

243
244exports.encodePack = encodePack;
245function encodePack(emit) {
246 var sha1sum = sha1();
247 var left;
248 return function (item) {
249 if (item === undefined) {
250 if (left !== 0) throw new Error("Some items were missing");
251 return emit();
252 }
253 if (typeof item.num === "number") {
254 if (left !== undefined) throw new Error("Header already sent");
255 left = item.num;
256 write(packHeader(item.num));
257 }
258 else if (typeof item.type === "string" && bodec.isBinary(item.body)) {
259 // The header must be sent before items.
260 if (typeof left !== "number") throw new Error("Headers not sent yet");
261
262 // Make sure we haven't sent all the items already
263 if (!left) throw new Error("All items already sent");
264
265 // Send the item in packstream format
266 write(packFrame(item));
267
268 // Send the checksum after the last item
269 if (!--left) {
270 emit(bodec.fromHex(sha1sum.digest()));
271 }
272 }
273 else {
274 throw new Error("Invalid item");
275 }
276 };
277 function write(chunk) {
278 sha1sum.update(chunk);
279 emit(chunk);
280 }
281}
282
283function packHeader(length) {
284 return bodec.fromArray([

Callers

nothing calls this directly

Calls 3

packHeaderFunction · 0.85
packFrameFunction · 0.85
writeFunction · 0.70

Tested by

no test coverage detected