Skip to content

Commit

Permalink
TypeScript type fixes, for #1949 (#1968)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
joshxyzhimself authored Feb 24, 2023
1 parent 987d23d commit 06f77a9
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 27 deletions.
10 changes: 10 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-check

'use strict'

/* global WebAssembly */
Expand Down Expand Up @@ -85,7 +87,15 @@ try {
channels.connected = { hasSubscribers: false }
}

/**
* @type {import('../types/client').default}
*/
class Client extends DispatcherBase {
/**
*
* @param {string|URL} url
* @param {import('../types/client').Client.Options} options
*/
constructor (url, {
interceptors,
maxHeaderSize,
Expand Down
57 changes: 57 additions & 0 deletions test/types/client.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,63 @@ expectAssignable<Client>(new Client('', {
}))
expectAssignable<Client>(new Client(new URL('http://localhost'), {}))

/**
* Tests for Client.Options:
*/
{
expectAssignable<Client>(new Client('', {
maxHeaderSize: 16384
}))
expectAssignable<Client>(new Client('', {
headersTimeout: 300e3
}))
expectAssignable<Client>(new Client('', {
connectTimeout: 300e3
}))
expectAssignable<Client>(new Client('', {
bodyTimeout: 300e3
}))
expectAssignable<Client>(new Client('', {
keepAliveTimeout: 4e3
}))
expectAssignable<Client>(new Client('', {
keepAliveMaxTimeout: 600e3
}))
expectAssignable<Client>(new Client('', {
keepAliveTimeoutThreshold: 1e3
}))
expectAssignable<Client>(new Client('', {
socketPath: '/var/run/docker.sock'
}))
expectAssignable<Client>(new Client('', {
pipelining: 1
}))
expectAssignable<Client>(new Client('', {
strictContentLength: true
}))
expectAssignable<Client>(new Client('', {
maxCachedSessions: 1
}))
expectAssignable<Client>(new Client('', {
maxRedirections: 1
}))
expectAssignable<Client>(new Client('', {
maxRequestsPerClient: 1
}))
expectAssignable<Client>(new Client('', {
localAddress: '127.0.0.1'
}))
expectAssignable<Client>(new Client('', {
maxResponseSize: -1
}))
expectAssignable<Client>(new Client('', {
autoSelectFamily: true
}))
expectAssignable<Client>(new Client('', {
autoSelectFamilyAttemptTimeout: 300e3
}))
}

{
const client = new Client('')

Expand Down
76 changes: 49 additions & 27 deletions types/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import Dispatcher from './dispatcher'
import DispatchInterceptor from './dispatcher'
import buildConnector from "./connector";

export default Client

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

declare namespace Client {
export declare namespace Client {
export interface OptionsInterceptors {
Client: readonly DispatchInterceptor[];
}
export interface Options {
/** TODO */
interceptors?: OptionsInterceptors;
/** The maximum length of request headers in bytes. Default: `16384` (16KiB). */
maxHeaderSize?: number;
/** The amount of time the parser will wait to receive the complete HTTP headers (Node 14 and above only). Default: `300e3` milliseconds (300s). */
headersTimeout?: number;
/** @deprecated unsupported socketTimeout, use headersTimeout & bodyTimeout instead */
socketTimeout?: never;
/** @deprecated unsupported requestTimeout, use headersTimeout & bodyTimeout instead */
requestTimeout?: never;
/** TODO */
connectTimeout?: number;
/** 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). */
bodyTimeout?: number;
/** @deprecated unsupported idleTimeout, use keepAliveTimeout instead */
idleTimeout?: never;
/** @deprecated unsupported keepAlive, use pipelining=0 instead */
keepAlive?: never;
/** 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). */
keepAliveTimeout?: number | null;
keepAliveTimeout?: number;
/** @deprecated unsupported maxKeepAliveTimeout, use keepAliveMaxTimeout instead */
maxKeepAliveTimeout?: never;
/** the maximum allowed `idleTimeout` when overridden by *keep-alive* hints from the server. Default: `600e3` milliseconds (10min). */
keepAliveMaxTimeout?: number | null;
keepAliveMaxTimeout?: number;
/** 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). */
keepAliveTimeoutThreshold?: number | null;
keepAliveTimeoutThreshold?: number;
/** TODO */
socketPath?: string;
/** 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`. */
pipelining?: number | null;
/** **/
connect?: buildConnector.BuildOptions | buildConnector.connector | null;
/** The maximum length of request headers in bytes. Default: `16384` (16KiB). */
maxHeaderSize?: number | null;
/** 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). */
bodyTimeout?: number | null;
/** The amount of time the parser will wait to receive the complete HTTP headers (Node 14 and above only). Default: `300e3` milliseconds (300s). */
headersTimeout?: number | null;
pipelining?: number;
/** @deprecated use the connect option instead */
tls?: never;
/** If `true`, an error is thrown when the request content-length header doesn't match the length of the request body. Default: `true`. */
strictContentLength?: boolean;
/** @deprecated use the connect option instead */
tls?: TlsOptions | null;
/** */
/** TODO */
maxCachedSessions?: number;
/** TODO */
maxRedirections?: number;
/** TODO */
connect?: buildConnector.BuildOptions | buildConnector.connector;
/** TODO */
maxRequestsPerClient?: number;
/** TODO */
localAddress?: string;
/** Max response body size in bytes, -1 is disabled */
maxResponseSize?: number | null;
maxResponseSize?: number;
/** Enables a family autodetection algorithm that loosely implements section 5 of RFC 8305. */
autoSelectFamily?: boolean;
/** The amount of time in milliseconds to wait for a connection attempt to finish before trying the next address when using the `autoSelectFamily` option. */
autoSelectFamilyAttemptTimeout?: number;

interceptors?: {Client: readonly DispatchInterceptor[] | undefined}
autoSelectFamilyAttemptTimeout?: number;
}

export interface SocketInfo {
localAddress?: string
localPort?: number
Expand All @@ -61,6 +83,6 @@ declare namespace Client {
bytesWritten?: number
bytesRead?: number
}


}

export default Client;

0 comments on commit 06f77a9

Please sign in to comment.