OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 import 'dart:convert' show UTF8; | 5 import 'dart:convert' show ChunkedConversionSink, UTF8; |
6 import 'dart:core' hide Resource; | 6 import 'dart:core' hide Resource; |
7 | 7 |
8 import 'package:analyzer/dart/element/element.dart'; | 8 import 'package:analyzer/dart/element/element.dart'; |
9 import 'package:analyzer/file_system/file_system.dart'; | 9 import 'package:analyzer/file_system/file_system.dart'; |
10 import 'package:analyzer/src/generated/engine.dart'; | 10 import 'package:analyzer/src/generated/engine.dart'; |
11 import 'package:analyzer/src/generated/source.dart'; | 11 import 'package:analyzer/src/generated/source.dart'; |
12 import 'package:analyzer/src/summary/format.dart'; | 12 import 'package:analyzer/src/summary/format.dart'; |
13 import 'package:analyzer/src/summary/idl.dart'; | 13 import 'package:analyzer/src/summary/idl.dart'; |
14 import 'package:analyzer/src/summary/summarize_elements.dart'; | 14 import 'package:analyzer/src/summary/summarize_elements.dart'; |
| 15 import 'package:convert/convert.dart'; |
15 import 'package:crypto/crypto.dart'; | 16 import 'package:crypto/crypto.dart'; |
16 | 17 |
17 /** | 18 /** |
18 * Storage for cache data. | 19 * Storage for cache data. |
19 */ | 20 */ |
20 abstract class CacheStorage { | 21 abstract class CacheStorage { |
21 /** | 22 /** |
22 * Return bytes for the given [key], `null` if [key] is not in the storage. | 23 * Return bytes for the given [key], `null` if [key] is not in the storage. |
23 */ | 24 */ |
24 List<int> get(String key); | 25 List<int> get(String key); |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 _sourceContentMap[source] = content; | 236 _sourceContentMap[source] = content; |
236 } | 237 } |
237 return content; | 238 return content; |
238 } | 239 } |
239 | 240 |
240 /** | 241 /** |
241 * Return the key of the content based [source] information. | 242 * Return the key of the content based [source] information. |
242 */ | 243 */ |
243 String _getCacheSourceContentKey(Source source) { | 244 String _getCacheSourceContentKey(Source source) { |
244 List<int> hash = _getSourceContentHash(source); | 245 List<int> hash = _getSourceContentHash(source); |
245 String hashStr = CryptoUtils.bytesToHex(hash); | 246 String hashStr = hex.encode(hash); |
246 return '$hashStr.content'; | 247 return '$hashStr.content'; |
247 } | 248 } |
248 | 249 |
249 /** | 250 /** |
250 * Get the bundle for the given key. | 251 * Get the bundle for the given key. |
251 */ | 252 */ |
252 PackageBundle _getLibraryBundle(String key) { | 253 PackageBundle _getLibraryBundle(String key) { |
253 PackageBundle bundle = _bundleMap[key]; | 254 PackageBundle bundle = _bundleMap[key]; |
254 if (bundle == null) { | 255 if (bundle == null) { |
255 List<int> bytes = storage.get(key); | 256 List<int> bytes = storage.get(key); |
256 if (bytes == null) { | 257 if (bytes == null) { |
257 return null; | 258 return null; |
258 } | 259 } |
259 bundle = new PackageBundle.fromBuffer(bytes); | 260 bundle = new PackageBundle.fromBuffer(bytes); |
260 _bundleMap[key] = bundle; | 261 _bundleMap[key] = bundle; |
261 } | 262 } |
262 return bundle; | 263 return bundle; |
263 } | 264 } |
264 | 265 |
265 /** | 266 /** |
266 * Return the key of the bundle of the [librarySource]. | 267 * Return the key of the bundle of the [librarySource]. |
267 */ | 268 */ |
268 String _getLibraryBundleKey(Source librarySource) { | 269 String _getLibraryBundleKey(Source librarySource) { |
269 List<int> hash = _getLibraryClosureHash(librarySource); | 270 List<int> hash = _getLibraryClosureHash(librarySource); |
270 String hashStr = CryptoUtils.bytesToHex(hash); | 271 String hashStr = hex.encode(hash); |
271 return '$hashStr.summary'; | 272 return '$hashStr.summary'; |
272 } | 273 } |
273 | 274 |
274 /** | 275 /** |
275 * Return the whole source closure of the library with the given | 276 * Return the whole source closure of the library with the given |
276 * [librarySource]. It includes defining units and parts of the library and | 277 * [librarySource]. It includes defining units and parts of the library and |
277 * of all its directly or indirectly imported or exported libraries. | 278 * of all its directly or indirectly imported or exported libraries. |
278 */ | 279 */ |
279 List<Source> _getLibraryClosure(Source librarySource) { | 280 List<Source> _getLibraryClosure(Source librarySource) { |
280 return _libraryClosureMap.putIfAbsent(librarySource, () { | 281 return _libraryClosureMap.putIfAbsent(librarySource, () { |
281 Set<Source> closure = new Set<Source>(); | 282 Set<Source> closure = new Set<Source>(); |
282 _appendLibraryClosure(closure, librarySource); | 283 _appendLibraryClosure(closure, librarySource); |
283 return closure.toList(); | 284 return closure.toList(); |
284 }); | 285 }); |
285 } | 286 } |
286 | 287 |
287 /** | 288 /** |
288 * Return the [context]-specific hash of the closure of the library with | 289 * Return the [context]-specific hash of the closure of the library with |
289 * the given [librarySource]. | 290 * the given [librarySource]. |
290 */ | 291 */ |
291 List<int> _getLibraryClosureHash(Source librarySource) { | 292 List<int> _getLibraryClosureHash(Source librarySource) { |
292 return _libraryClosureHashMap.putIfAbsent(librarySource, () { | 293 return _libraryClosureHashMap.putIfAbsent(librarySource, () { |
293 List<Source> closure = _getLibraryClosure(librarySource); | 294 List<Source> closure = _getLibraryClosure(librarySource); |
294 MD5 md5 = new MD5(); | 295 |
| 296 Digest digest; |
| 297 |
| 298 var digestSink = new ChunkedConversionSink<Digest>.withCallback( |
| 299 (List<Digest> digests) { |
| 300 digest = digests.single; |
| 301 }); |
| 302 |
| 303 var byteSink = md5.startChunkedConversion(digestSink); |
| 304 |
295 for (Source source in closure) { | 305 for (Source source in closure) { |
296 List<int> sourceHash = _getSourceContentHash(source); | 306 List<int> sourceHash = _getSourceContentHash(source); |
297 md5.add(sourceHash); | 307 byteSink.add(sourceHash); |
298 } | 308 } |
299 md5.add(configSalt); | 309 byteSink.add(configSalt); |
300 return md5.close(); | 310 |
| 311 byteSink.close(); |
| 312 // TODO(paulberry): this call to `close` should not be needed. |
| 313 // Can be removed once |
| 314 // https://github.com/dart-lang/crypto/issues/33 |
| 315 // is fixed – ensure the min version constraint on crypto is updated, tho. |
| 316 // Does not cause any problems in the mean time. |
| 317 digestSink.close(); |
| 318 |
| 319 return digest.bytes; |
301 }); | 320 }); |
302 } | 321 } |
303 | 322 |
304 /** | 323 /** |
305 * Compute a hash of the given [source] contents. | 324 * Compute a hash of the given [source] contents. |
306 */ | 325 */ |
307 List<int> _getSourceContentHash(Source source) { | 326 List<int> _getSourceContentHash(Source source) { |
308 return _sourceContentHashMap.putIfAbsent(source, () { | 327 return _sourceContentHashMap.putIfAbsent(source, () { |
309 String sourceText = source.contents.data; | 328 String sourceText = source.contents.data; |
310 List<int> sourceBytes = UTF8.encode(sourceText); | 329 List<int> sourceBytes = UTF8.encode(sourceText); |
311 return (new MD5()..add(sourceBytes)).close(); | 330 return md5.convert(sourceBytes).bytes; |
312 }); | 331 }); |
313 } | 332 } |
314 | 333 |
315 /** | 334 /** |
316 * Return a source representing the URI that results from resolving the given | 335 * Return a source representing the URI that results from resolving the given |
317 * (possibly relative) [containedUri] against the URI associated with the | 336 * (possibly relative) [containedUri] against the URI associated with the |
318 * [containingSource], whether or not the resulting source exists, or `null` | 337 * [containingSource], whether or not the resulting source exists, or `null` |
319 * if either the [containedUri] is invalid or if it cannot be resolved against | 338 * if either the [containedUri] is invalid or if it cannot be resolved against |
320 * the [containingSource]'s URI. | 339 * the [containingSource]'s URI. |
321 */ | 340 */ |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 */ | 430 */ |
412 final String id; | 431 final String id; |
413 | 432 |
414 /** | 433 /** |
415 * The payload bundle. | 434 * The payload bundle. |
416 */ | 435 */ |
417 final PackageBundle bundle; | 436 final PackageBundle bundle; |
418 | 437 |
419 LibraryBundleWithId(this.source, this.id, this.bundle); | 438 LibraryBundleWithId(this.source, this.id, this.bundle); |
420 } | 439 } |
OLD | NEW |