-
Notifications
You must be signed in to change notification settings - Fork 37
/
fs.js
148 lines (140 loc) · 3.29 KB
/
fs.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
147
148
/**
* Node fs module that returns promises
*/
var fs = require("fs"),
LazyArray = require("./lazy-array").LazyArray,
Buffer = require("buffer").Buffer,
defer = require("./promise").defer,
when = require("./promise").when,
convertNodeAsyncFunction = require("./promise").convertNodeAsyncFunction;
// convert all the non-sync functions
for (var i in fs) {
if (i.match(/Sync$/) || i.match(/watch/)) {
exports[i] = fs[i];
}
else{
exports[i] = convertNodeAsyncFunction(fs[i]);
}
}
function File(fd){
var file = new LazyArray({
some: function(callback){
var deferred = defer();
function readAndSend(){
var buffer = new Buffer(4096);
if(fd.then){
fd.then(function(resolvedFd){
fd = resolvedFd;
fs.read(fd, buffer, 0, 4096, null, readResponse);
});
}else{
fs.read(fd, buffer, 0, 4096, null, readResponse);
}
function readResponse(err, bytesRead){
if(err){
deferred.reject(err);
return;
}
if (bytesRead === 0){
fs.close(fd);
deferred.resolve();
}
else {
var result;
if(bytesRead < 4096){
result = callback(buffer.slice(0, bytesRead));
}else{
result = callback(buffer);
}
if(result){
deferred.resolve();
}else{
readAndSend(fd);
}
}
}
}
readAndSend();
return deferred.promise;
},
length: 0
});
file.fd = fd;
file.then = function(callback, errback){
fd.then(function(){
callback(file);
}, errback);
};
file.write = function(contents, options){
return exports.write(file, contents, options);
}
file.close = function(){
return exports.close(file);
}
return file;
}
File.prototype = LazyArray.prototype;
var nodeRead = exports.read;
exports.read = function(path, options){
if(path instanceof File){
var args = arguments;
return when(path.fd, function(fd){
args[0] = fd;
return nodeRead.apply(this, args);
});
}else{
return exports.readFileSync(path, options).toString((options && options.charset) || "utf8");
}
};
var nodeWrite = exports.write;
exports.write = function(path, contents, options){
if(path instanceof File){
var args = arguments;
return when(path.fd, function(fd){
args[0] = fd;
return nodeWrite.apply(this, args);
});
}else{
return exports.writeFileSync(path, contents, options);
}
};
var nodeClose = exports.close;
exports.close = function(file){
if(file instanceof File){
var args = arguments;
return when(path.fd, function(fd){
args[0] = fd;
return nodeClose.apply(this, args);
});
}
throw new Error("Must be given a file descriptor");
};
var nodeOpenSync = exports.openSync;
exports.openSync = function(){
if(typeof mode == "string"){
arguments[1] = mode.replace(/b/,'');
}
return File(nodeOpenSync.apply(this, arguments));
};
nodeOpen = exports.open;
exports.open = function(path, mode){
if(typeof mode == "string"){
arguments[1] = mode.replace(/b/,'');
}
return File(nodeOpen.apply(this, arguments));
};
exports.makeDirectory = exports.mkdirSync;
exports.makeTree = function(path){
if(path.charAt(path.length-1) == '/') {
path = path.substring(0, path.length - 1);
}
try{
fs.statSync(path);
}catch(e){
var index = path.lastIndexOf('/');
if(index > -1){
exports.makeTree(path.substring(0, index));
}
fs.mkdirSync(path, 0777);
}
};