Class: HttpMimic::JSRuntime
- Inherits:
-
Object
- Object
- HttpMimic::JSRuntime
- Defined in:
- lib/http_mimic/js_runtime.rb
Constant Summary collapse
- STDIN_RUNNER =
<<~JS const input = std.in.readAsString(); const result = eval(input); if (result !== undefined) { if (typeof result === "object" && result !== null) { console.log(JSON.stringify(result)); } else { console.log(result); } } JS
Class Method Summary collapse
- .eval(js_code, timeout: nil, install_dir: nil, auto_download: nil) ⇒ Object
- .eval_json(js_code, timeout: nil, install_dir: nil, auto_download: nil) ⇒ Object
Class Method Details
.eval(js_code, timeout: nil, install_dir: nil, auto_download: nil) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/http_mimic/js_runtime.rb', line 22 def eval(js_code, timeout: nil, install_dir: nil, auto_download: nil) ensure_qjs_installed!(install_dir: install_dir, auto_download: auto_download) qjs_bin = Downloader.qjs_path(install_dir: install_dir) raise BinaryNotFoundError, "QuickJS binary 'qjs' not found. Run HttpMimic.download_qjs! to install." unless qjs_bin cmd = [qjs_bin, '--std', '-e', STDIN_RUNNER] effective_timeout = timeout || HttpMimic.configuration.default_timeout stdout = nil stderr = nil status = nil begin if effective_timeout && effective_timeout > 0 Timeout.timeout(effective_timeout) do stdout, stderr, status = Open3.capture3(*cmd, stdin_data: js_code.to_s) end else stdout, stderr, status = Open3.capture3(*cmd, stdin_data: js_code.to_s) end rescue Timeout::Error raise JSTimeoutError.new("JavaScript execution timed out after #{effective_timeout}s") end unless status && status.success? raise JSError.new( "JavaScript execution failed: #{stderr.to_s.strip}", stderr: stderr, exit_code: status ? status.exitstatus : nil ) end stdout.to_s.strip end |
.eval_json(js_code, timeout: nil, install_dir: nil, auto_download: nil) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/http_mimic/js_runtime.rb', line 58 def eval_json(js_code, timeout: nil, install_dir: nil, auto_download: nil) output = eval(js_code, timeout: timeout, install_dir: install_dir, auto_download: auto_download) return nil if output.nil? || output.empty? JSON.parse(output) rescue JSON::ParserError => e raise ResponseParseError, "Failed to parse JavaScript JSON output: #{e.} (Output: #{output.inspect})" end |