xsc/pandect

Unexpected side-effect of sha on stream

mattiasw2 opened this issue · 2 comments

I got an unexpected side effect by calculating the sha1 a missing etag.

I had to add a call to reset below, since otherwise the stream was consumed, and an empty pictures was sent from the email server.

Can this be improved? (Using 0.6.0)

  (let [etag (if etag etag (pandect.algo.sha1/sha1 body))]
    ;; if I do a sha on a stream, I need to reset it afterwards, since otherwise, and empty image will be sent to the client
    (if (= (type body) org.httpkit.BytesInputStream) (.reset body))
xsc commented

@mattiasw2 I don't think this can be improved on pandect's side since this is indeed expected behaviour when it comes to stream processing. Just like with slurp or clojure.java.io/copy! the input stream gets completely consumed to do the desired work.

What I would do in your case is read the body into a byte array first (e.g. with ztellman's byte-streams library [1]), then calculate the ETag and return the byte array as the new body. This would remove any special cases from your code.

Plus, you only really need to do any of this if the ETag is missing.

[1] https://github.com/ztellman/byte-streams

I agree on your solution. Thanks!