Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit e3c619142f54effdea62446a32b3c7974e0430d0 0 parents
@wezm authored
56 Cakefile
@@ -0,0 +1,56 @@
+# Derived from https://github.com/assaf/zombie/blob/master/Cakefile, MIT licenced
+
+fs = require("fs")
+path = require("path")
+{spawn, exec} = require("child_process")
+stdout = process.stdout
+
+# Use executables installed with npm bundle.
+process.env["PATH"] = "node_modules/.bin:#{process.env["PATH"]}"
+
+# ANSI Terminal Colors.
+bold = "\033[0;1m"
+red = "\033[0;31m"
+green = "\033[0;32m"
+reset = "\033[0m"
+
+# Log a message with a color.
+log = (message, color, explanation) ->
+ console.log color + message + reset + ' ' + (explanation or '')
+
+# Handle error and kill the process.
+onerror = (err)->
+ if err
+ process.stdout.write "#{red}#{err.stack}#{reset}\n"
+ process.exit -1
+
+
+## Building ##
+
+build = (callback)->
+ log "Compiling CoffeeScript to JavaScript ...", green
+ exec "rm -rf lib && coffee -c -l -b -o lib src", (err, stdout)->
+ callback err
+task "build", "Compile CoffeeScript to JavaScript", -> build onerror
+
+task "watch", "Continously compile CoffeeScript to JavaScript", ->
+ cmd = spawn("coffee", ["-cw", "-o", ".", "src"])
+ cmd.stdout.on "data", (data)-> process.stdout.write green + data + reset
+ cmd.on "error", onerror
+
+
+clean = (callback)->
+ exec "rm -rf lib", callback
+task "clean", "Remove temporary files and such", -> clean onerror
+
+
+## Testing ##
+
+runTests = (callback)->
+ log "Running test suite ...", green
+ exec "nodeunit test", (err, stdout)->
+ process.stdout.write stdout
+ callback err if callback
+task "test", "Run all tests", ->
+ runTests (err)->
+ process.stdout.on "drain", -> process.exit -1 if err
29 package.json
@@ -0,0 +1,29 @@
+{
+ "name": "connect-kyoto",
+ "description": "Kyoto Tycoon session store for Connect",
+ "version": "0.1.0",
+ "homepage": "https://github.com/wezm/connect-kyoto",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/wezm/connect-kyoto.git"
+ },
+ "author": "Wesley Moore <wes@wezm.net> (http://www.wezm.net/)",
+ "main": "lib/index.js",
+ "directories": {
+ "lib": "lib"
+ },
+ "scripts": {
+ "test": "cake test"
+ },
+ "dependencies": {
+ "connect": "~1.0",
+ "kyoto-client": "~0.3.0"
+ },
+ "devDependencies": {
+ "coffee-script": "~ 1.0.0",
+ "nodeunit": "~ 0.5.0"
+ },
+ "engines": {
+ "node": ">=0.4.3"
+ }
+}
1  src/lib/index.coffee
@@ -0,0 +1 @@
+exports.KyotoStore = require './kyoto-store'
75 src/lib/kyoto-store.coffee
@@ -0,0 +1,75 @@
+Store = require('connect').session.Store
+kyoto = require 'kyoto-client'
+
+###
+ * Session Store Implementation:
+ *
+ * Every session store _must_ implement the following methods
+ *
+ * - `.get(sid, callback)`
+ * - `.set(sid, session, callback)`
+ * - `.destroy(sid, callback)`
+ *
+ * Recommended methods include, but are not limited to:
+ *
+ * - `.length(callback)`
+ * - `.clear(callback)`
+###
+
+class KyotoStore extends Store
+ constructor: (options) ->
+ options = options or {}
+ unless options.db #and options.host and options.port
+ throw new Error("db, host and port must be specified")
+ Store.call this, options
+ @kyoto = new kyoto.Db(options.db).open(options.host, options.port)
+ # @db = options.db or 1
+ this
+
+ get: (sid, callback) ->
+ @kyoto.get sid, (error, value) ->
+ return callback error if error
+ return callback undefined, value unless value?
+
+ try
+ callback undefined, JSON.parse(value.toString())
+ catch exception
+ console.log exception
+ callback exception
+
+ set: (sid, session, callback) ->
+ # try {
+ # var maxAge = sess.cookie.maxAge
+ # , ttl = 'number' == typeof maxAge
+ # ? maxAge / 1000 | 0
+ # : oneDay
+ # , sess = JSON.stringify(sess);
+ # this.client.setex(sid, ttl, sess, function(){
+ # fn && fn.apply(this, arguments);
+ # });
+ # } catch (err) {
+ # fn && fn(err);
+ # }
+ try
+ maxAge = session.cookie.maxAge
+ ttl = maxAge / 1000 | 0
+ @kyoto.set sid, JSON.stringify(session), (error) ->
+ callback()
+ catch exception
+ console.log exception
+ callback exception
+
+ destroy: (sid, callback) ->
+ @kyoto.remove sid, (error) ->
+ # Ignore the error for now.
+ callback()
+
+ length: (callback) ->
+ @kyoto.status (error, info) ->
+ callback info.count
+
+ clear: (callback) ->
+ @kyoto.clear (error) ->
+ callback error
+
+module.exports = KyotoStore
0  src/test/kyoto-store_test.coffee
No changes.
Please sign in to comment.
Something went wrong with that request. Please try again.