superbot-teleport 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1620839ecc26d6c0292f19d72cc4ec53cea926a3534cfe4f6b711b5dcac354ac
4
- data.tar.gz: 78eb318940b908ef33b6c414dbb368697fdecdeb0b385b5b12367407df83da94
3
+ metadata.gz: 4cf993d114e2d4d467b2b14447bc00703fec337a3a3ed31493b276b9b497c8b0
4
+ data.tar.gz: a5d01bc4e1ff057bdbabf44fe7f8442a179c51f39f1f52c1a8846a6461910ba8
5
5
  SHA512:
6
- metadata.gz: c057d335c073daed278599683e49f527cfd2dc29a89b3193c1efead1dcf0244a553658d7e85ca19d632ab9ea4d0c1c96678d186cf5a66eb73b2509fcce618494
7
- data.tar.gz: 14996fac924ad819156dc915fa9bc90ede7b89d00f77dcaa9509f043d71d3120a863cc987e134603569d344a4ba33e1fb462c596b4cc486c0604de9b413a485c
6
+ metadata.gz: 0f05f544ed9c7b1953f287473deb3abe8d1946431cf148ea44f3c609d831c8da8307fd7fa8ae679c3ac7ce8c15fa38590e179090fb9a3f4b8019a6de8fc2704e
7
+ data.tar.gz: 19be38e67e0bd137c11eeb887a521dd6ed5043e4087bad4ae5270c8be7055361b99ce2591578ef7b424bba4994ded339f114a9a6da01ce7ffbc4a7d7f66cf0af
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- superbot-teleport (0.1.1)
4
+ superbot-teleport (0.2.0)
5
5
  excon
6
6
  sinatra
7
7
 
@@ -1,5 +1,6 @@
1
- require 'sinatra'
1
+ require 'sinatra/base'
2
2
  require 'excon'
3
+ require 'json'
3
4
 
4
5
  module Superbot
5
6
  module Teleport
@@ -10,25 +11,13 @@ module Superbot
10
11
  s.set :show_exceptions, false
11
12
 
12
13
  s.before do
13
- $__request = request
14
- end
15
-
16
- s.error Excon::Error::Socket do
17
- puts "="*30
18
- puts $__request.path_info
19
-
20
- if $!.message.end_with? "(Errno::ECONNREFUSED)"
21
- status 500
22
- "upstream failed"
23
- else
24
- raise "unknown"
25
- end
14
+ $__superbot_teleport_request_for_errors = request
26
15
  end
27
16
 
28
17
  s.set :connection, (
29
18
  Excon.new ENV.fetch("SUPERBOT_TELEPORT_UPSTREAM_URL"), {
30
19
  persistent: true,
31
- connect_timeout: 5,
20
+ connect_timeout: 3,
32
21
  read_timeout: 5,
33
22
  write_timeout: 5,
34
23
  debug_request: (ENV["EXCON_DEBUG"] == "true"),
@@ -36,25 +25,73 @@ module Superbot
36
25
  }
37
26
  )
38
27
 
39
- [:get, :post, :delete].each do |method|
40
- s.send method, "/wd/hub/*" do
41
- path = params[:splat].join("/")
28
+ s.helpers do
29
+ def request_path(params)
30
+ params[:splat].join("/")
31
+ end
32
+
33
+ def safe_parse_json(string_or_io, on_error: nil)
34
+ JSON.parse (string_or_io.respond_to?(:read) ? string_or_io.read : string_or_io)
35
+ rescue
36
+ on_error
37
+ end
38
+
39
+ def proxy(method, params, opts={})
40
+ raise "DELETE may not contain body" if method == :delete && opts[:body]
41
+ opts[:headers] ||= {}
42
42
 
43
43
  unless ENV.fetch("SUPERBOT_TELEPORT_UPSTREAM_URL").include? "localhost"
44
- path = "wd/hub/#{path}"
44
+ path = "wd/hub/#{request_path(params)}"
45
45
  headers["Content-Type"] = "application/json"
46
+ else
47
+ path = request_path
46
48
  end
47
49
 
48
- upstream = settings.connection.request({
49
- method: method, path: path, body: request.body, headers: headers
50
- })
50
+ settings.connection.request({method: method, path: path}.merge(opts))
51
+ end
51
52
 
53
+ def respond(upstream)
52
54
  headers = upstream.headers
53
55
  status upstream.status
54
56
  upstream.body
55
57
  end
56
58
  end
57
59
 
60
+ s.get "/wd/hub/*" do
61
+ respond proxy(:get, params, {headers: headers, body: request.body})
62
+ end
63
+
64
+ s.post "/wd/hub/*" do
65
+ case request_path(params)
66
+ when "session"
67
+ parsed_body = safe_parse_json request.body, on_error: {}
68
+
69
+ if ENV['SUPERBOT_TELEPORT_REGION'] && parsed_body['desiredCapabilities']
70
+ parsed_body['desiredCapabilities']['superOptions'] = { "region": ENV['SUPERBOT_TELEPORT_REGION'] }
71
+ end
72
+
73
+ respond proxy(:post, params, {headers: headers, body: parsed_body.to_json, read_timeout: 500})
74
+ else
75
+ respond proxy(:post, params, {headers: headers, body: request.body})
76
+ end
77
+ end
78
+
79
+ s.delete "/wd/hub/*" do
80
+ respond proxy(:delete, params, {headers: headers})
81
+ end
82
+
83
+ s.error Excon::Error::Socket do
84
+ puts "="*30
85
+ puts $__superbot_teleport_request_for_errors.path_info
86
+
87
+ if $!.message.end_with? "(Errno::ECONNREFUSED)"
88
+ status 500
89
+ "upstream does not respond"
90
+ else
91
+ raise "unknown: #{$!.message}"
92
+ end
93
+ end
94
+
58
95
  s.run!
59
96
  end
60
97
  end
@@ -1,5 +1,5 @@
1
1
  module Superbot
2
2
  module Teleport
3
- VERSION = "0.1.1"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: superbot-teleport
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Superbot HQ