Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

source-maps.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. 'use strict'
  2. const convertSourceMap = require('convert-source-map')
  3. const libCoverage = require('istanbul-lib-coverage')
  4. const libSourceMaps = require('istanbul-lib-source-maps')
  5. const fs = require('./fs-promises')
  6. const os = require('os')
  7. const path = require('path')
  8. const pMap = require('p-map')
  9. class SourceMaps {
  10. constructor (opts) {
  11. this.cache = opts.cache
  12. this.cacheDirectory = opts.cacheDirectory
  13. this.loadedMaps = {}
  14. this._sourceMapCache = libSourceMaps.createSourceMapStore()
  15. }
  16. cachedPath (source, hash) {
  17. return path.join(
  18. this.cacheDirectory,
  19. `${path.parse(source).name}-${hash}.map`
  20. )
  21. }
  22. purgeCache () {
  23. this._sourceMapCache = libSourceMaps.createSourceMapStore()
  24. this.loadedMaps = {}
  25. }
  26. extract (code, filename) {
  27. const sourceMap = convertSourceMap.fromSource(code) || convertSourceMap.fromMapFileSource(code, path.dirname(filename))
  28. return sourceMap ? sourceMap.toObject() : undefined
  29. }
  30. registerMap (filename, hash, sourceMap) {
  31. if (!sourceMap) {
  32. return
  33. }
  34. if (this.cache && hash) {
  35. const mapPath = this.cachedPath(filename, hash)
  36. fs.writeFileSync(mapPath, JSON.stringify(sourceMap))
  37. } else {
  38. this._sourceMapCache.registerMap(filename, sourceMap)
  39. }
  40. }
  41. async remapCoverage (obj) {
  42. const transformed = await this._sourceMapCache.transformCoverage(
  43. libCoverage.createCoverageMap(obj)
  44. )
  45. return transformed.data
  46. }
  47. async reloadCachedSourceMaps (report) {
  48. await pMap(
  49. Object.entries(report),
  50. async ([absFile, fileReport]) => {
  51. if (!fileReport || !fileReport.contentHash) {
  52. return
  53. }
  54. const hash = fileReport.contentHash
  55. if (!(hash in this.loadedMaps)) {
  56. try {
  57. const mapPath = this.cachedPath(absFile, hash)
  58. this.loadedMaps[hash] = JSON.parse(await fs.readFile(mapPath, 'utf8'))
  59. } catch (e) {
  60. // set to false to avoid repeatedly trying to load the map
  61. this.loadedMaps[hash] = false
  62. }
  63. }
  64. if (this.loadedMaps[hash]) {
  65. this._sourceMapCache.registerMap(absFile, this.loadedMaps[hash])
  66. }
  67. },
  68. { concurrency: os.cpus().length }
  69. )
  70. }
  71. }
  72. module.exports = SourceMaps