From 24258e052271f6bfe964dce46760050ffdfcb751 Mon Sep 17 00:00:00 2001 From: Jacopo Date: Thu, 18 Apr 2024 12:10:54 -0400 Subject: [PATCH] Init --- .github/workflows/main.yml | 28 ++++++ .gitignore | 8 ++ .ruby-version | 1 + Gemfile | 9 ++ Gemfile.lock | 52 +++++++++++ LICENSE.txt | 21 +++++ README.md | 35 ++++++++ Rakefile | 6 ++ assets-redirect.gemspec | 36 ++++++++ bin/console | 10 +++ bin/rake | 7 ++ bin/setup | 6 ++ lib/assets/redirect.rb | 7 ++ lib/assets/redirect/sprockets.rb | 66 ++++++++++++++ lib/assets/redirect/version.rb | 5 ++ ...globe-391dafc3c8e0f58faf6677e72efb27c1.png | Bin 0 -> 3205 bytes ...globe-391dafc3c8e0f58faf6677e72efb27c1.png | 1 + test/lib/assets/redirect/sprockets_test.rb | 82 ++++++++++++++++++ test/test_helper.rb | 8 ++ 19 files changed, 388 insertions(+) create mode 100644 .github/workflows/main.yml create mode 100644 .gitignore create mode 100644 .ruby-version create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 Rakefile create mode 100644 assets-redirect.gemspec create mode 100755 bin/console create mode 100755 bin/rake create mode 100755 bin/setup create mode 100644 lib/assets/redirect.rb create mode 100644 lib/assets/redirect/sprockets.rb create mode 100644 lib/assets/redirect/version.rb create mode 100644 test/fixtures/assets/snowglobe-391dafc3c8e0f58faf6677e72efb27c1.png create mode 120000 test/fixtures/hidden-assets/snowglobe-391dafc3c8e0f58faf6677e72efb27c1.png create mode 100644 test/lib/assets/redirect/sprockets_test.rb create mode 100644 test/test_helper.rb diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..7fcf49a --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,28 @@ +name: Ruby + +on: + push: + branches: + - master + + pull_request: + +jobs: + build: + runs-on: ubuntu-latest + name: Ruby ${{ matrix.ruby }} + strategy: + matrix: + ruby: + - "2.7.8" + - "3.3.0" + + steps: + - uses: actions/checkout@v4 + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + bundler-cache: true + - name: Run the default task + run: bundle exec rake diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9106b2a --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +/.bundle/ +/.yardoc +/_yardoc/ +/coverage/ +/doc/ +/pkg/ +/spec/reports/ +/tmp/ diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 0000000..6a81b4c --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +2.7.8 diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..0373ef5 --- /dev/null +++ b/Gemfile @@ -0,0 +1,9 @@ +source "https://rubygems.org" + +gemspec + +gem "rake" +gem "minitest" +gem "mocha" +gem "rack-test" +gem "activesupport" diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..9bade24 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,52 @@ +PATH + remote: . + specs: + assets-redirect (0.1.0) + rack + +GEM + remote: https://rubygems.org/ + specs: + activesupport (7.1.3.2) + base64 + bigdecimal + concurrent-ruby (~> 1.0, >= 1.0.2) + connection_pool (>= 2.2.5) + drb + i18n (>= 1.6, < 2) + minitest (>= 5.1) + mutex_m + tzinfo (~> 2.0) + base64 (0.2.0) + bigdecimal (3.1.7) + concurrent-ruby (1.2.3) + connection_pool (2.4.1) + drb (2.2.1) + i18n (1.14.4) + concurrent-ruby (~> 1.0) + minitest (5.22.3) + mocha (2.1.0) + ruby2_keywords (>= 0.0.5) + mutex_m (0.2.0) + rack (1.6.13.16) + rack-test (0.6.3) + rack (>= 1.0) + rake (13.2.1) + ruby2_keywords (0.0.5) + tzinfo (2.0.6) + concurrent-ruby (~> 1.0) + +PLATFORMS + arm64-darwin-22 + ruby + +DEPENDENCIES + activesupport + assets-redirect! + minitest + mocha + rack-test + rake + +BUNDLED WITH + 2.4.22 diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..5509e46 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2024 Jacopo + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..01fd970 --- /dev/null +++ b/README.md @@ -0,0 +1,35 @@ +# Assets::Redirect + +TODO: Delete this and the text below, and describe your gem + +Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/assets/redirect`. To experiment with that code, run `bin/console` for an interactive prompt. + +## Installation + +TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org. + +Install the gem and add to the application's Gemfile by executing: + + $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG + +If bundler is not being used to manage dependencies, install the gem by executing: + + $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG + +## Usage + +TODO: Write usage instructions here + +## Development + +After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. + +To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org). + +## Contributing + +Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/assets-redirect. + +## License + +The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..9945b30 --- /dev/null +++ b/Rakefile @@ -0,0 +1,6 @@ +require "bundler/gem_tasks" +require "minitest/test_task" + +Minitest::TestTask.create + +task default: :test diff --git a/assets-redirect.gemspec b/assets-redirect.gemspec new file mode 100644 index 0000000..d7f25c9 --- /dev/null +++ b/assets-redirect.gemspec @@ -0,0 +1,36 @@ +require_relative "lib/assets/redirect/version" + +Gem::Specification.new do |spec| + spec.name = "assets-redirect" + spec.version = Assets::Redirect::VERSION + spec.authors = ["Jacopo"] + spec.email = ["jacopo@37signals.com"] + + spec.summary = "Redirect not found assets to their latest digested version." + spec.description = <<-EOS + Rack middleware which will look up your assets manifest file and redirect a + 404 assets request to the current digested version of the asset. + EOS + + spec.homepage = "https://github.com/basecamp/assets_redirect" + spec.license = "MIT" + spec.required_ruby_version = ">= 2.7.8" + + spec.metadata["homepage_uri"] = spec.homepage + spec.metadata["source_code_uri"] = spec.homepage + spec.metadata["changelog_uri"] = %Q(#{spec.metadata["source_code_uri"]}/CHANGELOG.md) + + # Specify which files should be added to the gem when it is released. + # The `git ls-files -z` loads the files in the RubyGem that have been added into git. + spec.files = Dir.chdir(__dir__) do + `git ls-files -z`.split("\x0").reject do |f| + (File.expand_path(f) == __FILE__) || + f.start_with?(*%w[bin/ test/ .git .github Gemfile]) + end + end + spec.bindir = "exe" + spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) } + spec.require_paths = ["lib"] + + spec.add_dependency "rack" +end diff --git a/bin/console b/bin/console new file mode 100755 index 0000000..ae0f5d1 --- /dev/null +++ b/bin/console @@ -0,0 +1,10 @@ +#!/usr/bin/env ruby + +require "bundler/setup" +require "assets/redirect" + +# You can add fixtures and/or initialization code here to make experimenting +# with your gem easier. You can also use a different console, if you like. + +require "irb" +IRB.start(__FILE__) diff --git a/bin/rake b/bin/rake new file mode 100755 index 0000000..523a5c5 --- /dev/null +++ b/bin/rake @@ -0,0 +1,7 @@ +#!/usr/bin/env ruby + +require "bundler/setup" +require "assets/redirect" +require "rake" + +Rake.application.run diff --git a/bin/setup b/bin/setup new file mode 100755 index 0000000..cf4ad25 --- /dev/null +++ b/bin/setup @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +set -euo pipefail +IFS=$'\n\t' +set -vx + +bundle install diff --git a/lib/assets/redirect.rb b/lib/assets/redirect.rb new file mode 100644 index 0000000..4bdfda0 --- /dev/null +++ b/lib/assets/redirect.rb @@ -0,0 +1,7 @@ +require_relative "redirect/version" +require_relative "redirect/sprockets" + +module Assets::Redirect + @install = true + attr_accessor :install +end diff --git a/lib/assets/redirect/sprockets.rb b/lib/assets/redirect/sprockets.rb new file mode 100644 index 0000000..339d526 --- /dev/null +++ b/lib/assets/redirect/sprockets.rb @@ -0,0 +1,66 @@ +require "rack" +require "rack/request" +require "rack/mime" + +class Assets::Redirect::Sprockets + # TODO comment and explain args, then refactor further + # then test with BCX and Readme and auto install and CI + def initialize(app, environment, public_path: "/public", assets_path: "/assets", assets_host: nil) + @app = app + @environment = environment + @public_path = public_path + @assets_path = assets_path + @assets_host = assets_host + end + + def call(env) + @request = Rack::Request.new(env) + + if asset_not_found? && digest_path && file_exists?(redirect_path) + redirect_to_latest_version(env) + else + @app.call(env) + end + end + + private + def asset_not_found? + @request.path.start_with?(@assets_path) && !file_exists?(@request.path.chomp("/")) + end + + def file_exists?(asset_path) + File.exist?(File.join(@public_path, asset_path)) + end + + def redirect_path + "#{@assets_path}/#{digest_path}" + end + + def digest_path + logical_path = @request.path.sub(/^#{@assets_path}\//, "").sub(/-[a-z0-9]{32}(?<=.)/i, "") + @environment[ logical_path ]&.digest_path + end + + def redirect_to_latest_version(env) + url = URI(computed_assets_host || @request.url) + url.path = redirect_path + + headers = { "Location" => url.to_s, + "Content-Type" => Rack::Mime.mime_type(::File.extname(digest_path)), + "Cache-Control" => "no-cache; max-age=0" } + + [ 302, headers, [ redirect_message(url.to_s) ] ] + end + + def computed_assets_host + if @assets_host.respond_to?(:call) + @assets_host.call(@request) + else + @assets_host + end + end + + def redirect_message(location) + %[Redirecting to #{location}] + end +end diff --git a/lib/assets/redirect/version.rb b/lib/assets/redirect/version.rb new file mode 100644 index 0000000..0f019a7 --- /dev/null +++ b/lib/assets/redirect/version.rb @@ -0,0 +1,5 @@ +module Assets + module Redirect + VERSION = "0.1.0" + end +end diff --git a/test/fixtures/assets/snowglobe-391dafc3c8e0f58faf6677e72efb27c1.png b/test/fixtures/assets/snowglobe-391dafc3c8e0f58faf6677e72efb27c1.png new file mode 100644 index 0000000000000000000000000000000000000000..1d6c1d19a0525ed336b28941ebef0473a00aa470 GIT binary patch literal 3205 zcmbuChdUIG1IAIt*;|>1i|jA5w~mlbIolZ#l6|rd;cT)l5>h%V<7Dq~k+X$J=7qbg z%ceMf{SUwQdEU?aAAFwYO@0Wu$3)LdPeDP!WNZXB|F@C<8cIj=k1G$zxfB$Prp91B zi_jmtMSzE#*1Uts_t;f@OH*u~S&c|N`zhx5b%(d%O?82ENr~Ze0}emV0lqYNC@pv| z0 z;_og}l&i&|9WEaSfyTUicNzJ$MKvZv(+Ive6n z+^4tR2oG&cGTuxI1$JaL;JDVcshV0RF3Hjzg|r=xifhd&-Kq77Nt7{3l#~p*&3tG6 z6RHu(}flnh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W>Pf^C88_z1 zv35>x)GJ*6eS(3YaFy}WPNN`#!zZgNsgIxID(ef&pNh0uqP(I7H9^Rw?9TqMW9r(s zLqcchEdoB_AAMLXH5`Ykq&zLhC0@2{UW?$SK+{}%DE@c*(j8dA7H-Dds zIFks7%ewnEW2JPi@1#T;f};e7bDpPQR>z8T^h`G$KBWo}-o2^0r=nrc(mfQ60=#|b&}^jo1F zw>AZiktcGVxRITu!JCChTypXYzT$gSaIC@F4qRi4K1n1GkA35-aP{gaTw;k1?rDE` zf0&<6uEF)PSfmyQ2QjMkLtbZ&4GUYBe*}ZAK8?I;*e-2Y0Jyen4|g+%b>(>;dp`ZY d!u$M^Wu(9G3u0w{ stub(digest_path: "snowglobe-391dafc3c8e0f58faf6677e72efb27c1.png"), + "deleted-snowglobe.png" => stub(digest_path: "deleted-snowglobe-391dafc3c8e0f58faf6677e72efb27c1.png") + } + + Assets::Redirect::Sprockets.new(default_app, @environment, **{ public_path: "test/fixtures" }.merge(options)) + end + + def default_app + lambda { |env| + headers = {"Content-Type" => "text/html"} + [ 200, headers, [ "OK" ] ] + } + end + + def test_redirect + get "http://example.org/assets/snowglobe-8f05909c25ad578ba8fbb27fb28da779.png" + assert_equal "http://example.org/assets/snowglobe-391dafc3c8e0f58faf6677e72efb27c1.png", + last_response.headers["Location"] + assert_equal "image/png", last_response.header["Content-Type"] + assert_equal "no-cache; max-age=0", last_response.headers["Cache-control"] + assert_equal 302, last_response.status + + get "http://example.org/assets/snowglobe.png" + assert_equal "http://example.org/assets/snowglobe-391dafc3c8e0f58faf6677e72efb27c1.png", + last_response.headers["Location"] + end + + def test_no_redirect + get "http://example.org/assets/snowglobe-391dafc3c8e0f58faf6677e72efb27c1.png" + assert_equal 200, last_response.status + + get "http://example.org/users" + assert_equal 200, last_response.status + + get "http://example.org/assets/unmatched-391dafc3c8e0f58faf6677e72efb27c1.png" + assert_equal 200, last_response.status + + get "http://example.org/assets/deleted-snowglobe-391dafc3c840f58faf6677e72efb27c1.png" + assert_equal 200, last_response.status + end + + def test_custom_assets_path + self.app = build_app(assets_path: "/hidden-assets") + + get "http://example.org/hidden-assets/snowglobe-399dafc3c8e0f58faf6677e72efb27c1.png" + assert_equal "http://example.org/hidden-assets/snowglobe-391dafc3c8e0f58faf6677e72efb27c1.png", + last_response.headers["Location"] + + get "http://example.org/assets/snowglobe-399dafc3c8e0f58faf6677e72efb27c1.png" + assert_equal 200, last_response.status + end + + def test_custom_assets_host + self.app = build_app(assets_host: "http://test.cloudfront.net") + + get "http://example.org/assets/snowglobe-399dafc3c8e0f58faf6677e72efb27c1.png" + assert_equal "http://test.cloudfront.net/assets/snowglobe-391dafc3c8e0f58faf6677e72efb27c1.png", + last_response.headers["Location"] + + self.app = build_app( + :assets_host => Proc.new do |request| + if request.path.end_with?(".png") + "http://test.cloudfront.net" + end + end + ) + + get "http://example.org/assets/snowglobe-399dafc3c8e0f58faf6677e72efb27c1.png" + assert_equal "http://test.cloudfront.net/assets/snowglobe-391dafc3c8e0f58faf6677e72efb27c1.png", + last_response.headers["Location"] + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..80e05d2 --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,8 @@ +$LOAD_PATH.unshift File.expand_path("../lib", __dir__) +require "assets/redirect/sprockets" +require "minitest/autorun" +require "rack/test" +require 'mocha/minitest' +require "active_support/all" + +ActiveSupport.test_order = :random