Skip to content

Commit

Permalink
fix: avoid using deprecated Nan APIs
Browse files Browse the repository at this point in the history
Starting with Nan 2.9.x certain Nan::Callback::Call APIs are
deprecated. Instead there are mechanisms in place that allow native
modules to preserve async context across async calls.
  • Loading branch information
ofrobots committed Apr 3, 2018
1 parent f54301a commit 4bfa143
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 10 deletions.
11 changes: 8 additions & 3 deletions packages/grpc-native-core/ext/call.cc
Original file line number Diff line number Diff line change
Expand Up @@ -446,13 +446,18 @@ class ServerCloseResponseOp : public Op {
};

tag::tag(Callback *callback, OpVec *ops, Call *call, Local<Value> call_value)
: callback(callback), ops(ops), call(call) {
: callback(callback),
async_resource(NULL),
ops(ops),
call(call) {
HandleScope scope;
async_resource = new Nan::AsyncResource("grpc:tag"); // Needs handle scope.
call_persist.Reset(call_value);
}

tag::~tag() {
delete callback;
delete async_resource;
delete ops;
}

Expand All @@ -468,10 +473,10 @@ void CompleteTag(void *tag, const char *error_message) {
Nan::Set(tag_obj, op_ptr->GetOpType(), op_ptr->GetNodeValue());
}
Local<Value> argv[] = {Nan::Null(), tag_obj};
callback->Call(2, argv);
callback->Call(2, argv, tag_struct->async_resource);
} else {
Local<Value> argv[] = {Nan::Error(error_message)};
callback->Call(1, argv);
callback->Call(1, argv, tag_struct->async_resource);
}
bool success = (error_message == NULL);
bool is_final_op = false;
Expand Down
1 change: 1 addition & 0 deletions packages/grpc-native-core/ext/call.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ struct tag {
v8::Local<v8::Value> call_value);
~tag();
Nan::Callback *callback;
Nan::AsyncResource *async_resource;
OpVec *ops;
Call *call;
Nan::Persistent<v8::Value, Nan::CopyablePersistentTraits<v8::Value>>
Expand Down
9 changes: 4 additions & 5 deletions packages/grpc-native-core/ext/call_credentials.cc
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ NAUV_WORK_CB(SendPluginCallback) {
// Get Local<Function> from Nan::Callback*
**plugin_callback};
Nan::Callback *callback = state->callback;
callback->Call(argc, argv);
callback->Call(argc, argv, data->async_resource);
delete data;
}
}
Expand All @@ -245,11 +245,10 @@ int plugin_get_metadata(
grpc_metadata creds_md[GRPC_METADATA_CREDENTIALS_PLUGIN_SYNC_MAX],
size_t *num_creds_md, grpc_status_code *status,
const char **error_details) {
HandleScope scope;
plugin_state *p_state = reinterpret_cast<plugin_state *>(state);
plugin_callback_data *data = new plugin_callback_data;
data->service_url = context.service_url;
data->cb = cb;
data->user_data = user_data;
plugin_callback_data *data =
new plugin_callback_data(context.service_url, cb, user_data);

uv_mutex_lock(&p_state->plugin_mutex);
p_state->pending_callbacks->push(data);
Expand Down
15 changes: 15 additions & 0 deletions packages/grpc-native-core/ext/call_credentials.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,24 @@ class CallCredentials : public Nan::ObjectWrap {
/* Auth metadata plugin functionality */

typedef struct plugin_callback_data {
plugin_callback_data(const char *service_url_,
grpc_credentials_plugin_metadata_cb cb_,
void *user_data_)
: service_url(service_url_),
cb(cb_),
user_data(user_data_),
async_resource(NULL) {
Nan::HandleScope scope;
async_resource = new Nan::AsyncResource("grpc:plugin_callback_data");
}
~plugin_callback_data() {
delete async_resource;
}

const char *service_url;
grpc_credentials_plugin_metadata_cb cb;
void *user_data;
Nan::AsyncResource *async_resource;
} plugin_callback_data;

typedef struct plugin_state {
Expand Down
4 changes: 3 additions & 1 deletion packages/grpc-native-core/ext/node_grpc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ typedef struct log_args {

typedef struct logger_state {
Nan::Callback *callback;
Nan::AsyncResource *async_resource;
std::queue<log_args *> *pending_args;
uv_mutex_t mutex;
uv_async_t async;
Expand Down Expand Up @@ -202,7 +203,7 @@ NAUV_WORK_CB(LogMessagesCallback) {
.ToLocalChecked();
const int argc = 5;
Local<Value> argv[argc] = {file, line, severity, message, timestamp};
grpc_logger_state.callback->Call(argc, argv);
grpc_logger_state.callback->Call(argc, argv, grpc_logger_state.async_resource);
delete[] arg->core_args.message;
delete arg;
}
Expand Down Expand Up @@ -252,6 +253,7 @@ NAN_METHOD(SetDefaultLoggerCallback) {
grpc_logger_state.logger_set = true;
}
grpc_logger_state.callback = new Nan::Callback(info[0].As<v8::Function>());
grpc_logger_state.async_resource = new Nan::AsyncResource("grpc:logger");
}

NAN_METHOD(SetLogVerbosity) {
Expand Down
2 changes: 1 addition & 1 deletion packages/grpc-native-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
],
"dependencies": {
"lodash": "^4.15.0",
"nan": "^2.0.0",
"nan": "^2.10.0",
"node-pre-gyp": "0.7.0",
"protobufjs": "^5.0.0"
},
Expand Down

0 comments on commit 4bfa143

Please sign in to comment.