Skip to content

Commit ea1fbdb

Browse files
committed
Update to support changes in latest Node types
1 parent 07caef5 commit ea1fbdb

5 files changed

Lines changed: 30 additions & 39 deletions

File tree

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,27 @@
11
// There's a few places where we attach extra data to some node objects during
22
// connection setup etc, to better track data & handle issues:
33

4-
declare module "net" {
5-
import * as net from 'net';
6-
import * as stream from 'stream';
4+
import * as stream from 'stream';
5+
import * as net from 'net';
76

7+
declare module "net" {
88
interface Socket {
99
// Normally only defined on TLSSocket, but useful to explicitly include here
1010
// Undefined on plain HTTP, 'true' on TLSSocket.
1111
encrypted?: boolean;
1212

1313
// Internal reference to the parent socket, available on TLS sockets
14-
_parent?: Socket;
14+
_parent?: net.Socket;
1515

1616
// Internal reference to the underlying stream, available on _stream_wrap
1717
stream?: stream.Duplex & Partial<net.Socket>;
1818
}
1919
}
2020

2121
declare module "tls" {
22-
import * as stream from 'stream';
23-
import * as net from 'net';
24-
2522
interface TLSSocket {
26-
// This is a real field that actually exists - unclear why it's not
27-
// in the type definitions.
28-
servername?: string;
29-
30-
_handle?: { // Internal, used for monkeypatching & error tracking
23+
// Internal handle, used for monkeypatching & error tracking
24+
_handle?: {
3125
oncertcb?: (info: any) => any;
3226
_parentWrap?: { // SocketWrapper
3327
stream?: stream.Duplex & Partial<net.Socket>
@@ -37,11 +31,6 @@ declare module "tls" {
3731
}
3832

3933
declare module "http" {
40-
// Two missing methods from the official types:
4134
export function validateHeaderName(name: string): void;
4235
export function validateHeaderValue(name: string, value: unknown): void;
4336
}
44-
45-
declare class AggregateError extends Error {
46-
errors: Error[]
47-
}

src/admin/admin-server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ export class AdminServer<Plugins extends { [key: string]: AdminPlugin<any, any>
301301

302302
this.server.on('error', reject);
303303

304-
this.server.on('upgrade', async (req: http.IncomingMessage, socket: net.Socket, head: Buffer) => {
304+
this.server.on('upgrade', async (req, socket, head) => {
305305
const reqOrigin = req.headers['origin'] as string | undefined;
306306
if (this.requiredOrigin && !await strictOriginMatch(reqOrigin, this.requiredOrigin)) {
307307
console.warn(`Websocket request from invalid origin: ${req.headers['origin']}`);

src/server/mockttp-server.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ export class MockttpServer extends AbstractMockttp implements Mockttp {
363363
return Promise.resolve();
364364
}
365365

366-
public listenerCount(event: string, listener?: Function): number {
366+
public listenerCount(event: string, listener?: ((...args: any[]) => void)): number {
367367
return this.eventEmitter.listenerCount(event, listener);
368368
}
369369

@@ -1217,7 +1217,7 @@ ${await this.suggestRule(request)}`
12171217
upstreamSocket.pipe(socket);
12181218

12191219
if (type === 'raw') {
1220-
socket.on('data', (data) => {
1220+
socket.on('data', (data: Buffer) => {
12211221
const eventTimestamp = now();
12221222
setImmediate(() => {
12231223
this.eventEmitter.emit('raw-passthrough-data', {
@@ -1228,7 +1228,7 @@ ${await this.suggestRule(request)}`
12281228
} satisfies RawPassthroughDataEvent);
12291229
});
12301230
});
1231-
upstreamSocket.on('data', (data) => {
1231+
upstreamSocket.on('data', (data: Buffer) => {
12321232
const eventTimestamp = now();
12331233
setImmediate(() => {
12341234
this.eventEmitter.emit('raw-passthrough-data', {
@@ -1247,7 +1247,7 @@ ${await this.suggestRule(request)}`
12471247
upstreamSocket.destroy();
12481248
});
12491249

1250-
upstreamSocket.on('error', (e) => {
1250+
upstreamSocket.on('error', (e: ErrorLike) => {
12511251
if (this.debug) console.warn(`Upstream ${type} passthrough error to ${hostname}:${targetPort}:`, e);
12521252
eventData.tags.push(`${type}-passthrough-error:${e.code || 'UNKNOWN'}`);
12531253
socket.destroy()

src/util/socket-extensions.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ export interface SocketMetadata {
2020
[key: string]: any;
2121
}
2222

23+
export interface SocketTimingData {
24+
initialSocket: number; // Initial raw socket time, since unix epoch
25+
26+
// High-precision timestamps:
27+
initialSocketTimestamp: number;
28+
tunnelSetupTimestamp?: number; // Latest CONNECT completion, if any
29+
tlsConnectedTimestamp?: number; // Latest TLS handshake completion, if any
30+
lastRequestTimestamp?: number; // Latest request or websocket request time, if any
31+
}
32+
2333
declare module 'net' {
2434
interface Socket {
2535
/**
@@ -47,15 +57,7 @@ declare module 'net' {
4757
* Our recordings of various timestamps, used for monitoring &
4858
* performance analysis later on
4959
*/
50-
[SocketTimingInfo]?: {
51-
initialSocket: number; // Initial raw socket time, since unix epoch
52-
53-
// High-precision timestamps:
54-
initialSocketTimestamp: number;
55-
tunnelSetupTimestamp?: number; // Latest CONNECT completion, if any
56-
tlsConnectedTimestamp?: number; // Latest TLS handshake completion, if any
57-
lastRequestTimestamp?: number; // Latest request or websocket request time, if any
58-
}
60+
[SocketTimingInfo]?: SocketTimingData;
5961

6062
// Set on TLSSocket, defined here for convenient access on _all_ sockets
6163
[TlsMetadata]?: TlsSocketMetadata;
@@ -101,22 +103,22 @@ declare module 'tls' {
101103
}
102104

103105
declare module 'http2' {
104-
class Http2Session {
106+
interface Http2Session {
105107
// session.socket is cleared before error handling kicks in. That's annoying,
106108
// so we manually preserve the socket elsewhere to work around it.
107109
initialSocket?: net.Socket;
108110
}
109111

110-
class ServerHttp2Stream {
112+
interface ServerHttp2Stream {
111113
// Treated the same as net.Socket, when we unwrap them in our combo server:
112-
[LastHopEncrypted]?: net.Socket[typeof LastHopEncrypted];
113-
[LastTunnelAddress]?: net.Socket[typeof LastTunnelAddress];
114-
[SocketTimingInfo]?: net.Socket[typeof SocketTimingInfo];
114+
[LastHopEncrypted]?: boolean;
115+
[LastTunnelAddress]?: string;
116+
[SocketTimingInfo]?: SocketTimingData;
115117
[SocketMetadata]?: SocketMetadata;
116118
}
117119
}
118120

119121
export type SocketIsh<MinProps extends keyof net.Socket & keyof tls.TLSSocket> =
120122
streams.Duplex &
121123
Partial<Pick<net.Socket, MinProps>> &
122-
Partial<Pick<tls.TLSSocket, MinProps>>;
124+
Partial<Pick<tls.TLSSocket, MinProps>>;

tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"compilerOptions": {
3-
"target": "ES2020",
3+
"target": "ES2022",
44
"module": "commonjs",
55
"outDir": "./dist",
66
"declaration": true,
77
"sourceMap": true,
88
"declarationMap": true,
99
"strict": true,
1010
"lib": [
11-
"ES2020",
11+
"ES2022"
1212
],
1313
"paths": {
1414
"https-proxy-agent": ["./custom-typings/proxy-agent-modules.d.ts"],

0 commit comments

Comments
 (0)