Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Native: Copy slice data into Buffer when receiving binary metadata #1610

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/grpc-native-core/ext/call.cc
Expand Up @@ -158,7 +158,7 @@ Local<Value> ParseMetadata(const grpc_metadata_array *metadata_array) {
array = Local<Array>::Cast(maybe_array.ToLocalChecked());
}
if (grpc_is_binary_header(elem->key)) {
Nan::Set(array, array->Length(), CreateBufferFromSlice(elem->value));
Nan::Set(array, array->Length(), CopyBufferFromSlice(elem->value));
} else {
// TODO(murgatroid99): Use zero-copy string construction instead
Nan::Set(array, array->Length(), CopyStringFromSlice(elem->value));
Expand Down
18 changes: 4 additions & 14 deletions packages/grpc-native-core/ext/slice.cc
Expand Up @@ -33,12 +33,6 @@ using v8::String;
using v8::Value;

namespace {
void SliceFreeCallback(char *data, void *hint) {
grpc_slice *slice = reinterpret_cast<grpc_slice *>(hint);
grpc_slice_unref(*slice);
delete slice;
}

void string_destroy_func(void *user_data) {
delete reinterpret_cast<Nan::Utf8String *>(user_data);
}
Expand Down Expand Up @@ -74,16 +68,12 @@ Local<String> CopyStringFromSlice(const grpc_slice slice) {
.ToLocalChecked());
}

Local<Value> CreateBufferFromSlice(const grpc_slice slice) {
Local<Value> CopyBufferFromSlice(const grpc_slice slice) {
Nan::EscapableHandleScope scope;
grpc_slice *slice_ptr = new grpc_slice;
*slice_ptr = grpc_slice_ref(slice);
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());
Nan::CopyBuffer(reinterpret_cast<const char *>(GRPC_SLICE_START_PTR(slice)),
GRPC_SLICE_LENGTH(slice))
.ToLocalChecked());
}

} // namespace node
Expand Down
2 changes: 1 addition & 1 deletion packages/grpc-native-core/ext/slice.h
Expand Up @@ -32,7 +32,7 @@ grpc_slice CreateSliceFromBuffer(const v8::Local<v8::Value> source);

v8::Local<v8::String> CopyStringFromSlice(const grpc_slice slice);

v8::Local<v8::Value> CreateBufferFromSlice(const grpc_slice slice);
v8::Local<v8::Value> CopyBufferFromSlice(const grpc_slice slice);

} // namespace node
} // namespace grpc