-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FrontendServerAssetReader #871
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file | ||
// for details. 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:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:path/path.dart' as p; | ||
|
||
import 'asset_reader.dart'; | ||
|
||
/// A reader for Dart sources and related source maps provided by the Frontend | ||
/// Server. | ||
class FrontendServerAssetReader implements AssetReader { | ||
final File _mapOriginal; | ||
final File _mapIncremental; | ||
final File _jsonOriginal; | ||
final File _jsonIncremental; | ||
|
||
// Map of Dart module server path to source map contents. | ||
final _mapContents = <String, String>{}; | ||
|
||
bool _haveReadOriginals = false; | ||
|
||
/// Creates a [FrontendServerAssetReader]. | ||
/// | ||
/// [outputPath] is the file path to the Frontend Server kernel file e.g. | ||
/// | ||
/// /some/path/main.dart.dill | ||
/// | ||
/// Corresponding `.json` and `.map` files will be read relative to | ||
/// [outputPath]. | ||
FrontendServerAssetReader( | ||
String outputPath, | ||
) : _mapOriginal = File('$outputPath.map'), | ||
_mapIncremental = File('$outputPath.incremental.map'), | ||
_jsonOriginal = File('$outputPath.json'), | ||
_jsonIncremental = File('$outputPath.incremental.json'); | ||
|
||
@override | ||
Future<String> dartSourceContents(String serverPath) => | ||
throw UnimplementedError(); | ||
|
||
@override | ||
Future<String> sourceMapContents(String serverPath) async { | ||
if (!serverPath.endsWith('lib.js.map')) return null; | ||
if (!serverPath.startsWith('/')) serverPath = '/$serverPath'; | ||
// Strip the .map, sources are looked up by their js path. | ||
return _mapContents[p.withoutExtension(serverPath)]; | ||
} | ||
|
||
/// Updates the internal caches by reading the Frontend Server output files. | ||
/// | ||
/// Will only read the incremental files on additional calls. | ||
Future<void> updateCaches() async { | ||
if (!_haveReadOriginals) { | ||
await _updateCaches(_mapOriginal, _jsonOriginal); | ||
_haveReadOriginals = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed to an if else |
||
} else { | ||
await _updateCaches(_mapIncremental, _jsonIncremental); | ||
} | ||
} | ||
|
||
Future<void> _updateCaches(File map, File json) async { | ||
if (!(await map.exists() && await json.exists())) { | ||
throw StateError('$map and $json do not exist.'); | ||
} | ||
var sourceContents = await map.readAsBytes(); | ||
var sourceInfo = | ||
jsonDecode(await json.readAsString()) as Map<String, dynamic>; | ||
for (var key in sourceInfo.keys) { | ||
var info = sourceInfo[key]; | ||
_mapContents[key] = utf8.decode(sourceContents | ||
.getRange( | ||
info['sourcemap'][0] as int, | ||
info['sourcemap'][1] as int, | ||
) | ||
.toList()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"/web/main.dart.lib.js":{"code":[0,2929],"sourcemap":[0,373]},"/packages/webdev_example_app/message.dart.lib.js":{"code":[2929,3844],"sourcemap":[373,568]},"/packages/path/src/path_set.dart.lib.js":{"code":[3844,97029],"sourcemap":[568,24042]},"/packages/path/src/path_exception.dart.lib.js":{"code":[97029,98717],"sourcemap":[24042,24277]},"/packages/path/src/characters.dart.lib.js":{"code":[98717,100285],"sourcemap":[24277,24602]},"/packages/path/src/utils.dart.lib.js":{"code":[100285,102035],"sourcemap":[24602,25191]}} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file | ||
// for details. 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:io'; | ||
|
||
import 'package:dwds/dwds.dart'; | ||
import 'package:path/path.dart' as p; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
FrontendServerAssetReader assetReader; | ||
Directory tempFixtures; | ||
File jsonOriginal; | ||
File mapOriginal; | ||
|
||
Future<void> _createTempFixtures() async { | ||
var fixtures = p.join('test', 'fixtures'); | ||
tempFixtures = await Directory.systemTemp.createTemp('dwds_test_fixtures'); | ||
await tempFixtures.create(); | ||
jsonOriginal = await File(p.join(fixtures, 'main.dart.dill.json')) | ||
.copy(p.join(tempFixtures.path, 'main.dart.dill.json')); | ||
mapOriginal = await File(p.join(fixtures, 'main.dart.dill.map')) | ||
.copy(p.join(tempFixtures.path, 'main.dart.dill.map')); | ||
} | ||
|
||
setUp(() async { | ||
await _createTempFixtures(); | ||
assetReader = | ||
FrontendServerAssetReader(p.join(tempFixtures.path, 'main.dart.dill')); | ||
await assetReader.updateCaches(); | ||
}); | ||
|
||
tearDown(() async { | ||
if (await tempFixtures.exists()) await tempFixtures.delete(recursive: true); | ||
}); | ||
|
||
group('FrontendServerAssetReader', () { | ||
group('source maps', () { | ||
test('can be read', () async { | ||
var result = | ||
await assetReader.sourceMapContents('web/main.dart.lib.js.map'); | ||
expect(result, isNotNull); | ||
}); | ||
|
||
test('are cached', () async { | ||
// Remove the underlying fixtures. | ||
await tempFixtures.delete(recursive: true); | ||
|
||
var cachedResult = | ||
await assetReader.sourceMapContents('web/main.dart.lib.js.map'); | ||
expect(cachedResult, isNotNull); | ||
}); | ||
|
||
test('are null if the path does not exist', () async { | ||
var result = | ||
await assetReader.sourceMapContents('web/foo.dart.lib.js.map'); | ||
expect(result, isNull); | ||
jakemac53 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
test('are updated with new incremental results', () async { | ||
var missingResult = | ||
await assetReader.sourceMapContents('web/foo.dart.lib.js.map'); | ||
expect(missingResult, isNull); | ||
|
||
// Update fixture. | ||
await File(p.join(tempFixtures.path, 'main.dart.dill.incremental.json')) | ||
.writeAsString((await jsonOriginal.readAsString()) | ||
.replaceAll('web/main.dart.lib.js', 'web/foo.dart.lib.js')); | ||
await File(p.join(tempFixtures.path, 'main.dart.dill.incremental.map')) | ||
.writeAsString((await mapOriginal.readAsString()) | ||
.replaceAll('web/main.dart.lib.js', 'web/foo.dart.lib.js')); | ||
|
||
await assetReader.updateCaches(); | ||
|
||
var newResult = | ||
await assetReader.sourceMapContents('web/foo.dart.lib.js.map'); | ||
expect(newResult, isNotNull); | ||
}); | ||
}); | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually interesting..... this wouldn't be anything specific to the frontend_server.
At the same time we will want an identical abstraction to this one which knows how to serve the JS files.
This class should probably extend an abstract DartSourceAssetReader blass which reads from the FS using a package resolver.
And then this file should also add an additional
jsFileContents(String serverPath)
method which we can plug into our server in webdev.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure we want to go with the base class approach. I think we'd want to delegate to a
DartSourceAssetReader
or something. I'll cross that hump when I get there though.