fetch

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package fetch provides the HTTP API exposed to WebGo programs.

Index

Constants

View Source
const (
	Millisecond time.Duration = time.Millisecond
	Second      time.Duration = time.Second
)

Variables

View Source
var (
	ErrInvalidHeader   = errors.New("invalid Fetch header")
	ErrForbiddenHeader = errors.New("forbidden Fetch request header")
	ErrHeadersTooLarge = errors.New("Fetch headers exceed size limit")
)
View Source
var ErrBodyConsumed = errors.New("response body has already been consumed")

ErrBodyConsumed reports a second attempt to consume one response body.

View Source
var ErrInvalidText = errors.New("response body is not valid UTF-8")

Functions

This section is empty.

Types

type API

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

API binds WebGo Fetch calls to one page URL and network executor.

func New

func New(baseURL *url.URL, do func(context.Context, *network.Request) (*network.Response, error)) *API

New creates a page-scoped Fetch API.

func NewPage

func NewPage(ctx context.Context, baseURL *url.URL, do func(context.Context, *network.Request) (*network.Response, error), enqueue func(func()) bool) *API

NewPage creates an asynchronous Fetch API bound to a page callback queue.

func NewPageWithClock added in v0.11.0

func NewPageWithClock(ctx context.Context, baseURL *url.URL, do func(context.Context, *network.Request) (*network.Response, error), enqueue func(func()) bool, clock Clock) *API

func (*API) Close

func (api *API) Close()

Close rejects new Fetches and waits for all request goroutines to release references.

func (*API) Fetch

func (api *API) Fetch(request Request, success func(Response), failure func(string))

Fetch starts request asynchronously and delivers exactly one callback.

func (*API) SetLimiter added in v0.11.0

func (api *API) SetLimiter(limiter *Limiter)

type AbortController added in v0.11.0

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

AbortController owns one signal used to cancel Fetch requests.

func NewAbortController added in v0.11.0

func NewAbortController() *AbortController

NewAbortController creates a controller with an active signal.

func (*AbortController) Abort added in v0.11.0

func (controller *AbortController) Abort()

Abort cancels the signal. Repeated calls are harmless.

func (*AbortController) Signal added in v0.11.0

func (controller *AbortController) Signal() *AbortSignal

Signal returns this controller's signal.

type AbortSignal added in v0.11.0

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

AbortSignal is a read-only cancellation state for a Fetch request.

func (*AbortSignal) Aborted added in v0.11.0

func (signal *AbortSignal) Aborted() bool

Aborted reports whether the signal has been canceled.

type Clock added in v0.11.0

type Clock interface {
	AfterFunc(time.Duration, func()) Timer
}

type CredentialsMode

type CredentialsMode string

CredentialsMode controls Cookie and HTTP authentication handling.

const (
	CredentialsOmit       CredentialsMode = "omit"
	CredentialsSameOrigin CredentialsMode = "same-origin"
	CredentialsInclude    CredentialsMode = "include"
)
type Header map[string][]string

Header preserves every value associated with an HTTP header name.

type HeaderEntry added in v0.11.0

type HeaderEntry struct{ Name, Value string }

HeaderEntry is one ordered HTTP field name/value pair.

type Headers added in v0.11.0

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

Headers stores ordered HTTP fields while preserving repeated values.

func NewHeaders added in v0.11.0

func NewHeaders() *Headers

NewHeaders creates an empty request header collection.

func (*Headers) Append added in v0.11.0

func (headers *Headers) Append(name, value string) error

Append adds one validated request header field.

func (*Headers) Clone added in v0.11.0

func (headers *Headers) Clone() *Headers

Clone returns an independent header collection.

func (*Headers) Delete added in v0.11.0

func (headers *Headers) Delete(name string)

Delete removes all values for name.

func (*Headers) Entries added in v0.11.0

func (headers *Headers) Entries() []HeaderEntry

Entries returns a defensive ordered copy.

func (*Headers) Get added in v0.11.0

func (headers *Headers) Get(name string) (string, bool)

Get returns the first value for name.

func (*Headers) Has added in v0.11.0

func (headers *Headers) Has(name string) bool

Has reports whether name has at least one value.

func (*Headers) Set added in v0.11.0

func (headers *Headers) Set(name, value string) error

Set replaces all values for name while retaining the first matching position.

func (*Headers) Values added in v0.11.0

func (headers *Headers) Values(name string) []string

Values returns all values for name in insertion order.

type Limiter added in v0.11.0

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

Limiter bounds in-flight Fetch operations shared by a browser session.

func NewLimiter added in v0.11.0

func NewLimiter(limit int) *Limiter

func (*Limiter) Acquire added in v0.11.0

func (limiter *Limiter) Acquire() bool

func (*Limiter) Release added in v0.11.0

func (limiter *Limiter) Release()

type Request

type Request struct {
	Method      string
	URL         string
	Header      Header
	Headers     *Headers
	Body        []byte
	Text        string
	JSON        string
	Params      *urlapi.URLSearchParams
	FormData    *formapi.FormData
	Signal      *AbortSignal
	Timeout     time.Duration
	Credentials CredentialsMode
}

Request describes an HTTP request issued by a WebGo program. Body takes precedence over Text when both are set.

type Response

type Response struct {
	Status     int
	StatusText string
	URL        string
	Redirected bool
	Header     Header
	Headers    *ResponseHeaders
	// contains filtered or unexported fields
}

Response is delivered to a successful Fetch callback. Additional response metadata and body helpers are added by the HTTP lifecycle implementation.

func (Response) BodyUsed added in v0.11.0

func (response Response) BodyUsed() bool

func (Response) Bytes

func (response Response) Bytes() ([]byte, error)

Bytes consumes the response body and returns a defensive copy.

func (Response) JSON

func (response Response) JSON(target any) error

JSON consumes and decodes the response body into target.

func (Response) Text

func (response Response) Text() (string, error)

Text consumes the response body as UTF-8 text.

type ResponseHeaders added in v0.11.0

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

ResponseHeaders is an immutable, defensive view of response fields.

func (*ResponseHeaders) Entries added in v0.11.0

func (headers *ResponseHeaders) Entries() []HeaderEntry

func (*ResponseHeaders) Get added in v0.11.0

func (headers *ResponseHeaders) Get(name string) (string, bool)

func (*ResponseHeaders) Values added in v0.11.0

func (headers *ResponseHeaders) Values(name string) []string

type Timer added in v0.11.0

type Timer interface{ Stop() bool }

Jump to

Keyboard shortcuts

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