nix type/service confusion

This commit is contained in:
AJ ONeal 2016-09-30 03:03:26 -04:00
parent 5829b2064b
commit 12ea767bf8
1 changed files with 13 additions and 18 deletions

View File

@ -96,6 +96,7 @@ module.exports.create = function (opts) {
machine.port = machine._headers[2]; machine.port = machine._headers[2];
machine.bodyLen = parseInt(machine._headers[3], 10) || -1; machine.bodyLen = parseInt(machine._headers[3], 10) || -1;
machine.service = machine._headers[4]; machine.service = machine._headers[4];
//console.log('machine.service', machine.service);
return true; return true;
} }
@ -140,22 +141,21 @@ module.exports.create = function (opts) {
} }
machine.bufIndex += partLen; machine.bufIndex += partLen;
machine.service = machine.service || machine.type; machine.service = machine.service;
machine.type = machine.type || machine.service;
data = machine.buf.slice(0, machine.bufIndex); data = machine.buf.slice(0, machine.bufIndex);
//console.log('machine.service', machine.service);
// //
// data, end, error // data, end, error
// //
if ('end' === machine.type) { if ('end' === machine.service) {
msg = {}; msg = {};
msg.family = machine.family; msg.family = machine.family;
msg.address = machine.address; msg.address = machine.address;
msg.port = machine.port; msg.port = machine.port;
msg.service = 'end'; msg.service = 'end';
msg.type = msg.type || 'end';
msg.data = data; msg.data = data;
if (machine.emit) { if (machine.emit) {
@ -165,7 +165,7 @@ module.exports.create = function (opts) {
(machine.onend||machine.onmessage)(msg); (machine.onend||machine.onmessage)(msg);
} }
} }
else if ('error' === machine.type) { else if ('error' === machine.service) {
try { try {
msg = JSON.parse(machine.data.toString()); msg = JSON.parse(machine.data.toString());
} catch(e) { } catch(e) {
@ -176,7 +176,6 @@ module.exports.create = function (opts) {
msg.address = machine.address; msg.address = machine.address;
msg.port = machine.port; msg.port = machine.port;
msg.service = 'error'; msg.service = 'error';
msg.type = msg.type || 'error';
msg.data = data; msg.data = data;
if (machine.emit) { if (machine.emit) {
@ -193,7 +192,6 @@ module.exports.create = function (opts) {
msg.address = machine.address; msg.address = machine.address;
msg.port = machine.port; msg.port = machine.port;
msg.service = machine.service; msg.service = machine.service;
msg.type = machine.type || 'data';
msg.data = data; msg.data = data;
if (machine.emit) { if (machine.emit) {
@ -228,26 +226,23 @@ module.exports.create = function (opts) {
}; };
module.exports.pack = function (address, data, type) { module.exports.pack = function (address, data, service) {
data = data || Buffer.alloc(1); data = data || Buffer.alloc(1);
if (!data.byteLength) { if (!data.byteLength) {
data = Buffer.alloc(1); data = Buffer.alloc(1);
} }
if (type || (data.byteLength <= '|__ERROR__|'.length)) {
if ('error' === type || '|__ERROR__|' === data.toString()) { if ('error' === service) {
address.type = 'error'; address.service = 'error';
address.service = 'error'; }
} else if ('end' === service) {
else if ('end' === type || '|__END__|' === data.toString()) { address.service = 'end';
address.type = 'end';
address.service = 'end';
}
} }
var version = 1; var version = 1;
var header = Buffer.from([ var header = Buffer.from([
/*servername,*/ address.family, address.address, address.port, data.byteLength /*servername,*/ address.family, address.address, address.port, data.byteLength
, (address.type || address.service || '') , (address.service || '')
].join(',')); ].join(','));
var meta = Buffer.from([ 255 - version, header.length ]); var meta = Buffer.from([ 255 - version, header.length ]);
var buf = Buffer.alloc(meta.byteLength + header.byteLength + data.byteLength); var buf = Buffer.alloc(meta.byteLength + header.byteLength + data.byteLength);