mirror of https://github.com/lukechilds/node.git
Browse Source
- Rename fs.cat to fs.readFile - Move file.write to fs.writeFile - Allow strings for the flag argument to fs.open ("r", "r+", "w", "w+", "a", "a+") - Remove the unused 'File' module / classv0.7.4-release
Tim Caswell
15 years ago
committed by
Ryan Dahl
11 changed files with 101 additions and 217 deletions
@ -1,156 +1 @@ |
|||||
var fs = require("./fs"); |
throw new Error("The 'file' module has been removed. 'file.read' is now 'fs.readFile', and 'file.write' is now 'fs.writeFile'."); |
||||
var events = require('events'); |
|
||||
/*jslint onevar: true, undef: true, eqeqeq: true, plusplus: true, regexp: true, newcap: true, immed: true */ |
|
||||
/*globals exports, node, __filename */ |
|
||||
|
|
||||
exports.debugLevel = 0; // Increase to get more verbose debug output
|
|
||||
|
|
||||
function debugMessage (msg) { |
|
||||
if (exports.debugLevel > 0) { |
|
||||
process.error(__filename + ": " + msg.toString()); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
function debugObject (obj) { |
|
||||
if (exports.debugLevel > 0) { |
|
||||
process.error(__filename + ": " + JSON.stringify(obj)); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
exports.read = fs.cat; |
|
||||
|
|
||||
exports.write = function (filename, data, encoding) { |
|
||||
var promise = new events.Promise(); |
|
||||
|
|
||||
encoding = encoding || "utf8"; // default to utf8
|
|
||||
|
|
||||
fs.open(filename, process.O_WRONLY | process.O_TRUNC | process.O_CREAT, 0666) |
|
||||
.addCallback(function (fd) { |
|
||||
function doWrite (_data) { |
|
||||
fs.write(fd, _data, 0, encoding) |
|
||||
.addErrback(function () { |
|
||||
fs.close(fd); |
|
||||
promise.emitError(); |
|
||||
}) |
|
||||
.addCallback(function (written) { |
|
||||
if (written === _data.length) { |
|
||||
fs.close(fd); |
|
||||
promise.emitSuccess(); |
|
||||
} else { |
|
||||
doWrite(_data.slice(written)); |
|
||||
} |
|
||||
}); |
|
||||
} |
|
||||
doWrite(data); |
|
||||
}) |
|
||||
.addErrback(function () { |
|
||||
promise.emitError(); |
|
||||
}); |
|
||||
|
|
||||
return promise; |
|
||||
}; |
|
||||
|
|
||||
exports.File = function (filename, mode, options) { |
|
||||
var self = this; |
|
||||
|
|
||||
options = options || {}; |
|
||||
self.encoding = options.encoding || "utf8"; |
|
||||
|
|
||||
self.filename = filename; |
|
||||
|
|
||||
self.actionQueue = []; |
|
||||
self.currentAction = null; |
|
||||
|
|
||||
switch (mode) { |
|
||||
case "r": |
|
||||
self.flags = process.O_RDONLY; |
|
||||
break; |
|
||||
|
|
||||
case "r+": |
|
||||
self.flags = process.O_RDWR; |
|
||||
break; |
|
||||
|
|
||||
case "w": |
|
||||
self.flags = process.O_CREAT | process.O_TRUNC | process.O_WRONLY; |
|
||||
break; |
|
||||
|
|
||||
case "w+": |
|
||||
self.flags = process.O_CREAT | process.O_TRUNC | process.O_RDWR; |
|
||||
break; |
|
||||
|
|
||||
case "a": |
|
||||
self.flags = process.O_APPEND | process.O_CREAT | process.O_WRONLY; |
|
||||
break; |
|
||||
|
|
||||
case "a+": |
|
||||
self.flags = process.O_APPEND | process.O_CREAT | process.O_RDWR; |
|
||||
break; |
|
||||
|
|
||||
default: |
|
||||
throw new Error("Unknown mode"); |
|
||||
} |
|
||||
|
|
||||
self.open(self.filename, self.flags, 0666).addCallback(function (fd) { |
|
||||
debugMessage(self.filename + " opened. fd = " + fd); |
|
||||
self.fd = fd; |
|
||||
}).addErrback(function () { |
|
||||
self.emit("error", ["open"]); |
|
||||
}); |
|
||||
}; |
|
||||
|
|
||||
var proto = exports.File.prototype; |
|
||||
|
|
||||
proto._maybeDispatch = function () { |
|
||||
var self, args, method, promise, userPromise; |
|
||||
|
|
||||
self = this; |
|
||||
|
|
||||
if (self.currentAction) { return; } |
|
||||
self.currentAction = self.actionQueue.shift(); |
|
||||
if (!self.currentAction) { return; } |
|
||||
|
|
||||
debugObject(self.currentAction); |
|
||||
|
|
||||
args = self.currentAction.args || []; |
|
||||
method = self.currentAction.method; |
|
||||
|
|
||||
|
|
||||
if (method !== "open") { |
|
||||
args.unshift(self.fd); |
|
||||
} |
|
||||
|
|
||||
if (!args[3] && (method === "read" || method === "write")) { |
|
||||
args[3] = self.encoding; |
|
||||
} |
|
||||
promise = fs[method].apply(self, args); |
|
||||
|
|
||||
userPromise = self.currentAction.promise; |
|
||||
|
|
||||
promise.addCallback(function () { |
|
||||
process.assert(self.currentAction.promise === userPromise); |
|
||||
userPromise.emitSuccess.apply(userPromise, arguments); |
|
||||
self.currentAction = null; |
|
||||
self._maybeDispatch(); |
|
||||
}).addErrback(function () { |
|
||||
debugMessage("Error in method " + method); |
|
||||
process.assert(self.currentAction.promise === userPromise); |
|
||||
userPromise.emitError.apply(userPromise, arguments); |
|
||||
self.currentAction = null; |
|
||||
self._maybeDispatch(); |
|
||||
}); |
|
||||
}; |
|
||||
|
|
||||
proto._queueAction = function (method, args) { |
|
||||
var userPromise = new events.Promise(); |
|
||||
this.actionQueue.push({ method: method, args: args, promise: userPromise }); |
|
||||
this._maybeDispatch(); |
|
||||
return userPromise; |
|
||||
}; |
|
||||
|
|
||||
|
|
||||
(["open", "write", "read", "close"]).forEach(function (name) { |
|
||||
proto[name] = function () { |
|
||||
return this._queueAction(name, Array.prototype.slice.call(arguments, 0)); |
|
||||
}; |
|
||||
}); |
|
||||
|
|
||||
|
@ -1,38 +0,0 @@ |
|||||
process.mixin(require("./common")); |
|
||||
|
|
||||
var testTxt = path.join(fixturesDir, "test.txt"); |
|
||||
|
|
||||
var libDir = path.join(testDir, "../../lib"); |
|
||||
require.paths.unshift(libDir); |
|
||||
process.mixin(require("file")); |
|
||||
|
|
||||
var fileUnlinked = false; |
|
||||
|
|
||||
var file = new File(testTxt, "w+"); |
|
||||
file.write("hello\n"); |
|
||||
file.write("world\n"); |
|
||||
setTimeout(function () { |
|
||||
file.write("hello\n"); |
|
||||
file.write("world\n"); |
|
||||
file.close().addCallback(function () { |
|
||||
error("file closed..."); |
|
||||
var out = fs.cat(testTxt).wait(); |
|
||||
print("the file contains: "); |
|
||||
p(out); |
|
||||
assert.equal("hello\nworld\nhello\nworld\n", out); |
|
||||
var file2 = new File(testTxt, "r"); |
|
||||
file2.read(5).addCallback(function (data) { |
|
||||
puts("read(5): " + JSON.stringify(data)); |
|
||||
assert.equal("hello", data); |
|
||||
fs.unlink(testTxt).addCallback(function () { |
|
||||
fileUnlinked = true; |
|
||||
}); |
|
||||
}); |
|
||||
file2.close(); |
|
||||
}); |
|
||||
}, 10); |
|
||||
|
|
||||
process.addListener("exit", function () { |
|
||||
assert.equal(true, fileUnlinked); |
|
||||
puts("done"); |
|
||||
}); |
|
Loading…
Reference in new issue