Class: MilkTea::Run::WasmPreviewServer
- Inherits:
-
Object
- Object
- MilkTea::Run::WasmPreviewServer
- Defined in:
- lib/milk_tea/tooling/run.rb
Constant Summary collapse
- HOST =
"127.0.0.1"- DEFAULT_IDLE_TIMEOUT =
600
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
- #close_server ⇒ Object
- #default_entry_name ⇒ Object
- #handle_client(socket) ⇒ Object
-
#initialize(root_dir:, host: HOST, port: nil, idle_timeout: DEFAULT_IDLE_TIMEOUT) ⇒ WasmPreviewServer
constructor
A new instance of WasmPreviewServer.
- #install_signal_handlers ⇒ Object
- #listen! ⇒ Object
- #mime_type_for(path) ⇒ Object
- #resolve_request_path(target) ⇒ Object
- #restore_signal_handlers(previous_handlers) ⇒ Object
- #serve_forever(trap_signals: true) ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #url_for(entry_name) ⇒ Object
- #wake_server ⇒ Object
- #write_response(socket, status, message, body) ⇒ Object
Constructor Details
#initialize(root_dir:, host: HOST, port: nil, idle_timeout: DEFAULT_IDLE_TIMEOUT) ⇒ WasmPreviewServer
Returns a new instance of WasmPreviewServer.
15 16 17 18 19 20 21 22 23 |
# File 'lib/milk_tea/tooling/run.rb', line 15 def initialize(root_dir:, host: HOST, port: nil, idle_timeout: DEFAULT_IDLE_TIMEOUT) @root_dir = File.(root_dir) @host = host @port = port @idle_timeout = idle_timeout @thread = nil @server = nil @running = false end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
25 26 27 |
# File 'lib/milk_tea/tooling/run.rb', line 25 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
25 26 27 |
# File 'lib/milk_tea/tooling/run.rb', line 25 def port @port end |
Instance Method Details
#close_server ⇒ Object
200 201 202 203 204 205 206 |
# File 'lib/milk_tea/tooling/run.rb', line 200 def close_server @server&.close rescue IOError, Errno::EBADF nil ensure @server = nil end |
#default_entry_name ⇒ Object
136 137 138 139 |
# File 'lib/milk_tea/tooling/run.rb', line 136 def default_entry_name html_entries = Dir.children(@root_dir).grep(/\.html\z/).sort html_entries.first || "index.html" end |
#handle_client(socket) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/milk_tea/tooling/run.rb', line 90 def handle_client(socket) request_line = socket.gets return unless request_line method, target, = request_line.split(" ", 3) while (header = socket.gets) break if header == "\r\n" end unless ["GET", "HEAD"].include?(method) write_response(socket, 405, "Method Not Allowed", "method not allowed\n") return end path = resolve_request_path(target) unless path && File.file?(path) write_response(socket, 404, "Not Found", "not found\n") return end body = File.binread(path) headers = { "Content-Type" => mime_type_for(path), "Content-Length" => body.bytesize.to_s, "Cache-Control" => "no-cache", "Cross-Origin-Opener-Policy" => "same-origin", "Cross-Origin-Embedder-Policy" => "require-corp", "Connection" => "close", } socket.write("HTTP/1.1 200 OK\r\n") headers.each { |name, value| socket.write("#{name}: #{value}\r\n") } socket.write("\r\n") socket.write(body) if method == "GET" end |
#install_signal_handlers ⇒ Object
176 177 178 179 180 181 |
# File 'lib/milk_tea/tooling/run.rb', line 176 def install_signal_handlers { "TERM" => Signal.trap("TERM") { @running = false }, "INT" => Signal.trap("INT") { @running = false }, } end |
#listen! ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/milk_tea/tooling/run.rb', line 47 def listen! return self if @server @server = TCPServer.new(@host, @port || 0) @port = @server.addr[1] self end |
#mime_type_for(path) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/milk_tea/tooling/run.rb', line 141 def mime_type_for(path) case File.extname(path) when ".html" "text/html; charset=utf-8" when ".js" "text/javascript; charset=utf-8" when ".css" "text/css; charset=utf-8" when ".json", ".mtdbg.json" "application/json; charset=utf-8" when ".wasm" "application/wasm" when ".wav" "audio/wav" when ".png" "image/png" when ".ppm" "image/x-portable-pixmap" else "application/octet-stream" end end |
#resolve_request_path(target) ⇒ Object
126 127 128 129 130 131 132 133 134 |
# File 'lib/milk_tea/tooling/run.rb', line 126 def resolve_request_path(target) request_path = target.to_s.split("?", 2).first request_path = "/" if request_path.empty? relative = request_path == "/" ? default_entry_name : request_path.delete_prefix("/") candidate = File.(relative, @root_dir) return nil unless candidate.start_with?(@root_dir + File::SEPARATOR) || candidate == @root_dir candidate end |
#restore_signal_handlers(previous_handlers) ⇒ Object
183 184 185 186 187 188 189 |
# File 'lib/milk_tea/tooling/run.rb', line 183 def restore_signal_handlers(previous_handlers) previous_handlers.each do |signal_name, handler| Signal.trap(signal_name, handler) end rescue ArgumentError nil end |
#serve_forever(trap_signals: true) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/milk_tea/tooling/run.rb', line 61 def serve_forever(trap_signals: true) listen! @running = true previous_handlers = trap_signals ? install_signal_handlers : nil last_request_at = Time.now while @running if @idle_timeout.positive? && Time.now - last_request_at > @idle_timeout break end readable, = IO.select([@server], nil, nil, 0.25) next unless readable client = @server.accept Thread.new(client) do |socket| begin handle_client(socket) ensure last_request_at = Time.now socket.close unless socket.closed? end end end ensure close_server restore_signal_handlers(previous_handlers) if previous_handlers end |
#start ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/milk_tea/tooling/run.rb', line 27 def start listen! @thread = Thread.new { serve_forever(trap_signals: false) } self rescue StandardError stop raise end |
#stop ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/milk_tea/tooling/run.rb', line 36 def stop @running = false if @thread wake_server @thread.join(1) end @thread = nil close_server nil end |
#url_for(entry_name) ⇒ Object
55 56 57 58 59 |
# File 'lib/milk_tea/tooling/run.rb', line 55 def url_for(entry_name) raise RunError, "wasm preview server has not been started" unless @port "http://#{@host}:#{@port}/#{entry_name}" end |
#wake_server ⇒ Object
191 192 193 194 195 196 197 198 |
# File 'lib/milk_tea/tooling/run.rb', line 191 def wake_server return unless @server && @port socket = TCPSocket.new(@host, @port) socket.close rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH nil end |
#write_response(socket, status, message, body) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/milk_tea/tooling/run.rb', line 164 def write_response(socket, status, , body) body ||= "" socket.write("HTTP/1.1 #{status} #{}\r\n") socket.write("Content-Type: text/plain; charset=utf-8\r\n") socket.write("Content-Length: #{body.bytesize}\r\n") socket.write("Cross-Origin-Opener-Policy: same-origin\r\n") socket.write("Cross-Origin-Embedder-Policy: require-corp\r\n") socket.write("Connection: close\r\n") socket.write("\r\n") socket.write(body) end |