diff options
-rw-r--r-- | src/node/ext/byte_buffer.cc | 39 | ||||
-rw-r--r-- | src/node/ext/byte_buffer.h | 4 | ||||
-rw-r--r-- | src/node/ext/slice.cc | 5 |
3 files changed, 16 insertions, 32 deletions
diff --git a/src/node/ext/byte_buffer.cc b/src/node/ext/byte_buffer.cc index 7d6fb19860..a99b96bea5 100644 --- a/src/node/ext/byte_buffer.cc +++ b/src/node/ext/byte_buffer.cc @@ -45,6 +45,7 @@ namespace grpc { namespace node { +using Nan::Callback; using Nan::MaybeLocal; using v8::Function; @@ -62,7 +63,11 @@ grpc_byte_buffer *BufferToByteBuffer(Local<Value> buffer) { } namespace { -void delete_buffer(char *data, void *hint) { delete[] data; } +void delete_buffer(char *data, void *hint) { + grpc_slice *slice = static_cast<grpc_slice *>(hint); + grpc_slice_unref(*slice); + delete slice; +} } Local<Value> ByteBufferToBuffer(grpc_byte_buffer *buffer) { @@ -75,31 +80,15 @@ Local<Value> ByteBufferToBuffer(grpc_byte_buffer *buffer) { Nan::ThrowError("Error initializing byte buffer reader."); return scope.Escape(Nan::Undefined()); } - grpc_slice slice = grpc_byte_buffer_reader_readall(&reader); - size_t length = GRPC_SLICE_LENGTH(slice); - char *result = new char[length]; - memcpy(result, GRPC_SLICE_START_PTR(slice), length); - grpc_slice_unref(slice); - return scope.Escape(MakeFastBuffer( - Nan::NewBuffer(result, length, delete_buffer, NULL).ToLocalChecked())); + grpc_slice *slice = new grpc_slice; + *slice = grpc_byte_buffer_reader_readall(&reader); + grpc_byte_buffer_reader_destroy(&reader); + char *result = reinterpret_cast<char *>(GRPC_SLICE_START_PTR(*slice)); + size_t length = GRPC_SLICE_LENGTH(*slice); + Local<Value> buf = + Nan::NewBuffer(result, length, delete_buffer, slice).ToLocalChecked(); + return scope.Escape(buf); } -Local<Value> MakeFastBuffer(Local<Value> slowBuffer) { - Nan::EscapableHandleScope scope; - Local<Object> globalObj = Nan::GetCurrentContext()->Global(); - MaybeLocal<Value> constructorValue = Nan::Get( - globalObj, Nan::New("Buffer").ToLocalChecked()); - Local<Function> bufferConstructor = Local<Function>::Cast( - constructorValue.ToLocalChecked()); - const int argc = 3; - Local<Value> consArgs[argc] = { - slowBuffer, - Nan::New<Number>(::node::Buffer::Length(slowBuffer)), - Nan::New<Number>(0) - }; - MaybeLocal<Object> fastBuffer = Nan::NewInstance(bufferConstructor, - argc, consArgs); - return scope.Escape(fastBuffer.ToLocalChecked()); -} } // namespace node } // namespace grpc diff --git a/src/node/ext/byte_buffer.h b/src/node/ext/byte_buffer.h index 55bc0ab377..e8c4ac90bd 100644 --- a/src/node/ext/byte_buffer.h +++ b/src/node/ext/byte_buffer.h @@ -50,10 +50,6 @@ grpc_byte_buffer *BufferToByteBuffer(v8::Local<v8::Value> buffer); /* Convert a grpc_byte_buffer to a Node.js Buffer */ v8::Local<v8::Value> ByteBufferToBuffer(grpc_byte_buffer *buffer); -/* Convert a ::node::Buffer to a fast Buffer, as defined in the Node - Buffer documentation */ -v8::Local<v8::Value> MakeFastBuffer(v8::Local<v8::Value> slowBuffer); - } // namespace node } // namespace grpc diff --git a/src/node/ext/slice.cc b/src/node/ext/slice.cc index 98a80b3d2f..104dd9e22c 100644 --- a/src/node/ext/slice.cc +++ b/src/node/ext/slice.cc @@ -37,7 +37,6 @@ #include <grpc/support/alloc.h> #include "slice.h" -#include "byte_buffer.h" namespace grpc { namespace node { @@ -93,9 +92,9 @@ Local<Value> CreateBufferFromSlice(const grpc_slice slice) { Nan::EscapableHandleScope scope; grpc_slice *slice_ptr = new grpc_slice; *slice_ptr = grpc_slice_ref(slice); - return scope.Escape(MakeFastBuffer(Nan::NewBuffer( + return scope.Escape(Nan::NewBuffer( const_cast<char *>(reinterpret_cast<const char *>(GRPC_SLICE_START_PTR(*slice_ptr))), - GRPC_SLICE_LENGTH(*slice_ptr), SliceFreeCallback, slice_ptr).ToLocalChecked())); + GRPC_SLICE_LENGTH(*slice_ptr), SliceFreeCallback, slice_ptr).ToLocalChecked()); } } // namespace node |