Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions generate/descriptor.json
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,6 @@
"cred": {
"cType": "git_cred",
"functions": {
"git_cred_default_new":{
"isAsync": false
},
"git_cred_ssh_custom_new": {
"ignore": true
},
Expand All @@ -265,14 +262,8 @@
"git_cred_ssh_key_from_agent":{
"isAsync": false
},
"git_cred_ssh_key_new":{
"isAsync": false
},
"git_cred_userpass": {
"ignore": true
},
"git_cred_userpass_plaintext_new": {
"isAsync": false
}
}
},
Expand Down
61 changes: 61 additions & 0 deletions generate/partials/field_accessors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,21 @@ void {{ cppClassName }}::{{ field.name }}_asyncAfter(uv_work_t* req, int status)

TryCatch tryCatch;
Handle<Value> result = instance->{{ field.name }}->Call({{ field.args|jsArgsCount }}, argv);

if (result->IsObject() && result->ToObject()->Has(NanNew("then"))) {
Handle<Value> thenProp = result->ToObject()->Get(NanNew("then"));

if (thenProp->IsFunction()) {
// we can be reasonbly certain that the result is a promise
Local<Object> promise = result->ToObject();

NanAssignPersistent(baton->promise, promise);

uv_queue_work(uv_default_loop(), &baton->req, {{ field.name }}_asyncWork, {{ field.name }}_asyncPromisePolling);
return;
}
}

{{ field.returnType }} resultStatus;

{%each field|returnsInfo true false as _return%}
Expand All @@ -151,6 +166,52 @@ void {{ cppClassName }}::{{ field.name }}_asyncAfter(uv_work_t* req, int status)
{%endeach%}
baton->done = true;
}

void {{ cppClassName }}::{{ field.name }}_asyncPromisePolling(uv_work_t* req, int status) {
NanScope();

{{ field.name|titleCase }}Baton* baton = static_cast<{{ field.name|titleCase }}Baton*>(req->data);
Local<Object> promise = NanNew<Object>(baton->promise);
NanCallback* isPendingFn = new NanCallback(promise->Get(NanNew("isPending")).As<Function>());
Local<Value> argv[0];
Local<Boolean> isPending = isPendingFn->Call(0, argv)->ToBoolean();

if (isPending->Value()) {
uv_queue_work(uv_default_loop(), &baton->req, {{ field.name }}_asyncWork, {{ field.name }}_asyncPromisePolling);
return;
}

NanCallback* isFulfilledFn = new NanCallback(promise->Get(NanNew("isFulfilled")).As<Function>());
Local<Boolean> isFulfilled = isFulfilledFn->Call(0, argv)->ToBoolean();

if (isFulfilled->Value()) {
NanCallback* resultFn = new NanCallback(promise->Get(NanNew("value")).As<Function>());
Handle<Value> result = resultFn->Call(0, argv);
{{ field.returnType }} resultStatus;

{%each field|returnsInfo true false as _return%}
if (result.IsEmpty() || result->IsNativeError()) {
baton->result = {{ field.returnError }};
}
else if (!result->IsNull() && !result->IsUndefined()) {
{{ _return.cppClassName }}* wrapper = ObjectWrap::Unwrap<{{ _return.cppClassName }}>(result->ToObject());
wrapper->selfFreeing = false;

baton->{{ _return.name }} = wrapper->GetRefValue();
baton->result = {{ field.returnSuccess }};
}
else {
baton->result = {{ field.returnNoResults }};
}
{%endeach%}
baton->done = true;
}
else {
// promise was rejected
baton->result = {{ field.returnError }};
baton->done = false;
}
}
{%endif%}
{%endif%}
{%endeach%}
2 changes: 2 additions & 0 deletions generate/templates/struct_header.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ class {{ cppClassName }} : public ObjectWrap {
);
static void {{ field.name }}_asyncWork(uv_work_t* req);
static void {{ field.name }}_asyncAfter(uv_work_t* req, int status);
static void {{ field.name }}_asyncPromisePolling(uv_work_t* req, int status);
struct {{ field.name|titleCase }}Baton {
{%each field.args|argsInfo as arg %}
{{ arg.cType }} {{ arg.name}};
{%endeach%}
uv_work_t req;
{{ field.returnType }} result;
Persistent<Object> promise;
bool done;
};
{%endif%}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"version": "2.0.0"
}
},
"homepage": "https://github.com/tbranyen/nodegit",
"homepage": "http://nodegit.org",
"keywords": [
"libgit2",
"git2",
Expand Down Expand Up @@ -46,7 +46,7 @@
"main": "lib/nodegit.js",
"repository": {
"type": "git",
"url": "git://github.com/tbranyen/nodegit.git"
"url": "git://github.com/nodegit/nodegit.git"
},
"directories": {
"build": "./build",
Expand Down
30 changes: 15 additions & 15 deletions test/tests/cred.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ describe("Cred", function() {
var NodeGit = require("../../");

it("can create default credentials", function() {
var defaultCreds = NodeGit.Cred.defaultNew();

assert(defaultCreds instanceof NodeGit.Cred);
NodeGit.Cred.defaultNew().then(function (defaultCreds) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to return Promises within the tests

assert(defaultCreds instanceof NodeGit.Cred);
});
});

it("can create ssh credentials using passed keys", function() {
var sshCreds
= NodeGit.Cred.sshKeyNew(
"username",
"public key",
"private key",
"passphrase");

assert(sshCreds instanceof NodeGit.Cred);
NodeGit.Cred.sshKeyNew(
"username",
"public key",
"private key",
"passphrase")
.then(function (sshCreds) {
assert(sshCreds instanceof NodeGit.Cred);
});
});

it("can create credentials using plaintext", function() {
var plaintextCreds
= NodeGit.Cred.userpassPlaintextNew("username", "password");

assert(plaintextCreds instanceof NodeGit.Cred);
NodeGit.Cred.userpassPlaintextNew("username", "password")
.then(function (plaintextCreds) {
assert(plaintextCreds instanceof NodeGit.Cred);
});
});
});