Officially allow binding config via calls to freeze, thaw, etc.

This commit is contained in:
Peter Taoussanis 2020-07-25 10:19:00 +02:00
parent 25706d09d5
commit 8f71638a19
2 changed files with 50 additions and 41 deletions

View file

@ -1151,55 +1151,60 @@
(comment (wrap-header (.getBytes "foo") {:compressor-id :lz4 (comment (wrap-header (.getBytes "foo") {:compressor-id :lz4
:encryptor-id nil})) :encryptor-id nil}))
(defn fast-freeze
"Like `freeze` but:
- Writes data without a Nippy header
- Drops all support for compression and encryption
- Must be thawed with `fast-thaw`
Equivalent to (but a little faster than):
`(freeze x {:compressor nil :encryptor nil :no-header? true})"
[x]
(let [baos (ByteArrayOutputStream. 64)
dos (DataOutputStream. baos)]
(with-cache (-freeze-with-meta! x dos))
(.toByteArray baos)))
(defn- call-with-bindings (defn- call-with-bindings
"Allow opts to override config bindings. Undocumented." "Allow opts to override config bindings."
[opts f] [opts f]
(let [opt->bindings (if (empty? opts)
(fn [bindings id var] (f)
(if-let [o (find opts id)] (let [opt->bindings
(assoc bindings var (val o)) (fn [bindings id var]
(do bindings))) (let [v (get opts id :default)]
(if (identical? v :default)
(do bindings)
(assoc bindings var v))))
bindings bindings
(-> nil (-> nil
(opt->bindings :freeze-fallback #'*freeze-fallback*) (opt->bindings :freeze-fallback #'*freeze-fallback*)
(opt->bindings :auto-freeze-compressor #'*auto-freeze-compressor*) (opt->bindings :auto-freeze-compressor #'*auto-freeze-compressor*)
(opt->bindings :serializable-whitelist #'*serializable-whitelist*) (opt->bindings :serializable-whitelist #'*serializable-whitelist*)
(opt->bindings :custom-readers #'*custom-readers*))] (opt->bindings :custom-readers #'*custom-readers*))]
(if-not bindings (if-not bindings
(f) ; Common case (f) ; Common case
(try (try
(push-thread-bindings bindings) (push-thread-bindings bindings)
(f) (f)
(finally (finally
(pop-thread-bindings)))))) (pop-thread-bindings)))))))
(comment (comment
(enc/qb 1e4 (enc/qb 1e4
(call-with-bindings {} (fn [] *freeze-fallback*)) (call-with-bindings {} (fn [] *freeze-fallback*))
(call-with-bindings {:freeze-fallback "foo"} (fn [] *freeze-fallback*)))) (call-with-bindings {:freeze-fallback "foo"} (fn [] *freeze-fallback*))))
(defn fast-freeze
"Like `freeze` but:
- Writes data without a Nippy header
- Drops all support for compression and encryption
- Must be thawed with `fast-thaw`
Equivalent to (but a little faster than) `freeze` with opts:
- :compressor nil
- :encryptor nil
- :no-header? true"
[x]
(let [baos (ByteArrayOutputStream. 64)
dos (DataOutputStream. baos)]
(with-cache (-freeze-with-meta! x dos))
(.toByteArray baos)))
(defn freeze (defn freeze
"Serializes arg (any Clojure data type) to a byte array. To freeze custom "Serializes arg (any Clojure data type) to a byte array. To freeze custom
types, extend the Clojure reader or see `extend-freeze`." types, extend the Clojure reader or see `extend-freeze`."
([x] (freeze x nil)) ([x] (freeze x nil))
([x {:as opts ([x {:as opts
:keys [compressor encryptor password] :keys [compressor encryptor password serializable-whitelist]
:or {compressor :auto :or {compressor :auto
encryptor aes128-gcm-encryptor}}] encryptor aes128-gcm-encryptor}}]
@ -1625,8 +1630,11 @@
- Drops all support for compression and encryption - Drops all support for compression and encryption
- Supports only data frozen with `fast-freeze` - Supports only data frozen with `fast-freeze`
Equivalent to (but a little faster than): Equivalent to (but a little faster than) `thaw` with opts:
`(thaw x {:compressor nil :encryptor nil :no-header? true})" - :compressor nil
- :encryptor nil
- :no-header? true"
[^bytes ba] [^bytes ba]
(let [dis (DataInputStream. (ByteArrayInputStream. ba))] (let [dis (DataInputStream. (ByteArrayInputStream. ba))]
(with-cache (thaw-from-in! dis)))) (with-cache (thaw-from-in! dis))))
@ -1647,7 +1655,8 @@
([ba] (thaw ba nil)) ([ba] (thaw ba nil))
([^bytes ba ([^bytes ba
{:as opts {:as opts
:keys [v1-compatibility? compressor encryptor password] :keys [v1-compatibility? compressor encryptor password
serializable-whitelist]
:or {compressor :auto :or {compressor :auto
encryptor :auto}}] encryptor :auto}}]

View file

@ -278,10 +278,10 @@
(is (is
(= :quarantined (= :quarantined
(get-in (get-in
(binding [nippy/*serializable-whitelist* #{}] (nippy/thaw
(nippy/thaw (nippy/freeze (java.util.concurrent.Semaphore. 1)
(binding [nippy/*serializable-whitelist* "*"] {:serializable-whitelist "*"})
(nippy/freeze (java.util.concurrent.Semaphore. 1))))) {:serializable-whitelist #{}})
[:nippy/unthawable :cause])) [:nippy/unthawable :cause]))
"Thaw will quarantine Serializable objects approved when freezing.") "Thaw will quarantine Serializable objects approved when freezing.")