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

msgpack.decode(new Uint8Array(data)) leads to "Uncaught TypeError: this.readUInt32BE is not a function" #16

Closed
kochelmonster opened this issue Dec 16, 2015 · 3 comments

Comments

@kochelmonster
Copy link

I tracked that down to the following method in decode-buffer.js:

DecodeBuffer.prototype.append = function(chunk) {
  var prev = this.offset ? this.buffer.slice(this.offset) : this.buffer;
  this.buffer = prev ? Buffer.concat([prev, chunk]) : chunk;
  this.offset = 0;
};

I think the buffer assignment has to be replaced to:

  this.buffer = prev ? Buffer.concat([prev, chunk]) : Buffer(chunk);
@kawanet
Copy link
Owner

kawanet commented Feb 12, 2016

The current version of msgpack.decode requests a Buffer instance instead of a native Uint8Array instance as a chunk. Buffer has some of utility methods compared to Uint8Array's.

I guess msgpack.decode(Buffer(new Uint8Array(data)) would work.

By the way, the module was started when node.js was v0.12. It seems that Buffer implementation is changed since io.js. The module may have better to support Uint8Array. It'd fast on browsers.

$ ~/.nvm/v0.10.41/bin/node -pe 'Buffer(1) instanceof Uint8Array'
false
$ ~/.nvm/versions/node/v0.12.9/bin/node -pe 'Buffer(1) instanceof Uint8Array'
false
$ ~/.nvm/versions/node/v4.2.3/bin/node -pe 'Buffer(1) instanceof Uint8Array'
true
$ ~/.nvm/versions/node/v5.3.0/bin/node -pe 'Buffer(1) instanceof Uint8Array'
true

@kawanet kawanet closed this as completed Feb 12, 2016
@kochelmonster
Copy link
Author

I use msgpack in chrome. Buffer is not exported to global namespace.

@kawanet
Copy link
Owner

kawanet commented Feb 26, 2016

I use msgpack in chrome. Buffer is not exported to global namespace.

I see. Now it accepts Uint8Array instance since version 0.1.17!

var data = [0x81, 0xA3, 0x66, 0x6F, 0x6F, 0xA3, 0x62, 0x61, 0x72];
msgpack.decode(new Uint8Array(data)); // => {foo: "bar"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants