文件下载和客户端断开连接?

时间:2016-05-29 10:31:46

标签: javascript node.js express

给出以下代码:

async function (req, res, next) {
  const fd = await fs.open("myfile.txt")
  fs.createReadStream(null, { fd, autoClose: false })
    .on('error', next)
    .on('end', () => fs.close(fd))
    .pipe(res)
}

如果客户端在下载整个文件之前断开连接会怎样?即仍然会调用end或文件描述符泄露,如果是,我该如何解决?如果调用`end'这意味着即使客户端不再侦听整个文件也会被读取?

注意,我知道我可以在没有fd的情况下执行此操作,但这是对更复杂代码的简化。

1 个答案:

答案 0 :(得分:2)

看起来文件不会被关闭,或者至少不会调用你的end事件处理程序。

您可以为此创建测试,但这是我的:

const http = require('http');
const fs   = require('fs');

var server = http.createServer(function (req, res) {
    console.log('server request received');
    const fd = fs.openSync('myfile.txt', 'r');
    fs.createReadStream(null, { fd, autoClose: false })
        .on('error', err => {
            console.error('file error', err);
        })
        .on('end', () => {
            console.log('file end');
            fs.close(fd);
        })
        .on('close', () => {
            console.log('file closed');
        })
        .pipe(res)
            .on('error', err => console.error('server stream error', err))
            .on('end', () => console.log('server stream end'))
            .on('close', () => console.log('server stream closed'))
    ;
});

server.listen(0, 'localhost', () => {
    const address = server.address();

    var req = http.request({port: address.port}, function (res) {
        res.on('data', data => {
            console.log('response data');
            // Comment next line to read whole file.
            req.abort();
        });
        res.on('end', () => {
            console.log('response ended');
            setTimeout(() => server.close(), 1000);
        });
        req.once('abort', () => {
            console.log('request aborted');
        });
    });

    req.end(() => {
        console.log('request sent');
    });
});

我删除了异步/等待运行测试的简单性。

调用req.abort()时,我得到以下输出:

request sent
server request received
response data
request aborted
response ended
server stream closed

评论该行后,允许代码读取整个文件:

request sent
server request received
response data
response data
response data
response data
response data
response data
file end
response data
response ended

要创建测试myfile.txt,我使用了以下命令:

dd if=/dev/zero of=myfile.txt bs=1k count=200