From 4426c5a5673970ea343c77cc6fbb6eedd3bb5325 Mon Sep 17 00:00:00 2001 From: Gil Pedersen Date: Mon, 18 Mar 2024 11:47:39 +0100 Subject: [PATCH] Make content encoder and options explicitly typed and extensible --- lib/types/route.d.ts | 17 +++-------------- lib/types/server/encoders.d.ts | 19 +++++++++++++++++++ lib/types/server/index.d.ts | 1 + lib/types/server/server.d.ts | 11 ++++++----- 4 files changed, 29 insertions(+), 19 deletions(-) create mode 100644 lib/types/server/encoders.d.ts diff --git a/lib/types/route.d.ts b/lib/types/route.d.ts index 87a62f293..afbd10128 100644 --- a/lib/types/route.d.ts +++ b/lib/types/route.d.ts @@ -3,7 +3,7 @@ import { ObjectSchema, ValidationOptions, SchemaMap, Schema } from 'joi'; import { PluginSpecificConfiguration} from './plugin'; import { MergeType, ReqRef, ReqRefDefaults, MergeRefs, AuthMode } from './request'; -import { RouteRequestExtType, RouteExtObject, Server } from './server'; +import { ContentDecoders, ContentEncoders, RouteRequestExtType, RouteExtObject, Server } from './server'; import { Lifecycle, Json, HTTP_METHODS_PARTIAL } from './utils'; /** @@ -208,11 +208,6 @@ export interface RouteOptionsCors { */ export type PayloadOutput = 'data' | 'stream' | 'file'; -/** - * [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-routeoptionspayloadcompression) - */ -export type PayloadCompressionDecoderSettings = object; - /** * Determines how the request payload is processed. * [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-routeoptionspayload) @@ -237,7 +232,7 @@ export interface RouteOptionsPayload { * An object where each key is a content-encoding name and each value is an object with the desired decoder settings. Note that encoder settings are set in compression. * [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-routeoptionspayloadcompression) */ - compression?: Record | undefined; + compression?: { [P in keyof ContentDecoders]?: Parameters[0] } | undefined; /** * @default 'application/json'. @@ -643,12 +638,6 @@ export interface RouteOptionsValidate { state?: RouteOptionsResponseSchema | undefined; } -/** - * For context [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-routeoptionscompression) - * For context [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-serverencoderencoding-encoder) - */ -export type RouteCompressionEncoderSettings = object; - export interface CommonRouteProperties { /** * Application-specific route configuration state. Should not be used by plugins which should use options.plugins[name] instead. @@ -683,7 +672,7 @@ export interface CommonRouteProperties { * An object where each key is a content-encoding name and each value is an object with the desired encoder settings. Note that decoder settings are set in compression. * [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-routeoptionscompression) */ - compression?: Record | undefined; + compression?: { [P in keyof ContentEncoders]?: Parameters[0] } | undefined; /** * @default false (no CORS headers). diff --git a/lib/types/server/encoders.d.ts b/lib/types/server/encoders.d.ts new file mode 100644 index 000000000..c91fd7df3 --- /dev/null +++ b/lib/types/server/encoders.d.ts @@ -0,0 +1,19 @@ +import { createDeflate, createGunzip, createGzip, createInflate } from 'zlib'; + +/** + * Available [content encoders](https://github.com/hapijs/hapi/blob/master/API.md#-serverencoderencoding-encoder). + */ +export interface ContentEncoders { + + deflate: typeof createDeflate; + gzip: typeof createGzip; +} + +/** + * Available [content decoders](https://github.com/hapijs/hapi/blob/master/API.md#-serverdecoderencoding-decoder). + */ +export interface ContentDecoders { + + deflate: typeof createInflate; + gzip: typeof createGunzip; +} diff --git a/lib/types/server/index.d.ts b/lib/types/server/index.d.ts index b4b17a394..f1ff8aea4 100644 --- a/lib/types/server/index.d.ts +++ b/lib/types/server/index.d.ts @@ -1,5 +1,6 @@ export * from './auth'; export * from './cache'; +export * from './encoders'; export * from './events'; export * from './ext'; export * from './info'; diff --git a/lib/types/server/server.d.ts b/lib/types/server/server.d.ts index 1c410c49f..e53a7a1eb 100644 --- a/lib/types/server/server.d.ts +++ b/lib/types/server/server.d.ts @@ -1,5 +1,5 @@ import * as http from 'http'; -import * as zlib from 'zlib'; +import { Stream } from 'stream'; import { Root } from 'joi'; import { Mimos } from '@hapi/mimos'; @@ -25,8 +25,6 @@ import { } from '../request'; import { ResponseToolkit } from '../response'; import { - PayloadCompressionDecoderSettings, - RouteCompressionEncoderSettings, RulesOptions, RulesProcessor, ServerRoute @@ -34,6 +32,7 @@ import { import { HTTP_METHODS, Lifecycle } from '../utils'; import { ServerAuth } from './auth'; import { ServerCache } from './cache'; +import { ContentDecoders, ContentEncoders } from './encoders'; import { ServerEventsApplication, ServerEvents } from './events'; import { ServerExtEventsObject, @@ -291,7 +290,8 @@ export class Server { * @return Return value: none. * [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-serverdecoderencoding-decoder) */ - decoder(encoding: string, decoder: ((options: PayloadCompressionDecoderSettings) => zlib.Gunzip)): void; + decoder(encoding: T, decoder: ContentDecoders[T]): void; + decoder(encoding: string, decoder: ((options?: object) => Stream)): void; /** * Extends various framework interfaces with custom methods where: @@ -340,7 +340,8 @@ export class Server { * @return Return value: none. * [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-serverencoderencoding-encoder) */ - encoder(encoding: string, encoder: ((options: RouteCompressionEncoderSettings) => zlib.Gzip)): void; + encoder(encoding: T, encoder: ContentEncoders[T]): void; + encoder(encoding: string, encoder: ((options?: object) => Stream)): void; /** * Used within a plugin to expose a property via server.plugins[name] where: