stream-utils / raw-body

Get and validate the raw body of a readable stream

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error: Cannot switch to old mode now.

fabdrol opened this issue · comments

I'm using https://github.com/balderdashy/skipper to upload files in a sails js application, which gave the error Error: Cannot switch to old mode now. After some digging, I found out that the error is fixed by commenting out line #33 in stream-utils/raw-body/index.js. It seems to be related to this issue: https://github.com/balderdashy/skipper/issues/10.

While I'm convinced this is an issue related to balderdashy/skipper specifically, I'm posting this here anyway - maybe you guys can shed some light on this issue.

Cheers, Fabian

you're probably using the stream somewhere else as well, which you shouldn't be doing (except for maybe pipes)

@fabdrol can you post what "line 33" refers to? Right is it is if (options.length != null && !isNaN(options.length)), but I don't think that is what you are referring to in this case.

Sorry, it's line 40/41 in the code in the repo:

 if (typeof stream.pause === 'function')
      stream.pause()

I think I see the problem. It looks like skipper is setting the steam into the "new mode" and then passing it to this module, which cannot then call pause() since that is an old-mode method. Basically the issue boils down to you cannot pass in streams that have been switched into the "new mode" in node.js 0.10, since this module supports node.js 0.8.

Actually, sorry, re-opening.

So what we need to do here is determine if the stream is in old mode or not. If it is in old mode, we need to pause() is on error, but if it is in new mode, we do not have to do anything but remove the data listeners because it'll automatically pause.

@fabdrol I am working on a fix. It doesn't look like skipper is using raw-body directly, but rather an old connect that is indirectly using this module, so even when I fix this here, skipper will need to do a lot of updates to pull in the fix.

Nevermind, I was wrong on why the error is cause. It's because req is already being piped somewhere else before it gets handed to this module. It is an error in skipper; I will post it over there. We could certainly have a better error come out of this module, though.

@dougwilson hmm, okay. Good to know some background. Is the issue specific to the older version, or would a newer version have the same problems?

All versions have the same problem. It's an issue with now skipper is trying to pipe the request multiple times if the request did not come in with a known Content-Type header.

Ps, if this is not an issue with this module but with skipper, we can close this right?

@fabdrol I was going to close it when I made this module provide a better response to the situation, but we can close it regardless, because yes, the issue you reported is cause by what skipper is doing and needs to be fixed there regardless.

@dougwilson okay, thanks for your help anyway!

No problem. I just posted a possible fix to skipper on that thread you referenced earlier, by the way. I didn't actually test to confirm that it works, but I'm pretty sure it will.