Skip to content
Browse files

API: Remove buffered file object (node.File)

With the addition of non-libeio stdio (17c6a67)
this class is no longer being used internally. It has proved buggy and isn't
full-featured enough to be very useful.  Since it's implemented entirely in
javascript it will be easy for someone to extra into their own library if
needed.
  • Loading branch information...
1 parent 8658999 commit 82cb1b5acb53000690448bf3d9b5d1f40ee66f76 @ry ry committed
Showing with 1 addition and 290 deletions.
  1. +0 −164 src/file.js
  2. +0 −29 test/mjsunit/test-file-open.js
  3. +0 −20 test/mjsunit/test-fs-file-read.js
  4. +1 −77 website/api.txt
View
164 src/file.js
@@ -39,167 +39,3 @@ node.fs.cat = function (path, encoding) {
});
return cat_promise;
};
-
-node.fs.File = function (options) {
- var self = this;
- self.__proto__ = new node.EventEmitter();
-
- options = options || {};
-
- if (options.encoding === "utf8") {
- self.encoding = node.UTF8;
- } else {
- self.encoding = node.RAW;
- }
- //node.debug("encoding: opts=" + options.encoding + " self=" + self.encoding);
- self.fd = typeof options.fd != 'undefined' ? options.fd : null;
-
- var actionQueue = [];
-
- // Adds a method to the queue.
- function createAction (method, args) {
- var promise = new node.Promise();
-
- promise.method = method;
- promise.args = args;
-
- //node.debug("add action " + method + " " + JSON.stringify(args));
-
- actionQueue.push(promise);
-
- // If the queue was empty, immediately call the method.
- if (actionQueue.length == 1) act();
-
- return promise;
- }
-
- function act () {
- var promise = actionQueue[0]; // peek at the head of the queue
- if (promise) {
- //node.debug("internal apply " + JSON.stringify(promise.args));
- internal_methods[promise.method].apply(self, promise.args);
- }
- }
-
- // called after each action finishes (when it returns from the thread pool)
- function success () {
- //node.debug("success called");
-
- var promise = actionQueue[0];
-
- if (!promise) throw "actionQueue empty when it shouldn't be.";
-
- var args = [];
- for (var i = 0; i < arguments.length; i++) {
- args.push(arguments[i]);
- }
-
- promise.emitSuccess(args);
-
- actionQueue.shift();
- act();
- }
-
- function error () {
- var promise = actionQueue[0];
-
- if (!promise) throw "actionQueue empty when it shouldn't be.";
-
- var args = [];
- for (var i = 0; i < arguments.length; i++) {
- args.push(arguments[i]);
- }
-
- promise.emitError(args);
- self.emitError(args);
- }
-
- var internal_methods = {
- open: function (path, mode) {
- var flags;
- switch (mode) {
- case "r":
- flags = node.O_RDONLY;
- break;
- case "r+":
- flags = node.O_RDWR;
- break;
- case "w":
- flags = node.O_CREAT | node.O_TRUNC | node.O_WRONLY;
- break;
- case "w+":
- flags = node.O_CREAT | node.O_TRUNC | node.O_RDWR;
- break;
- case "a":
- flags = node.O_APPEND | node.O_CREAT | node.O_WRONLY;
- break;
- case "a+":
- flags = node.O_APPEND | node.O_CREAT | node.O_RDWR;
- break;
- default:
- throw "Unknown mode";
- }
- // fix the mode here
- var promise = node.fs.open(path, flags, 0666);
-
- promise.addCallback(function (fd) {
- self.fd = fd;
- success(fd);
- });
-
- promise.addErrback(error);
- },
-
- close: function ( ) {
- var promise = node.fs.close(self.fd);
-
- promise.addCallback(function () {
- self.fd = null;
- success();
- });
-
- promise.addErrback(error);
- },
-
- read: function (length, position) {
- //node.debug("encoding: " + self.encoding);
- var promise = node.fs.read(self.fd, length, position, self.encoding);
- promise.addCallback(function (chunk) {
- success(chunk);
- });
- promise.addErrback(error);
- },
-
- write: function (data, position) {
- //node.debug("internal write");
- var promise = node.fs.write(self.fd, data, position);
- promise.addCallback(success);
- promise.addErrback(error);
- }
- };
-
- self.open = function (path, mode) {
- return createAction("open", [path, mode]);
- };
-
- self.close = function () {
- return createAction("close", []);
- };
-
- self.read = function (length, pos) {
- return createAction("read", [length, pos]);
- };
-
- self.write = function (buf, pos) {
- //node.debug("external write");
- return createAction("write", [buf, pos]);
- };
-
- self.print = function (data) {
- return self.write(data, null);
- };
-
- self.puts = function (data) {
- return self.write(data + "\n", null);
- };
-};
View
29 test/mjsunit/test-file-open.js
@@ -1,29 +0,0 @@
-include("mjsunit.js");
-
-var got_error = false;
-var opened = false;
-var closed = false;
-
-function onLoad () {
- var dirname = node.path.dirname(__filename);
- var fixtures = node.path.join(dirname, "fixtures");
- var x = node.path.join(fixtures, "x.txt");
-
- file = new node.fs.File;
- file.addListener("error", function () { got_error = true; });
-
- file.open(x, "r").addCallback(function () {
- opened = true;
- file.close().addCallback(function () {
- closed = true;
- });
- });
-
- puts("hey!");
-}
-
-function onExit () {
- assertFalse(got_error);
- assertTrue(opened);
- assertTrue(closed);
-}
View
20 test/mjsunit/test-fs-file-read.js
@@ -1,20 +0,0 @@
-include("mjsunit.js");
-
-var dirname = node.path.dirname(__filename);
-var fixtures = node.path.join(dirname, "fixtures");
-var x = node.path.join(fixtures, "x.txt");
-
-var contents = "";
-
-var f = new node.fs.File({ encoding: "utf8" });
-f.open(x, "r+");
-f.read(10000).addCallback(function (chunk) {
- contents = chunk;
- puts("got chunk: " + JSON.stringify(chunk));
-});
-f.close();
-
-
-function onExit () {
- assertEquals("xyz\n", contents);
-}
View
78 website/api.txt
@@ -403,12 +403,7 @@ the +node+ namespace (+node.SIGINT+, +node.SIGUSR1+, ...).
=== File I/O
-This part of the API is split into two parts: simple wrappers
-around standard POSIX file I/O functions and a user-friendly
-+File+ object.
-
-==== POSIX Wrappers
-
+File I/O is provided by simple wrappers around standard POSIX functions.
All POSIX wrappers have a similar form.
They return a promise (+node.Promise+). Example:
@@ -523,77 +518,6 @@ node.fs.cat("/etc/passwd", "utf8").addCallback(function (content) {
- on error: no parameters.
-==== +node.fs.File+
-
-A buffered file object.
-
-Internal request queues exist for each instance of +node.fs.File+ so that
-multiple commands can be issued at once. Thus the following is safe:
-
-----------------------------------------
-var file = new node.fs.File();
-file.open("/tmp/blah", "w+");
-file.write("hello");
-file.write("world");
-file.close();
-----------------------------------------
-
-Here is an example of reading the first 10 bytes from +/etc/passwd+ and
-outputting it to +stdout+.
-
-----------------------------------------
-var file = new node.fs.File({encoding: "utf8"});
-file.open("/etc/passwd", "r");
-file.read(10).addCallback(function (chunk) {
- puts(chunk);
-});
-file.close();
-----------------------------------------
-
-[cols="1,2,10",options="header"]
-|=========================================================
-|Event |Parameters | Notes
-|+"error"+ | | Emitted if an error happens.
-|=========================================================
-
-+new node.fs.File(options={})+::
-Creates a new file object.
-+
-The +options+ argument is optional. It can contain
-the following fields
-+
-- +fd+: a file descriptor for the file.
-- +encoding+: how +file.read()+ should return data. Either +"raw"+ or +"utf8"+.
-Defaults to +"raw"+.
-
-
-+file.open(path, mode)+::
-Opens the file at +path+.
-+
-+mode+ is a string:
-+
-- "r", open for reading and writing.
-- "r+", open for only reading.
-- "w", create a new file for reading and writing; if it already exists truncate it.
-- "w+", create a new file for writing only; if it already exists truncate it.
-- "a", create a new file for writing and reading. Writes append to the end of the file.
-- "a+"
-
-
-+file.read(length, position)+::
-Reads +length+ bytes from the file at +position+. +position+ can be omitted
-to write at the current file position.
-
-
-+file.write(data, position)+::
-Writes +data+ to the file. +position+ can be omitted to write at the current
-file position.
-
-
-+file.close()+::
-Closes the file.
-
-
=== HTTP
The HTTP interfaces in Node are designed to support many features

0 comments on commit 82cb1b5

Please sign in to comment.
Something went wrong with that request. Please try again.