fs.js 4.5 KB

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