Commit 3fb38e34e18d878a3e24a56e505e0bafb73f8211
meta
Dominic Tarr committed on 4/20/2016, 3:29:01 AMParent: a90889a879dea8da10d898d7714c795c81df85e0
Files changed
index.js | changed |
index.js | ||
---|---|---|
@@ -30,8 +30,36 @@ | ||
30 | 30 | function toArray (h) { |
31 | 31 | return Array.isArray(h) ? h : [h] |
32 | 32 | } |
33 | 33 | |
34 | +function single (fn) { | |
35 | + var waiting = {} | |
36 | + function async (key, cb) { | |
37 | + if(!waiting[key]) { | |
38 | + waiting[key] = [cb] | |
39 | + var cbs = waiting[key] | |
40 | + fn(key, function done (err, result) { | |
41 | + if(cbs.length) | |
42 | + delete waiting[key] | |
43 | + while(cbs.length) cbs.shift()(err, result) | |
44 | + }) | |
45 | + } | |
46 | + else | |
47 | + waiting[value].push(cb) | |
48 | + } | |
49 | + | |
50 | + //dump all the things that have been done already, | |
51 | + //when something has been added? | |
52 | + async.done = function (key, err, value) { | |
53 | + if(!waiting[key]) return | |
54 | + var cbs = waiting[key] | |
55 | + delete waiting[key] | |
56 | + while(cbs.length) cbs.shift()(err, result) | |
57 | + } | |
58 | + | |
59 | + return async | |
60 | +} | |
61 | + | |
34 | 62 | var Blobs = module.exports = function (config) { |
35 | 63 | var dir |
36 | 64 | if('string' === typeof config) |
37 | 65 | dir = config, config = {dir: dir} |
@@ -66,32 +94,45 @@ | ||
66 | 94 | if(tmp) return cb() |
67 | 95 | else waiting.push(cb) |
68 | 96 | } |
69 | 97 | |
98 | + var stat = single(fs.stat) | |
99 | + | |
70 | 100 | var tmpdir = path.join(dir, 'tmp') |
71 | 101 | |
72 | 102 | rimraf(tmpdir, function () { |
73 | 103 | mkdirp(tmpdir, function () { |
74 | 104 | tmp = true; while(waiting.length) waiting.shift()() |
75 | 105 | }) |
76 | 106 | }) |
77 | 107 | |
108 | + function toMeta(hash, stat) { | |
109 | + if(!stat) return null | |
110 | + return {id: hash, size: stat.size, ts: +stat.ctime} | |
111 | + } | |
112 | + | |
78 | 113 | function has (hash) { |
79 | 114 | return function (cb) { |
80 | - fs.stat(toPath(dir, hash), function (err, stat) { | |
115 | + stat(toPath(dir, hash), function (err, stat) { | |
81 | 116 | cb(null, !!stat) |
82 | 117 | }) |
83 | 118 | } |
84 | 119 | } |
85 | 120 | |
86 | 121 | function size (hash) { |
87 | 122 | return function (cb) { |
88 | - fs.stat(toPath(dir, hash), function (err, stat) { | |
123 | + stat(toPath(dir, hash), function (err, stat) { | |
89 | 124 | cb(null, stat ? stat.size : null) |
90 | 125 | }) |
91 | 126 | } |
92 | 127 | } |
93 | 128 | |
129 | + var meta = function (hash, cb) { | |
130 | + stat(toPath(dir, hash), function (err, stat) { | |
131 | + cb(err, toMeta(hash, stat)) | |
132 | + }) | |
133 | + } | |
134 | + | |
94 | 135 | function createTester (test) { |
95 | 136 | return function (hashes, cb) { |
96 | 137 | var n = !Array.isArray(hashes) |
97 | 138 | cont.para(toArray(hashes).map(test)) (function (_, ary) { |
@@ -107,9 +148,8 @@ | ||
107 | 148 | } |
108 | 149 | |
109 | 150 | var listeners = [] |
110 | 151 | |
111 | - | |
112 | 152 | return { |
113 | 153 | get: function (opts) { |
114 | 154 | if(isHash(opts)) |
115 | 155 | return Read(toPath(dir, opts)) |
@@ -120,9 +160,9 @@ | ||
120 | 160 | 'multiblob.get: {hash} is mandatory' |
121 | 161 | )) |
122 | 162 | |
123 | 163 | var stream = defer.source() |
124 | - fs.stat(toPath(dir, hash), function (err, stat) { | |
164 | + stat(toPath(dir, hash), function (err, stat) { | |
125 | 165 | if(opts.size != null && opts.size !== stat.size) |
126 | 166 | stream.abort(new Error('incorrect file length,' |
127 | 167 | + ' requested:' + opts.size + ' file was:' + stat.size |
128 | 168 | + ' for file:' + hash |
@@ -143,8 +183,9 @@ | ||
143 | 183 | |
144 | 184 | size: createTester(size), |
145 | 185 | |
146 | 186 | has: createTester(has), |
187 | + meta: meta, | |
147 | 188 | |
148 | 189 | add: function (hash, cb) { |
149 | 190 | if(!cb) cb = hash, hash = null |
150 | 191 | |
@@ -195,19 +236,16 @@ | ||
195 | 236 | |
196 | 237 | if(!isLive && !isOld) |
197 | 238 | throw new Error('ls with neither old or new is empty') |
198 | 239 | |
199 | - var long = (opts.size || opts.long) | |
200 | - | |
240 | + var long = (opts.size || opts.long || opts.meta) | |
201 | 241 | var old = pull( |
202 | 242 | glob(path.join(dir, '*', '*', '*')), |
203 | - long | |
204 | - ? paramap(function (filename, cb) { | |
205 | - fs.stat(filename, function (err, stat) { | |
206 | - cb(err, {id: toHash(filename), size: stat.size, ts: +stat.ctime}) | |
207 | - }) | |
208 | - }, 32) | |
209 | - : pull.map(toHash) | |
243 | + long ? paramap(function (filename, cb) { | |
244 | + stat(filename, function (err, stat) { | |
245 | + cb(err, toMeta(toHash(filename), stat)) | |
246 | + }) | |
247 | + }, 32) : pull.map(toHash) | |
210 | 248 | ) |
211 | 249 | |
212 | 250 | if(!isLive) return old |
213 | 251 | |
@@ -228,11 +266,4 @@ | ||
228 | 266 | } |
229 | 267 | } |
230 | 268 | } |
231 | 269 | |
232 | - | |
233 | - | |
234 | - | |
235 | - | |
236 | - | |
237 | - | |
238 | - |
Built with git-ssb-web