Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(types): deduplicate listen options and export it #4013

Merged
merged 1 commit into from Jul 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was moved due to VSCode's auto-import, should I leave it as-is, or move it back to the top? 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am ok with this change

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