Returns withClientHints() as Thenable in nodejs / non-client-hints browsers

This commit is contained in:
Faisal Salman 2023-03-15 23:22:34 +07:00
parent f8dde65d54
commit c78346d3b4
2 changed files with 50 additions and 40 deletions

View File

@ -33,6 +33,11 @@ JavaScript library to detect Browser, Engine, OS, CPU, and Device type/model fro
<td><br/><p>UAParser.js has been upgraded to detect comprehensive device data based on the User-Agent and User-Agent Client Hints.</p><p>This package supports all device types including Apple and Android devices and can be used either in a browser (client-side) or Node.js environment (server-side).</p><p>Visit <a href="https://www.npmjs.com/package/@51degrees/ua-parser-js">↗ 51Degrees <u>UAParser</u></a> to get started.</p>
</td>
</tr>
<tr>
<td colspan="2">
<a href="https://opencollective.com/ua-parser-js">↗ Become a sponsor</a>
</td>
</tr>
</tbody>
</table>
@ -41,13 +46,11 @@ JavaScript library to detect Browser, Engine, OS, CPU, and Device type/model fro
# Documentation
### UAParser([user-agent:string][,extensions:object][,headers:object(since@1.1)])
In The Browser environment you dont need to pass the user-agent string to the function, you can just call the funtion and it should automatically get the string from the `window.navigator.userAgent`, but that is not the case in nodejs. The user-agent string must be passed in nodejs for the function to work.
Usually you can find the user agent in:
`request.headers["user-agent"]`.
In the browser environment you dont need to pass the user-agent string to the function, you can just call the funtion and it should automatically get the string from the `window.navigator.userAgent`, but that is not the case in nodejs. The user-agent string must be passed in' nodejs for the function to work. Usually you can find the user agent in: `request.headers["user-agent"]`.
## Constructor
When you call `UAParser` with the `new` keyword `UAParser` will return a new instance with an empty result object, you have to call one of the available methods to get the information from the user-agent string.
When you call `UAParser` with the `new` keyword, `UAParser` will return a new instance with an empty result object, you have to call one of the available methods to get the information from the user-agent string.
Like so:
* `new UAParser([user-agent:string][,extensions:object][,headers:object(since@1.1)])`
```js
@ -183,7 +186,7 @@ Ubuntu, Unix, VectorLinux, Viera, watchOS, WebOS, Windows [Phone/Mobile], Zenwal
* set UA string to be parsed
* returns current instance
#### * `is()` utility `since@1.1`
#### * `is():boolean` utility `since@1.1`
```js
// Is just a shorthand to check whether specified item has a property with equals value (case-insensitive)
@ -247,7 +250,7 @@ let engine = uap.getEngine();
engine.is("Blink"); // true
```
#### * `toString()` utility `since@1.1`
#### * `toString():string` utility `since@1.1`
```js
// Retrieve full-name values as a string
@ -288,9 +291,9 @@ engine.version; // "28.0.1500.95"
engine.toString(); // "Blink 28.0.1500.95"
```
#### * `withClientHints()` `since@1.1`
#### * `withClientHints():Promise<object>|Thenable<object>` `since@1.1`
Unlike reading user-agent data, accessing client-hints data in browser-environment must be done in an asynchronous way. Worry not, you can chain the UAParser's `get*` method with `withClientHints()` to read the client-hints data as well that will return the updated data as a `Promise`.
Unlike reading user-agent data, accessing client-hints data in browser-environment must be done in an asynchronous way. Worry not, you can chain the UAParser's `get*` method with `withClientHints()` to read the client-hints data as well that will return the updated data as a `Promise`. In nodejs-environment / browser-environment with non-secure context or without client-hints support (basically anything that's not chromium-based) this will return the updated data as a `Thenable` (can be chained with `then()`).
```js
(async function () {
@ -452,6 +455,8 @@ http.createServer(function (req, res) {
/* // BEGIN since@1.1 - you can also pass client-hints data to UAParser
// note: only works in secure context (https:// or localhost or file://)
var getHighEntropyValues = 'Sec-CH-UA-Full-Version-List, Sec-CH-UA-Mobile, Sec-CH-UA-Model, Sec-CH-UA-Platform, Sec-CH-UA-Platform-Version, Sec-CH-UA-Arch, Sec-CH-UA-Bitness';
res.setHeader('Accept-CH', getHighEntropyValues);
res.setHeader('Critical-CH', getHighEntropyValues);

View File

@ -909,7 +909,10 @@
return str ? str : UNDEF_TYPE;
};
UAParserData.prototype.withClientHints = function () {
// nodejs / non-client-hints browsers
if (!NAVIGATOR_UADATA) {
var HTTP_UACH = uaCH;
switch (itemType) {
case UA_BROWSER:
@ -923,52 +926,54 @@
case UA_OS:
return new UAParserOS(ua, rgxMap, HTTP_UACH).parseCH().get();
default :
return {
'ua' : ua,
'ua_ch' : uaCH,
'browser' : new UAParserBrowser(ua, rgxMap[UA_BROWSER], HTTP_UACH).parseCH().get(),
'cpu' : new UAParserCPU(ua, rgxMap[UA_CPU], HTTP_UACH).parseCH().get(),
'device' : new UAParserDevice(ua, rgxMap[UA_DEVICE], HTTP_UACH).parseCH().get(),
'engine' : new UAParserEngine(ua, rgxMap[UA_ENGINE]).get(),
'os' : new UAParserOS(ua, rgxMap[UA_OS], HTTP_UACH).parseCH().get()
};
return new UAParserResult(ua, rgxMap, HTTP_UACH)
.set('ua', ua)
.set('ua_ch', uaCH)
.set(UA_BROWSER, new UAParserBrowser(ua, rgxMap[UA_BROWSER], HTTP_UACH).parseCH().get())
.set(UA_CPU, new UAParserCPU(ua, rgxMap[UA_CPU], HTTP_UACH).parseCH().get())
.set(UA_DEVICE, new UAParserDevice(ua, rgxMap[UA_DEVICE], HTTP_UACH).parseCH().get())
.set(UA_ENGINE, new UAParserEngine(ua, rgxMap[UA_ENGINE]).get())
.set(UA_OS, new UAParserOS(ua, rgxMap[UA_OS], HTTP_UACH).parseCH().get())
.get();
}
}
// browsers based on chromium 85+
return NAVIGATOR_UADATA
.getHighEntropyValues(CH_ALL_VALUES)
.then(function (res) {
var JS_UACH = new UAParserDataCH(res, false),
browser = new UAParserBrowser(ua, rgxMap, JS_UACH).parseCH().get(),
cpu = new UAParserCPU(ua, ((itemType == UA_RESULT) ? rgxMap[UA_CPU] : rgxMap), JS_UACH).parseCH().get(),
device = new UAParserDevice(ua, rgxMap, JS_UACH).parseCH().get(),
engine = new UAParserEngine(ua, rgxMap).get(),
os = new UAParserOS(ua, rgxMap, JS_UACH).parseCH().get();
var JS_UACH = new UAParserDataCH(res, false);
switch (itemType) {
case UA_BROWSER:
return browser;
return UAParserBrowser(ua, rgxMap, JS_UACH).parseCH().get();
case UA_CPU:
return cpu;
return new UAParserCPU(ua, rgxMap, JS_UACH).parseCH().get();
case UA_DEVICE:
return device;
return new UAParserDevice(ua, rgxMap, JS_UACH).parseCH().get();
case UA_ENGINE:
return engine;
return new UAParserEngine(ua, rgxMap).get();
case UA_OS:
return os;
return new UAParserOS(ua, rgxMap, JS_UACH).parseCH().get();
default :
return {
'ua' : ua,
'ua_ch' : JS_UACH,
'browser' : browser,
'cpu' : cpu,
'device' : device,
'engine' : engine,
'os' : os
};
return new UAParserResult(ua, rgxMap, JS_UACH)
.set('ua', ua)
.set('ua_ch', JS_UACH)
.set(UA_BROWSER, new UAParserBrowser(ua, rgxMap[UA_BROWSER], JS_UACH).parseCH().get())
.set(UA_CPU, new UAParserCPU(ua, rgxMap[UA_CPU], JS_UACH).parseCH().get())
.set(UA_DEVICE, new UAParserDevice(ua, rgxMap[UA_DEVICE], JS_UACH).parseCH().get())
.set(UA_ENGINE, new UAParserEngine(ua, rgxMap[UA_ENGINE]).get())
.set(UA_OS, new UAParserOS(ua, rgxMap[UA_OS], JS_UACH).parseCH().get())
.get();
}
});
};
if (!NAVIGATOR_UADATA) {
UAParserData.prototype.then = function (cb) {
cb(this);
return this;
};
}
return new UAParserData();
})(data);
}