truncate output
This commit is contained in:
@@ -43,7 +43,7 @@ pub async fn run_loop(
|
||||
}
|
||||
}
|
||||
message = socket.next() => {
|
||||
match handle_socket_message(socket, message, &pending).await {
|
||||
match handle_socket_message(socket, message, &pending, config.max_result_bytes).await {
|
||||
SocketAction::Break => break,
|
||||
SocketAction::ClearPongDeadline => awaiting_pong = false,
|
||||
SocketAction::Continue => {}
|
||||
@@ -65,6 +65,7 @@ async fn handle_socket_message(
|
||||
socket: &mut DuplexStream,
|
||||
message: Option<Result<Message, rocket_ws::result::Error>>,
|
||||
pending: &PendingReplies,
|
||||
max_result_bytes: usize,
|
||||
) -> SocketAction {
|
||||
match message {
|
||||
Some(Ok(Message::Pong(_))) => SocketAction::ClearPongDeadline,
|
||||
@@ -79,11 +80,13 @@ async fn handle_socket_message(
|
||||
if let Ok(ClientMsg::ExecResult {
|
||||
exec_id,
|
||||
exit_code,
|
||||
stdout,
|
||||
stderr,
|
||||
mut stdout,
|
||||
mut stderr,
|
||||
}) = serde_json::from_str(&text)
|
||||
&& let Some((_, reply)) = pending.remove(&exec_id)
|
||||
{
|
||||
truncate_utf8(&mut stdout, max_result_bytes);
|
||||
truncate_utf8(&mut stderr, max_result_bytes);
|
||||
reply
|
||||
.send(ExecResult {
|
||||
exit_code,
|
||||
@@ -100,6 +103,19 @@ async fn handle_socket_message(
|
||||
}
|
||||
}
|
||||
|
||||
fn truncate_utf8(text: &mut String, max_bytes: usize) {
|
||||
if text.len() <= max_bytes {
|
||||
return;
|
||||
}
|
||||
|
||||
let mut end = max_bytes;
|
||||
while end > 0 && !text.is_char_boundary(end) {
|
||||
end -= 1;
|
||||
}
|
||||
|
||||
text.truncate(end);
|
||||
}
|
||||
|
||||
async fn dispatch(
|
||||
socket: &mut DuplexStream,
|
||||
pending: &PendingReplies,
|
||||
|
||||
Reference in New Issue
Block a user