-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
myMessage.toBuffer() does not work in browser, although works in nodejs #309
Comments
|
Uh-huuh. The npm request module I'm using does not send any data, when it gets ArrayBuffer in the body. |
As far as I know https://github.com/feross/buffer is used to emulate node buffers in the browser, when browserify'ing code. Looks like its constructor can handle ArrayBuffers and typed arrays (see englercj's comment on where it copies). |
Request may also support sending a typed array (worth a try): body: process.browser ? new Uint8Array(myMessage.toBuffer()) : myMessage.toBuffer() Just keep in mind that if you end up having to create the buffer manually to pass it in, that the browser buffer module will do a copy operation to create the buffer: var Buffer = require('buffer').Buffer; // browserify will take care of it
// ...
// new Buffer(ArrayBuffer) creates a copy of the array buffer.
body: process.browser ? new Buffer(new Uint8Array(myMessage.toBuffer())) : myMessage.toBuffer() Whereas creating a typed array from an array buffer does not create a copy. |
Tried both, and they did not work... What I observed: both solutions (new Buffer() and Uint8Array) return something that is "instanceof Uint8Array", unlike the node buffer. Any ideas? I guess I may try substituting request module for something else and then try the UInt8Array... |
You need to pass a typed array to the buffer ctor: new Buffer(new Uint8Array(myMessage.toBuffer())) Also you should never do |
ok, found it. Part of the problem was solved by using an alternative library browser-request iriscouch/browser-request#16 and the second issue was totally unrelated - the webrequest library kept appending cookies that belonged to that domain without my consent, and the google server turned the request down the moment it saw cookies. In the end I didn't even need Buffer, converting to Uint8Array was enough. Thanks. |
Hi,
I'm not sure if it's a bug or a feature, but I tried to send the buffered message via node.js and everything worked. When I ran the same script inside a browser (browser extension) it failed at the point that (probably) the recipient didn't receive any data in the body...
Plus one more funny thing - I tried to analyze the myMessage.toBuffer() and it turns out it's an ArrayBuffer anyway.
Do you please have any idea, what may be wrong?
The text was updated successfully, but these errors were encountered: