js

package
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 9, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package js provides JavaScript-based data transformation using goja runtime.

Index

Constants

View Source
const JSTimeout = 5 * time.Second

JSTimeout is the maximum execution time for JavaScript transforms.

Variables

View Source
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

func NewHeaders(h http.Header, readonly bool) *Headers

NewHeaders creates a new Headers object from http.Header.

func NewReadonlyHeaders

func NewReadonlyHeaders(h http.Header) *Headers

NewReadonlyHeaders creates a read-only Headers object.

func NewWritableHeaders

func NewWritableHeaders(h http.Header) *Headers

NewWritableHeaders creates a writable Headers object.

func (*Headers) Append

func (h *Headers) Append(name, value string)

Append adds a value to a header, allowing multiple values. Only works on writable Headers.

func (*Headers) Delete

func (h *Headers) Delete(name string)

Delete removes a header. Only works on writable Headers.

func (*Headers) Entries

func (h *Headers) Entries() [][2]string

Entries returns all header entries as [name, value] pairs.

func (*Headers) ForEach

func (h *Headers) ForEach(vm *goja.Runtime, callback goja.Callable) error

ForEach calls a function for each header entry.

func (*Headers) Get

func (h *Headers) Get(name string) any

Get returns the value for a header name, or null if not found. Case-insensitive lookup.

func (*Headers) Has

func (h *Headers) Has(name string) bool

Has returns true if the header exists. Case-insensitive lookup.

func (*Headers) Keys

func (h *Headers) Keys() []string

Keys returns all header names.

func (*Headers) Set

func (h *Headers) Set(name, value string)

Set sets a header value, replacing any existing value. Only works on writable Headers.

func (*Headers) ToHTTPHeader

func (h *Headers) ToHTTPHeader() http.Header

ToHTTPHeader returns the underlying http.Header.

func (*Headers) ToJSObject

func (h *Headers) ToJSObject(vm *goja.Runtime) *goja.Object

ToJSObject creates a goja object with Headers methods.

func (*Headers) Values

func (h *Headers) Values() []string

Values returns all header values.

type MockResult

type MockResult struct {
	Output any            // Mock response data
	Ctx    map[string]any // Modified context
}

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

func NewRequest(r *http.Request) *Request

NewRequest creates a new Request object from an HTTP request.

func (*Request) Headers

func (r *Request) Headers() *Headers

Headers returns the request headers.

func (*Request) Method

func (r *Request) Method() string

Method returns the HTTP method.

func (*Request) ToJSObject

func (r *Request) ToJSObject(vm *goja.Runtime) *goja.Object

ToJSObject creates a goja object with Request properties and methods. The object is sealed to prevent modification of its structure.

func (*Request) URL

func (r *Request) URL() string

URL returns the request URL as a string.

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) Headers

func (r *Response) Headers() *Headers

Headers returns the response headers.

func (*Response) Ok

func (r *Response) Ok() bool

Ok returns true if status is in the 200-299 range.

func (*Response) SetStatus

func (r *Response) SetStatus(status int)

SetStatus sets the HTTP status code. Status must be in the valid HTTP range (100-599). Invalid values are ignored.

func (*Response) SetStatusText

func (r *Response) SetStatusText(text string)

SetStatusText sets the HTTP status text.

func (*Response) Status

func (r *Response) Status() int

Status returns the HTTP status code.

func (*Response) StatusText

func (r *Response) StatusText() string

StatusText returns the HTTP status text.

func (*Response) ToHTTPHeader

func (r *Response) ToHTTPHeader() http.Header

ToHTTPHeader returns the underlying http.Header for writing to response.

func (*Response) ToJSObject

func (r *Response) ToJSObject(vm *goja.Runtime) *goja.Object

ToJSObject creates a goja object with Response properties and methods. The object is sealed to prevent modification of its structure.

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

type TransformError struct {
	Status int
	Body   any // string or object (will be JSON serialized)
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL