-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
source_map_utils.ts
117 lines (86 loc) · 3.01 KB
/
source_map_utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import _ from 'lodash'
import { SourceMapConsumer } from 'source-map'
import type { BasicSourceMapConsumer } from 'source-map'
// @ts-ignore
import mappingsWasm from 'source-map/lib/mappings.wasm'
import $utils from './utils'
const sourceMapExtractionRegex = /\/\/\s*[@#]\s*sourceMappingURL\s*=\s*(data:[^\s]*)/g
const regexDataUrl = /data:[^;\n]+(?:;charset=[^;\n]+)?;base64,([a-zA-Z0-9+/]+={0,2})/ // matches data urls
let sourceMapConsumers: Record<string, BasicSourceMapConsumer> = {}
const initializeSourceMapConsumer = async (script, sourceMap): Promise<BasicSourceMapConsumer | null> => {
if (!sourceMap) return null
// @ts-ignore
SourceMapConsumer.initialize({
'lib/mappings.wasm': mappingsWasm,
})
const consumer = await new SourceMapConsumer(sourceMap)
sourceMapConsumers[script.fullyQualifiedUrl] = consumer
return consumer
}
const extractSourceMap = (fileContents) => {
let dataUrlMatch
try {
let sourceMapMatch = fileContents.match(sourceMapExtractionRegex)
if (!sourceMapMatch) return null
const url = _.last(sourceMapMatch) as any
dataUrlMatch = url.match(regexDataUrl)
} catch (err) {
// ignore unable to match regex. there's nothing we
// can do about it and we don't want to thrown an exception
if (err.message === 'Maximum call stack size exceeded') return null
throw err
}
if (!dataUrlMatch) return null
const sourceMapBase64 = dataUrlMatch[1]
const sourceMap = base64toJs(sourceMapBase64)
return sourceMap
}
const getSourceContents = (filePath, sourceFile) => {
if (!sourceMapConsumers[filePath]) return null
try {
return sourceMapConsumers[filePath].sourceContentFor(sourceFile)
} catch (err) {
// ignore the sourceFile not being in the source map. there's nothing we
// can do about it and we don't want to thrown an exception
if (err && err.message.indexOf('not in the SourceMap') > -1) return
throw err
}
}
const getSourcePosition = (filePath, position) => {
const sourceMapConsumer = sourceMapConsumers[filePath]
if (!sourceMapConsumer) return null
const { source, line, column } = sourceMapConsumer.originalPositionFor(position)
if (!source || line == null || column == null) return
// if the file is outside of the projectRoot
// originalPositionFor will not provide the correct relative path
// https://github.com/cypress-io/cypress/issues/16255
// @ts-expect-error
const sourceIndex = sourceMapConsumer._absoluteSources.indexOf(source)
// @ts-expect-error
const file = sourceMapConsumer._sources.at(sourceIndex)
return {
file,
line,
column,
}
}
const base64toJs = (base64) => {
const mapString = $utils.decodeBase64Unicode(base64)
try {
return JSON.parse(mapString)
} catch (err) {
return null
}
}
const destroySourceMapConsumers = () => {
Object.values(sourceMapConsumers).forEach((consumer) => {
consumer.destroy()
})
}
export default {
getSourcePosition,
getSourceContents,
extractSourceMap,
initializeSourceMapConsumer,
destroySourceMapConsumers,
}