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