Files
Deluan Quintão 42c7046e9f
Pipeline: Test, Lint, Build / Get version info (push) Canceled after 0s
Pipeline: Test, Lint, Build / Lint Go code (push) Canceled after 0s
Pipeline: Test, Lint, Build / Validate DB migrations (push) Canceled after 0s
Pipeline: Test, Lint, Build / Test Go code (push) Canceled after 0s
Pipeline: Test, Lint, Build / Test Go plugins (push) Canceled after 0s
Pipeline: Test, Lint, Build / Report coverage (push) Canceled after 0s
Pipeline: Test, Lint, Build / Test Go code (Windows) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Test JS code (push) Canceled after 0s
Pipeline: Test, Lint, Build / Lint i18n files (push) Canceled after 0s
Pipeline: Test, Lint, Build / Check Docker configuration (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (darwin/amd64) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (darwin/arm64) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (linux/386) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (linux/amd64) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (linux/arm/v5) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (linux/arm/v6) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (linux/arm/v7) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (linux/arm64) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (linux/riscv64) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (windows/386) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (windows/amd64) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Push to GHCR (push) Canceled after 0s
Pipeline: Test, Lint, Build / Push to Docker Hub () (push) Canceled after 0s
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build Windows installers (push) Canceled after 0s
Pipeline: Test, Lint, Build / Package/Release (push) Canceled after 0s
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Canceled after 0s
feat(plugins): generate stream support for host services in ndpgen (#6562)
2026-11-27 21:20:09 -05:00
..
…

ndpgen

Navidrome Plugin Development Kit (PDK) code generator. It reads Go interface definitions with special annotations and generates client wrappers for WASM plugins.

This tool is the unified code generator that handle both host function wrappers and capability wrappers.

Usage

ndpgen -input <dir> -output <dir> [-package <name>] [-v] [-dry-run] [-host-only] [-go] [-rust]

Flags

Flag Description Default
-input Directory containing Go source files with annotated interfaces Required
-output Directory where generated files will be written Same as input
-package Package name for generated files Inferred from output
-v Verbose output false
-dry-run Parse and validate without writing files false
-host-only Generate only host function wrappers (capability support TBD) true
-go Generate Go client wrappers true*
-rust Generate Rust client wrappers false

* -go is enabled by default when -rust is not specified. Use -go -rust to generate both languages.

Example

go run ./plugins/cmd/ndpgen \
  -input ./plugins/host \
  -output ./plugins/pdk

Annotations

//nd:hostservice

Marks an interface as a host service that will have wrappers generated.

//nd:hostservice name=<ServiceName> permission=<permission>
type MyService interface { ... }
Parameter Description Required
name Service name used in generated type names and function prefixes Yes
permission Permission required by plugins to use this service Yes

//nd:hostfunc

Marks a method within a host service interface for export to plugins.

//nd:hostfunc [name=<export_name>]
MethodName(ctx context.Context, ...) (result Type, err error)
Parameter Description Required
name Custom export name (default: <servicename>_<methodname> in lowercase) No

Input Format

Host service interfaces must follow these conventions:

  1. First parameter must be context.Context - Required for all methods
  2. Last return value should be error - For proper error handling
  3. Annotations must be on consecutive lines - No blank comment lines between doc and annotation

Example Interface

package host

import "context"

// SubsonicAPIService provides access to Navidrome's Subsonic API.
// This documentation becomes part of the generated code.
//nd:hostservice name=SubsonicAPI permission=subsonicapi
type SubsonicAPIService interface {
    // Call executes a Subsonic API request and returns the response.
    //nd:hostfunc
    Call(ctx context.Context, uri string) (responseJSON string, err error)
}

Generated Output

Go Client Library (Go/TinyGo WASM)

Generated files are named nd_host_<servicename>.go (lowercase) and placed in $output/go/host/. The $output/go/ directory becomes a complete Go module (github.com/navidrome/navidrome/plugins/pdk/go) with package name host, intended for import by Navidrome plugins built with TinyGo.

The generator creates:

  • nd_host_<servicename>.go - Client wrapper code (WASM build)
  • nd_host_<servicename>_stub.go - Mock implementations for non-WASM platforms (testing)
  • doc.go - Package documentation listing all available services
  • go.mod - Go module file with required dependencies

Each service file includes:

  • // Code generated by ndpgen. DO NOT EDIT. header
  • Required imports (encoding/json, errors, github.com/extism/go-pdk)
  • //go:wasmimport declarations for each host function
  • Response struct types and any struct definitions from the service
  • Wrapper functions that handle memory allocation and JSON parsing

Testing Plugins with Mocks

The stub files (*_stub.go) contain testify/mock implementations that allow plugin authors to unit test their code on non-WASM platforms.

Each host service has:

  • A private mock struct embedding mock.Mock
  • An exported auto-instantiated mock instance (e.g., host.CacheMock, host.ArtworkMock)
  • Wrapper functions that delegate to the mock

Example: Testing a plugin that uses the Cache service

package myplugin

import (
    "testing"
    
    "github.com/navidrome/navidrome/plugins/pdk/go/host"
)

func TestMyPluginFunction(t *testing.T) {
    // Set expectations on the mock
    host.CacheMock.On("GetString", "my-key").Return("cached-value", true, nil)
    host.CacheMock.On("SetString", "new-key", "new-value", int64(3600)).Return(nil)
    
    // Call your plugin code that uses host.CacheGetString and host.CacheSetString
    result := myPluginFunction()
    
    // Assert the result
    if result != "expected" {
        t.Errorf("unexpected result: %s", result)
    }
    
    // Verify all expected calls were made
    host.CacheMock.AssertExpectations(t)
}

Resetting mocks between tests:

If you need to reset mock state between tests, testify's mock doesn't have a built-in reset. Either use separate test functions (testify automatically resets between test runs), or create a helper to set up fresh expectations.

Rust Client Library

When using -rust, Rust client files are generated in a rust/ subdirectory.

Supported Types

ndpgen supports these Go types in method signatures:

Type JSON Representation
string, int, bool, etc. Native JSON types
[]T (slices) JSON arrays
map[K]V (maps) JSON objects
*T (pointers) Nullable fields
interface{} / any Converts to any
Custom structs JSON objects (must be JSON-serializable)
io.ReadCloser, io.WriteCloser Stream handle (JSON number, 0 = none)

Parameter and result names used by the generated code (ctx, p, stack, service, req, reqBytes, reqMem, resp, response, responsePtr, responseMem, responseBytes, err, svcErr, conv, stream) are rejected (other than the context.Context parameter and the error result).

Streams

io.ReadCloser and io.WriteCloser are supported in host services only. They can be:

  • parameters or results of a host function
  • fields of a struct that is itself a parameter or result (value or pointer)

They travel as handles. A stream passed to a host function is consumed: the plugin can no longer use it. The service gets a wrapper, not the original value (type assertions such as *os.File or io.Seeker fail), and must finish with it before returning: it is closed when the plugin call ends. So a service must not return a stream it was given, or anything built on one. A returned stream belongs to the plugin's current call.

A host function that takes or returns a stream must return an error.

Not supported: streams in capabilities, in shared types, in slices or maps, or in structs nested inside other structs. Any other io type (such as io.Reader or io.ReadWriteCloser), a local type defined from one, and an embedded io field are rejected. A struct with stream fields cannot have embedded fields.

In the Go client a stream stays io.ReadCloser/io.WriteCloser. In the Rust client it is Option<nd_pdk_types::stream::Stream>.

Multiple Return Values

Methods can return multiple values (plus error):

//nd:hostfunc
Search(ctx context.Context, query string) (results []string, total int, hasMore bool, err error)

Generates:

type ServiceSearchResponse struct {
    Results []string `json:"results,omitempty"`
    Total   int      `json:"total,omitempty"`
    HasMore bool     `json:"hasMore,omitempty"`
    Error   string   `json:"error,omitempty"`
}

Running Tests

go test ./plugins/cmd/ndpgen/...