简体   繁体   中英

How to close download stream on cancel in REST API made with HAPI

I have implemented an Rest API with HAPI which provides an CSV download from an database. When I start the download with an wget http://localhost/csv/download and cancel it in the middle of the download the server keeps pushing the stream until the end.

Code

async function downloadCsvHandler(request, responseToolkit) {
    let responseStream = new Transform({
        allowHalfOpen: false,
        writableObjectMode: true,
        transform: convertJsonToCsvMethod
    })
    return responseToolkit.response(responseStream)
}

Question

Why HAPI does not close the stream even when the download was cancelled?

The problem was that I used stream.pipeline to merge my streams into one, which caused the stream to start though HAPI has not started to consume. By using stream1.pipe(stream2) the streaming is not activated until HAPI is reading from it which also causes the stream to stop when HAPI stops.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM