import { ReadableStream } from "../experimental/streams/index.js"; import { CookieJar } from "../http/index.js"; /** * This module provides a WebSocket API for k6. * * https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/ */ /** * The Websocket object provides the API for creating and managing WebSocket * connections as well as for sending and receiving data on the connection. */ export class WebSocket { protected __brand: never; /** * The absolute URL to which the Websocket connection is established. */ readonly url: string; /** * The current state of the Websocket connection. */ readonly readyState: ReadyState; /** * The number of bytes of data that have been queued using calls to send() * but not yet transmitted to the network. * * This value resets to zero once all queued data has been sent. * This value does not reset to zero when the connection is closed; * if you keep calling send(), this will continue to climb. */ readonly bufferedAmount: number; /** * The type of binary data being transmitted over the connection. * @default "blob" */ binaryType: BinaryType; /** * The Websocket constructor returns a newly created WebSocket object. * https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/ * * @param url - The URL to which to connect; this should be the URL to which the WebSocket server will respond. * @param protocols - Either a single protocol string or an array of protocol strings. The param is reserved for future use and will be presently ignored. * @param params - Used for setting various WebSocket connection parameters such as headers, cookie jar, compression, etc. */ constructor(url: string, protocols?: null, params?: Params | null); /** * Enqueues data to be transmitted to the server over the WebSocket connection. * https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-send/ * * @param data - the data to send to the server */ send(data: string | ArrayBuffer | Blob | ArrayBufferView): void; /** * Bind event names to event handlers to be executed when their * respective event is received by the server. * https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-addeventlistener/ * * @param event - the event to listen for * @param listener - the callback to invoke when the event is emitted */ addEventListener(event: EventName, listener: (event: MessageEvent | ErrorEvent) => void): void; /** * Closes the WebSocket connection or connection attempt, if any. * https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-close/ * * @param code - An integer WebSocket connection close code value indicating a reason for closure. * @param reason - A human-readable string WebSocket connection close reason. No longer than 123 bytes of UTF-8 text. */ close(code?: number, reason?: string): void; /** * Sends a ping message over the WebSocket connection. * https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-ping/ */ ping(): void; /** * Sets an event handler which is invoked when a message event happens. * https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-onmessage/ * * @param event - the message event */ onmessage: (event?: MessageEvent) => void; /** * Sets an event handler which is invoked when the WebSocket connection's opens. * https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-onopen/ */ onopen: () => void; /** * Sets an event handler which is invoked when the WebSocket connection's closes. * https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-onclose/ */ onclose: () => void; /** * Sets an event handler which is invoked when errors occur. * https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-onerror/ * * @param event - the error event */ onerror: (event?: ErrorEvent) => void; /** * Sets an event handler which is invoked when a ping message is received. * https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-onping/ */ onping: () => void; /** * Sets an event handler which is invoked when a pong message is received. * https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-onpong/ */ onpong: () => void; static readonly CONNECTING: 0; static readonly OPEN: 1; static readonly CLOSING: 2; static readonly CLOSED: 3; readonly CONNECTING: 0; readonly OPEN: 1; readonly CLOSING: 2; readonly CLOSED: 3; } export type BlobPart = ArrayBuffer | ArrayBufferView | Blob | string; export interface BlobOptions { type?: string | undefined; } /** * The Blob represents binary data and can be used to interact with it. */ export class Blob { readonly type: string; readonly size: number; constructor(blobParts?: BlobPart[], options?: BlobOptions); arrayBuffer(): Promise; bytes(): Promise; slice(start?: number, end?: number): Blob; stream(): ReadableStream; text(): Promise; } /** * k6 specific WebSocket parameters. * https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/params/ */ export interface Params { /** Request headers. */ headers?: Record; /** * Compression algorithm. * If the option is left unset, it defaults to no compression. */ compression?: CompressionAlgorithm; /** The custom metric tags. */ tags?: Record; /** * The cookie jar that will be used when making the initial HTTP request to establish the WebSocket connection. * If empty, the default VU cookie jar will be used. */ jar?: CookieJar; } /** * ReadyState describes the possible states of a WebSocket connection. */ export type ReadyState = 0 | 1 | 2 | 3; /** * BinaryType describes the possible types of binary data that can be * transmitted over a Websocket connection. */ export type BinaryType = "blob" | "arraybuffer"; /** * EventName describes the possible events that can be emitted * by a Websocket connection. */ export type EventName = "open" | "close" | "error" | "message" | "ping" | "pong"; /** * MessageEvent is a simple interface that holds the data of a message received from the server. */ export interface MessageEvent { /** * The data sent by the message emitter. */ data: string | ArrayBuffer | Blob; /** * the type of the event. */ type: EventName; /** * The read-only property that returns the time (in milliseconds) at which the event was created. */ timestamp: number; } /** * ErrorEvent is a simple interface that holds the data of an error event. */ export interface ErrorEvent { /** * the type of the event. */ type: EventName; /** * The read-only property that returns the error message. */ error: string; /** * The read-only property that returns the time (in milliseconds) at which the event was created. */ timestamp: number; } /** * CompressionAlgorithm describes the possible compression algorithms. */ export type CompressionAlgorithm = "deflate";