-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
executable file
·146 lines (120 loc) · 3.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
var os = require('os');
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var rimraf = require('rimraf');
function FSContext(options) {
this.readOnly = options.isReadOnly;
this.keyPrefix = options.keyPrefix;
}
function prefixKey(prefix, key) {
return path.join(prefix, key);
}
function _put(keyPrefix, key, value, callback) {
if(this.readOnly) {
return callback("Error: Write operation on readOnly context.");
}
var keyPath = prefixKey(keyPrefix, key);
fs.writeFile(keyPath, value, function(err) {
if(err) {
return callback("Error: unable to write to disk. Error was: " + err.stack);
}
callback();
});
}
FSContext.prototype.putObject = function(key, value, callback) {
var json = JSON.stringify(value);
_put(this.keyPrefix, key, json, callback);
};
FSContext.prototype.putBuffer = function(key, value, callback) {
_put(this.keyPrefix, key, value, callback);
};
FSContext.prototype.delete = function (key, callback) {
if(this.readOnly) {
return callback("Error: Write operation on readOnly context.");
}
var keyPath = prefixKey(this.keyPrefix, key);
fs.unlink(keyPath, function(err) {
if(err && err.code !== 'ENOENT') {
return callback("Error: unable to delete key from disk. Error was " + err);
}
callback();
});
};
FSContext.prototype.clear = function (callback) {
if(this.readOnly) {
return callback("Error: Write operation on readOnly context.");
}
var dir = this.keyPrefix;
// rm -fr <user/fs/dir/root>
rimraf(dir, function(err) {
if(err) {
return callback(err);
}
// Now create it again so we have an empty root for this user's fs
mkdirp(dir, callback);
});
};
function _get(keyPrefix, encoding, key, callback) {
var keyPath = prefixKey(keyPrefix, key);
fs.readFile(keyPath, {encoding: encoding}, function(err, data) {
if(err && err.code !== 'ENOENT') {
return callback("Error: unable to get key from disk. Error was " + err);
}
callback(null, data);
});
}
FSContext.prototype.getObject = function(key, callback) {
_get(this.keyPrefix, 'utf8', key, function(err, data) {
if(err) {
return callback(err);
}
if(data) {
try {
data = JSON.parse(data);
} catch(e) {
return callback(e);
}
}
callback(null, data);
});
};
FSContext.prototype.getBuffer = function(key, callback) {
_get(this.keyPrefix, /* leave as raw Buffer */ null, key, callback);
};
function FSProvider(options) {
this.name = options.name;
this.root = path.normalize(options.root || path.join(os.tmpDir(), 'filer-data'));
this.keyPrefix = options.keyPrefix;
}
FSProvider.isSupported = function() {
return (typeof module !== 'undefined' && module.exports);
};
FSProvider.prototype.open = function(callback) {
if(!this.keyPrefix) {
return callback("Error: Missing keyPrefix");
}
var that = this;
var dir = path.join(this.root, this.keyPrefix);
mkdirp(dir, function(err) {
if (err && err.code !== 'EEXIST') {
return callback(err);
}
that.keyPrefix = dir;
// Check for any nodes. We need a least a supernode
// or this will need to be formatted.
fs.readdir(dir, function(err, entries) {
if(err) {
return callback(err);
}
callback(null, entries.length === 0);
});
});
};
FSProvider.prototype.getReadOnlyContext = function() {
return new FSContext({isReadOnly: true, keyPrefix: this.keyPrefix});
};
FSProvider.prototype.getReadWriteContext = function() {
return new FSContext({isReadOnly: false, keyPrefix: this.keyPrefix});
};
module.exports = FSProvider;