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

lib: add navigator.deviceMemory #50229

Closed
wants to merge 1 commit into from

Conversation

Uzlopak
Copy link
Contributor

@Uzlopak Uzlopak commented Oct 18, 2023

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/tsc

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Oct 18, 2023
src/node_os.cc Outdated
Comment on lines 178 to 180
// Max-limit the reported value to 8GB to reduce fingerprintability of
// high-spec machines.
if (approximated_device_memory_gb_ > 8) approximated_device_memory_gb_ = 8.0;
Copy link
Member

@H4ad H4ad Oct 18, 2023

Choose a reason for hiding this comment

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

I don't think finger-printability is an issue on Node, but I'm not sure about this, so raising this comment just to get some feedback.

At least on spec, this is just a recommendation: https://www.w3.org/TR/device-memory/#computing-device-memory-value

If we do not limit the amount of memory, it will have the same behavior of getTotalMemory, so I don't know if worth to have this function without this behavior of limiting the amount of memory.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, fingerprinting is not a concern in Node.js. Also, even if it was, why not use os.totalmem()/1024**3 to get the value?

Copy link
Contributor Author

@Uzlopak Uzlopak Oct 18, 2023

Choose a reason for hiding this comment

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

I just ensured that the code is in sync with chrome and according to the documentation/spec.

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Max-limit the reported value to 8GB to reduce fingerprintability of
// high-spec machines.
if (approximated_device_memory_gb_ > 8) approximated_device_memory_gb_ = 8.0;

I prefer we remove this check

@tniessen
Copy link
Member

navigator is not a good API and I'm still unsure whether it was a good idea to add it at all. The only benefit is interoperability with other JavaScript runtimes, but it seems that neither Safari nor Firefox implement the deviceMemory property. I suppose Google's main goal is to establish the Device-Memory header, but that's unrelated to this PR and generally not very useful for server runtimes.

@aduh95
Copy link
Contributor

aduh95 commented Oct 18, 2023

+1 to what Tobias said, and also I'd be -1 on introducing properties to our implementation of Navigator unless it's listed on the HTML spec (https://html.spec.whatwg.org/multipage/system-state.html#the-navigator-object).

@RafaelGSS RafaelGSS added the semver-minor PRs that contain new features and should be released in the next minor version. label Oct 18, 2023
@tniessen tniessen added the experimental Issues and PRs related to experimental features. label Oct 19, 2023
@GeoffreyBooth GeoffreyBooth added the web-standards Issues and PRs related to Web APIs label Oct 26, 2023
@GeoffreyBooth
Copy link
Member

Let’s please ping @nodejs/web-standards for all issues and PRs related to navigator.

lib/internal/navigator.js Show resolved Hide resolved
@jasnell
Copy link
Member

jasnell commented Oct 28, 2023

Whether or not folks think navigator is a good API in general is not all that material here, in my opinion. I think the more relevant question is whether the new property yields any significant value. The main reason to add anything to navigator is cross-runtime interop and portability. The fact that none of the other runtimes expose navigator.deviceMemory and it is currently only implemented in blink-based browsers rather defeats that argument. I wouldn't say no to adding this but I also don't think there's value is adding it right now.

@Uzlopak
Copy link
Contributor Author

Uzlopak commented Oct 29, 2023

@jasnell

To be honest, I think nodejs should consider itself as the "brother" of Chrome because both use v8. Bun uses the same engine as Safari, so their behavior is closer than it is to Chrome and nodejs.

Thats why I also directly took the code from the chromium sourcecode, as I would prefer function parity with Chrome.

@tniessen
Copy link
Member

To be honest, I think nodejs should consider itself as the "brother" of Chrome because both use v8. Bun uses the same engine as Safari, so their behavior is closer than it is to Chrome and nodejs.

Thats why I also directly took the code from the chromium sourcecode, as I would prefer function parity with Chrome.

Why should the choice of using V8 imply anything about feature parity between Node.js and Chrome? This API is not part of V8, not part of the JS language specification, and currently not even part of the HTML specification. Deno, Cloudflare Workers, etc. are also based on V8, and none of them have implemented this API as far as I know.

Copy link
Member

@targos targos left a comment

Choose a reason for hiding this comment

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

Implementation is unnecessarily complex

Copy link
Member

@benjamingr benjamingr left a comment

Choose a reason for hiding this comment

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

LGTM with the fingerprinting check removed

@Uzlopak Uzlopak force-pushed the add-navigator-deviceMemory branch 2 times, most recently from 0aa3941 to 169c7d0 Compare October 30, 2023 09:02
@Uzlopak
Copy link
Contributor Author

Uzlopak commented Oct 30, 2023

@targos
implemented it now in javascript according to the spec by using the most significant byte.

@benjamingr
removed the lower and upper bounds.

@nodejs/web-standards
PTAL

Copy link
Member

@tniessen tniessen left a comment

Choose a reason for hiding this comment

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

I don't think this should land, for the reasons outlined in #50229 (comment).

I also don't understand @Uzlopak's argument that "nodejs should consider itself as the "brother" of Chrome because both use v8" and that one might thus "prefer function parity with Chrome."

@Uzlopak
Copy link
Contributor Author

Uzlopak commented Oct 30, 2023

My thoughts were, as I stated before, that navigator.deviceMemory is a feature in Chrome. Just want to remind you that implementing the navigator global in node is about browser compatibility. Also this feature has a market share of 75% (chrome 63%, edge 5%, opera 3%, Samsung Internet 2%, etc..).
Personally for me Chrome sets the standard and not Firefox with 3 % or Safari with 19% market share ...

And just because Firefox and Safari didnt implement it, doesnt result in the conclusion that we should not implement it - same as Chrome supporting this feature is not an argument for you.

A good argument against this feature is, that it is still a draft since 2018. Maybe there is a rule already covering these cases

navigator.deviceMemory is also used in few github projects https://github.com/search?q=navigator.deviceMemory&type=code

So we could agree on not implementing a draft spec. Or we can agree on implemeting something which is supported by the most used browser.

I personally dont have a strong opinion on this feature. I just saw a low hanging fruit, so I picked it.

Copy link
Member

@panva panva left a comment

Choose a reason for hiding this comment

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

Just want to remind you that implementing the navigator global in node is about browser compatibility

I would argue the point is interoperability, not compatibility.

Since this is a) draft, b) Chrome-only property and so c) one already has to work around its lack of presence in other browsers and runtimes if they wish to have interoperable code, I don't think we should be landing this.

@Uzlopak
Copy link
Contributor Author

Uzlopak commented Oct 31, 2023

Allrighty. :)

@Uzlopak Uzlopak closed this Oct 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. experimental Issues and PRs related to experimental features. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. semver-minor PRs that contain new features and should be released in the next minor version. web-standards Issues and PRs related to Web APIs
Projects
None yet
Development

Successfully merging this pull request may close these issues.