Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot stop an operation or limit max file size to avoid crashing the server #98

Open
dancherb opened this issue Mar 21, 2024 · 0 comments

Comments

@dancherb
Copy link

dancherb commented Mar 21, 2024

As I stream the server to the user's browser via Express, I monitor the ZIP size and call stream.destroy() and res.end() to end the operation if it exceeds the max file size.

This ends the stream to the user's browser and causes them to recieve a corrupt ZIP (no problem, we'll send them an email alert explaining what happened).

However - under the hood, it seems like s3-zip continues its operation and memory continues to fill up. In a production environment on Heroku, our web app crashes way before stream.destroy is even called due to memory limits.

How can we implement a max file size, or stop all operations if the ZIP being built reaches a certain size? Otherwise, any implementation of S3-zip makes a server easy susceptible to overloading and crashing.

Thanks!

import s3_zip from 's3-zip'

const maxSizeBytes = 300 * 1024 * 1024

const stream = s3_zip.archive(
        { s3, bucket: process.env.S3_BUCKET, debug: true }, 
        '', 
        sourcePaths, 
        finalPaths
    );
    
    let currentSize = 0;
    
    stream.on('data', (data) => {
        currentSize += data.length;
        
        if(currentSize > maxSizeBytes) { 
            stream.destroy(new Error('ZIP file size exceeds limit'));
        }
    });

    stream.on('error', (error) => {
        console.error('ZIP generation error:', error);
        
        res.end()
    });

    pipeline(stream, res, (error) => {
        if(error) {
            console.log('pipeline error =', error)
        } else {
            console.log('pipeline success');
        }
    });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant