Skip to content

Commit

Permalink
refactor(types): deduplicate listen options and export it
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranet committed Jul 9, 2022
1 parent 20263a1 commit c56d4c8
Showing 1 changed file with 49 additions and 91 deletions.
140 changes: 49 additions & 91 deletions types/instance.d.ts
@@ -1,6 +1,6 @@
import * as http from 'http'
import { FastifyError } from '@fastify/error'
import { ConstraintStrategy, HTTPVersion } from 'find-my-way'
import * as http from 'http'
import { CallbackFunc as LightMyRequestCallback, Chain as LightMyRequestChain, InjectOptions, Response as LightMyRequestResponse } from 'light-my-request'
import { AddContentTypeParser, ConstructorAction, FastifyBodyParser, getDefaultJsonParser, hasContentTypeParser, ProtoAction, removeAllContentTypeParsers, removeContentTypeParser } from './content-type-parser'
import { onCloseAsyncHookHandler, onCloseHookHandler, onErrorAsyncHookHandler, onErrorHookHandler, onReadyAsyncHookHandler, onReadyHookHandler, onRegisterHookHandler, onRequestAsyncHookHandler, onRequestHookHandler, onResponseAsyncHookHandler, onResponseHookHandler, onRouteHookHandler, onSendAsyncHookHandler, onSendHookHandler, onTimeoutAsyncHookHandler, onTimeoutHookHandler, preHandlerAsyncHookHandler, preHandlerHookHandler, preParsingAsyncHookHandler, preParsingHookHandler, preSerializationAsyncHookHandler, preSerializationHookHandler, preValidationAsyncHookHandler, preValidationHookHandler } from './hooks'
Expand Down Expand Up @@ -30,6 +30,52 @@ export interface PrintRoutesOptions {
includeHooks?: boolean
}

export interface FastifyListenOptions {
/**
* Default to `0` (picks the first available open port).
*/
port?: number;
/**
* Default to `localhost`.
*/
host?: string;
/**
* Will be ignored if `port` is specified.
* @see [Identifying paths for IPC connections](https://nodejs.org/api/net.html#identifying-paths-for-ipc-connections).
*/
path?: string;
/**
* Specify the maximum length of the queue of pending connections.
* The actual length will be determined by the OS through sysctl settings such as `tcp_max_syn_backlog` and `somaxconn` on Linux.
* Default to `511`.
*/
backlog?: number;
/**
* Default to `false`.
*/
exclusive?: boolean;
/**
* For IPC servers makes the pipe readable for all users.
* Default to `false`.
*/
readableAll?: boolean;
/**
* For IPC servers makes the pipe writable for all users.
* Default to `false`.
*/
writableAll?: boolean;
/**
* For TCP servers, setting `ipv6Only` to `true` will disable dual-stack support, i.e., binding to host `::` won't make `0.0.0.0` be bound.
* Default to `false`.
*/
ipv6Only?: boolean;
/**
* An AbortSignal that may be used to close a listening server.
* @since This option is available only in Node.js v15.6.0 and greater
*/
signal?: AbortSignal;
}

type NotInInterface<Key, _Interface> = Key extends keyof _Interface ? never : Key
type FindMyWayVersion<RawServer extends RawServerBase> = RawServer extends http.Server ? HTTPVersion.V1 : HTTPVersion.V2

Expand Down Expand Up @@ -117,96 +163,8 @@ export interface FastifyInstance<
inject(opts: InjectOptions | string): Promise<LightMyRequestResponse>;
inject(): LightMyRequestChain;

listen(opts: {
/**
* Default to `0` (picks the first available open port).
*/
port?: number;
/**
* Default to `localhost`.
*/
host?: string;
/**
* Will be ignored if `port` is specified.
* @see [Identifying paths for IPC connections](https://nodejs.org/api/net.html#identifying-paths-for-ipc-connections).
*/
path?: string;
/**
* Specify the maximum length of the queue of pending connections.
* The actual length will be determined by the OS through sysctl settings such as `tcp_max_syn_backlog` and `somaxconn` on Linux.
* Default to `511`.
*/
backlog?: number;
/**
* Default to `false`.
*/
exclusive?: boolean;
/**
* For IPC servers makes the pipe readable for all users.
* Default to `false`.
*/
readableAll?: boolean;
/**
* For IPC servers makes the pipe writable for all users.
* Default to `false`.
*/
writableAll?: boolean;
/**
* For TCP servers, setting `ipv6Only` to `true` will disable dual-stack support, i.e., binding to host `::` won't make `0.0.0.0` be bound.
* Default to `false`.
*/
ipv6Only?: boolean;
/**
* An AbortSignal that may be used to close a listening server.
* @since This option is available only in Node.js v15.6.0 and greater
*/
signal?: AbortSignal;
}, callback: (err: Error | null, address: string) => void): void;
listen(opts?: {
/**
* Default to `0` (picks the first available open port).
*/
port?: number;
/**
* Default to `localhost`.
*/
host?: string;
/**
* Will be ignored if `port` is specified.
* @see [Identifying paths for IPC connections](https://nodejs.org/api/net.html#identifying-paths-for-ipc-connections).
*/
path?: string;
/**
* Specify the maximum length of the queue of pending connections.
* The actual length will be determined by the OS through sysctl settings such as `tcp_max_syn_backlog` and `somaxconn` on Linux.
* Default to `511`.
*/
backlog?: number;
/**
* Default to `false`.
*/
exclusive?: boolean;
/**
* For IPC servers makes the pipe readable for all users.
* Default to `false`.
*/
readableAll?: boolean;
/**
* For IPC servers makes the pipe writable for all users.
* Default to `false`.
*/
writableAll?: boolean;
/**
* For TCP servers, setting `ipv6Only` to `true` will disable dual-stack support, i.e., binding to host `::` won't make `0.0.0.0` be bound.
* Default to `false`.
*/
ipv6Only?: boolean;
/**
* An AbortSignal that may be used to close a listening server.
* @since This option is available only in Node.js v15.6.0 and greater
*/
signal?: AbortSignal;
}): Promise<string>;
listen(opts: FastifyListenOptions, callback: (err: Error | null, address: string) => void): void;
listen(opts?: FastifyListenOptions): Promise<string>;
listen(callback: (err: Error | null, address: string) => void): void;

/**
Expand Down

0 comments on commit c56d4c8

Please sign in to comment.