-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnpm.go
458 lines (427 loc) · 13.4 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package npm
import (
"archive/tar"
"compress/gzip"
"context"
"encoding/json"
"fmt"
"io"
"io/fs"
"net/http"
"os"
"path"
"path/filepath"
"sort"
"strings"
"github.com/Masterminds/semver/v3"
"github.com/matthewmueller/glob"
"golang.org/x/sync/errgroup"
"golang.org/x/sync/singleflight"
)
type Manifest struct {
Name string `json:"name,omitempty"`
Main string `json:"main,omitempty"`
Browser string `json:"browser,omitempty"`
Files []string `json:"files,omitempty"`
Imports map[string]map[string]string `json:"imports,omitempty"`
// TODO: this can also be a string or list
Exports map[string]string `json:"exports,omitempty"`
Dependencies map[string]string `json:"dependencies,omitempty"`
}
func Install(ctx context.Context, dir string, packages ...string) error {
eg := new(errgroup.Group)
sg := new(singleflight.Group)
if len(packages) == 0 {
manifestPath := filepath.Join(dir, "package.json")
manifest, err := os.ReadFile(manifestPath)
if err != nil {
return fmt.Errorf("unable to read package.json: %w", err)
}
var pkg struct {
Dependencies map[string]string `json:"dependencies,omitempty"`
}
if err := json.Unmarshal(manifest, &pkg); err != nil {
return fmt.Errorf("unable to unmarshal package.json: %w", err)
}
for dep, version := range pkg.Dependencies {
if isLocal(version) || isAbsolute(version) {
packages = append(packages, version)
continue
}
pkgname := fmt.Sprintf("%s@%s", dep, version)
packages = append(packages, pkgname)
}
}
for _, pkg := range packages {
pkg := pkg
eg.Go(func() error {
return install(ctx, sg, dir, pkg)
})
}
return eg.Wait()
}
func install(ctx context.Context, sg *singleflight.Group, dir string, pkgname string) error {
pkg, err := resolvePackage(dir, pkgname)
if err != nil {
return err
}
// Only install a package once
// TODO: this may need to get smarter to handle different versions
_, err, _ = sg.Do(pkg.Key(), func() (interface{}, error) {
if err := pkg.Install(ctx, sg, dir); err != nil {
return nil, fmt.Errorf("npm install %s: %w", pkgname, err)
}
return nil, nil
})
return err
}
type installable interface {
Key() string
Install(ctx context.Context, sg *singleflight.Group, to string) error
}
func parseScope(pkgname string) (scope string, name string) {
index := strings.LastIndex(pkgname, "/")
if index == -1 {
return "", pkgname
}
return pkgname[:index], pkgname[index+1:]
}
// Version resolves the version of a package. To get the latest you can do
// `version, err := npm.Version(ctx, "preact", "*")`.
func Version(ctx context.Context, pkgname, constraint string) (string, error) {
version, err := resolveVersion(pkgname, constraint)
if err != nil {
return "", err
}
return version, nil
}
func resolvePackage(dir, pkgname string) (installable, error) {
if isLocal(pkgname) {
return readLocalPackage(filepath.Join(dir, pkgname))
} else if isAbsolute(pkgname) {
return readLocalPackage(pkgname)
}
index := strings.LastIndex(pkgname, "@")
if index == -1 {
return nil, fmt.Errorf("npm: unable to install %[1]s because it's missing the version (e.g. %[1][email protected])", pkgname)
}
pkgName, version := pkgname[:index], pkgname[index+1:]
scope, name := parseScope(pkgName)
if version == "" {
return nil, fmt.Errorf("npm: unable to install %[1]s because it's missing the version (e.g. %[1][email protected])", pkgname)
} else if version == "latest" {
return nil, fmt.Errorf("npm: unable to install %[1]s because tagged versions aren't supported yet", pkgname)
}
version, err := resolveVersion(pkgName, version)
if err != nil {
return nil, err
}
return &remotePackage{
Scope: scope,
Name: name,
Version: version,
}, nil
}
type remotePackage struct {
Scope string `json:"scope,omitempty"`
Name string `json:"name,omitempty"`
Version string `json:"version,omitempty"`
}
var _ installable = (*remotePackage)(nil)
func (p *remotePackage) Key() string {
if p.Scope == "" {
return p.Name
}
return fmt.Sprintf("%s/%s", p.Scope, p.Name)
}
func (p *remotePackage) url() string {
if p.Scope == "" {
return fmt.Sprintf(`https://registry.npmjs.org/%[1]s/-/%[1]s-%[2]s.tgz`, p.Name, p.Version)
}
return fmt.Sprintf(`https://registry.npmjs.org/%[1]s/%[2]s/-/%[2]s-%[3]s.tgz`, p.Scope, p.Name, p.Version)
}
func (p *remotePackage) dir(root string) string {
if p.Scope == "" {
return filepath.Join(root, "node_modules", p.Name)
}
return filepath.Join(root, "node_modules", p.Scope, p.Name)
}
func (p *remotePackage) Install(ctx context.Context, sg *singleflight.Group, to string) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, p.url(), nil)
if err != nil {
return fmt.Errorf("unable to create request for %s: %w", p.Name, err)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("unable to download %s: %w", p.Name, err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
return fmt.Errorf("unexpected status code while installing %s: %d", p.Name, res.StatusCode)
}
gzipReader, err := gzip.NewReader(res.Body)
if err != nil {
return fmt.Errorf("unable to create gzip reader: %w", err)
}
defer gzipReader.Close()
tarReader := tar.NewReader(gzipReader)
for {
header, err := tarReader.Next()
if err == io.EOF {
break
} else if err != nil {
return fmt.Errorf("unable to get next header: %w", err)
}
fileInfo := header.FileInfo()
dir := filepath.Join(p.dir(to), rootless(filepath.Dir(header.Name)))
filename := filepath.Join(dir, fileInfo.Name())
if fileInfo.IsDir() {
if err := os.MkdirAll(filename, fileInfo.Mode()); err != nil {
return fmt.Errorf("unable to make directory %q from tarball: %w", filename, err)
}
continue
}
if err = os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("unable to make directory for file %q from tarball: %w", filename, err)
}
file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fileInfo.Mode())
if err != nil {
return fmt.Errorf("unable to open file %q from tarball: %w", filename, err)
}
if written, err := io.Copy(file, tarReader); err != nil {
return fmt.Errorf("unable to copy file %q from tarball: %w", filename, err)
} else if written != header.Size {
return fmt.Errorf("unable to copy file %q from tarball: wrote %d bytes, expected %d", filename, written, header.Size)
}
if err = file.Close(); err != nil {
return fmt.Errorf("unable to close file %q from tarball: %w", filename, err)
}
}
// Install dependencies
manifestPath := filepath.Join(p.dir(to), "package.json")
manifest, err := os.ReadFile(manifestPath)
if err != nil {
return fmt.Errorf("unable to read package.json: %w", err)
}
var pkg struct {
Dependencies map[string]string `json:"dependencies,omitempty"`
}
if err := json.Unmarshal(manifest, &pkg); err != nil {
return fmt.Errorf("unable to unmarshal package.json: %w", err)
}
eg := new(errgroup.Group)
for dep, version := range pkg.Dependencies {
pkgname := fmt.Sprintf("%s@%s", dep, version)
eg.Go(func() error {
return install(ctx, sg, to, pkgname)
})
}
return eg.Wait()
}
func resolveVersions(pkgName string) (semver.Collection, error) {
req, err := http.NewRequest(http.MethodGet, `https://registry.npmjs.org/`+pkgName, nil)
if err != nil {
return nil, fmt.Errorf("unable to create request to resolve version for %s: %w", pkgName, err)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("unable to preform request to resolve version for %s: %w", pkgName, err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, fmt.Errorf("unexpected status code while resolving version for %s: %d", pkgName, res.StatusCode)
}
var pkg struct {
Versions map[string]struct{} `json:"versions,omitempty"`
}
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("unable to read body while resolving version for %s: %w", pkgName, err)
}
if err := json.Unmarshal(body, &pkg); err != nil {
return nil, fmt.Errorf("unable to unmarshal body while resolving version for %s: %w", pkgName, err)
}
var versions semver.Collection
for version := range pkg.Versions {
v, err := semver.NewVersion(version)
if err != nil {
// Ignore errors that might be in the NPM registry.
continue
}
versions = append(versions, v)
}
sort.Sort(versions)
return versions, nil
}
func resolveVersion(pkgName, constraint string) (string, error) {
versions, err := resolveVersions(pkgName)
if err != nil {
return "", fmt.Errorf("unable to resolve versions for %s: %w", pkgName, err)
}
checker, err := semver.NewConstraint(constraint)
if err != nil {
return "", fmt.Errorf("unable to create a new constraint for %s@%s: %w", pkgName, constraint, err)
}
for i := len(versions) - 1; i >= 0; i-- {
if checker.Check(versions[i]) {
return versions[i].String(), nil
}
}
return "", fmt.Errorf("unable to resolve version for %s@%s: no matching version found", pkgName, constraint)
}
func readLocalPackage(pkgdir string) (*localPackage, error) {
manifestPath := filepath.Join(pkgdir, "package.json")
manifest, err := os.ReadFile(manifestPath)
if err != nil {
return nil, fmt.Errorf("unable to read package.json: %w", err)
}
var pkg struct {
Name string `json:"name,omitempty"`
}
if err := json.Unmarshal(manifest, &pkg); err != nil {
return nil, fmt.Errorf("unable to unmarshal package.json: %w", err)
}
return &localPackage{
Name: pkg.Name,
Path: pkgdir,
}, nil
}
type localPackage struct {
Name string `json:"name,omitempty"`
Path string `json:"path,omitempty"`
}
var _ installable = (*localPackage)(nil)
func (p *localPackage) Key() string {
return p.Name
}
// Ignored paths when installing local packages.
// Based on: https://github.com/npm/npm-packlist/blob/8c182a187e71c9f39b5558c2f6846c9161286441/lib/index.js#L14
var ignorePaths = map[string]bool{
"node_modules": true,
".git": true,
".DS_Store": true,
".npmignore": true,
".gitignore": true,
".npmrc": true,
"npm-debug.log": true,
}
// Install local package to the given directory. This is a very limited
// implementation.
// TODO: better align with: https://github.com/npm/npm-packlist
func (p *localPackage) Install(ctx context.Context, sg *singleflight.Group, to string) error {
pkgPath := p.Path
if filepath.IsLocal(pkgPath) {
pkgPath = filepath.Join(to, p.Path)
}
manifestName := "package.json"
manifestJson, err := os.ReadFile(filepath.Join(pkgPath, manifestName))
if err != nil {
return fmt.Errorf("unable to read %s for %s: %w", manifestName, p.Path, err)
}
var manifest Manifest
if err := json.Unmarshal(manifestJson, &manifest); err != nil {
return fmt.Errorf("unable to unmarshal %s for %s: %w", manifestName, p.Path, err)
}
fileMap := map[string]bool{
manifestName: true,
}
if manifest.Main != "" {
fileMap[filepath.Clean(manifest.Main)] = true
}
if manifest.Browser != "" {
fileMap[filepath.Clean(manifest.Browser)] = true
}
for _, file := range manifest.Files {
err := glob.Walk(filepath.Join(pkgPath, file+"**"), func(path string, de fs.DirEntry, err error) error {
if err != nil {
return fmt.Errorf("error while walking %s to install local package: %w", path, err)
}
name := de.Name()
if hiddenPath(name) || ignorePaths[name] {
if de.IsDir() {
return fs.SkipDir
}
return nil
} else if de.IsDir() {
return nil
}
rel, err := filepath.Rel(pkgPath, path)
if err != nil {
return fmt.Errorf("unable to get relative path for %s to install local package: %w", path, err)
}
fileMap[rel] = true
return nil
})
if err != nil {
return err
}
}
for _, imp := range manifest.Imports {
for _, p := range imp {
fileMap[filepath.Clean(p)] = true
}
}
for _, p := range manifest.Exports {
fileMap[filepath.Clean(p)] = true
}
files := make([]string, len(fileMap))
i := 0
for file := range fileMap {
files[i] = file
i++
}
nodeDir := filepath.Join(to, "node_modules", manifest.Name)
if err := copyFiles(pkgPath, nodeDir, files...); err != nil {
return fmt.Errorf("unable to copy files to install local package: %w", err)
}
eg := new(errgroup.Group)
for dep, version := range manifest.Dependencies {
pkgname := fmt.Sprintf("%s@%s", dep, version)
eg.Go(func() error {
return install(ctx, sg, to, pkgname)
})
}
return eg.Wait()
}
func copyFiles(from, to string, files ...string) error {
eg := new(errgroup.Group)
for _, file := range files {
file := file
eg.Go(func() error {
return copyFile(filepath.Join(from, file), filepath.Join(to, file))
})
}
return eg.Wait()
}
func copyFile(src, dst string) error {
srcFile, err := os.Open(src)
if err != nil {
return fmt.Errorf("unable to open %s to copy: %w", src, err)
}
defer srcFile.Close()
if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
return fmt.Errorf("unable to make directory for %s to copy: %w", dst, err)
}
dstFile, err := os.Create(dst)
if err != nil {
return fmt.Errorf("unable to create %s to copy: %w", dst, err)
}
defer dstFile.Close()
if _, err = io.Copy(dstFile, srcFile); err != nil {
return fmt.Errorf("unable to copy %s to %s: %w", src, dst, err)
}
return nil
}
func rootless(fpath string) string {
parts := strings.Split(fpath, string(filepath.Separator))
return path.Join(parts[1:]...)
}
func isLocal(pkgname string) bool {
return strings.HasPrefix(pkgname, ".")
}
func isAbsolute(pkgname string) bool {
return strings.HasPrefix(pkgname, "/")
}
func hiddenPath(p string) bool {
return strings.HasPrefix(p, ".")
}