22// Use of this source code is governed by a BSD-style license that can be
33// found in the LICENSE file.
44
5- // @dart = 2.9
6-
75// Note: this is a copy from flutter tools, updated to work with dwds tests
86
97import 'dart:async' ;
108import 'dart:convert' ;
119import 'dart:io' ;
1210import 'dart:typed_data' ;
1311
14- import 'package:dwds/dwds .dart' ;
12+ import 'package:dwds/asset_reader .dart' ;
1513import 'package:file/file.dart' ;
1614import 'package:logging/logging.dart' ;
1715import 'package:mime/mime.dart' as mime;
@@ -32,10 +30,10 @@ class TestAssetServer implements AssetReader {
3230 static const String _defaultMimeType = 'application/octet-stream' ;
3331 final FileSystem _fileSystem;
3432 final HttpServer _httpServer;
35- final Map <String , Uint8List > _files = < String , Uint8List > {};
36- final Map <String , Uint8List > _sourcemaps = < String , Uint8List > {};
37- final Map <String , Uint8List > _metadata = < String , Uint8List > {};
38- String _mergedMetadata;
33+ final Map <String , Uint8List > _files = {};
34+ final Map <String , Uint8List > _sourceMaps = {};
35+ final Map <String , Uint8List > _metadata = {};
36+ late String _mergedMetadata;
3937 final PackageConfig _packageConfig;
4038 final InternetAddress internetAddress;
4139
@@ -47,6 +45,15 @@ class TestAssetServer implements AssetReader {
4745 this ._fileSystem,
4846 ) : basePath = _parseBasePathFromIndexHtml (index);
4947
48+ bool hasFile (String path) => _files.containsKey (path);
49+ Uint8List getFile (String path) => _files[path]! ;
50+
51+ bool hasSourceMap (String path) => _sourceMaps.containsKey (path);
52+ Uint8List getSourceMap (String path) => _sourceMaps[path]! ;
53+
54+ bool hasMetadata (String path) => _metadata.containsKey (path);
55+ Uint8List getMetadata (String path) => _metadata[path]! ;
56+
5057 /// Start the web asset server on a [hostname] and [port] .
5158 ///
5259 /// Unhandled exceptions will throw a exception with the error and stack
@@ -66,10 +73,6 @@ class TestAssetServer implements AssetReader {
6673 return server;
6774 }
6875
69- Uint8List getFile (String path) => _files[path];
70-
71- Uint8List getSourceMap (String path) => _sourcemaps[path];
72-
7376 // handle requests for JavaScript source, dart sources maps, or asset files.
7477 Future <shelf.Response > handleRequest (shelf.Request request) async {
7578 var headers = < String , String > {};
@@ -94,21 +97,18 @@ class TestAssetServer implements AssetReader {
9497 requestPath = _stripBasePath (requestPath, basePath) ?? requestPath;
9598
9699 requestPath = requestPath.startsWith ('/' ) ? requestPath : '/$requestPath ' ;
97- if (requestPath == null ) {
98- return shelf.Response .notFound ('' );
99- }
100100
101101 // If this is a JavaScript file, it must be in the in-memory cache.
102102 // Attempt to look up the file by URI.
103- if (_files. containsKey (requestPath)) {
103+ if (hasFile (requestPath)) {
104104 final List <int > bytes = getFile (requestPath);
105105 headers[HttpHeaders .contentLengthHeader] = bytes.length.toString ();
106106 headers[HttpHeaders .contentTypeHeader] = 'application/javascript' ;
107107 return shelf.Response .ok (bytes, headers: headers);
108108 }
109109 // If this is a sourcemap file, then it might be in the in-memory cache.
110110 // Attempt to lookup the file by URI.
111- if (_sourcemaps. containsKey (requestPath)) {
111+ if (hasSourceMap (requestPath)) {
112112 final List <int > bytes = getSourceMap (requestPath);
113113 headers[HttpHeaders .contentLengthHeader] = bytes.length.toString ();
114114 headers[HttpHeaders .contentTypeHeader] = 'application/json' ;
@@ -124,7 +124,7 @@ class TestAssetServer implements AssetReader {
124124 // Attempt to determine the file's mime type. if this is not provided some
125125 // browsers will refuse to render images/show video et cetera. If the tool
126126 // cannot determine a mime type, fall back to application/octet-stream.
127- String mimeType;
127+ String ? mimeType;
128128 if (length >= 12 ) {
129129 mimeType = mime.lookupMimeType (
130130 file.path,
@@ -160,10 +160,6 @@ class TestAssetServer implements AssetReader {
160160 var manifest =
161161 _castStringKeyedMap (json.decode (manifestFile.readAsStringSync ()));
162162 for (var filePath in manifest.keys) {
163- if (filePath == null ) {
164- _logger.severe ('Invalid manfiest file: $filePath ' );
165- continue ;
166- }
167163 var offsets = _castStringKeyedMap (manifest[filePath]);
168164 var codeOffsets = (offsets['code' ] as List <dynamic >).cast <int >();
169165 var sourcemapOffsets =
@@ -200,7 +196,7 @@ class TestAssetServer implements AssetReader {
200196 sourcemapStart,
201197 sourcemapEnd - sourcemapStart,
202198 );
203- _sourcemaps ['$filePath .map' ] = sourcemapView;
199+ _sourceMaps ['$filePath .map' ] = sourcemapView;
204200
205201 var metadataStart = metadataOffsets[0 ];
206202 var metadataEnd = metadataOffsets[1 ];
@@ -259,36 +255,42 @@ class TestAssetServer implements AssetReader {
259255 }
260256
261257 @override
262- Future <String > dartSourceContents (String serverPath) {
263- serverPath = _stripBasePath (serverPath, basePath);
264- var result = _resolveDartFile (serverPath);
265- if (result.existsSync ()) {
266- return result.readAsString ();
258+ Future <String ?> dartSourceContents (String serverPath) async {
259+ final stripped = _stripBasePath (serverPath, basePath);
260+ if (stripped != null ) {
261+ var result = _resolveDartFile (stripped);
262+ if (result.existsSync ()) {
263+ return result.readAsString ();
264+ }
267265 }
268266 _logger.severe ('Source not found: $serverPath ' );
269267 return null ;
270268 }
271269
272270 @override
273- Future <String > sourceMapContents (String serverPath) async {
274- serverPath = _stripBasePath (serverPath, basePath);
275- var path = '/$serverPath ' ;
276- if (_sourcemaps.containsKey (path)) {
277- return utf8.decode (_sourcemaps[path]);
271+ Future <String ?> sourceMapContents (String serverPath) async {
272+ final stripped = _stripBasePath (serverPath, basePath);
273+ if (stripped != null ) {
274+ var path = '/$stripped ' ;
275+ if (hasSourceMap (path)) {
276+ return utf8.decode (getSourceMap (path));
277+ }
278278 }
279279 _logger.severe ('Source map not found: $serverPath ' );
280280 return null ;
281281 }
282282
283283 @override
284- Future <String > metadataContents (String serverPath) async {
285- serverPath = _stripBasePath (serverPath, basePath);
286- if (serverPath.endsWith ('.ddc_merged_metadata' )) {
287- return _mergedMetadata;
288- }
289- var path = '/$serverPath ' ;
290- if (_metadata.containsKey (path)) {
291- return utf8.decode (_metadata[path]);
284+ Future <String ?> metadataContents (String serverPath) async {
285+ final stripped = _stripBasePath (serverPath, basePath);
286+ if (stripped != null ) {
287+ if (stripped.endsWith ('.ddc_merged_metadata' )) {
288+ return _mergedMetadata;
289+ }
290+ var path = '/$stripped ' ;
291+ if (hasMetadata (path)) {
292+ return utf8.decode (getMetadata (path));
293+ }
292294 }
293295 _logger.severe ('Metadata not found: $serverPath ' );
294296 return null ;
@@ -299,7 +301,7 @@ class TestAssetServer implements AssetReader {
299301/// the same structure (`Map<String, dynamic>` ) with the correct runtime types.
300302Map <String , dynamic > _castStringKeyedMap (dynamic untyped) {
301303 var map = untyped as Map <dynamic , dynamic >;
302- return map? .cast <String , dynamic >();
304+ return map.cast <String , dynamic >();
303305}
304306
305307String _stripLeadingSlashes (String path) {
@@ -309,7 +311,7 @@ String _stripLeadingSlashes(String path) {
309311 return path;
310312}
311313
312- String _stripBasePath (String path, String basePath) {
314+ String ? _stripBasePath (String path, String basePath) {
313315 path = _stripLeadingSlashes (path);
314316 if (path.startsWith (basePath)) {
315317 path = path.substring (basePath.length);
@@ -327,5 +329,6 @@ String _parseBasePathFromIndexHtml(String index) {
327329 }
328330 final contents = file.readAsStringSync ();
329331 final matches = RegExp (r'<base href="/([^>]*)/">' ).allMatches (contents);
330- return matches.isEmpty ? '' : matches.first.group (1 );
332+ if (matches.isEmpty) return '' ;
333+ return matches.first.group (1 ) ?? '' ;
331334}
0 commit comments