mirror of
https://github.com/faisalman/ua-parser-js.git
synced 2025-09-27 07:58:45 +03:00
Returns withClientHints()
as Thenable
in nodejs / non-client-hints browsers
This commit is contained in:
parent
f8dde65d54
commit
c78346d3b4
21
readme.md
21
readme.md
@ -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><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>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td colspan="2">
|
||||||
|
<a href="https://opencollective.com/ua-parser-js">↗ Become a sponsor</a>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
@ -41,13 +46,11 @@ JavaScript library to detect Browser, Engine, OS, CPU, and Device type/model fro
|
|||||||
# Documentation
|
# Documentation
|
||||||
### UAParser([user-agent:string][,extensions:object][,headers:object(since@1.1)])
|
### 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.
|
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"]`.
|
||||||
Usually you can find the user agent in:
|
|
||||||
`request.headers["user-agent"]`.
|
|
||||||
|
|
||||||
|
|
||||||
## Constructor
|
## 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:
|
Like so:
|
||||||
* `new UAParser([user-agent:string][,extensions:object][,headers:object(since@1.1)])`
|
* `new UAParser([user-agent:string][,extensions:object][,headers:object(since@1.1)])`
|
||||||
```js
|
```js
|
||||||
@ -183,7 +186,7 @@ Ubuntu, Unix, VectorLinux, Viera, watchOS, WebOS, Windows [Phone/Mobile], Zenwal
|
|||||||
* set UA string to be parsed
|
* set UA string to be parsed
|
||||||
* returns current instance
|
* returns current instance
|
||||||
|
|
||||||
#### * `is()` utility `since@1.1`
|
#### * `is():boolean` utility `since@1.1`
|
||||||
|
|
||||||
```js
|
```js
|
||||||
// Is just a shorthand to check whether specified item has a property with equals value (case-insensitive)
|
// 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
|
engine.is("Blink"); // true
|
||||||
```
|
```
|
||||||
|
|
||||||
#### * `toString()` utility `since@1.1`
|
#### * `toString():string` utility `since@1.1`
|
||||||
|
|
||||||
```js
|
```js
|
||||||
// Retrieve full-name values as a string
|
// Retrieve full-name values as a string
|
||||||
@ -288,9 +291,9 @@ engine.version; // "28.0.1500.95"
|
|||||||
engine.toString(); // "Blink 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
|
```js
|
||||||
(async function () {
|
(async function () {
|
||||||
@ -452,6 +455,8 @@ http.createServer(function (req, res) {
|
|||||||
|
|
||||||
/* // BEGIN since@1.1 - you can also pass client-hints data to UAParser
|
/* // 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';
|
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('Accept-CH', getHighEntropyValues);
|
||||||
res.setHeader('Critical-CH', getHighEntropyValues);
|
res.setHeader('Critical-CH', getHighEntropyValues);
|
||||||
|
@ -909,7 +909,10 @@
|
|||||||
return str ? str : UNDEF_TYPE;
|
return str ? str : UNDEF_TYPE;
|
||||||
};
|
};
|
||||||
UAParserData.prototype.withClientHints = function () {
|
UAParserData.prototype.withClientHints = function () {
|
||||||
|
|
||||||
|
// nodejs / non-client-hints browsers
|
||||||
if (!NAVIGATOR_UADATA) {
|
if (!NAVIGATOR_UADATA) {
|
||||||
|
|
||||||
var HTTP_UACH = uaCH;
|
var HTTP_UACH = uaCH;
|
||||||
switch (itemType) {
|
switch (itemType) {
|
||||||
case UA_BROWSER:
|
case UA_BROWSER:
|
||||||
@ -923,52 +926,54 @@
|
|||||||
case UA_OS:
|
case UA_OS:
|
||||||
return new UAParserOS(ua, rgxMap, HTTP_UACH).parseCH().get();
|
return new UAParserOS(ua, rgxMap, HTTP_UACH).parseCH().get();
|
||||||
default :
|
default :
|
||||||
return {
|
return new UAParserResult(ua, rgxMap, HTTP_UACH)
|
||||||
'ua' : ua,
|
.set('ua', ua)
|
||||||
'ua_ch' : uaCH,
|
.set('ua_ch', uaCH)
|
||||||
'browser' : new UAParserBrowser(ua, rgxMap[UA_BROWSER], HTTP_UACH).parseCH().get(),
|
.set(UA_BROWSER, new UAParserBrowser(ua, rgxMap[UA_BROWSER], HTTP_UACH).parseCH().get())
|
||||||
'cpu' : new UAParserCPU(ua, rgxMap[UA_CPU], HTTP_UACH).parseCH().get(),
|
.set(UA_CPU, new UAParserCPU(ua, rgxMap[UA_CPU], HTTP_UACH).parseCH().get())
|
||||||
'device' : new UAParserDevice(ua, rgxMap[UA_DEVICE], HTTP_UACH).parseCH().get(),
|
.set(UA_DEVICE, new UAParserDevice(ua, rgxMap[UA_DEVICE], HTTP_UACH).parseCH().get())
|
||||||
'engine' : new UAParserEngine(ua, rgxMap[UA_ENGINE]).get(),
|
.set(UA_ENGINE, new UAParserEngine(ua, rgxMap[UA_ENGINE]).get())
|
||||||
'os' : new UAParserOS(ua, rgxMap[UA_OS], HTTP_UACH).parseCH().get()
|
.set(UA_OS, new UAParserOS(ua, rgxMap[UA_OS], HTTP_UACH).parseCH().get())
|
||||||
};
|
.get();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// browsers based on chromium 85+
|
||||||
return NAVIGATOR_UADATA
|
return NAVIGATOR_UADATA
|
||||||
.getHighEntropyValues(CH_ALL_VALUES)
|
.getHighEntropyValues(CH_ALL_VALUES)
|
||||||
.then(function (res) {
|
.then(function (res) {
|
||||||
|
|
||||||
var JS_UACH = new UAParserDataCH(res, false),
|
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();
|
|
||||||
|
|
||||||
switch (itemType) {
|
switch (itemType) {
|
||||||
case UA_BROWSER:
|
case UA_BROWSER:
|
||||||
return browser;
|
return UAParserBrowser(ua, rgxMap, JS_UACH).parseCH().get();
|
||||||
case UA_CPU:
|
case UA_CPU:
|
||||||
return cpu;
|
return new UAParserCPU(ua, rgxMap, JS_UACH).parseCH().get();
|
||||||
case UA_DEVICE:
|
case UA_DEVICE:
|
||||||
return device;
|
return new UAParserDevice(ua, rgxMap, JS_UACH).parseCH().get();
|
||||||
case UA_ENGINE:
|
case UA_ENGINE:
|
||||||
return engine;
|
return new UAParserEngine(ua, rgxMap).get();
|
||||||
case UA_OS:
|
case UA_OS:
|
||||||
return os;
|
return new UAParserOS(ua, rgxMap, JS_UACH).parseCH().get();
|
||||||
default :
|
default :
|
||||||
return {
|
return new UAParserResult(ua, rgxMap, JS_UACH)
|
||||||
'ua' : ua,
|
.set('ua', ua)
|
||||||
'ua_ch' : JS_UACH,
|
.set('ua_ch', JS_UACH)
|
||||||
'browser' : browser,
|
.set(UA_BROWSER, new UAParserBrowser(ua, rgxMap[UA_BROWSER], JS_UACH).parseCH().get())
|
||||||
'cpu' : cpu,
|
.set(UA_CPU, new UAParserCPU(ua, rgxMap[UA_CPU], JS_UACH).parseCH().get())
|
||||||
'device' : device,
|
.set(UA_DEVICE, new UAParserDevice(ua, rgxMap[UA_DEVICE], JS_UACH).parseCH().get())
|
||||||
'engine' : engine,
|
.set(UA_ENGINE, new UAParserEngine(ua, rgxMap[UA_ENGINE]).get())
|
||||||
'os' : os
|
.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();
|
return new UAParserData();
|
||||||
})(data);
|
})(data);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user