node.jsgoogle-cloud-functionsnode-archiver

Cloud Function response stream


I have a script that is building a zip file on demand. It's working great locally using the Cloud Functions Local Emulator. When deployed, it's not streaming the response. The whole zip response is built, then sent. I know this is happening because the content-length response header is being set, which is not happening locally.

How can I get Cloud Functions to stream the response?

exports.zip = (req, res) => {
    const zipRequest = {
        media: [{
            'url': 'https://storage.googleapis.com/...',
            'file': 'file1.jpg'
        }, {
            'url': 'https://storage.googleapis.com/...',
            'file': 'file2.jpg'
        }, {
            'url': 'https://storage.googleapis.com/...',
            'file': 'file3.jpg'
        }],
        filename: 'photos.zip'
    };

    res.attachment(zipRequest.filename);
    res.setHeader('Content-Type', 'application/zip');

    let zip = Archiver('zip');
    zip.on('end', () => {
      res.end();
    });

    zip.pipe(res);
    zipRequest.media.forEach((file) => {
        zip.append(request.get(file.url), { name: file.name });
    });

    zip.finalize();
};

Solution

  • Currently there is no way to stream the response as it will be buffering the response due to how the infrastructure is implemented and there's no way around it right now, which means it would not see the results until the response is closed.