feross / typedarray-to-buffer

Convert a typed array to a Buffer without a copy.
MIT License
66 stars 16 forks source link

Fails in combination with Buffer module trying to test with Buffer.isBuffer #5

Closed dignifiedquire closed 8 years ago

dignifiedquire commented 8 years ago

Given the following:

// global.Buffer should be the buffer module from npm
var array = new Uint8Array([1,2,3])
var buf = toBuffer(array)
assert(Buffer.isBuffer(buf)) // throws

The reason for this is that the isBuffer check checks for ._isBuffer on the element but the code path on here https://github.com/feross/typedarray-to-buffer/blob/master/index.js#L17-L19 does not add that property.

feross commented 8 years ago

Right you are. Sorry about that.

feross commented 8 years ago

Actually, what version of browserify are you using? The latest version of buffer (used in browserify 13) doesn't require setting ._isBuffer manually on each instance since it's inherited through the prototype chain. See: https://github.com/feross/buffer/blob/master/index.js#L418

dignifiedquire commented 8 years ago

Arrrrggh -.- you are right the buffer version was outdated in one dependency (I'm using webpack for this one) updating it makes the issue go away.. Thanks.

feross commented 8 years ago

Hoping that by inheriting the whole prototype (i.e. Buffer is now a proper ES6 subclass of Uint8Array) we won't have version incompatibilities like this going forward. Sorry that this bit you though :(

feross commented 8 years ago

Actually, there's an even better solution. We can just stop depending on the internals of the browser buffer implementation since new Buffer(arrayBuffer) doesn't do a copy. See PR https://github.com/feross/typedarray-to-buffer/pull/6

dignifiedquire commented 8 years ago

6 looks really good! thanks for the quick responses