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

Function compression

packages/effect/src/unstable/http/HttpMiddleware.ts:452–541  ·  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

450 * @since 4.0.0
451 */
452export const compression = (
453 options?: {
454 /**
455 * Server preference order. Negotiation picks the first accepted algorithm
456 * supported by the platform. Defaults to `["br", "gzip", "deflate"]`;
457 * `zstd` must be explicitly opted into.
458 */
459 readonly algorithms?: ReadonlyArray<CompressionAlgorithm> | undefined
460 /**
461 * Minimum body size in bytes when the length is known. Unknown-length bodies
462 * are always compressed. Defaults to `1024`.
463 */
464 readonly minSize?: number | undefined
465 /** Replaces the default content-type predicate. */
466 readonly compressible?: ((contentType: string) => boolean) | undefined
467 /** Per-algorithm levels. Platforms without a level knob ignore them. */
468 readonly levels?: {
469 readonly gzip?: number | undefined
470 readonly deflate?: number | undefined
471 readonly br?: number | undefined
472 readonly zstd?: number | undefined
473 } | undefined
474 } | undefined
475): <E, R>(
476 httpApp: Effect.Effect<HttpServerResponse, E, R>
477) => Effect.Effect<HttpServerResponse, E, R | HttpServerRequest | HttpPlatform> => {
478 const preferred = options?.algorithms ?? defaultAlgorithms
479 const minSize = options?.minSize ?? 1024
480 const compressible = options?.compressible ?? compressionInternal.defaultCompressible
481 const levels = { ...defaultLevels, ...options?.levels }
482 const levelOptions: Record<CompressionAlgorithm, { readonly level: number | undefined }> = {
483 gzip: { level: levels.gzip },
484 deflate: { level: levels.deflate },
485 br: { level: levels.br },
486 zstd: { level: levels.zstd }
487 }
488 const transform = (
489 compression: HttpPlatform["Service"]["compression"],
490 acceptEncoding: string | undefined,
491 response: HttpServerResponse
492 ): Effect.Effect<HttpServerResponse> => {
493 if (
494 response.status < 200 ||
495 response.status === 204 ||
496 response.status === 206 ||
497 response.status === 304
498 ) {
499 return Effect.succeed(response)
500 }
501 const currentEncoding = response.headers["content-encoding"]
502 if (currentEncoding !== undefined) {
503 return Effect.succeed(
504 currentEncoding.trim().toLowerCase() === "identity"
505 ? Response.removeHeader(response, "content-encoding")
506 : response
507 )
508 }
509 const body = response.body

Callers

nothing calls this directly

Calls 3

getUnsafeMethod · 0.80
transformFunction · 0.70

Tested by

no test coverage detected