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 ¶
- Variables
- type FromBytes
- type FromDisk
- type FromHTTP
- type FromIoReader
- type FromString
- type HTTPOptions
- func (o *HTTPOptions) WithBasicAuth(username, password string) *HTTPOptions
- func (o *HTTPOptions) WithBearerAuth(token string) *HTTPOptions
- func (o *HTTPOptions) WithHeaderAuth(headers map[string]string) *HTTPOptions
- func (o *HTTPOptions) WithNoAuth() *HTTPOptions
- func (o *HTTPOptions) WithTimeout(timeout time.Duration) *HTTPOptions
- type Loader
- type MockLoader
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
GetSourceURL returns the source URL of the script.
type FromDisk ¶
type FromDisk struct {
// contains filtered or unexported fields
}
func NewFromDisk ¶
func (*FromDisk) GetSourceURL ¶
GetSourceURL returns the source URL of the script.
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 ¶
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 ¶
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 ¶
GetSourceURL returns the source URL. This method is part of the Loader interface and identifies the source location.
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
InferLoader determines the appropriate loader for the given input.
For string inputs, it applies the following checks in order:
- URI scheme detection (http/https -> HTTP, file -> Disk)
- File path format detection (absolute paths, known extensions)
- Base64 validation (attempts decode)
- 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
NewFromStringBase64 attempts to base64 decode the input string. If decoding fails, it falls back to using the original string directly.
type MockLoader ¶
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