Skip to content
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

fix: callbackify resulting function should have one more argument #72

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

erossignon
Copy link

@erossignon erossignon commented May 18, 2022

Fixing a callbackify behavior that is different from the NodeJS implementation ( when NodeJS version >= 12.0)

In node, the callbackified function has a length that is one more than the length of the original function. For instance:

     callbackify((a,b,c)=>Promise<void>).length = 4 

this is not the case in the current version (<=0.12.4) where

   callbackify((a,b,c)=>Promise<void>).length = 3 

This PR provides the fix and the associated unit test.

@ljharb
Copy link
Member

ljharb commented May 18, 2022

In which node version is this the case?

Bear in mind that the current node-util largely matches node 0.12.

test/browser/callbackify.js Outdated Show resolved Hide resolved
util.js Outdated Show resolved Hide resolved
util.js Show resolved Hide resolved
@erossignon erossignon force-pushed the BugFix/Callbackified_Function_should_have_one_more_argument branch from f2409b2 to 87caee5 Compare May 18, 2022 19:38
@@ -0,0 +1,8 @@

module.exports = function nodeJSVersion(arg) {
Copy link
Member

Choose a reason for hiding this comment

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

This shouldn’t be needed at all; the feature detection is sufficient (and more reliable)

Copy link
Author

@erossignon erossignon May 19, 2022

Choose a reason for hiding this comment

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

can you elaborate?
I found that detecting the nodeJSVersion was needed because, in versions 10 and 11 of NodeJS, the length is not increased although the function length is configurable; the fix was only introduced in version >=12. I needed a way to detect these 2 special cases in the unit test.
(see table above)

Copy link
Member

Choose a reason for hiding this comment

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

I'm skeptical - in my experimentation, it works fine in node 10:

function f() {}
Object.getOwnPropertyDescriptor(f, 'length').configurable; // true
Object.defineProperty(f, 'length', { value: 42 });
f.length; // 42

but then we should be feature-detecting this failure. we shouldn't be hardcoding specific versions of things, especially since any issue in specific node versions also matches issues in some Chrome versions.

Copy link
Author

Choose a reason for hiding this comment

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

@ljharb , yes fct.length is configurable in node10 onward but the util.callbackify do not take advantage of this and keep the fct.length unchanged. the behavior has been fixed by the nodejs team from node12 onwards.

we shouldn't be hardcoding specific versions of things,
I have remove the code that check against a specific version of node in util.js .

Let me know if there is anything else I can do .

Copy link
Member

Choose a reason for hiding this comment

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

this still seems an issue, since this file shouldn't exist

@erossignon erossignon force-pushed the BugFix/Callbackified_Function_should_have_one_more_argument branch from 41b988e to aed9474 Compare May 22, 2022 07:19
util.js Show resolved Hide resolved
util.js Show resolved Hide resolved
@erossignon erossignon requested a review from ljharb May 24, 2022 19:19
@wobsoriano
Copy link

Any updates on this? :)

Comment on lines +2 to +4
module.exports = function isFunctionLengthConfigurable(arg) {
return Object.getOwnPropertyDescriptor(function () { }, 'length').configurable;
}
Copy link
Member

Choose a reason for hiding this comment

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

this already exists on npm - https://npmjs.com/functions-have-names in the functionsHaveConfigurableNames property. let's just use that.

@@ -0,0 +1,8 @@

module.exports = function nodeJSVersion(arg) {
Copy link
Member

Choose a reason for hiding this comment

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

this still seems an issue, since this file shouldn't exist

Object.defineProperties(callbackified,
getOwnPropertyDescriptors(original));
const desc = getOwnPropertyDescriptors(original);
var isFunctionLengthConfigurable = Object.getOwnPropertyDescriptor(callbackified, 'length').configurable;
Copy link
Member

Choose a reason for hiding this comment

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

this should be using the file above, or better functions-have-names

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants