Skip to content
Snippets Groups Projects
Commit 8d4aec37 authored by murgatroid99's avatar murgatroid99
Browse files

Added most of the plugin implementation

parent 2e85b046
No related branches found
No related tags found
No related merge requests found
...@@ -242,5 +242,34 @@ NAN_METHOD(Credentials::CreateInsecure) { ...@@ -242,5 +242,34 @@ NAN_METHOD(Credentials::CreateInsecure) {
info.GetReturnValue().Set(WrapStruct(NULL)); info.GetReturnValue().Set(WrapStruct(NULL));
} }
NAN_METHOD(Credentials::CreateFromPlugin) {
if (!info[0]->IsFunction()) {
return Nan::ThrowTypeError(
"createFromPlugin's argument must be a function");
}
grpc_metadata_credentials_plugin plugin;
plugin_state *state = new plugin_state;
state->callback = new Nan::Callback(info[0].As<Function>());
plugin.get_metadata = plugin_get_metadata;
plugin.destroy = plugin_destroy_state;
plugin.state = reinterpret_cast<void*>(state);
grpc_credentials *creds = grpc_metadata_credentials_create_from_plugin(plugin,
NULL);
if (creds == NULL) {
info.GetReturnValue().SetNull();
} else {
info.GetReturnValue().Set(WrapStruct(creds()));
}
}
void plugin_get_metadata(void *state, const char *service_url,
grpc_credentials_plugin_metadata_cb cb,
void *user_data) {
uv_async_t *async = new uv_async_t;
uv_async_init(uv_default_loop(),
async,
PluginCallback);
}
} // namespace node } // namespace node
} // namespace grpc } // namespace grpc
...@@ -83,13 +83,37 @@ typedef struct plugin_state { ...@@ -83,13 +83,37 @@ typedef struct plugin_state {
Nan::Callback *callback; Nan::Callback *callback;
} plugin_state; } plugin_state;
typedef struct plugin_callback_data {
plugin_state *state;
const char *service_url;
grpc_credentials_plugin_metadata_cb cb;
void *user_data;
} plugin_callback_data;
void plugin_get_metadata(void *state, const char *service_url, void plugin_get_metadata(void *state, const char *service_url,
grpc_credentials_plugin_metadata_cb cb, void *user_data); grpc_credentials_plugin_metadata_cb cb,
void *user_data);
void plugin_destroy_state(void *state); void plugin_destroy_state(void *state);
static NAN_METHOD(PluginCallback); static NAN_METHOD(PluginCallback);
NAN_INLINE NAUV_WORK_CB(SendPluginCallback) {
Nan::HandleScope scope;
plugin_callback_data *data = reinterpret_cast<plugin_callback_data>(
async->data);
v8::Local<v8::Function> plugin_callback = Nan::GetFunction(
Nan::New<v8::FunctionTemplate>(PluginCallback).ToLocalChecked());
// Attach cb and user_data to plugin_callback so that it can access them later
const int argc = 2;
v8::Local<v8::Value> argv = {Nan::New(data->service_url).ToLocalChecked(),
plugin_callback};
NanCallback *callback = static_cast<NanCallback*>(async->data);
callback->Call(argc, argv);
uv_unref((uv_handle_t *)async);
delete async;
}
} // namespace node } // namespace node
} // namespace grpc } // namespace grpc
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment