serviceworker

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2026 License: MIT Imports: 27 Imported by: 0

Documentation

Overview

Package serviceworker owns origin-scoped Service Worker registrations.

Index

Constants

View Source
const (
	MaxCachesPerOrigin    = 32
	MaxCacheEntries       = 4096
	MaxCacheResponseBytes = 4 << 20
	MaxCacheOriginBytes   = 128 << 20
)
View Source
const (
	MaxRegistrations     = 64
	MaxWorkerScriptBytes = 2 << 20
)

Variables

View Source
var (
	ErrCacheQuota   = errors.New("service worker Cache Storage quota exceeded")
	ErrCacheRequest = errors.New("service worker Cache only stores GET requests")
)
View Source
var (
	ErrInsecureContext = errors.New("service worker requires a secure context")
	ErrCrossOrigin     = errors.New("service worker script and scope must be same-origin")
	ErrScope           = errors.New("service worker scope exceeds the script directory")
	ErrQuota           = errors.New("service worker registration quota exceeded")
)
View Source
var (
	ErrPersistenceIO      = errors.New("service worker persistence I/O failure")
	ErrPersistenceCorrupt = errors.New("corrupt service worker profile data")
)
View Source
var ErrServiceWorkerProcess = errors.New("service worker process failure")

Functions

func IsSecureContext

func IsSecureContext(target *url.URL) bool

IsSecureContext accepts HTTPS and HTTP loopback development origins.

Types

type Cache

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

Cache is a handle to one named origin cache.

func (*Cache) Delete

func (cache *Cache) Delete(request *network.Request) bool

Delete removes one request entry.

func (*Cache) Keys

func (cache *Cache) Keys() []*network.Request

Keys returns cloned requests in deterministic URL order.

func (*Cache) Match

func (cache *Cache) Match(request *network.Request) (*network.Response, bool)

Match returns a cloned cached response.

func (*Cache) Put

func (cache *Cache) Put(request *network.Request, response *network.Response) error

Put atomically stores a buffered response after stripping credential headers.

type CacheStorage

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

CacheStorage is a concurrency-safe, origin-partitioned response store.

func (*CacheStorage) Delete

func (storage *CacheStorage) Delete(origin, name string) bool

Delete removes a named cache and releases its quota atomically.

func (*CacheStorage) Has

func (storage *CacheStorage) Has(origin, name string) bool

Has reports whether a named cache exists for origin.

func (*CacheStorage) Keys

func (storage *CacheStorage) Keys(origin string) []string

Keys returns cache names in deterministic order.

func (*CacheStorage) Match

func (storage *CacheStorage) Match(origin string, request *network.Request) (*network.Response, bool)

Match searches named caches in stable lexical order.

func (*CacheStorage) Open

func (storage *CacheStorage) Open(origin, name string) (*Cache, error)

Open creates or returns one named cache without crossing an Origin boundary.

type FetchScript

type FetchScript func(context.Context, *network.Request) (*network.Response, error)

FetchScript retrieves one Service Worker script through Browser-owned I/O.

type Manager

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

Manager stores registrations for one browser profile.

func NewManager

func NewManager() *Manager

NewManager creates an empty in-memory registration store.

func NewPersistentManager

func NewPersistentManager(dataRoot string) (*Manager, error)

NewPersistentManager restores Service Worker profile state below dataRoot. A corrupt file is quarantined without suppressing healthy Origins.

func (*Manager) Caches

func (manager *Manager) Caches() *CacheStorage

Caches returns the origin-partitioned Cache Storage owned by this profile.

func (*Manager) Close

func (manager *Manager) Close() error

Close stops every idle or active Service Worker process owned by the profile.

func (*Manager) Controller

func (manager *Manager) Controller(clientURL *url.URL) *runtimemodel.ServiceWorkerRegistration

Controller returns the active claimed worker for a client, if any.

func (*Manager) DispatchFetch

func (manager *Manager) DispatchFetch(ctx context.Context, request *network.Request, fallback NetworkFallback) (*network.Response, error)

DispatchFetch delivers a controlled request to the longest matching active worker. A worker that does not synchronously call respondWith uses fallback.

func (*Manager) GetRegistration

func (manager *Manager) GetRegistration(clientURL *url.URL, clientValue string) (*runtimemodel.ServiceWorkerRegistration, error)

GetRegistration returns the longest active scope matching clientValue.

func (*Manager) GetRegistrations

func (manager *Manager) GetRegistrations(clientURL *url.URL) ([]runtimemodel.ServiceWorkerRegistration, error)

GetRegistrations returns same-origin registrations ordered by scope.

func (*Manager) Register

func (manager *Manager) Register(ctx context.Context, clientURL *url.URL, scriptValue, scopeValue string, fetch FetchScript) (runtimemodel.ServiceWorkerRegistration, error)

Register fetches and atomically installs or updates one scope.

func (*Manager) Unregister

func (manager *Manager) Unregister(clientURL *url.URL, scopeValue string) (bool, error)

Unregister removes one same-origin scope and makes its workers redundant.

func (*Manager) Update

func (manager *Manager) Update(ctx context.Context, clientURL *url.URL, scopeValue string, fetch FetchScript) (runtimemodel.ServiceWorkerRegistration, error)

Update revalidates the currently configured script URL.

type NetworkFallback

type NetworkFallback func(context.Context, *network.Request) (*network.Response, error)

NetworkFallback performs a request without re-entering Service Worker dispatch.

Jump to

Keyboard shortcuts

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