-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move to
compress
and decompress
route options and typescrip…
…t improvements (#199) * test (types): move `index.test-d.ts` file to `test/types` folder * feat!: add `compress` and `decompress` route shorthand configuration properties * test: update routes related tests * types: update typescript types * test (types): actually test typescript types * docs: update documentation * chore: add new npm scripts * test: make sure the old `(de)compress` route option won't break * fix: make sure the old `(de)compress` route option won't break * fix: typo * types: add `package.json` types entry * refactor: use `request` instead of `req` * fix (types): mark `config.(de)compress` use as deprecated
- Loading branch information
Showing
8 changed files
with
437 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,79 @@ | ||
import { FastifyPlugin, FastifyReply, FastifyRequest } from 'fastify'; | ||
import { Input, InputObject } from 'into-stream'; | ||
import { Stream } from 'stream'; | ||
import { BrotliOptions, ZlibOptions } from 'zlib'; | ||
import { | ||
FastifyPluginCallback, | ||
FastifyReply, | ||
FastifyRequest, | ||
RawServerBase, | ||
RawServerDefault | ||
} from 'fastify' | ||
import * as fastify from 'fastify' | ||
import { Input, InputObject } from 'into-stream' | ||
import { Stream } from 'stream' | ||
import { BrotliOptions, ZlibOptions } from 'zlib' | ||
|
||
type EncodingToken = 'br' | 'deflate' | 'gzip' | 'identity'; | ||
|
||
export interface FastifyCompressOptions { | ||
brotliOptions?: BrotliOptions; | ||
customTypes?: RegExp; | ||
encodings?: EncodingToken[]; | ||
forceRequestEncoding?: EncodingToken; | ||
global?: boolean; | ||
inflateIfDeflated?: boolean; | ||
onInvalidRequestPayload?: (encoding: string, request: FastifyRequest, error: Error) => Error | undefined | null; | ||
onUnsupportedEncoding?: (encoding: string, request: FastifyRequest, reply: FastifyReply) => string | Buffer | Stream; | ||
onUnsupportedRequestEncoding?: (encoding: string, request: FastifyRequest, reply: FastifyReply) => Error | undefined | null; | ||
requestEncodings?: EncodingToken[]; | ||
threshold?: number; | ||
zlib?: unknown; | ||
zlibOptions?: ZlibOptions; | ||
} | ||
|
||
interface RouteCompressOptions extends Pick<FastifyCompressOptions, | ||
| 'brotliOptions' | ||
| 'customTypes' | ||
| 'encodings' | ||
| 'inflateIfDeflated' | ||
| 'onUnsupportedEncoding' | ||
| 'threshold' | ||
| 'zlib' | ||
| 'zlibOptions' | ||
> {} | ||
|
||
interface RouteDecompressOptions extends Pick<FastifyCompressOptions, | ||
| 'forceRequestEncoding' | ||
| 'onInvalidRequestPayload' | ||
| 'onUnsupportedRequestEncoding' | ||
| 'requestEncodings' | ||
| 'zlib' | ||
> {} | ||
|
||
interface FastifyCompressRouteOptions { | ||
compress?: RouteCompressOptions | false; | ||
decompress?: RouteDecompressOptions | false; | ||
} | ||
|
||
export interface RouteOptions extends fastify.RouteOptions, FastifyCompressRouteOptions {} | ||
|
||
export interface RoutesConfigCompressOptions { | ||
/** @deprecated `config.compress` is deprecated, use `compress` shorthand option instead */ | ||
compress?: RouteCompressOptions | false; | ||
/** @deprecated `config.decompress` is deprecated, use `decompress` shorthand option instead */ | ||
decompress?: RouteDecompressOptions | false; | ||
} | ||
|
||
declare module 'fastify' { | ||
export interface FastifyContextConfig extends RoutesConfigCompressOptions {} | ||
|
||
export interface RouteShorthandOptions< | ||
RawServer extends RawServerBase = RawServerDefault | ||
> extends FastifyCompressRouteOptions {} | ||
|
||
declare module "fastify" { | ||
interface FastifyReply { | ||
compress(input: Stream | Input | InputObject): void; | ||
} | ||
} | ||
|
||
type EncodingToken = 'br' | 'deflate' | 'gzip' | 'identity' | ||
|
||
export interface FastifyCompressOptions { | ||
global?: boolean | ||
threshold?: number | ||
customTypes?: RegExp | ||
zlib?: NodeModule | ||
brotliOptions?: BrotliOptions | ||
zlibOptions?: ZlibOptions | ||
inflateIfDeflated?: boolean | ||
onUnsupportedEncoding?: (encoding: string, request: FastifyRequest, reply: FastifyReply) => string | Buffer | Stream | ||
encodings?: Array<EncodingToken> | ||
requestEncodings?: Array<EncodingToken> | ||
forceRequestEncoding?: EncodingToken | ||
onUnsupportedRequestEncoding?: (encoding: string, request: FastifyRequest, reply: FastifyReply) => Error | undefined | null | ||
onInvalidRequestPayload?: (encoding: string, request: FastifyRequest, error: Error) => Error | undefined | null | ||
export interface RouteOptions extends FastifyCompressRouteOptions {} | ||
} | ||
|
||
declare const fastifyCompress: FastifyPlugin<FastifyCompressOptions> | ||
export default fastifyCompress; | ||
declare const fastifyCompress: FastifyPluginCallback<FastifyCompressOptions> | ||
export default fastifyCompress |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.