| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /// Standalone utility that manages loading source maps for all Dart scripts | 5 /// Standalone utility that manages loading source maps for all Dart scripts |
| 6 /// on the page compiled with DDC. | 6 /// on the page compiled with DDC. |
| 7 /// | 7 /// |
| 8 /// Example JavaScript usage: | 8 /// Example JavaScript usage: |
| 9 /// $dartStackTraceUtility.addLoadedListener(function() { | 9 /// $dartStackTraceUtility.addLoadedListener(function() { |
| 10 /// // All Dart source maps are now loaded. It is now safe to start your | 10 /// // All Dart source maps are now loaded. It is now safe to start your |
| 11 /// // Dart application compiled with DDC. | 11 /// // Dart application compiled with DDC. |
| 12 /// dart_library.start('your_dart_application'); | 12 /// dart_library.start('your_dart_application'); |
| 13 /// }) | 13 /// }) |
| 14 /// | 14 /// |
| 15 /// If $dartStackTraceUtility is set, the dart:core StackTrace class calls | 15 /// If $dartStackTraceUtility is set, the dart:core StackTrace class calls |
| 16 /// $dartStackTraceUtility.mapper(someJSStackTrace) | 16 /// $dartStackTraceUtility.mapper(someJSStackTrace) |
| 17 /// to apply source maps. | 17 /// to apply source maps. |
| 18 /// | 18 /// |
| 19 /// This utility can be compiled to JavaScript using Dart2JS while the rest | 19 /// This utility can be compiled to JavaScript using Dart2JS while the rest |
| 20 /// of the application is compiled with DDC or could be compiled with DDC. | 20 /// of the application is compiled with DDC or could be compiled with DDC. |
| 21 | 21 |
| 22 @JS() | 22 @JS() |
| 23 library stack_trace_mapper; | 23 library stack_trace_mapper; |
| 24 | 24 |
| 25 import 'dart:async'; | |
| 26 import 'dart:html'; | |
| 27 | |
| 28 import 'package:js/js.dart'; | 25 import 'package:js/js.dart'; |
| 29 import 'package:path/path.dart' as path; | 26 import 'package:path/path.dart' as path; |
| 30 import 'package:source_map_stack_trace/source_map_stack_trace.dart'; | 27 import 'package:source_map_stack_trace/source_map_stack_trace.dart'; |
| 31 import 'package:source_maps/source_maps.dart'; | 28 import 'package:source_maps/source_maps.dart'; |
| 32 import 'package:source_span/source_span.dart'; | 29 import 'package:source_span/source_span.dart'; |
| 33 import 'package:stack_trace/stack_trace.dart'; | 30 import 'package:stack_trace/stack_trace.dart'; |
| 34 | 31 |
| 35 typedef void ReadyCallback(); | 32 typedef void ReadyCallback(); |
| 36 | 33 |
| 37 /// Global object DDC uses to see if a stack trace utility has been registered. | 34 /// Global object DDC uses to see if a stack trace utility has been registered. |
| 38 @JS(r'$dartStackTraceUtility') | 35 @JS(r'$dartStackTraceUtility') |
| 39 external set dartStackTraceUtility(DartStackTraceUtility value); | 36 external set dartStackTraceUtility(DartStackTraceUtility value); |
| 40 | 37 |
| 41 typedef String StackTraceMapper(String stackTrace); | 38 typedef String StackTraceMapper(String stackTrace); |
| 42 typedef dynamic SourceMapProvider(String modulePath); | 39 typedef dynamic SourceMapProvider(String modulePath); |
| 43 typedef String SetSourceMapProvider(SourceMapProvider provider); | 40 typedef void SetSourceMapProvider(SourceMapProvider provider); |
| 44 | 41 |
| 45 @JS() | 42 @JS() |
| 46 @anonymous | 43 @anonymous |
| 47 class DartStackTraceUtility { | 44 class DartStackTraceUtility { |
| 48 external factory DartStackTraceUtility( | 45 external factory DartStackTraceUtility( |
| 49 {StackTraceMapper mapper, SetSourceMapProvider setSourceMapProvider}); | 46 {StackTraceMapper mapper, SetSourceMapProvider setSourceMapProvider}); |
| 50 } | 47 } |
| 51 | 48 |
| 52 @JS('JSON.stringify') | 49 @JS('JSON.stringify') |
| 53 external String _stringify(dynamic json); | 50 external String _stringify(dynamic json); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 var span = _bundle.spanFor(line, column, files: files, uri: uri); | 86 var span = _bundle.spanFor(line, column, files: files, uri: uri); |
| 90 // TODO(jacobr): we shouldn't have to filter out invalid sourceUrl entries | 87 // TODO(jacobr): we shouldn't have to filter out invalid sourceUrl entries |
| 91 // here. | 88 // here. |
| 92 if (span == null || span.start.sourceUrl == null) return null; | 89 if (span == null || span.start.sourceUrl == null) return null; |
| 93 var pathSegments = span.start.sourceUrl.pathSegments; | 90 var pathSegments = span.start.sourceUrl.pathSegments; |
| 94 if (pathSegments.isNotEmpty && pathSegments.last == 'null') return null; | 91 if (pathSegments.isNotEmpty && pathSegments.last == 'null') return null; |
| 95 return span; | 92 return span; |
| 96 } | 93 } |
| 97 } | 94 } |
| 98 | 95 |
| 99 String _toSourceMapLocation(String url) { | |
| 100 // The url may have cache busting query parameters which we need to maintain | |
| 101 // in the source map url. | |
| 102 // For example: | |
| 103 // http://localhost/foo.js?cachebusting=23419 | |
| 104 // Should get source map | |
| 105 // http://localhost/foo.js.map?cachebusting=23419 | |
| 106 var uri = Uri.parse(url); | |
| 107 return uri.replace(path: '${uri.path}.map').toString(); | |
| 108 } | |
| 109 | |
| 110 LazyMapping _mapping; | 96 LazyMapping _mapping; |
| 111 | 97 |
| 112 String mapper(String rawStackTrace) { | 98 String mapper(String rawStackTrace) { |
| 113 if (_mapping == null) { | 99 if (_mapping == null) { |
| 114 // This should not happen if the user has waited for the ReadyCallback | 100 // This should not happen if the user has waited for the ReadyCallback |
| 115 // to start the application. | 101 // to start the application. |
| 116 throw new StateError('Source maps are not done loading.'); | 102 throw new StateError('Source maps are not done loading.'); |
| 117 } | 103 } |
| 118 return mapStackTrace(_mapping, new Trace.parse(rawStackTrace)).toString(); | 104 return mapStackTrace(_mapping, new Trace.parse(rawStackTrace)).toString(); |
| 119 } | 105 } |
| 120 | 106 |
| 121 void setSourceMapProvider(SourceMapProvider provider) async { | 107 void setSourceMapProvider(SourceMapProvider provider) { |
| 122 _mapping = new LazyMapping(provider); | 108 _mapping = new LazyMapping(provider); |
| 123 } | 109 } |
| 124 | 110 |
| 125 main() { | 111 main() { |
| 126 // Register with DDC. | 112 // Register with DDC. |
| 127 dartStackTraceUtility = new DartStackTraceUtility( | 113 dartStackTraceUtility = new DartStackTraceUtility( |
| 128 mapper: allowInterop(mapper), | 114 mapper: allowInterop(mapper), |
| 129 setSourceMapProvider: allowInterop(setSourceMapProvider)); | 115 setSourceMapProvider: allowInterop(setSourceMapProvider)); |
| 130 } | 116 } |
| OLD | NEW |