/**
 * Module dependencies.
 */

var fs = require('fs')
  , path = require('path')
  , crypto = require('crypto');

/**
 * MD5 hash
 * 
 * @param {String} s
 * @param {String} encoding, optional, default is hex
 * @return depend on encoding
 * @api private
 */
function md5(s, encoding) {
	var h = crypto.createHash('md5');
	h.update(s);
	return h.digest(encoding || 'hex');
};

var FileStore = module.exports = function FileStore(filedir) {
    this.filedir = filedir;
    if(!path.existsSync(filedir)) {
        fs.mkdirSync(filedir, '755');
    }
};

FileStore.prototype.get = function(key, callback) {
    var p = path.join(this.filedir, md5(key));
    fs.readFile(p, function (err, data) {
        if(err) {
            callback();
        } else {
            try {
                callback(null, JSON.parse(data));
            } catch(e) {
                callback();
            }
        }
    });
};

FileStore.prototype.set = function(key, value, callback) {
    var p = path.join(this.filedir, md5(key));
    fs.writeFile(p, JSON.stringify(value), function (err) {
        callback && callback(err);
    });
};

FileStore.prototype.destroy = function(key, callback) {
    var p = path.join(this.filedir, md5(key));
    fs.unlink(path, function(err) {
        callback && callback();
    });
};

FileStore.prototype.length = function(callback) {
    fs.readdir(this.filedir, function(err, files) {
        callback(null, files ? files.length : 0);
    });
};

FileStore.prototype.clear = function(callback) {
    fs.readdir(this.filedir, function(err, files) {
        var count = 0;
        if(files) {
            for(var i = 0, len = files.length; i < len; i++) {
                var p = path.join(this.filedir, files[i]);
                rs.unlinkSync(p);
            }
        }
        callback && callback();
    });
};

@@ -543,24 +543,346 @@
 ;%0A    %7D%0A%7D;%0A%0A
+try %7B%0A%09/**%0A%09 * Support for connect.session: connect %3E 1.4.0 must need this.%0A%09 * %0A%09 * Inherit from %60Store.prototype%60.%0A%09 */%0A%09var Store = require('connect').session.Store;%0A%09FileStore.prototype.__proto__ = Store.prototype;%0A%7D catch(e) %7B%0A%09console.warning('Would not support for connect.session if you dont install connect');%0A%7D%0A%0A
 FileStore.pr
@@ -2132,29 +2132,28 @@
 ck && callback();%0A    %7D);%0A%7D;
-%0A

support connect 1.4.0 session