Skip to content

Z_DATA_ERROR when fetching a "Content-Encoding: gzip, deflate" response from the server #3762

@kettanaito

Description

@kettanaito

Bug Description

fetch is terminated with a Z_DATA_ERROR when fetching a gzip, deflate compressed response from the server.

Reproducible By

Here's a 0 dependency reproduction:

  1. https://github.com/kettanaito/undici-gzip-deflate
  2. node ./index.js

Here's a copy-pastable reproduction:

import http from 'node:http'
import zlib from 'node:zlib'

const server = new http.Server((req, res) => {
  res.setHeader('content-encoding', 'gzip, deflate')
  res.end(zlib.deflateSync(zlib.gzipSync('hello world')))
})

server.listen(56789, async () => {
  const response = await fetch('http://localhost:56789/', {
    headers: { 'accept-encoding': 'gzip, deflate' },
  })
  const text = await response.text()
  console.assert(text === 'hello world')
})

Expected Behavior

  1. Fetch happens without errors.
  2. await response.text() returns a decompressed response string 'hello world'.

Logs & Screenshots

node:internal/deps/undici/undici:11190
            fetchParams.controller.controller.error(new TypeError("terminated", {
                                                    ^

TypeError: terminated
    at Fetch.onAborted (node:internal/deps/undici/undici:11190:53)
    at Fetch.emit (node:events:517:28)
    at Fetch.terminate (node:internal/deps/undici/undici:10375:14)
    at fetchParams.controller.resume (node:internal/deps/undici/undici:11167:36)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  [cause]: Error: incorrect header check
      at Zlib.zlibOnError [as onerror] (node:zlib:189:17) {
    errno: -3,
    code: 'Z_DATA_ERROR'
  }
}

Node.js v18.20.3

Environment

Reproducible on all versions of Node:

  • 18.20.3
  • 20.11.0
  • 22.3.0
  • 23.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions