node_grpc.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include <node.h>
  34. #include <nan.h>
  35. #include <v8.h>
  36. #include "grpc/grpc.h"
  37. #include "call.h"
  38. #include "channel.h"
  39. #include "server.h"
  40. #include "completion_queue_async_worker.h"
  41. #include "credentials.h"
  42. #include "server_credentials.h"
  43. using v8::Handle;
  44. using v8::Value;
  45. using v8::Object;
  46. using v8::Uint32;
  47. using v8::String;
  48. void InitStatusConstants(Handle<Object> exports) {
  49. NanScope();
  50. Handle<Object> status = NanNew<Object>();
  51. exports->Set(NanNew("status"), status);
  52. Handle<Value> OK(NanNew<Uint32, uint32_t>(GRPC_STATUS_OK));
  53. status->Set(NanNew("OK"), OK);
  54. Handle<Value> CANCELLED(NanNew<Uint32, uint32_t>(GRPC_STATUS_CANCELLED));
  55. status->Set(NanNew("CANCELLED"), CANCELLED);
  56. Handle<Value> UNKNOWN(NanNew<Uint32, uint32_t>(GRPC_STATUS_UNKNOWN));
  57. status->Set(NanNew("UNKNOWN"), UNKNOWN);
  58. Handle<Value> INVALID_ARGUMENT(
  59. NanNew<Uint32, uint32_t>(GRPC_STATUS_INVALID_ARGUMENT));
  60. status->Set(NanNew("INVALID_ARGUMENT"), INVALID_ARGUMENT);
  61. Handle<Value> DEADLINE_EXCEEDED(
  62. NanNew<Uint32, uint32_t>(GRPC_STATUS_DEADLINE_EXCEEDED));
  63. status->Set(NanNew("DEADLINE_EXCEEDED"), DEADLINE_EXCEEDED);
  64. Handle<Value> NOT_FOUND(NanNew<Uint32, uint32_t>(GRPC_STATUS_NOT_FOUND));
  65. status->Set(NanNew("NOT_FOUND"), NOT_FOUND);
  66. Handle<Value> ALREADY_EXISTS(
  67. NanNew<Uint32, uint32_t>(GRPC_STATUS_ALREADY_EXISTS));
  68. status->Set(NanNew("ALREADY_EXISTS"), ALREADY_EXISTS);
  69. Handle<Value> PERMISSION_DENIED(
  70. NanNew<Uint32, uint32_t>(GRPC_STATUS_PERMISSION_DENIED));
  71. status->Set(NanNew("PERMISSION_DENIED"), PERMISSION_DENIED);
  72. Handle<Value> UNAUTHENTICATED(
  73. NanNew<Uint32, uint32_t>(GRPC_STATUS_UNAUTHENTICATED));
  74. status->Set(NanNew("UNAUTHENTICATED"), UNAUTHENTICATED);
  75. Handle<Value> RESOURCE_EXHAUSTED(
  76. NanNew<Uint32, uint32_t>(GRPC_STATUS_RESOURCE_EXHAUSTED));
  77. status->Set(NanNew("RESOURCE_EXHAUSTED"), RESOURCE_EXHAUSTED);
  78. Handle<Value> FAILED_PRECONDITION(
  79. NanNew<Uint32, uint32_t>(GRPC_STATUS_FAILED_PRECONDITION));
  80. status->Set(NanNew("FAILED_PRECONDITION"), FAILED_PRECONDITION);
  81. Handle<Value> ABORTED(NanNew<Uint32, uint32_t>(GRPC_STATUS_ABORTED));
  82. status->Set(NanNew("ABORTED"), ABORTED);
  83. Handle<Value> OUT_OF_RANGE(
  84. NanNew<Uint32, uint32_t>(GRPC_STATUS_OUT_OF_RANGE));
  85. status->Set(NanNew("OUT_OF_RANGE"), OUT_OF_RANGE);
  86. Handle<Value> UNIMPLEMENTED(
  87. NanNew<Uint32, uint32_t>(GRPC_STATUS_UNIMPLEMENTED));
  88. status->Set(NanNew("UNIMPLEMENTED"), UNIMPLEMENTED);
  89. Handle<Value> INTERNAL(NanNew<Uint32, uint32_t>(GRPC_STATUS_INTERNAL));
  90. status->Set(NanNew("INTERNAL"), INTERNAL);
  91. Handle<Value> UNAVAILABLE(NanNew<Uint32, uint32_t>(GRPC_STATUS_UNAVAILABLE));
  92. status->Set(NanNew("UNAVAILABLE"), UNAVAILABLE);
  93. Handle<Value> DATA_LOSS(NanNew<Uint32, uint32_t>(GRPC_STATUS_DATA_LOSS));
  94. status->Set(NanNew("DATA_LOSS"), DATA_LOSS);
  95. }
  96. void InitCallErrorConstants(Handle<Object> exports) {
  97. NanScope();
  98. Handle<Object> call_error = NanNew<Object>();
  99. exports->Set(NanNew("callError"), call_error);
  100. Handle<Value> OK(NanNew<Uint32, uint32_t>(GRPC_CALL_OK));
  101. call_error->Set(NanNew("OK"), OK);
  102. Handle<Value> ERROR(NanNew<Uint32, uint32_t>(GRPC_CALL_ERROR));
  103. call_error->Set(NanNew("ERROR"), ERROR);
  104. Handle<Value> NOT_ON_SERVER(
  105. NanNew<Uint32, uint32_t>(GRPC_CALL_ERROR_NOT_ON_SERVER));
  106. call_error->Set(NanNew("NOT_ON_SERVER"), NOT_ON_SERVER);
  107. Handle<Value> NOT_ON_CLIENT(
  108. NanNew<Uint32, uint32_t>(GRPC_CALL_ERROR_NOT_ON_CLIENT));
  109. call_error->Set(NanNew("NOT_ON_CLIENT"), NOT_ON_CLIENT);
  110. Handle<Value> ALREADY_INVOKED(
  111. NanNew<Uint32, uint32_t>(GRPC_CALL_ERROR_ALREADY_INVOKED));
  112. call_error->Set(NanNew("ALREADY_INVOKED"), ALREADY_INVOKED);
  113. Handle<Value> NOT_INVOKED(
  114. NanNew<Uint32, uint32_t>(GRPC_CALL_ERROR_NOT_INVOKED));
  115. call_error->Set(NanNew("NOT_INVOKED"), NOT_INVOKED);
  116. Handle<Value> ALREADY_FINISHED(
  117. NanNew<Uint32, uint32_t>(GRPC_CALL_ERROR_ALREADY_FINISHED));
  118. call_error->Set(NanNew("ALREADY_FINISHED"), ALREADY_FINISHED);
  119. Handle<Value> TOO_MANY_OPERATIONS(
  120. NanNew<Uint32, uint32_t>(GRPC_CALL_ERROR_TOO_MANY_OPERATIONS));
  121. call_error->Set(NanNew("TOO_MANY_OPERATIONS"), TOO_MANY_OPERATIONS);
  122. Handle<Value> INVALID_FLAGS(
  123. NanNew<Uint32, uint32_t>(GRPC_CALL_ERROR_INVALID_FLAGS));
  124. call_error->Set(NanNew("INVALID_FLAGS"), INVALID_FLAGS);
  125. }
  126. void InitOpTypeConstants(Handle<Object> exports) {
  127. NanScope();
  128. Handle<Object> op_type = NanNew<Object>();
  129. exports->Set(NanNew("opType"), op_type);
  130. Handle<Value> SEND_INITIAL_METADATA(
  131. NanNew<Uint32, uint32_t>(GRPC_OP_SEND_INITIAL_METADATA));
  132. op_type->Set(NanNew("SEND_INITIAL_METADATA"), SEND_INITIAL_METADATA);
  133. Handle<Value> SEND_MESSAGE(
  134. NanNew<Uint32, uint32_t>(GRPC_OP_SEND_MESSAGE));
  135. op_type->Set(NanNew("SEND_MESSAGE"), SEND_MESSAGE);
  136. Handle<Value> SEND_CLOSE_FROM_CLIENT(
  137. NanNew<Uint32, uint32_t>(GRPC_OP_SEND_CLOSE_FROM_CLIENT));
  138. op_type->Set(NanNew("SEND_CLOSE_FROM_CLIENT"), SEND_CLOSE_FROM_CLIENT);
  139. Handle<Value> SEND_STATUS_FROM_SERVER(
  140. NanNew<Uint32, uint32_t>(GRPC_OP_SEND_STATUS_FROM_SERVER));
  141. op_type->Set(NanNew("SEND_STATUS_FROM_SERVER"), SEND_STATUS_FROM_SERVER);
  142. Handle<Value> RECV_INITIAL_METADATA(
  143. NanNew<Uint32, uint32_t>(GRPC_OP_RECV_INITIAL_METADATA));
  144. op_type->Set(NanNew("RECV_INITIAL_METADATA"), RECV_INITIAL_METADATA);
  145. Handle<Value> RECV_MESSAGE(
  146. NanNew<Uint32, uint32_t>(GRPC_OP_RECV_MESSAGE));
  147. op_type->Set(NanNew("RECV_MESSAGE"), RECV_MESSAGE);
  148. Handle<Value> RECV_STATUS_ON_CLIENT(
  149. NanNew<Uint32, uint32_t>(GRPC_OP_RECV_STATUS_ON_CLIENT));
  150. op_type->Set(NanNew("RECV_STATUS_ON_CLIENT"), RECV_STATUS_ON_CLIENT);
  151. Handle<Value> RECV_CLOSE_ON_SERVER(
  152. NanNew<Uint32, uint32_t>(GRPC_OP_RECV_CLOSE_ON_SERVER));
  153. op_type->Set(NanNew("RECV_CLOSE_ON_SERVER"), RECV_CLOSE_ON_SERVER);
  154. }
  155. void init(Handle<Object> exports) {
  156. NanScope();
  157. grpc_init();
  158. InitStatusConstants(exports);
  159. InitCallErrorConstants(exports);
  160. InitOpTypeConstants(exports);
  161. grpc::node::Call::Init(exports);
  162. grpc::node::Channel::Init(exports);
  163. grpc::node::Server::Init(exports);
  164. grpc::node::CompletionQueueAsyncWorker::Init(exports);
  165. grpc::node::Credentials::Init(exports);
  166. grpc::node::ServerCredentials::Init(exports);
  167. }
  168. NODE_MODULE(grpc, init)