Documentation
¶
Overview ¶
Package js provides JavaScript-based data transformation using goja runtime.
Index ¶
- Constants
- Variables
- type CompiledHelpers
- type Headers
- func (h *Headers) Append(name, value string)
- func (h *Headers) Delete(name string)
- func (h *Headers) Entries() [][2]string
- func (h *Headers) ForEach(vm *goja.Runtime, callback goja.Callable) error
- func (h *Headers) Get(name string) any
- func (h *Headers) Has(name string) bool
- func (h *Headers) Keys() []string
- func (h *Headers) Set(name, value string)
- func (h *Headers) ToHTTPHeader() http.Header
- func (h *Headers) ToJSObject(vm *goja.Runtime) *goja.Object
- func (h *Headers) Values() []string
- type MockResult
- type PostTransformResult
- type PreTransformResult
- type Request
- type Response
- func (r *Response) Headers() *Headers
- func (r *Response) Ok() bool
- func (r *Response) SetStatus(status int)
- func (r *Response) SetStatusText(text string)
- func (r *Response) Status() int
- func (r *Response) StatusText() string
- func (r *Response) ToHTTPHeader() http.Header
- func (r *Response) ToJSObject(vm *goja.Runtime) *goja.Object
- type TransformContext
- type TransformError
- type Transformer
- func (t *Transformer) ApplyMock(ctx map[string]any, sql string, input map[string]any, tc *TransformContext) (*MockResult, error)
- func (t *Transformer) ApplyPost(ctx map[string]any, input map[string]any, output any, tc *TransformContext) (*PostTransformResult, error)
- func (t *Transformer) ApplyPostToAllRows(ctx map[string]any, input map[string]any, rows any, tc *TransformContext) (*PostTransformResult, error)
- func (t *Transformer) ApplyPostToEachRow(ctx map[string]any, input map[string]any, rows []map[string]any, ...) ([]any, map[string]any, error)
- func (t *Transformer) ApplyPre(ctx map[string]any, sql string, input map[string]any, tc *TransformContext) (*PreTransformResult, error)
- func (t *Transformer) SetHelpers(h *CompiledHelpers)
Constants ¶
const JSTimeout = 5 * time.Second
JSTimeout is the maximum execution time for JavaScript transforms.
Variables ¶
var ErrJSTimeout = errors.New("JavaScript execution timeout")
ErrJSTimeout is returned when JS execution exceeds JSTimeout.
Functions ¶
This section is empty.
Types ¶
type CompiledHelpers ¶
type CompiledHelpers struct {
// contains filtered or unexported fields
}
CompiledHelpers holds pre-compiled global helper code.
func CompileHelpers ¶
func CompileHelpers(jsCode string, jsFiles []string, configDir string) (*CompiledHelpers, error)
CompileHelpers compiles global helper code from inline JS and files. Returns nil if no helpers are configured.
func (*CompiledHelpers) InjectInto ¶
func (h *CompiledHelpers) InjectInto(vm *goja.Runtime) error
InjectInto executes helpers in the VM, adding them to globalThis.
type Headers ¶
type Headers struct {
// contains filtered or unexported fields
}
Headers implements the Web API Headers interface for use in JavaScript. It provides case-insensitive header access with get/set/has/delete methods.
func NewHeaders ¶
NewHeaders creates a new Headers object from http.Header.
func NewReadonlyHeaders ¶
NewReadonlyHeaders creates a read-only Headers object.
func NewWritableHeaders ¶
NewWritableHeaders creates a writable Headers object.
func (*Headers) Append ¶
Append adds a value to a header, allowing multiple values. Only works on writable Headers.
func (*Headers) Get ¶
Get returns the value for a header name, or null if not found. Case-insensitive lookup.
func (*Headers) Set ¶
Set sets a header value, replacing any existing value. Only works on writable Headers.
func (*Headers) ToHTTPHeader ¶
ToHTTPHeader returns the underlying http.Header.
func (*Headers) ToJSObject ¶
ToJSObject creates a goja object with Headers methods.
type MockResult ¶
MockResult holds the result of a mock execution.
type PostTransformResult ¶
type PostTransformResult struct {
Output any // Transformed output
Ctx map[string]any // Modified context
}
PostTransformResult holds the result of a post-transform execution.
type PreTransformResult ¶
type PreTransformResult struct {
Output map[string]any // Transformed input parameters
SQL string // Modified SQL (or original if unchanged)
Ctx map[string]any // Modified context
}
PreTransformResult holds the result of a pre-transform execution.
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
Request implements a simplified Web API Request interface for use in JavaScript. It provides read-only access to request properties.
func NewRequest ¶
NewRequest creates a new Request object from an HTTP request.
func (*Request) ToJSObject ¶
ToJSObject creates a goja object with Request properties and methods. The object is sealed to prevent modification of its structure.
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
Response implements a simplified Web API Response interface for use in JavaScript. It provides writable access to response properties for post-transform.
func NewResponse ¶
func NewResponse() *Response
NewResponse creates a new Response object with default values.
func (*Response) SetStatus ¶
SetStatus sets the HTTP status code. Status must be in the valid HTTP range (100-599). Invalid values are ignored.
func (*Response) SetStatusText ¶
SetStatusText sets the HTTP status text.
func (*Response) StatusText ¶
StatusText returns the HTTP status text.
func (*Response) ToHTTPHeader ¶
ToHTTPHeader returns the underlying http.Header for writing to response.
type TransformContext ¶
type TransformContext struct {
Request *Request // HTTP request (read-only in JS)
Response *Response // HTTP response (writable in post-transform)
}
TransformContext holds HTTP context for transforms.
func NewTransformContext ¶
func NewTransformContext(r *http.Request) *TransformContext
NewTransformContext creates a new TransformContext from an HTTP request.
type TransformError ¶
TransformError represents an error thrown from JavaScript transform. Supports Lambda-style format: throw { status: 400, body: "message" } or throw { status: 400, body: { message: "error" } }
func (*TransformError) Error ¶
func (e *TransformError) Error() string
type Transformer ¶
type Transformer struct {
// contains filtered or unexported fields
}
Transformer applies JavaScript transformations.
func CompileFilter ¶
func CompileFilter(code string) (*Transformer, error)
CompileFilter creates a filter function with free variable (ctx) and parameters (row, input). Returns boolean indicating whether the row should be included.
func CompileMockJS ¶
func CompileMockJS(code string) (*Transformer, error)
CompileMockJS creates a mock JS function with free variables (ctx, sql) and parameter (input). Same signature as CompilePre but returns raw error (no wrapping).
func CompilePost ¶
func CompilePost(code string) (*Transformer, error)
CompilePost creates a post-transform with free variable (ctx) and parameters (input, output). Free variable ctx can be read/modified in the function body.
func CompilePre ¶
func CompilePre(code string) (*Transformer, error)
CompilePre creates a pre-transform with free variables (ctx, sql) and parameter (input). Free variables ctx and sql can be read/modified in the function body.
func (*Transformer) ApplyMock ¶
func (t *Transformer) ApplyMock(ctx map[string]any, sql string, input map[string]any, tc *TransformContext) (*MockResult, error)
ApplyMock applies mock transform with free variables ctx and sql. Returns the mock response data and updated context. Mock can return any data type (object for "one", array for "many"). tc is optional and provides HTTP request context.
func (*Transformer) ApplyPost ¶
func (t *Transformer) ApplyPost(ctx map[string]any, input map[string]any, output any, tc *TransformContext) (*PostTransformResult, error)
ApplyPost applies post-transform with free variable ctx. Returns the transformed output and updated context. tc is optional and provides HTTP request/response context.
func (*Transformer) ApplyPostToAllRows ¶
func (t *Transformer) ApplyPostToAllRows(ctx map[string]any, input map[string]any, rows any, tc *TransformContext) (*PostTransformResult, error)
ApplyPostToAllRows applies post-transform to the entire result array. For post-transform with "all" mode: ctx as free variable, input and output (entire array) as parameters. tc is optional and provides HTTP request/response context.
func (*Transformer) ApplyPostToEachRow ¶
func (t *Transformer) ApplyPostToEachRow(ctx map[string]any, input map[string]any, rows []map[string]any, tc *TransformContext) ([]any, map[string]any, error)
ApplyPostToEachRow applies post-transform to each row individually. For post-transform with "each" mode: ctx as free variable, input and output (each row) as parameters. tc is optional and provides HTTP request/response context.
func (*Transformer) ApplyPre ¶
func (t *Transformer) ApplyPre(ctx map[string]any, sql string, input map[string]any, tc *TransformContext) (*PreTransformResult, error)
ApplyPre applies pre-transform with free variables ctx and sql. Returns the transformed input, modified SQL, and updated context. tc is optional and provides HTTP request context.
func (*Transformer) SetHelpers ¶
func (t *Transformer) SetHelpers(h *CompiledHelpers)
SetHelpers sets the compiled helpers for this transformer.