Help us shape the future of Go! Take the 2026 Go Developer Survey

javascript

package
v0.45.3 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Overview

Package javascript evaluates Spectre payload normaliser scripts.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ModuleDeclaration added in v0.19.0

func ModuleDeclaration() string

ModuleDeclaration returns the TypeScript declaration of the spectre module.

func WriteModuleDeclaration added in v0.19.0

func WriteModuleDeclaration(output string) error

WriteModuleDeclaration writes the module declaration to output, creating its parent directory.

Types

type Action added in v0.43.0

type Action interface {
	// contains filtered or unexported methods
}

Action is what happens to the responses a rule selects.

type ActionIgnore added in v0.43.0

type ActionIgnore struct{}

ActionIgnore skips comparison, so the responses never quarantine.

type ActionMatch added in v0.43.0

type ActionMatch struct {
	Type       Option[schema.TypeRef]
	Normalises bool
}

ActionMatch decodes bodies as Type, or as text when Type is None, then compares them after the rule's normaliser, if it has one.

type Endpoint added in v0.12.0

type Endpoint struct {
	// contains filtered or unexported fields
}

Endpoint maps a protocol-specific request pattern to a payload type.

func (Endpoint) Direction added in v0.13.0

func (e Endpoint) Direction() model.Direction

Direction returns the proxy that uses the endpoint.

func (Endpoint) Pattern added in v0.12.0

func (e Endpoint) Pattern() string

Pattern returns the protocol-specific pattern the endpoint matches.

func (Endpoint) Protocol added in v0.19.0

func (e Endpoint) Protocol() model.Protocol

Protocol returns the protocol key that interprets the endpoint.

func (Endpoint) Type added in v0.19.0

func (e Endpoint) Type() schema.TypeRef

Type returns the fully qualified name of the endpoint's payload type.

type Evaluator

type Evaluator struct {
	// contains filtered or unexported fields
}

Evaluator owns the JavaScript runtime for one payload normalisation. It is confined to its caller because Sobek runtime access is not synchronized.

func (*Evaluator) Close

func (e *Evaluator) Close()

Close releases the evaluator's context watcher.

func (*Evaluator) NormaliseField added in v0.11.0

func (e *Evaluator) NormaliseField(target FieldTarget, value Option[any]) (Option[any], error)

NormaliseField invokes the normaliser registered for a JSON field path. None stands for an absent field in both the argument and the result.

func (*Evaluator) NormaliseMessage added in v0.11.0

func (e *Evaluator) NormaliseMessage(target schema.TypeRef, value Option[any]) (Option[any], error)

NormaliseMessage invokes the normaliser registered for an object type. None stands for an absent message in both the argument and the result.

func (*Evaluator) NormaliseResponse added in v0.43.0

func (e *Evaluator) NormaliseResponse(rule Rule, response any) (Option[any], error)

NormaliseResponse invokes a rule's response normaliser on a JSON response object. None stands for an undefined result.

func (*Evaluator) SelectsRequest added in v0.43.0

func (e *Evaluator) SelectsRequest(rule Rule, request Request) (bool, error)

SelectsRequest reports whether a rule's request function accepts a request.

func (*Evaluator) SelectsRequestHeader added in v0.44.0

func (e *Evaluator) SelectsRequestHeader(rule Rule, name string, value Option[string]) (bool, error)

SelectsRequestHeader reports whether a rule's function for a request header accepts its value, which is undefined when the request lacks the header.

func (*Evaluator) SelectsResponseHeader added in v0.44.0

func (e *Evaluator) SelectsResponseHeader(rule Rule, name string, value Option[string]) (bool, error)

SelectsResponseHeader reports whether a rule's function for a response header accepts its value, which is undefined when the response lacks the header.

func (*Evaluator) SelectsStatus added in v0.43.0

func (e *Evaluator) SelectsStatus(rule Rule, status int) (bool, error)

SelectsStatus reports whether a rule's status function accepts a status.

type FieldTarget added in v0.19.0

type FieldTarget struct {
	// contains filtered or unexported fields
}

FieldTarget identifies a JSON field path relative to an explicitly named type. The two components remain separate because namespace names can overlap field paths.

func NewFieldTarget added in v0.19.0

func NewFieldTarget(typeName schema.TypeRef, path string) FieldTarget

NewFieldTarget creates a target without resolving it against a schema.

func (FieldTarget) Path added in v0.19.0

func (t FieldTarget) Path() string

Path returns the JSON field path relative to the root type.

func (FieldTarget) String added in v0.19.0

func (t FieldTarget) String() string

String returns a human-readable target name, not a unique identity.

func (FieldTarget) Type added in v0.19.0

func (t FieldTarget) Type() schema.TypeRef

Type returns the fully qualified name of the target's root object type.

type HeaderScript added in v0.44.0

type HeaderScript struct{ Name string }

HeaderScript selects the messages whose header a script function accepts.

type HeaderSelector added in v0.44.0

type HeaderSelector interface {
	// contains filtered or unexported methods
}

HeaderSelector selects the requests or responses a rule applies to by one header.

type HeaderValue added in v0.44.0

type HeaderValue struct{ Name, Value string }

HeaderValue selects messages whose header, with its values joined by ", ", equals Value. It never selects a message without the header.

func (HeaderValue) Selects added in v0.44.0

func (h HeaderValue) Selects(value Option[string]) bool

Selects reports whether a header's joined value equals Value.

type Program

type Program struct {
	// contains filtered or unexported fields
}

Program is one immutable set of scripts, shared by all payload normalisations. It retains no runtime-local callback state.

func NewProgram

func NewProgram(ctx context.Context, evaluationTimeout time.Duration, declarations fs.FS, scripts fs.FS) (*Program, error)

NewProgram type-checks every TypeScript script against the schema declarations, links them, then evaluates them once within evaluationTimeout to collect their registrations.

func (*Program) Endpoints added in v0.12.0

func (p *Program) Endpoints(direction model.Direction) []Endpoint

Endpoints returns the endpoints declared for a direction, in declaration order.

func (*Program) Fields

func (p *Program) Fields() []FieldTarget

Fields returns the declared type names and JSON field paths, sorted by type then path.

func (*Program) Messages

func (p *Program) Messages() []schema.TypeRef

Messages returns the object type names declared by the scripts.

func (*Program) NewEvaluator

func (p *Program) NewEvaluator(ctx context.Context) (*Evaluator, error)

NewEvaluator creates an isolated evaluator for one payload normalisation. Re-evaluation must reproduce the registrations used to build the schema plan.

func (*Program) Rules added in v0.43.0

func (p *Program) Rules() []Rule

Rules returns the ingress rules in declaration order.

func (*Program) Schema added in v0.19.0

func (p *Program) Schema() *schema.Schema

Schema returns the payload types the scripts were checked against.

type Request added in v0.43.0

type Request struct {
	Method string
	// Path is escaped and excludes the query.
	Path   string
	Header http.Header
}

Request is an inbound HTTP request as a rule's selectors see it.

func (Request) HeaderValue added in v0.44.0

func (r Request) HeaderValue(name string) Option[string]

HeaderValue returns a header's values joined by ", ", or None without any.

type RequestPattern added in v0.43.0

type RequestPattern struct{ Pattern string }

RequestPattern selects requests matching a ServeMux pattern.

type RequestScript added in v0.43.0

type RequestScript struct{}

RequestScript selects the requests a script function accepts.

type RequestSelector added in v0.43.0

type RequestSelector interface {
	// contains filtered or unexported methods
}

RequestSelector selects the requests a rule applies to.

type Response added in v0.44.0

type Response struct {
	Status int
	Header http.Header
}

Response is an HTTP response as a rule's selectors see it.

func (Response) HeaderValue added in v0.44.0

func (r Response) HeaderValue(name string) Option[string]

HeaderValue returns a header's values joined by ", ", or None without any.

type Rule added in v0.43.0

type Rule struct {
	// contains filtered or unexported fields
}

Rule decides how ingress compares the responses it selects. Rules apply in declaration order, and the first that selects a response decides.

func (Rule) Action added in v0.43.0

func (r Rule) Action() Action

Action returns what happens to the responses the rule selects.

func (Rule) Index added in v0.43.0

func (r Rule) Index() int

Index returns the rule's position among the scripts' rules.

func (Rule) Protocol added in v0.43.0

func (r Rule) Protocol() model.Protocol

Protocol returns the protocol key that interprets the rule.

func (Rule) Request added in v0.43.0

func (r Rule) Request() RequestSelector

Request returns how the rule selects requests.

func (Rule) RequestHeaders added in v0.44.0

func (r Rule) RequestHeaders() []HeaderSelector

RequestHeaders returns how the rule selects requests by their headers, sorted by name.

func (Rule) ResponseHeaders added in v0.44.0

func (r Rule) ResponseHeaders() []HeaderSelector

ResponseHeaders returns how the rule selects responses by their headers, sorted by name.

func (Rule) SelectsEveryResponse added in v0.44.0

func (r Rule) SelectsEveryResponse() bool

SelectsEveryResponse reports whether the rule selects every response to a request its pattern and request header values select.

func (Rule) SelectsRequestHeaders added in v0.44.0

func (r Rule) SelectsRequestHeaders(header http.Header) bool

SelectsRequestHeaders reports whether a request has the header values the rule requires. Header functions are left to the caller.

func (Rule) Status added in v0.43.0

func (r Rule) Status() StatusSelector

Status returns how the rule selects responses by status.

func (Rule) String added in v0.43.0

func (r Rule) String() string

String names the rule the way a script declares it.

type StatusAny added in v0.43.0

type StatusAny struct{}

StatusAny selects every response.

type StatusCodes added in v0.43.0

type StatusCodes struct{ Codes []int }

StatusCodes selects responses with one of the codes.

type StatusScript added in v0.43.0

type StatusScript struct{}

StatusScript selects the responses whose status a script function accepts.

type StatusSelector added in v0.43.0

type StatusSelector interface {
	// contains filtered or unexported methods
}

StatusSelector selects the responses a rule applies to by their status.

Jump to

Keyboard shortcuts

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