blob: 1040f70d719f900399d0a73ed097fd6e9c7d7819 [file] [log] [blame]
murgatroid99749666e2015-01-12 18:25:58 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
murgatroid99749666e2015-01-12 18:25:58 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
murgatroid99749666e2015-01-12 18:25:58 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
murgatroid99749666e2015-01-12 18:25:58 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
murgatroid99749666e2015-01-12 18:25:58 -080016 *
17 */
18
murgatroid99e5061512015-01-12 18:14:35 -080019#include <string.h>
murgatroid99e5061512015-01-12 18:14:35 -080020
murgatroid99e5061512015-01-12 18:14:35 -080021#include <nan.h>
Craig Tiller5b1c5f22017-04-19 09:52:18 -070022#include <node.h>
David Garcia Quintas30bd4eb2015-06-01 21:08:59 -070023#include "grpc/byte_buffer_reader.h"
Craig Tiller5b1c5f22017-04-19 09:52:18 -070024#include "grpc/grpc.h"
murgatroid99e2672c92016-11-09 15:12:22 -080025#include "grpc/slice.h"
murgatroid99e5061512015-01-12 18:14:35 -080026
murgatroid997bd32502015-01-29 18:02:17 -080027#include "byte_buffer.h"
Craig Tiller7c70b6c2017-01-23 07:48:42 -080028#include "slice.h"
murgatroid997bd32502015-01-29 18:02:17 -080029
murgatroid99e5061512015-01-12 18:14:35 -080030namespace grpc {
31namespace node {
32
murgatroid99c6460142017-03-31 11:22:52 -070033using Nan::Callback;
murgatroid99c0a64cd2016-08-15 13:14:16 -070034using Nan::MaybeLocal;
murgatroid992b097832015-09-17 13:56:25 -070035
murgatroid997bd32502015-01-29 18:02:17 -080036using v8::Function;
murgatroid992b097832015-09-17 13:56:25 -070037using v8::Local;
murgatroid997bd32502015-01-29 18:02:17 -080038using v8::Object;
39using v8::Number;
murgatroid99e5061512015-01-12 18:14:35 -080040using v8::Value;
41
murgatroid992b097832015-09-17 13:56:25 -070042grpc_byte_buffer *BufferToByteBuffer(Local<Value> buffer) {
43 Nan::HandleScope scope;
Craig Tiller7c70b6c2017-01-23 07:48:42 -080044 grpc_slice slice = CreateSliceFromBuffer(buffer);
David Garcia Quintas59f905d2015-06-08 16:31:19 -070045 grpc_byte_buffer *byte_buffer(grpc_raw_byte_buffer_create(&slice, 1));
Craig Tillerd41a4a72016-10-26 16:16:06 -070046 grpc_slice_unref(slice);
murgatroid99e5061512015-01-12 18:14:35 -080047 return byte_buffer;
48}
49
Patryk Lesiewicz777777e2016-02-10 11:27:44 -080050namespace {
murgatroid99c6460142017-03-31 11:22:52 -070051void delete_buffer(char *data, void *hint) {
52 grpc_slice *slice = static_cast<grpc_slice *>(hint);
53 grpc_slice_unref(*slice);
54 delete slice;
55}
Patryk Lesiewicz777777e2016-02-10 11:27:44 -080056}
57
murgatroid992b097832015-09-17 13:56:25 -070058Local<Value> ByteBufferToBuffer(grpc_byte_buffer *buffer) {
59 Nan::EscapableHandleScope scope;
murgatroid99e5061512015-01-12 18:14:35 -080060 if (buffer == NULL) {
murgatroid992b097832015-09-17 13:56:25 -070061 return scope.Escape(Nan::Null());
murgatroid99e5061512015-01-12 18:14:35 -080062 }
David Garcia Quintas30bd4eb2015-06-01 21:08:59 -070063 grpc_byte_buffer_reader reader;
David Garcia Quintas6721d4f2016-06-30 17:17:23 -070064 if (!grpc_byte_buffer_reader_init(&reader, buffer)) {
65 Nan::ThrowError("Error initializing byte buffer reader.");
66 return scope.Escape(Nan::Undefined());
67 }
murgatroid99c6460142017-03-31 11:22:52 -070068 grpc_slice *slice = new grpc_slice;
69 *slice = grpc_byte_buffer_reader_readall(&reader);
70 grpc_byte_buffer_reader_destroy(&reader);
71 char *result = reinterpret_cast<char *>(GRPC_SLICE_START_PTR(*slice));
72 size_t length = GRPC_SLICE_LENGTH(*slice);
73 Local<Value> buf =
74 Nan::NewBuffer(result, length, delete_buffer, slice).ToLocalChecked();
75 return scope.Escape(buf);
murgatroid997bd32502015-01-29 18:02:17 -080076}
77
murgatroid99e5061512015-01-12 18:14:35 -080078} // namespace node
Craig Tiller190d3602015-02-18 09:23:38 -080079} // namespace grpc