git ssb

0+

cel / pull-git-remote-helper



Tree: eead209ce126ffd087a7c49489717bf124bdda58

Files: eead209ce126ffd087a7c49489717bf124bdda58 / lib / pkt-line.js

3254 bytesRaw
1var buffered = require('pull-buffered')
2var util = require('./util')
3
4function rev(str) {
5 return str === '0000000000000000000000000000000000000000' ? null : str
6}
7
8// from pull-stream/source.js
9function abortCb(cb, abort, onAbort) {
10 cb(abort)
11 onAbort && onAbort(abort === true ? null: abort)
12 return
13}
14
15function pktLineEncode(read) {
16 var ended
17 return function (end, cb) {
18 if (ended) return cb(ended)
19 read(end, function (end, data) {
20 if (ended = end) {
21 cb(end)
22 } else {
23 if (data)
24 data += '\n'
25 else
26 data = ''
27 var len = data ? data.length + 4 : 0
28 var hexLen = ('000' + len.toString(16)).substr(-4)
29 var pkt = hexLen + data
30 // console.error('>', JSON.stringify(pkt))
31 cb(end, pkt)
32 }
33 })
34 }
35}
36
37function pktLineDecode(read, options) {
38 var b = buffered(read)
39 var readPrefix = b.chunks(4)
40 var ended
41
42 function readPackLine(abort, cb) {
43 if (ended) return cb(ended)
44 readPrefix(abort, function (end, buf) {
45 if (ended = end) return cb(end)
46 var len = parseInt(buf, 16)
47 if (!len)
48 return cb(null, new Buffer(''))
49 b.chunks(len - 4)(null, function (end, buf) {
50 if (ended = end) return cb(end)
51 cb(end, buf)
52 })
53 })
54 }
55
56 function readPackLineStr(abort, cb) {
57 if (ended) return cb(ended)
58 readPackLine(abort, function (end, buf) {
59 if (ended = end) return cb(end)
60 // trim newline
61 var len = buf.length
62 if (buf[len - 1] == 0xa)
63 len--
64 var line = buf.toString('ascii', 0, len)
65 cb(null, line)
66 })
67 }
68
69 function readPackLineStr1(abort, cb) {
70 if (ended) return cb(ended)
71 readPackLine(abort, function (end, buf) {
72 if (ended = end) return cb(end)
73 if (buf.length === 0) return cb(true)
74 cb(null, buf.toString('utf8'))
75 })
76 }
77
78 function readUpdate(abort, cb) {
79 readPackLine(abort, function (end, line) {
80 if (end) return cb(end)
81 if (!line.length) return cb(true)
82 var args = util.split3(line.toString('ascii'))
83 var args2 = util.split2(args[2], '\0')
84 var caps = args2[1]
85 if (caps) {
86 if (options.onCaps) {
87 var capsSet = {}
88 caps.trim().split(' ').forEach(function (cap) {
89 capsSet[cap] = true
90 })
91 options.onCaps(capsSet)
92 }
93 }
94 cb(null, {
95 old: rev(args[0]),
96 new: rev(args[1]),
97 name: args2[0]
98 })
99 })
100 }
101
102 function havesWants(onEnd) {
103 return function readWant(abort, cb) {
104 readPackLineStr(abort, function (end, line) {
105 if (end) return abortCb(cb, end, onEnd)
106 if (line === 'done') return abortCb(cb, true, onEnd)
107 if (line.length === 0) return cb(null, {type: 'flush-pkt'})
108 var args = util.split3(line)
109 var caps = args[2]
110 if (caps) {
111 if (options.onCaps)
112 options.onCaps(caps)
113 }
114 cb(null, {
115 type: args[0],
116 hash: args[1],
117 })
118 })
119 }
120 }
121
122 b.pktLines = readPackLine
123 b.pktLineStrs = readPackLineStr1
124 b.updates = readUpdate
125 b.wants = b.haves = havesWants
126
127 return b
128}
129
130exports.encode = pktLineEncode
131exports.decode = pktLineDecode
132

Built with git-ssb-web