diff --git a/packages/quickjs-types/package.json b/packages/quickjs-types/package.json index c6b648d..1b0c717 100644 --- a/packages/quickjs-types/package.json +++ b/packages/quickjs-types/package.json @@ -1,6 +1,6 @@ { "name": "@caido/quickjs-types", - "version": "0.10.0", + "version": "0.11.0", "description": "Typing for the QuickJS Engine", "author": "Caido Labs Inc. ", "license": "MIT", diff --git a/packages/quickjs-types/scripts/typing.sh b/packages/quickjs-types/scripts/typing.sh index 4968c09..d89bccc 100755 --- a/packages/quickjs-types/scripts/typing.sh +++ b/packages/quickjs-types/scripts/typing.sh @@ -12,6 +12,7 @@ rsync -av --exclude=node_modules \ --include='net.d.ts' \ --include='os.d.ts' \ --include='path.d.ts' \ + --include='abort.d.ts' \ --exclude='*' \ ../../../dependency-llrt/types/ \ ./src/llrt/ diff --git a/packages/quickjs-types/src/caido/http.d.ts b/packages/quickjs-types/src/caido/http.d.ts new file mode 100644 index 0000000..f324d4c --- /dev/null +++ b/packages/quickjs-types/src/caido/http.d.ts @@ -0,0 +1,323 @@ +declare module "caido:http" { + interface BlobOpts { + /** + * One of either `'transparent'` or `'native'`. When set to `'native'`, line endings in string source parts + * will be converted to the platform native line-ending as specified by `import { EOL } from 'os'`. + */ + endings?: "transparent" | "native"; + /** + * The Blob content-type. The intent is for `type` to convey the MIME media type of the data, + * however no validation of the type format is performed. + */ + type?: string | undefined; + } + + /** + * The `Body` of a {@link Response} or {@link Request}. + * Currently NOT a `ReadableStream`. + */ + type Body = QuickJS.ArrayBufferView | Blob | null; + + /** + * A [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) encapsulates immutable, raw data. + */ + class Blob { + /** + * Creates a new `Blob` object containing a concatenation of the given sources. + * + * {ArrayBuffer}, and {Blob} sources are copied into the 'Blob' and can therefore be + * safely modified after the 'Blob' is created. + * + * String sources are also copied into the `Blob`. + */ + constructor(parts: Array, opts?: BlobOpts); + /** + * The total size of the `Blob` in bytes. + */ + readonly size: number; + /** + * The content-type of the `Blob`. + */ + readonly type: string; + /** + * Returns a promise that fulfills with an [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) containing a copy of + * the `Blob` data. + */ + arrayBuffer(): Promise; + /** + * Creates and returns a new `Blob` containing a subset of this `Blob` objects + * data. The original `Blob` is not altered. + * @param start The starting index. + * @param end The ending index. + * @param type The content-type for the new `Blob` + */ + slice(start?: number, end?: number, type?: string): Blob; + /** + * Returns a promise that fulfills with the contents of the `Blob` decoded as a UTF-8 string. + */ + text(): Promise; + /** + * Returns a promise that resolves with an Uint8Array containing the contents of the Blob. + */ + bytes(): Promise; + } + + interface FileOpts extends BlobOpts { + /** + * The last modified date of the file as the number of milliseconds since the Unix epoch (January 1, 1970 at midnight). + * Files without a known last modified date return the current date. + */ + lastModified?: number; + } + + class File extends Blob { + /** + * Returns a newly constructed File. + */ + constructor( + data: Array, + fileName: string, + opts?: FileOpts, + ); + /** + * Name of the file referenced by the File object. + */ + readonly name: string; + /** + * The last modified date of the file as the number of milliseconds since the Unix epoch (January 1, 1970 at midnight). + * Files without a known last modified date return the current date. + */ + readonly lastModified: number; + } + + type HeadersLike = Record | Headers; + + type HeadersOpts = string[][] | HeadersLike; + + class Headers implements Iterable<[string, string]> { + /** + * Creates a new Headers object. + */ + constructor(opts?: HeadersOpts); + /** + * Appends a new value onto an existing header inside a Headers object, or adds the header if it does not already exist. + */ + readonly append: (name: string, value: string) => void; + /** + * Deletes a header from a Headers object. + */ + readonly delete: (name: string) => void; + /** + * A String sequence representing the values of the retrieved header or null if this header is not set. + */ + readonly get: (name: string) => string | null; + /** + * Returns a boolean stating whether a Headers object contains a certain header. + */ + readonly has: (name: string) => boolean; + /** + * Sets a new value for an existing header inside a Headers object, or adds the header if it does not already exist. + */ + readonly set: (name: string, value: string) => void; + /** + * Returns an array containing the values of all Set-Cookie headers associated with a response. + */ + readonly getSetCookie: () => string[]; + /** + * Executes a provided function once for each key/value pair in this Headers object. + */ + readonly forEach: ( + callbackfn: (value: string, key: string) => void, + ) => void; + /** + * Returns an iterator allowing you to go through all keys of the key/value pairs contained in this object. + */ + readonly keys: () => IterableIterator; + /** + * Returns an iterator allowing you to go through all values of the key/value pairs contained in this object. + */ + readonly values: () => IterableIterator; + /** + * Returns an iterator allowing to go through all key/value pairs contained in this object. + */ + readonly entries: () => IterableIterator<[string, string]>; + readonly [Symbol.iterator]: () => Iterator<[string, string]>; + } + + interface RequestOpts { + url?: string; + method?: string; + signal?: AbortSignal; + body?: Blob; + headers?: HeadersLike; + } + + type RequestCache = "no-cache"; + + type RequestMode = "navigate"; + + /** + * The Request interface of the Fetch API represents a resource request. + */ + class Request { + /** + * Creates a new Request object. + */ + constructor(input: string | Request, init?: RequestOpts); + /** + * Contains the cache mode of the request + */ + readonly cache: RequestCache; + /** + * Contains the associated Headers object of the request. + */ + readonly headers: Headers; + /** + * Contains the request's method (GET, POST, etc.) + */ + readonly method: string; + /** + * Contains the mode of the request + */ + readonly mode: RequestMode; + /** + * Contains the URL of the request. + */ + readonly url: string; + /** + * Contains the request's keepalive setting (true or false), which indicates whether llrt will + * keep the associated connection alive. + */ + readonly keepalive: boolean; + /** + * Returns the {@link AbortSignal} associated with the request + */ + readonly signal: AbortSignal; + /** + * The body content. + */ + readonly body: Body; + /** + * Stores true or false to indicate whether or not the body has been used in a request yet. + */ + readonly bodyUsed: boolean; + /** + * Returns a promise that resolves with an ArrayBuffer representation of the request body. + */ + readonly arrayBuffer: () => Promise; + /** + * Returns a promise that resolves with a {@link Blob} representation of the request body. + */ + readonly blob: () => Promise; + /** + * Returns a promise that resolves with a {@link Uint8Array} representation of the request body. + */ + readonly bytes: () => Promise; + /** + * Returns a promise that resolves with the result of parsing the request body as JSON. + */ + readonly json: () => Promise; + /** + * Returns a promise that resolves with a text representation of the request body. + */ + readonly text: () => Promise; + /** + * Creates a copy of the current {@link Request} object. + */ + readonly clone: () => Request; + } + + type ResponseType = "basic" | "error"; + + interface ResponseInit { + readonly status?: number; + readonly statusText?: string; + readonly headers?: HeadersLike; + } + + interface ResponseOpts extends ResponseInit { + readonly url?: string; + readonly signal?: AbortSignal; + } + + /** + * The Response interface of the Fetch API represents the response to a request. + */ + class Response { + /** + * Creates a new Response object. + */ + constructor(body?: Body, opts?: ResponseOpts); + + /** + * The {@link Headers} object associated with the response. + */ + readonly headers: Headers; + /** + * A boolean indicating whether the response was successful (status in the range 200 – 299) or not. + */ + readonly ok: boolean; + /** + * The status code of the response. (This will be 200 for a success). + */ + readonly status: number; + /** + * The status message corresponding to the status code. (e.g., OK for 200). + */ + readonly statusText: string; + /** + * The type of the response. + */ + readonly type: ResponseType; + readonly url: string; + /** + * Indicates whether or not the response is the result of a redirect (that is, its URL list has more than one entry). + */ + readonly redirected: boolean; + /** + * The body content (NOT IMPLEMENTED YET). + */ + readonly body: null; + /** + * Stores a boolean value that declares whether the body has been used in a response yet. + */ + readonly bodyUsed: boolean; + /** + * Returns a promise that resolves with an {@link ArrayBuffer} representation of the response body. + */ + readonly arrayBuffer: () => Promise; + /** + * Returns a promise that resolves with a {@link Blob} representation of the response body. + */ + readonly blob: () => Promise; + /** + * Returns a promise that resolves with the result of parsing the response body text as JSON. + */ + readonly json: () => Promise; + /** + * Returns a promise that resolves with a text representation of the response body. + */ + readonly text: () => Promise; + /** + * Creates a clone of a {@link Response} object. + */ + readonly clone: () => Response; + /** + * Returns a new {@link Response} object associated with a network error. + */ + static error(): Response; + /** + * Returns a new {@link Response} object for returning the provided JSON encoded data. + */ + static json(data: any, init?: ResponseInit): Response; + /** + * Returns a new {@link Response} with a different URL. + */ + static redirect(url: string, status?: number): Response; + } + + function fetch( + input: string | Request, + init?: RequestOpts, + ): Promise; +} diff --git a/packages/quickjs-types/src/caido/index.d.ts b/packages/quickjs-types/src/caido/index.d.ts index ac73333..7c75b61 100644 --- a/packages/quickjs-types/src/caido/index.d.ts +++ b/packages/quickjs-types/src/caido/index.d.ts @@ -3,3 +3,5 @@ /// /// /// + +/// diff --git a/packages/quickjs-types/src/llrt/abort.d.ts b/packages/quickjs-types/src/llrt/abort.d.ts new file mode 100644 index 0000000..3d2a067 --- /dev/null +++ b/packages/quickjs-types/src/llrt/abort.d.ts @@ -0,0 +1,69 @@ +export {}; + +declare global { + class AbortController { + /** + * Creates a new `AbortController` object instance. + */ + constructor(); + + /** + * Returns the AbortSignal object associated with this object. + */ + readonly signal: AbortSignal; + + /** + * Invoking this method will set this object's AbortSignal's aborted flag and signal to any observers that the associated activity is to be aborted. + */ + abort(reason?: any): void; + } + + /** A signal object that allows you to communicate with a DOM request (such as a Fetch) and abort it if required via an AbortController object. */ + class AbortSignal extends EventTarget { + /** + * Creates a new `AbortSignal` object instance. + */ + constructor(); + + /** + * Returns true if this AbortSignal's AbortController has signaled to abort, and false otherwise. + */ + readonly aborted: boolean; + + /** + * A JavaScript value providing the abort reason, once the signal has aborted. + */ + readonly reason: any; + + /** + * Registers an event listener callback to execute when an `abort` event is observed. + */ + onabort: null | ((this: AbortSignal, event: Event) => any); + + /** + * Throws the signal's abort reason if the signal has been aborted; otherwise it does nothing. + */ + throwIfAborted(): void; + + /** + * Returns an `AbortSignal` instance that is already set as aborted. + * + * @param reason The reason for the abort. + */ + static abort(reason?: any): AbortSignal; + + /** + * Returns an `AbortSignal` instance that will automatically abort after a specified time. + * + * @param milliseconds The number of milliseconds to wait before aborting. + */ + static timeout(milliseconds: number): AbortSignal; + + /** + * Returns an `AbortSignal` that aborts when any of the given abort signals abort. + * + * @param signals An array of `AbortSignal` objects to observe. + */ + static any(signals: AbortSignal[]): AbortSignal; + } +} diff --git a/packages/quickjs-types/src/llrt/index.d.ts b/packages/quickjs-types/src/llrt/index.d.ts index 336caff..87fcd87 100644 --- a/packages/quickjs-types/src/llrt/index.d.ts +++ b/packages/quickjs-types/src/llrt/index.d.ts @@ -1,3 +1,4 @@ +/// /// /// /// diff --git a/packages/quickjs-types/src/llrt/os.d.ts b/packages/quickjs-types/src/llrt/os.d.ts index 7a7a234..26fe31b 100644 --- a/packages/quickjs-types/src/llrt/os.d.ts +++ b/packages/quickjs-types/src/llrt/os.d.ts @@ -42,4 +42,10 @@ declare module "os" { * * `\r\n` on Windows */ const EOL: string; + + /** + * Returns the operating system CPU architecture for which the LLRT binary was compiled. + * Possible values are 'arm64', 'x64'. The return value is equivalent to `process.arch`. + */ + export function arch(): string; }