forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the Navigator object with the Device Memory API. Refs: nodejs#39540 Refs: https://html.spec.whatwg.org/multipage/system-state.html#the-navigator-object Refs: https://w3c.github.io/device-memory
- Loading branch information
Showing
17 changed files
with
227 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -361,5 +361,6 @@ module.exports = { | |
btoa: 'readable', | ||
atob: 'readable', | ||
performance: 'readable', | ||
navigator: 'readable', | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use strict'; | ||
|
||
const { | ||
ObjectDefineProperties, | ||
ObjectSetPrototypeOf, | ||
MathClz32, | ||
} = primordials; | ||
|
||
const { | ||
codes: { | ||
ERR_ILLEGAL_CONSTRUCTOR, | ||
} | ||
} = require('internal/errors'); | ||
|
||
const { | ||
getCPUs, | ||
getOSInformation, | ||
getTotalMem, | ||
} = internalBinding('os'); | ||
|
||
class Navigator { | ||
constructor() { | ||
throw new ERR_ILLEGAL_CONSTRUCTOR(); | ||
} | ||
} | ||
|
||
class InternalNavigator {} | ||
InternalNavigator.prototype.constructor = Navigator.prototype.constructor; | ||
ObjectSetPrototypeOf(InternalNavigator.prototype, Navigator.prototype); | ||
|
||
function getDeviceMemory() { | ||
const mem = getTotalMem() / 1024 / 1024; | ||
if (mem <= 0.25 * 1024) return 0.25; | ||
if (mem >= 8 * 1024) return 8; | ||
const lowerBound = 1 << 31 - MathClz32(mem - 1); | ||
const upperBound = lowerBound * 2; | ||
return mem - lowerBound <= upperBound - mem ? | ||
lowerBound / 1024 : | ||
upperBound / 1024; | ||
} | ||
|
||
function getPlatform() { | ||
if (process.platform === 'win32') return 'Win32'; | ||
if (process.platform === 'android') return 'Android'; | ||
return getOSInformation()[0]; | ||
} | ||
|
||
const cpuData = getCPUs(); | ||
ObjectDefineProperties(Navigator.prototype, { | ||
deviceMemory: { | ||
configurable: true, | ||
enumerable: true, | ||
value: getDeviceMemory(), | ||
}, | ||
hardwareConcurrency: { | ||
configurable: true, | ||
enumerable: true, | ||
value: cpuData ? cpuData.length / 7 : 1, | ||
}, | ||
platform: { | ||
configurable: true, | ||
enumerable: true, | ||
value: getPlatform(), | ||
}, | ||
}); | ||
|
||
module.exports = new InternalNavigator(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
spec: https://w3c.github.io/device-memory/ | ||
suggested_reviewers: | ||
- npm1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
test(function() { | ||
assert_equals(typeof navigator.deviceMemory, "number", | ||
"navigator.deviceMemory returns a number"); | ||
assert_true(navigator.deviceMemory >= 0, | ||
"navigator.deviceMemory returns a positive value"); | ||
assert_true([0.25, 0.5, 1, 2, 4, 8].includes(navigator.deviceMemory), | ||
"navigator.deviceMemory returns a power of 2 between 0.25 and 8"); | ||
}, "navigator.deviceMemory is a positive number, a power of 2, between 0.25 and 8"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// META: script=/resources/WebIDLParser.js | ||
// META: script=/resources/idlharness.js | ||
// META: timeout=long | ||
|
||
// https://w3c.github.io/device-memory/ | ||
|
||
"use strict"; | ||
|
||
idl_test( | ||
['device-memory'], | ||
['html'], | ||
async idl_array => { | ||
if (self.GLOBAL.isWorker()) { | ||
idl_array.add_objects({ WorkerNavigator: ['navigator'] }); | ||
} else { | ||
idl_array.add_objects({ Navigator: ['navigator'] }); | ||
} | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// GENERATED CONTENT - DO NOT EDIT | ||
// Content was automatically extracted by Reffy into webref | ||
// (https://github.com/w3c/webref) | ||
// Source: Device Memory 1 (https://w3c.github.io/device-memory/) | ||
|
||
[ | ||
SecureContext, | ||
Exposed=(Window,Worker) | ||
] interface mixin NavigatorDeviceMemory { | ||
readonly attribute double deviceMemory; | ||
}; | ||
|
||
Navigator includes NavigatorDeviceMemory; | ||
WorkerNavigator includes NavigatorDeviceMemory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
/* eslint-disable no-global-assign */ | ||
require('../common'); | ||
const { notStrictEqual } = require('assert'); | ||
|
||
performance = undefined; | ||
notStrictEqual(globalThis.performance, undefined); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
require('../common'); | ||
const assert = require('assert'); | ||
const { cpus } = require('os'); | ||
const { platform } = require('process'); | ||
|
||
assert.ok(navigator.deviceMemory >= 0.25 && navigator.deviceMemory <= 8); | ||
|
||
assert.strictEqual(navigator.platform, { | ||
aix: 'AIX', | ||
android: 'Android', | ||
darwin: 'Darwin', | ||
freebsd: 'FreeBSD', | ||
linux: 'Linux', | ||
openbsd: 'OpenBSD', | ||
sunos: 'SunOS', | ||
win32: 'Win32', | ||
}[platform]); | ||
|
||
assert.ok(navigator.hardwareConcurrency >= 1); | ||
assert.strictEqual(navigator.hardwareConcurrency, cpus().length); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"idlharness.https.any.js": { | ||
"skip": "idlharness cannot recognize Node.js environment" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
require('../common'); | ||
const { WPTRunner } = require('../common/wpt'); | ||
|
||
const runner = new WPTRunner('device-memory'); | ||
|
||
runner.runJsTests(); |