angel_static 2.1.3+2

  • Readme
  • Changelog
  • Example
  • Installing
  • 90

static #

Pub build status

Static server infrastructure for Angel.

Can also handle Range requests now, making it suitable for media streaming, ex. music, video, etc.

Installation #

In pubspec.yaml:

    angel_static: ^2.0.0-alpha

Usage #

To serve files from a directory, you need to create a VirtualDirectory. Keep in mind that angel_static uses package:file instead of dart:io.

import 'package:angel_framework/angel_framework.dart';
import 'package:angel_framework/http.dart';
import 'package:angel_static/angel_static.dart';
import 'package:file/local.dart';

main() async {
  var app = Angel();
  var fs = const LocalFileSystem();

  // Normal static server
  var vDir = VirtualDirectory(app, fs, source: Directory('./public'));

  // Send Cache-Control, ETag, etc. as well
  var vDir = CachingVirtualDirectory(app, fs, source: Directory('./public'));

  // Mount the VirtualDirectory's request handler

  // Start your server!!!
  await AngelHttp(app).startServer();

Push State #

VirtualDirectory also exposes a pushState method that returns a request handler that serves the file at a given path as a fallback, unless the user is requesting that file. This can be very useful for SPA's.

// Create VirtualDirectory as well
var vDir = CachingVirtualDirectory(...);

// Mount it

// Fallback to index.html on 404

Options #

The VirtualDirectory API accepts a few named parameters:

  • source: A Directory containing the files to be served. If left null, then Angel will serve either from web (in development) or build/web (in production), depending on your ANGEL_ENV.
  • indexFileNames: A List<String> of filenames that should be served as index pages. Default is ['index.html'].
  • publicPath: To serve index files, you need to specify the virtual path under which angel_static is serving your files. If you are not serving static files at the site root, please include this.
  • callback: Runs before sending a file to a client. Use this to set headers, etc. If it returns anything other than null or true, then the callback's result will be sent to the user, instead of the file contents.

2.1.3+1 #

  • Apply control flow lints.

2.1.3 #

  • Apply lints.
  • Pin to Dart >=2.0.0 <3.0.0.
  • Use at least version 2.0.0-rc.0 of angel_framework.

2.1.2+1 #

  • Fix a typo that prevented Range requests from working.

2.1.2 #

  • Patch support for range+streaming in Caching server.

2.1.1 #

  • URI-encode paths in directory listing. This produces correct URL's, always.

2.1.0 #

  • Include support for the Range header.
  • Use MD5 for etags, instead of a weak ETag.

2.0.2 #

  • Fixed invalid HTML for directory listings.

2.0.1 #

  • Remove use of sendFile.
  • Add a p.isWithin check to ensure that paths do not escape the source directory.
  • Handle HEAD requests.

2.0.0 #

  • Upgrade dependencies to Angel 2 + file@5.
  • Replace useStream with useBuffer.
  • Remove package:intl, just use HttpDate instead.

1.3.0+1 #

  • Dart 2 fixes.
  • Enable optionally writing responses to the buffer instead of streaming.

1.3.0 #

  • pushState uses strict mode when accepts is passed.

1.3.0-alpha+2 #

  • Added an accepts option to pushState.
  • Added optional directory listings.

1.3.0-alpha+1 #

  • ETags once again only encode the first 50 bytes of files. Resolves #27.

1.3.0-alpha #

  • Removed file transformers.
  • VirtualDirectory is no longer an AngelPlugin, and instead exposes a handleRequest middleware.
  • Added pushState to VirtualDirectory.

1.2.5 #

  • Fixed a bug where onlyInProduction was not properly adhered to.
  • Fixed another bug where Accept-Encoding was not properly adhered to.
  • Setting maxAge to null will now prevent a CachingVirtualDirectory from sending an Expires header.
  • Pre-built assets can now be mass-deleted with VirtualDirectory.cleanFromDisk(). Resolves #22.

1.2.4+1 #

Fixed a bug where Accept-Encoding was not properly adhered to.

1.2.4 #


  • MIME types will now default to application/octet-stream.
  • When streamToIO is true, the body will only be sent gzipped if the request explicitly allows it.

1.2.3 #

Fixed #40 and #41, which dealt with paths being improperly served when using a publicPath.


import 'package:angel_framework/angel_framework.dart';
import 'package:angel_framework/http.dart';
import 'package:angel_static/angel_static.dart';
import 'package:file/local.dart';
import 'package:logging/logging.dart';

main(List<String> args) async {
  var app = Angel();
  var http = AngelHttp(app);
  var fs = const LocalFileSystem();
  var vDir = CachingVirtualDirectory(
    allowDirectoryListing: true,
    source: args.isEmpty ? fs.currentDirectory :[0]),
    maxAge: const Duration(days: 24).inSeconds,

    ..addExtension('', 'text/plain')
    ..addExtension('dart', 'text/dart')
    ..addExtension('lock', 'text/plain')
    ..addExtension('markdown', 'text/plain')
    ..addExtension('md', 'text/plain')
    ..addExtension('yaml', 'text/plain');

  app.logger = Logger('example')
    ..onRecord.listen((rec) {
      if (rec.error != null) print(rec.error);
      if (rec.stackTrace != null) print(rec.stackTrace);

  app.fallback((req, res) => throw AngelHttpException.notFound());

  var server = await http.startServer('', 3000);
  print('Serving from ${vDir.source.path}');
  print('Listening at http://${server.address.address}:${server.port}');

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  angel_static: ^2.1.3+2

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:angel_static/angel_static.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Apr 8, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
angel_framework ^2.0.0-rc.0 2.1.1
convert ^2.0.0 2.1.1
crypto ^2.0.0 2.1.4
file ^5.0.0 5.1.0
http_parser ^3.0.0 3.1.4
path ^1.4.2 1.6.4
range_header ^2.0.0 2.0.2+2
Transitive dependencies
angel_container 1.1.0
angel_http_exception 1.1.0
angel_model 1.0.3
angel_route 3.1.0+1
async 2.4.1
charcode 1.1.3
code_buffer 1.0.1
collection 1.14.12
combinator 1.1.0
dart2_constant 1.0.2+dart2
http2 1.0.0
http_server 0.9.8+3
intl 0.16.1
merge_map 1.0.2
meta 1.1.8
mime 0.9.6+3
mock_request 1.0.6
quiver 2.1.3
quiver_hashcode 2.0.0
recase 2.0.1 3.0.0
source_span 1.7.0
stack_trace 1.9.3
string_scanner 1.0.5
term_glyph 1.1.0
tuple 1.0.3
typed_data 1.1.6
uuid 2.0.4
Dev dependencies
angel_test ^2.0.0-alpha
http any
logging ^0.11.0 0.11.4
matcher ^0.12.0 0.12.6
pedantic ^1.0.0
test ^1.0.0