Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 23 additions & 25 deletions utils/serialization_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,34 @@ const Transform = require('stream').Transform;
* emits 'message' with the data for that message. All socket
* communications should be piped through this.
*/
function DeserializeStream(options) {
if (!(this instanceof DeserializeStream)) {
return new DeserializeStream(options);
}
class DeserializeStream extends Transform {

Transform.call(this, options);
// true once we've pulled off the message length
// for the next message we'll need to deserialize
this._inBody = false;
constructor(options) {
super(options);

// track how many bytes of this message we've received so far
this._messageConsumed = 0;
// Transform.call(this, options);
// true once we've pulled off the message length
// for the next message we'll need to deserialize
this._inBody = false;

// how long this message will be
this._messageLen = -1;
// track how many bytes of this message we've received so far
this._messageConsumed = 0;

// as bytes of this message arrive, store them in this
// buffer until we have the whole thing
this._messageBuffer = [];
// how long this message will be
this._messageLen = -1;

// TODO: These are specific to parsing a service response...
// don't use them everywhere
// the first byte in a service response is true/false service success/fail
this._deserializeServiceResp = false;
// as bytes of this message arrive, store them in this
// buffer until we have the whole thing
this._messageBuffer = [];

this._serviceRespSuccess = null;
}
// TODO: These are specific to parsing a service response...
// don't use them everywhere
// the first byte in a service response is true/false service success/fail
this._deserializeServiceResp = false;

this._serviceRespSuccess = null;
}

DeserializeStream.prototype = {
_transform(chunk, encoding, done) {
let pos = 0;
let chunkLen = chunk.length;
Expand Down Expand Up @@ -124,7 +123,7 @@ DeserializeStream.prototype = {
}
}
done();
},
}

emitMessage(buffer) {
if (this._deserializeServiceResp) {
Expand All @@ -134,14 +133,13 @@ DeserializeStream.prototype = {
else {
this.emit('message', buffer);
}
},
}

setServiceRespDeserialize() {
this._deserializeServiceResp = true;
}
};

util.inherits(DeserializeStream, Transform);

//-----------------------------------------------------------------------

Expand Down