Skip to content

Commit

Permalink
feat: types (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m authored Dec 6, 2021
1 parent a62ace5 commit 05fa50d
Show file tree
Hide file tree
Showing 4 changed files with 2,167 additions and 77 deletions.
48 changes: 48 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Based on code by @alejo90
* @see https://github.com/DefinitelyTyped/DefinitelyTyped/blob/1d58999ab82c14bed518d0aec2ab2a5343e9f83b/types/mitm/index.d.ts
* @license MIT
*/

import * as http from "http";
import * as net from "net";

interface SocketOptions {
port: number;
host?: string | undefined;
localAddress?: string | undefined;
localPort?: string | undefined;
family?: number | undefined;
allowHalfOpen?: boolean | undefined;
}

interface BypassableSocket extends net.Socket {
bypass(): void;
}

type SocketConnectCallback = (
socket: BypassableSocket,
opts: SocketOptions
) => void;

type SocketConnectionCallback = (
socket: net.Socket,
opts: SocketOptions
) => void;

type HttpCallback = (
request: http.IncomingMessage,
response: http.ServerResponse
) => void;

type Event = "connect" | "connection" | "request";

type Callback = SocketConnectCallback | SocketConnectionCallback | HttpCallback;

export default interface Mitm {
disable(): void;
on(event: Event, callback: Callback): void;
on(event: "connect", callback: SocketConnectCallback): void;
on(event: "connection", callback: SocketConnectionCallback): void;
on(event: "request", callback: HttpCallback): void;
}
29 changes: 29 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as net from "net";
import * as http from "http";

import { expectType } from "tsd";

import Mitm from "./index.js";

export function test(mitm: Mitm) {
mitm.disable();

mitm.on("connect", (bypassableSocket, opts): void => {
expectType<() => void>(bypassableSocket.bypass);

expectType<number>(opts.port);
expectType<string | undefined>(opts.host);
});

mitm.on("connection", (socket, opts): void => {
expectType<net.Socket>(socket);

expectType<number>(opts.port);
expectType<string | undefined>(opts.host);
});

mitm.on("request", (request, response): void => {
expectType<http.IncomingMessage>(request);
expectType<http.ServerResponse>(response);
});
}
Loading

0 comments on commit 05fa50d

Please sign in to comment.