tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

HTTPResponse.ts (3296B)


      1 /**
      2 * @license
      3 * Copyright 2023 Google Inc.
      4 * SPDX-License-Identifier: Apache-2.0
      5 */
      6 
      7 import type Protocol from 'devtools-protocol';
      8 
      9 import type {SecurityDetails} from '../common/SecurityDetails.js';
     10 
     11 import type {Frame} from './Frame.js';
     12 import type {HTTPRequest} from './HTTPRequest.js';
     13 
     14 /**
     15 * @public
     16 */
     17 export interface RemoteAddress {
     18  ip?: string;
     19  port?: number;
     20 }
     21 
     22 /**
     23 * The HTTPResponse class represents responses which are received by the
     24 * {@link Page} class.
     25 *
     26 * @public
     27 */
     28 export abstract class HTTPResponse {
     29  /**
     30   * @internal
     31   */
     32  constructor() {}
     33 
     34  /**
     35   * The IP address and port number used to connect to the remote
     36   * server.
     37   */
     38  abstract remoteAddress(): RemoteAddress;
     39 
     40  /**
     41   * The URL of the response.
     42   */
     43  abstract url(): string;
     44 
     45  /**
     46   * True if the response was successful (status in the range 200-299).
     47   */
     48  ok(): boolean {
     49    // TODO: document === 0 case?
     50    const status = this.status();
     51    return status === 0 || (status >= 200 && status <= 299);
     52  }
     53 
     54  /**
     55   * The status code of the response (e.g., 200 for a success).
     56   */
     57  abstract status(): number;
     58 
     59  /**
     60   * The status text of the response (e.g. usually an "OK" for a
     61   * success).
     62   */
     63  abstract statusText(): string;
     64 
     65  /**
     66   * An object with HTTP headers associated with the response. All
     67   * header names are lower-case.
     68   */
     69  abstract headers(): Record<string, string>;
     70 
     71  /**
     72   * {@link SecurityDetails} if the response was received over the
     73   * secure connection, or `null` otherwise.
     74   */
     75  abstract securityDetails(): SecurityDetails | null;
     76 
     77  /**
     78   * Timing information related to the response.
     79   */
     80  abstract timing(): Protocol.Network.ResourceTiming | null;
     81 
     82  /**
     83   * Promise which resolves to a buffer with response body.
     84   *
     85   * @remarks
     86   *
     87   * The buffer might be re-encoded by the browser
     88   * based on HTTP-headers or other heuristics. If the browser
     89   * failed to detect the correct encoding, the buffer might
     90   * be encoded incorrectly. See https://github.com/puppeteer/puppeteer/issues/6478.
     91   */
     92  abstract content(): Promise<Uint8Array>;
     93 
     94  /**
     95   * {@inheritDoc HTTPResponse.content}
     96   */
     97  async buffer(): Promise<Buffer> {
     98    const content = await this.content();
     99    return Buffer.from(content);
    100  }
    101  /**
    102   * Promise which resolves to a text (utf8) representation of response body.
    103   */
    104  async text(): Promise<string> {
    105    const content = await this.content();
    106    return new TextDecoder().decode(content);
    107  }
    108 
    109  /**
    110   * Promise which resolves to a JSON representation of response body.
    111   *
    112   * @remarks
    113   *
    114   * This method will throw if the response body is not parsable via
    115   * `JSON.parse`.
    116   */
    117  async json(): Promise<any> {
    118    const content = await this.text();
    119    return JSON.parse(content);
    120  }
    121 
    122  /**
    123   * A matching {@link HTTPRequest} object.
    124   */
    125  abstract request(): HTTPRequest;
    126 
    127  /**
    128   * True if the response was served from either the browser's disk
    129   * cache or memory cache.
    130   */
    131  abstract fromCache(): boolean;
    132 
    133  /**
    134   * True if the response was served by a service worker.
    135   */
    136  abstract fromServiceWorker(): boolean;
    137 
    138  /**
    139   * A {@link Frame} that initiated this response, or `null` if
    140   * navigating to error pages.
    141   */
    142  abstract frame(): Frame | null;
    143 }