diff options
author | Luke Wilde <lukew@serenityos.org> | 2023-04-20 19:15:21 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-04-21 07:59:50 +0200 |
commit | 8addcd237c9c5ba89734e2dfbb541ecc69287e7e (patch) | |
tree | e8f50735457a6e30a30b44f378a8b700ca35bf56 /Userland/Libraries/LibWeb | |
parent | 1b6492d0fbacf2a99b5e8a81ccdf6de202189ba3 (diff) | |
download | serenity-8addcd237c9c5ba89734e2dfbb541ecc69287e7e.zip |
LibWeb: Make WebSocket#send support typed arrays, Blob and DataView
Required by jigsawpuzzles.io, which particularly uses typed arrays.
Diffstat (limited to 'Userland/Libraries/LibWeb')
-rw-r--r-- | Userland/Libraries/LibWeb/WebSockets/WebSocket.cpp | 23 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/WebSockets/WebSocket.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/WebSockets/WebSocket.idl | 6 |
3 files changed, 23 insertions, 8 deletions
diff --git a/Userland/Libraries/LibWeb/WebSockets/WebSocket.cpp b/Userland/Libraries/LibWeb/WebSockets/WebSocket.cpp index 05b9432814..6814ac7a5a 100644 --- a/Userland/Libraries/LibWeb/WebSockets/WebSocket.cpp +++ b/Userland/Libraries/LibWeb/WebSockets/WebSocket.cpp @@ -11,12 +11,14 @@ #include <LibWeb/DOM/Event.h> #include <LibWeb/DOM/EventDispatcher.h> #include <LibWeb/DOM/IDLEventListener.h> +#include <LibWeb/FileAPI/Blob.h> #include <LibWeb/HTML/CloseEvent.h> #include <LibWeb/HTML/EventHandler.h> #include <LibWeb/HTML/EventNames.h> #include <LibWeb/HTML/MessageEvent.h> #include <LibWeb/HTML/Origin.h> #include <LibWeb/HTML/Window.h> +#include <LibWeb/WebIDL/AbstractOperations.h> #include <LibWeb/WebIDL/DOMException.h> #include <LibWeb/WebIDL/ExceptionOr.h> #include <LibWeb/WebSockets/WebSocket.h> @@ -185,13 +187,30 @@ WebIDL::ExceptionOr<void> WebSocket::close(Optional<u16> code, Optional<Deprecat } // https://websockets.spec.whatwg.org/#dom-websocket-send -WebIDL::ExceptionOr<void> WebSocket::send(DeprecatedString const& data) +WebIDL::ExceptionOr<void> WebSocket::send(Variant<JS::Handle<JS::Object>, JS::Handle<FileAPI::Blob>, DeprecatedString> const& data) { auto state = ready_state(); if (state == WebSocket::ReadyState::Connecting) return WebIDL::InvalidStateError::create(realm(), "Websocket is still CONNECTING"); if (state == WebSocket::ReadyState::Open) { - m_websocket->send(data); + TRY_OR_THROW_OOM(vm(), + data.visit( + [this](DeprecatedString const& string) -> ErrorOr<void> { + m_websocket->send(string); + return {}; + }, + [this](JS::Handle<JS::Object> 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.cell())); + m_websocket->send(data_buffer, false); + return {}; + }, + [this](JS::Handle<FileAPI::Blob> const& blob) -> ErrorOr<void> { + auto byte_buffer = TRY(ByteBuffer::copy(blob->bytes())); + m_websocket->send(byte_buffer, false); + return {}; + })); // 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. } diff --git a/Userland/Libraries/LibWeb/WebSockets/WebSocket.h b/Userland/Libraries/LibWeb/WebSockets/WebSocket.h index 40b3d185a4..31b5b4050a 100644 --- a/Userland/Libraries/LibWeb/WebSockets/WebSocket.h +++ b/Userland/Libraries/LibWeb/WebSockets/WebSocket.h @@ -58,7 +58,7 @@ public: void set_binary_type(DeprecatedString const& type) { m_binary_type = type; }; WebIDL::ExceptionOr<void> close(Optional<u16> code, Optional<DeprecatedString> reason); - WebIDL::ExceptionOr<void> send(DeprecatedString const& data); + WebIDL::ExceptionOr<void> send(Variant<JS::Handle<JS::Object>, JS::Handle<FileAPI::Blob>, DeprecatedString> const& data); private: void on_open(); diff --git a/Userland/Libraries/LibWeb/WebSockets/WebSocket.idl b/Userland/Libraries/LibWeb/WebSockets/WebSocket.idl index c857e07763..97ce64abad 100644 --- a/Userland/Libraries/LibWeb/WebSockets/WebSocket.idl +++ b/Userland/Libraries/LibWeb/WebSockets/WebSocket.idl @@ -25,9 +25,5 @@ interface WebSocket : EventTarget { attribute EventHandler onmessage; attribute DOMString binaryType; - undefined send(USVString data); - // FIXME: Support other kinds of send() calls - // undefined send(Blob data); - // undefined send(ArrayBuffer data); - // undefined send(ArrayBufferView data); + undefined send((BufferSource or Blob or USVString) data); }; |