Readable.on ('end', ...) never starts

I try to transfer some sound to my server and then transfer it to the service specified by the user, the user will provide me with someHostName , which sometimes cannot support this type of request.

My problem is that when this happens, clientRequest.on('end',..) never starts, I think this is because it was sent to someHostReq , which got messed up when someHostName is "wrong".

My question is:

Is there anyway that I can still have clientRequest.on('end',..) , even if the clientRequest thread has something wrong?

If not: how did I find that something happened to someHostReq "immediately"? someHostReq.on('error') does not start, except after a while.

code:

  someHostName = 'somexample.com' function checkIfPaused(request){//every 1 second check .isPaused console.log(request.isPaused()+'>>>>'); setTimeout(function(){checkIfPaused(request)},1000); } router.post('/', function (clientRequest, clientResponse) { clientRequest.on('data', function (chunk) { console.log('pushing data'); }); clientRequest.on('end', function () {//when done streaming audio console.log('im at the end'); }); //end clientRequest.on('end',) options = { hostname: someHostName, method: 'POST', headers: {'Transfer-Encoding': 'chunked'} }; var someHostReq = http.request(options, function(res){ var data = '' someHostReq.on('data',function(chunk){data+=chunk;}); someHostReq.on('end',function(){ console.log('someHostReq.end is called'); }); }); clientRequest.pipe(someHostReq); checkIfPaused(clientRequest); }); 

output:

if the hostname is correct:

  pushing data . . pushing data false>>> pushing data . . pushing data pushing data false>>> pushing data . . pushing data console.log('im at the end'); true>>> //continues to be true, that fine 

in case of an invalid host name:

  pushing data . . pushing data false>>>> pushing data . . pushing data pushing data false>>>> pushing data . . pushing data true>>>> true>>>> true>>>> //it stays true and clientRequest.on('end') is never called //even tho the client is still streaming data, no more "pushing data" appears 

if you think my question is a duplicate:

You can switch to the current mode by doing one of the following:

Adding a 'data' event handler to listen to data.

Call the resume () method to open the stream explicitly.

Call the pipe () method to send data to Writable.

source: https://nodejs.org/api/stream.html#stream_class_stream_readable

+5
source share
1 answer

The behavior in the case of an incorrect host name seems to be a problem for buffers, if the destination stream buffer is full (because someHost does not receive pending pieces of data), the pipe will not continue to read the original stream, because the channel will automatically control the stream. Since the pipe does not read the source stream, you never reach the "end" event.

Anyway, can I still have clientRequest.on ('end', ..) even if the clientRequest streams have something wrong with this?

The end event will not fire unless the data is completely destroyed. To get the "end" caused by a suspended stream, you need to call resume() (first drop from the wrong hostname or fall back into the buffer) to set the steam in flowMode or read() again to the end.

But how to determine when I should have done any of the above?

someHostReq.on ('error') is a natural place, but if it starts too long:

First, try setting a low timeout request (less than someHostReq.on ('error') takes a trigger, as it seems to you too much time) request.setTimeout(timeout[, callback]) and check if it works with the correct host name. If it works, just use the callback or timeout event to determine when the server time is Out and use one of the above methods to get to the end.

If the timeOut solution does not work or does not meet your requirements, you should play with flags in clientRequest.on('data') , clientRequest.on('end') and / or clienteRequest.isPaused to guess when you are stuck in the buffer. When you think you're stuck, apply one of the methods above to get to the end of the stream. Fortunately, it takes less time to find the buffer than waiting for someHostReq.on('error') (maybe two request.isPaused() = true without reach ' data' events are enough to determine if you are stuck).

How did I find that something happened to someHostReq "immediately"? someHostReq.on ('error') does not start, except after some time.

Errors are triggered when triggered. You cannot detect this immediately. ¿Why not just send a beacon confirmation request to verify support before pipeline flows? Some kind of:

"Check service specified by the user ..." If OK → Flow of requesting user pipes to OR FAIL service → Notify user of incorrect service.

+1
source

Source: https://habr.com/ru/post/1239540/


All Articles