-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (55 loc) · 1.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
module.exports = create_loose
var parse_loose = require('./parse')
, path = require('path')
, TYPE_MAP
TYPE_MAP = {
'commit': 1
, 'tree': 2
, 'blob': 3
, 'tag': 4
}
create_loose.accept = accept
function create_loose(entry, fs, find_oid, ready) {
var source = entry.path
, prefix_str = /\/([0-9a-fA-F]{2})/.exec(source)[1]
, prefix = parseInt(prefix_str, 16)
ready(null, {
readable: true
, writable: true
, read: loose_read
, write: loose_write
})
function loose_read(oid, ready) {
if(oid[0] !== prefix) {
return ready(null, undefined)
}
var rest = oid.slice(1).toString('hex')
, dir = path.join(source, rest)
fs.createReadStream(dir)
.on('error', function(err) { ready(null, undefined) })
.pipe(parse_loose())
.on('error', function(err) { ready(err) })
.on('data', function(obj) { ready(null, parse(obj)) })
}
function loose_write(oid, blob, ready) {
// TODO: implement this
}
}
function accept(path) {
return /objects\/([0-9a-fA-F]{2})$/.test(path)
}
function parse(buf) {
for(var i = 0, len = buf.length; i < len; ++i) {
if(buf.readUInt8(i) === 32) {
break
}
}
var stridx = i
, strtype = buf.slice(0, stridx).toString('utf8')
for(i += 1; i < len; ++i) {
if(buf.readUInt8(i) === 0) {
break
}
}
return {type: TYPE_MAP[strtype], data: buf.slice(i + 1)}
}