Class: ShopifyCLI::Theme::DevServer::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/shopify_cli/theme/dev_server/proxy.rb

Constant Summary collapse

"_secure_session_id"
/#{SESSION_COOKIE_NAME}=(\h+)/
60 * 60 * 23

Instance Method Summary collapse

Constructor Details

#initialize(ctx, theme, param_builder) ⇒ Proxy

Returns a new instance of Proxy.



32
33
34
35
36
37
38
39
40
# File 'lib/shopify_cli/theme/dev_server/proxy.rb', line 32

def initialize(ctx, theme, param_builder)
  @ctx = ctx
  @theme = theme
  @param_builder = param_builder

  @core_endpoints = Set.new
  @secure_session_id = nil
  @last_session_cookie_refresh = nil
end

Instance Method Details

#call(env) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/shopify_cli/theme/dev_server/proxy.rb', line 42

def call(env)
  headers = extract_http_request_headers(env)
  headers["Host"] = shop
  headers["Cookie"] = add_session_cookie(headers["Cookie"])
  headers["Accept-Encoding"] = "none"
  headers["User-Agent"] = "Shopify CLI"
  query = URI.decode_www_form(env["QUERY_STRING"])
  replace_templates = build_replacement_param(env)
  response = if replace_templates.any?
    # Pass to SFR the recently modified templates in `replace_templates` or
    # `replace_extension_templates` body param
    headers["Authorization"] = "Bearer #{bearer_token}"
    form_data = URI.decode_www_form(env["rack.input"].read).to_h
    request(
      "POST", env["PATH_INFO"],
      headers: headers,
      query: query,
      form_data: form_data.merge(replace_templates).merge(_method: env["REQUEST_METHOD"])
    )
  else
    request(
      env["REQUEST_METHOD"], env["PATH_INFO"],
      headers: headers,
      query: query,
      body_stream: (env["rack.input"] if has_body?(headers))
    )
  end

  headers = get_response_headers(response, env)

  unless headers["x-storefront-renderer-rendered"]
    @core_endpoints << env["PATH_INFO"]
  end

  body = patch_body(env, response.body)
  body = [body] unless body.respond_to?(:each)
  [response.code, headers, body]
end