mock-violentmonkey
Version:
Mock violentmonkey's globals for testing userscripts
124 lines (123 loc) • 4.24 kB
TypeScript
/**
* This is not meant to be completely spec compliant.
* It is meant to be spec compliant where it matters for GM_xmlhttpRequest
* For example abort before send behaves weirdly (even more so depending on the browser)
* but luckily GM_xmlhttpRequest will never abort before send so it doesn't matter
*/
/**
* Wrapper for built-in http.js to emulate the browser XMLHttpRequest object.
*
* @author Dan DeFelippi <dan@driverdan.com>
* @author MeLusc <https://github.com/melusc>
* @contributor David Ellis <d.f.ellis@ieee.org>
* @license MIT
*/
import { Buffer } from 'node:buffer';
export type UppercaseMethods = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'OPTIONS';
export type Method = UppercaseMethods | Lowercase<UppercaseMethods>;
export type ReadyState = 0 | 1 | 2 | 3 | 4;
export type Options = {
base?: string | URL | undefined;
};
export type Events = 'abort' | 'error' | 'load' | 'loadend' | 'loadstart' | 'progress' | 'readystatechange' | 'timeout';
export type Headers = Record<string, string | undefined>;
/**
* `XMLHttpRequest` constructor.
*
* Supported options for the `opts` object are:
*
* - `agent`: An http.Agent instance; http.globalAgent may be used; if 'undefined', agent usage is disabled
*
* @param {Object} opts optional "options" object
*/
declare class XMLHttpRequest {
#private;
/**
* Constants
*/
static readonly UNSENT = 0;
static readonly OPENED = 1;
static readonly HEADERS_RECEIVED = 2;
static readonly LOADING = 3;
static readonly DONE = 4;
readonly UNSENT = 0;
readonly OPENED = 1;
readonly HEADERS_RECEIVED = 2;
readonly LOADING = 3;
readonly DONE = 4;
onabort: (() => void) | undefined;
onerror: (() => void) | undefined;
onload: (() => void) | undefined;
onloadend: (() => void) | undefined;
onloadstart: (() => void) | undefined;
onprogress: (() => void) | undefined;
onreadystatechange: (() => void) | undefined;
ontimeout: (() => void) | undefined;
readyState: ReadyState;
responseBuffer: Buffer;
responseURL: string;
status: number;
statusText: string;
timeout: number;
/**
* @param options Set the options for the request
*/
constructor(options?: Options);
/**
* Sets a header for the request.
*
* @param string header Header name
* @param string value Header value
* @return boolean Header added
*/
setRequestHeader: (header: string, value: Headers[string]) => boolean;
/**
* Open the connection. Currently supports local server requests.
*
* @param string method Connection method (eg GET, POST)
* @param string url URL for the connection.
* @param string user Username for basic authentication (optional)
* @param string password Password for basic authentication (optional)
*/
open: (method: Method, url: string, user?: string, password?: string) => void;
/**
* Gets a header from the server response.
*
* @param string header Name of header to get.
* @return string Text of the header or null if it doesn't exist.
*/
getResponseHeader: (header: string) => string | string[] | null | undefined;
/**
* Gets all the response headers.
*
* @return string A string with all response headers separated by CR+LF
*/
getAllResponseHeaders: () => string;
/**
* Gets a request header
*
* @param string name Name of header to get
* @return string Returns the request header or empty string if not set
*/
getRequestHeader: (name: string) => string;
/**
* Sends the request to the server.
*
* @param {unknown} data Optional data to send as request body.
*/
send: (data?: Buffer) => void;
/**
* Aborts a request.
*/
abort: () => void;
/**
* Adds an event listener. Preferred method of binding to events.
*/
addEventListener: (event: Events, callback: () => void) => void;
/**
* Remove an event callback that has already been bound.
* Only works on the matching funciton, cannot be a copy.
*/
removeEventListener: (event: Events, callback: () => void) => void;
}
export { XMLHttpRequest };