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

myMessage.toBuffer() does not work in browser, although works in nodejs #309

Closed
novellizator opened this issue Jul 30, 2015 · 7 comments
Closed
Labels

Comments

@novellizator
Copy link

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...

// https://www.npmjs.com/package/request
request.post({
  url: SOME_URL,
  headers: {
    'Content-Type': 'application/octet-stream',
    'Authorization': 'Bearer '+ accessToken
  },
  encoding: null, //  if you expect binary data
  body: myMessage.toBuffer()
}, (error, response, body) => {
  console.log('error, body', error, body);
  if (!error) {
    //readSyncRequest(body, cb);
  }
});

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?

@englercj
Copy link

.toBuffer() returns a node buffer in node, and an ArrayBuffer in the browser. What isn't working for you?

@novellizator
Copy link
Author

Uh-huuh. The npm request module I'm using does not send any data, when it gets ArrayBuffer in the body.
Is there any way within the browser to convert this arraybuffer to buffer so that I could send it via that function? https://github.com/dcodeIO/node-memcpy seems to be only for nodejs...

@dcodeIO
Copy link
Member

dcodeIO commented Jul 30, 2015

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).

@englercj
Copy link

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.

@novellizator
Copy link
Author

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.
Also, new Buffer(myArrayBuffer).length === 0 (the UInt8Array and node native buffer return the right non-zero length).

Any ideas? I guess I may try substituting request module for something else and then try the UInt8Array...

@englercj
Copy link

You need to pass a typed array to the buffer ctor:

new Buffer(new Uint8Array(myMessage.toBuffer()))

Also you should never do obj instanceof Buffer, you should only do Buffer.isBuffer(obj) which is what request does. The polyfil ensures this method works correctly.

@novellizator
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants