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

feat: automatic compression #81

Open
frattaro opened this issue Jun 18, 2019 · 2 comments
Open

feat: automatic compression #81

frattaro opened this issue Jun 18, 2019 · 2 comments

Comments

@frattaro
Copy link

frattaro commented Jun 18, 2019

This is a:

  • [✓] Feature request or change
  • [] Bug report

For feature requests or changes:

Current behavior (if any)

Specified files are uploaded as-is.

Proposed behavior

Option to compress before sending.

Justification

I turn off manage resources because I'm already creating the bucket in the serverless.yml. serverless-finch is very useful to specify a source directory and move only those files. Can an option be added to pre-compress the files, deal with what headers need to be dealt with?

I guess you'd want to support gzip and brotli

@frattaro frattaro changed the title feat: automatic gzip feat: automatic compression Jun 18, 2019
@sprockow
Copy link
Contributor

As an alternative, I would potentially suggest that the compression is done outside of serverless-finch by another build/tooling process. There are too many options that would probably need to be considered as to how to configure the compression, plus lots of various compression implementations to choose from.

For example, I was able to quickly accomplish this using a couple of well supported webpack plugins:

[
    new CompressionPlugin({
      filename: '[path][base].br',
      algorithm: 'brotliCompress',
      test: /\.(js|css|html|svg)$/,
      compressionOptions: {
        params: {
          [zlib.constants.BROTLI_PARAM_QUALITY]: 5,
        },
      },
      threshold: 10240,
      minRatio: 0.8,
    }),

    new CompressionPlugin({
      filename: '[path][base].gz',
      algorithm: 'gzip',
      test: /\.js$|\.css$|\.html$/,
      threshold: 10240,
      minRatio: 0.8,
    })
  ]

This will output a copy of the uncompressed files both in gzip and brotli format, with .gz and .br file endings.

That said, there is still the problem with serverless-finch. The new compression file suffixes will break the mime lookup logic. I was suddenly dealing with the prospect of creating dozens of rules for each compression + file-type (ie css vs js vs png vs..etc).

Came up with a potential solution that I could turn into a PR if anyone is interested. This helper function will use the existing mime lookup library used by serverless-finch and will return the appropriate content-encoding header as well.

function getMimeTypeAndContentEncoding(filePath) {
  const match = /(.+\..+)\.(gz|br)/.exec(filePath);

  if (match) {
    const [fullMatch, strippedFilePath, encodingFileEnding] = match;

    return {
      ContentType: mime.lookup(strippedFilePath),
      ContentEncoding: ContentEncodingMap[encodingFileEnding]
    };
  }

  return { ContentType: mime.lookup(filePath) };
}

@sprockow
Copy link
Contributor

sprockow commented Jan 23, 2022

There seems to be a little bit of activity in this repo again, so I'm revisiting this after a long absence. I rebased my PR #109 and fixed merge conflicts. I also added those README changes promised.

Is this a feature we'd like in this repo? I've been using this myself for a while now using my fork. I'd love to get this contributed upstream.

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

2 participants