Line data Source code
1 : // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2 : // for details. All rights reserved. Use of this source code is governed by a
3 : // BSD-style license that can be found in the LICENSE file.
4 :
5 : import 'package:collection/collection.dart';
6 : import 'package:package_resolver/package_resolver.dart';
7 : import 'package:source_map_stack_trace/source_map_stack_trace.dart' as mapper;
8 : import 'package:source_maps/source_maps.dart';
9 :
10 : /// A class for mapping JS stack traces to Dart stack traces using source maps.
11 : class StackTraceMapper {
12 : /// The parsed source map.
13 : ///
14 : /// This is initialized lazily in `mapStackTrace()`.
15 : Mapping _mapping;
16 :
17 : /// The package resolution information passed to dart2js.
18 : final SyncPackageResolver _packageResolver;
19 :
20 : /// The URL of the SDK root from which dart2js loaded its sources.
21 : final Uri _sdkRoot;
22 :
23 : /// The contents of the source map.
24 : final String _mapContents;
25 :
26 : /// The URL of the source map.
27 : final Uri _mapUrl;
28 :
29 : StackTraceMapper(this._mapContents,
30 : {Uri mapUrl, SyncPackageResolver packageResolver, Uri sdkRoot})
31 : : _mapUrl = mapUrl,
32 : _packageResolver = packageResolver,
33 0 : _sdkRoot = sdkRoot;
34 :
35 : /// Converts [trace] into a Dart stack trace.
36 : StackTrace mapStackTrace(StackTrace trace) {
37 0 : _mapping ??= parseExtended(_mapContents, mapUrl: _mapUrl);
38 0 : return mapper.mapStackTrace(_mapping, trace,
39 0 : packageResolver: _packageResolver, sdkRoot: _sdkRoot);
40 : }
41 :
42 : /// Returns a Map representation which is suitable for JSON serialization.
43 : Map<String, dynamic> serialize() {
44 0 : return {
45 0 : 'mapContents': _mapContents,
46 0 : 'sdkRoot': _sdkRoot?.toString(),
47 : 'packageConfigMap':
48 0 : _serializePackageConfigMap(_packageResolver.packageConfigMap),
49 0 : 'packageRoot': _packageResolver.packageRoot?.toString(),
50 0 : 'mapUrl': _mapUrl?.toString(),
51 : };
52 : }
53 :
54 : /// Returns a [StackTraceMapper] contained in the provided serialized
55 : /// representation.
56 : static StackTraceMapper deserialize(Map serialized) {
57 : if (serialized == null) return null;
58 0 : String packageRoot = serialized['packageRoot'] as String ?? '';
59 0 : return new StackTraceMapper(serialized['mapContents'],
60 0 : sdkRoot: Uri.parse(serialized['sdkRoot']),
61 0 : packageResolver: packageRoot.isNotEmpty
62 0 : ? new SyncPackageResolver.root(Uri.parse(serialized['packageRoot']))
63 0 : : new SyncPackageResolver.config(
64 0 : _deserializePackageConfigMap(serialized['packageConfigMap'])),
65 0 : mapUrl: Uri.parse(serialized['mapUrl']));
66 : }
67 :
68 : /// Converts a [packageConfigMap] into a format suitable for JSON
69 : /// serialization.
70 : static Map<String, String> _serializePackageConfigMap(
71 : Map<String, Uri> packageConfigMap) {
72 : if (packageConfigMap == null) return null;
73 0 : return mapMap(packageConfigMap, value: (_, value) => '$value');
74 : }
75 :
76 : /// Converts a serialized package config map into a format suitable for
77 : /// the [PackageResolver]
78 : static Map<String, Uri> _deserializePackageConfigMap(
79 : Map<String, String> serialized) {
80 : if (serialized == null) return null;
81 0 : return mapMap(serialized, value: (_, value) => Uri.parse(value));
82 : }
83 : }
|