-
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.
🕵️♂️ Parse client hints headers easily
- Loading branch information
Showing
27 changed files
with
559 additions
and
566 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
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 |
---|---|---|
@@ -1,5 +1,2 @@ | ||
node_modules | ||
*.cjs | ||
*.mjs | ||
*.d.ts | ||
|
||
/index.* |
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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
.* | ||
src | ||
scripts | ||
jest.config.cjs | ||
tsconfig.json | ||
|
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 |
---|---|---|
@@ -1 +1 @@ | ||
17.6 | ||
20 |
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,2 @@ | ||
plugins: | ||
- "@trivago/prettier-plugin-sort-imports" |
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 @@ | ||
# Change Log | ||
|
||
## 1.0.0 | ||
|
||
Stable release: No breaking changes |
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 |
---|---|---|
@@ -1,51 +1,57 @@ | ||
# client-hints | ||
# client-hints [![](https://img.shields.io/npm/v/client-hints.svg?style=flat-square)](https://www.npmjs.com/package/client-hints) | ||
|
||
🕵️♂️ Parse client hints headers | ||
|
||
Use client hints, where available | ||
|
||
```js | ||
import { ClientHints } from 'client-hints' | ||
import { userAgentParser } from 'some-user-agent-parser-library' | ||
import { ClientHints } from "client-hints"; | ||
import { userAgentParser } from "some-user-agent-parser-library"; | ||
|
||
app.get('/api-endpoint', (req, res) => { | ||
const hints = new ClientHints(req.headers) | ||
app.get("/api-endpoint", (req, res) => { | ||
const hints = new ClientHints(req.headers); | ||
|
||
const isMobile = hints.mobile ?? (userAgentParser(req.get('user-agent')).device?.type === 'mobile') | ||
// ↑ cheap ↑ more expensive | ||
const isMobile = | ||
hints.mobile ?? // cheap | ||
userAgentParser(req.get("user-agent")).device?.type === "mobile"; // more expensive | ||
|
||
res.send( isMobile ? 'Mobile page' : 'Desktop page' ) | ||
}) | ||
res.send(isMobile ? "Mobile page" : "Desktop page"); | ||
}); | ||
``` | ||
# Available detections | ||
## Available detections | ||
All detections return `undefined` if the relevant header is not available. | ||
| Feature | Type | Header | Meaning | Adoption level | ||
| - | - | - | - | - | ||
| `vendorName` | _string_ | Sec-CH-UA / Sec-CH-UA-Full-Version-List | User Agent vendor version list | Experimental | ||
| `vendorVersion` | _string_ | Sec-CH-UA-Full-Version | User agent vendor version | Experimental | ||
| `mobile` | _boolean_ | Sec-CH-UA-Mobile | Boolean: Is this a mobile device | Experimental | ||
| `platform` | _string_ | Sec-CH-UA-Platform | Operating system name | Experimental | ||
| `platformVersion` | _string_ | Sec-CH-UA-Platform-Version | Operating system version | Experimental | ||
| `arch` | _string_ | Sec-CH-UA-Arch | CPU architecture | Experimental | ||
| `model` | _string_ | Sec-CH-UA-Model | Device model | Experimental | ||
| `fetchMode` | _string_ | Sec-Fetch-Mode | Navigation type | | ||
| `fetchDest`, `fetchDestination` | _string_ | Sec-Fetch-Dest | Resource type | | ||
| `fetchUser` | _boolean_ | Sec-Fetch-User | Was the request triggered by user action | | ||
| `fetchSite` | _string_ | Sec-Fetch-Site | Relationship to origin | | ||
| `dpr`, `devicePixelRatio` | _string_ | Content-DPR | Image device to pixel ratio | Experimental | ||
| `deviceMemory` | _number_ | Device-Memory | Appr. available RAM | Experimental | ||
| `dpr`, `devicePixelRatio` | _number_ | DPR | Device pixel ratio | Deprecated | ||
| `contentDpr`, `contentDevicePixelRatio` | _number_ | Content-DPR | Image device pixel ratio | Deprecated | ||
| `ect`, `effectiveConnectionType` | _string_ | ECT | Network profile (G) | Experimental | ||
| `downlink` | _number_ | Downlink | Network speed (Mbps) | Experimental | ||
| `viewportWidth` | _number_ | Viewport-Width | Layout viewport width | Deprecated | ||
| `width` | _number_ | Width | Resource desired width | Deprecated | ||
| Feature | Type | Header | Meaning | Adoption level | | ||
| --------------------------------------- | --------- | --------------------------------------- | ---------------------------------------- | -------------- | | ||
| `arch`, `architecture` | _string_ | Sec-CH-UA-Arch | CPU architecture | Experimental | | ||
| `contentDpr`, `contentDevicePixelRatio` | _number_ | Content-DPR | Image device pixel ratio | Deprecated | | ||
| `deviceMemory` | _number_ | Device-Memory | Appr. available RAM | Experimental | | ||
| `downlink` | _number_ | Downlink | Network speed (Mbps) | Experimental | | ||
| `dpr`, `devicePixelRatio` | _number_ | DPR | Device pixel ratio | Deprecated | | ||
| `dpr`, `devicePixelRatio` | _string_ | Content-DPR | Image device to pixel ratio | Experimental | | ||
| `ect`, `effectiveConnectionType` | _string_ | ECT | Network profile (G) | Experimental | | ||
| `fetchDest`, `fetchDestination` | _string_ | Sec-Fetch-Dest | Resource type | | ||
| `fetchMode` | _string_ | Sec-Fetch-Mode | Navigation type | | ||
| `fetchSite` | _string_ | Sec-Fetch-Site | Relationship to origin | | ||
| `fetchUser` | _boolean_ | Sec-Fetch-User | Was the request triggered by user action | | ||
| `mobile` | _boolean_ | Sec-CH-UA-Mobile | Boolean: Is this a mobile device | Experimental | | ||
| `model` | _string_ | Sec-CH-UA-Model | Device model | Experimental | | ||
| `platform` | _string_ | Sec-CH-UA-Platform | Operating system name | Experimental | | ||
| `platformVersion` | _string_ | Sec-CH-UA-Platform-Version | Operating system version | Experimental | | ||
| `purpose` | _string_ | Sec-Purpose | Resource purpose (prefetch) | Experimental | | ||
| `vendorName` | _string_ | Sec-CH-UA / Sec-CH-UA-Full-Version-List | User Agent vendor version list | Experimental | | ||
| `vendorVersion` | _string_ | Sec-CH-UA-Full-Version | User agent vendor version | Experimental | | ||
| `viewportWidth` | _number_ | Viewport-Width | Layout viewport width | Deprecated | | ||
| `width` | _number_ | Width | Resource desired width | Deprecated | | ||
## Client Hints Intruction Response Header | ||
Set the value of `Accept-CH` header to include the headers you want the browse to send. | ||
Example: | ||
``` | ||
Accept-CH: Sec-CH-UA-Mobile,Sec-CH-UA-Full-Version,Sec-CH-UA-Full-Version-List,Sec-CH-UA-Model,Sec-CH-UA-Platform,Sec-CH-UA-Platform-Version,ECT | ||
```plaintext | ||
Accept-CH: * | ||
``` |
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,4 @@ | ||
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ | ||
module.exports = { | ||
preset: "ts-jest", | ||
}; |
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 |
---|---|---|
@@ -1,5 +1,18 @@ | ||
#!/usr/bin/env bash | ||
|
||
esbuild src/index.js --outfile=index.cjs --format=cjs --bundle --target=node16 | ||
esbuild src/index.js --outfile=index.mjs --format=esm --bundle --target=node16 | ||
tsc src/index.js --declaration --allowJs --emitDeclarationOnly --outDir . | ||
failures=0 | ||
|
||
esbuild src/index.ts --outfile=index.cjs --format=cjs --bundle --sourcemap | ||
failures=$((failures + $?)) | ||
|
||
esbuild src/index.ts --outfile=index.mjs --format=esm --bundle --sourcemap | ||
failures=$((failures + $?)) | ||
|
||
tsc src/index.ts --declaration --emitDeclarationOnly --declarationMap --outDir types --target esnext --moduleResolution node | ||
failures=$((failures + $?)) | ||
|
||
mv types/index.d.ts . | ||
mv types/index.d.ts.map . | ||
rm -rf types | ||
|
||
exit $failures |
Oops, something went wrong.