Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Carson committed Aug 9, 2013
1 parent 19c4f58 commit b23b2d2
Show file tree
Hide file tree
Showing 8 changed files with 309 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -12,3 +12,5 @@ logs
results

npm-debug.log
.DS_Store
node_modules
4 changes: 4 additions & 0 deletions History.md
@@ -0,0 +1,4 @@
0.0.1 / 2013-08-10
==================

* Initial commit
7 changes: 7 additions & 0 deletions Makefile
@@ -0,0 +1,7 @@

index.html: lib/connect-firebase.js
dox \
--title "Connect Firebase" \
--desc "Firebase session store for connect backed by the [firebase sdk](https://www.firebase.com/docs/nodejs-quickstart.html)." \
--ribbon "http://github.com/ca98am79/connect-firebase" \
$< > $@
54 changes: 51 additions & 3 deletions README.md
@@ -1,4 +1,52 @@
connect-firebase
================
# Connect Firebase

Firebase session store for Connect
connect-firebase is a Firebase session store backed by the [firebase sdk](https://www.firebase.com/docs/nodejs-quickstart.html)

## Installation

$ npm install connect-firebase

## Options

- `firebase` An existing Firebase to store sessions

## Usage

var connect = require('connect'),
FirebaseStore = require('connect-firebase')(connect);
connect()
.use(connect.cookieParser())
.use(connect.session({ store: new FirebaseStore({firebase: 'connect-sessions.firebaseIO-demo.com'}), secret: 'keyboard cat'}))

Or with [express](http://expressjs.com/)

FirebaseStore = require('connect-firebase')(express);
var app = express(
express.cookieParser(),
express.session({ store: new FirebaseStore({firebase: 'connect-sessions.firebaseIO-demo.com'}), secret: 'keyboard cat'})
);

## LICENSE - "MIT License"

Copyright (c) 2013 Mike Carson, http://ca98am79.com/

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
2 changes: 2 additions & 0 deletions index.js
@@ -0,0 +1,2 @@

module.exports = require('./lib/connect-firebase');
122 changes: 122 additions & 0 deletions lib/connect-firebase.js
@@ -0,0 +1,122 @@
/*!
* Connect - Firebase
* Copyright(c) 2013 Mike Carson <ca98am79@gmail.com>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var Firebase = require('firebase');

/**
* One day in milliseconds.
*/

var oneDayInMilliseconds = 86400000;

/**
* Return the `FirebaseStore` 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 FirebaseStore with the given `options`.
*
* @param {Object} options
* @api public
*/

function FirebaseStore(options) {
options = options || {};
Store.call(this, options);

this.firebase = options.firebase;

};

/*
* Inherit from `Store`.
*/

FirebaseStore.prototype.__proto__ = Store.prototype;

/**
* Attempt to fetch session by the given `sid`.
*
* @param {String} sid
* @param {Function} fn
* @api public
*/

FirebaseStore.prototype.get = function (sid, fn) {

var now = +new Date;
var sessionRef = new Firebase('https://' + this.firebase + '/sessions/' + sid);

sessionRef.on('value', function (snapshot) {
if (snapshot.val() === null) {
return fn(null, null);
} else {
var session = snapshot.val();
sess = JSON.parse(session.sess);

fn(null, sess);
}
});

};

/**
* Commit the given `sess` object associated with the given `sid`.
*
* @param {String} sid
* @param {Session} sess
* @param {Function} fn
* @api public
*/

FirebaseStore.prototype.set = function (sid, sess, fn) {

var expires = typeof sess.cookie.maxAge === 'number' ? (+new Date()) + sess.cookie.maxAge : (+new Date()) + oneDayInMilliseconds;
sess_string = JSON.stringify(sess);

var sessionRef = new Firebase('https://' + this.firebase + '/sessions/' + sid);
sessionRef.set({
expires: JSON.stringify(expires),
type: 'connect-session',
sess: sess_string
}, function (error) {
if (error) {
fn(error);
} else {
fn(null, sess);
}
});
}

/**
* Destroy the session associated with the given `sid`.
*
* @param {String} sid
* @param {Function} fn
* @api public
*/

FirebaseStore.prototype.destroy = function (sid, fn) {

var sessionRef = new Firebase('https://' + this.firebase + '/sessions/' + sid);
sessionRef.remove(fn);
};

return FirebaseStore;
};
24 changes: 24 additions & 0 deletions package.json
@@ -0,0 +1,24 @@
{
"name": "connect-firebase",
"description": "Firebase session store for Connect",
"version": "0.0.1",
"author": "Mike Carson <ca98am79@gmail.com> (http://ca98am79.com)",
"main": "./index.js",
"dependencies": {
"connect": "*",
"firebase": "*"
},
"devDependencies": {
"should": "*",
"mocha": "*"
},
"engines": {
"node": "*"
},
"optionalDependencies": {},
"homepage": "https://github.com/ca98am79/connect-firebase",
"repository": {
"type": "git",
"url": "git://github.com/ca98am79/connect-firebase.git"
}
}
97 changes: 97 additions & 0 deletions test/test.js
@@ -0,0 +1,97 @@
var firebase = 'connect-sessions.firebaseIO-demo.com';

var should = require('should'),
connect = require('connect'),
FirebaseStore = require(__dirname + '/../lib/connect-firebase.js')(connect);

describe('FirebaseStore', function () {
describe('Instantiation', function () {
it('should be able to be created', function () {
var store = new FirebaseStore({
firebase: firebase
});
store.should.be.an.instanceOf(FirebaseStore)
});
});
describe('Setting', function () {
it('should store data correctly', function (done) {
var store = new FirebaseStore({
firebase: firebase
});
store.set('1234', {
cookie: {
maxAge: 2000
},
name: 'tj'
}, function (err, res) {
if (err) throw err;
res.cookie.should.eql({
maxAge: 2000
});
res.name.should.eql('tj');

done();
});
});

});
describe('Getting', function () {
before(function () {
var store = new FirebaseStore({
firebase: firebase
});
store.set('1234', {
cookie: {
maxAge: 2000
},
name: 'tj'
}, function () {});
});

it('should get data correctly', function (done) {
var store = new FirebaseStore({
firebase: firebase
});
store.get('1234', function (err, res) {
if (err) throw err;
res.cookie.should.eql({
maxAge: 2000
});
res.name.should.eql('tj');

done();
});
});

});
describe('Destroying', function () {
before(function () {
var store = new FirebaseStore({
firebase: firebase
});
store.set('12345', {
cookie: {
maxAge: 2000
},
name: 'tj'
}, function () {});
});

it('should destroy data correctly', function (done) {
var store = new FirebaseStore({
firebase: firebase
});
store.destroy('12345', function (err, res) {
if (err) throw err;

store.get('12345', function (err, res) {
if (err) throw err;
should.not.exist(res);

done();
});
});
});

});
});

0 comments on commit b23b2d2

Please sign in to comment.