Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/web/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2155,6 +2155,12 @@ async function httpNetworkFetch (
flush: zlib.constants.BROTLI_OPERATION_FLUSH,
finishFlush: zlib.constants.BROTLI_OPERATION_FLUSH
}))
} else if (coding === 'zstd' && typeof zlib.createZstdDecompress === 'function') {
// Node.js v23.8.0+ and v22.15.0+ supports Zstandard
decoders.push(zlib.createZstdDecompress({
flush: zlib.constants.ZSTD_e_continue,
finishFlush: zlib.constants.ZSTD_e_end
}))
} else {
decoders.length = 0
break
Expand Down
37 changes: 32 additions & 5 deletions test/fetch/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ const assert = require('node:assert')
const { createServer } = require('node:http')
const { once } = require('node:events')
const { fetch } = require('../..')
const { createBrotliCompress, createGzip, createDeflate } = require('node:zlib')
const zlib = require('node:zlib')
const { closeServerAsPromise } = require('../utils/node-http')

test('content-encoding header is case-iNsENsITIve', async (t) => {
const contentCodings = 'GZiP, bR'
const text = 'Hello, World!'

const server = createServer({ joinDuplicateHeaders: true }, (req, res) => {
const gzip = createGzip()
const brotli = createBrotliCompress()
const gzip = zlib.createGzip()
const brotli = zlib.createBrotliCompress()

res.setHeader('Content-Encoding', contentCodings)
res.setHeader('Content-Type', 'text/plain')
Expand All @@ -39,8 +39,8 @@ test('response decompression according to content-encoding should be handled in
const text = 'Hello, World!'

const server = createServer({ joinDuplicateHeaders: true }, (req, res) => {
const gzip = createGzip()
const deflate = createDeflate()
const gzip = zlib.createGzip()
const deflate = zlib.createDeflate()

res.setHeader('Content-Encoding', contentCodings)
res.setHeader('Content-Type', 'text/plain')
Expand All @@ -58,3 +58,30 @@ test('response decompression according to content-encoding should be handled in

assert.strictEqual(await response.text(), text)
})

test('should decompress zstandard response',
{ skip: typeof zlib.createZstdDecompress !== 'function' },
async (t) => {
const contentCodings = 'zstd'
const text = 'Hello, World!'
const server = createServer({ joinDuplicateHeaders: true }, (req, res) => {
const zstd = zlib.createZstdCompress()

res.setHeader('Content-Encoding', contentCodings)
res.setHeader('Content-Type', 'text/plain')

zstd.pipe(res)
zstd.write(text)
zstd.end()
}
).listen(0)
t.after(closeServerAsPromise(server))

await once(server, 'listening')
const url = `http://localhost:${server.address().port}`

const response = await fetch(url)
assert.strictEqual(await response.text(), text)
assert.strictEqual(response.headers.get('content-encoding'), contentCodings)
assert.strictEqual(response.headers.get('content-type'), 'text/plain')
})
Loading