From 1d5e74c1fdb78856ceb5a55b5044e30971f7c341 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?A=2E=20Rom=C3=A1n?= Date: Tue, 14 Jun 2022 09:36:16 +0200 Subject: [PATCH] refactor(types): deduplicate `listen` options and export it --- types/instance.d.ts | 140 ++++++++++++++++---------------------------- 1 file changed, 49 insertions(+), 91 deletions(-) diff --git a/types/instance.d.ts b/types/instance.d.ts index 73a0ea4a9a1..02b1a04a9ff 100644 --- a/types/instance.d.ts +++ b/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' @@ -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 extends keyof _Interface ? never : Key type FindMyWayVersion = RawServer extends http.Server ? HTTPVersion.V1 : HTTPVersion.V2 @@ -117,96 +163,8 @@ export interface FastifyInstance< inject(opts: InjectOptions | string): Promise; 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; + listen(opts: FastifyListenOptions, callback: (err: Error | null, address: string) => void): void; + listen(opts?: FastifyListenOptions): Promise; listen(callback: (err: Error | null, address: string) => void): void; /**