Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
tnantoka
committed
Sep 24, 2011
0 parents
commit 8597de0
Showing
6 changed files
with
333 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Mac OS X automatic create files | ||
.DS_Store | ||
|
||
# Vim automatic create files | ||
*~ | ||
.*.sw* | ||
|
||
# Static, Temporary, Content files | ||
sessions | ||
sessions2 | ||
|
||
# npm modules | ||
node_modules | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
0.0.1 / 2011-09-25 | ||
================== | ||
|
||
* Publish |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
|
||
# Connect FS | ||
|
||
connect-fs is a FileSystem session store, just copied connect-redis. | ||
|
||
connect-fs support only connect `>= 1.4.0`. | ||
|
||
## Installation | ||
|
||
$ npm install connect-fs | ||
|
||
## Options | ||
|
||
- `dir='.'` Direcotry to save session files | ||
|
||
## Usage | ||
|
||
var connect = require('connect') | ||
, FSStore = require('connect-fs')(connect); | ||
|
||
connect.createServer( | ||
connect.cookieParser(), | ||
connect.session({ store: new FSStore, secret: 'your secret' }) | ||
); | ||
|
||
with express | ||
|
||
var FSStore = require('connect-fs')(express); | ||
|
||
app.configure(function() { | ||
app.set('views', __dirname + '/views'); | ||
app.set('view engine', 'ejs'); | ||
app.use(express.bodyParser()); | ||
app.use(express.methodOverride()); | ||
app.use(express.cookieParser()); | ||
app.use(express.session({ | ||
store: new FSStore, | ||
secret: 'your secret', | ||
cookie: { maxAge: 7 * 24 * 60 * 60 * 1000 } // 1 week | ||
})); | ||
app.use(app.router); | ||
app.use(express.static(__dirname + '/public')); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
/*! | ||
* Connect - FileSystem | ||
* Copyright(c) 2011 tnantoka <bornneet@livedoor.com> | ||
* MIT Licensed | ||
* forked from https://github.com/visionmedia/connect-redis | ||
*/ | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var fs = require('fs'); | ||
var path = require('path'); | ||
var events = require('events'); | ||
|
||
/** | ||
* One day in seconds. | ||
*/ | ||
|
||
var oneDay = 86400; | ||
|
||
/** | ||
* Return the `FSStore` extending `connect`'s session Store. | ||
* | ||
* @param {object} connect | ||
* @return {Function} | ||
* @api public | ||
*/ | ||
|
||
module.exports = function(connect){ | ||
|
||
/** | ||
* Connect's Store. | ||
*/ | ||
|
||
var Store = connect.session.Store; | ||
|
||
/** | ||
* Initialize FSStore with the given `options`. | ||
* | ||
* @param {Object} options | ||
* @api public | ||
*/ | ||
|
||
function FSStore(options) { | ||
options = options || {}; | ||
Store.call(this, options); | ||
|
||
this.client = new events.EventEmitter(); | ||
var self = this; | ||
|
||
this.dir = options.dir || './sessions'; | ||
|
||
fs.stat(this.dir, function(err, stats) { | ||
if (err && err.errno != 2) throw err; | ||
if (stats && stats.isDirectory()) { | ||
self.client.emit('connect'); | ||
} else { | ||
fs.mkdir(self.dir, 0755, function(err) { | ||
if (err) throw err; | ||
self.client.emit('connect'); | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Inherit from `Store`. | ||
*/ | ||
|
||
FSStore.prototype.__proto__ = Store.prototype; | ||
|
||
/** | ||
* Attempt to fetch session by the given `sid`. | ||
* | ||
* @param {String} sid | ||
* @param {Function} fn | ||
* @api public | ||
*/ | ||
|
||
FSStore.prototype.get = function(sid, fn){ | ||
var now = new Date().getTime(); | ||
fs.readFile(path.join(this.dir, sid + '.json'), function(err, data) { | ||
if (err) fn(err); | ||
// AssesionError occurs !? | ||
//try { | ||
if (!data) { | ||
return fn(); | ||
} | ||
data = JSON.parse(data); | ||
if (data.expired < now) { | ||
return fn(); | ||
} else { | ||
delete data.expired; | ||
fn(null, data); | ||
} | ||
//} catch (e) { | ||
// fn(e); | ||
//} | ||
} | ||
); | ||
}; | ||
|
||
|
||
/** | ||
* Commit the given `sess` object associated with the given `sid`. | ||
* | ||
* @param {String} sid | ||
* @param {Session} sess | ||
* @param {Function} fn | ||
* @api public | ||
*/ | ||
|
||
FSStore.prototype.set = function(sid, sess, fn) { | ||
try { | ||
var maxAge = sess.cookie.maxAge; | ||
var now = new Date().getTime(); | ||
var expired = maxAge ? now + maxAge : now + oneDay; | ||
sess.expired = expired; | ||
sess = JSON.stringify(sess); | ||
|
||
fs.writeFile(path.join(this.dir, sid + '.json'), sess, function(err) { | ||
// if (fn) fn.apply(this, arguments); | ||
if (fn) { | ||
if (err) fn(err); | ||
fn(null, true); | ||
} | ||
}); | ||
} catch (e) { | ||
if (fn) fn(e); | ||
} | ||
}; | ||
|
||
|
||
/** | ||
* Destroy the session associated with the given `sid`. | ||
* | ||
* @param {String} sid | ||
* @api public | ||
*/ | ||
|
||
FSStore.prototype.destroy = function(sid, fn){ | ||
fs.unlink(path.join(this.dir, sid + '.json'), fn); | ||
}; | ||
|
||
|
||
/** | ||
* Fetch number of sessions. | ||
* | ||
* @param {Function} fn | ||
* @api public | ||
*/ | ||
|
||
FSStore.prototype.length = function(fn){ | ||
fs.readdir(this.dir, function(err, files) { | ||
if (err) fn(err); | ||
var length = 0; | ||
for (var i = 0; i < files.length; i++) { | ||
if (/\.json$/.test(files[i])) { | ||
length++; | ||
} | ||
} | ||
fn(null, length); | ||
}); | ||
}; | ||
|
||
|
||
/** | ||
* Clear all sessions. | ||
* | ||
* @param {Function} fn | ||
* @api public | ||
*/ | ||
|
||
FSStore.prototype.clear = function(fn){ | ||
var self = this; | ||
var count = 0; | ||
this.length(function(err, length) { | ||
fs.readdir(self.dir, function(err, files) { | ||
if (err) fn(err); | ||
for (var i = 0; i < files.length; i++) { | ||
if (/\.json$/.test(files[i])) { | ||
fs.unlink(path.join(self.dir, files[i]), function(err) { | ||
if (err) fn(err); | ||
if (++count == length) fn(null, true); | ||
}); | ||
} | ||
} | ||
}); | ||
}); | ||
}; | ||
|
||
return FSStore; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "connect-fs", | ||
"description": "FileSystem session store for Connect", | ||
"version": "0.0.1", | ||
"author": "tnantoka <bornneet@livedoor.com>", | ||
"main": "lib/connect-fs", | ||
"dependencies": { | ||
}, | ||
"devDependencies": { | ||
"connect": "1.4.x" | ||
}, | ||
"engines": { | ||
"node": ">= 0.1.98" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var assert = require('assert') | ||
, connect = require('connect') | ||
, FSStore = require('./lib/connect-fs.js')(connect); | ||
|
||
var store = new FSStore; | ||
var store_alt = new FSStore({ dir: 'sessions2' }); | ||
|
||
store.client.on('connect', function(){ | ||
// #set() | ||
store.set('123', { cookie: { maxAge: 2000 }, name: 'tnantoka' }, function(err, ok){ | ||
assert.ok(!err, '#set() got an error'); | ||
assert.ok(ok, '#set() is not ok'); | ||
|
||
// #get() | ||
store.get('123', function(err, data){ | ||
assert.ok(!err, '#get() got an error'); | ||
assert.deepEqual({ cookie: { maxAge: 2000 }, name: 'tnantoka' }, data); | ||
|
||
// #length() | ||
store.length(function(err, len){ | ||
assert.ok(!err, '#length() got an error'); | ||
assert.equal(1, len, '#length() with keys'); | ||
|
||
// #db option | ||
store_alt.length(function (err, len) { | ||
assert.ok(!err, '#alt db got an error'); | ||
assert.equal(0, len, '#alt db with keys'); | ||
|
||
// #clear() | ||
store.clear(function(err, ok){ | ||
assert.ok(!err, '#clear()'); | ||
assert.ok(ok, '#clear()'); | ||
|
||
// #length() | ||
store.length(function(err, len){ | ||
assert.ok(!err, '#length()'); | ||
assert.equal(0, len, '#length() without keys'); | ||
|
||
// #set null | ||
store.set('123', { cookie: { maxAge: 2000 }, name: 'tnantoka' }, function(){ | ||
store.destroy('123', function(){ | ||
store.length(function(err, len){ | ||
assert.equal(0, len, '#set() null'); | ||
console.log('done'); | ||
// store.client.end(); | ||
// store_alt.client.end(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}) | ||
}); | ||
}); | ||
|