{"modulePath":"github.com/daveshanley/vacuum","version":"v0.43.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package fetch\n\nPackage fetch provides a Web Fetch API compliant implementation for use with Goja JavaScript runtime and the vacuum event loop.\n\n## Constants\n\n```go\nconst (\n\t// DefaultTimeout is the default timeout for fetch requests\n\tDefaultTimeout = 30 * time.Second\n\n\t// DefaultMaxResponseSize is the default maximum response body size (10MB)\n\tDefaultMaxResponseSize = 10 * 1024 * 1024\n\n\t// DefaultUserAgent is the default User-Agent header\n\tDefaultUserAgent = \"vacuum/1.0\"\n)\n```\n\n## Variables\n\n```go\nvar (\n\t// ErrFetchTimeout is returned when a fetch request exceeds the configured timeout\n\tErrFetchTimeout = errors.New(\"fetch request timed out\")\n\n\t// ErrNetworkFailure is returned for network-level errors (DNS, connection refused, etc.)\n\tErrNetworkFailure = errors.New(\"network error\")\n\n\t// ErrBodyAlreadyUsed is returned when attempting to read a Response body that has already been consumed\n\tErrBodyAlreadyUsed = errors.New(\"body has already been consumed\")\n\n\t// ErrInvalidURL is returned when the provided URL cannot be parsed\n\tErrInvalidURL = errors.New(\"invalid URL\")\n\n\t// ErrInsecureNotAllowed is returned when attempting HTTP (non-HTTPS) and AllowInsecure is false\n\tErrInsecureNotAllowed = errors.New(\"insecure HTTP not allowed\")\n\n\t// ErrPrivateNetworkNotAllowed is returned when attempting to access private/local networks\n\t// and AllowPrivateNetworks is false\n\tErrPrivateNetworkNotAllowed = errors.New(\"private network access not allowed\")\n\n\t// ErrHostBlocked is returned when the target host is in the BlockedHosts list\n\tErrHostBlocked = errors.New(\"host is blocked\")\n\n\t// ErrHostNotAllowed is returned when AllowedHosts is set and the target host is not in the list\n\tErrHostNotAllowed = errors.New(\"host not in allowed list\")\n\n\t// ErrResponseTooLarge is returned when the response body exceeds MaxResponseSize\n\tErrResponseTooLarge = errors.New(\"response body exceeds maximum allowed size\")\n\n\t// ErrRedirectNotAllowed is returned when redirect mode is \"error\" and a redirect is encountered\n\tErrRedirectNotAllowed = errors.New(\"redirects not allowed\")\n)\n```\n\n## Functions\n\n```go\nfunc ParseStatusTextFromHeader(status string) string\n```\nParseStatusTextFromHeader extracts just the status text from Go's resp.Status which is formatted as \"200 OK\"\n\n## Types\n\n```go\ntype EventLoop interface {\n\tRegisterCallback() func(func())\n\tRuntime() *goja.Runtime\n}\n```\nEventLoop defines the minimal interface needed from the event loop\n\n```go\ntype Fetch struct {\n\t// contains filtered or unexported fields\n}\n```\nFetch provides the fetch() function for JavaScript\n\n```go\nfunc NewFetchModule(loop EventLoop, config *FetchConfig) *Fetch\n```\nNewFetchModule creates a new Fetch pointer with the given configuration\n\n```go\nfunc NewFetchModuleFromConfig(loop EventLoop, cfg *config.FetchConfig) (*Fetch, error)\n```\nNewFetchModuleFromConfig creates a Fetch from config.FetchConfig. Converts CLI/config system configuration to the internal FetchConfig. Returns an error if TLS certificate configuration is invalid.\n\n```go\nfunc (m *Fetch) Register()\n```\nRegister registers the fetch function in the JavaScript runtime\n\n```go\ntype FetchConfig struct {\n\t// HTTPClient is the underlying HTTP client to use. If nil, a default client is created.\n\tHTTPClient *http.Client\n\n\t// DefaultTimeout is the timeout for fetch requests. Defaults to 30 seconds.\n\tDefaultTimeout time.Duration\n\n\t// MaxResponseSize is the maximum allowed response body size in bytes.\n\t// Defaults to 10MB. Set to 0 for unlimited (not recommended).\n\tMaxResponseSize int64\n\n\t// AllowedHosts is a list of hosts that are allowed to be fetched.\n\t// If nil or empty, all hosts are allowed (subject to BlockedHosts).\n\t// If set, only these hosts can be fetched.\n\tAllowedHosts []string\n\n\t// BlockedHosts is a list of hosts that are blocked from being fetched.\n\t// This is checked after AllowedHosts.\n\tBlockedHosts []string\n\n\t// AllowInsecure allows HTTP (non-HTTPS) requests.\n\t// Default is false (HTTPS only).\n\tAllowInsecure bool\n\n\t// AllowPrivateNetworks allows requests to private/local network addresses\n\t// (localhost, 127.0.0.1, 10.x.x.x, 192.168.x.x, 172.16-31.x.x, etc.)\n\t// Default is false.\n\tAllowPrivateNetworks bool\n\n\t// UserAgent is the User-Agent header to send with requests.\n\t// Defaults to \"vacuum-fetch/1.0\".\n\tUserAgent string\n}\n```\nFetchConfig contains configuration options for the fetch module. By default, fetch is restrictive: HTTPS-only and no private networks.\n\n```go\nfunc DefaultFetchConfig() *FetchConfig\n```\nDefaultFetchConfig returns a FetchConfig with secure defaults. - HTTPS only (AllowInsecure = false) - No private networks (AllowPrivateNetworks = false) - 30 second timeout - 10MB max response size\n\n```go\nfunc (c *FetchConfig) ValidateURL(rawURL string) error\n```\nValidateURL checks if a URL is allowed based on the configuration. Returns nil if allowed, or an appropriate error if not.\n\n```go\ntype Headers struct {\n\t// contains filtered or unexported fields\n}\n```\nHeaders implement the Web Fetch API Headers interface. [https://developer.mozilla.org/en-US/docs/Web/API/Headers](https://developer.mozilla.org/en-US/docs/Web/API/Headers)\n\n```go\nfunc NewHeaders(vm *goja.Runtime) *Headers\n```\nNewHeaders creates a new Headers object\n\n```go\nfunc NewHeadersFromHTTP(vm *goja.Runtime, h http.Header) *Headers\n```\nNewHeadersFromHTTP creates a Headers object from an http.Header\n\n```go\nfunc NewHeadersFromObject(vm *goja.Runtime, obj *goja.Object) *Headers\n```\nNewHeadersFromObject creates a Headers object from a JavaScript object\n\n```go\nfunc (h *Headers) ToGojaObject() *goja.Object\n```\nToGojaObject creates the JavaScript Headers object\n\n```go\nfunc (h *Headers) ToHTTPHeader() http.Header\n```\nToHTTPHeader converts to http.Header for use with http.Request\n\n```go\ntype Response struct {\n\t// contains filtered or unexported fields\n}\n```\nResponse implements the Web Fetch API Response interface. [https://developer.mozilla.org/en-US/docs/Web/API/Response](https://developer.mozilla.org/en-US/docs/Web/API/Response)\n\n```go\nfunc NewResponse(vm *goja.Runtime, init ResponseInit) *Response\n```\nNewResponse creates a new Response object\n\n```go\nfunc (r *Response) SetPromiseConstructor(ctor goja.Value)\n```\nSetPromiseConstructor sets the Promise constructor for creating Promises\n\n```go\nfunc (r *Response) ToGojaObject() *goja.Object\n```\nToGojaObject creates the JavaScript Response object\n\n```go\ntype ResponseInit struct {\n\tStatus     int\n\tStatusText string\n\tHeaders    *Headers\n\tURL        string\n\tBody       []byte\n\tRedirected bool\n}\n```\nResponseInit contains initialization options for creating a Response\n\n","path":"github.com/daveshanley/vacuum/plugin/javascript/eventloop/fetch","name":"fetch","synopsis":"Package fetch provides a Web Fetch API compliant implementation for use with Goja JavaScript runtime and the vacuum event loop.","isRedistributable":true}
