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

fetch

package
v0.33.2 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package fetch provides a Web Fetch API compliant implementation for use with Goja JavaScript runtime and the vacuum event loop.

Index

Constants

View Source
const (
	// DefaultTimeout is the default timeout for fetch requests
	DefaultTimeout = 30 * time.Second

	// DefaultMaxResponseSize is the default maximum response body size (10MB)
	DefaultMaxResponseSize = 10 * 1024 * 1024

	// DefaultUserAgent is the default User-Agent header
	DefaultUserAgent = "vacuum/1.0"
)

Variables

View Source
var (
	// ErrFetchTimeout is returned when a fetch request exceeds the configured timeout
	ErrFetchTimeout = errors.New("fetch request timed out")

	// ErrNetworkFailure is returned for network-level errors (DNS, connection refused, etc.)
	ErrNetworkFailure = errors.New("network error")

	// ErrBodyAlreadyUsed is returned when attempting to read a Response body that has already been consumed
	ErrBodyAlreadyUsed = errors.New("body has already been consumed")

	// ErrInvalidURL is returned when the provided URL cannot be parsed
	ErrInvalidURL = errors.New("invalid URL")

	// ErrInsecureNotAllowed is returned when attempting HTTP (non-HTTPS) and AllowInsecure is false
	ErrInsecureNotAllowed = errors.New("insecure HTTP not allowed")

	// ErrPrivateNetworkNotAllowed is returned when attempting to access private/local networks
	// and AllowPrivateNetworks is false
	ErrPrivateNetworkNotAllowed = errors.New("private network access not allowed")

	// ErrHostBlocked is returned when the target host is in the BlockedHosts list
	ErrHostBlocked = errors.New("host is blocked")

	// ErrHostNotAllowed is returned when AllowedHosts is set and the target host is not in the list
	ErrHostNotAllowed = errors.New("host not in allowed list")

	// ErrResponseTooLarge is returned when the response body exceeds MaxResponseSize
	ErrResponseTooLarge = errors.New("response body exceeds maximum allowed size")

	// ErrRedirectNotAllowed is returned when redirect mode is "error" and a redirect is encountered
	ErrRedirectNotAllowed = errors.New("redirects not allowed")
)

Functions

func ParseStatusTextFromHeader

func ParseStatusTextFromHeader(status string) string

ParseStatusTextFromHeader extracts just the status text from Go's resp.Status which is formatted as "200 OK"

Types

type EventLoop

type EventLoop interface {
	RegisterCallback() func(func())
	Runtime() *goja.Runtime
}

EventLoop defines the minimal interface needed from the event loop

type Fetch

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

Fetch provides the fetch() function for JavaScript

func NewFetchModule

func NewFetchModule(loop EventLoop, config *FetchConfig) *Fetch

NewFetchModule creates a new Fetch pointer with the given configuration

func NewFetchModuleFromConfig

func NewFetchModuleFromConfig(loop EventLoop, cfg *config.FetchConfig) (*Fetch, error)

NewFetchModuleFromConfig creates a Fetch from config.FetchConfig. Converts CLI/config system configuration to the internal FetchConfig. Returns an error if TLS certificate configuration is invalid.

func (*Fetch) Register

func (m *Fetch) Register()

Register registers the fetch function in the JavaScript runtime

type FetchConfig

type FetchConfig struct {
	// HTTPClient is the underlying HTTP client to use. If nil, a default client is created.
	HTTPClient *http.Client

	// DefaultTimeout is the timeout for fetch requests. Defaults to 30 seconds.
	DefaultTimeout time.Duration

	// MaxResponseSize is the maximum allowed response body size in bytes.
	// Defaults to 10MB. Set to 0 for unlimited (not recommended).
	MaxResponseSize int64

	// AllowedHosts is a list of hosts that are allowed to be fetched.
	// If nil or empty, all hosts are allowed (subject to BlockedHosts).
	// If set, only these hosts can be fetched.
	AllowedHosts []string

	// BlockedHosts is a list of hosts that are blocked from being fetched.
	// This is checked after AllowedHosts.
	BlockedHosts []string

	// AllowInsecure allows HTTP (non-HTTPS) requests.
	// Default is false (HTTPS only).
	AllowInsecure bool

	// AllowPrivateNetworks allows requests to private/local network addresses
	// (localhost, 127.0.0.1, 10.x.x.x, 192.168.x.x, 172.16-31.x.x, etc.)
	// Default is false.
	AllowPrivateNetworks bool

	// UserAgent is the User-Agent header to send with requests.
	// Defaults to "vacuum-fetch/1.0".
	UserAgent string
}

FetchConfig contains configuration options for the fetch module. By default, fetch is restrictive: HTTPS-only and no private networks.

func DefaultFetchConfig

func DefaultFetchConfig() *FetchConfig

DefaultFetchConfig returns a FetchConfig with secure defaults. - HTTPS only (AllowInsecure = false) - No private networks (AllowPrivateNetworks = false) - 30 second timeout - 10MB max response size

func (*FetchConfig) ValidateURL

func (c *FetchConfig) ValidateURL(rawURL string) error

ValidateURL checks if a URL is allowed based on the configuration. Returns nil if allowed, or an appropriate error if not.

type Headers

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

Headers implement the Web Fetch API Headers interface. https://developer.mozilla.org/en-US/docs/Web/API/Headers

func NewHeaders

func NewHeaders(vm *goja.Runtime) *Headers

NewHeaders creates a new Headers object

func NewHeadersFromHTTP

func NewHeadersFromHTTP(vm *goja.Runtime, h http.Header) *Headers

NewHeadersFromHTTP creates a Headers object from an http.Header

func NewHeadersFromObject

func NewHeadersFromObject(vm *goja.Runtime, obj *goja.Object) *Headers

NewHeadersFromObject creates a Headers object from a JavaScript object

func (*Headers) ToGojaObject

func (h *Headers) ToGojaObject() *goja.Object

ToGojaObject creates the JavaScript Headers object

func (*Headers) ToHTTPHeader

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

ToHTTPHeader converts to http.Header for use with http.Request

type Response

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

Response implements the Web Fetch API Response interface. https://developer.mozilla.org/en-US/docs/Web/API/Response

func NewResponse

func NewResponse(vm *goja.Runtime, init ResponseInit) *Response

NewResponse creates a new Response object

func (*Response) SetPromiseConstructor

func (r *Response) SetPromiseConstructor(ctor goja.Value)

SetPromiseConstructor sets the Promise constructor for creating Promises

func (*Response) ToGojaObject

func (r *Response) ToGojaObject() *goja.Object

ToGojaObject creates the JavaScript Response object

type ResponseInit

type ResponseInit struct {
	Status     int
	StatusText string
	Headers    *Headers
	URL        string
	Body       []byte
	Redirected bool
}

ResponseInit contains initialization options for creating a Response

Jump to

Keyboard shortcuts

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