Class: DiffTest::Integrations::RailsJs::BodyProcessor
- Inherits:
-
Object
- Object
- DiffTest::Integrations::RailsJs::BodyProcessor
- Defined in:
- lib/diff_test/integrations/rails_js/body_processor.rb
Constant Summary collapse
- HEAD_TAG_REGEX =
/<head( [^<]+)?>/
Instance Attribute Summary collapse
-
#content_length ⇒ Object
readonly
Returns the value of attribute content_length.
-
#new_body ⇒ Object
readonly
Returns the value of attribute new_body.
Instance Method Summary collapse
-
#initialize(body, options) ⇒ BodyProcessor
constructor
A new instance of BodyProcessor.
- #process!(env) ⇒ Object
- #script ⇒ Object
Constructor Details
#initialize(body, options) ⇒ BodyProcessor
9 10 11 12 |
# File 'lib/diff_test/integrations/rails_js/body_processor.rb', line 9 def initialize(body, ) @body = body = end |
Instance Attribute Details
#content_length ⇒ Object (readonly)
Returns the value of attribute content_length.
7 8 9 |
# File 'lib/diff_test/integrations/rails_js/body_processor.rb', line 7 def content_length @content_length end |
#new_body ⇒ Object (readonly)
Returns the value of attribute new_body.
7 8 9 |
# File 'lib/diff_test/integrations/rails_js/body_processor.rb', line 7 def new_body @new_body end |
Instance Method Details
#process!(env) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/diff_test/integrations/rails_js/body_processor.rb', line 14 def process!(env) @env = env @body.close if @body.respond_to?(:close) @new_body = [] @body.each { |line| @new_body << line.to_s } @content_length = 0 @script_added = false @new_body = @new_body.map do |line| if !@script_added && line['<head'] line = line.gsub(HEAD_TAG_REGEX) { |match| %(#{match}#{script}) } @script_added = true end @content_length += line.bytesize line end end |
#script ⇒ Object
36 37 38 39 40 41 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 |
# File 'lib/diff_test/integrations/rails_js/body_processor.rb', line 36 def script " <script>\n let timeout = null\n var owner = globalThis || window\n owner.diffTestTrackedJsFiles = owner.diffTestTrackedJsFiles || [];\n owner.diffTestTrackJsFile = function(file_path) {\n owner.diffTestTrackedJsFiles.push(file_path);\n if(!timeout) {\n timeout = setTimeout(flushTrackedJsFiles, 0);\n }\n }\n\n owner.diffTestTrackJsFileAuto = owner.diffTestTrackJsFile;\n\n function flushTrackedJsFiles() {\n timeout = null\n const file_paths = owner.diffTestTrackedJsFiles;\n owner.diffTestTrackedJsFiles = [];\n\n const path = \"/__diff_test_js_beacon\";\n const payload = JSON.stringify({ test_id: \#{DiffTest::TestExecution.current.id.to_json}, file_paths: file_paths });\n\n if (owner.navigator && owner.navigator.sendBeacon) {\n // Use sendBeacon in capable browsers/environments\n window.navigator.sendBeacon(path, payload);\n } else {\n // Fallback: Use fetch from Service Worker or other Worker\n fetch(path, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: payload\n }).catch((err) => {\n // Handle error logging or silent fail\n console.error('diff_test_js_beacon_failure', err);\n });\n }\n }\n </script>\n HTML\nend\n" |