Module: Ruact::Testing::FlightExtractor
- Defined in:
- lib/ruact/testing/flight_extractor.rb
Overview
Pulls the Flight wire byte string out of whatever a host request spec
hands the have_ruact_component matcher: an ActionDispatch/Rack response
object (read via .body) or a raw String, in either of the two page
shapes ruact emits —
1. a raw `text/x-component` body (RSC/navigation request), OR
2. a full HTML document embedding the payload in the `__FLIGHT_DATA`
bootstrap `<script>` (a plain browser GET).
A Ruact::Server function-call/query answer is plain JSON, not Flight —
feeding it here raises NotAFlightResponseError pointing at JSON.parse,
rather than silently failing to parse. Pure: no I/O, no global state.
Constant Summary collapse
- FLIGHT_DATA_PUSH =
Matches the
<local>.push("<ruby-inspected literal>")line the__FLIGHT_DATAbootstrap script emits (seeRuact::ViewHelper#ruact_flight_data_script). ANCHORED to the queue assignment (self.__FLIGHT_DATA = self.__FLIGHT_DATA || [])) so an unrelated earlier script on the page (analyticsdataLayer.push(…), etc.) can never be mistaken for the Flight payload. The captured group is the full double-quoted Ruby string literal (quotes included), honoring escaped quotes so a\"inside the payload does not end it. / self\.__FLIGHT_DATA\s*=\s*self\.__FLIGHT_DATA\s*\|\|\s*\[\]\)\s*; # queue assignment \s*[A-Za-z_$][\w$]*\.push\( # <local>.push( (?<literal>"(?:\\.|[^"\\])*") \)\s*; /mx- FLIGHT_ROW_START =
A Flight wire body always starts with a row header:
<hex>:(id + colon) or a hint row:H. JSON bodies start with{/[/"; HTML with<. /\A\s*(?:\h+:|:H)/- SIMPLE_ESCAPES =
Reverses
String#inspecton the captured__FLIGHT_DATAliteral.JSON.parseis unsafe here: Ruby's inspect escapes sequences JSON rejects (\#before{/$/@,\e,\a, …). These are the common single-char escapes; an unrecognized\cyields the literalc. { "n" => "\n", "t" => "\t", "r" => "\r", "s" => " ", "0" => "\0", "a" => "\a", "b" => "\b", "e" => "\e", "f" => "\f", "v" => "\v", '"' => '"', "\\" => "\\" }.freeze
Class Method Summary collapse
-
.extract(response_or_string) ⇒ String
The extracted Flight wire byte string.
Class Method Details
.extract(response_or_string) ⇒ String
Returns the extracted Flight wire byte string.
59 60 61 62 63 64 |
# File 'lib/ruact/testing/flight_extractor.rb', line 59 def extract(response_or_string) body, content_type = read(response_or_string) return extract_by_content_type(body, content_type) if content_type extract_by_sniffing(body) end |