|
| 1 | +"use strict" |
| 2 | + |
| 3 | +function FSMock(store) { |
| 4 | + const self = this |
| 5 | + self.exists = (file, cb) => cb(null, !!store[file]) |
| 6 | + self.readFile = (file, cb) => store[file] ? cb(null, store[file]) : cb(new Error("ENOTFOUND: " + file)) |
| 7 | + self.writeFile = (file, data, cb) => cb(null, store[file] = data) |
| 8 | + self.unlink = (file, cb) => store[file] ? cb(null, delete store[file]) : cb(new Error("ENOTFOUND: " + file)) |
| 9 | +} |
| 10 | + |
| 11 | +module.exports = function ZeroNetStorageFS() { |
| 12 | + //store stuff in memory |
| 13 | + |
| 14 | + const self = this |
| 15 | + |
| 16 | + let json, file, fs, jfs |
| 17 | + |
| 18 | + const getPath = (a, b) => a + "/" + b |
| 19 | + |
| 20 | + self.file = { |
| 21 | + exists: (zite, version, inner_path, cb) => fs.exists(getPath(zite, inner_path), res => cb(null, res)), |
| 22 | + read: (zite, version, inner_path, cb) => fs.readFile(getPath(zite, inner_path), cb), |
| 23 | + write: (zite, version, inner_path, cb) => fs.readFile(getPath(zite, inner_path), cb), |
| 24 | + remove: (zite, version, inner_path, cb) => fs.unlink(getPath(zite, inner_path), cb) |
| 25 | + } |
| 26 | + |
| 27 | + self.json = { |
| 28 | + exists: (key, cb) => jfs.exists(getPath("json", key), res => cb(null, res)), |
| 29 | + read: (key, cb) => jfs.readFile(getPath("json", key), cb), |
| 30 | + write: (key, data, cb) => jfs.writeFile(getPath("json", key), data, cb), |
| 31 | + remove: (key, cb) => jfs.unlink(getPath("json", key), cb) |
| 32 | + } |
| 33 | + |
| 34 | + self.start = cb => { |
| 35 | + json = {} |
| 36 | + jfs = new FSMock(json) |
| 37 | + file = {} |
| 38 | + fs = new FSMock(json) |
| 39 | + cb() |
| 40 | + } |
| 41 | + |
| 42 | + self.stop = cb => { |
| 43 | + json = null |
| 44 | + jfs = null |
| 45 | + file = null |
| 46 | + fs = null |
| 47 | + cb() |
| 48 | + } |
| 49 | + |
| 50 | +} |
0 commit comments