navidrome/plugins/host/websocket_gen.go
2026-01-12 08:16:04 -05:00

193 lines
5.4 KiB
Go

// Code generated by hostgen. DO NOT EDIT.
package host
import (
"context"
"encoding/json"
extism "github.com/extism/go-sdk"
)
// WebSocketConnectRequest is the request type for WebSocket.Connect.
type WebSocketConnectRequest struct {
Url string `json:"url"`
Headers map[string]string `json:"headers"`
ConnectionID string `json:"connectionID"`
}
// WebSocketConnectResponse is the response type for WebSocket.Connect.
type WebSocketConnectResponse struct {
NewConnectionID string `json:"newConnectionID,omitempty"`
Error string `json:"error,omitempty"`
}
// RegisterWebSocketHostFunctions registers WebSocket service host functions.
// The returned host functions should be added to the plugin's configuration.
func RegisterWebSocketHostFunctions(service WebSocketService) []extism.HostFunction {
return []extism.HostFunction{
newWebSocketConnectHostFunction(service),
newWebSocketSendTextHostFunction(service),
newWebSocketSendBinaryHostFunction(service),
newWebSocketCloseConnectionHostFunction(service),
}
}
func newWebSocketConnectHostFunction(service WebSocketService) extism.HostFunction {
return extism.NewHostFunctionWithStack(
"websocket_connect",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
// Read JSON request from plugin memory
reqBytes, err := p.ReadBytes(stack[0])
if err != nil {
websocketWriteError(p, stack, err)
return
}
var req WebSocketConnectRequest
if err := json.Unmarshal(reqBytes, &req); err != nil {
websocketWriteError(p, stack, err)
return
}
// Call the service method
newconnectionid, err := service.Connect(ctx, req.Url, req.Headers, req.ConnectionID)
if err != nil {
websocketWriteError(p, stack, err)
return
}
// Write JSON response to plugin memory
resp := WebSocketConnectResponse{
NewConnectionID: newconnectionid,
}
websocketWriteResponse(p, stack, resp)
},
[]extism.ValueType{extism.ValueTypePTR},
[]extism.ValueType{extism.ValueTypePTR},
)
}
func newWebSocketSendTextHostFunction(service WebSocketService) extism.HostFunction {
return extism.NewHostFunctionWithStack(
"websocket_sendtext",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
// Read parameters from stack
connectionID, err := p.ReadString(stack[0])
if err != nil {
return
}
message, err := p.ReadString(stack[1])
if err != nil {
return
}
// Call the service method
err = service.SendText(ctx, connectionID, message)
if err != nil {
// Write error string to plugin memory
if ptr, err := p.WriteString(err.Error()); err == nil {
stack[0] = ptr
}
return
}
// Write empty string to indicate success
if ptr, err := p.WriteString(""); err == nil {
stack[0] = ptr
}
},
[]extism.ValueType{extism.ValueTypePTR, extism.ValueTypePTR},
[]extism.ValueType{extism.ValueTypePTR},
)
}
func newWebSocketSendBinaryHostFunction(service WebSocketService) extism.HostFunction {
return extism.NewHostFunctionWithStack(
"websocket_sendbinary",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
// Read parameters from stack
connectionID, err := p.ReadString(stack[0])
if err != nil {
return
}
data, err := p.ReadBytes(stack[1])
if err != nil {
return
}
// Call the service method
err = service.SendBinary(ctx, connectionID, data)
if err != nil {
// Write error string to plugin memory
if ptr, err := p.WriteString(err.Error()); err == nil {
stack[0] = ptr
}
return
}
// Write empty string to indicate success
if ptr, err := p.WriteString(""); err == nil {
stack[0] = ptr
}
},
[]extism.ValueType{extism.ValueTypePTR, extism.ValueTypePTR},
[]extism.ValueType{extism.ValueTypePTR},
)
}
func newWebSocketCloseConnectionHostFunction(service WebSocketService) extism.HostFunction {
return extism.NewHostFunctionWithStack(
"websocket_closeconnection",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
// Read parameters from stack
connectionID, err := p.ReadString(stack[0])
if err != nil {
return
}
code := extism.DecodeI32(stack[1])
reason, err := p.ReadString(stack[2])
if err != nil {
return
}
// Call the service method
err = service.CloseConnection(ctx, connectionID, code, reason)
if err != nil {
// Write error string to plugin memory
if ptr, err := p.WriteString(err.Error()); err == nil {
stack[0] = ptr
}
return
}
// Write empty string to indicate success
if ptr, err := p.WriteString(""); err == nil {
stack[0] = ptr
}
},
[]extism.ValueType{extism.ValueTypePTR, extism.ValueTypeI32, extism.ValueTypePTR},
[]extism.ValueType{extism.ValueTypePTR},
)
}
// websocketWriteResponse writes a JSON response to plugin memory.
func websocketWriteResponse(p *extism.CurrentPlugin, stack []uint64, resp any) {
respBytes, err := json.Marshal(resp)
if err != nil {
websocketWriteError(p, stack, err)
return
}
respPtr, err := p.WriteBytes(respBytes)
if err != nil {
stack[0] = 0
return
}
stack[0] = respPtr
}
// websocketWriteError writes an error response to plugin memory.
func websocketWriteError(p *extism.CurrentPlugin, stack []uint64, err error) {
errResp := struct {
Error string `json:"error"`
}{Error: err.Error()}
respBytes, _ := json.Marshal(errResp)
respPtr, _ := p.WriteBytes(respBytes)
stack[0] = respPtr
}