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

Added support for constructing from ArrayBuffer and added test that verifies support #42

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ function Buffer (subject, encoding, noZero) {
} else if (type === 'object' && subject !== null) { // assume object is array-like
if (subject.type === 'Buffer' && isArray(subject.data))
subject = subject.data
length = +subject.length > 0 ? Math.floor(+subject.length) : 0
// Support ArrayBuffer subjects
if (subject.length === undefined && typeof subject.byteLength === 'number')
length = subject.byteLength
Copy link
Owner

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.

else
length = +subject.length > 0 ? Math.floor(+subject.length) : 0
} else
throw new Error('First argument needs to be a number, array or string.')

Expand Down
13 changes: 13 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,19 @@ test('new buffer from float64array', function (t) {
t.end()
})

test('new buffer from ArrayBuffer', function(t) {
if (typeof ArrayBuffer !== 'undefined' && typeof DataView !== 'undefined') {
var b1 = new ArrayBuffer(2)
var dataView = new DataView(b1)
dataView.setInt16(0, 256, true)
var b2 = new B(b1)
t.equal(b1.byteLength, b2.length)
t.equal(b1[0], 0)
t.equal(b1[1], 1)
Copy link
Owner

Choose a reason for hiding this comment

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

Indexing into ArrayBuffers doesn't work. It's a bug that it works in node 0.10 and will fail in node 0.12 and browsers

Copy link
Author

Choose a reason for hiding this comment

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

Interesting, yeah I only tested this in latest Chrome (works like a charm, can parse and send MQTT over WebSocket) and assumed from the lack of any other accessors this would be the only way to have it work.

I can try to change the websocket-stream library to construct an Uint8Array from the ArrayBuffer before wrapping with a Buffer.

}
t.end()
})

test('buffer toArrayBuffer()', function (t) {
var data = [1, 2, 3, 4, 5, 6, 7, 8]
if (typeof Uint8Array !== 'undefined') {
Expand Down