-
-
Notifications
You must be signed in to change notification settings - Fork 240
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
Added support for constructing from ArrayBuffer and added test that verifies support #42
Added support for constructing from ArrayBuffer and added test that verifies support #42
Conversation
Thanks for the PR! I would like to accept this, but I'm very hesitant for the reasons mentioned here: #39 (comment) |
length = +subject.length > 0 ? Math.floor(+subject.length) : 0 | ||
// Support ArrayBuffer subjects | ||
if (subject.length === undefined && typeof subject.byteLength === 'number') | ||
length = subject.byteLength |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's actually not enough to just set the length correctly. subject
itself should be wrapped in a Uint8Array
so the data can actually be pulled out.
Thanks and apologies for not seeing other issues similar to this =] |
No problem. :) I'd open an issue with websocket-stream if you still think there's a bug here, though I expect you could just do |
So specifically This means it will opts.type = BufferPatched;
//...
function BufferPatched(subject) {
if (subject instanceof ArrayBuffer) {
subject = new Uint8Array(subject);
}
return Buffer.apply(this, arguments);
}
util.inherits(BufferPatched, Buffer); |
This sounds like reasonable solution to me :) |
Just thinking: would adding to the constructor of Buffer in this library add ArrayBuffer support for everyone? if (typeof ArrayBuffer === 'function' && subject instanceof ArrayBuffer) {
subject = new Uint8Array(subject);
} |
It would, and it's tempting to add it. But I'm worried that people would write code that depends on this behavior when it won't work in node.js. Maybe I should just send a PR to node core since this is an annoying API issue that keeps coming up. |
We can add support for this feature now that node.js#next has support! nodejs/node#2002 |
ArrayBuffer can be passed into the constructor as of buffer 3.4.0. |
No description provided.