Skip to content

Commit 23d0f7a

Browse files
Fix WebSocket send function buffer handling
Refactor WebSocket send function to handle ArrayBuffer and ArrayBufferView correctly.
1 parent fb05cc7 commit 23d0f7a

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

‎src/WebSocketWrapper.h‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -341,15 +341,26 @@ template <bool SSL>
341341
static uint32_t uWS_WebSocket_send_fast_buffer(v8::Local<v8::Object> receiver,
342342
v8::Local<v8::Value> message,
343343
bool isBinary, bool compress) {
344-
printf("Hitting fast buffer path\n");
345344
auto *ws = (uWS::WebSocket<SSL, true, PerSocketData> *) receiver->GetAlignedPointerFromInternalField(0);
346-
if (!ws || !message->IsArrayBufferView()) return 0;
345+
if (!ws) return 0;
347346

348-
auto array = message.As<v8::ArrayBufferView>();
349-
auto contents = array->Buffer()->GetBackingStore();
350-
char* data = static_cast<char*>(contents->Data()) + array->ByteOffset();
347+
char* data = nullptr;
348+
size_t length = 0;
349+
350+
if (message->IsArrayBufferView()) {
351+
auto view = message.As<v8::ArrayBufferView>();
352+
length = view->ByteLength();
353+
data = static_cast<char*>(view->Buffer()->GetBackingStore()->Data()) + view->ByteOffset();
354+
} else if (message->IsArrayBuffer()) {
355+
auto ab = message.As<v8::ArrayBuffer>();
356+
length = ab->ByteLength();
357+
data = static_cast<char*>(ab->GetBackingStore()->Data());
358+
} else {
359+
// Not a buffer type we handle fast
360+
return 0;
361+
}
351362

352-
return ws->send(std::string_view(data, array->ByteLength()),
363+
return ws->send(std::string_view(data, length),
353364
isBinary ? uWS::OpCode::BINARY : uWS::OpCode::TEXT, compress);
354365
}
355366

0 commit comments

Comments
 (0)