Resource is an abstraction to help deal with links and subresources of web pages.

Each Resource has content and a URL, and may have links to other URLs, which can again be represented as a Resource, and so on; resources can thus form a tree of (sub)resources.

For example, a web page (DomResource) might link to a stylesheet (StylesheetResource) which may link to a font (LeafResource).

Each such subclass of Resource exposes the links it contains. Note that besides the user-visible links made by <a> elements, links are also created by e.g. the src="…" of an <img> element (in HTML), or the url(…) in a background-image value (in CSS).

The target of a Link can be modified, which updates the resource content accordingly.

Each subclass also provides a dry() method that transforms the contents to be usable outside of its original context (e.g. served from a different URL), and to be as accurately as possible a snapshot of its current state (e.g. dynamic DOM state is made part of its HTML).

Any Resource’s content can be accessed as a blob. Subclasses may also provide other formats: DomResource.doc, DomResource.string, and StylesheetResource.string.

Hierarchy

Methods

  • Fetch the resource a given link points to, and return it as a Resource.

    This method does not modify the given link; the caller can store the created Resource in link.resource, to grow a tree of links and resources.

    Example

    link.resource = await Resource.fromLink(link)
    

    Returns

    The newly created Resource.

    Parameters

    • link: SubresourceLink

      The link pointing to the resource.

    • config: GlobalConfig & { fetchResource?: Fetchy; signal?: AbortSignal } = {}

      Optional environment configuration.

    Returns Promise<Resource>

  • Create a Resource from a Blob and a URL, and a subresource type.

    Note that the URL is not resolved (see fromLink for that), but is used to interpret any relative links that the resource may contain.

    Currently, the subresourceType is mandatory, and determines what subclass of Resource is instantiated (in freeze-dry, this method is only used for subresources, so the expected type is always known). The Blob’s MIME type is ignored.

    Returns

    An instance of a subclass of Resource matching the given subresource type.

    Parameters

    Returns Promise<Resource>

  • Determine the Resource subclass to use for the given subresource type.

    Returns

    The appropriate Resource subclass, or undefined if the type is not supported.

    Parameters

    • subresourceType: undefined | SubresourceType

      The type of subresource expected by the parent resource, e.g. 'image' or 'style'. Note this is not the same as its MIME type.

    Returns undefined | ResourceFactory

  • Perform a function on each subresource link.

    Returns

    A promise that completes when all invocations have completed.

    Parameters

    Returns Promise<void>

  • ‘Dry’ the resource, i.e. make it static and context-free.

    Returns void

  • Make ‘outward’ links absolute, and ‘within-document’ links relative (e.g. href="#top").

    Returns void

Constructors

Properties

url: `${string}:${string}`

URL of the resource.

blob: Blob

A Blob with the current resource content.

links: Link[]

An array of Links, providing a live view on the links defined in the resource. Changing the target of a link will change the resource content.

Accessors

  • An array of Links (a subset of links), containing only subresource links, and for whose subresourceType a Resource subclass exists. That is, those links that fromLink accepts.

    Returns SubresourceLink[]