shelf_gzip 4.0.1 copy "shelf_gzip: ^4.0.1" to clipboard
shelf_gzip: ^4.0.1 copied to clipboard

Shelf middleware to GZIP encoding responses, with compression level and compression scope by content-type.

shelf_gzip #

pub package Null Safety Dart CI GitHub Tag New Commits Last Commits Pull Requests Code size License

Shelf middleware to GZIP encoding responses, with compression level and compression scope by content-type.

Usage #

import 'dart:async' show runZonedGuarded;
import 'dart:io';

import 'package:path/path.dart' show join, dirname;
import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf/shelf_io.dart' as shelf_io;
import 'package:shelf_gzip/shelf_gzip.dart';
import 'package:shelf_static/shelf_static.dart';

void main() {
  // Assumes the server lives in bin/ and that `pub build` ran
  var pathToBuild =
  join(dirname(Platform.script.toFilePath()), '..', 'build/web');
  var staticHandler =
  createStaticHandler(pathToBuild, defaultDocument: 'index.html');

  var portEnv = Platform.environment['PORT'];
  var port = portEnv == null ? 9999 : int.parse(portEnv);

  runZonedGuarded(() async {
    var handler = const shelf.Pipeline()
            .addMiddleware(gzipMiddleware)
            .addHandler(staticHandler);

    await shelf_io.serve(handler, '0.0.0.0', port);

    print("Serving $pathToBuild on port $port");
  }, (e, stackTrace) => print('Server error: $e $stackTrace'));
}

When not compress #

The gzip encoding won't be applied if:

  • The Content-Type is for an already compressed type. See isAlreadyCompressedContentType.

  • A small response body (length < 512) will not benefit from being compressed.

Compression Level #

The default gzip encoder compression level is set to 4, since this is the recommended level for live compression (not stored file compression). A level 4 compression has the best trade off for text/code content and CPU usage.

  • The original default gzip encoder compression level is 6.

Features and bugs #

Please file feature requests and bugs at the issue tracker.

Authors #

License #

BSD-3-Clause License

6
likes
150
pub points
81%
popularity

Publisher

verified publisherjpryan.me

Shelf middleware to GZIP encoding responses, with compression level and compression scope by content-type.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

shelf

More

Packages that depend on shelf_gzip