Skip to content

Commit deae9fb

Browse files
Merge pull request #1 from moumen-soliman/1.1.0
Support Server v1.1.0
2 parents a5b42ee + 6ca8acf commit deae9fb

File tree

4 files changed

+213
-104
lines changed

4 files changed

+213
-104
lines changed

README.md

Lines changed: 141 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,24 @@
1212
+-----------------+-----------------+-----------------+
1313
```
1414

15-
A lightweight JavaScript/TypeScript package for generating hashed fingerprints based on device data. Includes options for customizable device information, IP address integration, and cookie management.
15+
A lightweight JavaScript/TypeScript package that generates unique hashed fingerprints for devices in both browser and server environments. The library allows customization of what data is included in the fingerprint, with options for saving the hash in cookies, passing headers for server-side use, and providing the user's IP directly.
16+
17+
[**NPM**](https://www.npmjs.com/package/hashed-device-fingerprint-js)
1618

1719
## Features
18-
- Device Data Fingerprinting: Collects user agent, screen resolution, platform, and more.
19-
- SHA-256 Hashing: Uses [js-sha256](https://www.npmjs.com/package/js-sha256) for secure hashing.
20-
- IP Address Integration: Fetch IP automatically, pass it manually, or disable it entirely.
21-
- Cookie Management: Optionally save hashed fingerprints in cookies.
22-
- Fully Configurable: Enable or disable specific device data fields as needed.
23-
- TypeScript Support: Fully typed for better integration with modern frameworks like Next.js.
20+
- Generate a unique fingerprint hash based on:
21+
- User Agent
22+
- Browser/System Language
23+
- Screen Resolution (Browser-only)
24+
- Platform (e.g., `Win32` or `Linux`)
25+
- Hardware Concurrency (Browser-only)
26+
- IP Address (with options to pass it manually or fetch automatically)
27+
- Support for browser and server environments:
28+
- **Browser**: Uses `navigator` and `screen` properties.
29+
- **Server**: Relies on HTTP headers and server-side properties.
30+
- Save the hashed fingerprint in a cookie (browser only) with a customizable expiration time.
31+
- Fully customizable: enable or disable specific data points.
32+
- Compatible with both `require` (CommonJS) and `import` (ES Modules).
2433

2534
## Installation
2635
Install the package using npm:
@@ -30,9 +39,9 @@ npm install hashed-device-fingerprint-js
3039
```
3140

3241
## Usage
33-
### Basic Usage
42+
### Default Behavior (Browser)
3443

35-
Generate a hashed fingerprint with all options enabled (default behavior):
44+
By default, all options are enabled. The library generates a fingerprint hash using all available device data and automatically fetches the user's IP address.
3645

3746
```typescript
3847
import { generateHashedFingerprint } from 'hashed-device-fingerprint-js';
@@ -42,6 +51,29 @@ generateHashedFingerprint()
4251
.catch(error => console.error('Error:', error));
4352
```
4453

54+
### Server-Side Usage
55+
56+
In a Server environment, pass HTTP headers to generate a fingerprint. Use the environment option to specify the server-side environment.
57+
58+
```typescript
59+
const { generateHashedFingerprint } = require('hashed-device-fingerprint-js');
60+
// import { generateHashedFingerprint } from 'hashed-device-fingerprint-js';
61+
62+
// Example HTTP headers
63+
const headers = {
64+
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
65+
'accept-language': 'en-US,en;q=0.9',
66+
'x-forwarded-for': '203.0.113.45',
67+
};
68+
69+
generateHashedFingerprint({
70+
environment: 'server',
71+
headers,
72+
})
73+
.then(hash => console.log('Fingerprint Hash:', hash))
74+
.catch(error => console.error('Error:', error));
75+
```
76+
4577
## Custom Options
4678
Customize the behavior by passing an options object:
4779

@@ -61,54 +93,6 @@ generateHashedFingerprint({
6193
.catch(error => console.error('Error:', error));
6294
```
6395

64-
## Options
65-
66-
- `saveToCookie`
67-
- Type: `boolean`
68-
- Default: `true`
69-
- Description: Save the generated hash in a cookie.
70-
71-
- `cookieExpiryDays`
72-
- Type: `number`
73-
- Default: `7`
74-
- Description: Number of days before the cookie expires.
75-
76-
- `useUserAgent`
77-
- Type: `boolean`
78-
- Default: `true`
79-
- Description: Include the browser's user agent string in the fingerprint.
80-
81-
- `useLanguage`
82-
- Type: `boolean`
83-
- Default: `true`
84-
- Description: Include the browser's language setting in the fingerprint.
85-
86-
- `useScreenResolution`
87-
- Type: `boolean`
88-
- Default: `true`
89-
- Description: Include the screen's resolution and color depth in the fingerprint.
90-
91-
- `usePlatform`
92-
- Type: `boolean`
93-
- Default: `true`
94-
- Description: Include platform information (e.g., "Win32", "MacIntel").
95-
96-
- `useConcurrency`
97-
- Type: `boolean`
98-
- Default: `true`
99-
- Description: Include the number of logical processors.
100-
101-
- `useIP`
102-
- Type: `boolean`
103-
- Default: `true`
104-
- Description: Fetch and include the user's IP address.
105-
106-
- `userIP`
107-
- Type: `string`
108-
- Default: `null`
109-
- Description: Provide an IP address manually (overrides API fetch).
110-
111-
11296
## IP Address Handling
11397
The IP address is included in the fingerprint based on these rules:
11498

@@ -140,6 +124,39 @@ generateHashedFingerprint({ userIP: '203.0.113.45' })
140124
.catch(error => console.error('Error:', error));
141125
```
142126

127+
### Server API
128+
```javascript
129+
const { generateHashedFingerprint } = require('hashed-device-fingerprint-js');
130+
131+
// Basic route
132+
app.get('/hash', async (req, res) => {
133+
// Define headers for fingerprint generation
134+
const headers = {
135+
'user-agent': req.headers['user-agent'] || 'Unknown',
136+
'accept-language': req.headers['accept-language'] || 'Unknown',
137+
'x-forwarded-for': req.headers['x-forwarded-for'] || req.connection.remoteAddress || 'Unknown',
138+
};
139+
140+
try {
141+
// Generate the fingerprint hash
142+
const hash = await generateHashedFingerprint({
143+
environment: 'server',
144+
headers,
145+
});
146+
147+
// Log the hash
148+
console.log('Generated Hash:', hash);
149+
150+
// Send the hash as the response
151+
res.send({ hash });
152+
} catch (error) {
153+
// Handle errors
154+
console.error('Error generating fingerprint:', error);
155+
res.status(500).send({ error: 'Failed to generate fingerprint' });
156+
}
157+
});
158+
```
159+
143160
## TypeScript Support
144161
The package includes TypeScript type definitions for all options and methods. Here’s a quick example:
145162

@@ -172,8 +189,74 @@ generateHashedFingerprint()
172189
});
173190
```
174191

192+
## Options
193+
194+
- `saveToCookie`
195+
- Type: `boolean`
196+
- Default: `true`
197+
- Description: Save the generated hash in a cookie.
198+
199+
- `cookieExpiryDays`
200+
- Type: `number`
201+
- Default: `7`
202+
- Description: Number of days before the cookie expires.
203+
204+
- `useUserAgent`
205+
- Type: `boolean`
206+
- Default: `true`
207+
- Description: Include the browser's user agent string in the fingerprint.
208+
209+
- `useLanguage`
210+
- Type: `boolean`
211+
- Default: `true`
212+
- Description: Include the browser's language setting in the fingerprint.
213+
214+
- `useScreenResolution`
215+
- Type: `boolean`
216+
- Default: `true`
217+
- Description: Include the screen's resolution and color depth in the fingerprint.
218+
219+
- `usePlatform`
220+
- Type: `boolean`
221+
- Default: `true`
222+
- Description: Include platform information (e.g., "Win32", "MacIntel").
223+
224+
- `useConcurrency`
225+
- Type: `boolean`
226+
- Default: `true`
227+
- Description: Include the number of logical processors.
228+
229+
- `useIP`
230+
- Type: `boolean`
231+
- Default: `true`
232+
- Description: Fetch and include the user's IP address.
233+
234+
- `userIP`
235+
- Type: `string`
236+
- Default: `null`
237+
- Description: Provide an IP address manually (overrides API fetch).
238+
239+
- `headers`
240+
- Type: `Record<string, string[]>`
241+
- Default: `{}`
242+
- Description: HTTP headers for server-side fingerprinting.
243+
244+
- `environment`
245+
- Type: `'browser' | 'server'`
246+
- Default: `Auto-detected`
247+
- Description: Specify the environment explicitly (e.g., `'browser'` or `'server'`).
248+
175249
## License
176250
This package is licensed under the [MIT License](https://opensource.org/license/mit/).
177251

178252
## Contributing
179-
Contributions are welcome! Feel free to submit issues or pull requests.
253+
- Fork the repository.
254+
- Create a new branch: git checkout -b feature-name.
255+
- Commit your changes: git commit -m 'Add feature'.
256+
- Push to the branch: git push origin feature-name.
257+
- Submit a pull request.
258+
259+
## Support
260+
If you encounter any issues or have questions, feel free to open an issue on [**Repo Issues**](https://github.com/moumen-soliman/hashed-device-fingerprint-js/issues).
261+
262+
Happy coding! 🚀

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hashed-device-fingerprint-js",
3-
"version": "1.0.7",
3+
"version": "1.1.0",
44
"homepage": "https://moumen-soliman.github.io/hashed-device-fingerprint-docs/",
55
"description": "A lightweight library for generating hashed fingerprints based on selected device data.",
66
"main": "dist/index.min.js",
@@ -33,6 +33,7 @@
3333
"js-sha256": "^0.11.0"
3434
},
3535
"devDependencies": {
36+
"@types/node": "^22.9.0",
3637
"esbuild": "^0.24.0",
3738
"typescript": "^5.6.3"
3839
},

0 commit comments

Comments
 (0)