mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-04-25 17:25:08 +02:00
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:
committed by
Jelle Raaijmakers
parent
7114872073
commit
8a79792a58
Notes:
github-actions[bot]
2025-12-10 20:19:49 +00:00
Author: https://github.com/trflynn89 Commit: https://github.com/LadybirdBrowser/ladybird/commit/8a79792a583 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/7086 Reviewed-by: https://github.com/gmta ✅
@@ -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.
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user