Skip to main content

ServiceWorkerAuthorization

The service-worker half of TwinfinitySession.registerServiceWorker: adds the session's token to requests the page could not authenticate itself.

Index

Properties

readonlyapiUrl

apiUrl: string

The URL the page registered the worker for, typically the session's whole origin. fetch adds a token only to API requests under it; see isApiRequest.

Methods

addFetchListener

  • addFetchListener(): void
  • Answers every fetch that appliesTo with fetch. For a worker that does nothing else; one that also logs or caches calls fetch from its own listener instead.


    Returns void

appliesTo

  • appliesTo(request: Request): boolean
  • Whether fetch would add a token to request: it is an API request under apiUrl (isApiRequest) and carries no Authorization header. What the request is for does not matter: a <script src>, a stylesheet, a worker script or an <embed> is authorized like an image is, and keeping uploaded content from being served as any of those is the server's job (ADR 0009).


    Parameters

    • request: Request

    Returns boolean

fetch

  • fetch(request: Request, clientId: string): Promise<Response>
  • Sends request, with the token of the page identified by clientId added when appliesTo holds and that page has one to lend. Otherwise sends it unchanged.

    The request body passes through unread, so a request is sent exactly once. There is no retry on a 401: the worker replaces a token before it expires, so a 401 is a real refusal.


    Parameters

    • request: Request
    • clientId: string

      the requesting page, i.e. FetchEvent.clientId.

    Returns Promise<Response>