parseBridgeEnvelope function

BridgeEnvelope? parseBridgeEnvelope(
  1. String raw
)

Parses one skin→native envelope. Returns null when the message must be dropped: over 64 KB, not a JSON object, or missing type. payload defaults to {} when absent or not an object. Never throws.

Implementation

BridgeEnvelope? parseBridgeEnvelope(String raw) {
  if (utf8.encode(raw).length > gameMaxMessageBytes) return null;
  try {
    final root = jsonDecode(raw);
    if (root is! Map) return null;
    final type = _optString(root['type']);
    if (type.isEmpty) return null;
    final id = _optString(root['id']);
    final payload = root['payload'];
    return BridgeEnvelope(
      id: id.isEmpty ? null : id,
      type: type,
      payload: payload is Map
          ? Map<String, dynamic>.from(payload)
          : <String, dynamic>{},
    );
  } catch (_) {
    return null;
  }
}