Sha256: 06da3d19e39e491460a402e0c549641e8689864a97873ea59a30a6d789dd2e5d

Contents?: true

Size: 1.93 KB

Versions: 31

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

module ShopifyCLI
  module Theme
    module DevServer
      class CdnFonts
        FONTS_PATH = "/fonts"
        FONTS_CDN = "https://fonts.shopifycdn.com"
        FONTS_REGEX = %r{#{FONTS_CDN}}

        def initialize(app, theme:)
          @app = app
          @theme = theme
        end

        def call(env)
          path = env["PATH_INFO"]

          # Serve from fonts CDN
          return serve_font(env) if path.start_with?(FONTS_PATH)

          # Proxy the request, and replace the URLs in the response
          status, headers, body = @app.call(env)
          body = replace_font_urls(body)
          [status, headers, body]
        end

        private

        def serve_font(env)
          parameters = %w(PATH_INFO QUERY_STRING REQUEST_METHOD rack.input)
          path, query, method, body_stream = *env.slice(*parameters).values

          uri = fonts_cdn_uri(path, query)

          response = Net::HTTP.start(uri.host, 443, use_ssl: true) do |http|
            req_class = Net::HTTP.const_get(method.capitalize)
            req = req_class.new(uri)
            req.initialize_http_header(fonts_cdn_headers)
            req.body_stream = body_stream
            http.request(req)
          end

          [
            response.code.to_s,
            {
              "Content-Type" => response.content_type,
              "Content-Length" => response.content_length.to_s,
            },
            [response.body],
          ]
        end

        def fonts_cdn_headers
          {
            "Referer" => "https://#{@theme.shop}",
            "Transfer-Encoding" => "chunked",
          }
        end

        def fonts_cdn_uri(path, query)
          uri = URI.join("#{FONTS_CDN}/", path.gsub(%r{^#{FONTS_PATH}\/}, ""))
          uri.query = query.split("&").last
          uri
        end

        def replace_font_urls(body)
          [body.join.gsub(FONTS_REGEX, FONTS_PATH)]
        end
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
shopify-cli-2.24.0 lib/shopify_cli/theme/dev_server/cdn_fonts.rb
shopify-cli-2.23.0 lib/shopify_cli/theme/dev_server/cdn_fonts.rb
shopify-cli-2.22.0 lib/shopify_cli/theme/dev_server/cdn_fonts.rb
shopify-cli-2.21.0 lib/shopify_cli/theme/dev_server/cdn_fonts.rb
shopify-cli-2.20.1 lib/shopify_cli/theme/dev_server/cdn_fonts.rb
shopify-cli-2.20.0 lib/shopify_cli/theme/dev_server/cdn_fonts.rb
shopify-cli-2.19.0 lib/shopify_cli/theme/dev_server/cdn_fonts.rb
shopify-cli-2.18.1 lib/shopify_cli/theme/dev_server/cdn_fonts.rb
shopify-cli-2.18.0 lib/shopify_cli/theme/dev_server/cdn_fonts.rb
shopify-cli-2.17.0 lib/shopify_cli/theme/dev_server/cdn_fonts.rb
shopify-cli-2.16.1 lib/shopify_cli/theme/dev_server/cdn_fonts.rb
shopify-cli-2.16.0 lib/shopify_cli/theme/dev_server/cdn_fonts.rb
shopify-cli-2.15.6 lib/shopify_cli/theme/dev_server/cdn_fonts.rb
shopify-cli-2.15.5 lib/shopify_cli/theme/dev_server/cdn_fonts.rb
shopify-cli-2.15.4 lib/shopify_cli/theme/dev_server/cdn_fonts.rb
shopify-cli-2.15.3 lib/shopify_cli/theme/dev_server/cdn_fonts.rb
shopify-cli-2.15.2 lib/shopify_cli/theme/dev_server/cdn_fonts.rb
shopify-cli-2.15.1 lib/shopify_cli/theme/dev_server/cdn_fonts.rb
shopify-cli-2.15.0 lib/shopify_cli/theme/dev_server/cdn_fonts.rb
shopify-cli-2.14.0 lib/shopify_cli/theme/dev_server/cdn_fonts.rb