-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
cdnjs.go
74 lines (64 loc) · 1.84 KB
/
cdnjs.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
package loader
import (
"encoding/json"
"fmt"
"github.com/sirupsen/logrus"
)
type cdnjsEnvelope struct {
Name string `json:"name"`
Filename string `json:"filename"`
Version string `json:"version"`
Assets []struct {
Version string `json:"version"`
Files []string `json:"files"`
}
}
func cdnjs(logger logrus.FieldLogger, path string, parts []string) (string, error) {
name := parts[0]
version := parts[1]
filename := parts[2]
data, err := fetch(logger, "https://api.cdnjs.com/libraries/"+name)
if err != nil {
return "", err
}
var envelope cdnjsEnvelope
if err := json.Unmarshal(data, &envelope); err != nil {
return "", err
}
// CDNJS doesn't actually send 404s, nonexistent libs' data is just *empty*.
if envelope.Name == "" {
return "", fmt.Errorf("cdnjs: no such library: %s", name)
}
// If no version is specified, use the default/latest one.
if version == "" {
version = envelope.Version
}
// If no filename is specified, use the default one, but make sure it actually exists in the
// chosen version (it may have changed name over the years). If not, the first listed file
// that does exist in that version is a pretty safe guess.
if filename == "" {
filename = envelope.Filename
backupFilename := filename
filenameExistsInVersion := false
for _, ver := range envelope.Assets {
if ver.Version != version {
continue
}
if len(ver.Files) == 0 {
return "",
fmt.Errorf("cdnjs: no files for version %s of %s, this is a problem with the library or cdnjs not k6",
version, path)
}
backupFilename = ver.Files[0]
for _, file := range ver.Files {
if file == filename {
filenameExistsInVersion = true
}
}
}
if !filenameExistsInVersion {
filename = backupFilename
}
}
return "https://cdnjs.cloudflare.com/ajax/libs/" + name + "/" + version + "/" + filename, nil
}