-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs.go
47 lines (38 loc) · 969 Bytes
/
js.go
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
package main
import (
"io/ioutil"
"net/http"
"net/url"
"strings"
)
func fromJs(ctx Context, res *http.Response) {
Info(ctx.Depth, "Processing JavaScript...")
var sourceMapUrl url.URL
str, err := ioutil.ReadAll(res.Body)
if err != nil {
Error(ctx.Depth, "Failed to read body:", err)
return
}
startIndex := strings.LastIndex(string(str), "//# sourceMappingURL=")
if startIndex == -1 {
// fallback if there is no embedded source maps
sourceMapUrl = ctx.Url
sourceMapUrl.Path = sourceMapUrl.Path + ".map"
} else {
after := string(str[startIndex+len("//# sourceMappingURL="):])
ref, err := url.Parse(after)
if err != nil {
Error(ctx.Depth, "Failed to parse JavaScript source map url:", err)
return
}
sourceMapUrl = *ctx.Url.ResolveReference(ref)
}
sourceMapCtx := ctx
sourceMapCtx.Url = sourceMapUrl
sourceMapCtx.Depth++
sourceMap, ok := fetch(sourceMapCtx)
if !ok {
return
}
fromSourceMap(sourceMapCtx, sourceMap)
}