Files: ef1a2eaa163709173d29cc9f3fc446695e466fe7 / store.js
1695 bytesRaw
1 | var u = require('./util') |
2 | |
3 | module.exports = function (prefix, storage) { |
4 | return { |
5 | get: function (id, cb) { |
6 | var data = ( |
7 | storage[prefix+'_versions_'+id] || storage[prefix+'_version_'+id] |
8 | ) |
9 | if(data) |
10 | u.hash(data, function (err, _id) { |
11 | if(err) cb(err) |
12 | else if(_id !== id) cb(u.HashError(_id, id)) |
13 | else cb(null, data) |
14 | }) |
15 | else cb(new Error('not found:'+id)) |
16 | }, |
17 | |
18 | add: function (data, id, cb) { |
19 | if(!cb) cb = id, id = null |
20 | u.hash(data, function (err, _id) { |
21 | if(err) cb(err) |
22 | else if(id && _id !== id) cb(u.HashError(_id, id)) |
23 | else { |
24 | try { |
25 | storage[prefix+'_versions_'+_id] = u.toUtf8(data) |
26 | } |
27 | catch(err) { return cb(err) } //this will be quota error |
28 | cb(null, _id) |
29 | } |
30 | }) |
31 | }, |
32 | |
33 | has: function (id, cb) { |
34 | return cb(null, |
35 | !!storage[prefix+'_versions_'+id] || |
36 | !!storage[prefix+'_version_'+id] //legacy |
37 | ) |
38 | }, |
39 | |
40 | rm: function (id, cb) { |
41 | delete storage[prefix+'_versions_'+id] |
42 | |
43 | cb() |
44 | }, |
45 | |
46 | |
47 | ls: function (cb) { |
48 | var match = new RegExp('^'+prefix+'_versions_'), ary = [] |
49 | for(var key in storage) { |
50 | if(match.test(key)) { |
51 | var data = storage[key] |
52 | ary.push({ |
53 | id: key.replace(match, ''), |
54 | size: data && data.length || 0 |
55 | }) |
56 | } |
57 | } |
58 | cb(null, ary) |
59 | }, |
60 | |
61 | destroy: function (cb) { |
62 | var match = new RegExp('^'+prefix+'_versions_') |
63 | for(var key in storage) { |
64 | if(match.test(key)) { |
65 | delete storage[key] |
66 | } |
67 | } |
68 | |
69 | cb() |
70 | } |
71 | } |
72 | } |
73 | |
74 | |
75 | |
76 | |
77 | |
78 | |
79 | |
80 | |
81 | |
82 | |
83 | |
84 | |
85 | |
86 | |
87 | |
88 | |
89 |
Built with git-ssb-web