1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'dart:typed_data';
import 'package:dwds/data/build_result.dart';
// ignore: import_of_legacy_library_into_null_safe
import 'package:dwds/dwds.dart';
import 'package:html/dom.dart';
import 'package:html/parser.dart';
import 'package:logging/logging.dart' as logging;
import 'package:meta/meta.dart';
import 'package:mime/mime.dart' as mime;
import 'package:package_config/package_config.dart';
import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf/shelf_io.dart' as shelf;
import 'package:vm_service/vm_service.dart' as vm_service;
import '../artifacts.dart';
import '../asset.dart';
import '../base/common.dart';
import '../base/file_system.dart';
import '../base/io.dart';
import '../base/logger.dart';
import '../base/net.dart';
import '../base/platform.dart';
import '../build_info.dart';
import '../build_system/targets/shader_compiler.dart';
import '../build_system/targets/web.dart';
import '../bundle_builder.dart';
import '../cache.dart';
import '../compile.dart';
import '../convert.dart';
import '../dart/package_map.dart';
import '../devfs.dart';
import '../globals.dart' as globals;
import '../project.dart';
import '../vmservice.dart';
import '../web/bootstrap.dart';
import '../web/chrome.dart';
import '../web/compile.dart';
import '../web/file_generators/flutter_js.dart' as flutter_js;
import '../web/memory_fs.dart';
import 'sdk_web_configuration.dart';
typedef DwdsLauncher = Future<Dwds> Function({
required AssetReader assetReader,
required Stream<BuildResult> buildResults,
required ConnectionProvider chromeConnection,
required LoadStrategy loadStrategy,
required bool enableDebugging,
ExpressionCompiler? expressionCompiler,
bool enableDebugExtension,
String hostname,
bool useSseForDebugProxy,
bool useSseForDebugBackend,
bool useSseForInjectedClient,
UrlEncoder? urlEncoder,
bool spawnDds,
bool enableDevtoolsLaunch,
DevtoolsLauncher? devtoolsLauncher,
bool launchDevToolsInNewWindow,
SdkConfigurationProvider? sdkConfigurationProvider,
bool emitDebugEvents,
});
// A minimal index for projects that do not yet support web.
const String _kDefaultIndex = '''
<html>
<head>
<base href="/">
</head>
<body>
<script src="main.dart.js"></script>
</body>
</html>
''';
/// An expression compiler connecting to FrontendServer.
///
/// This is only used in development mode.
class WebExpressionCompiler implements ExpressionCompiler {
WebExpressionCompiler(this._generator, {
required FileSystem? fileSystem,
}) : _fileSystem = fileSystem;
final ResidentCompiler _generator;
final FileSystem? _fileSystem;
@override
Future<ExpressionCompilationResult> compileExpressionToJs(
String isolateId,
String libraryUri,
int line,
int column,
Map<String, String> jsModules,
Map<String, String> jsFrameValues,
String moduleName,
String expression,
) async {
final CompilerOutput? compilerOutput =
await _generator.compileExpressionToJs(libraryUri, line, column,
jsModules, jsFrameValues, moduleName, expression);
if (compilerOutput != null && compilerOutput.outputFilename != null) {
final String content = utf8.decode(
_fileSystem!.file(compilerOutput.outputFilename).readAsBytesSync());
return ExpressionCompilationResult(
content, compilerOutput.errorCount > 0);
}
return ExpressionCompilationResult(
"InternalError: frontend server failed to compile '$expression'",
true);
}
@override
Future<void> initialize({String? moduleFormat, bool? soundNullSafety}) async {}
@override
Future<bool> updateDependencies(Map<String, ModuleInfo> modules) async => true;
}
/// A web server which handles serving JavaScript and assets.
///
/// This is only used in development mode.
class WebAssetServer implements AssetReader {
@visibleForTesting
WebAssetServer(
this._httpServer,
this._packages,
this.internetAddress,
this._modules,
this._digests,
this._nullSafetyMode,
) : basePath = _parseBasePathFromIndexHtml(globals.fs.currentDirectory
.childDirectory('web')
.childFile('index.html'));
// Fallback to "application/octet-stream" on null which
// makes no claims as to the structure of the data.
static const String _kDefaultMimeType = 'application/octet-stream';
final Map<String, String> _modules;
final Map<String, String> _digests;
int get selectedPort => _httpServer.port;
void performRestart(List<String> modules) {
for (final String module in modules) {
// We skip computing the digest by using the hashCode of the underlying buffer.
// Whenever a file is updated, the corresponding Uint8List.view it corresponds
// to will change.
final String moduleName =
module.startsWith('/') ? module.substring(1) : module;
final String name = moduleName.replaceAll('.lib.js', '');
final String path = moduleName.replaceAll('.js', '');
_modules[name] = path;
_digests[name] = _webMemoryFS.files[moduleName].hashCode.toString();
}
}
@visibleForTesting
List<String> write(
File codeFile,
File manifestFile,
File sourcemapFile,
File metadataFile,
) {
return _webMemoryFS.write(codeFile, manifestFile, sourcemapFile, metadataFile);
}
/// Start the web asset server on a [hostname] and [port].
///
/// If [testMode] is true, do not actually initialize dwds or the shelf static
/// server.
///
/// Unhandled exceptions will throw a [ToolExit] with the error and stack
/// trace.
static Future<WebAssetServer> start(
ChromiumLauncher? chromiumLauncher,
String hostname,
int? port,
UrlTunneller? urlTunneller,
bool useSseForDebugProxy,
bool useSseForDebugBackend,
bool useSseForInjectedClient,
BuildInfo buildInfo,
bool enableDwds,
bool enableDds,
Uri entrypoint,
ExpressionCompiler? expressionCompiler,
NullSafetyMode nullSafetyMode, {
bool testMode = false,
DwdsLauncher dwdsLauncher = Dwds.start,
}) async {
InternetAddress address;
if (hostname == 'any') {
address = InternetAddress.anyIPv4;
} else {
address = (await InternetAddress.lookup(hostname)).first;
}
HttpServer? httpServer;
const int kMaxRetries = 4;
for (int i = 0; i <= kMaxRetries; i++) {
try {
httpServer = await HttpServer.bind(address, port ?? await globals.os.findFreePort());
break;
} on SocketException catch (e, s) {
if (i >= kMaxRetries) {
globals.printError('Failed to bind web development server:\n$e', stackTrace: s);
throwToolExit('Failed to bind web development server:\n$e');
}
await Future<void>.delayed(const Duration(milliseconds: 100));
}
}
// Allow rendering in a iframe.
httpServer!.defaultResponseHeaders.remove('x-frame-options', 'SAMEORIGIN');
final PackageConfig packageConfig = buildInfo.packageConfig;
final Map<String, String> digests = <String, String>{};
final Map<String, String> modules = <String, String>{};
final WebAssetServer server = WebAssetServer(
httpServer,
packageConfig,
address,
modules,
digests,
nullSafetyMode,
);
if (testMode) {
return server;
}
// In release builds deploy a simpler proxy server.
if (buildInfo.mode != BuildMode.debug) {
final ReleaseAssetServer releaseAssetServer = ReleaseAssetServer(
entrypoint,
fileSystem: globals.fs,
platform: globals.platform,
flutterRoot: Cache.flutterRoot,
webBuildDirectory: getWebBuildDirectory(),
basePath: server.basePath,
);
runZonedGuarded(() {
shelf.serveRequests(httpServer!, releaseAssetServer.handle);
}, (Object e, StackTrace s) {
globals.printTrace('Release asset server: error serving requests: $e:$s');
});
return server;
}
// Return a version string for all active modules. This is populated
// along with the `moduleProvider` update logic.
Future<Map<String, String>> digestProvider() async => digests;
// Ensure dwds is present and provide middleware to avoid trying to
// load the through the isolate APIs.
final Directory directory =
await _loadDwdsDirectory(globals.fs, globals.logger);
shelf.Handler middleware(FutureOr<shelf.Response> Function(shelf.Request) innerHandler) {
return (shelf.Request request) async {
if (request.url.path.endsWith('dwds/src/injected/client.js')) {
final Uri uri = directory.uri.resolve('src/injected/client.js');
final String result =
await globals.fs.file(uri.toFilePath()).readAsString();
return shelf.Response.ok(result, headers: <String, String>{
HttpHeaders.contentTypeHeader: 'application/javascript',
});
}
return innerHandler(request);
};
}
logging.Logger.root.level = logging.Level.ALL;
logging.Logger.root.onRecord.listen(log);
// In debug builds, spin up DWDS and the full asset server.
final Dwds dwds = await dwdsLauncher(
assetReader: server,
enableDebugExtension: true,
buildResults: const Stream<BuildResult>.empty(),
chromeConnection: () async {
final Chromium chromium = await chromiumLauncher!.connectedInstance;
return chromium.chromeConnection;
},
hostname: hostname,
urlEncoder: urlTunneller,
enableDebugging: true,
useSseForDebugProxy: useSseForDebugProxy,
useSseForDebugBackend: useSseForDebugBackend,
useSseForInjectedClient: useSseForInjectedClient,
loadStrategy: FrontendServerRequireStrategyProvider(
ReloadConfiguration.none,
server,
PackageUriMapper(packageConfig),
digestProvider,
server.basePath!,
).strategy,
expressionCompiler: expressionCompiler,
spawnDds: enableDds,
sdkConfigurationProvider: SdkWebConfigurationProvider(globals.artifacts!),
);
shelf.Pipeline pipeline = const shelf.Pipeline();
if (enableDwds) {
pipeline = pipeline.addMiddleware(middleware);
pipeline = pipeline.addMiddleware(dwds.middleware);
}
final shelf.Handler dwdsHandler =
pipeline.addHandler(server.handleRequest);
final shelf.Cascade cascade =
shelf.Cascade().add(dwds.handler).add(dwdsHandler);
runZonedGuarded(() {
shelf.serveRequests(httpServer!, cascade.handler);
}, (Object e, StackTrace s) {
globals.printTrace('Dwds server: error serving requests: $e:$s');
});
server.dwds = dwds;
server._dwdsInit = true;
return server;
}
final NullSafetyMode _nullSafetyMode;
final HttpServer _httpServer;
final WebMemoryFS _webMemoryFS = WebMemoryFS();
final PackageConfig _packages;
final InternetAddress internetAddress;
late final Dwds dwds;
late Directory entrypointCacheDirectory;
bool _dwdsInit = false;
@visibleForTesting
HttpHeaders get defaultResponseHeaders => _httpServer.defaultResponseHeaders;
@visibleForTesting
Uint8List? getFile(String path) => _webMemoryFS.files[path];
@visibleForTesting
Uint8List? getSourceMap(String path) => _webMemoryFS.sourcemaps[path];
@visibleForTesting
Uint8List? getMetadata(String path) => _webMemoryFS.metadataFiles[path];
@visibleForTesting
/// The base path to serve from.
///
/// It should have no leading or trailing slashes.
String? basePath = '';
// handle requests for JavaScript source, dart sources maps, or asset files.
@visibleForTesting
Future<shelf.Response> handleRequest(shelf.Request request) async {
if (request.method != 'GET') {
// Assets are served via GET only.
return shelf.Response.notFound('');
}
final String? requestPath = _stripBasePath(request.url.path, basePath);
if (requestPath == null) {
return shelf.Response.notFound('');
}
// If the response is `/`, then we are requesting the index file.
if (requestPath == '/' || requestPath.isEmpty) {
return _serveIndex();
}
final Map<String, String> headers = <String, String>{};
// Track etag headers for better caching of resources.
final String? ifNoneMatch = request.headers[HttpHeaders.ifNoneMatchHeader];
headers[HttpHeaders.cacheControlHeader] = 'max-age=0, must-revalidate';
// If this is a JavaScript file, it must be in the in-memory cache.
// Attempt to look up the file by URI.
final String webServerPath =
requestPath.replaceFirst('.dart.js', '.dart.lib.js');
if (_webMemoryFS.files.containsKey(requestPath) || _webMemoryFS.files.containsKey(webServerPath)) {
final List<int>? bytes = getFile(requestPath) ?? getFile(webServerPath);
// Use the underlying buffer hashCode as a revision string. This buffer is
// replaced whenever the frontend_server produces new output files, which
// will also change the hashCode.
final String etag = bytes.hashCode.toString();
if (ifNoneMatch == etag) {
return shelf.Response.notModified();
}
headers[HttpHeaders.contentLengthHeader] = bytes!.length.toString();
headers[HttpHeaders.contentTypeHeader] = 'application/javascript';
headers[HttpHeaders.etagHeader] = etag;
return shelf.Response.ok(bytes, headers: headers);
}
// If this is a sourcemap file, then it might be in the in-memory cache.
// Attempt to lookup the file by URI.
if (_webMemoryFS.sourcemaps.containsKey(requestPath)) {
final List<int>? bytes = getSourceMap(requestPath);
final String etag = bytes.hashCode.toString();
if (ifNoneMatch == etag) {
return shelf.Response.notModified();
}
headers[HttpHeaders.contentLengthHeader] = bytes!.length.toString();
headers[HttpHeaders.contentTypeHeader] = 'application/json';
headers[HttpHeaders.etagHeader] = etag;
return shelf.Response.ok(bytes, headers: headers);
}
// If this is a metadata file, then it might be in the in-memory cache.
// Attempt to lookup the file by URI.
if (_webMemoryFS.metadataFiles.containsKey(requestPath)) {
final List<int>? bytes = getMetadata(requestPath);
final String etag = bytes.hashCode.toString();
if (ifNoneMatch == etag) {
return shelf.Response.notModified();
}
headers[HttpHeaders.contentLengthHeader] = bytes!.length.toString();
headers[HttpHeaders.contentTypeHeader] = 'application/json';
headers[HttpHeaders.etagHeader] = etag;
return shelf.Response.ok(bytes, headers: headers);
}
File file = _resolveDartFile(requestPath);
// If all of the lookups above failed, the file might have been an asset.
// Try and resolve the path relative to the built asset directory.
if (!file.existsSync()) {
final Uri potential = globals.fs
.directory(getAssetBuildDirectory())
.uri
.resolve(requestPath.replaceFirst('assets/', ''));
file = globals.fs.file(potential);
}
if (!file.existsSync()) {
final Uri webPath = globals.fs.currentDirectory
.childDirectory('web')
.uri
.resolve(requestPath);
file = globals.fs.file(webPath);
}
if (!file.existsSync()) {
// Paths starting with these prefixes should've been resolved above.
if (requestPath.startsWith('assets/') ||
requestPath.startsWith('packages/')) {
return shelf.Response.notFound('');
}
return _serveIndex();
}
// For real files, use a serialized file stat plus path as a revision.
// This allows us to update between canvaskit and non-canvaskit SDKs.
final String etag = file.lastModifiedSync().toIso8601String() +
Uri.encodeComponent(file.path);
if (ifNoneMatch == etag) {
return shelf.Response.notModified();
}
final int length = file.lengthSync();
// Attempt to determine the file's mime type. if this is not provided some
// browsers will refuse to render images/show video etc. If the tool
// cannot determine a mime type, fall back to application/octet-stream.
final String mimeType = mime.lookupMimeType(
file.path,
headerBytes: await file.openRead(0, mime.defaultMagicNumbersMaxLength).first,
) ?? _kDefaultMimeType;
headers[HttpHeaders.contentLengthHeader] = length.toString();
headers[HttpHeaders.contentTypeHeader] = mimeType;
headers[HttpHeaders.etagHeader] = etag;
return shelf.Response.ok(file.openRead(), headers: headers);
}
/// Tear down the http server running.
Future<void> dispose() async {
if (_dwdsInit) {
await dwds.stop();
}
return _httpServer.close();
}
/// Write a single file into the in-memory cache.
void writeFile(String filePath, String contents) {
writeBytes(filePath, utf8.encode(contents) as Uint8List);
}
void writeBytes(String filePath, Uint8List contents) {
_webMemoryFS.files[filePath] = contents;
}
/// Determines what rendering backed to use.
WebRendererMode webRenderer = WebRendererMode.html;
shelf.Response _serveIndex() {
final Map<String, String> headers = <String, String>{
HttpHeaders.contentTypeHeader: 'text/html',
};
final File indexFile = globals.fs.currentDirectory
.childDirectory('web')
.childFile('index.html');
if (indexFile.existsSync()) {
String indexFileContent = indexFile.readAsStringSync();
if (indexFileContent.contains(kBaseHrefPlaceholder)) {
indexFileContent = indexFileContent.replaceAll(kBaseHrefPlaceholder, '/');
headers[HttpHeaders.contentLengthHeader] = indexFileContent.length.toString();
return shelf.Response.ok(indexFileContent,headers: headers);
}
headers[HttpHeaders.contentLengthHeader] =
indexFile.lengthSync().toString();
return shelf.Response.ok(indexFile.openRead(), headers: headers);
}
headers[HttpHeaders.contentLengthHeader] = _kDefaultIndex.length.toString();
return shelf.Response.ok(_kDefaultIndex, headers: headers);
}
// Attempt to resolve `path` to a dart file.
File _resolveDartFile(String path) {
// Return the actual file objects so that local engine changes are automatically picked up.
switch (path) {
case 'dart_sdk.js':
return _resolveDartSdkJsFile;
case 'dart_sdk.js.map':
return _resolveDartSdkJsMapFile;
}
// This is the special generated entrypoint.
if (path == 'web_entrypoint.dart') {
return entrypointCacheDirectory.childFile('web_entrypoint.dart');
}
// If this is a dart file, it must be on the local file system and is
// likely coming from a source map request. The tool doesn't currently
// consider the case of Dart files as assets.
final File dartFile =
globals.fs.file(globals.fs.currentDirectory.uri.resolve(path));
if (dartFile.existsSync()) {
return dartFile;
}
final List<String> segments = path.split('/');
if (segments.first.isEmpty) {
segments.removeAt(0);
}
// The file might have been a package file which is signaled by a
// `/packages/<package>/<path>` request.
if (segments.first == 'packages') {
final Uri? filePath = _packages
.resolve(Uri(scheme: 'package', pathSegments: segments.skip(1)));
if (filePath != null) {
final File packageFile = globals.fs.file(filePath);
if (packageFile.existsSync()) {
return packageFile;
}
}
}
// Otherwise it must be a Dart SDK source or a Flutter Web SDK source.
final Directory dartSdkParent = globals.fs
.directory(
globals.artifacts!.getHostArtifact(HostArtifact.engineDartSdkPath))
.parent;
final File dartSdkFile = globals.fs.file(dartSdkParent.uri.resolve(path));
if (dartSdkFile.existsSync()) {
return dartSdkFile;
}
final Directory flutterWebSdk = globals.fs
.directory(globals.artifacts!.getHostArtifact(HostArtifact.flutterWebSdk));
final File webSdkFile = globals.fs.file(flutterWebSdk.uri.resolve(path));
return webSdkFile;
}
File get _resolveDartSdkJsFile =>
globals.fs.file(globals.artifacts!.getHostArtifact(
kDartSdkJsArtifactMap[webRenderer]![_nullSafetyMode]!
));
File get _resolveDartSdkJsMapFile =>
globals.fs.file(globals.artifacts!.getHostArtifact(
kDartSdkJsMapArtifactMap[webRenderer]![_nullSafetyMode]!
));
@override
Future<String?> dartSourceContents(String serverPath) async {
serverPath = _stripBasePath(serverPath, basePath)!;
final File result = _resolveDartFile(serverPath);
if (result.existsSync()) {
return result.readAsString();
}
return null;
}
@override
Future<String> sourceMapContents(String serverPath) async {
serverPath = _stripBasePath(serverPath, basePath)!;
return utf8.decode(_webMemoryFS.sourcemaps[serverPath]!);
}
@override
Future<String?> metadataContents(String serverPath) async {
final String? resultPath = _stripBasePath(serverPath, basePath);
if (resultPath == 'main_module.ddc_merged_metadata') {
return _webMemoryFS.mergedMetadata;
}
if (_webMemoryFS.metadataFiles.containsKey(resultPath)) {
return utf8.decode(_webMemoryFS.metadataFiles[resultPath]!);
}
throw Exception('Could not find metadata contents for $serverPath');
}
@override
Future<void> close() async {}
}
class ConnectionResult {
ConnectionResult(this.appConnection, this.debugConnection, this.vmService);
final AppConnection? appConnection;
final DebugConnection? debugConnection;
final vm_service.VmService vmService;
}
typedef VmServiceFactory = Future<vm_service.VmService> Function(
Uri, {
CompressionOptions compression,
required Logger logger,
});
/// The web specific DevFS implementation.
class WebDevFS implements DevFS {
/// Create a new [WebDevFS] instance.
///
/// [testMode] is true, do not actually initialize dwds or the shelf static
/// server.
WebDevFS({
required this.hostname,
required int? port,
required this.packagesFilePath,
required this.urlTunneller,
required this.useSseForDebugProxy,
required this.useSseForDebugBackend,
required this.useSseForInjectedClient,
required this.buildInfo,
required this.enableDwds,
required this.enableDds,
required this.entrypoint,
required this.expressionCompiler,
required this.chromiumLauncher,
required this.nullAssertions,
required this.nativeNullAssertions,
required this.nullSafetyMode,
this.testMode = false,
}) : _port = port;
final Uri entrypoint;
final String hostname;
final String packagesFilePath;
final UrlTunneller? urlTunneller;
final bool useSseForDebugProxy;
final bool useSseForDebugBackend;
final bool useSseForInjectedClient;
final BuildInfo buildInfo;
final bool enableDwds;
final bool enableDds;
final bool testMode;
final ExpressionCompiler? expressionCompiler;
final ChromiumLauncher? chromiumLauncher;
final bool nullAssertions;
final bool nativeNullAssertions;
final int? _port;
final NullSafetyMode nullSafetyMode;
late WebAssetServer webAssetServer;
Dwds get dwds => webAssetServer.dwds;
// A flag to indicate whether we have called `setAssetDirectory` on the target device.
@override
bool hasSetAssetDirectory = false;
@override
bool didUpdateFontManifest = false;
Future<DebugConnection>? _cachedExtensionFuture;
StreamSubscription<void>? _connectedApps;
/// Connect and retrieve the [DebugConnection] for the current application.
///
/// Only calls [AppConnection.runMain] on the subsequent connections.
Future<ConnectionResult?> connect(
bool useDebugExtension, {
@visibleForTesting
VmServiceFactory vmServiceFactory = createVmServiceDelegate,
}) {
final Completer<ConnectionResult> firstConnection =
Completer<ConnectionResult>();
// Note there is an asynchronous gap between this being set to true and
// [firstConnection] completing; thus test the boolean to determine if
// the current connection is the first.
bool foundFirstConnection = false;
_connectedApps =
dwds.connectedApps.listen((AppConnection appConnection) async {
try {
final DebugConnection debugConnection = useDebugExtension
? await (_cachedExtensionFuture ??=
dwds.extensionDebugConnections.stream.first)
: await dwds.debugConnection(appConnection);
if (foundFirstConnection) {
appConnection.runMain();
} else {
foundFirstConnection = true;
final vm_service.VmService vmService = await vmServiceFactory(
Uri.parse(debugConnection.uri),
logger: globals.logger,
);
firstConnection
.complete(ConnectionResult(appConnection, debugConnection, vmService));
}
} on Exception catch (error, stackTrace) {
if (!firstConnection.isCompleted) {
firstConnection.completeError(error, stackTrace);
}
}
}, onError: (Object error, StackTrace stackTrace) {
globals.printError(
'Unknown error while waiting for debug connection:$error\n$stackTrace',
);
if (!firstConnection.isCompleted) {
firstConnection.completeError(error, stackTrace);
}
});
return firstConnection.future;
}
@override
List<Uri> sources = <Uri>[];
@override
DateTime? lastCompiled;
@override
PackageConfig? lastPackageConfig;
// We do not evict assets on the web.
@override
Set<String> get assetPathsToEvict => const <String>{};
@override
Uri? get baseUri => _baseUri;
Uri? _baseUri;
@override
Future<Uri> create() async {
webAssetServer = await WebAssetServer.start(
chromiumLauncher,
hostname,
_port,
urlTunneller,
useSseForDebugProxy,
useSseForDebugBackend,
useSseForInjectedClient,
buildInfo,
enableDwds,
enableDds,
entrypoint,
expressionCompiler,
nullSafetyMode,
testMode: testMode,
);
final int selectedPort = webAssetServer.selectedPort;
if (buildInfo.dartDefines.contains('FLUTTER_WEB_AUTO_DETECT=true')) {
webAssetServer.webRenderer = WebRendererMode.autoDetect;
} else if (buildInfo.dartDefines.contains('FLUTTER_WEB_USE_SKIA=true')) {
webAssetServer.webRenderer = WebRendererMode.canvaskit;
}
if (hostname == 'any') {
_baseUri = Uri.http('localhost:$selectedPort', webAssetServer.basePath!);
} else {
_baseUri = Uri.http('$hostname:$selectedPort', webAssetServer.basePath!);
}
return _baseUri!;
}
@override
Future<void> destroy() async {
await webAssetServer.dispose();
await _connectedApps?.cancel();
}
@override
Uri deviceUriToHostUri(Uri deviceUri) {
return deviceUri;
}
@override
String get fsName => 'web_asset';
@override
Directory? get rootDirectory => null;
@override
Future<UpdateFSReport> update({
required Uri mainUri,
required ResidentCompiler generator,
required bool trackWidgetCreation,
required String pathToReload,
required List<Uri> invalidatedFiles,
required PackageConfig packageConfig,
required String dillOutputPath,
required DevelopmentShaderCompiler shaderCompiler,
DevFSWriter? devFSWriter,
String? target,
AssetBundle? bundle,
DateTime? firstBuildTime,
bool bundleFirstUpload = false,
bool fullRestart = false,
String? projectRootPath,
File? dartPluginRegistrant,
}) async {
assert(trackWidgetCreation != null);
assert(generator != null);
lastPackageConfig = packageConfig;
final File mainFile = globals.fs.file(mainUri);
final String outputDirectoryPath = mainFile.parent.path;
if (bundleFirstUpload) {
webAssetServer.entrypointCacheDirectory =
globals.fs.directory(outputDirectoryPath);
generator.addFileSystemRoot(outputDirectoryPath);
final String entrypoint = globals.fs.path.basename(mainFile.path);
webAssetServer.writeBytes(entrypoint, mainFile.readAsBytesSync());
webAssetServer.writeBytes('require.js', requireJS.readAsBytesSync());
webAssetServer.writeBytes(
'stack_trace_mapper.js', stackTraceMapper.readAsBytesSync());
webAssetServer.writeFile(
'manifest.json', '{"info":"manifest not generated in run mode."}');
webAssetServer.writeFile('flutter.js', flutter_js.generateFlutterJsFile());
webAssetServer.writeFile('flutter_service_worker.js',
'// Service worker not loaded in run mode.');
webAssetServer.writeFile(
'version.json', FlutterProject.current().getVersionInfo());
webAssetServer.writeFile(
'main.dart.js',
generateBootstrapScript(
requireUrl: 'require.js',
mapperUrl: 'stack_trace_mapper.js',
),
);
webAssetServer.writeFile(
'main_module.bootstrap.js',
generateMainModule(
entrypoint: entrypoint,
nullAssertions: nullAssertions,
nativeNullAssertions: nativeNullAssertions,
),
);
// TODO(zanderso): refactor the asset code in this and the regular devfs to
// be shared.
if (bundle != null) {
await writeBundle(
globals.fs.directory(getAssetBuildDirectory()),
bundle.entries,
bundle.entryKinds,
);
}
}
final DateTime candidateCompileTime = DateTime.now();
if (fullRestart) {
generator.reset();
}
// The tool generates an entrypoint file in a temp directory to handle
// the web specific bootstrap logic. To make it easier for DWDS to handle
// mapping the file name, this is done via an additional file root and
// special hard-coded scheme.
final CompilerOutput? compilerOutput = await generator.recompile(
Uri(
scheme: 'org-dartlang-app',
path: '/${mainUri.pathSegments.last}',
),
invalidatedFiles,
outputPath: dillOutputPath,
packageConfig: packageConfig,
projectRootPath: projectRootPath,
fs: globals.fs,
dartPluginRegistrant: dartPluginRegistrant,
);
if (compilerOutput == null || compilerOutput.errorCount > 0) {
return UpdateFSReport();
}
// Only update the last compiled time if we successfully compiled.
lastCompiled = candidateCompileTime;
// list of sources that needs to be monitored are in [compilerOutput.sources]
sources = compilerOutput.sources;
late File codeFile;
File manifestFile;
File sourcemapFile;
File metadataFile;
late List<String> modules;
try {
final Directory parentDirectory = globals.fs.directory(outputDirectoryPath);
codeFile = parentDirectory.childFile('${compilerOutput.outputFilename}.sources');
manifestFile = parentDirectory.childFile('${compilerOutput.outputFilename}.json');
sourcemapFile = parentDirectory.childFile('${compilerOutput.outputFilename}.map');
metadataFile = parentDirectory.childFile('${compilerOutput.outputFilename}.metadata');
modules = webAssetServer._webMemoryFS.write(codeFile, manifestFile, sourcemapFile, metadataFile);
} on FileSystemException catch (err) {
throwToolExit('Failed to load recompiled sources:\n$err');
}
webAssetServer.performRestart(modules);
return UpdateFSReport(
success: true,
syncedBytes: codeFile.lengthSync(),
invalidatedSourcesCount: invalidatedFiles.length,
);
}
@visibleForTesting
final File requireJS = globals.fs.file(globals.fs.path.join(
globals.artifacts!.getHostArtifact(HostArtifact.engineDartSdkPath).path,
'lib',
'dev_compiler',
'kernel',
'amd',
'require.js',
));
@visibleForTesting
final File stackTraceMapper = globals.fs.file(globals.fs.path.join(
globals.artifacts!.getHostArtifact(HostArtifact.engineDartSdkPath).path,
'lib',
'dev_compiler',
'web',
'dart_stack_trace_mapper.js',
));
@override
void resetLastCompiled() {
// Not used for web compilation.
}
@override
Set<String> get shaderPathsToEvict => <String>{};
}
class ReleaseAssetServer {
ReleaseAssetServer(
this.entrypoint, {
required FileSystem fileSystem,
required String? webBuildDirectory,
required String? flutterRoot,
required Platform platform,
this.basePath = '',
}) : _fileSystem = fileSystem,
_platform = platform,
_flutterRoot = flutterRoot,
_webBuildDirectory = webBuildDirectory,
_fileSystemUtils =
FileSystemUtils(fileSystem: fileSystem, platform: platform);
final Uri entrypoint;
final String? _flutterRoot;
final String? _webBuildDirectory;
final FileSystem _fileSystem;
final FileSystemUtils _fileSystemUtils;
final Platform _platform;
@visibleForTesting
/// The base path to serve from.
///
/// It should have no leading or trailing slashes.
final String? basePath;
// Locations where source files, assets, or source maps may be located.
List<Uri> _searchPaths() => <Uri>[
_fileSystem.directory(_webBuildDirectory).uri,
_fileSystem.directory(_flutterRoot).uri,
_fileSystem.directory(_flutterRoot).parent.uri,
_fileSystem.currentDirectory.uri,
_fileSystem.directory(_fileSystemUtils.homeDirPath).uri,
];
Future<shelf.Response> handle(shelf.Request request) async {
if (request.method != 'GET') {
// Assets are served via GET only.
return shelf.Response.notFound('');
}
Uri? fileUri;
final String? requestPath = _stripBasePath(request.url.path, basePath);
if (requestPath == null) {
return shelf.Response.notFound('');
}
if (request.url.toString() == 'main.dart') {
fileUri = entrypoint;
} else {
for (final Uri uri in _searchPaths()) {
final Uri potential = uri.resolve(requestPath);
if (potential == null ||
!_fileSystem.isFileSync(
potential.toFilePath(windows: _platform.isWindows))) {
continue;
}
fileUri = potential;
break;
}
}
if (fileUri != null) {
final File file = _fileSystem.file(fileUri);
final Uint8List bytes = file.readAsBytesSync();
// Fallback to "application/octet-stream" on null which
// makes no claims as to the structure of the data.
final String mimeType =
mime.lookupMimeType(file.path, headerBytes: bytes) ??
'application/octet-stream';
return shelf.Response.ok(bytes, headers: <String, String>{
'Content-Type': mimeType,
});
}
final File file = _fileSystem
.file(_fileSystem.path.join(_webBuildDirectory!, 'index.html'));
return shelf.Response.ok(file.readAsBytesSync(), headers: <String, String>{
'Content-Type': 'text/html',
});
}
}
@visibleForTesting
void log(logging.LogRecord event) {
final String error = event.error == null? '': 'Error: ${event.error}';
if (event.level >= logging.Level.SEVERE) {
globals.printError('${event.loggerName}: ${event.message}$error', stackTrace: event.stackTrace);
} else if (event.level == logging.Level.WARNING) {
// Note: Temporary fix for https://github.com/flutter/flutter/issues/109792
// TODO(annagrin): Remove the condition after the bogus warning is
// removed in dwds: https://github.com/dart-lang/webdev/issues/1722
if (!event.message.contains('No module for')) {
globals.printWarning('${event.loggerName}: ${event.message}$error');
}
} else {
globals.printTrace('${event.loggerName}: ${event.message}$error');
}
}
Future<Directory> _loadDwdsDirectory(
FileSystem fileSystem, Logger logger) async {
final String toolPackagePath =
fileSystem.path.join(Cache.flutterRoot!, 'packages', 'flutter_tools');
final String packageFilePath =
fileSystem.path.join(toolPackagePath, '.dart_tool', 'package_config.json');
final PackageConfig packageConfig = await loadPackageConfigWithLogging(
fileSystem.file(packageFilePath),
logger: logger,
);
return fileSystem.directory(packageConfig['dwds']!.packageUriRoot);
}
String? _stripBasePath(String path, String? basePath) {
path = _stripLeadingSlashes(path);
if (basePath != null && path.startsWith(basePath)) {
path = path.substring(basePath.length);
} else {
// The given path isn't under base path, return null to indicate that.
return null;
}
return _stripLeadingSlashes(path);
}
String _stripLeadingSlashes(String path) {
while (path.startsWith('/')) {
path = path.substring(1);
}
return path;
}
String _stripTrailingSlashes(String path) {
while (path.endsWith('/')) {
path = path.substring(0, path.length - 1);
}
return path;
}
String? _parseBasePathFromIndexHtml(File indexHtml) {
final String htmlContent =
indexHtml.existsSync() ? indexHtml.readAsStringSync() : _kDefaultIndex;
final Document document = parse(htmlContent);
final Element? baseElement = document.querySelector('base');
String? baseHref =
baseElement?.attributes == null ? null : baseElement!.attributes['href'];
if (baseHref == null || baseHref == kBaseHrefPlaceholder) {
baseHref = '';
} else if (!baseHref.startsWith('/')) {
throw ToolExit(
'Error: The base href in "web/index.html" must be absolute (i.e. start '
'with a "/"), but found: `${baseElement!.outerHtml}`.\n'
'$basePathExample',
);
} else if (!baseHref.endsWith('/')) {
throw ToolExit(
'Error: The base href in "web/index.html" must end with a "/", but found: `${baseElement!.outerHtml}`.\n'
'$basePathExample',
);
} else {
baseHref = _stripLeadingSlashes(_stripTrailingSlashes(baseHref));
}
return baseHref;
}
const String basePathExample = '''
For example, to serve from the root use:
<base href="/">
To serve from a subpath "foo" (i.e. http://localhost:8080/foo/ instead of http://localhost:8080/) use:
<base href="/foo/">
For more information, see: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base
''';