Skip to content

Commit 06f77a9

Browse files
TypeScript type fixes, for #1949 (#1968)
* update types for client * update types for client.js * revert some changes made in client.js * update client.js * use never for all @deprecated properties * add tests * revert since it's false anyways lol
1 parent 987d23d commit 06f77a9

File tree

3 files changed

+116
-27
lines changed

3 files changed

+116
-27
lines changed

lib/client.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// @ts-check
2+
13
'use strict'
24

35
/* global WebAssembly */
@@ -85,7 +87,15 @@ try {
8587
channels.connected = { hasSubscribers: false }
8688
}
8789

90+
/**
91+
* @type {import('../types/client').default}
92+
*/
8893
class Client extends DispatcherBase {
94+
/**
95+
*
96+
* @param {string|URL} url
97+
* @param {import('../types/client').Client.Options} options
98+
*/
8999
constructor (url, {
90100
interceptors,
91101
maxHeaderSize,

test/types/client.test-d.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,63 @@ expectAssignable<Client>(new Client('', {
1313
}))
1414
expectAssignable<Client>(new Client(new URL('http://localhost'), {}))
1515

16+
/**
17+
* Tests for Client.Options:
18+
*/
19+
{
20+
expectAssignable<Client>(new Client('', {
21+
maxHeaderSize: 16384
22+
}))
23+
expectAssignable<Client>(new Client('', {
24+
headersTimeout: 300e3
25+
}))
26+
expectAssignable<Client>(new Client('', {
27+
connectTimeout: 300e3
28+
}))
29+
expectAssignable<Client>(new Client('', {
30+
bodyTimeout: 300e3
31+
}))
32+
expectAssignable<Client>(new Client('', {
33+
keepAliveTimeout: 4e3
34+
}))
35+
expectAssignable<Client>(new Client('', {
36+
keepAliveMaxTimeout: 600e3
37+
}))
38+
expectAssignable<Client>(new Client('', {
39+
keepAliveTimeoutThreshold: 1e3
40+
}))
41+
expectAssignable<Client>(new Client('', {
42+
socketPath: '/var/run/docker.sock'
43+
}))
44+
expectAssignable<Client>(new Client('', {
45+
pipelining: 1
46+
}))
47+
expectAssignable<Client>(new Client('', {
48+
strictContentLength: true
49+
}))
50+
expectAssignable<Client>(new Client('', {
51+
maxCachedSessions: 1
52+
}))
53+
expectAssignable<Client>(new Client('', {
54+
maxRedirections: 1
55+
}))
56+
expectAssignable<Client>(new Client('', {
57+
maxRequestsPerClient: 1
58+
}))
59+
expectAssignable<Client>(new Client('', {
60+
localAddress: '127.0.0.1'
61+
}))
62+
expectAssignable<Client>(new Client('', {
63+
maxResponseSize: -1
64+
}))
65+
expectAssignable<Client>(new Client('', {
66+
autoSelectFamily: true
67+
}))
68+
expectAssignable<Client>(new Client('', {
69+
autoSelectFamilyAttemptTimeout: 300e3
70+
}))
71+
}
72+
1673
{
1774
const client = new Client('')
1875

types/client.d.ts

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import Dispatcher from './dispatcher'
44
import DispatchInterceptor from './dispatcher'
55
import buildConnector from "./connector";
66

7-
export default Client
8-
9-
/** A basic HTTP/1.1 client, mapped on top a single TCP/TLS connection. Pipelining is disabled by default. */
10-
declare class Client extends Dispatcher {
7+
/**
8+
* A basic HTTP/1.1 client, mapped on top a single TCP/TLS connection. Pipelining is disabled by default.
9+
*/
10+
export class Client extends Dispatcher {
1111
constructor(url: string | URL, options?: Client.Options);
1212
/** Property to get and set the pipelining factor. */
1313
pipelining: number;
@@ -17,40 +17,62 @@ declare class Client extends Dispatcher {
1717
destroyed: boolean;
1818
}
1919

20-
declare namespace Client {
20+
export declare namespace Client {
21+
export interface OptionsInterceptors {
22+
Client: readonly DispatchInterceptor[];
23+
}
2124
export interface Options {
25+
/** TODO */
26+
interceptors?: OptionsInterceptors;
27+
/** The maximum length of request headers in bytes. Default: `16384` (16KiB). */
28+
maxHeaderSize?: number;
29+
/** The amount of time the parser will wait to receive the complete HTTP headers (Node 14 and above only). Default: `300e3` milliseconds (300s). */
30+
headersTimeout?: number;
31+
/** @deprecated unsupported socketTimeout, use headersTimeout & bodyTimeout instead */
32+
socketTimeout?: never;
33+
/** @deprecated unsupported requestTimeout, use headersTimeout & bodyTimeout instead */
34+
requestTimeout?: never;
35+
/** TODO */
36+
connectTimeout?: number;
37+
/** The timeout after which a request will time out, in milliseconds. Monitors time between receiving body data. Use `0` to disable it entirely. Default: `300e3` milliseconds (300s). */
38+
bodyTimeout?: number;
39+
/** @deprecated unsupported idleTimeout, use keepAliveTimeout instead */
40+
idleTimeout?: never;
41+
/** @deprecated unsupported keepAlive, use pipelining=0 instead */
42+
keepAlive?: never;
2243
/** the timeout after which a socket without active requests will time out. Monitors time between activity on a connected socket. This value may be overridden by *keep-alive* hints from the server. Default: `4e3` milliseconds (4s). */
23-
keepAliveTimeout?: number | null;
44+
keepAliveTimeout?: number;
45+
/** @deprecated unsupported maxKeepAliveTimeout, use keepAliveMaxTimeout instead */
46+
maxKeepAliveTimeout?: never;
2447
/** the maximum allowed `idleTimeout` when overridden by *keep-alive* hints from the server. Default: `600e3` milliseconds (10min). */
25-
keepAliveMaxTimeout?: number | null;
48+
keepAliveMaxTimeout?: number;
2649
/** A number subtracted from server *keep-alive* hints when overriding `idleTimeout` to account for timing inaccuracies caused by e.g. transport latency. Default: `1e3` milliseconds (1s). */
27-
keepAliveTimeoutThreshold?: number | null;
50+
keepAliveTimeoutThreshold?: number;
51+
/** TODO */
52+
socketPath?: string;
2853
/** The amount of concurrent requests to be sent over the single TCP/TLS connection according to [RFC7230](https://tools.ietf.org/html/rfc7230#section-6.3.2). Default: `1`. */
29-
pipelining?: number | null;
30-
/** **/
31-
connect?: buildConnector.BuildOptions | buildConnector.connector | null;
32-
/** The maximum length of request headers in bytes. Default: `16384` (16KiB). */
33-
maxHeaderSize?: number | null;
34-
/** The timeout after which a request will time out, in milliseconds. Monitors time between receiving body data. Use `0` to disable it entirely. Default: `300e3` milliseconds (300s). */
35-
bodyTimeout?: number | null;
36-
/** The amount of time the parser will wait to receive the complete HTTP headers (Node 14 and above only). Default: `300e3` milliseconds (300s). */
37-
headersTimeout?: number | null;
54+
pipelining?: number;
55+
/** @deprecated use the connect option instead */
56+
tls?: never;
3857
/** If `true`, an error is thrown when the request content-length header doesn't match the length of the request body. Default: `true`. */
3958
strictContentLength?: boolean;
40-
/** @deprecated use the connect option instead */
41-
tls?: TlsOptions | null;
42-
/** */
59+
/** TODO */
60+
maxCachedSessions?: number;
61+
/** TODO */
62+
maxRedirections?: number;
63+
/** TODO */
64+
connect?: buildConnector.BuildOptions | buildConnector.connector;
65+
/** TODO */
4366
maxRequestsPerClient?: number;
67+
/** TODO */
68+
localAddress?: string;
4469
/** Max response body size in bytes, -1 is disabled */
45-
maxResponseSize?: number | null;
70+
maxResponseSize?: number;
4671
/** Enables a family autodetection algorithm that loosely implements section 5 of RFC 8305. */
4772
autoSelectFamily?: boolean;
4873
/** The amount of time in milliseconds to wait for a connection attempt to finish before trying the next address when using the `autoSelectFamily` option. */
49-
autoSelectFamilyAttemptTimeout?: number;
50-
51-
interceptors?: {Client: readonly DispatchInterceptor[] | undefined}
74+
autoSelectFamilyAttemptTimeout?: number;
5275
}
53-
5476
export interface SocketInfo {
5577
localAddress?: string
5678
localPort?: number
@@ -61,6 +83,6 @@ declare namespace Client {
6183
bytesWritten?: number
6284
bytesRead?: number
6385
}
64-
65-
6686
}
87+
88+
export default Client;

0 commit comments

Comments
 (0)