Skip to content

Commit

Permalink
Upgrade to Crystal 0.35.0 (#570)
Browse files Browse the repository at this point in the history
* Update to ameba ~> 0.12.0

* Use Compress::Deflate and Compress::Gzip

* Drop unused require

* Fix spec due to defer request upgrade
  • Loading branch information
bcardiff authored Jun 10, 2020
1 parent 70684a2 commit a8c0f09
Showing 4 changed files with 26 additions and 13 deletions.
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ dependencies:
development_dependencies:
ameba:
github: crystal-ameba/ameba
version: ~> 0.10.0
version: ~> 0.12.0

crystal: 0.30.0

3 changes: 3 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -48,6 +48,9 @@ def create_ws_request_and_return_io_and_context(handler, request)
rescue IO::Error
# Raises because the IO::Memory is empty
end
{% if compare_versions(Crystal::VERSION, "0.35.0-0") >= 0 %}
response.upgrade_handler.try &.call(io)
{% end %}
io.rewind
{io, context}
end
30 changes: 22 additions & 8 deletions src/kemal/helpers/helpers.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require "flate"
require "gzip"
{% if compare_versions(Crystal::VERSION, "0.35.0-0") >= 0 %}
require "compress/deflate"
require "compress/gzip"
{% end %}
require "mime"

# Adds given `Kemal::Handler` to handlers chain.
@@ -142,14 +144,26 @@ def send_file(env : HTTP::Server::Context, path : String, mime_type : String? =
condition = config.is_a?(Hash) && config["gzip"]? == true && filesize > minsize && Kemal::Utils.zip_types(file_path)
if condition && request_headers.includes_word?("Accept-Encoding", "gzip")
env.response.headers["Content-Encoding"] = "gzip"
Gzip::Writer.open(env.response) do |deflate|
IO.copy(file, deflate)
end
{% if compare_versions(Crystal::VERSION, "0.35.0-0") >= 0 %}
Compress::Gzip::Writer.open(env.response) do |deflate|
IO.copy(file, deflate)
end
{% else %}
Gzip::Writer.open(env.response) do |deflate|
IO.copy(file, deflate)
end
{% end %}
elsif condition && request_headers.includes_word?("Accept-Encoding", "deflate")
env.response.headers["Content-Encoding"] = "deflate"
Flate::Writer.open(env.response) do |deflate|
IO.copy(file, deflate)
end
{% if compare_versions(Crystal::VERSION, "0.35.0-0") >= 0 %}
Compress::Deflate::Writer.open(env.response) do |deflate|
IO.copy(file, deflate)
end
{% else %}
Flate::Writer.open(env.response) do |deflate|
IO.copy(file, deflate)
end
{% end %}
else
env.response.content_length = filesize
IO.copy(file, env.response)
4 changes: 0 additions & 4 deletions src/kemal/static_file_handler.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
{% if !flag?(:without_zlib) %}
require "zlib"
{% end %}

module Kemal
class StaticFileHandler < HTTP::StaticFileHandler
# ameba:disable Metrics/CyclomaticComplexity

0 comments on commit a8c0f09

Please sign in to comment.