Permalink
Browse files

Added promise, system, and fs-promise

  • Loading branch information...
kriszyp committed Jun 24, 2010
1 parent 58b772e commit 0c0792ab51e964e28234386712684e27d3cd04c1
Showing with 683 additions and 7 deletions.
  1. +4 −1 engines/rhino/lib/fs-promise.js
  2. +1 −1 lib/async.js
  3. +110 −0 lib/fs-promise.js
  4. +1 −1 lib/jsgi-client.js
  5. +1 −1 lib/jsgi-worker.js
  6. +2 −2 lib/lazy-array.js
  7. +543 −0 lib/promise.js
  8. +1 −1 lib/settings.js
  9. +20 −0 lib/system.js
@@ -1,4 +1,7 @@
var File = require("file");
exports.readFileSync = exports.read = File.read;
exports.writeFileSync = exports.write = File.write;
-exports.statSync = File.stat;
+exports.statSync = File.stat;
+
+exports.makeTree = File.mkdirs;
+exports.makeDirectory = File.mkdir;
View
@@ -31,7 +31,7 @@ sleepAndAdd4(10).addCallback(function(value){
*/
-var Promise = require("promise").Promise;
+var Promise = require("./promise").Promise;
// the async library
exports.async = function(generatorFunction){
return function(){
View
@@ -0,0 +1,110 @@
+/**
+* Node fs module that returns promises
+*/
+
+var fs = require("fs"),
+ LazyArray = require("./lazy-array").LazyArray,
+ Buffer = require("buffer").Buffer,
+ defer = require("./promise").defer;
+ convertNodeAsyncFunction = require("./promise").convertNodeAsyncFunction;
+
+// convert all the non-sync functions
+for (var i in fs) {
+ if (i.match(/Sync$/) || i.match(/watch/)) {
+ exports[i] = fs[i];
+ }
+ else{
+ exports[i] = convertNodeAsyncFunction(fs[i]);
+ }
+}
+function File(fd){
+ var file = new LazyArray({
+ some: function(callback){
+ var deferred = defer();
+ function readAndSend(){
+ var buffer = new Buffer(4096);
+ fs.read(fd, buffer, 0, 4096, null, function(err, bytesRead){
+ if(err){
+ deferred.reject(err);
+ return;
+ }
+ if (bytesRead === 0){
+ fs.close(fd);
+ deferred.resolve();
+ }
+ else {
+ var result;
+ if(bytesRead < 4096){
+ result = callback(buffer.slice(0, bytesRead));
+ }else{
+ result = callback(buffer);
+ }
+ if(result){
+ deferred.resolve();
+ }else{
+ readAndSend(fd);
+ }
+ }
+ });
+ }
+ readAndSend();
+ return deferred.promise;
+ },
+ length: 0
+ });
+ file.fd = fd;
+ file.write = function(contents){
+
+ }
+ return file;
+}
+File.prototype = LazyArray.prototype;
+
+var nodeRead = exports.read;
+exports.read = function(path, options){
+ if(path instanceof File){
+ arguments[0] = path.fd;
+ return nodeRead.apply(this, arguments);
+ }else{
+ return exports.readFileSync(path, options).toString((options && options.charset) || "utf8");
+ }
+};
+
+var nodeWrite = exports.write;
+exports.write = function(path, contents, options){
+ if(path instanceof File){
+ arguments[0] = path.fd;
+ return nodeWrite.apply(this, arguments);
+ }else{
+ return exports.writeFileSync(path, contents, options);
+ }
+};
+var nodeClose = exports.close;
+exports.close = function(path, contents, options){
+ if(path instanceof File){
+ arguments[0] = path.fd;
+ }
+ return nodeClose.apply(this, arguments);
+};
+
+
+nodeOpen = exports.open;
+exports.open = function(){
+ return nodeOpen.apply(this, arguments).then(File);
+};
+
+exports.makeDirectory = exports.mkdirSync;
+
+exports.makeTree = function(path){
+ var index = path.lastIndexOf('/');
+ if(index === -1){
+ return;
+ }
+ var path = path.substring(0, index);
+ try{
+ fs.statSync(path);
+ }catch(e){
+ exports.makeTree(path);
+ fs.mkdirSync(path, 0777);
+ }
+};
View
@@ -1,7 +1,7 @@
/**
* HTTP Client using the JSGI standard objects
*/
-var defer = require("promise").defer;
+var defer = require("commonjs-utils/promise").defer;
exports.request = function(request){
var xhr = new XMLHttpRequest();
View
@@ -14,7 +14,7 @@
*
*/
var observe = require("./observe").observe,
- defer = require("promise").defer;
+ defer = require("./promise").defer;
// Takes a JSGI 0.3 app.
// Messages are communicated between workers using the JSGI 0.3 object structure
// serialized in JSON format.
View
@@ -1,5 +1,5 @@
try{
- var when = require("promise").when;
+ var when = require("./promise").when;
}catch(e){
when = function(value, callback){
return callback(value);
@@ -44,7 +44,7 @@ function SomeWrapper(hasSomeAndLength){
}
this.totalCount = hasSomeAndLength.totalCount;
}
-SomeWrapper.prototype = [];
+exports.LazyArray.prototype = SomeWrapper.prototype = [];
SomeWrapper.prototype.some = function(callback){
this.source.some(callback);
}
Oops, something went wrong.

0 comments on commit 0c0792a

Please sign in to comment.