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

item.copy is not a function #7

Closed
bortexz opened this issue Sep 12, 2015 · 7 comments
Closed

item.copy is not a function #7

bortexz opened this issue Sep 12, 2015 · 7 comments

Comments

@bortexz
Copy link

bortexz commented Sep 12, 2015

Hi,

I'm using this module from webtorrent, and i get an error from this module when using webseeding (webconn of bittorrent-swarm uses this module). The error is in this piece of code:

return simpleGet(opts, function (err, res) {
    if (err) return cb(err)
    var chunks = []
    res.on('data', function (chunk) {
      chunks.push(chunk)
    })
    res.on('end', function () {
      cb(null, Buffer.concat(chunks), res)
    })
  })

Buffer.concat implementation uses item.copy() on every part of the list, except that if the length of chunks is 1, then it returns chunks[0]. In that case there's no error.

I think Buffer.concat expects array of Buffer or Buffer to be sent, but we're sending strings. I've got no error with the following code:

 res.on('data', function (chunk) {
      chunks.push(new Buffer(chunk))
 })

But anyway the webseed fails. I'm opening an issue in webtorrent too, will ref to this.

Any advice? Thanks!

@bortexz
Copy link
Author

bortexz commented Sep 17, 2015

It seems a problem with my config, i'll investigate why.

@bortexz bortexz closed this as completed Sep 17, 2015
@bortexz bortexz reopened this Sep 17, 2015
@bortexz
Copy link
Author

bortexz commented Sep 17, 2015

Ok i've been stuck on that and discovered that if i use stream-http i'm getting the data as a Uint8Array, but if i use simple-get i'm getting it as a string, and then fails.

This code with simple-get fails:

get('http://localhost:8002/torrent', function (err, res) {
      if (err) throw err
      console.log(res.statusCode) // 200
      var data = [] // List of Buffer objects

      res.on('data', function (chunk) {
        data.push(chunk) // Append Buffer object
      })

      res.on('end', function () {
        data = Buffer.concat(data) // Make one large Buffer of it
        console.log(data);
      })
    })

But this code with stream-http is working well

http.get('http://localhost:8002/torrent', function (res) {
      var data = [] // List of Buffer objects

      res.on('data', function (chunk) {
        data.push(chunk) // Append Buffer object
      })

      res.on('end', function () {
        data = Buffer.concat(data) // Make one large Buffer of it
      })
})

I don't understand why this is happening. I've seen that simple-get uses the node.js http with browserify, so i'm thinking that probably my node.js version has something to do with that. I've tried with 0.12.7, 0.12.4 and 4.1 and none of them worked well.

Any advice would be apprecieted, i've been on this error for days and i cannot move forward :S

Thanks

@feross
Copy link
Owner

feross commented Sep 21, 2015

You wouldn't happen to be using the --no-browser-field or --no-bf flags to browserify? These turn off package.json browser field resolution which would cause the unzip-response package to be included in browser builds, despite the declaration here: https://github.com/feross/simple-get/blob/master/package.json#L11

More info here: https://gist.github.com/defunctzombie/4339901

Also, what browser are you running your examples in?

@bortexz
Copy link
Author

bortexz commented Sep 21, 2015

Hi feross, thanks for your answer. No, i'm not using those flags, I use browserify with gulp and i'm using this config:

.pipe(browserify({
insertGlobals: true,
debug: true,
paths: ['client/'] // To make imports relative to this path, and save ourselfs from relative path hell
}))

I've tried latest version of Chrome and Firefox.

I'm now thinking on the paths: ['client/'] line, because i have a node.js backend and front end on the same project, and probably this is messing something up with browserify. The folder structure i'm using is the following:

/client
app/
components/
....
/server
/node_modules
package.json
gulpfile

If i understand well, I'm unzipping response when i shouldn't? That's why i'm seeing a string instead of a Buffer? That would explain a lot!

@feross
Copy link
Owner

feross commented Sep 21, 2015

If i understand well, I'm unzipping response when i shouldn't? That's why i'm seeing a string instead of a Buffer? That would explain a lot!

It was only a hunch. I still have no idea why this is happening. It would help if you could share a small snippet of code that causes this issue. Right now I can only guess what the problem is.

@feross
Copy link
Owner

feross commented Sep 21, 2015

Are you sure that you're using browserify 11?

@bortexz
Copy link
Author

bortexz commented Oct 1, 2015

Finally figured it out! I was using gulp-browserify, which is deprecated, and probably using an older versions of browserify.

@bortexz bortexz closed this as completed Oct 1, 2015
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

No branches or pull requests

2 participants