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

Uncaught Error: data must be a buffer, was: {} #30

Open
austinfrey opened this issue Nov 19, 2020 · 2 comments · May be fixed by #31
Open

Uncaught Error: data must be a buffer, was: {} #30

austinfrey opened this issue Nov 19, 2020 · 2 comments · May be fixed by #31

Comments

@austinfrey
Copy link

I am using muxrpc to communicate over a websocket from the browser to a local node process. (I am using browserify) When using the client in the browser, I am seeing this error when trying to call an rpc method: Uncaught Error: data must be a buffer, was: {}.

It looks like pull-reader throws this error here when checking if the response data is a buffer. In the browser, these responses are coming back as Blob objects, not buffers (not sure why though). The code below works fine when used serverside, so my hunch is the browser is doing some type of coercion on the data to convert it to a Blob. I ran a quick test to see if converting the blob to a buffer works and it seems too. Could a check be added to test if if (data instanceof Blob) convertBlobToBuf(data)?

We could do it in this module on this line: https://github.com/pull-stream/pull-ws/blob/master/source.js#L31

Or is there a better way to handle this. I can handle a PR but not sure which module a check like this should go, and also, maybe I'm just doing something wrong since I haven't seen any other GH issues reference this problem. @arj03 You are doing quite a bit on the browserside, have you ever seen this error?

Reproduce with this code.
client.js (browserify client.js -o bundle.js)

const {pull} = require('pull-stream')
const connect = require('pull-ws/client')
const MRPC = require('muxrpc')

const manifest = {
  hello: 'async'
}

connect('ws://localhost:3333', function (err, stream) {
  const mrpc = MRPC(manifest, null) ()
  if(err) console.log(err) //handle err
  pull(
    stream, 
    mrpc.createStream(), 
    stream
  )
  
  mrpc.hello('armor', function (err, outArgs) {
    if (err) {
      return console.error(err)
    }
    console.log(outArgs)
  })
})

server.js

const {pull} = require('pull-stream')
const createServer = require('pull-ws/server')
const MRPC = require('muxrpc')


const manifest = {
  hello: 'async'
}

const api = {
  hello: (name, cb) => cb(null, 'Hello ' + name)
}

createServer(function (stream) {
  const mrpc = MRPC(null, manifest) (api)

  pull(
    stream,
    mrpc.createStream(),
    stream
  )
}).listen(3333)
@austinfrey austinfrey linked a pull request Nov 19, 2020 that will close this issue
@arj03
Copy link

arj03 commented Nov 19, 2020

Hey Austin

I havn't run into this problem, not sure but maybe its because I havn't run async functions over muxrpc, only streams.

@austinfrey
Copy link
Author

austinfrey commented Nov 19, 2020

thanks @arj03, i did run into the same problem with stream so 🤷‍♂️ i submitted a PR but not sure I solved this the correct way.

I did see this comment at the top of source.js that made me think perhaps someone else has had a similar issue.

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

Successfully merging a pull request may close this issue.

2 participants