Module: Plushie::Protocol::Encode
- Defined in:
- lib/plushie/protocol/encode.rb
Overview
Outbound message encoding for the wire protocol.
Every method produces wire-ready bytes (iodata for msgpack, JSONL string for json). The session field defaults to "" and is injected by the Connection or SessionPool when multiplexing.
Class Method Summary collapse
-
.encode(map, format = :msgpack) ⇒ String
Encode an arbitrary hash as wire-format bytes.
-
.encode_advance_frame(timestamp, format = :msgpack) ⇒ String
Advance the animation clock by one frame.
-
.encode_binary(data, format) ⇒ String
Encode binary data for the wire format.
-
.encode_binary_field(payload, key, format) ⇒ Hash
Encode a binary field in a payload hash if present.
-
.encode_effect(id, kind, payload, format = :msgpack) ⇒ String
Request a platform effect (file dialog, clipboard, notification).
-
.encode_extension_command(node_id, op, payload, format = :msgpack) ⇒ String
Send a command directly to a native widget extension.
-
.encode_extension_commands(commands, format = :msgpack) ⇒ String
Send multiple extension commands in a single message.
-
.encode_image_op(op, payload, format = :msgpack) ⇒ String
Manage in-memory image handles (create, update, delete).
-
.encode_interact(id, action, selector = nil, payload = {}, format = :msgpack) ⇒ String
Simulate a user interaction (click, type, toggle, etc.).
-
.encode_patch(ops, format = :msgpack) ⇒ String
Incrementally patch the existing tree.
-
.encode_query(id, target, selector = {}, format = :msgpack) ⇒ String
Query the renderer's tree (find widget, get full tree).
-
.encode_reset(id, format = :msgpack) ⇒ String
Reset all session state.
-
.encode_screenshot(id, name, width = 1024, height = 768, format = :msgpack) ⇒ String
Capture rendered pixels.
-
.encode_settings(settings, format = :msgpack) ⇒ String
Encode application-level settings.
-
.encode_snapshot(tree, format = :msgpack) ⇒ String
Replace the entire UI tree.
-
.encode_subscribe(kind, tag, format = :msgpack, max_rate: nil) ⇒ String
Subscribe to an event category.
-
.encode_tree_hash(id, name, format = :msgpack) ⇒ String
Compute a SHA-256 hash of the renderer's current tree.
-
.encode_unsubscribe(kind, format = :msgpack) ⇒ String
Unsubscribe from an event category.
-
.encode_widget_op(op, payload, format = :msgpack) ⇒ String
Perform an operation on a widget (focus, scroll, etc.).
-
.encode_window_op(op, window_id, settings, format = :msgpack) ⇒ String
Manage a window (open, close, resize, etc.).
-
.stringify_keys(obj) ⇒ Object
Recursively convert all symbol keys to strings.
Class Method Details
.encode(map, format = :msgpack) ⇒ String
Encode an arbitrary hash as wire-format bytes.
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/plushie/protocol/encode.rb', line 23 def encode(map, format = :msgpack) case format when :json JSON.generate(stringify_keys(map)) + "\n" when :msgpack require "msgpack" MessagePack.pack(stringify_keys(map)) else raise ArgumentError, "unknown format: #{format.inspect}" end end |
.encode_advance_frame(timestamp, format = :msgpack) ⇒ String
Advance the animation clock by one frame.
312 313 314 |
# File 'lib/plushie/protocol/encode.rb', line 312 def encode_advance_frame(, format = :msgpack) encode({type: "advance_frame", session: "", timestamp: }, format) end |
.encode_binary(data, format) ⇒ String
Encode binary data for the wire format. JSON: base64-encoded string. MessagePack: raw binary pass-through.
341 342 343 344 345 346 |
# File 'lib/plushie/protocol/encode.rb', line 341 def encode_binary(data, format) case format when :json then Base64.strict_encode64(data) when :msgpack then data end end |
.encode_binary_field(payload, key, format) ⇒ Hash
Encode a binary field in a payload hash if present. Returns a new hash with the field encoded for the wire format, or the original hash if the field is absent or nil.
356 357 358 359 |
# File 'lib/plushie/protocol/encode.rb', line 356 def encode_binary_field(payload, key, format) return payload unless payload.is_a?(Hash) && payload.key?(key) && payload[key].is_a?(String) payload.merge(key => encode_binary(payload[key], format)) end |
.encode_effect(id, kind, payload, format = :msgpack) ⇒ String
Request a platform effect (file dialog, clipboard, notification).
153 154 155 156 157 158 |
# File 'lib/plushie/protocol/encode.rb', line 153 def encode_effect(id, kind, payload, format = :msgpack) encode({ type: "effect", session: "", id: id, kind: kind.to_s, payload: payload }, format) end |
.encode_extension_command(node_id, op, payload, format = :msgpack) ⇒ String
Send a command directly to a native widget extension.
201 202 203 204 205 206 |
# File 'lib/plushie/protocol/encode.rb', line 201 def encode_extension_command(node_id, op, payload, format = :msgpack) encode({ type: "extension_command", session: "", node_id: node_id, op: op.to_s, payload: payload }, format) end |
.encode_extension_commands(commands, format = :msgpack) ⇒ String
Send multiple extension commands in a single message.
213 214 215 216 217 218 219 220 |
# File 'lib/plushie/protocol/encode.rb', line 213 def encode_extension_commands(commands, format = :msgpack) encode({ type: "extension_commands", session: "", commands: commands.map { |c| {node_id: c[:node_id], op: c[:op].to_s, payload: c[:payload] || {}} } }, format) end |
.encode_image_op(op, payload, format = :msgpack) ⇒ String
Manage in-memory image handles (create, update, delete).
Binary fields (data, pixels) are base64-encoded for JSON and passed as raw binary for MessagePack.
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/plushie/protocol/encode.rb', line 173 def encode_image_op(op, payload, format = :msgpack) msg = {type: "image_op", session: "", op: op.to_s} msg[:handle] = payload[:handle] if payload[:handle] if payload[:data] msg[:data] = encode_binary(payload[:data], format) end if payload[:pixels] msg[:pixels] = encode_binary(payload[:pixels], format) msg[:width] = payload[:width] msg[:height] = payload[:height] end encode(msg, format) end |
.encode_interact(id, action, selector = nil, payload = {}, format = :msgpack) ⇒ String
Simulate a user interaction (click, type, toggle, etc.).
252 253 254 255 256 257 258 259 |
# File 'lib/plushie/protocol/encode.rb', line 252 def encode_interact(id, action, selector = nil, payload = {}, format = :msgpack) msg = { type: "interact", session: "", id: id, action: action.to_s, payload: payload } msg[:selector] = selector if selector encode(msg, format) end |
.encode_patch(ops, format = :msgpack) ⇒ String
Incrementally patch the existing tree.
75 76 77 |
# File 'lib/plushie/protocol/encode.rb', line 75 def encode_patch(ops, format = :msgpack) encode({type: "patch", session: "", ops: ops}, format) end |
.encode_query(id, target, selector = {}, format = :msgpack) ⇒ String
Query the renderer's tree (find widget, get full tree).
233 234 235 236 237 238 |
# File 'lib/plushie/protocol/encode.rb', line 233 def encode_query(id, target, selector = {}, format = :msgpack) encode({ type: "query", session: "", id: id, target: target, selector: selector }, format) end |
.encode_reset(id, format = :msgpack) ⇒ String
Reset all session state.
299 300 301 |
# File 'lib/plushie/protocol/encode.rb', line 299 def encode_reset(id, format = :msgpack) encode({type: "reset", session: "", id: id}, format) end |
.encode_screenshot(id, name, width = 1024, height = 768, format = :msgpack) ⇒ String
Capture rendered pixels.
283 284 285 286 287 288 |
# File 'lib/plushie/protocol/encode.rb', line 283 def encode_screenshot(id, name, width = 1024, height = 768, format = :msgpack) encode({ type: "screenshot", session: "", id: id, name: name, width: width, height: height }, format) end |
.encode_settings(settings, format = :msgpack) ⇒ String
Encode application-level settings. Sent as the first message.
All fields inside settings are optional. See protocol.md for the full list: protocol_version, default_text_size, default_font, antialiasing, vsync, fonts, scale_factor, validate_props, extension_config, default_event_rate.
49 50 51 52 53 54 55 |
# File 'lib/plushie/protocol/encode.rb', line 49 def encode_settings(settings, format = :msgpack) encode({ type: "settings", session: "", settings: {protocol_version: Protocol::PROTOCOL_VERSION}.merge(settings) }, format) end |
.encode_snapshot(tree, format = :msgpack) ⇒ String
Replace the entire UI tree.
66 67 68 |
# File 'lib/plushie/protocol/encode.rb', line 66 def encode_snapshot(tree, format = :msgpack) encode({type: "snapshot", session: "", tree: tree}, format) end |
.encode_subscribe(kind, tag, format = :msgpack, max_rate: nil) ⇒ String
Subscribe to an event category.
90 91 92 93 94 |
# File 'lib/plushie/protocol/encode.rb', line 90 def encode_subscribe(kind, tag, format = :msgpack, max_rate: nil) msg = {type: "subscribe", session: "", kind: kind.to_s, tag: tag.to_s} msg[:max_rate] = max_rate if max_rate encode(msg, format) end |
.encode_tree_hash(id, name, format = :msgpack) ⇒ String
Compute a SHA-256 hash of the renderer's current tree.
271 272 273 |
# File 'lib/plushie/protocol/encode.rb', line 271 def encode_tree_hash(id, name, format = :msgpack) encode({type: "tree_hash", session: "", id: id, name: name}, format) end |
.encode_unsubscribe(kind, format = :msgpack) ⇒ String
Unsubscribe from an event category.
101 102 103 |
# File 'lib/plushie/protocol/encode.rb', line 101 def encode_unsubscribe(kind, format = :msgpack) encode({type: "unsubscribe", session: "", kind: kind.to_s}, format) end |
.encode_widget_op(op, payload, format = :msgpack) ⇒ String
Perform an operation on a widget (focus, scroll, etc.).
Binary fields (e.g. :data for load_font) are automatically base64-encoded for JSON and passed as raw binary for msgpack.
118 119 120 121 122 |
# File 'lib/plushie/protocol/encode.rb', line 118 def (op, payload, format = :msgpack) # Encode binary fields if present (load_font sends raw TTF/OTF data) payload = encode_binary_field(payload, :data, format) encode({type: "widget_op", session: "", op: op.to_s, payload: payload}, format) end |
.encode_window_op(op, window_id, settings, format = :msgpack) ⇒ String
Manage a window (open, close, resize, etc.).
135 136 137 138 139 140 |
# File 'lib/plushie/protocol/encode.rb', line 135 def encode_window_op(op, window_id, settings, format = :msgpack) encode({ type: "window_op", session: "", op: op.to_s, window_id: window_id, settings: settings }, format) end |
.stringify_keys(obj) ⇒ Object
Recursively convert all symbol keys to strings.
324 325 326 327 328 329 330 331 332 333 |
# File 'lib/plushie/protocol/encode.rb', line 324 def stringify_keys(obj) case obj when Hash obj.each_with_object({}) { |(k, v), h| h[k.to_s] = stringify_keys(v) } when Array obj.map { |v| stringify_keys(v) } else obj end end |