Skip to content

Commit 73ca7f6

Browse files
XEmAX32Emanuele Sacco
andauthored
🤖 Merge PR DefinitelyTyped#74419 Add types for k6/x/socketio by @XEmAX32
Co-authored-by: Emanuele Sacco <ema.sacco32@gmail.com>
1 parent 861a82a commit 73ca7f6

5 files changed

Lines changed: 185 additions & 0 deletions

File tree

types/k6__x__socketio/.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!**/*.d.ts
3+
!**/*.d.cts
4+
!**/*.d.mts
5+
!**/*.d.*.ts

types/k6__x__socketio/index.d.ts

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
2+
declare module "k6/x/socketio" {
3+
interface SocketIOOptions {
4+
/**
5+
* Socket.IO path (default: "/socket.io/")
6+
*/
7+
path?: string;
8+
/**
9+
* Namespace to connect to (default: "/")
10+
* Example: "/chat"
11+
*/
12+
namespace?: string;
13+
/**
14+
* Auth payload sent in the connect packet
15+
*/
16+
auth?: Record<string, any>;
17+
/**
18+
* Query parameters appended to the URL
19+
*/
20+
query?: Record<string, any>;
21+
/**
22+
* Passed directly to k6/ws.connect
23+
*/
24+
params?: {
25+
/** Request headers. */
26+
headers?: { [name: string]: string };
27+
28+
/**
29+
* Compression algorithm. The only supported algorithm is `deflate`.
30+
* If the option is left unset or empty, it defaults to no compression.
31+
*/
32+
compression?: string;
33+
34+
/** Response time metric tags. */
35+
tags?: { [name: string]: string };
36+
};
37+
}
38+
39+
/**
40+
* Socket.IO socket wrapper
41+
*
42+
* Provides Socket.IO-like API on top of k6/ws WebSocket connection.
43+
* Socket.IO-specific methods (on, emit, send) override the original k6/ws methods.
44+
* All other k6/ws socket methods are available as-is via the wrapper.
45+
*/
46+
interface Socket {
47+
/**
48+
* Register an event handler (Socket.IO version)
49+
* Overrides the original k6/ws on() method
50+
*
51+
* @param event - Event name ("connect", "disconnect", or custom event)
52+
* @param handler - Handler function that receives event data
53+
*/
54+
on(event: string, handler: (data?: any) => void): void;
55+
56+
/**
57+
* Emit an event to the server (Socket.IO specific)
58+
*
59+
* @param event - Event name
60+
* @param data - Optional data to send with the event
61+
*/
62+
emit(event: string, data?: any): void;
63+
64+
/**
65+
* Send data (Socket.IO version - alias for emit("message", data))
66+
* Overrides the original k6/ws send() method
67+
*
68+
* @param data - Data to send
69+
*/
70+
send(data: any): void;
71+
72+
/**
73+
* Close connection.
74+
* https://grafana.com/docs/k6/latest/javascript-api/k6-ws/socket/socket-close/
75+
*
76+
* @param code - WebSocket status code.
77+
* @example
78+
* socket.close();
79+
*/
80+
close(code?: number): void;
81+
}
82+
83+
/**
84+
* Connect to a Socket.IO server
85+
*
86+
* @param host - Base URL such as "http://localhost:4000" or "wss://example.com"
87+
* @param options - Optional configuration
88+
* @param handler - Optional callback function called with a Socket.IO-like socket wrapper
89+
* @returns The underlying k6/ws.connect result
90+
*
91+
* @example
92+
* ```typescript
93+
* import { io } from "k6/x/socketio";
94+
*
95+
* io("http://localhost:4000", {}, (socket) => {
96+
* socket.on("connect", () => {
97+
* socket.emit("hello", { payload: "hi from k6" });
98+
* });
99+
* });
100+
* ```
101+
*/
102+
function io(
103+
host: string,
104+
options?: SocketIOOptions,
105+
handler?: (socket: Socket) => void
106+
): any;
107+
108+
export { io, Socket, SocketIOOptions };
109+
}
110+
111+
declare module "xk6-socketio" {
112+
import { io, Socket, SocketIOOptions } from "k6/x/socketio";
113+
export { io, Socket, SocketIOOptions };
114+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { io, type Socket, type SocketIOOptions } from "k6/x/socketio";
2+
3+
const opts: SocketIOOptions = {
4+
path: "/socket.io/",
5+
namespace: "/chat",
6+
auth: { token: "abc" },
7+
query: { a: 1 },
8+
params: {
9+
headers: { Authorization: "Bearer token" },
10+
compression: "deflate",
11+
tags: { name: "socketio" },
12+
},
13+
};
14+
15+
io("http://localhost:4000", opts, (socket: Socket) => {
16+
socket.on("connect", () => {});
17+
socket.on("hello", (data) => {
18+
const _x: any = data;
19+
});
20+
21+
socket.emit("hello", { payload: "hi from k6" });
22+
socket.send({ msg: "message" });
23+
24+
socket.close();
25+
26+
});
27+
28+
// also works without options/handler
29+
io("wss://example.com");

types/k6__x__socketio/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "@types/k6__x__socketio",
3+
"version": "0.1.9999",
4+
"private": true,
5+
"nonNpm": true,
6+
"nonNpmDescription": "Type definitions for xk6 extension providing Socket.IO support in k6, not distributed via npm",
7+
"projects": ["https://github.com/XEmAX32/xk6-socket.io"],
8+
"owners": [
9+
{
10+
"name": "Emanuele Sacco",
11+
"url": "https://github.com/xemax32"
12+
}
13+
],
14+
"dependencies": {
15+
"@types/k6": "*"
16+
},
17+
"devDependencies": {
18+
"@types/k6__x__socketio": "workspace:."
19+
}
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"compilerOptions": {
3+
"module": "node16",
4+
"lib": ["es2015"],
5+
"noEmit": true,
6+
"types": [],
7+
"forceConsistentCasingInFileNames": true,
8+
"noImplicitAny": true,
9+
"noImplicitThis": true,
10+
"strictNullChecks": true,
11+
"strictFunctionTypes": true
12+
},
13+
"files": [
14+
"index.d.ts",
15+
"k6__x__socketio-tests.ts"
16+
]
17+
}

0 commit comments

Comments
 (0)