loader

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

README

Script Loaders

This package defines the Loader interface, which provides a common abstraction for accessing script content from various sources.

Overview

The Loader interface standardizes how script content is retrieved and identified within the go-polyscript framework. It serves as a building block in the execution pipeline where script content is loaded, compiled, and evaluated.

Architecture

Loaders fit into the execution flow as follows:

  1. A Loader provides script content to the compiler
  2. The compiler processes this content into executable form
  3. The ExecutableUnit maintains a reference to the original loader
  4. During evaluation, the source information may be used for error reporting

Implementation

When creating a new loader, follow the error patterns defined in errors.go and refer to existing implementations for consistency.

See the source files in this directory for specific loader implementations.

Documentation

Overview

Package loader provides implementations of the Loader interface for various source types.

This file implements the HTTP loader, which allows fetching scripts from HTTP/HTTPS URLs. The loader supports various authentication methods via the httpauth package:

  • No authentication: Use loader.WithNoAuth()
  • Basic authentication: Use loader.WithBasicAuth(username, password)
  • Bearer token: Use loader.WithBearerAuth(token)
  • Custom headers: Use loader.WithHeaderAuth(headers)

The loader also supports context-based operations for timeout and cancellation control.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSchemeUnsupported  = errors.New("unsupported scheme")
	ErrScriptNotAvailable = errors.New("script not available")
	ErrInputEmpty         = errors.New("input is empty")
)

ErrNoScript is returned when there is no script to return.

Functions

This section is empty.

Types

type FromBytes

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

FromBytes implements the Loader interface for content from a byte slice.

func NewFromBytes

func NewFromBytes(content []byte) (*FromBytes, error)

NewFromBytes creates a new Loader from a byte slice.

func (*FromBytes) GetReader

func (l *FromBytes) GetReader() (io.ReadCloser, error)

GetReader returns a new reader for the stored content.

func (*FromBytes) GetSourceURL

func (l *FromBytes) GetSourceURL() *url.URL

GetSourceURL returns the source URL of the script.

func (*FromBytes) String

func (l *FromBytes) String() string

type FromDisk

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

func NewFromDisk

func NewFromDisk(path string) (*FromDisk, error)

func (*FromDisk) GetReader

func (l *FromDisk) GetReader() (io.ReadCloser, error)

func (*FromDisk) GetSourceURL

func (l *FromDisk) GetSourceURL() *url.URL

GetSourceURL returns the source URL of the script.

func (*FromDisk) String

func (l *FromDisk) String() string

type FromHTTP

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

FromHTTP implements a loader for HTTP/HTTPS URLs. It allows loading scripts from remote web servers with various authentication options.

func NewFromHTTP

func NewFromHTTP(rawURL string) (*FromHTTP, error)

NewFromHTTP creates a new HTTP loader with the given URL and default options. This is the simplest way to create an HTTP loader when you don't need custom configuration.

Example:

loader, err := loader.NewFromHTTP("https://localhost:8080/script.js")
if err != nil {
    return err
}

See also NewFromHTTPWithOptions for more customization options.

func NewFromHTTPWithOptions

func NewFromHTTPWithOptions(rawURL string, options *HTTPOptions) (*FromHTTP, error)

NewFromHTTPWithOptions creates a new HTTP loader with the given URL and custom options. Use this when you need authentication, custom timeouts, or other HTTP configuration.

Examples:

// With basic auth
options := loader.DefaultHTTPOptions().WithBasicAuth("user", "pass")
loader, err := loader.NewFromHTTPWithOptions("https://localhost:8080/script.js", options)

// With bearer token
options := loader.DefaultHTTPOptions().WithBearerAuth("token123")
loader, err := loader.NewFromHTTPWithOptions("https://localhost:8080/script.js", options)

// With custom timeout
options := loader.DefaultHTTPOptions().WithTimeout(10 * time.Second)
loader, err := loader.NewFromHTTPWithOptions("https://localhost:8080/script.js", options)

func (*FromHTTP) GetReader

func (l *FromHTTP) GetReader() (io.ReadCloser, error)

GetReader returns a reader for the HTTP content. This method is part of the Loader interface and is used internally by the polyscript system to fetch the script content.

The returned io.ReadCloser must be closed by the caller when done. HTTP errors are handled and converted to appropriate error types.

func (*FromHTTP) GetReaderWithContext

func (l *FromHTTP) GetReaderWithContext(ctx context.Context) (io.ReadCloser, error)

GetReaderWithContext returns a reader for the HTTP content with context support. This allows for request cancellation and timeouts via context.

The returned io.ReadCloser must be closed by the caller when done. HTTP errors are handled and converted to appropriate error types.

func (*FromHTTP) GetSourceURL

func (l *FromHTTP) GetSourceURL() *url.URL

GetSourceURL returns the source URL. This method is part of the Loader interface and identifies the source location.

func (*FromHTTP) String

func (l *FromHTTP) String() string

String returns a string representation of the HTTP loader. This is useful for debugging and logging.

type FromIoReader

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

FromIoReader implements the Loader interface for content from an io.Reader.

func NewFromIoReader

func NewFromIoReader(reader io.Reader, sourceName string) (*FromIoReader, error)

NewFromIoReader creates a new Loader from an io.Reader source. The entire reader content is read and stored to allow multiple GetReader calls.

func (*FromIoReader) GetReader

func (l *FromIoReader) GetReader() (io.ReadCloser, error)

GetReader returns a new reader for the stored content.

func (*FromIoReader) GetSourceURL

func (l *FromIoReader) GetSourceURL() *url.URL

GetSourceURL returns the source URL of the script.

func (*FromIoReader) String

func (l *FromIoReader) String() string

type FromString

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

FromString implements the Loader interface for string content.

func NewFromString

func NewFromString(content string) (*FromString, error)

NewFromString creates a new loader from string content. The content is trimmed of whitespace and must be non-empty.

func (*FromString) GetReader

func (l *FromString) GetReader() (io.ReadCloser, error)

func (*FromString) GetSourceURL

func (l *FromString) GetSourceURL() *url.URL

GetSourceURL returns the source URL of the script.

func (*FromString) String

func (l *FromString) String() string

type HTTPOptions

type HTTPOptions struct {
	// Timeout specifies a time limit for requests made by this Client
	// Default is 30 seconds if using DefaultHTTPOptions()
	Timeout time.Duration

	// TLSConfig specifies the TLS configuration to use
	// This is optional and allows for advanced TLS configuration
	TLSConfig *tls.Config

	// InsecureSkipVerify skips TLS certificate verification when set to true
	// Default is false (certificates are verified) in DefaultHTTPOptions()
	// Warning: Setting to true reduces security and should only be done in test environments
	InsecureSkipVerify bool

	// Authentication to use for HTTP requests
	Authenticator httpauth.Authenticator

	// Headers for additional headers not related to authentication
	Headers map[string]string
}

HTTPOptions contains configuration options for HTTP loader. Use DefaultHTTPOptions() to get sensible defaults, then modify as needed.

Example:

options := loader.DefaultHTTPOptions()
options.Timeout = 10 * time.Second
options = options.WithBasicAuth("user", "pass")

func DefaultHTTPOptions

func DefaultHTTPOptions() *HTTPOptions

DefaultHTTPOptions returns default options for HTTP loader. This provides a sensible starting point that you can then customize.

Default values: - Timeout: 30 seconds - InsecureSkipVerify: false (certificate validation enabled) - Authenticator: NoAuth (no authentication) - Headers: empty map (initialized, ready for values)

func (*HTTPOptions) WithBasicAuth

func (o *HTTPOptions) WithBasicAuth(username, password string) *HTTPOptions

WithBasicAuth returns a copy of options with Basic authentication set.

func (*HTTPOptions) WithBearerAuth

func (o *HTTPOptions) WithBearerAuth(token string) *HTTPOptions

WithBearerAuth returns a copy of options with Bearer token authentication set.

func (*HTTPOptions) WithHeaderAuth

func (o *HTTPOptions) WithHeaderAuth(headers map[string]string) *HTTPOptions

WithHeaderAuth returns a copy of options with custom header authentication set.

func (*HTTPOptions) WithNoAuth

func (o *HTTPOptions) WithNoAuth() *HTTPOptions

WithNoAuth returns a copy of options with no authentication set.

func (*HTTPOptions) WithTimeout

func (o *HTTPOptions) WithTimeout(timeout time.Duration) *HTTPOptions

WithTimeout returns a copy of options with the specified timeout.

type Loader

type Loader interface {
	GetReader() (io.ReadCloser, error)
	GetSourceURL() *url.URL
}

Loader is an interface used by the engines to load scripts or binaries.

func InferLoader added in v0.0.4

func InferLoader(input any) (Loader, error)

InferLoader determines the appropriate loader for the given input.

For string inputs, it applies the following checks in order:

  1. URI scheme detection (http/https -> HTTP, file -> Disk)
  2. File path format detection (absolute paths, known extensions)
  3. Base64 validation (attempts decode)
  4. String fallback

Other input types:

  • []byte: FromBytes loader
  • io.Reader: FromIoReader loader
  • Loader: returned unchanged

Returns an error for unsupported input types or loader creation failures.

func NewFromStringBase64 added in v0.0.4

func NewFromStringBase64(content string) (Loader, error)

NewFromStringBase64 attempts to base64 decode the input string. If decoding fails, it falls back to using the original string directly.

type MockLoader

type MockLoader struct {
	mock.Mock
}

MockLoader implements the loader.Loader interface for testing

func NewMockLoaderWithContent

func NewMockLoaderWithContent(content []byte) *MockLoader

Helper method to easily create a mock with content

func (*MockLoader) Close

func (m *MockLoader) Close() error

func (*MockLoader) GetReader

func (m *MockLoader) GetReader() (io.ReadCloser, error)

func (*MockLoader) GetSourceURL

func (m *MockLoader) GetSourceURL() *url.URL

Directories

Path Synopsis
Package httpauth provides authentication strategies for HTTP requests in go-polyscript.
Package httpauth provides authentication strategies for HTTP requests in go-polyscript.

Jump to

Keyboard shortcuts

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