get-source.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. "use strict";
  2. /* ------------------------------------------------------------------------ */
  3. const { assign } = Object,
  4. isBrowser = (typeof window !== 'undefined') && (window.window === window) && window.navigator,
  5. SourceMapConsumer = require ('source-map').SourceMapConsumer,
  6. SyncPromise = require ('./impl/SyncPromise'),
  7. path = require ('./impl/path'),
  8. dataURIToBuffer = require ('data-uri-to-buffer'),
  9. nodeRequire = isBrowser ? null : module.require
  10. /* ------------------------------------------------------------------------ */
  11. const memoize = f => {
  12. const m = x => (x in m.cache) ? m.cache[x] : (m.cache[x] = f(x))
  13. m.forgetEverything = () => { m.cache = Object.create (null) }
  14. m.cache = Object.create (null)
  15. return m
  16. }
  17. function impl (fetchFile, sync) {
  18. const PromiseImpl = sync ? SyncPromise : Promise
  19. const SourceFileMemoized = memoize (path => SourceFile (path, fetchFile (path)))
  20. function SourceFile (srcPath, text) {
  21. if (text === undefined) return SourceFileMemoized (path.resolve (srcPath))
  22. return PromiseImpl.resolve (text).then (text => {
  23. let file
  24. let lines
  25. let resolver
  26. let _resolve = loc => (resolver = resolver || SourceMapResolverFromFetchedFile (file)) (loc)
  27. return (file = {
  28. path: srcPath,
  29. text,
  30. get lines () { return lines = (lines || text.split ('\n')) },
  31. resolve (loc) {
  32. const result = _resolve (loc)
  33. if (sync) {
  34. try { return SyncPromise.valueFrom (result) }
  35. catch (e) { return assign ({}, loc, { error: e }) }
  36. } else {
  37. return Promise.resolve (result)
  38. }
  39. },
  40. _resolve,
  41. })
  42. })
  43. }
  44. function SourceMapResolverFromFetchedFile (file) {
  45. /* Extract the last sourceMap occurence (TODO: support multiple sourcemaps) */
  46. const re = /\u0023 sourceMappingURL=(.+)\n?/g
  47. let lastMatch = undefined
  48. while (true) {
  49. const match = re.exec (file.text)
  50. if (match) lastMatch = match
  51. else break
  52. }
  53. const url = lastMatch && lastMatch[1]
  54. const defaultResolver = loc => assign ({}, loc, {
  55. sourceFile: file,
  56. sourceLine: (file.lines[loc.line - 1] || '')
  57. })
  58. return url ? SourceMapResolver (file.path, url, defaultResolver)
  59. : defaultResolver
  60. }
  61. function SourceMapResolver (originalFilePath, sourceMapPath, fallbackResolve) {
  62. const srcFile = sourceMapPath.startsWith ('data:')
  63. ? SourceFile (originalFilePath, dataURIToBuffer (sourceMapPath).toString ())
  64. : SourceFile (path.relativeToFile (originalFilePath, sourceMapPath))
  65. const parsedMap = srcFile.then (f => SourceMapConsumer (JSON.parse (f.text)))
  66. const sourceFor = memoize (function sourceFor (filePath) {
  67. return srcFile.then (f => {
  68. const fullPath = path.relativeToFile (f.path, filePath)
  69. return parsedMap.then (x => SourceFile (
  70. fullPath,
  71. x.sourceContentFor (filePath, true /* return null on missing */) || undefined))
  72. })
  73. })
  74. return loc => parsedMap.then (x => {
  75. const originalLoc = x.originalPositionFor (loc)
  76. return originalLoc.source ? sourceFor (originalLoc.source).then (x =>
  77. x._resolve (assign ({}, loc, {
  78. line: originalLoc.line,
  79. column: originalLoc.column + 1,
  80. name: originalLoc.name
  81. }))
  82. )
  83. : fallbackResolve (loc)
  84. }).catch (e =>
  85. assign (fallbackResolve (loc), { sourceMapError: e }))
  86. }
  87. return assign (function getSource (path) {
  88. const file = SourceFile (path)
  89. if (sync) {
  90. try { return SyncPromise.valueFrom (file) }
  91. catch (e) {
  92. const noFile = {
  93. path,
  94. text: '',
  95. lines: [],
  96. error: e,
  97. resolve (loc) {
  98. return assign ({}, loc, { error: e, sourceLine: '', sourceFile: noFile })
  99. }
  100. }
  101. return noFile
  102. }
  103. }
  104. return file
  105. }, {
  106. resetCache: () => SourceFileMemoized.forgetEverything (),
  107. getCache: () => SourceFileMemoized.cache
  108. })
  109. }
  110. /* ------------------------------------------------------------------------ */
  111. module.exports = impl (function fetchFileSync (path) {
  112. return new SyncPromise (resolve => {
  113. if (isBrowser) {
  114. let xhr = new XMLHttpRequest ()
  115. xhr.open ('GET', path, false /* SYNCHRONOUS XHR FTW :) */)
  116. xhr.send (null)
  117. resolve (xhr.responseText)
  118. } else {
  119. resolve (nodeRequire ('fs').readFileSync (path, { encoding: 'utf8' }))
  120. }
  121. })
  122. }, true)
  123. /* ------------------------------------------------------------------------ */
  124. module.exports.async = impl (function fetchFileAsync (path) {
  125. return new Promise ((resolve, reject) => {
  126. if (isBrowser) {
  127. let xhr = new XMLHttpRequest ()
  128. xhr.open ('GET', path)
  129. xhr.onreadystatechange = event => {
  130. if (xhr.readyState === 4) {
  131. if (xhr.status === 200) {
  132. resolve (xhr.responseText)
  133. } else {
  134. reject (new Error (xhr.statusText))
  135. }
  136. }
  137. }
  138. xhr.send (null)
  139. } else {
  140. nodeRequire ('fs').readFile (path, { encoding: 'utf8' }, (e, x) => {
  141. e ? reject (e) : resolve (x)
  142. })
  143. }
  144. })
  145. })
  146. /* ------------------------------------------------------------------------ */