-
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// 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 `FrotnendServerAssetReader`. | ||
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.
|
||
/// | ||
/// [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) => | ||
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. 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 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. Not sure we want to go with the base class approach. I think we'd want to delegate to a |
||
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 |
||
} | ||
await _updateCaches(_mapIncremental, _jsonIncremental); | ||
} | ||
|
||
Future<void> _updateCaches(File map, File json) async { | ||
if (!(await map.exists() && await json.exists())) 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. We should consider throwing here (or logging an error at least) |
||
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()); | ||
} | ||
} | ||
} |
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.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// 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.absolute(p.relative('test/fixtures', from: p.current)); | ||
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. You shouldn't need any of the 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. I think that is required for Windows testing? It's the pattern we do for the other fixtures, hence why I copied it. 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. It shouldn't be necessary, this is just getting passed to a normal File constructor. |
||
tempFixtures = | ||
jakemac53 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Directory(p.join(Directory.systemTemp.path, '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 { | ||
var result = | ||
jakemac53 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
await assetReader.sourceMapContents('web/main.dart.lib.js.map'); | ||
expect(result, isNotNull); | ||
|
||
// 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); | ||
}); | ||
}); | ||
}); | ||
} |
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.
unmatched quotes