-
Notifications
You must be signed in to change notification settings - Fork 10
/
get-source.js
176 lines (146 loc) · 7.38 KB
/
get-source.js
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"use strict";
/* ------------------------------------------------------------------------ */
const { assign } = Object,
isBrowser = (typeof window !== 'undefined') && (window.window === window) && window.navigator,
SourceMapConsumer = require ('source-map').SourceMapConsumer,
SyncPromise = require ('./impl/SyncPromise'),
path = require ('./impl/path'),
dataURIToBuffer = require ('data-uri-to-buffer'),
nodeRequire = isBrowser ? null : module.require
/* ------------------------------------------------------------------------ */
const memoize = f => {
const m = x => (x in m.cache) ? m.cache[x] : (m.cache[x] = f(x))
m.forgetEverything = () => { m.cache = Object.create (null) }
m.cache = Object.create (null)
return m
}
function impl (fetchFile, sync) {
const PromiseImpl = sync ? SyncPromise : Promise
const SourceFileMemoized = memoize (path => SourceFile (path, fetchFile (path)))
function SourceFile (srcPath, text) {
if (text === undefined) return SourceFileMemoized (path.resolve (srcPath))
return PromiseImpl.resolve (text).then (text => {
let file
let lines
let resolver
let _resolve = loc => (resolver = resolver || SourceMapResolverFromFetchedFile (file)) (loc)
return (file = {
path: srcPath,
text,
get lines () { return lines = (lines || text.split ('\n')) },
resolve (loc) {
const result = _resolve (loc)
if (sync) {
try { return SyncPromise.valueFrom (result) }
catch (e) { return assign ({}, loc, { error: e }) }
} else {
return Promise.resolve (result)
}
},
_resolve,
})
})
}
function SourceMapResolverFromFetchedFile (file) {
/* Extract the last sourceMap occurence (TODO: support multiple sourcemaps) */
const re = /\u0023 sourceMappingURL=(.+)\n?/g
let lastMatch = undefined
while (true) {
const match = re.exec (file.text)
if (match) lastMatch = match
else break
}
const url = lastMatch && lastMatch[1]
const defaultResolver = loc => assign ({}, loc, {
sourceFile: file,
sourceLine: (file.lines[loc.line - 1] || '')
})
return url ? SourceMapResolver (file.path, url, defaultResolver)
: defaultResolver
}
function SourceMapResolver (originalFilePath, sourceMapPath, fallbackResolve) {
const srcFile = sourceMapPath.startsWith ('data:')
? SourceFile (originalFilePath, dataURIToBuffer (sourceMapPath).toString ())
: SourceFile (path.relativeToFile (originalFilePath, sourceMapPath))
const parsedMap = srcFile.then (f => SourceMapConsumer (JSON.parse (f.text)))
const sourceFor = memoize (function sourceFor (filePath) {
return srcFile.then (f => {
const fullPath = path.relativeToFile (f.path, filePath)
return parsedMap.then (x => SourceFile (
fullPath,
x.sourceContentFor (filePath, true /* return null on missing */) || undefined))
})
})
return loc => parsedMap.then (x => {
const originalLoc = x.originalPositionFor (loc)
return originalLoc.source ? sourceFor (originalLoc.source).then (x =>
x._resolve (assign ({}, loc, {
line: originalLoc.line,
column: originalLoc.column + 1,
name: originalLoc.name
}))
)
: fallbackResolve (loc)
}).catch (e =>
assign (fallbackResolve (loc), { sourceMapError: e }))
}
return assign (function getSource (path) {
const file = SourceFile (path)
if (sync) {
try { return SyncPromise.valueFrom (file) }
catch (e) {
const noFile = {
path,
text: '',
lines: [],
error: e,
resolve (loc) {
return assign ({}, loc, { error: e, sourceLine: '', sourceFile: noFile })
}
}
return noFile
}
}
return file
}, {
resetCache: () => SourceFileMemoized.forgetEverything (),
getCache: () => SourceFileMemoized.cache
})
}
/* ------------------------------------------------------------------------ */
module.exports = impl (function fetchFileSync (path) {
return new SyncPromise (resolve => {
if (isBrowser) {
let xhr = new XMLHttpRequest ()
xhr.open ('GET', path, false /* SYNCHRONOUS XHR FTW :) */)
xhr.send (null)
resolve (xhr.responseText)
} else {
resolve (nodeRequire ('fs').readFileSync (path, { encoding: 'utf8' }))
}
})
}, true)
/* ------------------------------------------------------------------------ */
module.exports.async = impl (function fetchFileAsync (path) {
return new Promise ((resolve, reject) => {
if (isBrowser) {
let xhr = new XMLHttpRequest ()
xhr.open ('GET', path)
xhr.onreadystatechange = event => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
resolve (xhr.responseText)
} else {
reject (new Error (xhr.statusText))
}
}
}
xhr.send (null)
} else {
nodeRequire ('fs').readFile (path, { encoding: 'utf8' }, (e, x) => {
e ? reject (e) : resolve (x)
})
}
})
})
/* ------------------------------------------------------------------------ */