Skip to content

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

Open
@austinfrey

Description

@austinfrey

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions