Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buffer.buffer in iojs 3.0 #2473

Closed
Nibbler999 opened this issue Aug 20, 2015 · 4 comments
Closed

Buffer.buffer in iojs 3.0 #2473

Nibbler999 opened this issue Aug 20, 2015 · 4 comments
Labels
buffer Issues and PRs related to the buffer subsystem.

Comments

@Nibbler999
Copy link
Contributor

Buffers have picked up a new .buffer property in 3.0 that seems to be a side effect of the Uint8Array stuff but is breaking code.

2.5

> new Buffer("test").buffer;
undefined

3.1

> new Buffer("test").buffer;
ArrayBuffer {}

Can you remove this new property or do I need to file PR against the modules this breaks?

@bnoordhuis
Copy link
Member

I don't think it's a property we want to remove (or even can.) I'm curious though, how is it breaking code?

@bnoordhuis bnoordhuis added the buffer Issues and PRs related to the buffer subsystem. label Aug 20, 2015
@Nibbler999
Copy link
Contributor Author

Slightly simplified example from engine.io:

var packets = exports.packets = {
    message:  4
};

var packet = {
    data: new Buffer("hello"),
    type: 'message'
};

encodePacket = function (packet, supportsBinary, callback) {

  var data = (packet.data === undefined)
    ? undefined
    : packet.data.buffer || packet.data;

  if (Buffer.isBuffer(data)) {
    return encodeBuffer(packet, supportsBinary, callback);
  } else if (data instanceof ArrayBuffer) {
    return encodeArrayBuffer(packet, supportsBinary, callback);
  }

};

function encodeBuffer(packet, supportsBinary, callback) {
  var data = packet.data;
  var typeBuffer = new Buffer(1);
  typeBuffer[0] = packets[packet.type];
  return callback(Buffer.concat([typeBuffer, data]));
}

function encodeArrayBuffer(packet, supportsBinary, callback) {
  var data = (packet.data === undefined)
    ? undefined
    : packet.data.buffer || packet.data;

  var contentArray = new Uint8Array(data);
  var resultBuffer = new Buffer(1 + data.byteLength);

  resultBuffer[0] = packets[packet.type];
  for (var i = 0; i < contentArray.length; i++){
    resultBuffer[i+1] = contentArray[i];
  }

  return callback(resultBuffer);
}

encodePacket(packet, true, function (message) {
    console.log(message.toString());
});

In 2.5 this will send the message "hello" to your client. In 3.0 it will send them 8kb of what looks like random memory contents.

@bnoordhuis
Copy link
Member

Thanks, I see what you mean. It's regrettable but I think we'll have to chalk this one up to the price of progress.

Even if we could remove the .buffer property (which from a technical POV is undesirable for a number of reasons), it couldn't land in v3.x because it's an API change. You'd end up having to fix engine.io for v3.x anyway.

@Nibbler999
Copy link
Contributor Author

Understood, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
buffer Issues and PRs related to the buffer subsystem.
Projects
None yet
Development

No branches or pull requests

2 participants