-
Notifications
You must be signed in to change notification settings - Fork 327
/
npm_registry.go
175 lines (145 loc) · 4.23 KB
/
npm_registry.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
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
package datasource
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"sync"
"time"
"github.com/tidwall/gjson"
"golang.org/x/exp/maps"
)
type NpmRegistryAPIClient struct {
// Registries from the npmrc config
// This should only be written to when the client is first being created.
// Other functions should not modify it & it is not covered by the mutex.
registries NpmRegistryConfig
// cache fields
mu sync.Mutex
cacheTimestamp *time.Time // If set, this means we loaded from a cache
details map[string]npmRegistryPackageDetails
}
type npmRegistryPackageDetails struct {
// Only cache the info needed for the DependencyClient
Versions map[string]npmRegistryDependencies
Tags map[string]string
}
func NewNpmRegistryAPIClient(workdir string) (*NpmRegistryAPIClient, error) {
registries, err := LoadNpmRegistryConfig(workdir)
if err != nil {
return nil, err
}
return &NpmRegistryAPIClient{
registries: registries,
details: make(map[string]npmRegistryPackageDetails),
}, nil
}
type npmRegistryVersions struct {
Versions []string
Tags map[string]string
}
func (c *NpmRegistryAPIClient) Versions(ctx context.Context, pkg string) (npmRegistryVersions, error) {
pkgDetails, err := c.getPackageDetails(ctx, pkg)
if err != nil {
return npmRegistryVersions{}, err
}
return npmRegistryVersions{
Versions: maps.Keys(pkgDetails.Versions),
Tags: pkgDetails.Tags,
}, nil
}
type npmRegistryDependencies struct {
// TODO: These maps should preserve ordering from JSON response
Dependencies map[string]string
DevDependencies map[string]string
PeerDependencies map[string]string
OptionalDependencies map[string]string
BundleDependencies []string
}
func (c *NpmRegistryAPIClient) Dependencies(ctx context.Context, pkg, version string) (npmRegistryDependencies, error) {
pkgDetails, err := c.getPackageDetails(ctx, pkg)
if err != nil {
return npmRegistryDependencies{}, err
}
if deps, ok := pkgDetails.Versions[version]; ok {
return deps, nil
}
return npmRegistryDependencies{}, fmt.Errorf("no version %s for package %s", version, pkg)
}
func (c *NpmRegistryAPIClient) FullJSON(ctx context.Context, pkg, version string) (gjson.Result, error) {
return c.get(ctx, pkg, version)
}
func (c *NpmRegistryAPIClient) get(ctx context.Context, urlComponents ...string) (gjson.Result, error) {
req, err := c.registries.BuildRequest(ctx, urlComponents...)
if err != nil {
return gjson.Result{}, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return gjson.Result{}, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return gjson.Result{}, errors.New(resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return gjson.Result{}, err
}
res := gjson.ParseBytes(body)
return res, nil
}
func (c *NpmRegistryAPIClient) getPackageDetails(ctx context.Context, pkg string) (npmRegistryPackageDetails, error) {
c.mu.Lock()
pkgData, ok := c.details[pkg]
c.mu.Unlock()
if ok {
return pkgData, nil
}
// Not cached, make the network request
jsonData, err := c.get(ctx, pkg)
if err != nil {
return npmRegistryPackageDetails{}, err
}
versions := make(map[string]npmRegistryDependencies)
for v, data := range jsonData.Get("versions").Map() {
versions[v] = npmRegistryDependencies{
Dependencies: jsonToStringMap(data.Get("dependencies")),
DevDependencies: jsonToStringMap(data.Get("devDependencies")),
PeerDependencies: jsonToStringMap(data.Get("peerDependencies")),
OptionalDependencies: jsonToStringMap(data.Get("optionalDependencies")),
BundleDependencies: jsonToStringSlice(data.Get("bundleDependencies")),
}
}
pkgData = npmRegistryPackageDetails{
Versions: versions,
Tags: jsonToStringMap(jsonData.Get("dist-tags")),
}
c.mu.Lock()
c.details[pkg] = pkgData
c.mu.Unlock()
return pkgData, nil
}
func jsonToStringSlice(v gjson.Result) []string {
arr := v.Array()
if len(arr) == 0 {
return nil
}
strs := make([]string, len(arr))
for i, s := range arr {
strs[i] = s.String()
}
return strs
}
func jsonToStringMap(v gjson.Result) map[string]string {
mp := v.Map()
if len(mp) == 0 {
return nil
}
strs := make(map[string]string)
for k, s := range mp {
strs[k] = s.String()
}
return strs
}