diff --git a/src/HttpResponseWrapper.h b/src/HttpResponseWrapper.h index 4a7f7e5..0fd1abb 100644 --- a/src/HttpResponseWrapper.h +++ b/src/HttpResponseWrapper.h @@ -106,8 +106,7 @@ struct HttpResponseWrapper { if (res) { std::string_view ip = res->getRemoteAddress(); - /* Todo: we need to pass a copy here */ - args.GetReturnValue().Set(ArrayBuffer_New(isolate, (void *) ip.data(), ip.length()/*, ArrayBufferCreationMode::kInternalized*/)); + args.GetReturnValue().Set(ArrayBuffer_NewCopy(isolate, (void *) ip.data(), ip.length())); } } @@ -119,8 +118,7 @@ struct HttpResponseWrapper { if (res) { std::string_view ip = res->getRemoteAddressAsText(); - /* Todo: we need to pass a copy here */ - args.GetReturnValue().Set(ArrayBuffer_New(isolate, (void *) ip.data(), ip.length()/*, ArrayBufferCreationMode::kInternalized*/)); + args.GetReturnValue().Set(ArrayBuffer_NewCopy(isolate, (void *) ip.data(), ip.length())); } } @@ -132,8 +130,7 @@ struct HttpResponseWrapper { if (res) { std::string_view ip = res->getProxiedRemoteAddress(); - /* Todo: we need to pass a copy here */ - args.GetReturnValue().Set(ArrayBuffer_New(isolate, (void *) ip.data(), ip.length()/*, ArrayBufferCreationMode::kInternalized*/)); + args.GetReturnValue().Set(ArrayBuffer_NewCopy(isolate, (void *) ip.data(), ip.length())); } } @@ -145,8 +142,7 @@ struct HttpResponseWrapper { if (res) { std::string_view ip = res->getProxiedRemoteAddressAsText(); - /* Todo: we need to pass a copy here */ - args.GetReturnValue().Set(ArrayBuffer_New(isolate, (void *) ip.data(), ip.length()/*, ArrayBufferCreationMode::kInternalized*/)); + args.GetReturnValue().Set(ArrayBuffer_NewCopy(isolate, (void *) ip.data(), ip.length())); } } diff --git a/src/Utilities.h b/src/Utilities.h index 1dd630e..fbc1435 100644 --- a/src/Utilities.h +++ b/src/Utilities.h @@ -34,6 +34,12 @@ Local ArrayBuffer_New(Isolate *isolate, void *data, size_t leng return ArrayBuffer::New(isolate, std::shared_ptr(backingStore.release())); } +Local ArrayBuffer_NewCopy(Isolate *isolate, void *data, size_t length) { + Local ab = ArrayBuffer::New(isolate, length); + memcpy(ab->GetBackingStore()->Data(), data, length); + return ab; +} + struct PerSocketData { UniquePersistent socketPf; }; diff --git a/src/WebSocketWrapper.h b/src/WebSocketWrapper.h index d2d9850..b42c929 100644 --- a/src/WebSocketWrapper.h +++ b/src/WebSocketWrapper.h @@ -136,8 +136,7 @@ struct WebSocketWrapper { if (ws) { std::string_view ip = ws->getRemoteAddress(); - /* Todo: we need to pass a copy here */ - args.GetReturnValue().Set(ArrayBuffer_New(isolate, (void *) ip.data(), ip.length()/*, ArrayBufferCreationMode::kInternalized*/)); + args.GetReturnValue().Set(ArrayBuffer_NewCopy(isolate, (void *) ip.data(), ip.length())); } } @@ -149,8 +148,7 @@ struct WebSocketWrapper { if (ws) { std::string_view ip = ws->getRemoteAddressAsText(); - /* Todo: we need to pass a copy here */ - args.GetReturnValue().Set(ArrayBuffer_New(isolate, (void *) ip.data(), ip.length()/*, ArrayBufferCreationMode::kInternalized*/)); + args.GetReturnValue().Set(ArrayBuffer_NewCopy(isolate, (void *) ip.data(), ip.length())); } } @@ -248,7 +246,6 @@ struct WebSocketWrapper { topicsArray->Set(isolate->GetCurrentContext(), topicsArray->Length(), topicString).IsNothing(); }); - /* Todo: we need to pass a copy here */ args.GetReturnValue().Set(topicsArray); } }