-
Notifications
You must be signed in to change notification settings - Fork 3
/
task_maps.go
75 lines (63 loc) · 1.51 KB
/
task_maps.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
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
package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"os"
"path"
"strings"
)
// TaskMaps consumes source maps URLs and extracts them into the file system
type TaskMaps struct {
Output string
In chan *url.URL
Out chan *url.URL
}
func (TaskMaps) Name() string {
return "maps"
}
func (task *TaskMaps) Finish() {
close(task.Out)
}
func (task *TaskMaps) URLs() <-chan *url.URL {
return task.In
}
func (task *TaskMaps) Run(surl *url.URL) error {
resp, err := http.Get(surl.String())
if err != nil {
return fmt.Errorf("make http request: %v", err)
}
if resp.StatusCode >= 300 {
return fmt.Errorf("invalid response: %s", resp.Status)
}
var m SourceMap
err = json.NewDecoder(resp.Body).Decode(&m)
if err != nil {
return fmt.Errorf("read JSON: %v", err)
}
for i, fname := range m.FileNames {
fname = strings.ReplaceAll(fname, "../", ".")
fname = strings.ReplaceAll(fname, "webpack://", "")
fname = strings.ReplaceAll(fname, "://", "")
fname = path.Join(task.Output, surl.Hostname(), fname)
if i >= len(m.Contents) {
return errors.New("sources is longer than sourcesContent")
}
if strings.HasPrefix(fname, "external ") {
return errors.New("external source maps unsupported")
}
parent, _ := path.Split(fname)
err = os.MkdirAll(parent, 0770)
if err != nil {
return fmt.Errorf("create dir: %v", err)
}
err = os.WriteFile(fname, []byte(m.Contents[i]), 0660)
if err != nil {
return fmt.Errorf("write file: %v", err)
}
}
task.Out <- surl
return nil
}