LibRequests+LibWeb: Don't copy data sent over WebSockets

We used to need an allocated ByteBuffer to send over IPC. Our IPC now
accepts spans for types like String and ByteBuffer though, so we don't
need to allocate buffers at the caller.
This commit is contained in:
Timothy Flynn
2025-12-10 08:01:16 -05:00
committed by Jelle Raaijmakers
parent 7114872073
commit 8a79792a58
Notes: github-actions[bot] 2025-12-10 20:19:49 +00:00
3 changed files with 18 additions and 21 deletions

View File

@@ -308,24 +308,21 @@ WebIDL::ExceptionOr<void> WebSocket::send(Variant<GC::Root<WebIDL::BufferSource>
if (state == Requests::WebSocket::ReadyState::Connecting)
return WebIDL::InvalidStateError::create(realm(), "Websocket is still CONNECTING"_utf16);
if (state == Requests::WebSocket::ReadyState::Open) {
TRY_OR_THROW_OOM(vm(),
data.visit(
[this](String const& string) -> ErrorOr<void> {
m_websocket->send(string);
return {};
},
[this](GC::Root<WebIDL::BufferSource> const& buffer_source) -> ErrorOr<void> {
// FIXME: While the spec doesn't say to do this, it's not observable except from potentially throwing OOM.
// Can we avoid this copy?
auto data_buffer = TRY(WebIDL::get_buffer_source_copy(*buffer_source->raw_object()));
m_websocket->send(data_buffer, false);
return {};
},
[this](GC::Root<FileAPI::Blob> const& blob) -> ErrorOr<void> {
auto byte_buffer = TRY(ByteBuffer::copy(blob->raw_bytes()));
m_websocket->send(byte_buffer, false);
return {};
}));
data.visit(
[this](String const& string) {
m_websocket->send(string);
},
[this](GC::Root<WebIDL::BufferSource> const& buffer_source) {
ReadonlyBytes buffer;
if (auto array_buffer = buffer_source->viewed_array_buffer(); array_buffer && !array_buffer->is_detached())
buffer = array_buffer->buffer();
m_websocket->send(buffer, false);
},
[this](GC::Root<FileAPI::Blob> const& blob) {
m_websocket->send(blob->raw_bytes(), false);
});
// TODO : If the data cannot be sent, e.g. because it would need to be buffered but the buffer is full, the user agent must flag the WebSocket as full and then close the WebSocket connection.
// TODO : Any invocation of this method with a string argument that does not throw an exception must increase the bufferedAmount attribute by the number of bytes needed to express the argument as UTF-8.
}