1

I have a process uploading files into mongodb which works fine. But I have one big problem. If for some reason the connection is lost (the client browser closed, bad internet connection, power is down etc.), I have no indication and busboy/req streaming fires no event which result a promise not handled error after some time. When I'm testing it by uploading and closing the browser immediately. I see that few files have been uploaded to mongodb. Something like 10 out of 100 files in the batch. I have no indication if the last one was fully uploaded and no event to respond with resolve/reject. Code:

static upload (req) {
    return new Promise((resolve, reject) => {

        const docIds = [];
        const busboy = new Busboy({ headers: req.headers });

        const gridFSBucket = MongoDb.GridFSBucket({
            chunkSizeBytes: 64 * 1024,
            bucketName: 'bucketFiles',
            writeConcern: {w: 1}
        });

        busboy.on('file', function (fieldName, file, fileName, encoding, mimeType) {

            const bucketStream = gridFSBucket.openUploadStream(fileName, { metadata: { encoding,  mimeType} });

            file.on('error', function () {
                // Never hitting that
                reject(error);
            });
            file.pipe(bucketStream)
                .on('error', function (error) {
                    // Never hitting that
                    reject(error);
                })
                .on('finish', function (doc) {
                    docIds.push(doc._id);
                });
        });
        busboy.on('error', function (error) {
            // Never hitting that
            reject(error);
        });
        busboy.on('finish', function () {
            //Everything uploaded BUT
            //if connection lost never hitting that
            resolve(docIds);
        });

        req.on('error', function (error) {
            // Never hitting that
            reject(error);
        });
        req.pipe(busboy);
    });
}
Itay Merchav
  • 954
  • 8
  • 8

1 Answers1

2

I just find it out. To know if unexpected stream/request end occurred, you need to listen to req.connection('error' ...) event, like:

req.connection.on('error', function (error) {
    //do something like cancelling the mongodb session ...
    reject(error)
});
Itay Merchav
  • 954
  • 8
  • 8