murgatroid99 | 749666e | 2015-01-12 18:25:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
murgatroid99 | 749666e | 2015-01-12 18:25:58 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | * 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 |
murgatroid99 | 749666e | 2015-01-12 18:25:58 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
murgatroid99 | 749666e | 2015-01-12 18:25:58 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | * 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. |
murgatroid99 | 749666e | 2015-01-12 18:25:58 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 19 | #include <string.h> |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 20 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 21 | #include <nan.h> |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 22 | #include <node.h> |
David Garcia Quintas | 30bd4eb | 2015-06-01 21:08:59 -0700 | [diff] [blame] | 23 | #include "grpc/byte_buffer_reader.h" |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 24 | #include "grpc/grpc.h" |
murgatroid99 | e2672c9 | 2016-11-09 15:12:22 -0800 | [diff] [blame] | 25 | #include "grpc/slice.h" |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 26 | |
murgatroid99 | 7bd3250 | 2015-01-29 18:02:17 -0800 | [diff] [blame] | 27 | #include "byte_buffer.h" |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 28 | #include "slice.h" |
murgatroid99 | 7bd3250 | 2015-01-29 18:02:17 -0800 | [diff] [blame] | 29 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 30 | namespace grpc { |
| 31 | namespace node { |
| 32 | |
murgatroid99 | c646014 | 2017-03-31 11:22:52 -0700 | [diff] [blame] | 33 | using Nan::Callback; |
murgatroid99 | c0a64cd | 2016-08-15 13:14:16 -0700 | [diff] [blame] | 34 | using Nan::MaybeLocal; |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 35 | |
murgatroid99 | 7bd3250 | 2015-01-29 18:02:17 -0800 | [diff] [blame] | 36 | using v8::Function; |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 37 | using v8::Local; |
murgatroid99 | 7bd3250 | 2015-01-29 18:02:17 -0800 | [diff] [blame] | 38 | using v8::Object; |
| 39 | using v8::Number; |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 40 | using v8::Value; |
| 41 | |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 42 | grpc_byte_buffer *BufferToByteBuffer(Local<Value> buffer) { |
| 43 | Nan::HandleScope scope; |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 44 | grpc_slice slice = CreateSliceFromBuffer(buffer); |
David Garcia Quintas | 59f905d | 2015-06-08 16:31:19 -0700 | [diff] [blame] | 45 | grpc_byte_buffer *byte_buffer(grpc_raw_byte_buffer_create(&slice, 1)); |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 46 | grpc_slice_unref(slice); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 47 | return byte_buffer; |
| 48 | } |
| 49 | |
Patryk Lesiewicz | 777777e | 2016-02-10 11:27:44 -0800 | [diff] [blame] | 50 | namespace { |
murgatroid99 | c646014 | 2017-03-31 11:22:52 -0700 | [diff] [blame] | 51 | void 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 Lesiewicz | 777777e | 2016-02-10 11:27:44 -0800 | [diff] [blame] | 56 | } |
| 57 | |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 58 | Local<Value> ByteBufferToBuffer(grpc_byte_buffer *buffer) { |
| 59 | Nan::EscapableHandleScope scope; |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 60 | if (buffer == NULL) { |
murgatroid99 | 2b09783 | 2015-09-17 13:56:25 -0700 | [diff] [blame] | 61 | return scope.Escape(Nan::Null()); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 62 | } |
David Garcia Quintas | 30bd4eb | 2015-06-01 21:08:59 -0700 | [diff] [blame] | 63 | grpc_byte_buffer_reader reader; |
David Garcia Quintas | 6721d4f | 2016-06-30 17:17:23 -0700 | [diff] [blame] | 64 | if (!grpc_byte_buffer_reader_init(&reader, buffer)) { |
| 65 | Nan::ThrowError("Error initializing byte buffer reader."); |
| 66 | return scope.Escape(Nan::Undefined()); |
| 67 | } |
murgatroid99 | c646014 | 2017-03-31 11:22:52 -0700 | [diff] [blame] | 68 | 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); |
murgatroid99 | 7bd3250 | 2015-01-29 18:02:17 -0800 | [diff] [blame] | 76 | } |
| 77 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 78 | } // namespace node |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 79 | } // namespace grpc |