diff --git a/README.md b/README.md index 8238a14..31a1f2a 100644 --- a/README.md +++ b/README.md @@ -49,8 +49,9 @@ One repository, one self-contained brick per directory. A Go brick carries its own go.mod and a TypeScript brick its own package.json, each with its own CHANGELOG and lint configuration, released independently under a path-prefixed tag such as `mailkit/v0.1.0` or -`gottext/v0.4.0`. Bricks share code only through those published tags, -never through sibling source, so what you pin is what you get. +`gottext/v0.4.0`. Bricks share code only through those published tags +or the Go pseudo-version of a commit on main, never through sibling +source, so what you pin is what you get. ## Reporting security issues diff --git a/pluginkit/graphwire/CHANGELOG.md b/pluginkit/graphwire/CHANGELOG.md index 19f04d8..9b97270 100644 --- a/pluginkit/graphwire/CHANGELOG.md +++ b/pluginkit/graphwire/CHANGELOG.md @@ -17,6 +17,8 @@ stdlib-only `pluginkit` module so its gqlparser dependency never enters ### Changed - The module needs Go 1.27.4. +- `Run` reads each plugin.json with `wire.Read` and rejects every manifest `wire.Read` rejects, with its error, a breaking change. +- The module requires `github.com/gopherium/framework/pluginkit` for that reader, so an application on this version takes that pluginkit too. ## [0.4.0] - 2026-09-26 diff --git a/pluginkit/graphwire/go.mod b/pluginkit/graphwire/go.mod index befe406..4070040 100644 --- a/pluginkit/graphwire/go.mod +++ b/pluginkit/graphwire/go.mod @@ -2,4 +2,7 @@ module github.com/gopherium/framework/pluginkit/graphwire go 1.27.4 -require github.com/vektah/gqlparser/v2 v2.5.36 +require ( + github.com/gopherium/framework/pluginkit v0.12.1-0.20261121220358-91b488ad6dbb + github.com/vektah/gqlparser/v2 v2.5.36 +) diff --git a/pluginkit/graphwire/go.sum b/pluginkit/graphwire/go.sum index a6f0b67..75831cf 100644 --- a/pluginkit/graphwire/go.sum +++ b/pluginkit/graphwire/go.sum @@ -1,5 +1,7 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gopherium/framework/pluginkit v0.12.1-0.20261121220358-91b488ad6dbb h1:AYsAXgC+XOYwvRVuG3eFYe3MajstMW5VF3BohM8yMqs= +github.com/gopherium/framework/pluginkit v0.12.1-0.20261121220358-91b488ad6dbb/go.mod h1:8ruHaYkA80It7CN0/UxZxJTrdAds9DUDOwqOB7Td49g= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= diff --git a/pluginkit/graphwire/graphwire.go b/pluginkit/graphwire/graphwire.go index 3859e12..51e8241 100644 --- a/pluginkit/graphwire/graphwire.go +++ b/pluginkit/graphwire/graphwire.go @@ -5,7 +5,6 @@ package graphwire import ( - "encoding/json" "errors" "fmt" "go/format" @@ -13,12 +12,13 @@ import ( "os" "path" "path/filepath" - "regexp" "sort" "strings" "github.com/vektah/gqlparser/v2/ast" "github.com/vektah/gqlparser/v2/parser" + + "github.com/gopherium/framework/pluginkit/wire" ) // Config parameterizes the resolver root generation for the consuming application. @@ -66,12 +66,10 @@ func validateConfig(cfg Config) error { return nil } -// manifest is the subset of plugin.json the graph wiring reads. +// manifest is one plugin.json as wire reads it, with the plugin root it came from. type manifest struct { - ID string `json:"id"` - Backend string `json:"backend"` - GraphQL bool `json:"graphql"` - root string + wire.Manifest + root string } // roots returns the plugin root directories scanned in order, defaulting to plugins. @@ -103,8 +101,6 @@ func loadRoots(dir string, roots []string) ([]manifest, error) { return manifests, nil } -var idPattern = regexp.MustCompile(`^[a-z][a-z0-9-]*$`) - // loadManifests loads the plugin manifest in each subdirectory of dir. func loadManifests(dir string) ([]manifest, error) { entries, err := os.ReadDir(dir) @@ -116,8 +112,7 @@ func loadManifests(dir string) ([]manifest, error) { if !entry.IsDir() { continue } - path := filepath.Join(dir, entry.Name(), "plugin.json") - m, err := loadManifest(path, entry.Name()) + m, err := loadManifest(dir, entry.Name()) if err != nil { return nil, err } @@ -126,29 +121,25 @@ func loadManifests(dir string) ([]manifest, error) { return manifests, nil } -// loadManifest reads and validates one plugin manifest. -func loadManifest(path, dir string) (manifest, error) { - data, err := os.ReadFile(path) - if err != nil { - return manifest{}, fmt.Errorf("graphwire: %s: %w", path, err) - } - var m manifest - if err := json.Unmarshal(data, &m); err != nil { - return manifest{}, fmt.Errorf("graphwire: %s: %w", path, err) +// loadManifest reads and checks the plugin.json in directory name under dir through wire, its path before any error. +func loadManifest(dir, name string) (manifest, error) { + read, err := wire.Read(os.DirFS(filepath.Join(dir, name))) + if err == nil { + err = validateManifest(read, name) } - if err := validateManifest(m, path, dir); err != nil { - return manifest{}, err + if err != nil { + return manifest{}, fmt.Errorf("graphwire: %s: %w", filepath.Join(dir, name, "plugin.json"), err) } - return m, nil + return manifest{Manifest: read}, nil } -// validateManifest checks the manifest fields the graph wiring relies on. -func validateManifest(m manifest, path, dir string) error { - if !idPattern.MatchString(m.ID) || m.ID != dir { - return fmt.Errorf("graphwire: %s: id %q does not match directory %q", path, m.ID, dir) +// validateManifest checks that m names its directory dir and that a graphql plugin has a backend. +func validateManifest(m wire.Manifest, dir string) error { + if m.ID != dir { + return fmt.Errorf("id %q does not match directory %q", m.ID, dir) } if m.GraphQL && m.Backend == "" { - return fmt.Errorf("graphwire: %s: graphql plugins require a backend", path) + return errors.New("graphql plugins require a backend") } return nil } diff --git a/pluginkit/graphwire/graphwire_test.go b/pluginkit/graphwire/graphwire_test.go index 3b7f13d..342cb43 100644 --- a/pluginkit/graphwire/graphwire_test.go +++ b/pluginkit/graphwire/graphwire_test.go @@ -8,6 +8,8 @@ import ( "slices" "strings" "testing" + + "github.com/gopherium/framework/pluginkit/wire" ) var testConfig = Config{ @@ -694,3 +696,75 @@ func TestGraphQLPluginsRequireABackend(t *testing.T) { t.Errorf("Run() error = %v, want the missing backend reported", err) } } + +func TestRunRejectsEveryManifestWireRejects(t *testing.T) { + t.Parallel() + + cases := map[string]struct { + dir string + manifest string + want string + }{ + "graphql spelled in another case": {"alpha", + `{"id": "alpha", "name": "Alpha", "backend": "example.com/myapp/plugins/alpha", "GraphQL": true}`, + `the key "GraphQL" must be spelled "graphql"`}, + "graphql repeated": {"alpha", + `{"id": "alpha", "name": "Alpha", "backend": "example.com/alpha", "graphql": false, "graphql": true}`, + `the key "graphql" appears twice`}, + "backend repeated": {"alpha", + `{"id": "alpha", "name": "Alpha", "backend": "example.com/one", "backend": "example.com/two", "graphql": true}`, + `the key "backend" appears twice`}, + "text that is not UTF-8": {"alpha", + `{"id": "alpha", "name": "Alph` + "\xff" + `", "backend": "example.com/alpha", "graphql": true}`, + "the manifest holds text that is not valid UTF-8"}, + "an id out of the id pattern": {"Alpha", + `{"id": "Alpha", "name": "Alpha", "backend": "example.com/myapp/plugins/alpha", "graphql": true}`, + `id "Alpha" must match ^[a-z][a-z0-9-]*$`}, + "an id naming another directory": {"alpha", + `{"id": "beta", "name": "Beta", "backend": "example.com/myapp/plugins/beta", "graphql": true}`, + `id "beta" does not match directory "alpha"`}, + "no name": {"alpha", + `{"id": "alpha", "backend": "example.com/myapp/plugins/alpha", "graphql": true}`, + "name is required"}, + } + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + t.Parallel() + + root := t.TempDir() + writeCore(t, root) + writePlugin(t, root, tc.dir, tc.manifest, alphaSchema) + if err := os.MkdirAll(filepath.Join(root, "cmd", "myapp"), 0o755); err != nil { + t.Fatalf("creating the output directory: %v", err) + } + path := filepath.Join(root, "plugins", tc.dir, "plugin.json") + + err := Run(root, testConfig) + + if want := "graphwire: " + path + ": " + tc.want; err == nil || err.Error() != want { + t.Errorf("Run() error = %v, want %q", err, want) + } + _, wireErr := wire.Load(root, []string{"plugins"}, nil) + if want := "pluginwire: " + path + ": " + tc.want; wireErr == nil || wireErr.Error() != want { + t.Errorf("wire.Load() error = %v, want %q", wireErr, want) + } + }) + } +} + +func TestRunChecksTheManifestBeforeItsDirectory(t *testing.T) { + t.Parallel() + + root := t.TempDir() + writeCore(t, root) + writePlugin(t, root, "alpha", + `{"id": "beta", "backend": "example.com/myapp/plugins/beta", "graphql": true}`, + alphaSchema) + + err := Run(root, testConfig) + + want := "graphwire: " + filepath.Join(root, "plugins", "alpha", "plugin.json") + ": name is required" + if err == nil || err.Error() != want { + t.Errorf("Run() error = %v, want %q", err, want) + } +}