web-console 3.5.1 → 4.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.markdown +91 -8
  3. data/MIT-LICENSE +1 -1
  4. data/README.markdown +41 -38
  5. data/Rakefile +14 -12
  6. data/lib/web-console.rb +3 -1
  7. data/lib/web_console/context.rb +8 -6
  8. data/lib/web_console/errors.rb +2 -0
  9. data/lib/web_console/evaluator.rb +14 -5
  10. data/lib/web_console/exception_mapper.rb +33 -10
  11. data/lib/web_console/extensions.rb +12 -23
  12. data/lib/web_console/injector.rb +32 -0
  13. data/lib/web_console/interceptor.rb +17 -0
  14. data/lib/web_console/middleware.rb +21 -24
  15. data/lib/web_console/permissions.rb +42 -0
  16. data/lib/web_console/railtie.rb +36 -19
  17. data/lib/web_console/request.rb +8 -20
  18. data/lib/web_console/session.rb +13 -9
  19. data/lib/web_console/source_location.rb +17 -0
  20. data/lib/web_console/tasks/extensions.rake +15 -13
  21. data/lib/web_console/tasks/templates.rake +9 -13
  22. data/lib/web_console/template.rb +4 -3
  23. data/lib/web_console/templates/console.js.erb +140 -38
  24. data/lib/web_console/templates/error_page.js.erb +7 -8
  25. data/lib/web_console/templates/index.html.erb +4 -0
  26. data/lib/web_console/templates/layouts/inlined_string.erb +1 -1
  27. data/lib/web_console/templates/layouts/javascript.erb +1 -1
  28. data/lib/web_console/templates/regular_page.js.erb +24 -0
  29. data/lib/web_console/templates/style.css.erb +182 -33
  30. data/lib/web_console/testing/erb_precompiler.rb +5 -3
  31. data/lib/web_console/testing/fake_middleware.rb +14 -9
  32. data/lib/web_console/testing/helper.rb +3 -1
  33. data/lib/web_console/version.rb +3 -1
  34. data/lib/web_console/view.rb +11 -3
  35. data/lib/web_console/whiny_request.rb +7 -5
  36. data/lib/web_console.rb +17 -8
  37. metadata +17 -15
  38. data/lib/web_console/response.rb +0 -23
  39. data/lib/web_console/whitelist.rb +0 -44
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 8a1cf82ab4c1380c5d85d7cdbda8b48d8ef0d7e4
4
- data.tar.gz: 15b623e433d5101d376444ad1122b00282fc3fa6
2
+ SHA256:
3
+ metadata.gz: ed1dcd754f3147bcefa20043ba34ea374d9acac479d68e3eae7b89b3ee68994d
4
+ data.tar.gz: 7556b35cf5abd8d6438931b7d4cce3dd082483617cfeca28545d7a1bb5c83d06
5
5
  SHA512:
6
- metadata.gz: 7e02abfa3203653103218c7a187c5cc4337b252f5a383ad5c3cfd06eee7d0a93f42f08230131bab0bc9481d7ba78cc8296230a78af7e393fa08bc1a08f8c4b76
7
- data.tar.gz: fae466a396dfdff3c5c2397c002ca6c5ca9d1e9b82d59a71d71f4c39b3752f2a3b41101675b9f8c1c9ec40edb71ab0784e007839cc53c2622660131a05883351
6
+ metadata.gz: 757a0810bb51d57678f588d7f70ac3734c0a32740aa357acab67edc880701588bf703ca6e90e0e228b3ea8a6d009bd32b8b5e0d3cfb7f0569d784c69028d2c08
7
+ data.tar.gz: 7ffb4ff41a124ca94852bc043dc4e745ee43e112c4e986e83ad9296f2d97cad17f83ce05b3f9917c5b68f74741aa674ec0bd052e97b588cd822cfbdd2a315e7b
data/CHANGELOG.markdown CHANGED
@@ -1,16 +1,83 @@
1
1
  # CHANGELOG
2
2
 
3
- ## master (unreleased)
3
+ ## main (unreleased)
4
+
5
+ # 4.2.1
6
+
7
+ * Support to Rails 7.1
8
+ * Support to Rack 3.0
9
+
10
+ ## 4.2.0
11
+
12
+ * [#308](https://github.com/rails/web-console/pull/308) Fix web-console inline templates rendering ([@voxik])
13
+ * [#306](https://github.com/rails/web-console/pull/306) Support Ruby 3.0 and above ([@ryanwood])
14
+
15
+ ## 4.1.0
16
+
17
+ * [#304](https://github.com/rails/web-console/pull/304) Add support for Rails 6.1 ([@stephannv])
18
+ * [#298](https://github.com/rails/web-console/pull/298) Prevent deprecation warnings by removing template formats ([@mikelkew])
19
+ * [#297](https://github.com/rails/web-console/pull/297) Use MutationObserver instead of Mutation Events ([@mikelkew])
20
+ * [#296](https://github.com/rails/web-console/pull/296) Add CSP nonce to injected scripts and styles ([@mikelkew])
21
+
22
+ ## 4.0.4
23
+
24
+ * [fb483743](https://github.com/rails/web-console/commit/fb483743a6a2a4168cdc0b2e03f48fc393991b73) Fix a crash on webrick with Rack 2.2.3 ([@gsamokovarov])
25
+
26
+ ## 4.0.3
27
+
28
+ * [#291](https://github.com/rails/web-console/pull/291) Deprecate config.web_console.whitelisted_ips ([@JuanitoFatas])
29
+ * [#290](https://github.com/rails/web-console/pull/290) Fix Content-Length for rack >= 2.1.0 ([@p8])
30
+
31
+ ## 4.0.2
32
+
33
+ * [#285](https://github.com/rails/web-console/pull/285) Increase timeout on paste ([@celvro])
34
+
35
+ ## 4.0.1
36
+
37
+ * [#279](https://github.com/rails/web-console/pull/279) Fix initial config.web_console.permissions value ([@patorash])
38
+
39
+ ## 4.0.0
40
+
41
+ * [61ce65b5](https://github.com/rails/web-console/commit/61ce65b599f56809de1bd8da6590a80acbd92017) Move to config.web_console.permissions ([@gsamokovarov])
42
+ * [96127ac1](https://github.com/rails/web-console/commit/96127aac143e1e653fffdc4bb65e1ce0b5ff342d) Introduce Binding#console as an alternative interface ([@gsamokovarov])
43
+ * [d4591ca5](https://github.com/rails/web-console/commit/d4591ca5396ed15a08818f3da11134852a485b27) Introduce Rails 6 support ([@gsamokovarov])
44
+ * [f97d8a88](https://github.com/rails/web-console/commit/f97d8a889a38366485e5c5e8985995c19bf61d13) Introduce Ruby 2.6 support ([@gsamokovarov])
45
+ * [d6deacd9](https://github.com/rails/web-console/commit/d6deacd9d5fcaabf3e3051d6985b53f924f86956) Drop Rails 5 support ([@gsamokovarov])
46
+ * [90fda878](https://github.com/rails/web-console/commit/90fda8789d402f05647c18f8cdf8e5c3d01692dd) Drop Ruby 2.4 support ([@gsamokovarov])
47
+ * [#265](https://github.com/rails/web-console/pull/265) Add support for nested exceptions ([@yuki24])
48
+
49
+ ## 3.7.0
50
+
51
+ * [#263](https://github.com/rails/web-console/pull/263) Show binding changes ([@causztic])
52
+ * [#258](https://github.com/rails/web-console/pull/258) Support Ctrl-A, Ctrl-W and Ctrl-U ([@gsamokovarov])
53
+ * [#257](https://github.com/rails/web-console/pull/257) Always try to keep the console underneath the website content ([@gsamokovarov])
54
+
55
+ ## 3.6.2
56
+
57
+ * [#255](https://github.com/rails/web-console/pull/255) Fix the truncated HTML body, because of wrong Content-Length header ([@timomeh])
58
+
59
+ ## 3.6.1
60
+
61
+ * [#252](https://github.com/rails/web-console/pull/252) Fix improper injection in Rack bodies like ActionDispatch::Response::RackBody ([@gsamokovarov])
62
+
63
+ ## 3.6.0
64
+
65
+ * [#254](https://github.com/rails/web-console/pull/254) Rescue ActionDispatch::RemoteIp::IpSpoofAttackError ([@wjordan])
66
+ * [#250](https://github.com/rails/web-console/pull/250) Close original body to comply with Rack SPEC ([@wagenet])
67
+ * [#249](https://github.com/rails/web-console/pull/249) Update for frozen-string-literal friendliness ([@pat])
68
+ * [#248](https://github.com/rails/web-console/pull/248) Fix copy on Safari ([@ybart])
69
+ * [#246](https://github.com/rails/web-console/pull/246) International keyboard special character input fixes ([@fl0l0u])
70
+ * [#244](https://github.com/rails/web-console/pull/244) Let WebConsole.logger respect Rails.logger ([@gsamokovarov])
4
71
 
5
72
  ## 3.5.1
6
73
 
7
- * [239](https://github.com/rails/web-console/pull/239) Fix the ActionDispatch::DebugExceptions integration ([@gsamokovarov])
74
+ * [#239](https://github.com/rails/web-console/pull/239) Fix the ActionDispatch::DebugExceptions integration ([@gsamokovarov])
8
75
 
9
76
  ## 3.5.0
10
77
 
11
- * [237](https://github.com/rails/web-console/pull/237) Bindex integration for JRuby 9k support ([@gsamokovarov])
12
- * [236](https://github.com/rails/web-console/pull/236) Remove unused Active Support lazy load hook ([@betesh])
13
- * [230](https://github.com/rails/web-console/pull/230) Handle invalid remote addresses ([@akirakoyasu])
78
+ * [#237](https://github.com/rails/web-console/pull/237) Bindex integration for JRuby 9k support ([@gsamokovarov])
79
+ * [#236](https://github.com/rails/web-console/pull/236) Remove unused Active Support lazy load hook ([@betesh])
80
+ * [#230](https://github.com/rails/web-console/pull/230) Handle invalid remote addresses ([@akirakoyasu])
14
81
 
15
82
  ## 3.4.0
16
83
 
@@ -22,7 +89,7 @@ Drop support for Rails `4.2.0`.
22
89
 
23
90
  ## 3.3.0
24
91
 
25
- * [203](https://github.com/rails/web-console/pull/203) Map bindings to traces based on the trace __FILE__ and __LINE__ ([@gsamokovarov])
92
+ * [#203](https://github.com/rails/web-console/pull/203) Map bindings to traces based on the trace __FILE__ and __LINE__ ([@gsamokovarov])
26
93
 
27
94
  ## 3.2.1
28
95
 
@@ -55,8 +122,8 @@ Drop support for Rails `4.2.0`.
55
122
  This is mainly a Rails 5 compatibility release. If you have the chance, please
56
123
  go to 3.1.0 instead.
57
124
 
58
- * [#181](https://github.com/rails/web-console/pull/181) Log internal Web Console errors (@schneems)
59
- * [#150](https://github.com/rails/web-console/pull/150) Revert #150. (@gsamokovarov)
125
+ * [#181](https://github.com/rails/web-console/pull/181) Log internal Web Console errors ([@schneems])
126
+ * [#150](https://github.com/rails/web-console/pull/150) Revert #150. ([@gsamokovarov])
60
127
 
61
128
  ## 2.2.1
62
129
 
@@ -95,6 +162,8 @@ go to 3.1.0 instead.
95
162
  * [#84](https://github.com/rails/web-console/pull/84) Allow Rails 5 as dependency in gemspec ([@jonatack])
96
163
  * [#69](https://github.com/rails/web-console/pull/69) Introduce middleware for request dispatch and console rendering ([@gsamokovarov])
97
164
 
165
+ [@stephannv]: https://github.com/stephannv
166
+ [@mikelkew]: https://github.com/mikelkew
98
167
  [@jonatack]: https://github.com/jonatack
99
168
  [@ryandao]: https://github.com/ryandao
100
169
  [@jeffnv]: https://github.com/jeffnv
@@ -108,3 +177,17 @@ go to 3.1.0 instead.
108
177
  [@schneems]: https://github.com/schneems
109
178
  [@betesh]: https://github.com/betesh
110
179
  [@akirakoyasu]: https://github.com/akirakoyasu
180
+ [@wagenet]: https://github.com/wagenet
181
+ [@wjordan]: https://github.com/wjordan
182
+ [@pat]: https://github.com/pat
183
+ [@ybart]: https://github.com/ybart
184
+ [@fl0l0u]: https://github.com/fl0l0u
185
+ [@timomeh]: https://github.com/timomeh
186
+ [@causztic]: https://github.com/causztic
187
+ [@yuki24]: https://github.com/yuki24
188
+ [@patorash]: https://github.com/patorash
189
+ [@celvro]: https://github.com/celvro
190
+ [@JuanitoFatas]: https://github.com/JuanitoFatas
191
+ [@p8]: https://github.com/p8
192
+ [@voxik]: https://github.com/voxik
193
+ [@ryanwood]: https://github.com/ryanwood
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2014-2016 Charlie Somerville, Genadi Samokovarov, Guillermo Iguaran and Ryan Dao
1
+ Copyright 2014-2016 Hailey Somerville, Genadi Samokovarov, Guillermo Iguaran and Ryan Dao
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.markdown CHANGED
@@ -1,11 +1,11 @@
1
1
  <p align=right>
2
- Documentation for:
2
+ <strong>Current version: 4.2.1</strong> | Documentation for:
3
3
  <a href=https://github.com/rails/web-console/tree/v1.0.4>v1.0.4</a>
4
4
  <a href=https://github.com/rails/web-console/tree/v2.2.1>v2.2.1</a>
5
- <a href=https://github.com/rails/web-console/tree/v3.0.0>v3.0.0</a>
5
+ <a href=https://github.com/rails/web-console/tree/v3.7.0>v3.7.0</a>
6
6
  </p>
7
7
 
8
- # Web Console [![Build Status](https://travis-ci.org/rails/web-console.svg?branch=master)](https://travis-ci.org/rails/web-console)
8
+ # Web Console [![CI](https://github.com/rails/web-console/actions/workflows/ci.yml/badge.svg)](https://github.com/rails/web-console/actions/workflows/ci.yml)
9
9
 
10
10
  _Web Console_ is a debugging tool for your Ruby on Rails applications.
11
11
 
@@ -17,8 +17,7 @@ _Web Console_ is a debugging tool for your Ruby on Rails applications.
17
17
 
18
18
  ## Installation
19
19
 
20
- _Web Console_ is meant to work as a Rails plugin. To install it in your current
21
- application, add the following to your `Gemfile`.
20
+ Add the following to your `Gemfile`:
22
21
 
23
22
  ```ruby
24
23
  group :development do
@@ -26,14 +25,11 @@ group :development do
26
25
  end
27
26
  ```
28
27
 
29
- After you save the `Gemfile` changes, make sure to run `bundle install` and
30
- restart your server for the _Web Console_ to kick in.
31
-
32
28
  ## Usage
33
29
 
34
30
  The web console allows you to create an interactive Ruby session in your
35
- browser. Those sessions are launched automatically in case on an error, but
36
- they can also be launched manually in any page.
31
+ browser. Those sessions are launched automatically in case of an error and can
32
+ also be launched manually in any page.
37
33
 
38
34
  For example, calling `console` in a view will display a console in the current
39
35
  page in the context of the view binding.
@@ -56,35 +52,35 @@ end
56
52
 
57
53
  The method is defined in `Kernel` and you can invoke it any application code.
58
54
 
59
- Only one `console` invocation is allowed once per request. If you happen to
60
- have multiple ones, a `WebConsole::DoubleRenderError` will be raised.
55
+ Only one `console` invocation per request is allowed. If you happen to
56
+ have multiple ones, `WebConsole::DoubleRenderError` will be raised.
61
57
 
62
58
  ## Configuration
63
59
 
64
- _Web Console_ allows you to execute arbitrary code on the server, so you
65
- should be very careful, who you give access to.
60
+ _Web Console_ allows you to execute arbitrary code on the server. Therefore, be
61
+ very careful who you give access to.
66
62
 
67
- ### config.web_console.whitelisted_ips
63
+ ### config.web_console.permissions
68
64
 
69
65
  By default, only requests coming from IPv4 and IPv6 localhosts are allowed.
70
66
 
71
- `config.web_console.whitelisted_ips` lets you control which IP's have access to
67
+ `config.web_console.permissions` lets you control which IP's have access to
72
68
  the console.
73
69
 
74
- You can whitelist single IP's or whole networks. Say you want to share your
75
- console with `192.168.0.100`. You can do this:
70
+ You can allow single IP's or whole networks. Say you want to share your
71
+ console with `192.168.0.100`:
76
72
 
77
73
  ```ruby
78
74
  class Application < Rails::Application
79
- config.web_console.whitelisted_ips = '192.168.0.100'
75
+ config.web_console.permissions = '192.168.0.100'
80
76
  end
81
77
  ```
82
78
 
83
- If you want to whitelist the whole private network, you can do:
79
+ If you want to allow the whole private network:
84
80
 
85
81
  ```ruby
86
82
  Rails.application.configure do
87
- config.web_console.whitelisted_ips = '192.168.0.0/16'
83
+ config.web_console.permissions = '192.168.0.0/16'
88
84
  end
89
85
  ```
90
86
 
@@ -93,13 +89,13 @@ case in 2.0.
93
89
 
94
90
  ### config.web_console.whiny_requests
95
91
 
96
- When a console cannot be shown for a given IP address or content type, a
97
- messages like the following is printed in the server logs:
92
+ When a console cannot be shown for a given IP address or content type,
93
+ messages such as the following is printed in the server logs:
98
94
 
99
95
  > Cannot render console from 192.168.1.133! Allowed networks:
100
96
  > 127.0.0.0/127.255.255.255, ::1
101
97
 
102
- If you don't wanna see this message anymore, set this option to `false`:
98
+ If you don't want to see this message anymore, set this option to `false`:
103
99
 
104
100
  ```ruby
105
101
  Rails.application.configure do
@@ -109,7 +105,7 @@ end
109
105
 
110
106
  ### config.web_console.template_paths
111
107
 
112
- If you wanna style the console yourself, you can place `style.css` at a
108
+ If you want to style the console yourself, then you can place `style.css` at a
113
109
  directory pointed by `config.web_console.template_paths`:
114
110
 
115
111
  ```ruby
@@ -118,14 +114,14 @@ Rails.application.configure do
118
114
  end
119
115
  ```
120
116
 
121
- You may wanna check the [templates] folder at the source tree for the files you
117
+ You may want to check the [templates] folder at the source tree for the files you
122
118
  may override.
123
119
 
124
120
  ### config.web_console.mount_point
125
121
 
126
122
  Usually the middleware of _Web Console_ is mounted at `/__web_console`.
127
- If you wanna change the path for some reasons, you can specify it
128
- by `config.web_console.mount_point`:
123
+ If there is a need to change the path, then you can specify it by
124
+ `config.web_console.mount_point`:
129
125
 
130
126
  ```ruby
131
127
  Rails.application.configure do
@@ -137,26 +133,31 @@ end
137
133
 
138
134
  ### Where did /console go?
139
135
 
140
- The remote terminal emulator was extracted in its own gem that is no longer
136
+ The remote terminal emulator was extracted in its own gem which is no longer
141
137
  bundled with _Web Console_.
142
138
 
143
139
  If you miss this feature, check out [rvt].
144
140
 
145
- ### Why I constantly get unavailable session errors?
141
+ ### Why do I constantly get unavailable session errors?
146
142
 
147
143
  All of _Web Console_ sessions are stored in memory. If you happen to run on a
148
- multi-process server (like Unicorn) you may get unavailable session errors
144
+ multi-process server (like Unicorn), you may encounter unavailable session errors
149
145
  while the server is still running. This is because a request may hit a
150
146
  different worker (process) that doesn't have the desired session in memory.
151
147
  To avoid that, if you use such servers in development, configure them so they
152
- server requests only out of one process.
148
+ serve requests only out of one process.
149
+
150
+ #### Passenger
151
+
152
+ Enable sticky sessions for [Passenger on Nginx] or [Passenger on Apache] to
153
+ prevent unavailable session errors.
153
154
 
154
155
  ### How to inspect local and instance variables?
155
156
 
156
157
  The interactive console executes Ruby code. Invoking `instance_variables` and
157
158
  `local_variables` will give you what you want.
158
159
 
159
- ### Why does console only appear on error pages but not when I call it?
160
+ ### Why does the console only appear on error pages but not when I call it?
160
161
 
161
162
  This can be happening if you are using `Rack::Deflater`. Be sure that
162
163
  `WebConsole::Middleware` is used after `Rack::Deflater`. The easiest way to do
@@ -168,23 +169,25 @@ Rails.application.configure do
168
169
  end
169
170
  ```
170
171
 
171
- ### Why I'm getting an undefined method `web_console`?
172
+ ### Why am I getting an undefined method `web_console`?
172
173
 
173
174
  Make sure your configuration lives in `config/environments/development.rb`.
174
175
 
175
176
  ## Credits
176
177
 
177
- * Shoutout to [Charlie Somerville] for [better_errors].
178
+ * Shoutout to [Hailey Somerville] for [better_errors].
178
179
  * Kudos to [John Mair] for [binding_of_caller] and [debug_inspector].
179
180
  * Thanks to [Charles Oliver Nutter] for all the _JRuby_ feedback.
180
181
  * Hugs and kisses to all of our [contributors]!
181
182
 
182
- [better_errors]: https://github.com/charliesome/better_errors
183
+ [better_errors]: https://github.com/BetterErrors/better_errors
183
184
  [debug_inspector]: https://github.com/banister/debug_inspector
184
185
  [binding_of_caller]: https://github.com/banister/binding_of_caller
185
- [Charlie Somerville]: https://github.com/charliesome
186
+ [Hailey Somerville]: https://github.com/haileys
186
187
  [John Mair]: https://github.com/banister
187
188
  [Charles Oliver Nutter]: https://github.com/headius
188
- [templates]: https://github.com/rails/web-console/tree/master/lib/web_console/templates
189
+ [templates]: https://github.com/rails/web-console/tree/main/lib/web_console/templates
189
190
  [rvt]: https://github.com/gsamokovarov/rvt
190
191
  [contributors]: https://github.com/rails/web-console/graphs/contributors
192
+ [Passenger on Nginx]: https://www.phusionpassenger.com/library/config/nginx/reference/#passengerstickysessions
193
+ [Passenger on Apache]: https://www.phusionpassenger.com/library/config/apache/reference/#passengerstickysessions
data/Rakefile CHANGED
@@ -1,26 +1,28 @@
1
+ # frozen_string_literal: true
2
+
1
3
  begin
2
- require 'bundler/setup'
4
+ require "bundler/setup"
3
5
  rescue LoadError
4
- puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ puts "You must `gem install bundler` and `bundle install` to run rake tasks"
5
7
  end
6
8
 
7
- require 'socket'
8
- require 'rake/testtask'
9
- require 'tmpdir'
10
- require 'securerandom'
11
- require 'json'
12
- require 'web_console/testing/erb_precompiler'
9
+ require "socket"
10
+ require "rake/testtask"
11
+ require "tmpdir"
12
+ require "securerandom"
13
+ require "json"
14
+ require "web_console/testing/erb_precompiler"
13
15
 
14
16
  EXPANDED_CWD = File.expand_path(File.dirname(__FILE__))
15
17
 
16
18
  Rake::TestTask.new(:test) do |t|
17
- t.libs << 'lib'
18
- t.libs << 'test'
19
- t.pattern = 'test/**/*_test.rb'
19
+ t.libs << "lib"
20
+ t.libs << "test"
21
+ t.pattern = "test/**/*_test.rb"
20
22
  t.verbose = false
21
23
  end
22
24
 
23
- Dir['lib/web_console/tasks/**/*.rake'].each { |task| load task }
25
+ Dir["lib/web_console/tasks/**/*.rake"].each { |task| load task }
24
26
 
25
27
  Bundler::GemHelper.install_tasks
26
28
 
data/lib/web-console.rb CHANGED
@@ -1 +1,3 @@
1
- require 'web_console'
1
+ # frozen_string_literal: true
2
+
3
+ require "web_console"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module WebConsole
2
4
  # A context lets you get object names related to the current session binding.
3
5
  class Context
@@ -17,12 +19,12 @@ module WebConsole
17
19
  private
18
20
 
19
21
  GLOBAL_OBJECTS = [
20
- 'instance_variables',
21
- 'local_variables',
22
- 'methods',
23
- 'class_variables',
24
- 'Object.constants',
25
- 'global_variables'
22
+ "instance_variables",
23
+ "local_variables",
24
+ "methods",
25
+ "class_variables",
26
+ "Object.constants",
27
+ "global_variables"
26
28
  ]
27
29
 
28
30
  def global
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module WebConsole
2
4
  # The base class for every Web Console related error.
3
5
  Error = Class.new(StandardError)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module WebConsole
2
4
  # Simple Ruby code evaluator.
3
5
  #
@@ -6,16 +8,23 @@ module WebConsole
6
8
  # return a string and will format exception output.
7
9
  class Evaluator
8
10
  # Cleanses exceptions raised inside #eval.
9
- cattr_reader :cleaner
10
- @@cleaner = ActiveSupport::BacktraceCleaner.new
11
- @@cleaner.add_silencer { |line| line.start_with?(File.expand_path('..', __FILE__)) }
11
+ cattr_reader :cleaner, default: begin
12
+ cleaner = ActiveSupport::BacktraceCleaner.new
13
+ cleaner.add_silencer { |line| line.start_with?(File.expand_path("..", __FILE__)) }
14
+ cleaner
15
+ end
12
16
 
13
17
  def initialize(binding = TOPLEVEL_BINDING)
14
18
  @binding = binding
15
19
  end
16
20
 
17
21
  def eval(input)
18
- "=> #{@binding.eval(input).inspect}\n"
22
+ # Binding#source_location is available since Ruby 2.6.
23
+ if @binding.respond_to? :source_location
24
+ "=> #{@binding.eval(input, *@binding.source_location).inspect}\n"
25
+ else
26
+ "=> #{@binding.eval(input).inspect}\n"
27
+ end
19
28
  rescue Exception => exc
20
29
  format_exception(exc)
21
30
  end
@@ -25,7 +34,7 @@ module WebConsole
25
34
  def format_exception(exc)
26
35
  backtrace = cleaner.clean(Array(exc.backtrace) - caller)
27
36
 
28
- format = "#{exc.class.name}: #{exc}\n"
37
+ format = "#{exc.class.name}: #{exc}\n".dup
29
38
  format << backtrace.map { |trace| "\tfrom #{trace}\n" }.join
30
39
  format
31
40
  end
@@ -1,8 +1,29 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module WebConsole
2
4
  class ExceptionMapper
5
+ attr_reader :exc
6
+
7
+ def self.follow(exc)
8
+ mappers = [new(exc)]
9
+
10
+ while cause = (cause || exc).cause
11
+ mappers << new(cause)
12
+ end
13
+
14
+ mappers
15
+ end
16
+
17
+ def self.find_binding(mappers, exception_object_id)
18
+ mappers.detect do |exception_mapper|
19
+ exception_mapper.exc.object_id == exception_object_id.to_i
20
+ end || mappers.first
21
+ end
22
+
3
23
  def initialize(exception)
4
24
  @backtrace = exception.backtrace
5
25
  @bindings = exception.bindings
26
+ @exc = exception
6
27
  end
7
28
 
8
29
  def first
@@ -15,19 +36,21 @@ module WebConsole
15
36
 
16
37
  private
17
38
 
18
- def guess_binding_for_index(index)
19
- file, line = @backtrace[index].to_s.split(':')
20
- line = line.to_i
39
+ def guess_binding_for_index(index)
40
+ file, line = @backtrace[index].to_s.split(":")
41
+ line = line.to_i
21
42
 
22
- @bindings.find do |binding|
23
- binding.eval('__FILE__') == file && binding.eval('__LINE__') == line
43
+ @bindings.find do |binding|
44
+ source_location = SourceLocation.new(binding)
45
+ source_location.path == file && source_location.lineno == line
46
+ end
24
47
  end
25
- end
26
48
 
27
- def guess_the_first_application_binding
28
- @bindings.find do |binding|
29
- binding.eval('__FILE__').to_s.start_with?(Rails.root.to_s)
49
+ def guess_the_first_application_binding
50
+ @bindings.find do |binding|
51
+ source_location = SourceLocation.new(binding)
52
+ source_location.path.to_s.start_with?(Rails.root.to_s)
53
+ end
30
54
  end
31
- end
32
55
  end
33
56
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Kernel
2
4
  module_function
3
5
 
@@ -6,8 +8,8 @@ module Kernel
6
8
  # If +binding+ isn't explicitly given it will default to the binding of the
7
9
  # previous frame. E.g. the one that invoked +console+.
8
10
  #
9
- # Raises DoubleRenderError if a double +console+ invocation per request is
10
- # detected.
11
+ # Raises +DoubleRenderError+ if a more than one +console+ invocation per
12
+ # request is detected.
11
13
  def console(binding = Bindex.current_bindings.second)
12
14
  raise WebConsole::DoubleRenderError if Thread.current[:__web_console_binding]
13
15
 
@@ -20,26 +22,13 @@ module Kernel
20
22
  end
21
23
  end
22
24
 
23
- module ActionDispatch
24
- class DebugExceptions
25
- def render_exception_with_web_console(request, exception)
26
- render_exception_without_web_console(request, exception).tap do
27
- backtrace_cleaner = request.get_header('action_dispatch.backtrace_cleaner')
28
- error = ExceptionWrapper.new(backtrace_cleaner, exception).exception
29
-
30
- # Get the original exception if ExceptionWrapper decides to follow it.
31
- Thread.current[:__web_console_exception] = error
32
-
33
- # ActionView::Template::Error bypass ExceptionWrapper original
34
- # exception following. The backtrace in the view is generated from
35
- # reaching out to original_exception in the view.
36
- if error.is_a?(ActionView::Template::Error)
37
- Thread.current[:__web_console_exception] = error.cause
38
- end
39
- end
40
- end
41
-
42
- alias_method :render_exception_without_web_console, :render_exception
43
- alias_method :render_exception, :render_exception_with_web_console
25
+ class Binding
26
+ # Instructs Web Console to render a console in the current binding, without
27
+ # the need to unroll the stack.
28
+ #
29
+ # Raises +DoubleRenderError+ if a more than one +console+ invocation per
30
+ # request is detected.
31
+ def console
32
+ Kernel.console(self)
44
33
  end
45
34
  end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WebConsole
4
+ # Injects content into a Rack body.
5
+ class Injector
6
+ def initialize(body, headers)
7
+ @body = "".dup
8
+
9
+ body.each { |part| @body << part }
10
+ body.close if body.respond_to?(:close)
11
+
12
+ @headers = headers
13
+ end
14
+
15
+ def inject(content)
16
+ # Set content-length header to the size of the current body
17
+ # + the extra content. Otherwise the response will be truncated.
18
+ if @headers[Rack::CONTENT_LENGTH]
19
+ @headers[Rack::CONTENT_LENGTH] = (@body.bytesize + content.bytesize).to_s
20
+ end
21
+
22
+ [
23
+ if position = @body.rindex("</body>")
24
+ [ @body.insert(position, content) ]
25
+ else
26
+ [ @body << content ]
27
+ end,
28
+ @headers
29
+ ]
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,17 @@
1
+ module WebConsole
2
+ module Interceptor
3
+ def self.call(request, error)
4
+ backtrace_cleaner = request.get_header("action_dispatch.backtrace_cleaner")
5
+
6
+ # Get the original exception if ExceptionWrapper decides to follow it.
7
+ Thread.current[:__web_console_exception] = error
8
+
9
+ # ActionView::Template::Error bypass ExceptionWrapper original
10
+ # exception following. The backtrace in the view is generated from
11
+ # reaching out to original_exception in the view.
12
+ if error.is_a?(ActionView::Template::Error)
13
+ Thread.current[:__web_console_exception] = error.cause
14
+ end
15
+ end
16
+ end
17
+ end