fs.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**
  2. * Node fs module that returns promises
  3. */
  4. if (typeof java === "object"){
  5. var fs = require("./engines/rhino/fs");
  6. // for rhino
  7. for(var i in fs){
  8. exports[i] = fs[i];
  9. }
  10. }
  11. else{
  12. var fs = require("fs"),
  13. LazyArray = require("./lazy-array").LazyArray,
  14. Buffer = require("buffer").Buffer,
  15. defer = require("./promise").defer,
  16. when = require("./promise").when,
  17. convertNodeAsyncFunction = require("./promise").convertNodeAsyncFunction;
  18. // convert all the non-sync functions that have a sync counterpart
  19. for (var i in fs) {
  20. if ((i + 'Sync') in fs) {
  21. // async
  22. if (fs[i]) {
  23. exports[i] = convertNodeAsyncFunction(fs[i], i === "readFile");
  24. }
  25. }
  26. else{
  27. // sync, or something that we can't auto-convert
  28. exports[i] = fs[i];
  29. }
  30. }
  31. function File(fd){
  32. var file = new LazyArray({
  33. some: function(callback){
  34. var deferred = defer();
  35. function readAndSend(){
  36. var buffer = new Buffer(4096);
  37. if(fd.then){
  38. fd.then(function(resolvedFd){
  39. fd = resolvedFd;
  40. fs.read(fd, buffer, 0, 4096, null, readResponse);
  41. });
  42. }else{
  43. fs.read(fd, buffer, 0, 4096, null, readResponse);
  44. }
  45. function readResponse(err, bytesRead){
  46. if(err){
  47. deferred.reject(err);
  48. return;
  49. }
  50. if (bytesRead === 0){
  51. fs.close(fd, function (err) {
  52. if(err){
  53. deferred.reject(err);
  54. return;
  55. }
  56. deferred.resolve();
  57. });
  58. }
  59. else {
  60. var result;
  61. if(bytesRead < 4096){
  62. result = callback(buffer.slice(0, bytesRead));
  63. }else{
  64. result = callback(buffer);
  65. }
  66. if(result){
  67. // if a promise is returned, we wait for it be fulfilled, allows for back-pressure indication
  68. if(result.then){
  69. result.then(function(result){
  70. if(result){
  71. deferred.resolve();
  72. }
  73. else{
  74. readAndSend(fd);
  75. }
  76. }, deferred.reject);
  77. }
  78. else{
  79. deferred.resolve();
  80. }
  81. }else{
  82. readAndSend(fd);
  83. }
  84. }
  85. }
  86. }
  87. readAndSend();
  88. return deferred.promise;
  89. },
  90. length: 0
  91. });
  92. file.fd = fd;
  93. file.then = function(callback, errback){
  94. fd.then(function(){
  95. callback(file);
  96. }, errback);
  97. };
  98. file.write = function(contents, options, encoding){
  99. return exports.write(file, contents, options, encoding);
  100. }
  101. file.close = function(){
  102. return exports.close(file);
  103. }
  104. file.writeSync = function(contents, options, encoding){
  105. return exports.writeSync(file.fd, contents, options, encoding);
  106. }
  107. file.closeSync = function(){
  108. return exports.closeSync(file.fd);
  109. }
  110. return file;
  111. }
  112. File.prototype = LazyArray.prototype;
  113. var nodeRead = exports.read;
  114. exports.read = function(path, options){
  115. if(path instanceof File){
  116. var args = arguments;
  117. return when(path.fd, function(fd){
  118. args[0] = fd;
  119. return nodeRead.apply(this, args);
  120. });
  121. }else{
  122. return exports.readFileSync(path, options).toString((options && options.charset) || "utf8");
  123. }
  124. };
  125. var nodeWrite = exports.write;
  126. exports.write = function(path, contents, options, encoding){
  127. if(path instanceof File){
  128. var id = Math.random();
  129. var args = arguments;
  130. return when(path.fd, function(fd){
  131. args[0] = fd;
  132. if(typeof contents == "string"){
  133. return nodeWrite(fd, contents, options, encoding);
  134. }
  135. return nodeWrite(fd, contents, 0, contents.length, null);
  136. });
  137. }else{
  138. return exports.writeFileSync(path, contents, options);
  139. }
  140. };
  141. var nodeClose = exports.close;
  142. exports.close = function(file){
  143. if(file instanceof File){
  144. var args = arguments;
  145. return when(file.fd, function(fd){
  146. args[0] = fd;
  147. return nodeClose.apply(this, args);
  148. });
  149. }
  150. throw new Error("Must be given a file descriptor");
  151. };
  152. var nodeOpenSync = exports.openSync;
  153. exports.openSync = function(){
  154. if(typeof mode == "string"){
  155. arguments[1] = mode.replace(/b/,'');
  156. }
  157. return File(nodeOpenSync.apply(this, arguments));
  158. };
  159. nodeOpen = exports.open;
  160. exports.open = function(path, mode){
  161. if(typeof mode == "string"){
  162. arguments[1] = mode.replace(/b/,'');
  163. }
  164. return File(nodeOpen.apply(this, arguments));
  165. };
  166. exports.makeDirectory = exports.mkdirSync;
  167. exports.makeTree = function(path){
  168. if(path.charAt(path.length-1) == '/') {
  169. path = path.substring(0, path.length - 1);
  170. }
  171. try{
  172. fs.statSync(path);
  173. }catch(e){
  174. var index = path.lastIndexOf('/');
  175. if(index > -1){
  176. exports.makeTree(path.substring(0, index));
  177. }
  178. fs.mkdirSync(path, 0777);
  179. }
  180. };
  181. exports.absolute = exports.realpathSync;
  182. exports.list = exports.readdirSync;
  183. exports.move = exports.rename;
  184. }