Skip to content

Commit fa57b53

Browse files
committed
Add parameters options to ZStandard encoder
1 parent d40e40e commit fa57b53

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

‎src/ring/middleware/content_encoding.clj‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,24 @@
2323
(defn deflate-encoder ^OutputStream [^OutputStream out]
2424
(DeflaterOutputStream. out))
2525

26-
(defn zstandard-encoder ^OutputStream [^OutputStream out]
27-
(ZstdOutputStream. out))
26+
(defn zstandard-encoder
27+
([] (zstandard-encoder {}))
28+
([{:keys [chain-log hash-log job-size level long min-match overlap-log
29+
search-log strategy target-length window-log workers]}]
30+
(fn [^OutputStream out]
31+
(-> (ZstdOutputStream. out)
32+
(cond-> level (.setLevel level))
33+
(cond-> long (.setLong long))
34+
(cond-> workers (.setWorkers workers))
35+
(cond-> overlap-log (.setOverlapLog overlap-log))
36+
(cond-> job-size (.setJobSize job-size))
37+
(cond-> target-length (.setTargetLength target-length))
38+
(cond-> min-match (.setMinMatch min-match))
39+
(cond-> search-log (.setSearchLog search-log))
40+
(cond-> chain-log (.setChainLog chain-log))
41+
(cond-> hash-log (.setHashLog hash-log))
42+
(cond-> window-log (.setWindowLog window-log))
43+
(cond-> strategy (.setStrategy strategy))))))
2844

2945
(def default-encoder-weights
3046
{"zstd" 5
@@ -44,7 +60,7 @@
4460
"deflate" deflate-encoder
4561
"gzip" gzip-encoder
4662
"identity" identity
47-
"zstd" zstandard-encoder})
63+
"zstd" (zstandard-encoder)})
4864

4965
(def default-status-codes #{200 404 403})
5066

‎test/ring/middleware/content_encoding_test.clj‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,23 @@
8484
(is (= [40 -75 47 -3 0 88 89 0 0 72 101 108 108 111 32 87 111 114 108 100]
8585
(vec (.toByteArray out))))))
8686

87+
(deftest zstd-content-encoding-params-test
88+
(let [response (ce/content-encoding-response
89+
{:status 200
90+
:headers {"Content-Type" "text/plain; charset=utf-8"}
91+
:body "aaaaaaaaabbbbbbbbcccccdddeeeaaaabbbbbbbabbab"}
92+
{:headers {"accept-encoding" "zstd"}}
93+
{:encoders {"zstd" (ce/zstandard-encoder {:level 1})}})
94+
out (ByteArrayOutputStream.)]
95+
(p/write-body-to-stream (:body response) response out)
96+
(is (= {:status 200
97+
:headers {"Content-Type" "text/plain; charset=utf-8"
98+
"Content-Encoding" "zstd"}}
99+
(dissoc response :body)))
100+
(is (= [40 -75 47 -3 0 72 -19 0 0 -104 97 97 98 99 99 99 99 99 100 100 100
101+
101 101 101 97 98 98 97 98 3 0 58 -115 -125 -52 50 -48 6]
102+
(vec (.toByteArray out))))))
103+
87104
(deftest different-content-encodings-test
88105
(let [response (ce/content-encoding-response
89106
plain-response

0 commit comments

Comments
 (0)