diff --git a/.gitignore b/.gitignore index 917abe7c..ced607a7 100644 --- a/.gitignore +++ b/.gitignore @@ -63,3 +63,4 @@ packages # Include when developing application packages. pubspec.lock +.dart_tool \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 80f84867..389d8478 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 2.0.0 +* Upgrade dependencies to Angel 2 + file@5. +* Replace `useStream` with `useBuffer`. + # 1.3.0+1 * Dart 2 fixes. * Enable optionally writing responses to the buffer instead of streaming. diff --git a/README.md b/README.md index 343a327b..760b9773 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ In `pubspec.yaml`: ```yaml dependencies: - angel_static: ^1.3.0-alpha + angel_static: ^2.0.0-alpha ``` # Usage diff --git a/example/main.dart b/example/main.dart index 593f2551..4bbee909 100644 --- a/example/main.dart +++ b/example/main.dart @@ -12,7 +12,7 @@ main() async { allowDirectoryListing: true, source: fs.directory(fs.currentDirectory), ); - app.use(vDir.handleRequest); + app.fallback(vDir.handleRequest); var server = await http.startServer('127.0.0.1', 3000); print('Listening at http://${server.address.address}:${server.port}'); diff --git a/lib/src/cache.dart b/lib/src/cache.dart index 4dc7020a..acb52b06 100644 --- a/lib/src/cache.dart +++ b/lib/src/cache.dart @@ -59,7 +59,7 @@ class CachingVirtualDirectory extends VirtualDirectory { this.onlyInProduction: false, this.useEtags: true, bool allowDirectoryListing, - bool useStream, + bool useBuffer, String publicPath, callback(File file, RequestContext req, ResponseContext res)}) : super(app, fileSystem, @@ -68,7 +68,7 @@ class CachingVirtualDirectory extends VirtualDirectory { publicPath: publicPath ?? '/', callback: callback, allowDirectoryListing: allowDirectoryListing, - useStream: useStream); + useBuffer: useBuffer); @override Future serveFile( diff --git a/lib/src/virtual_directory.dart b/lib/src/virtual_directory.dart index 8c795753..1ba07ce9 100644 --- a/lib/src/virtual_directory.dart +++ b/lib/src/virtual_directory.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'package:angel_framework/angel_framework.dart'; import 'package:file/file.dart'; +import 'package:http_parser/http_parser.dart'; import 'package:mime/mime.dart'; import 'package:path/path.dart' as p; @@ -49,7 +50,7 @@ class VirtualDirectory { /// If `true` (default: `true`), then files will be opened as streams and piped into the request. /// /// If not, the response buffer will be used instead. - final bool useStream; + final bool useBuffer; VirtualDirectory(this.app, this.fileSystem, {Directory source, @@ -57,7 +58,7 @@ class VirtualDirectory { this.publicPath: '/', this.callback, this.allowDirectoryListing: false, - this.useStream: true}) { + this.useBuffer: true}) { _prefix = publicPath.replaceAll(_straySlashes, ''); if (source != null) { _source = source; @@ -83,7 +84,7 @@ class VirtualDirectory { /// You can also limit this functionality to specific values of the `Accept` header, ex. `text/html`. /// If [accepts] is `null`, OR at least one of the content types in [accepts] is present, /// the view will be served. - RequestMiddleware pushState(String path, {Iterable accepts}) { + RequestHandler pushState(String path, {Iterable accepts}) { var vPath = path.replaceAll(_straySlashes, ''); if (_prefix?.isNotEmpty == true) vPath = '$_prefix/$vPath'; @@ -120,12 +121,12 @@ class VirtualDirectory { /// Writes the file at the path given by the [stat] to a response. Future serveStat(String absolute, String relative, FileStat stat, RequestContext req, ResponseContext res) async { - if (stat.type == FileSystemEntityType.DIRECTORY) + if (stat.type == FileSystemEntityType.directory) return await serveDirectory( fileSystem.directory(absolute), relative, stat, req, res); - else if (stat.type == FileSystemEntityType.FILE) + else if (stat.type == FileSystemEntityType.file) return await serveFile(fileSystem.file(absolute), stat, req, res); - else if (stat.type == FileSystemEntityType.LINK) { + else if (stat.type == FileSystemEntityType.link) { var link = fileSystem.link(absolute); return await servePath(await link.resolveSymbolicLinks(), req, res); } else @@ -144,7 +145,7 @@ class VirtualDirectory { } if (allowDirectoryListing == true) { - res.headers['content-type'] = 'text/html'; + res.contentType = new MediaType('text', 'html'); res ..write('') ..write('') @@ -226,12 +227,12 @@ class VirtualDirectory { var type = lookupMimeType(file.path) ?? 'application/octet-stream'; _ensureContentTypeAllowed(type, req); - res.headers['content-type'] = type; + res.contentType = new MediaType.parse(type); - if (useStream != true) { - res.buffer.add(await file.readAsBytes()); + if (useBuffer == true) { + await res.sendFile(file); } else { - await file.openRead().pipe(res); + await res.streamFile(file); } return false; diff --git a/pubspec.yaml b/pubspec.yaml index 8265af4a..c30ec1c3 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,16 +4,15 @@ environment: sdk: ">=1.8.0 <3.0.0" homepage: https://github.com/angel-dart/static author: Tobe O -version: 1.3.0+1 +version: 2.0.0-alpha dependencies: - angel_framework: ^1.1.0-alpha - dart2_constant: ^1.0.0 - file: ^2.0.0 + angel_framework: ^2.0.0-alpha + file: ^5.0.0 intl: ">=0.0.0 <1.0.0" mime: ^0.9.3 path: ^1.4.2 dev_dependencies: - angel_test: ^1.1.0-alpha + angel_test: ^2.0.0-alpha http: ^0.11.3 - mustache4dart: ^1.1.0 - test: ^0.12.13 + mustache4dart: ^3.0.0-dev.0.0 + test: ^1.0.0 diff --git a/test/all_test.dart b/test/all_test.dart index 5178ffc3..971ab023 100644 --- a/test/all_test.dart +++ b/test/all_test.dart @@ -18,21 +18,21 @@ main() { http = new AngelHttp(app); app.logger = new Logger('angel')..onRecord.listen(print); - app.use( + app.fallback( new VirtualDirectory(app, const LocalFileSystem(), source: testDir, publicPath: '/virtual', indexFileNames: ['index.txt']).handleRequest, ); - app.use( + app.fallback( new VirtualDirectory(app, const LocalFileSystem(), source: testDir, - useStream: false, + useBuffer: true, indexFileNames: ['index.php', 'index.txt']).handleRequest, ); - app.use('Fallback'); + app.fallback((req, res) => 'Fallback'); app.dumpTree(showMatchers: true); diff --git a/test/cache_sample.dart b/test/cache_sample.dart index 86f0e09a..444054fe 100644 --- a/test/cache_sample.dart +++ b/test/cache_sample.dart @@ -10,7 +10,7 @@ main() async { app = new Angel(); http = new AngelHttp(app); - app.use( + app.fallback( new CachingVirtualDirectory(app, const LocalFileSystem(), source: testDir, maxAge: 350, @@ -18,7 +18,7 @@ main() async { indexFileNames: ['index.txt']).handleRequest, ); - app.get('*', 'Fallback'); + app.get('*', (req, res) => 'Fallback'); app.dumpTree(showMatchers: true); diff --git a/test/cache_test.dart b/test/cache_test.dart index 285e01a2..8fea55cc 100644 --- a/test/cache_test.dart +++ b/test/cache_test.dart @@ -18,14 +18,14 @@ main() { app = new Angel(); http = new AngelHttp(app); - app.use( + app.fallback( new CachingVirtualDirectory(app, const LocalFileSystem(), source: testDir, maxAge: 350, onlyInProduction: false, //publicPath: '/virtual', indexFileNames: ['index.txt']).handleRequest, ); - app.get('*', 'Fallback'); + app.get('*', (req, res) => 'Fallback'); app.dumpTree(showMatchers: true); diff --git a/test/issue41_test.dart b/test/issue41_test.dart index 5db45080..e4bd0938 100644 --- a/test/issue41_test.dart +++ b/test/issue41_test.dart @@ -26,7 +26,7 @@ main() async { var app = new Angel(); app.logger = new Logger('angel')..onRecord.listen(print); - app.use( + app.fallback( new VirtualDirectory(app, const LocalFileSystem(), source: swaggerUiDistDir, publicPath: 'swagger/') .handleRequest, diff --git a/test/push_state_test.dart b/test/push_state_test.dart index 8d141071..8aede873 100644 --- a/test/push_state_test.dart +++ b/test/push_state_test.dart @@ -28,9 +28,9 @@ main() { ); app - ..use(vDir.handleRequest) - ..use(vDir.pushState('index.html')) - ..use('Fallback'); + ..fallback(vDir.handleRequest) + ..fallback(vDir.pushState('index.html')) + ..fallback((req, res) => 'Fallback'); app.logger = new Logger('push_state') ..onRecord.listen(