bldr/plugin/host.go
Rama master · réplica de lectura del repositorio original.
package bldr_plugin
import (
"context"
"strings"
)
// HostServiceIDPrefix is the prefix used for the plugin-host RPC services. This
// ID can be prepended to RPC service IDs to indicate the service is located on
// the plugin host (while running as a plugin).
const HostServiceIDPrefix = "plugin-host/"
// HostServerIDPrefix is the server ID prefix used for plugin-host originating RPC calls.
const HostServerIDPrefix = "plugin-host/"
// HostServerID constructs a server id for a component on the plugin host.
// remoteServerID can be empty
func HostServerID(remoteServerID string) string {
var id string
if remoteServerID != "" {
id = HostServerIDPrefix + remoteServerID
} else {
id = HostServerIDPrefix[:len(HostServerIDPrefix)-1]
}
return id
}
// PluginServerIDPrefix is the server id prefix for plugins.
// Incoming RPC calls from other plugins will have ServerID=PluginServerIDPrefix+RemotePluginID
const PluginServerIDPrefix = "plugin/"
// PluginServerID constructs a server id for a component on a plugin.
// remoteServerID can be empty
func PluginServerID(pluginID, remoteServerID string) string {
id := PluginServerIDPrefix + pluginID
if remoteServerID != "" {
id += "/" + remoteServerID
}
return id
}
// PluginServiceIDPrefix is the prefix used for calling services on other plugins.
//
// ID can be prepended to RPC service IDs to indicate the service is located on
// another plugin.
//
// For example: LookupRpcService<plugin/foo/my.Service> will access the
// my.Service service on the plugin with ID "foo".
const PluginServiceIDPrefix = "plugin/"
// PluginServiceID constructs a service id for routing to a service on a plugin.
func PluginServiceID(pluginID, serviceID string) string {
return PluginServiceIDPrefix + pluginID + "/" + serviceID
}
// HostVolumeServiceIDPrefix is the service ID prefix for the host ProxyVolume.
const HostVolumeServiceIDPrefix = "host-volume/"
// HostStorageID is the storage ID to use for the plugin host within a plugin.
const HostStorageID = "default"
// PluginAssetsFsIdPrefix is the prefix to use for the plugin assets unixfs id on the plugin bus.
const PluginAssetsFsIdPrefix = "plugin-assets/"
// PluginAssetsFsId returns the unixfs id for the assets filesystem for the given plugin id.
//
// Calling with an empty pluginID uses the PluginAssetsFsIdPrefix, which is an
// alias to the current plugin's assets while running in a plugin.
func PluginAssetsFsId(pluginID string) string {
if pluginID == "" {
return PluginAssetsFsIdPrefix[:len(PluginAssetsFsIdPrefix)-1]
}
return PluginAssetsFsIdPrefix + pluginID
}
// PluginDistFsIdPrefix is the prefix to use for the plugin dist unixfs id on the plugin bus.
const PluginDistFsIdPrefix = "plugin-dist/"
// PluginDistFsId returns the unixfs id for the dist filesystem for the given plugin id.
//
// Calling with an empty pluginID uses the PluginDistFsIdPrefix, which is an
// alias to the current plugin's assets while running in a plugin.
func PluginDistFsId(pluginID string) string {
if pluginID == "" {
return PluginDistFsIdPrefix[:len(PluginDistFsIdPrefix)-1]
}
return PluginDistFsIdPrefix + pluginID
}
// BldrHttpPrefix is the route prefix for bldr-controlled URL space.
// /b/
const BldrHttpPrefix = "/b/"
// PluginDistHttpPrefix is the route prefix to use for plugin dist.
// /b/pd/{plugin-id}
const PluginDistHttpPrefix = BldrHttpPrefix + "pd/"
// PluginAssetsHttpPrefix is the route prefix to use for plugin assets.
// /b/pa/{plugin-id}
const PluginAssetsHttpPrefix = BldrHttpPrefix + "pa/"
// PluginWebPkgHttpPrefix is the public URL path prefix for web packages.
// /b/pkg/
const PluginWebPkgHttpPrefix = BldrHttpPrefix + "pkg/"
// PluginHttpPrefix is the route prefix for plugin-controlled URL space.
// /p/{pluginId}/
const PluginHttpPrefix = "/p/"
// PluginAssetsWebPkgsDir is the directory within assets fs for web pkgs.
const PluginAssetsWebPkgsDir = "bldr-web-pkgs"
// PluginVolumeID is an alias to the host volume (while running as a plugin).
const PluginVolumeID = "plugin-host"
// PluginAssetsServiceID is the service id for the plugin assets fs rpc service.
//
// This service is exposed by the plugin host to the plugin.
const PluginAssetsServiceID = "plugin-assets/unixfs.rpc.FSCursorService"
// PluginDistServiceID is the service id for the plugin dist fs rpc service.
//
// This service is exposed by the plugin host to the plugin.
const PluginDistServiceID = "plugin-dist/unixfs.rpc.FSCursorService"
// PluginHTTPPath adds the plugin http prefix to the given path.
func PluginHTTPPath(pluginID string, httpPaths ...string) string {
// Build the plugin route prefix with a separator for the first path.
var sb strings.Builder
_, _ = sb.WriteString(PluginHttpPrefix)
_, _ = sb.WriteString(pluginID)
if len(httpPaths) == 0 || !strings.HasPrefix(httpPaths[0], "/") {
_, _ = sb.WriteString("/")
}
// Append the requested HTTP paths beneath the plugin route.
for _, httpPath := range httpPaths {
_, _ = sb.WriteString(httpPath)
}
return sb.String()
}
// PluginHTTPPathFromContext detects the current plugin from the context and
// conditionally adds the plugin http path prefix to the given path.
func PluginHTTPPathFromContext(ctx context.Context, httpPath string) string {
info := GetPluginContextInfo(ctx)
pluginID := info.GetPluginMeta().GetPluginId()
if pluginID != "" {
return PluginHTTPPath(pluginID, httpPath)
}
return httpPath
}
// ParseHTTPPathPluginID parses and validates a {plugin-id}/ prefix from a HTTP path.
func ParseHTTPPathPluginID(httpPath string) (pluginID string, suffix string, err error) {
// Locate the plugin ID boundary after an optional leading slash.
httpPath = strings.TrimPrefix(httpPath, "/")
slashIdx := strings.IndexRune(httpPath, '/')
// Separate the plugin ID from the remaining HTTP path.
pluginID = httpPath
if slashIdx != -1 {
pluginID = httpPath[:slashIdx]
suffix = httpPath[slashIdx:]
}
return pluginID, suffix, ValidatePluginID(pluginID, false)
}
// PluginDistHTTPPath adds the plugin distribution file prefix to the given path.
func PluginDistHTTPPath(pluginID, httpPath string) string {
// Build the distribution route prefix for the requested plugin.
var sb strings.Builder
_, _ = sb.WriteString(PluginDistHttpPrefix)
_, _ = sb.WriteString(pluginID)
if !strings.HasPrefix(httpPath, "/") {
_, _ = sb.WriteString("/")
}
// Append the distribution file path beneath the plugin route.
_, _ = sb.WriteString(httpPath)
return sb.String()
}
// PluginAssetHTTPPath adds the plugin asset file prefix to the given path.
func PluginAssetHTTPPath(pluginID string, httpPath string) string {
// Build the asset route prefix for the requested plugin.
var sb strings.Builder
_, _ = sb.WriteString(PluginAssetsHttpPrefix)
_, _ = sb.WriteString(pluginID)
if !strings.HasPrefix(httpPath, "/") {
_, _ = sb.WriteString("/")
}
// Append the asset file path beneath the plugin route.
_, _ = sb.WriteString(httpPath)
return sb.String()
}