-
Notifications
You must be signed in to change notification settings - Fork 24
/
npm.go
112 lines (99 loc) · 2.28 KB
/
npm.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
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
package npm
import (
"encoding/json"
"encoding/xml"
"fmt"
"net/http"
"time"
"github.com/ossf/package-feeds/feeds"
)
const (
FeedName = "npm"
)
var (
baseURL = "https://registry.npmjs.org/-/rss"
versionURL = "https://registry.npmjs.org/"
httpClient = &http.Client{
Timeout: 10 * time.Second,
}
)
type Response struct {
Packages []*Package `xml:"channel>item"`
}
type Package struct {
Title string `xml:"title"`
CreatedDate rfc1123Time `xml:"pubDate"`
Link string `xml:"link"`
Version string
}
type PackageVersion struct {
ID string `json:"_id"`
Rev string `json:"_rev"`
Name string `json:"name"`
DistTags struct {
Latest string `json:"latest"`
} `json:"dist-tags"`
}
type rfc1123Time struct {
time.Time
}
func (t *rfc1123Time) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var marshaledTime string
err := d.DecodeElement(&marshaledTime, &start)
if err != nil {
return err
}
decodedTime, err := time.Parse(time.RFC1123, marshaledTime)
if err != nil {
return err
}
*t = rfc1123Time{decodedTime}
return nil
}
func fetchPackages() ([]*Package, error) {
resp, err := httpClient.Get(baseURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
rssResponse := &Response{}
err = xml.NewDecoder(resp.Body).Decode(rssResponse)
if err != nil {
return nil, err
}
return rssResponse.Packages, nil
}
// Gets the package version from the NPM.
func fetchVersionInformation(packageName string) (string, error) {
resp, err := httpClient.Get(fmt.Sprintf("%s/%s", versionURL, packageName))
if err != nil {
return "", err
}
defer resp.Body.Close()
v := &PackageVersion{}
err = json.NewDecoder(resp.Body).Decode(v)
if err != nil {
return "", err
}
return v.DistTags.Latest, nil
}
type Feed struct{}
func (feed Feed) Latest(cutoff time.Time) ([]*feeds.Package, error) {
pkgs := []*feeds.Package{}
packages, err := fetchPackages()
if err != nil {
return pkgs, err
}
for _, pkg := range packages {
v, err := fetchVersionInformation(pkg.Title)
if err != nil {
return pkgs, fmt.Errorf("error in fetching version information: %w", err)
}
pkg, err := feeds.NewPackage(pkg.CreatedDate.Time, cutoff, pkg.Title, v, FeedName)
if err != nil {
continue
}
pkgs = append(pkgs, pkg)
}
return pkgs, nil
}