Skip to content

Commit

Permalink
Use json as default message encoder.
Browse files Browse the repository at this point in the history
  • Loading branch information
lsm committed Apr 13, 2016
1 parent edd46f3 commit 9eb012e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
7 changes: 7 additions & 0 deletions lib/queue/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var msg = require('./msg')
var type = require('../message/type')
var pubsub = require('./pubsub')
var reqrep = require('./reqrep')
Expand Down Expand Up @@ -57,6 +58,12 @@ Queue.prototype.rep = reqrep.rep
Queue.prototype.onReq = reqrep.onReq
Queue.prototype.onRep = reqrep.onRep

/**
* Message API
*/

Queue.prototype.encode = msg.encode
Queue.prototype.decode = msg.decode

/**
* Common sending patterns
Expand Down
25 changes: 25 additions & 0 deletions lib/queue/msg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var json = require('../message/json')
var Buffer = require('../message/type').Buffer
var Message = require('../message/index')

var encode = Message.encode
var decode = Message.decode

var formatId = Buffer(json.FORMAT_ID)
var encodeMsg = json.encodeMsg
var decodeMsg = json.decodeMsg


exports.encode = function(type, event, msg, meta) {
return encode({
type: type,
event: event,
formatId: formatId,
msg: msg,
meta: meta
}, encodeMsg)
}

exports.decode = function(buf) {
return decode(buf, decodeMsg)
}
2 changes: 0 additions & 2 deletions lib/socket/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ var QueueChannel = require('../queue/channel')
var SocketChannel = module.exports = function SocketChannel(socket, ns, chn) {
if (socket) {
this.queue = new QueueChannel(socket, ns, chn)
this.queue.encode = socket.queue.encode
this.queue.decode = socket.queue.decode
this.streams = socket.streams
} else {
Socket.call(this, new QueueChannel(this, ns, chn))
Expand Down
1 change: 0 additions & 1 deletion lib/socket/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ var Socket = module.exports = function(QueueClass) {
queue = QueueClass
this.queue = queue
this.streams = []
this.setMsgEncoder()
}

inherits(Socket, EventEmitter)
Expand Down
10 changes: 2 additions & 8 deletions lib/socket/msg.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
var json = require('../message/json')
var Buffer = require('../message/type').Buffer
var Message = require('../message/index')

var encode = Message.encode
Expand All @@ -19,12 +17,8 @@ exports.setMsgEncoder = function(encoder, formatId) {
decodeMsg = encoder.decode || encoder.unpack
}

if (!encodeMsg && !decodeMsg) {
// Default using JSON encoder.
encodeMsg = json.encodeMsg
decodeMsg = json.decodeMsg
formatId = Buffer(json.FORMAT_ID)
}
if (!encodeMsg || !decodeMsg)
throw new Error('Missing either encode/pack or decode/unpack function.')

this.queue.encode = function(type, event, msg, meta) {
return encode({
Expand Down

0 comments on commit 9eb012e

Please sign in to comment.