Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inject_js incompatible with chunked responses #533

Open
nate-at-gusto opened this issue Apr 8, 2022 · 6 comments
Open

inject_js incompatible with chunked responses #533

nate-at-gusto opened this issue Apr 8, 2022 · 6 comments

Comments

@nate-at-gusto
Copy link
Contributor

When used with an ActionController::Streaming-enabled action (stream: true), something in RMP screws up the chunked encoding while inserting the RMP script tags.

    
  <script async no== Info: Malformed encoding found in chunked-encoding
== Info: Closing connection 0
curl: (56) Malformed encoding found in chunked-encoding
@nate-at-gusto
Copy link
Contributor Author

Relevant TCP dump:

0000   02 00 00 00 45 00 04 4e 00 00 40 00 40 06 00 00   ....E..N..@.@...
0010   7f 00 00 01 7f 00 00 01 0b b8 e4 8d 4b 71 f5 02   ............Kq..
0020   7a 10 07 c5 80 18 18 d5 02 43 00 00 01 01 08 0a   z........C......
0030   d8 89 9d 62 01 4a 1c f9 32 0d 0a 0a 0a 0d 0a 35   ...b.J..2......5
0040   0d 0a 0a 20 20 20 20 0d 0a 39 64 0d 0a 20 20 3c   ...    ..9d..  <
0050   21 2d 2d 20 47 75 73 74 6f 20 41 6e 61 6c 79 74   !-- Gusto Analyt
0060   69 63 73 20 2d 2d 3e 0a 20 20 20 20 3c 73 63 72   ics -->.    <scr
0070   69 70 74 20 73 72 63 3d 22 2f 76 69 74 65 2d 64   ipt src="/vite-d
0080   65 76 2f 70 61 63 6b 73 2f 61 6e 61 6c 79 74 69   ev/packs/analyti
0090   63 73 2e 6a 73 22 20 63 72 6f 73 73 6f 72 69 67   cs.js" crossorig
00a0   69 6e 3d 22 61 6e 6f 6e 79 6d 6f 75 73 22 20 74   in="anonymous" t
00b0   79 70 65 3d 22 64 65 66 65 72 6a 73 22 20 64 65   ype="deferjs" de
00c0   66 65 72 3d 22 64 65 66 65 72 22 20 64 61 74 61   fer="defer" data
00d0   2d 62 75 6e 64 6c 65 72 3d 22 76 69 74 65 22 3e   -bundler="vite">
00e0   3c 2f 73 63 72 69 70 74 3e 0a 0d 0a 36 0d 0a 0a   </script>...6...
00f0   0a 20 20 20 20 0d 0a 31 33 0d 0a 0a 20 20 3c 73   .    ..13...  <s
0100   63 72 69 70 74 20 61 73 79 6e 63 20 6e 6f 6e 63   cript async nonc
0110   65 3d 22 22 20 74 79 70 65 3d 22 74 65 78 74 2f   e="" type="text/
0120   6a 61 76 61 73 63 72 69 70 74 22 20 69 64 3d 22   javascript" id="
0130   6d 69 6e 69 2d 70 72 6f 66 69 6c 65 72 22 20 73   mini-profiler" s
0140   72 63 3d 22 2f 6d 69 6e 69 2d 70 72 6f 66 69 6c   rc="/mini-profil
0150   65 72 2d 72 65 73 6f 75 72 63 65 73 2f 69 6e 63   er-resources/inc
0160   6c 75 64 65 73 2e 6a 73 3f 76 3d 33 35 61 37 39   ludes.js?v=35a79
0170   62 33 30 30 61 62 35 61 66 61 39 37 38 63 62 35   b300ab5afa978cb5
0180   39 61 66 30 62 30 35 65 30 35 39 22 20 64 61 74   9af0b05e059" dat
0190   61 2d 63 73 73 2d 75 72 6c 3d 22 2f 6d 69 6e 69   a-css-url="/mini
01a0   2d 70 72 6f 66 69 6c 65 72 2d 72 65 73 6f 75 72   -profiler-resour
01b0   63 65 73 2f 69 6e 63 6c 75 64 65 73 2e 63 73 73   ces/includes.css
01c0   3f 76 3d 33 35 61 37 39 62 33 30 30 61 62 35 61   ?v=35a79b300ab5a
01d0   66 61 39 37 38 63 62 35 39 61 66 30 62 30 35 65   fa978cb59af0b05e
01e0   30 35 39 22 20 64 61 74 61 2d 76 65 72 73 69 6f   059" data-versio
01f0   6e 3d 22 33 35 61 37 39 62 33 30 30 61 62 35 61   n="35a79b300ab5a
0200   66 61 39 37 38 63 62 35 39 61 66 30 62 30 35 65   fa978cb59af0b05e
0210   30 35 39 22 20 64 61 74 61 2d 70 61 74 68 3d 22   059" data-path="
0220   2f 6d 69 6e 69 2d 70 72 6f 66 69 6c 65 72 2d 72   /mini-profiler-r
0230   65 73 6f 75 72 63 65 73 2f 22 20 64 61 74 61 2d   esources/" data-
0240   63 75 72 72 65 6e 74 2d 69 64 3d 22 6b 38 33 72   current-id="k83r
0250   37 7a 33 78 6e 68 31 63 36 63 6d 7a 6b 70 65 74   7z3xnh1c6cmzkpet
0260   22 20 64 61 74 61 2d 69 64 73 3d 22 6b 38 33 72   " data-ids="k83r
0270   37 7a 33 78 6e 68 31 63 36 63 6d 7a 6b 70 65 74   7z3xnh1c6cmzkpet
0280   2c 36 31 32 36 69 77 39 6c 6d 6f 74 6b 67 68 73   ,6126iw9lmotkghs
0290   76 73 62 77 39 2c 6e 37 39 6a 67 37 6a 33 34 38   vsbw9,n79jg7j348
02a0   6b 74 78 34 7a 38 7a 75 69 68 2c 77 36 31 70 67   ktx4z8zuih,w61pg
02b0   79 67 77 62 34 6b 62 38 74 65 32 30 34 75 78 22   ygwb4kb8te204ux"
02c0   20 64 61 74 61 2d 68 6f 72 69 7a 6f 6e 74 61 6c    data-horizontal
02d0   2d 70 6f 73 69 74 69 6f 6e 3d 22 6c 65 66 74 22   -position="left"
02e0   20 64 61 74 61 2d 76 65 72 74 69 63 61 6c 2d 70    data-vertical-p
02f0   6f 73 69 74 69 6f 6e 3d 22 74 6f 70 22 20 64 61   osition="top" da
0300   74 61 2d 74 72 69 76 69 61 6c 3d 22 66 61 6c 73   ta-trivial="fals
0310   65 22 20 64 61 74 61 2d 63 68 69 6c 64 72 65 6e   e" data-children
0320   3d 22 66 61 6c 73 65 22 20 64 61 74 61 2d 6d 61   ="false" data-ma
0330   78 2d 74 72 61 63 65 73 3d 22 31 32 30 22 20 64   x-traces="120" d
0340   61 74 61 2d 63 6f 6e 74 72 6f 6c 73 3d 22 66 61   ata-controls="fa
0350   6c 73 65 22 20 64 61 74 61 2d 74 6f 74 61 6c 2d   lse" data-total-
0360   73 71 6c 2d 63 6f 75 6e 74 3d 22 74 72 75 65 22   sql-count="true"
0370   20 64 61 74 61 2d 61 75 74 68 6f 72 69 7a 65 64    data-authorized
0380   3d 22 74 72 75 65 22 20 64 61 74 61 2d 74 6f 67   ="true" data-tog
0390   67 6c 65 2d 73 68 6f 72 74 63 75 74 3d 22 61 6c   gle-shortcut="al
03a0   74 2b 70 22 20 64 61 74 61 2d 73 74 61 72 74 2d   t+p" data-start-
03b0   68 69 64 64 65 6e 3d 22 66 61 6c 73 65 22 20 64   hidden="false" d
03c0   61 74 61 2d 63 6f 6c 6c 61 70 73 65 2d 72 65 73   ata-collapse-res
03d0   75 6c 74 73 3d 22 74 72 75 65 22 20 64 61 74 61   ults="true" data
03e0   2d 68 74 6d 6c 2d 63 6f 6e 74 61 69 6e 65 72 3d   -html-container=
03f0   22 62 6f 64 79 22 20 64 61 74 61 2d 68 69 64 64   "body" data-hidd
0400   65 6e 2d 63 75 73 74 6f 6d 2d 66 69 65 6c 64 73   en-custom-fields
0410   3d 22 22 20 64 61 74 61 2d 74 75 72 62 6f 2d 70   ="" data-turbo-p
0420   65 72 6d 61 6e 65 6e 74 3d 22 66 61 6c 73 65 22   ermanent="false"
0430   3e 3c 2f 73 63 72 69 70 74 3e 0a 3c 2f 62 6f 64   ></script>.</bod
0440   79 3e 0a 3c 2f 68 74 6d 6c 3e 0a 0d 0a 30 0d 0a   y>.</html>...0..
0450   0d 0a                                             ..

@nate-at-gusto
Copy link
Contributor Author

@nateberkopec
Copy link
Collaborator

It looks like RMP is just straight up writing to the socket with response.write, and it doesn't write in chunks, which is what causes the problem here.

@nate-at-gusto
Copy link
Contributor Author

By the time RMP sees the response body, it's already been chunk-encoded. 🤔

End of my body looks like:

"13\r\n\n  </body>\n</html>\n\r\n", "0\r\n", "\r\n"

@nate-at-gusto nate-at-gusto changed the title Not compatible with ActionController::Streaming Incompatible with chunked responses Apr 8, 2022
@nate-at-gusto nate-at-gusto changed the title Incompatible with chunked responses inject_js incompatible with chunked responses Apr 8, 2022
@nate-at-gusto
Copy link
Contributor Author

It seems like the workaround would be to inject my own script, but I'm confused. get_profile_script reads like you should be able to call it and disable auto injection on a per-request basis, but I'm not sure how you're supposed to access the Profiler instance so you can call it...

@nateberkopec
Copy link
Collaborator

nateberkopec commented Apr 8, 2022

I was hoping I could do something like

Rack::MiniProfiler.current.inject_js = false 
Rack::MiniProfiler.new(nil, Rack::MiniProfiler.config).get_profile_script(request.env)

...in a template, but the current context is nil.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants