-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from turbonetix/implementation
READY: Implementation
- Loading branch information
Showing
5 changed files
with
325 additions
and
14 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
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,21 @@ | ||
var io = require('socket.io')(3000); | ||
var Logger = require('./..'); | ||
var options = { format: Logger.defaultFormat, stream: { write: function (line) { process.stdout.write(line); } } }; | ||
var logger = Logger(options); | ||
io.use(logger); | ||
io.on('connection', function (sock) { | ||
sock.emit('done'); | ||
}); | ||
|
||
setTimeout(function () { | ||
var sock = require('socket.io-client').connect('ws://localhost:3000'); | ||
sock.on('connect', function () { | ||
sock.emit('this'); | ||
sock.emit('is'); | ||
sock.emit('a'); | ||
sock.emit('test'); | ||
setTimeout(function () { | ||
process.exit(0); | ||
}, 1000); | ||
}); | ||
},1000); |
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 |
---|---|---|
@@ -1,15 +1,143 @@ | ||
var debug = require('debug')('logger'); | ||
var pkg = require('./../package.json'); | ||
var flat = require('flat'); | ||
var Router = require('socket.io-events'); | ||
|
||
exports = module.exports = Logger; | ||
exports.version = require('./../package.json').version; | ||
exports.version = pkg.version; | ||
|
||
function Logger () { | ||
if (!(this instanceof Logger)) return new Logger(); | ||
function logger () { | ||
/** | ||
* A very simple middleware for socket.io that will record the events and log | ||
* them to a stream. | ||
* | ||
* @return Logger | ||
*/ | ||
|
||
function Logger (options) { | ||
if (!(this instanceof Logger)) return new Logger(options); | ||
|
||
options = options || {}; | ||
|
||
debug('new logger v%s', pkg.version); | ||
|
||
var router = Router(); | ||
router.on(function (sock, args, cb) { | ||
debug('logger args.length %s', arguments.length); | ||
try { | ||
// "this" is the "socket" | ||
logger.stream().write(logger.format(sock, args) + "\n"); | ||
cb(); | ||
} | ||
catch (e) { | ||
debug('caught an error %s', e); | ||
console.trace(e); | ||
cb(e); | ||
} | ||
}); | ||
|
||
function logger (sock, cb) { | ||
debug('logger sock', sock, cb.toString()); | ||
router(sock, cb); | ||
} | ||
|
||
logger.__proto__ = Logger.prototype; | ||
|
||
if (options.stream) { | ||
logger.stream(options.stream); | ||
} | ||
|
||
if (options.format) { | ||
logger.format(options.format); | ||
} | ||
|
||
return logger; | ||
} | ||
|
||
Logger.prototype = {} | ||
/** | ||
* The default formating function for Logger | ||
* | ||
* @param {Object} sock | ||
* @param {args} args | ||
*/ | ||
|
||
Logger.defaultFormat = function (sock, args) { | ||
debug('defaultFormat', sock, args); | ||
args = args || []; | ||
var name = args.shift() || 'unkown'; | ||
var data; | ||
switch(args.length) { | ||
case 0: | ||
data = {}; | ||
break; | ||
case 1: | ||
data = args.shift(); | ||
break; | ||
default: | ||
data = args; | ||
break; | ||
}; | ||
return { | ||
socket: { | ||
id: sock.id | ||
}, | ||
event: { | ||
name: name, | ||
data: data | ||
} | ||
}; | ||
}; | ||
|
||
/** | ||
* Either sets / gets the format function, or invokes the format | ||
* function given the sock, and args. | ||
* | ||
* @api public | ||
* @param mixed | ||
* @return String | ||
*/ | ||
|
||
Logger.prototype.format = function () { | ||
var args = Array.prototype.slice.call(arguments); | ||
debug('format args', args); | ||
|
||
switch(arguments.length) { | ||
case 0: | ||
debug('get format'); | ||
if (!this._format) { | ||
debug('no format'); | ||
this.format(Logger.defaultFormat); | ||
} | ||
return this._format; | ||
break; | ||
case 1: | ||
debug('setting format') | ||
if ('function' === typeof args[0] && args[0].length >= 2) { | ||
this._format = args[0]; | ||
} | ||
return this; | ||
break; | ||
default: | ||
return JSON.stringify(flat.flatten(this.format().apply(this, args))); | ||
break; | ||
} | ||
|
||
}; | ||
|
||
/** | ||
* set / get the stream instance | ||
* | ||
* @api public | ||
* @param {Object} stream | ||
* @return Logger; | ||
*/ | ||
|
||
Logger.prototype.stream = function (stream) { | ||
if ('undefined' !== typeof stream && 'function' === typeof stream.write) { | ||
this._stream = stream; | ||
return this; | ||
} | ||
if (!this._stream || 'function' !== typeof this._stream.write) { | ||
this.stream(process.stdout); | ||
} | ||
return this._stream; | ||
}; |
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
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 |
---|---|---|
@@ -1,18 +1,96 @@ | ||
EventEmitter = require('events').EventEmitter | ||
Flat = require 'flat' | ||
|
||
describe 'lib', -> | ||
|
||
Given -> @sock = new EventEmitter | ||
Given -> @sock.id = 1 | ||
Given -> @args = ['some event', {some: 'data'}] | ||
Given -> @cb = jasmine.createSpy 'cb' | ||
Given -> @line = '{"socket.id":1,"event.name":"some event","event.data.some":"data"}' | ||
|
||
Given -> @lib = requireSubject 'lib', { | ||
Given -> @lib = requireSubject 'lib', | ||
'./../package.json': | ||
version: 1 | ||
} | ||
flat: Flat | ||
|
||
describe '.version', -> | ||
|
||
When -> @version = @lib.version | ||
Then -> expect(@version).toEqual 1 | ||
|
||
describe '#defaultFormat', -> | ||
|
||
When -> @res = @lib.defaultFormat @sock, @args | ||
Then -> expect(@res).toEqual | ||
socket: | ||
id: 1 | ||
event: | ||
name: 'some event' | ||
data: | ||
some: 'data' | ||
|
||
describe '#', -> | ||
|
||
When -> @res = @lib() | ||
Then -> expect(typeof @res).toBe 'function' | ||
And -> expect(@res instanceof @lib).toBe true | ||
|
||
describe '#(options:Object)', -> | ||
|
||
Given -> @options = stream: write: -> | ||
When -> @res = @lib @options | ||
Then -> expect(typeof @res).toBe 'function' | ||
And -> expect(@res instanceof @lib).toBe true | ||
And -> expect(@res.stream()).toEqual @options.stream | ||
|
||
describe 'prototype', -> | ||
|
||
Given -> @logger = @lib() | ||
|
||
describe.only '# (sock:Object, args:Array)', -> | ||
|
||
Given -> @packet = data: @args | ||
Given -> @stream = jasmine.createSpyObj 'stream', ['write'] | ||
Given -> @logger.stream @stream | ||
Given -> @logger @sock, @cb | ||
When -> @sock.onevent @packet | ||
Then -> expect(@stream.write).toHaveBeenCalled() | ||
And -> expect(@stream.write.argsForCall[0][0]).toEqual @line + '\n' | ||
|
||
describe '#format', -> | ||
|
||
When -> @format = @logger.format() | ||
Then -> expect(typeof @format).toBe 'function' | ||
And -> expect(@format.length).toBe 2 | ||
And -> expect(@format).toEqual @lib.defaultFormat | ||
|
||
describe '#format (format:Function)', -> | ||
|
||
Given -> @format = (a, b) -> | ||
When -> @logger.format @format | ||
Then -> expect(@logger.format()).toEqual @format | ||
|
||
describe '#format (sock:Object, args:Array)', -> | ||
|
||
When -> @res = @logger.format @sock, @args | ||
And -> expect(@res).toEqual @line | ||
|
||
describe '#stream', -> | ||
|
||
When -> @stream = @logger.stream() | ||
Then -> expect(typeof @stream).toBe 'object' | ||
And -> expect(typeof @stream.write).toBe 'function' | ||
|
||
describe '#stream (stream:Object)', -> | ||
|
||
Given -> @stream = write: -> | ||
When -> @logger.stream @stream | ||
Then -> expect(@logger.stream()).toEqual @stream | ||
|
||
describe '#stream (stream:mixed)', -> | ||
|
||
Given -> @stream = 'crap' | ||
Given -> @existing = @logger.stream() | ||
When -> @logger.stream @stream | ||
Then -> expect(@logger.stream()).toEqual @existing |