mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-09-03 06:35:00 +08:00
feat(pluginstore): add support for third-party plugin store sources and enhance plugin management
This commit is contained in:
@@ -13,10 +13,19 @@ import (
|
||||
|
||||
const (
|
||||
DefaultRegistryURL = "https://raw.githubusercontent.com/router-for-me/CLIProxyAPI-Plugins-Store/main/registry.json"
|
||||
DefaultSourceID = "official"
|
||||
DefaultSourceName = "Official"
|
||||
SchemaVersion = 1
|
||||
)
|
||||
|
||||
var pluginVersionPattern = regexp.MustCompile(`^[0-9][0-9A-Za-z.+-]*$`)
|
||||
var sourceIDPattern = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]*$`)
|
||||
|
||||
type Source struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
type Registry struct {
|
||||
SchemaVersion int `json:"schema_version"`
|
||||
@@ -36,6 +45,42 @@ type Plugin struct {
|
||||
Tags []string `json:"tags,omitempty"`
|
||||
}
|
||||
|
||||
func DefaultSource() Source {
|
||||
return Source{
|
||||
ID: DefaultSourceID,
|
||||
Name: DefaultSourceName,
|
||||
URL: DefaultRegistryURL,
|
||||
}
|
||||
}
|
||||
|
||||
func NormalizeSources(sources []Source) ([]Source, error) {
|
||||
out := []Source{DefaultSource()}
|
||||
seen := map[string]struct{}{DefaultSourceID: {}}
|
||||
for index, source := range sources {
|
||||
source.ID = strings.TrimSpace(source.ID)
|
||||
source.Name = strings.TrimSpace(source.Name)
|
||||
source.URL = strings.TrimSpace(source.URL)
|
||||
if source.URL == "" {
|
||||
continue
|
||||
}
|
||||
if source.ID == "" {
|
||||
source.ID = fmt.Sprintf("source-%d", index+1)
|
||||
}
|
||||
if !sourceIDPattern.MatchString(source.ID) {
|
||||
return nil, fmt.Errorf("invalid plugin store source id %q", source.ID)
|
||||
}
|
||||
if _, exists := seen[source.ID]; exists {
|
||||
return nil, fmt.Errorf("duplicate plugin store source id %q", source.ID)
|
||||
}
|
||||
seen[source.ID] = struct{}{}
|
||||
if source.Name == "" {
|
||||
source.Name = source.ID
|
||||
}
|
||||
out = append(out, source)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func ParseRegistry(data []byte) (Registry, error) {
|
||||
var registry Registry
|
||||
decoder := json.NewDecoder(bytes.NewReader(data))
|
||||
|
||||
@@ -160,6 +160,43 @@ func TestValidateRegistryRejectsInvalidEntries(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeSourcesAppendsToDefaultSource(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
sources, errNormalize := NormalizeSources([]Source{{
|
||||
ID: " community ",
|
||||
Name: " Community ",
|
||||
URL: " https://community.example/registry.json ",
|
||||
}})
|
||||
if errNormalize != nil {
|
||||
t.Fatalf("NormalizeSources() error = %v", errNormalize)
|
||||
}
|
||||
if len(sources) != 2 {
|
||||
t.Fatalf("sources len = %d, want 2", len(sources))
|
||||
}
|
||||
if sources[0].ID != DefaultSourceID || sources[0].URL != DefaultRegistryURL {
|
||||
t.Fatalf("default source = %#v", sources[0])
|
||||
}
|
||||
if sources[1].ID != "community" || sources[1].Name != "Community" || sources[1].URL != "https://community.example/registry.json" {
|
||||
t.Fatalf("third-party source = %#v", sources[1])
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeSourcesRejectsInvalidSourceIDs(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, errNormalize := NormalizeSources([]Source{{
|
||||
ID: "../community",
|
||||
URL: "https://community.example/registry.json",
|
||||
}})
|
||||
if errNormalize == nil {
|
||||
t.Fatal("NormalizeSources() error = nil")
|
||||
}
|
||||
if !strings.Contains(errNormalize.Error(), "invalid plugin store source id") {
|
||||
t.Fatalf("NormalizeSources() error = %v, want invalid source id", errNormalize)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGitHubRepositoryPartsRejectsNonRepositoryURLs(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user