Skip to content

Commit 8042daf

Browse files
committed
Use Node's new built-in zstd support for HTTP body where possible
1 parent 2124855 commit 8042daf

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77

88
strategy:
99
matrix:
10-
node-version: [18.x, 20.x, v20.11.1, '*']
10+
node-version: [18.x, 20.x, 22.x, 24.x, '*']
1111

1212
steps:
1313
- uses: actions/checkout@v2

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# http-encoding [![Build Status](https://github.com/httptoolkit/http-encoding/workflows/CI/badge.svg)](https://github.com/httptoolkit/http-encoding/actions) [![Available on NPM](https://img.shields.io/npm/v/http-encoding.svg)](https://npmjs.com/package/http-encoding)
22

3-
> _Part of [HTTP Toolkit](https://httptoolkit.tech): powerful tools for building, testing & debugging HTTP(S)_
3+
> _Part of [HTTP Toolkit](https://httptoolkit.com): powerful tools for building, testing & debugging HTTP(S)_
44
55
**Everything you need to handle HTTP message body content-encoding**
66

7-
This package includes methods to decode & encode all commonly used HTTP content encodings, in a consistent format, usable in both Node.js and browsers.
7+
This package includes methods to decode & encode all commonly used HTTP content encodings, in a consistent format, usable in a wide range of Node.js versions and browsers.
88

99
The supported codecs are:
1010

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"gzip",
3939
"deflate"
4040
],
41-
"author": "Tim Perry <tim@httptoolkit.tech>",
41+
"author": "Tim Perry <tim@httptoolkit.com>",
4242
"license": "Apache-2.0",
4343
"bugs": {
4444
"url": "https://github.com/httptoolkit/http-encoding/issues"
@@ -56,7 +56,7 @@
5656
"@types/chai": "^4.2.18",
5757
"@types/chai-as-promised": "^7.1.4",
5858
"@types/mocha": "^8.2.2",
59-
"@types/node": "^15.3.0",
59+
"@types/node": "^22.15.16",
6060
"@types/pify": "^5.0.1",
6161
"assert": "^2.0.0",
6262
"browserify-zlib": "^0.2.0",
@@ -76,7 +76,7 @@
7676
"stream-browserify": "^3.0.0",
7777
"ts-loader": "^9.3.1",
7878
"ts-node": "^10.9.1",
79-
"typescript": "4.7.4",
79+
"typescript": "^5.8.3",
8080
"util": "^0.12.3",
8181
"webpack": "^5.37.0",
8282
"zlib-browserify": "0.0.3"

src/index.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,39 @@ export const brotliDecompress = zlib.brotliDecompress
4949
return decompress(buffer);
5050
});
5151

52-
// Zstd is a non-built-in wasm implementation that initializes async. We handle this by
53-
// loading it when the first zstd buffer is decompressed. That lets us defer loading
52+
// Browser Zstd is a non-built-in wasm implementation that initializes async. We handle this
53+
// by loading it when the first zstd buffer is decompressed. That lets us defer loading
5454
// until that point too, which is good since it's large-ish & rarely used.
5555
let zstd: Promise<ZstdStreaming> | undefined;
5656
const getZstd = async () => {
57-
if (!zstd) {
57+
// In Node 22.15 / 23.8+, we can use zstd built-in:
58+
if (zlib.zstdCompress && zlib.zstdDecompress) {
59+
return {
60+
compress: (buffer: Uint8Array, level?: number) => {
61+
return new Promise<Uint8Array>((resolve, reject) => {
62+
const options = level !== undefined
63+
? { [zlib.constants.ZSTD_c_compressionLevel]: level }
64+
: {};
65+
66+
zlib.zstdCompress(buffer, options, (err, result) => {
67+
if (err) reject(err);
68+
else resolve(result);
69+
});
70+
});
71+
},
72+
decompress: (buffer: Uint8Array) => {
73+
return new Promise<Uint8Array>((resolve, reject) => {
74+
zlib.zstdDecompress(buffer, (err, result) => {
75+
if (err) reject(err);
76+
else resolve(result);
77+
});
78+
});
79+
}
80+
};
81+
}
82+
83+
// In older Node and browsers, we fall back to zstd-codec:
84+
else if (!zstd) {
5885
zstd = new Promise(async (resolve) => {
5986
const { ZstdCodec } = await import('zstd-codec');
6087
ZstdCodec.run((binding) => {

0 commit comments

Comments
 (0)