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

pass Array Buffer to constructor #39

Closed
wants to merge 1 commit into from

Conversation

calvinmetcalf
Copy link
Contributor

No description provided.

t.equal(b1[1], 1)
t.equal(b1[2], 2)
t.equal(b1[3], 3)
t.equal(b1[4], undefined)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't actually checking the right buffer, b2.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do'h all I checked was that it failed without this patch

@feross
Copy link
Owner

feross commented Jul 30, 2014

Hi, thanks for the PR! I don't think this is a good idea (aside from the issues I mention inline) because it would make this module diverge from the node.js buffer API.

An ArrayBuffer is not array-like: it lacks a length property indexes can't be accessed directly via buf[0], etc. Thus, new Buffer(new Uint8Array([0, 1, 2, 3]).buffer) fails in both node.js and this module.

If you have an ArrayBuffer, just wrap it in a Uint8Array before passing it to the constructor and you'll be good.

@feross feross closed this Jul 30, 2014
@calvinmetcalf
Copy link
Contributor Author

so the impetus for this was a bug using level-js (Level/level-js#34) caused by array buffers being turned into zero length Uint8Array objects, there is some code that made me think that you intended to support array buffers, but if you don't would you accept a pull to throw or print a warning if array buffers are given to the constructor (yes strictly speaking a divergence from node, but your more likely to get a typed array in browserified contexts)

@feross
Copy link
Owner

feross commented Aug 2, 2014

The code you linked is there to support TypedArrayViews like Uint8Array, etc. which have a byteLength property as well. The set function wouldn't work with an arraybuffer. :)

If level-js claims to support ArrayBuffers here (I don't know if it does), then this is a bug in how it's using this module. It should check if it's an ArrayBuffer and wrap it in a Uint8Array.

@calvinmetcalf
Copy link
Contributor Author

right forgot the typed arrays had both

On Fri, Aug 1, 2014 at 8:16 PM, Feross Aboukhadijeh <
[email protected]> wrote:

The code https://github.com/feross/buffer/blob/master/index.js#L96 you
linked is there to support TypedArrayViews like Uint8Array, etc. which have
a byteLength property as well. The set function wouldn't work with an
arraybuffer. :)

If level-js claims to support ArrayBuffers here (I don't know if it does),
then this is a bug in how it's using this module. It should check if it's
an ArrayBuffer and wrap it in a Uint8Array.


Reply to this email directly or view it on GitHub
#39 (comment).

-Calvin W. Metcalf

@feross
Copy link
Owner

feross commented Jun 18, 2015

We can add support for this feature now that node.js#next has support! nodejs/node#2002

@feross
Copy link
Owner

feross commented Aug 5, 2015

ArrayBuffer can be passed into the constructor as of buffer 3.4.0.

@calvinmetcalf
Copy link
Contributor Author

vindicated! thanks @feross

@feross
Copy link
Owner

feross commented Aug 5, 2015

@calvinmetcalf Yeah :) I agreed it's a great idea but want to keep this module exactly in sync with node. I argued to support this - happy it finally landed!!

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

Successfully merging this pull request may close these issues.

2 participants