-
Notifications
You must be signed in to change notification settings - Fork 61
decodePacket now accepts both Buffer and ArrayBuffer as data #64
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
Conversation
This is potentially not in line with how you planned the decode operation to work (only ever accepting Buffers) in a node-like environment. If it shouldn't be fixed in this part of the code, it might be a bug in browser detection/how nw.js manages its browser-websockets. Please give feedback. |
@Rainforce15 could you please add some test, to prevent any future regression? |
added Tests and fixed code to run properly from 0.10 onwards. |
lib/index.js
Outdated
} | ||
|
||
if (data instanceof ArrayBuffer) { | ||
data = new DataView(data); |
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.
how about using arrayBufferToBuffer
method here?
if (binaryType === 'arraybuffer') {
data = arrayBufferToBuffer(data);
}
test/node/index.js
Outdated
expect(new Uint8Array(decoded1.data)).to.eql(new Uint8Array(buffer1.slice(1))); | ||
expect(decoded2).to.eql({ type: 'message', data: new Buffer(buffer2.slice(1))}); | ||
expect(new Uint8Array(decoded2.data)).to.eql(new Uint8Array(buffer2.slice(1))); | ||
done(); |
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.
You should be able to remove the done()
here, since there is no callback
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.
Thank you for taking the time to review my code.
You are right, I didn't check for the existence of a helper function.
Tests work with arrayBufferToBuffer.
Done() cannot be removed, at least when I try to run the tests.
Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.
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.
I guess you also have to remove the done
callback here: it('should decode an ArrayBuffer/Buffer with undeclared type as binary', function(done) {
test/node/index.js
Outdated
done(); | ||
}); | ||
|
||
it('should decode an ArrayBuffer/Buffer as binary of type ArrayBuffer', function(done) { |
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.
Please correct me if I'm wrong, but aren't those cases already handled in the existing tests? To me, the missing test might be something like:
it('should encode/decode an ArrayBuffer as binary (with undefined binaryType)', function(done) {
var buffer = new ArrayBuffer(4);
var dataview = new DataView(buffer);
for (var i = 0; i < buffer.byteLength ; i++) dataview.setInt8(i, i);
encode({ type: 'message', data: buffer }, true, function(encoded) {
var decoded = decode(encoded);
expect(decoded).to.eql({ type: 'message', data: new Buffer([0,1,2,3]) });
expect(new Uint8Array(decoded.data)).to.eql(new Uint8Array(buffer));
done();
});
});
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.
The existing tests mostly test that running encode->decode works in succession - without ever checking the intermediate result of encode() or working of the functions on their own.(or what kind of data they create on different platforms) This might work for most cases, but since decode() is a publically available function, it can be called from anything and could use some more error proofing than just giving them controlled input from encode of the same platform, depending on design/intention, in my opinion.
In fact, you could create intermediate data that is against the design and would never notice with this test as long as you make the same "mistake" in both the output of encode() as well as the input of decode() (which apparently happens in nw.js and others). My experience with testing is very limited though, as is my understanding of the overal intention of how to use the code and under which circumstances.
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.
My concern is that some of the tests you added may already exist elsewhere in the file.
I agree though that the output of both method encode
and decode
could be tested, but that seems out of the scope of that particular PR, doesn't it?
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 tests that Buffer and ArrayBuffer both work as in- and output of decode(), which is what this fix is about (the intent that it fixes cross-platform issues in Socket.io and Node.js/nw.js/Electron when they use encode()/decode() because they can be of both types)
Therefore, I think the test is okay as it is creating a Buffer and ArrayBuffer, and testing it once with arrayBuffer as binaryType and another time with no binaryType (might as well be "message" though, afaik).
If this is beyond the intended scope of the function, please reject this PR.
@Rainforce15 thanks a lot, merged as 5aecaa9 |
Potential fix for Issue #60.