Options
All
  • Public
  • Public/Protected
  • All
Menu

External module "lib/fetch"

Wrappers around the fetch function.

Index

Variables

Const my_fetch

my_fetch: function = common.is_browser ? fetch : node_fetch.default

The effective fetch implementation run by the rest of the code.

There is no default fetch implementation for node.js, hence the necessity to import 'node-fetch'. However, if the code runs in a browser, there is an error message whereby only the fetch implementation in the Window is acceptable.

This variable is a simple, polyfill like switch between the two, relying on the existence (or not) of the process variable (built-in for node.js).

I guess this makes this entry a bit polyfill like:-)

Type declaration

    • (arg: string): Promise<any>
    • Parameters

      • arg: string

      Returns Promise<any>

Functions

Const check_Web_url

  • check_Web_url(address: string): string
  • Basic sanity check on a URL supposed to be used to retrieve a Web Resource.

    The function returns a (possibly slightly modified) version of the URL if everything is fine, or a null value if the input argument is not a URL (but should be used as a filename).

    There might be errors, however, in the case it is a URL.

    The checks are as follows:

    1. Check whether the protocol is http(s). Other protocols are not accepted (actually rejected by fetch, too);
    2. Run the URL through a valid-url check, which looks at the validity of the URL in terms of characters used, for example;
    3. Check that the port (if specified) is in the allowed range, i.e., > 1024;
    4. Check whether the host name is not localhost (or equivalents, see invalid_host_names); this is not allowed, unless the code runs in a client or the environment variable R2EPUB_LOCAL is set.

    Parameters

    • address: string

    Returns string

    • the URL itself (which might be slightly improved by the valid-url method) or null if this is, in fact, not a URL

fetch_html

  • fetch_html(html_url: string): Promise<JSDOM>
  • Fetch an HTML file via fetch_resource and parse the result into a DOM instance.

    async
    throws

    Error if something goes wrong with fetch or DOM Parsing

    Parameters

    • html_url: string

    Returns Promise<JSDOM>

    • DOM object for the parsed HTML

fetch_json

  • fetch_json(json_url: string): Promise<any>
  • Fetch an JSON file via fetch_resource and parse the result into an object.

    async
    throws

    Error if something goes wrong with fetch or DOM Parsing

    Parameters

    • json_url: string

    Returns Promise<any>

    • DOM object for the parsed HTML

fetch_resource

  • fetch_resource(resource_url: string, force_text?: boolean): Promise<any>
  • Fetch a resource.

    "Fetch" means fetching the resource on the Web. There is one exception, though: some W3C files (e.g., SVG logos) have been modified for EPUB use. These files are also available on the Web (see modified_epub_files) but if the local environment variable R2EPUB_MODIFIED_EPUB_FILES is set, then the value is considered to be the name of a local directory, and the files are picked up from that directory via direct, local file system access. This may speed up and, mainly, avoid some fetch errors that unfortunately occur.

    (I am not sure why those errors occur, mainly when collections are created. I suspect this may be related to an almost simultaneous access to the same files more or less in parallel via an async call to a Promise array, and the servers may not be properly set up for that.)

    async

    Parameters

    • resource_url: string
    • Default value force_text: boolean = false

      whether the resource should be returned as text in case no content type is set by the server

    Returns Promise<any>

    • resource; either a simple text, or a Stream

fetch_type

  • fetch_type(resource_url: string): Promise<string>
  • Fetch the media type of the resource.

    async

    Parameters

    • resource_url: string

    Returns Promise<string>

    • the media type