MCPcopy Create free account
hub / github.com/Effect-TS/effect / compression

Function compression

packages/effect/src/http/HttpMiddleware.ts:900–988  ·  view source on GitHub ↗
(
  options?: {
    /**
     * Server preference order. Negotiation picks the first accepted algorithm
     * supported by the platform. Defaults to `["br", "gzip", "deflate"]`;
     * `zstd` must be explicitly opted into.
     */
    readonly algorithms?: ReadonlyArray<CompressionAlgorithm> | undefined
    /**
     * Minimum body size in bytes when the length is known. Unknown-length bodies
     * are always compressed. Defaults to `1024`.
     */
    readonly minSize?: number | undefined
    /** Replaces the default content-type predicate. */
    readonly compressible?: ((contentType: string) => boolean) | undefined
    /** Per-algorithm levels. Platforms without a level knob ignore them. */
    readonly levels?: {
      readonly gzip?: number | undefined
      readonly deflate?: number | undefined
      readonly br?: number | undefined
      readonly zstd?: number | undefined
    } | undefined
  } | undefined
)

Source from the content-addressed store, hash-verified

898 * @since 4.0.0
899 */
900export const compression = (
901 options?: {
902 /**
903 * Server preference order. Negotiation picks the first accepted algorithm
904 * supported by the platform. Defaults to `["br", "gzip", "deflate"]`;
905 * `zstd` must be explicitly opted into.
906 */
907 readonly algorithms?: ReadonlyArray<CompressionAlgorithm> | undefined
908 /**
909 * Minimum body size in bytes when the length is known. Unknown-length bodies
910 * are always compressed. Defaults to `1024`.
911 */
912 readonly minSize?: number | undefined
913 /** Replaces the default content-type predicate. */
914 readonly compressible?: ((contentType: string) => boolean) | undefined
915 /** Per-algorithm levels. Platforms without a level knob ignore them. */
916 readonly levels?: {
917 readonly gzip?: number | undefined
918 readonly deflate?: number | undefined
919 readonly br?: number | undefined
920 readonly zstd?: number | undefined
921 } | undefined
922 } | undefined
923): <E, R>(
924 httpApp: Effect.Effect<HttpServerResponse, E, R>
925) => Effect.Effect<HttpServerResponse, E, R | HttpServerRequest | HttpPlatform> => {
926 const preferred = options?.algorithms ?? defaultAlgorithms
927 const minSize = options?.minSize ?? 1024
928 const compressible = options?.compressible ?? compressionInternal.defaultCompressible
929 const levels = { ...defaultLevels, ...options?.levels }
930 const levelOptions: Record<CompressionAlgorithm, { readonly level: number | undefined }> = {
931 gzip: { level: levels.gzip },
932 deflate: { level: levels.deflate },
933 br: { level: levels.br },
934 zstd: { level: levels.zstd }
935 }
936 const transform = (
937 compression: HttpPlatform["Service"]["compression"],
938 acceptEncoding: string | undefined,
939 response: HttpServerResponse
940 ): Effect.Effect<HttpServerResponse> => {
941 if (
942 response.status < 200 ||
943 response.status === 204 ||
944 response.status === 206 ||
945 response.status === 304
946 ) {
947 return Effect.succeed(response)
948 }
949 const currentEncoding = response.headers["content-encoding"]
950 if (currentEncoding !== undefined) {
951 return Effect.succeed(
952 currentEncoding.trim().toLowerCase() === "identity"
953 ? Response.removeHeader(response, "content-encoding")
954 : response
955 )
956 }
957 const body = response.body

Callers

nothing calls this directly

Calls 3

transformFunction · 0.70
getUnsafeMethod · 0.45

Tested by

no test coverage detected