Skip to content

Commit 42b955d

Browse files
committed
chore: add the suggestions
1 parent 324a416 commit 42b955d

File tree

4 files changed

+16
-22
lines changed

4 files changed

+16
-22
lines changed

src/invidious/routes/before_all.cr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ module Invidious::Routes::BeforeAll
6363
"/videoplayback",
6464
"/latest_version",
6565
"/download",
66+
"/companion/",
6667
}.any? { |r| env.request.resource.starts_with? r }
6768

6869
if env.request.cookies.has_key? "SID"

src/invidious/routes/companion.cr

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ module Invidious::Routes::Companion
2828
env.response.headers[key] = value
2929
end
3030

31-
if response.status_code >= 300
32-
return env.response.headers.delete("Transfer-Encoding")
33-
end
34-
35-
return proxy_file(response, env)
31+
return IO.copy response.body_io, env.response
3632
end
3733
end

src/invidious/yt_backend/connection_pool.cr

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,18 @@ struct YoutubeConnectionPool
4646
end
4747
end
4848

49-
class CompanionWrapper
49+
# Packages a `HTTP::Client` to an Invidious companion instance alongside the configuration for that instance.
50+
#
51+
# This is used as the resource for the `CompanionPool` as to allow the ability to
52+
# proxy the requests to Invidious companion from Invidious directly.
53+
# Instead of setting up routes in a reverse proxy.
54+
struct CompanionWrapper
5055
property client : HTTP::Client
5156
property companion : Config::CompanionConfig
5257

5358
def initialize(companion : Config::CompanionConfig)
5459
@companion = companion
55-
@client = HTTP::Client.new(companion.private_url)
60+
@client = make_client(companion.private_url, use_http_proxy: false)
5661
end
5762

5863
def close
@@ -73,7 +78,7 @@ struct CompanionConnectionPool
7378

7479
@pool = DB::Pool(CompanionWrapper).new(options) do
7580
companion = CONFIG.invidious_companion.sample
76-
client = make_client(companion.private_url, use_http_proxy: false)
81+
make_client(companion.private_url, use_http_proxy: false)
7782
CompanionWrapper.new(companion: companion)
7883
end
7984
end
@@ -84,10 +89,10 @@ struct CompanionConnectionPool
8489
begin
8590
response = yield wrapper
8691
rescue ex
87-
wrapper.client.close
92+
wrapper.close
8893

8994
companion = CONFIG.invidious_companion.sample
90-
client = make_client(companion.private_url, use_http_proxy: false)
95+
make_client(companion.private_url, use_http_proxy: false)
9196
wrapper = CompanionWrapper.new(companion: companion)
9297

9398
response = yield wrapper

src/invidious/yt_backend/youtube_api.cr

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -701,25 +701,17 @@ module YoutubeAPI
701701
# Send the POST request
702702

703703
begin
704-
response_body = ""
704+
response_body = Hash(String, JSON::Any).new
705705

706706
COMPANION_POOL.client do |wrapper|
707707
companion_base_url = wrapper.companion.private_url.path
708-
puts "Using companion: #{wrapper.companion.private_url}"
709708

710-
response = wrapper.client.post(companion_base_url + endpoint, headers: headers, body: data.to_json)
711-
response_body = response.body
712-
713-
if response.status_code != 200
714-
raise Exception.new(
715-
"Error while communicating with Invidious companion: " \
716-
"status code: #{response.status_code} and body: #{response_body.dump}"
717-
)
709+
wrapper.client.post("#{companion_base_url}#{endpoint}", headers: headers, body: data.to_json) do | response |
710+
response_body = JSON.parse(response.body_io).as_h
718711
end
719712
end
720713

721-
# Convert result to Hash
722-
return JSON.parse(response_body).as_h
714+
return response_body
723715
rescue ex
724716
raise InfoException.new("Error while communicating with Invidious companion: " + (ex.message || "no extra info found"))
725717
end

0 commit comments

Comments
 (0)