-
-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove promisify-any dependency #20
Comments
Just realized Bluebird is a dependency of promisify-any. Removing this should probably come before removing Bluebird. |
Also here let's check if tests already cover these cases / functionality enough in order to be on the safe side. I think coverage is very high but there are a few lines missed and you know how Mr. Murphy thinks about this :-) |
I think we can start on this after #30 has been merged |
#30 is merged, we can safely start with this one :-) |
I'm looking into this now and noticed some oddities I thought maybe you guys could chime in on. First off, a lot of the tests are returning the wrong value, here generateAccessToken: sinon.stub().returns({ client: {}, expiresAt: new Date(), user: {} }) should be: generateAccessToken: sinon.stub().returns('my-access-token') I believe these were left over from version 2 and never updated. Second, I'm not sure You guys have any thoughts on this? How to promisify this: AbstractGrantType.prototype.generateAccessToken = function(client, user, scope) {
if (this.model.generateAccessToken) {
return promisify(this.model.generateAccessToken, 3).call(this.model, client, user, scope)
.then(function(accessToken) {
return accessToken || tokenUtil.generateRandomToken();
});
}
return tokenUtil.generateRandomToken();
}; I initinally thought we could do this but it won't work since there is no callback: AbstractGrantType.prototype.generateAccessToken = function(client, user, scope) {
if (this.model.generateAccessToken) {
return require('util').promisify(this.model.generateAccessToken)(client, user, scope)
.then(function(accessToken) {
return accessToken || tokenUtil.generateRandomToken();
});
}
return tokenUtil.generateRandomToken();
}; |
Yes I ran the tests right now with If the functions are simple one-dimensional we could write our own wrapper like so:
I checked a few tests and it works so far but I am not sure if this works for all. |
Yes, that's kind of where I ended up. We could just write our own promisify function. Let me whip up a pseudo function and see what you think. |
@jwerre did you come up with anything? Maybe you open a draft pull request so we can work out the details using the code review? |
@jankapunkt I did but I don't love it. I'm not sure it's better than what is there. I'm going to test it out this morning and see if it works. Will post later today. |
Here... I'll just post what I have so far: module.exports = (fn, fnArgs) => {
// I was hoping for something like this but I can't get arguments from the
// function that is passed in.
//
// let lastArg = fn.prototype.arguments.length - 1;
// let isPromise = typeof fn.prototype.arguments[lastArg] !== 'function';
// if there is no callback in the arguments, return a promise
if (!fnArgs.length || typeof fnArgs[fnArgs.length - 1] !== 'function') {
return fn;
}
// if there is a callback warp in promise
return (...args) => {
return new Promise((resolve, reject) => {
fn(...args, (err, result) => {
if (err) {
return reject(err);
}
resolve(result);
});
});
};
}; So instead of: return promisify(this.model.generateAccessToken, 3) ...it would be: return promisify(this.model.generateAccessToken, arguments) I was hoping to get the arguments from the pass function but I don't think is possible so we'll have to just pass them in with the function. |
- Created `lib/utils/promisify.js` - Removed promisify any from `lib/grant-types/abstract-grant-type.js` - Updated AbstractGrantType test at `test/unit/grant-types/abstract-grant-type_test.js`
@jankapunkt I pushed a
Running either of these test should work fine. Take a look at the code and tell me what you think. |
@jankapunkt @HappyZombies, what would you guys say to removing support for callbacks all together? |
@jwerre I am ok with that, but I believe that'll be out of scope for removing this dependency. Going to a Promise based approach is definitely the way to go, but would require a lot of work and we need to be careful too. I think this can be something we tackle in a next major version. We can get started on that, but I really wanna focus on getting out important fixes, administrator work NOW. If bluebird has to stay for a while then that's ok for now. Once we get our next release out (which I am guessing will be 4.1.0), we can talk about what we want in a version 5. If removing promisify-any is being too much of a pain, let's put a hold on it for now and get back to it later. |
I agree with @HappyZombies but I will still take a look into it. In the meantime we can further discuss release management |
I agree as well. When we deprecate callbacks we can remove Bluebird and promise-any in one fell swoop so I don't think there is any reason to spend any more time on this. |
@jankapunkt Are you going to take a look at this, or is this safe to close? #48 |
I also had a look into promisify-any. promisify-any is capable of wrapping also generator functions. Is there any necessity to support generator functions? |
Just use
util.promisify
The text was updated successfully, but these errors were encountered: