-
Notifications
You must be signed in to change notification settings - Fork 35
/
fs.js
194 lines (184 loc) · 4.42 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* Node fs module that returns promises
*/
if (typeof java === "object"){
var fs = require("./engines/rhino/fs");
// for rhino
for(var i in fs){
exports[i] = fs[i];
}
}
else{
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 that have a sync counterpart
for (var i in fs) {
if ((i + 'Sync') in fs) {
// async
if (fs[i]) {
exports[i] = convertNodeAsyncFunction(fs[i], i === "readFile");
}
}
else{
// sync, or something that we can't auto-convert
exports[i] = 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, function (err) {
if(err){
deferred.reject(err);
return;
}
deferred.resolve();
});
}
else {
var result;
if(bytesRead < 4096){
result = callback(buffer.slice(0, bytesRead));
}else{
result = callback(buffer);
}
if(result){
// if a promise is returned, we wait for it be fulfilled, allows for back-pressure indication
if(result.then){
result.then(function(result){
if(result){
deferred.resolve();
}
else{
readAndSend(fd);
}
}, deferred.reject);
}
else{
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, encoding){
return exports.write(file, contents, options, encoding);
}
file.close = function(){
return exports.close(file);
}
file.writeSync = function(contents, options, encoding){
return exports.writeSync(file.fd, contents, options, encoding);
}
file.closeSync = function(){
return exports.closeSync(file.fd);
}
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, encoding){
if(path instanceof File){
var id = Math.random();
var args = arguments;
return when(path.fd, function(fd){
args[0] = fd;
if(typeof contents == "string"){
return nodeWrite(fd, contents, options, encoding);
}
return nodeWrite(fd, contents, 0, contents.length, null);
});
}else{
return exports.writeFileSync(path, contents, options);
}
};
var nodeClose = exports.close;
exports.close = function(file){
if(file instanceof File){
var args = arguments;
return when(file.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);
}
};
exports.absolute = exports.realpathSync;
exports.list = exports.readdirSync;
exports.move = exports.rename;
}