Skip to content
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 3 commits into from
Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dwds/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 1.0.0-dev

- Remove dependency on `package:build_daemon`.
- Add 'FrontendServerAssetReader` for use with Frontend Server builds.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unmatched quotes


**Breaking Changes:**
- No longer use the `BuildResult` abstraction from `package:build_daemon` but
Expand Down
2 changes: 2 additions & 0 deletions dwds/lib/dwds.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export 'src/connections/app_connection.dart' show AppConnection;
export 'src/connections/debug_connection.dart' show DebugConnection;
export 'src/handlers/dev_handler.dart' show AppConnectionException;
export 'src/readers/asset_reader.dart' show AssetReader;
export 'src/readers/frontend_server_asset_reader.dart'
show FrontendServerAssetReader;
export 'src/readers/proxy_server_asset_reader.dart' show ProxyServerAssetReader;
export 'src/services/chrome_proxy_service.dart' show ChromeDebugException;

Expand Down
78 changes: 78 additions & 0 deletions dwds/lib/src/readers/frontend_server_asset_reader.dart
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`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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) =>
Copy link
Contributor

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.

Copy link
Member Author

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.

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return?

Copy link
Member Author

Choose a reason for hiding this comment

The 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;
Copy link
Contributor

Choose a reason for hiding this comment

The 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());
}
}
}
1 change: 1 addition & 0 deletions dwds/test/fixtures/main.dart.dill.json
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]}}
1 change: 1 addition & 0 deletions dwds/test/fixtures/main.dart.dill.map

Large diffs are not rendered by default.

88 changes: 88 additions & 0 deletions dwds/test/readers/frontend_server_asset_reader_test.dart
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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't need any of the p.absolute(p.relative( here. But you should do p.join('test', 'fixtures').

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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);
});
});
});
}