2
2
// Use of this source code is governed by a BSD-style license that can be
3
3
// found in the LICENSE file.
4
4
5
- // @dart = 2.9
6
-
7
5
// Note: this is a copy from flutter tools, updated to work with dwds tests
8
6
9
7
import 'dart:async' ;
10
8
import 'dart:convert' ;
11
9
import 'dart:io' ;
12
10
import 'dart:typed_data' ;
13
11
14
- import 'package:dwds/dwds .dart' ;
12
+ import 'package:dwds/asset_reader .dart' ;
15
13
import 'package:file/file.dart' ;
16
14
import 'package:logging/logging.dart' ;
17
15
import 'package:mime/mime.dart' as mime;
@@ -32,10 +30,10 @@ class TestAssetServer implements AssetReader {
32
30
static const String _defaultMimeType = 'application/octet-stream' ;
33
31
final FileSystem _fileSystem;
34
32
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;
39
37
final PackageConfig _packageConfig;
40
38
final InternetAddress internetAddress;
41
39
@@ -47,6 +45,15 @@ class TestAssetServer implements AssetReader {
47
45
this ._fileSystem,
48
46
) : basePath = _parseBasePathFromIndexHtml (index);
49
47
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
+
50
57
/// Start the web asset server on a [hostname] and [port] .
51
58
///
52
59
/// Unhandled exceptions will throw a exception with the error and stack
@@ -66,10 +73,6 @@ class TestAssetServer implements AssetReader {
66
73
return server;
67
74
}
68
75
69
- Uint8List getFile (String path) => _files[path];
70
-
71
- Uint8List getSourceMap (String path) => _sourcemaps[path];
72
-
73
76
// handle requests for JavaScript source, dart sources maps, or asset files.
74
77
Future <shelf.Response > handleRequest (shelf.Request request) async {
75
78
var headers = < String , String > {};
@@ -94,21 +97,18 @@ class TestAssetServer implements AssetReader {
94
97
requestPath = _stripBasePath (requestPath, basePath) ?? requestPath;
95
98
96
99
requestPath = requestPath.startsWith ('/' ) ? requestPath : '/$requestPath ' ;
97
- if (requestPath == null ) {
98
- return shelf.Response .notFound ('' );
99
- }
100
100
101
101
// If this is a JavaScript file, it must be in the in-memory cache.
102
102
// Attempt to look up the file by URI.
103
- if (_files. containsKey (requestPath)) {
103
+ if (hasFile (requestPath)) {
104
104
final List <int > bytes = getFile (requestPath);
105
105
headers[HttpHeaders .contentLengthHeader] = bytes.length.toString ();
106
106
headers[HttpHeaders .contentTypeHeader] = 'application/javascript' ;
107
107
return shelf.Response .ok (bytes, headers: headers);
108
108
}
109
109
// If this is a sourcemap file, then it might be in the in-memory cache.
110
110
// Attempt to lookup the file by URI.
111
- if (_sourcemaps. containsKey (requestPath)) {
111
+ if (hasSourceMap (requestPath)) {
112
112
final List <int > bytes = getSourceMap (requestPath);
113
113
headers[HttpHeaders .contentLengthHeader] = bytes.length.toString ();
114
114
headers[HttpHeaders .contentTypeHeader] = 'application/json' ;
@@ -124,7 +124,7 @@ class TestAssetServer implements AssetReader {
124
124
// Attempt to determine the file's mime type. if this is not provided some
125
125
// browsers will refuse to render images/show video et cetera. If the tool
126
126
// cannot determine a mime type, fall back to application/octet-stream.
127
- String mimeType;
127
+ String ? mimeType;
128
128
if (length >= 12 ) {
129
129
mimeType = mime.lookupMimeType (
130
130
file.path,
@@ -160,10 +160,6 @@ class TestAssetServer implements AssetReader {
160
160
var manifest =
161
161
_castStringKeyedMap (json.decode (manifestFile.readAsStringSync ()));
162
162
for (var filePath in manifest.keys) {
163
- if (filePath == null ) {
164
- _logger.severe ('Invalid manfiest file: $filePath ' );
165
- continue ;
166
- }
167
163
var offsets = _castStringKeyedMap (manifest[filePath]);
168
164
var codeOffsets = (offsets['code' ] as List <dynamic >).cast <int >();
169
165
var sourcemapOffsets =
@@ -200,7 +196,7 @@ class TestAssetServer implements AssetReader {
200
196
sourcemapStart,
201
197
sourcemapEnd - sourcemapStart,
202
198
);
203
- _sourcemaps ['$filePath .map' ] = sourcemapView;
199
+ _sourceMaps ['$filePath .map' ] = sourcemapView;
204
200
205
201
var metadataStart = metadataOffsets[0 ];
206
202
var metadataEnd = metadataOffsets[1 ];
@@ -259,36 +255,42 @@ class TestAssetServer implements AssetReader {
259
255
}
260
256
261
257
@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
+ }
267
265
}
268
266
_logger.severe ('Source not found: $serverPath ' );
269
267
return null ;
270
268
}
271
269
272
270
@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
+ }
278
278
}
279
279
_logger.severe ('Source map not found: $serverPath ' );
280
280
return null ;
281
281
}
282
282
283
283
@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
+ }
292
294
}
293
295
_logger.severe ('Metadata not found: $serverPath ' );
294
296
return null ;
@@ -299,7 +301,7 @@ class TestAssetServer implements AssetReader {
299
301
/// the same structure (`Map<String, dynamic>` ) with the correct runtime types.
300
302
Map <String , dynamic > _castStringKeyedMap (dynamic untyped) {
301
303
var map = untyped as Map <dynamic , dynamic >;
302
- return map? .cast <String , dynamic >();
304
+ return map.cast <String , dynamic >();
303
305
}
304
306
305
307
String _stripLeadingSlashes (String path) {
@@ -309,7 +311,7 @@ String _stripLeadingSlashes(String path) {
309
311
return path;
310
312
}
311
313
312
- String _stripBasePath (String path, String basePath) {
314
+ String ? _stripBasePath (String path, String basePath) {
313
315
path = _stripLeadingSlashes (path);
314
316
if (path.startsWith (basePath)) {
315
317
path = path.substring (basePath.length);
@@ -327,5 +329,6 @@ String _parseBasePathFromIndexHtml(String index) {
327
329
}
328
330
final contents = file.readAsStringSync ();
329
331
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 ) ?? '' ;
331
334
}
0 commit comments