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

Decode ArrayBuffer #44

Closed
lpinca opened this issue Jul 9, 2016 · 2 comments
Closed

Decode ArrayBuffer #44

lpinca opened this issue Jul 9, 2016 · 2 comments

Comments

@lpinca
Copy link

lpinca commented Jul 9, 2016

Version: 0.1.20

Description:
An error is thrown when trying to decode an ArrayBuffer

/home/luigi/pack/node_modules/msgpack-lite/lib/read-core.js:15
    if (!func) throw new Error("Invalid type: " + (type ? ("0x" + type.toString(16)) : type));
               ^

Error: Invalid type: undefined
    at Codec.decode (/home/luigi/pack/node_modules/msgpack-lite/lib/read-core.js:15:22)
    at DecodeBuffer.fetch (/home/luigi/pack/node_modules/msgpack-lite/lib/decode-buffer.js:54:21)
    at DecodeBuffer.read (/home/luigi/pack/node_modules/msgpack-lite/lib/decode-buffer.js:39:28)
    at Object.decode (/home/luigi/pack/node_modules/msgpack-lite/lib/decode.js:10:18)
    at Object.<anonymous> (/home/luigi/pack/index.js:10:9)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)

Test case:

'use strict';

const msgpack = require('msgpack-lite');

const arr = new Uint8Array([163, 102, 111, 111]);

msgpack.decode(arr);         // Works as intended
msgpack.decode(arr.buffer);  // Throws

Is this expected? Should an ArrayBuffer be explicitly converted to a typed array before decoding it?
I stumbled upon this when trying to decode a message from a WebSocket object whose binaryType was set to arraybuffer.

@kawanet
Copy link
Owner

kawanet commented Jul 10, 2016

Should an ArrayBuffer be explicitly converted to a typed array before decoding it?

Right. Please just wrap with new Uint8Array(buf) which costs none.

const arr = new Uint8Array([163, 102, 111, 111]); // TypedArray
const buf = arr.buffer; // ArrayBuffer
msgpack.decode(new Uint8Array(buf));  // Works as intended

@lpinca
Copy link
Author

lpinca commented Jul 10, 2016

Ok, thank you for the quick answer.

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