Skip to content

Commit

Permalink
Merge branch 'main' into fix-css-sourcemap-default-charset
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfranca authored Sep 5, 2023
2 parents ed0404e + 42f7d5e commit 572235a
Show file tree
Hide file tree
Showing 27 changed files with 60 additions and 48 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ Get upgrade notes from Sprockets 3.x to 4.x at https://github.com/rails/sprocket

- Fix for precompile issues when multiple extensions map to the same MIME type (eg. `.jpeg` / `.jpg`). [#781](https://github.com/rails/sprockets/pull/781)
- Fix `application/css-sourcemap+json` charset [#764](https://github.com/rails/sprockets/pull/764)
- Fix compatibility with Rack 2 applications. [#790](https://github.com/rails/sprockets/pull/790)

## 4.2.0

- Rack 3 compatibility. [#758](https://github.com/rails/sprockets/pull/758)
- Fix thread safety of `Sprockets::CachedEnvironment` and `Sprockets::Cache::MemoryStore`. [#771](https://github.com/rails/sprockets/pull/771)
- Add support for Rack 3.0. Headers set by sprockets will now be lower case. [#758](https://github.com/rails/sprockets/pull/758)
- Make `Sprockets::Utils.module_include` thread safe on JRuby. [#759](https://github.com/rails/sprockets/pull/759)
- Fix typo in `asset.rb` file. [#768](https://github.com/rails/sprockets/pull/768)

## 4.1.1

Expand Down
2 changes: 1 addition & 1 deletion lib/sprockets/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def hexdigest
DigestUtils.pack_hexdigest(digest)
end

# Pubic: ETag String of Asset.
# Public: ETag String of Asset.
def etag
version = environment_version

Expand Down
48 changes: 29 additions & 19 deletions lib/sprockets/server.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
require 'set'
require 'time'
require 'rack/utils'
require 'rack'

module Sprockets
# `Server` is a concern mixed into `Environment` and
Expand All @@ -11,6 +11,16 @@ module Server
# Supported HTTP request methods.
ALLOWED_REQUEST_METHODS = ['GET', 'HEAD'].to_set.freeze

# :stopdoc:
if Gem::Version.new(Rack::RELEASE) < Gem::Version.new("3")
X_CASCADE = "X-Cascade"
VARY = "Vary"
else
X_CASCADE = "x-cascade"
VARY = "vary"
end
# :startdoc:

# `call` implements the Rack 1.x specification which accepts an
# `env` Hash and returns a three item tuple with the status code,
# headers, and body.
Expand Down Expand Up @@ -148,39 +158,39 @@ def not_modified_response(env, etag)
# Returns a 400 Forbidden response tuple
def bad_request_response(env)
if head_request?(env)
[ 400, { "content-type" => "text/plain", "content-length" => "0" }, [] ]
[ 400, { Rack::CONTENT_TYPE => "text/plain", Rack::CONTENT_LENGTH => "0" }, [] ]
else
[ 400, { "content-type" => "text/plain", "content-length" => "11" }, [ "Bad Request" ] ]
[ 400, { Rack::CONTENT_TYPE => "text/plain", Rack::CONTENT_LENGTH => "11" }, [ "Bad Request" ] ]
end
end

# Returns a 403 Forbidden response tuple
def forbidden_response(env)
if head_request?(env)
[ 403, { "content-type" => "text/plain", "content-length" => "0" }, [] ]
[ 403, { Rack::CONTENT_TYPE => "text/plain", Rack::CONTENT_LENGTH => "0" }, [] ]
else
[ 403, { "content-type" => "text/plain", "content-length" => "9" }, [ "Forbidden" ] ]
[ 403, { Rack::CONTENT_TYPE => "text/plain", Rack::CONTENT_LENGTH => "9" }, [ "Forbidden" ] ]
end
end

# Returns a 404 Not Found response tuple
def not_found_response(env)
if head_request?(env)
[ 404, { "content-type" => "text/plain", "content-length" => "0", "x-cascade" => "pass" }, [] ]
[ 404, { Rack::CONTENT_TYPE => "text/plain", Rack::CONTENT_LENGTH => "0", X_CASCADE => "pass" }, [] ]
else
[ 404, { "content-type" => "text/plain", "content-length" => "9", "x-cascade" => "pass" }, [ "Not found" ] ]
[ 404, { Rack::CONTENT_TYPE => "text/plain", Rack::CONTENT_LENGTH => "9", X_CASCADE => "pass" }, [ "Not found" ] ]
end
end

def method_not_allowed_response
[ 405, { "content-type" => "text/plain", "content-length" => "18" }, [ "Method Not Allowed" ] ]
[ 405, { Rack::CONTENT_TYPE => "text/plain", Rack::CONTENT_LENGTH => "18" }, [ "Method Not Allowed" ] ]
end

def precondition_failed_response(env)
if head_request?(env)
[ 412, { "content-type" => "text/plain", "content-length" => "0", "x-cascade" => "pass" }, [] ]
[ 412, { Rack::CONTENT_TYPE => "text/plain", Rack::CONTENT_LENGTH => "0", X_CASCADE => "pass" }, [] ]
else
[ 412, { "content-type" => "text/plain", "content-length" => "19", "x-cascade" => "pass" }, [ "Precondition Failed" ] ]
[ 412, { Rack::CONTENT_TYPE => "text/plain", Rack::CONTENT_LENGTH => "19", X_CASCADE => "pass" }, [ "Precondition Failed" ] ]
end
end

Expand All @@ -189,7 +199,7 @@ def precondition_failed_response(env)
def javascript_exception_response(exception)
err = "#{exception.class.name}: #{exception.message}\n (in #{exception.backtrace[0]})"
body = "throw Error(#{err.inspect})"
[ 200, { "content-type" => "application/javascript", "content-length" => body.bytesize.to_s }, [ body ] ]
[ 200, { Rack::CONTENT_TYPE => "application/javascript", Rack::CONTENT_LENGTH => body.bytesize.to_s }, [ body ] ]
end

# Returns a CSS response that hides all elements on the page and
Expand Down Expand Up @@ -242,7 +252,7 @@ def css_exception_response(exception)
}
CSS

[ 200, { "content-type" => "text/css; charset=utf-8", "content-length" => body.bytesize.to_s }, [ body ] ]
[ 200, { Rack::CONTENT_TYPE => "text/css; charset=utf-8", Rack::CONTENT_LENGTH => body.bytesize.to_s }, [ body ] ]
end

# Escape special characters for use inside a CSS content("...") string
Expand All @@ -258,18 +268,18 @@ def cache_headers(env, etag)
headers = {}

# Set caching headers
headers["cache-control"] = +"public"
headers["etag"] = %("#{etag}")
headers[Rack::CACHE_CONTROL] = +"public"
headers[Rack::ETAG] = %("#{etag}")

# If the request url contains a fingerprint, set a long
# expires on the response
if path_fingerprint(env["PATH_INFO"])
headers["cache-control"] << ", max-age=31536000, immutable"
headers[Rack::CACHE_CONTROL] << ", max-age=31536000, immutable"

# Otherwise set `must-revalidate` since the asset could be modified.
else
headers["cache-control"] << ", must-revalidate"
headers["vary"] = "Accept-Encoding"
headers[Rack::CACHE_CONTROL] << ", must-revalidate"
headers[VARY] = "Accept-Encoding"
end

headers
Expand All @@ -279,15 +289,15 @@ def headers(env, asset, length)
headers = {}

# Set content length header
headers["content-length"] = length.to_s
headers[Rack::CONTENT_LENGTH] = length.to_s

# Set content type header
if type = asset.content_type
# Set charset param for text/* mime types
if type.start_with?("text/") && asset.charset
type += "; charset=#{asset.charset}"
end
headers["content-type"] = type
headers[Rack::CONTENT_TYPE] = type
end

headers.merge(cache_headers(env, asset.etag))
Expand Down
2 changes: 1 addition & 1 deletion test/sprockets_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def test(name, &block)
end
end

class Sprockets::TestCase < MiniTest::Test
class Sprockets::TestCase < Minitest::Test
extend Sprockets::TestDefinition

FIXTURE_ROOT = File.join(__dir__, "fixtures")
Expand Down
2 changes: 1 addition & 1 deletion test/test_babel_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require 'sprockets/cache'
require 'sprockets/babel_processor'

class TestBabelProcessor < MiniTest::Test
class TestBabelProcessor < Minitest::Test
def setup
@env = Sprockets::Environment.new
@env.append_path File.expand_path("../fixtures", __FILE__)
Expand Down
10 changes: 5 additions & 5 deletions test/test_cache_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def test_clear
end
end

class TestNullStore < MiniTest::Test
class TestNullStore < Minitest::Test
def setup
@_store = Sprockets::Cache::NullStore.new
@store = Sprockets::Cache.new(Sprockets::Cache::NullStore.new)
Expand All @@ -99,7 +99,7 @@ def test_inspect
include CacheStoreNullTests
end

class TestMemoryStore < MiniTest::Test
class TestMemoryStore < Minitest::Test
def setup
@_store = Sprockets::Cache::MemoryStore.new
@store = Sprockets::Cache.new(@_store)
Expand Down Expand Up @@ -132,7 +132,7 @@ def test_set_with_lru
end
end

class TestZeroMemoryStore < MiniTest::Test
class TestZeroMemoryStore < Minitest::Test
def setup
@_store = Sprockets::Cache::MemoryStore.new(0)
@store = Sprockets::Cache.new(@_store)
Expand All @@ -145,7 +145,7 @@ def test_inspect
include CacheStoreNullTests
end

class TestFileStore < MiniTest::Test
class TestFileStore < Minitest::Test
def setup
@root = Dir::mktmpdir "sprockets-file-store"
@_store = Sprockets::Cache::FileStore.new(@root)
Expand Down Expand Up @@ -180,7 +180,7 @@ def test_clear_store_dir_not_exist
include CacheStoreTests
end

class TestZeroFileStore < MiniTest::Test
class TestZeroFileStore < Minitest::Test
def setup
@tmpdir = Dir::mktmpdir "sprockets-file-store-zero"
@_store = Sprockets::Cache::FileStore.new(@tmpdir, 0)
Expand Down
2 changes: 1 addition & 1 deletion test/test_closure_compressor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'sprockets/cache'
require 'sprockets/closure_compressor'

class TestClosureCompressor < MiniTest::Test
class TestClosureCompressor < Minitest::Test
def test_compress_javascript
input = {
data: "function foo() {\n return true;\n}",
Expand Down
2 changes: 1 addition & 1 deletion test/test_coffee_script_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require 'sprockets/coffee_script_processor'
require 'sprockets/source_map_utils'

class TestCoffeeScriptProcessor < MiniTest::Test
class TestCoffeeScriptProcessor < Minitest::Test
def setup
@env = Sprockets::Environment.new
@env.append_path File.expand_path("../fixtures", __FILE__)
Expand Down
2 changes: 1 addition & 1 deletion test/test_digest_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'minitest/autorun'
require 'sprockets/digest_utils'

class TestDigestUtils < MiniTest::Test
class TestDigestUtils < Minitest::Test
include Sprockets::DigestUtils

def test_detect_digest_class
Expand Down
2 changes: 1 addition & 1 deletion test/test_eco_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'sprockets/cache'
require 'sprockets/eco_processor'

class TestEcoProcessor < MiniTest::Test
class TestEcoProcessor < Minitest::Test
def test_compile_eco_template_to_js
input = {
content_type: 'application/javascript',
Expand Down
2 changes: 1 addition & 1 deletion test/test_ejs_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'sprockets/cache'
require 'sprockets/ejs_processor'

class TestEjsProcessor < MiniTest::Test
class TestEjsProcessor < Minitest::Test
def test_compile_ejs_template_to_js
input = {
content_type: 'application/javascript',
Expand Down
2 changes: 1 addition & 1 deletion test/test_encoding_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'minitest/autorun'
require 'sprockets/encoding_utils'

class TestDigestUtils < MiniTest::Test
class TestDigestUtils < Minitest::Test
include Sprockets::EncodingUtils

def test_deflate
Expand Down
2 changes: 1 addition & 1 deletion test/test_erb_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require 'sprockets/erb_processor'
require 'sass'

class TestERBProcessor < MiniTest::Test
class TestERBProcessor < Minitest::Test

def uri_path(path)
path = '/' + path if path[1] == ':' # Windows path / drive letter
Expand Down
2 changes: 1 addition & 1 deletion test/test_http_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'minitest/autorun'
require 'sprockets/http_utils'

class TestHTTPUtils < MiniTest::Test
class TestHTTPUtils < Minitest::Test
include Sprockets::HTTPUtils

def test_match_mime_type
Expand Down
2 changes: 1 addition & 1 deletion test/test_jsminc_compressor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
unless RUBY_PLATFORM.include?('java')
require 'sprockets/jsminc_compressor'

class TestJSMincCompressor < MiniTest::Test
class TestJSMincCompressor < Minitest::Test

def test_compress_javascript
input = {
Expand Down
2 changes: 1 addition & 1 deletion test/test_jst_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'sprockets/cache'
require 'sprockets/jst_processor'

class TestJstProcessor < MiniTest::Test
class TestJstProcessor < Minitest::Test
def test_export_js_template_in_JST
input = {
name: 'users/show',
Expand Down
2 changes: 1 addition & 1 deletion test/test_manifest_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'sprockets/manifest_utils'
require 'logger'

class TestManifestUtils < MiniTest::Test
class TestManifestUtils < Minitest::Test
include Sprockets::ManifestUtils

def test_generate_manifest_path
Expand Down
2 changes: 1 addition & 1 deletion test/test_path_dependency_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'minitest/autorun'
require 'sprockets/path_dependency_utils'

class TestPathDependencyUtils < MiniTest::Test
class TestPathDependencyUtils < Minitest::Test
include Sprockets::PathDependencyUtils

def test_entries_with_dependencies
Expand Down
2 changes: 1 addition & 1 deletion test/test_path_digest_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'minitest/autorun'
require 'sprockets/path_digest_utils'

class TestPathDigestUtils < MiniTest::Test
class TestPathDigestUtils < Minitest::Test
include Sprockets::PathDigestUtils

def test_file_stat_digest
Expand Down
2 changes: 1 addition & 1 deletion test/test_path_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'minitest/autorun'
require 'sprockets/path_utils'

class TestPathUtils < MiniTest::Test
class TestPathUtils < Minitest::Test
include Sprockets::PathUtils

DOSISH = File::ALT_SEPARATOR != nil
Expand Down
2 changes: 1 addition & 1 deletion test/test_processor_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_charset_supersedes_default
end
end

class TestProcessorUtils < MiniTest::Test
class TestProcessorUtils < Minitest::Test
include Sprockets::ProcessorUtils

class Processor
Expand Down
2 changes: 1 addition & 1 deletion test/test_require.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
require 'minitest/autorun'

class TestRequire < MiniTest::Test
class TestRequire < Minitest::Test
parallelize_me!

ROOT = File.expand_path("../..", __FILE__)
Expand Down
2 changes: 1 addition & 1 deletion test/test_source_map_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'minitest/autorun'
require 'sprockets/source_map_utils'

class TestSourceMapUtils < MiniTest::Test
class TestSourceMapUtils < Minitest::Test

def deep_dup(object)
Marshal.load( Marshal.dump(object) )
Expand Down
2 changes: 1 addition & 1 deletion test/test_uglifier_compressor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require 'sprockets/cache'
require 'sprockets/uglifier_compressor'

class TestUglifierCompressor < MiniTest::Test
class TestUglifierCompressor < Minitest::Test
def setup
@env = Sprockets::Environment.new
@env.append_path File.expand_path("../fixtures", __FILE__)
Expand Down
2 changes: 1 addition & 1 deletion test/test_uri_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'minitest/autorun'
require 'sprockets/uri_utils'

class TestURIUtils < MiniTest::Test
class TestURIUtils < Minitest::Test
include Sprockets::URIUtils

DOSISH = File::ALT_SEPARATOR != nil
Expand Down
Loading

0 comments on commit 572235a

Please sign in to comment.