Files: 4905ae9e843456de32f91555985ab19104e62aeb / test / pack.js
1745 bytesRaw
1 | var tape = require('tape') |
2 | var pack = require('../lib/pack') |
3 | var pull = require('pull-stream') |
4 | var repo = require('./repo') |
5 | var util = require('../lib/util') |
6 | |
7 | var objects = [ |
8 | {type: 'commit', object: repo.commit}, |
9 | {type: 'tree', object: repo.tree}, |
10 | {type: 'blob', object: repo.file} |
11 | ] |
12 | |
13 | function streamObject(read) { |
14 | var ended |
15 | return function readObject(abort, cb) { |
16 | read(abort, function (end, item) { |
17 | if (ended = end) return cb(end) |
18 | var data = item.object.data |
19 | cb(null, { |
20 | type: item.type, |
21 | length: data.length, |
22 | read: pull.once(data) |
23 | }) |
24 | }) |
25 | } |
26 | } |
27 | |
28 | function bufferObject(readObject) { |
29 | var ended |
30 | return function (abort, cb) { |
31 | readObject(abort, function next(end, object) { |
32 | if (ended = end) return cb(end) |
33 | var hasher = util.createGitObjectHash(object.type, object.length) |
34 | pull( |
35 | object.read, |
36 | hasher, |
37 | pull.collect(function (err, bufs) { |
38 | if (err) console.error(err) |
39 | // console.error('obj', type, length, JSON.stringify(buf.toString('ascii'))) |
40 | cb(err, { |
41 | type: object.type, |
42 | object: { |
43 | hash: hasher.digest('hex'), |
44 | data: Buffer.concat(bufs) |
45 | } |
46 | }) |
47 | }) |
48 | ) |
49 | }) |
50 | } |
51 | } |
52 | |
53 | tape('pack', function (t) { |
54 | var i = 0 |
55 | pull( |
56 | pull.values(objects), |
57 | streamObject, |
58 | pack.encode(objects.length), |
59 | pack.decode(function (err) { |
60 | t.error(err, 'decoded pack') |
61 | }), |
62 | bufferObject, |
63 | pull.drain(function (obj) { |
64 | if (i < objects.length) |
65 | t.deepEquals(obj, objects[i++]) |
66 | else |
67 | t.notOk(obj, 'unexpected object') |
68 | }, function (err) { |
69 | t.error(err, 'got objects') |
70 | t.end() |
71 | }) |
72 | ) |
73 | }) |
74 |
Built with git-ssb-web