2022-03-30 11:42:47 +03:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 21:20:29 +03:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-03-30 11:42:47 +03:00
|
|
|
|
|
|
|
package npm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
packages_model "code.gitea.io/gitea/models/packages"
|
|
|
|
npm_module "code.gitea.io/gitea/modules/packages/npm"
|
|
|
|
)
|
|
|
|
|
|
|
|
func createPackageMetadataResponse(registryURL string, pds []*packages_model.PackageDescriptor) *npm_module.PackageMetadata {
|
|
|
|
sort.Slice(pds, func(i, j int) bool {
|
|
|
|
return pds[i].SemVer.LessThan(pds[j].SemVer)
|
|
|
|
})
|
|
|
|
|
|
|
|
versions := make(map[string]*npm_module.PackageMetadataVersion)
|
|
|
|
distTags := make(map[string]string)
|
|
|
|
for _, pd := range pds {
|
|
|
|
versions[pd.SemVer.String()] = createPackageMetadataVersion(registryURL, pd)
|
|
|
|
|
2022-07-28 06:59:39 +03:00
|
|
|
for _, pvp := range pd.VersionProperties {
|
2022-03-30 11:42:47 +03:00
|
|
|
if pvp.Name == npm_module.TagProperty {
|
|
|
|
distTags[pvp.Value] = pd.Version.Version
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
latest := pds[len(pds)-1]
|
|
|
|
|
|
|
|
metadata := latest.Metadata.(*npm_module.Metadata)
|
|
|
|
|
|
|
|
return &npm_module.PackageMetadata{
|
|
|
|
ID: latest.Package.Name,
|
|
|
|
Name: latest.Package.Name,
|
|
|
|
DistTags: distTags,
|
|
|
|
Description: metadata.Description,
|
|
|
|
Readme: metadata.Readme,
|
|
|
|
Homepage: metadata.ProjectURL,
|
|
|
|
Author: npm_module.User{Name: metadata.Author},
|
|
|
|
License: metadata.License,
|
|
|
|
Versions: versions,
|
2023-03-17 21:39:19 +03:00
|
|
|
Repository: metadata.Repository,
|
2022-03-30 11:42:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createPackageMetadataVersion(registryURL string, pd *packages_model.PackageDescriptor) *npm_module.PackageMetadataVersion {
|
|
|
|
hashBytes, _ := hex.DecodeString(pd.Files[0].Blob.HashSHA512)
|
|
|
|
|
|
|
|
metadata := pd.Metadata.(*npm_module.Metadata)
|
|
|
|
|
|
|
|
return &npm_module.PackageMetadataVersion{
|
2022-09-03 01:00:18 +03:00
|
|
|
ID: fmt.Sprintf("%s@%s", pd.Package.Name, pd.Version.Version),
|
|
|
|
Name: pd.Package.Name,
|
|
|
|
Version: pd.Version.Version,
|
|
|
|
Description: metadata.Description,
|
|
|
|
Author: npm_module.User{Name: metadata.Author},
|
|
|
|
Homepage: metadata.ProjectURL,
|
|
|
|
License: metadata.License,
|
|
|
|
Dependencies: metadata.Dependencies,
|
|
|
|
DevDependencies: metadata.DevelopmentDependencies,
|
|
|
|
PeerDependencies: metadata.PeerDependencies,
|
|
|
|
OptionalDependencies: metadata.OptionalDependencies,
|
|
|
|
Readme: metadata.Readme,
|
2022-10-08 08:24:44 +03:00
|
|
|
Bin: metadata.Bin,
|
2022-03-30 11:42:47 +03:00
|
|
|
Dist: npm_module.PackageDistribution{
|
|
|
|
Shasum: pd.Files[0].Blob.HashSHA1,
|
|
|
|
Integrity: "sha512-" + base64.StdEncoding.EncodeToString(hashBytes),
|
|
|
|
Tarball: fmt.Sprintf("%s/%s/-/%s/%s", registryURL, url.QueryEscape(pd.Package.Name), url.PathEscape(pd.Version.Version), url.PathEscape(pd.Files[0].File.LowerName)),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-09-24 14:24:33 +03:00
|
|
|
|
|
|
|
func createPackageSearchResponse(pds []*packages_model.PackageDescriptor, total int64) *npm_module.PackageSearch {
|
|
|
|
objects := make([]*npm_module.PackageSearchObject, 0, len(pds))
|
|
|
|
for _, pd := range pds {
|
|
|
|
metadata := pd.Metadata.(*npm_module.Metadata)
|
|
|
|
|
|
|
|
scope := metadata.Scope
|
|
|
|
if scope == "" {
|
|
|
|
scope = "unscoped"
|
|
|
|
}
|
|
|
|
|
|
|
|
objects = append(objects, &npm_module.PackageSearchObject{
|
|
|
|
Package: &npm_module.PackageSearchPackage{
|
|
|
|
Scope: scope,
|
|
|
|
Name: metadata.Name,
|
|
|
|
Version: pd.Version.Version,
|
|
|
|
Date: pd.Version.CreatedUnix.AsLocalTime(),
|
|
|
|
Description: metadata.Description,
|
|
|
|
Author: npm_module.User{Name: metadata.Author},
|
|
|
|
Publisher: npm_module.User{Name: pd.Owner.Name},
|
|
|
|
Maintainers: []npm_module.User{}, // npm cli needs this field
|
|
|
|
Keywords: metadata.Keywords,
|
|
|
|
Links: &npm_module.PackageSearchPackageLinks{
|
|
|
|
Registry: pd.FullWebLink(),
|
|
|
|
Homepage: metadata.ProjectURL,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return &npm_module.PackageSearch{
|
|
|
|
Objects: objects,
|
|
|
|
Total: total,
|
|
|
|
}
|
|
|
|
}
|