0

I have this external definition that wraps up a buffer and functions that perform operations on that buffer.

var Packer = {
    _buffer: new Buffer(""),

    readShort: function(){
        var rs = this._buffer.readInt16BE();
        console.log('first buffer len ' + this._buffer.length);
        this._buffer = this._buffer.slice(2);
        console.log('second buffer len ' + this._buffer.length);
        return rs;
    },

    readString: function(){
        var len = this.readShort();
        console.log('while reading string len is ' + len);
        var rs = this._buffer.toString('utf8', 2, len + 2);
        this._buffer = this._buffer.slice(len + 2);
        return rs;
    },
};

exports.Packer = Packer;

Externally, I call the following:

var HelloBob = {
    handlePacket: function(p){
         //.. p is a string
         p = new Buffer(p);

         var packer = Object.spawn(pack.Packer, {_buffer: p});

         //Bob is trying to improve his knowledge
         var humansOnMoon = packer.readShort();
         var nameOfGalaxy = packer.readString();
    },
};

The problem is that the buffer does not update after the readShort operation (which implies a slice that cuts out a part of the buffer).

first buffer len 354
second buffer len 354
while reading string len is 354

I read that the buffer resulting after the slice memory shares memory with the original buffer. I have also attempted to copy the buffer (overlap it) to remove the first (2) elements but it behaves identically.

Why is this happening and how can I solve it?

Notes:

  • p is a buffer of length 354
  • Docs
7
  • The code slicing the array should work just fine, something else is going on? You're probably accessing the same array both times, either because it hasn't changed yet, or because you've done something wrong. It should be noted that you don't return the modified buffer, but the result of readInt16BE()?
    – adeneo
    Feb 20, 2016 at 20:52
  • @adeneo Indeed, I'm returning the value I read from the Buffer. The purpose is to call the read again on the same buffer but to read the following value not the same one (that's why I am slicing the buffer)
    – Gabe
    Feb 20, 2016 at 20:53
  • I think you'll have to post how you're calling the functions, and what Object.spawn does etc.
    – adeneo
    Feb 20, 2016 at 20:56
  • @adeneo The value of length is not subject to a change even between the two logs in the readShort function.
    – Gabe
    Feb 20, 2016 at 20:57
  • Did you try calling toString on the buffers, to see if that changes, it could be an issue with logging live "objects" (buffers)
    – adeneo
    Feb 20, 2016 at 20:58

1 Answer 1

0

I managed to fix it by setting the _buffer variable through a function (not through the initializer). Somehow, it behaves as if it was immutable if I set it in the {}.

var packer = Object.spawn(pack.Packer, {});
packer.setBuffer(new Buffer(p));

Added this setter:

setBuffer: function(nbf){
    this._buffer = nbf;
}
1
  • Sounds more like a scope issue, where this is not what you think it is, and the value of this depends on the execution context, so in other words it all depends on how the Object.spawn function works
    – adeneo
    Feb 20, 2016 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.