mafintosh / end-of-stream

Call a callback when a readable/writable/duplex stream has completed or failed.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

"premature close" error not thrown in node 8.2.0 or newer

trygve-lie opened this issue · comments

There has been a subtile change in node 8.2.0 and newer (nodejs/node#14024) which affect this module.

In a situation where a outgoing http stream is terminated prematurly, this module will pass on an error object on close in node versions 8.1.4 and older. In node version 8.2.0 this module will not pass on an error object.

Here is an example:

const { get, createServer } = require('http');
const eos = require('end-of-stream');

let external;

// Http server
createServer((req, res) => {
    res.writeHead(200);
    setTimeout(() => {
        external.abort();
        res.end('Hello World\n');
    }, 1000);
}).listen(3000);

// Proxy server
createServer((req, res) => {
    get('http://127.0.0.1:3000', inner => {
        eos(res, (err) => {
            console.log('stream ended', err);
        });
        inner.pipe(res);
    });
}).listen(3001, () => {
    external = get('http://127.0.0.1:3001');
});

In node version 8.1.4 or older this will output:

stream ended Error: premature close
    at ServerResponse.onclose (/home/trygve/Work/podium-2/podium-client/node_modules/end-of-stream/index.js:49:67)
    at emitNone (events.js:110:20)
    at ServerResponse.emit (events.js:207:7)
    at Socket.onServerResponseClose (_http_server.js:153:44)
    at emitOne (events.js:120:20)
    at Socket.emit (events.js:210:7)
    at TCP._handle.close [as _onclose] (net.js:546:12)

In node version 8.2.0 or newer the output will just be undefined.

This is due to a corrected error in node so I am not sure if this is expected behaviour. The stream are still terminated prematurely so I do kinda expect the same Error from this module in node version 8.2.0 and newer.

See also: nodejs/node#15029

The change in node is being reverted (nodejs/node#15404), so this can be closed

Glad to see it fixed :)