@repobit/dex-utils 0.6.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/README.md +253 -0
- package/dist/src/user.d.ts +1 -1
- package/dist/src/user.js +15 -15
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,16 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [1.1.0](https://github.com/bitdefender/dex-core/compare/@repobit/dex-utils@0.6.1...@repobit/dex-utils@1.1.0) (2025-04-02)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* **DEX-22143:** added documentation ([a3e4bf0](https://github.com/bitdefender/dex-core/commit/a3e4bf0cc359f0b4aea939cb386d50230dfd62f6))
|
|
12
|
+
* **DEX-22143:** added unit tests ([977bfbf](https://github.com/bitdefender/dex-core/commit/977bfbfff0b0f3deda9af4bd29aad8e399ce88d3))
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
6
16
|
## [0.6.1](https://github.com/bitdefender/dex-core/compare/@repobit/dex-utils@0.6.0...@repobit/dex-utils@0.6.1) (2025-03-26)
|
|
7
17
|
|
|
8
18
|
|
package/README.md
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
# `@repobit/dex-utils`
|
|
2
|
+
|
|
3
|
+
# BD Web Utilities
|
|
4
|
+
|
|
5
|
+
BD Web Utilities is an npm package that provides a comprehensive suite of tools for modern web applications. It simplifies common tasks such as cookie management, page context extraction, user management, and user agent detection, while also offering utility functions for dynamic script loading and performance optimization through throttling and debouncing.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Overview
|
|
10
|
+
|
|
11
|
+
This package is designed to help developers manage essential aspects of web applications with ease. It includes:
|
|
12
|
+
|
|
13
|
+
- **Cookie Management:** A simple wrapper around js-cookie to handle browser cookies.
|
|
14
|
+
- **Page Context Extraction:** A class to manage and extract page information such as locale, country, language, and query parameters.
|
|
15
|
+
- **User Management:** An abstract class for handling user login, fingerprinting, geolocation, and locale determination.
|
|
16
|
+
- **User Agent Detection:** An abstract class for parsing the browser’s user agent string to identify operating system and browser details.
|
|
17
|
+
- **Utility Functions:** Standalone functions for dynamically loading scripts and optimizing function execution using throttling and debouncing.
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## Key Features
|
|
22
|
+
|
|
23
|
+
- **Modular Design:** Import only the components you need.
|
|
24
|
+
- **Intuitive API:** Simple classes with clear public methods, properties, and getters.
|
|
25
|
+
- **Performance Optimization:** Throttling and debouncing functions improve responsiveness.
|
|
26
|
+
- **Cross-Platform Support:** Robust user agent detection for adapting to different devices and browsers.
|
|
27
|
+
- **User-Centric:** Manage user context with automated locale and geolocation detection.
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## Installation
|
|
32
|
+
|
|
33
|
+
Install via npm or yarn:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
npm install @repobit/dex-utils
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
# Classes
|
|
42
|
+
|
|
43
|
+
## Cookies
|
|
44
|
+
|
|
45
|
+
### Description:
|
|
46
|
+
A wrapper around the js-cookie library (https://www.npmjs.com/package/js-cookie) for managing browser cookies. Alongside the already existing methods, we have also added the has function.
|
|
47
|
+
|
|
48
|
+
### Public Methods & Properties:
|
|
49
|
+
|
|
50
|
+
- ## set(name: string, value: string, options: {expires: number, path: string, ...}): void
|
|
51
|
+
Use Case: Set a cookie with the corresponding name, value and options. For expires you pass the number of days.
|
|
52
|
+
|
|
53
|
+
- ## get(name: string, options: { domain: string, ...}): string
|
|
54
|
+
Use Case: Get the cookie value if it exists.
|
|
55
|
+
|
|
56
|
+
- ## has(name: string): boolean
|
|
57
|
+
Use Case: Checks if a cookie with the specified name exists. This is useful before attempting to access or modify cookie data.
|
|
58
|
+
|
|
59
|
+
### Usage
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
Cookies.set('key', 'value', { expires: 1 });
|
|
63
|
+
|
|
64
|
+
if (Cookies.has('key')) {
|
|
65
|
+
console.log(Cookies.get('key'));
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
---
|
|
70
|
+
|
|
71
|
+
## Page
|
|
72
|
+
|
|
73
|
+
### Description:
|
|
74
|
+
Manages page-related context such as locale, country, language, page name, and URL query parameters.
|
|
75
|
+
|
|
76
|
+
### Public Properties:
|
|
77
|
+
|
|
78
|
+
- ## locale: string
|
|
79
|
+
Locale in the format `${string}-${string}` (e.g., "en-us"). It is passed as a parameter to the constructor so it is up to the developer to set it correctly.
|
|
80
|
+
|
|
81
|
+
- ## country: string
|
|
82
|
+
Derived from the locale (e.g., for "en-us", country is "us").
|
|
83
|
+
|
|
84
|
+
- ## language: string
|
|
85
|
+
Derived from the locale (e.g., for "en-us", language is "en").
|
|
86
|
+
|
|
87
|
+
- ## name: string
|
|
88
|
+
Identifier for the page. It's the last part of the link in most cases (e.g, for "https://www.example.com/page", the name should be "page"). It is passed as a parameter to the constructor so it is up to the developer how they wish to set it.
|
|
89
|
+
|
|
90
|
+
- ## queryParams: URLSearchParams
|
|
91
|
+
URL query parameters extracted from the current page link.
|
|
92
|
+
|
|
93
|
+
- ## environment: 'dev' | 'stage' | 'prod'
|
|
94
|
+
Current deployment environment. It is passed as a parameter to the constructor so it is up to the developer to set it correctly depending on the project.
|
|
95
|
+
|
|
96
|
+
### Public Methods:
|
|
97
|
+
|
|
98
|
+
- ## getParamValue(key: string): string | null
|
|
99
|
+
Use Case: Retrieves the value of a specific query parameter from the URL.
|
|
100
|
+
|
|
101
|
+
### Usage
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
const page = new Page('en-us', 'homepage', 'prod');
|
|
105
|
+
console.log(page.country); // Outputs: "us"
|
|
106
|
+
console.log(page.getParamValue('ref')); // Retrieves the value of 'ref' from the URL query parameters
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
---
|
|
110
|
+
|
|
111
|
+
## User
|
|
112
|
+
|
|
113
|
+
### Description:
|
|
114
|
+
An abstract class that manages user-related operations such as login, fingerprint generation, and geolocation-based locale determination.
|
|
115
|
+
|
|
116
|
+
### Public properties:
|
|
117
|
+
|
|
118
|
+
- ## info: Promise<UserInfoResult | null>
|
|
119
|
+
Logged-in user's information (if available) received from Connect.
|
|
120
|
+
|
|
121
|
+
- ## fingerprint: Promise<string | null>
|
|
122
|
+
A unique ID for the user.
|
|
123
|
+
|
|
124
|
+
- ## country: Promise<string>
|
|
125
|
+
The user's country based on geolocation.
|
|
126
|
+
|
|
127
|
+
- ## locale: Promise<string>
|
|
128
|
+
The user's locale (e.g., "en-us") fetched using his country.
|
|
129
|
+
|
|
130
|
+
### Public Methods:
|
|
131
|
+
|
|
132
|
+
- ## login(): Promise<void>
|
|
133
|
+
Use Case: Initiates the user login process. If the user is not logged in, this method redirects to the login endpoint.
|
|
134
|
+
|
|
135
|
+
### Usage
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
User.info.then(info => {
|
|
139
|
+
if (!info) {
|
|
140
|
+
User.login();
|
|
141
|
+
} else {
|
|
142
|
+
console.log('User Info:', info);
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
## UserAgent
|
|
150
|
+
|
|
151
|
+
### Description:
|
|
152
|
+
An abstract class that parses the user agent string to identify operating system and browser details. This is built upon the already existing cssua (https://cssuseragent.org/).
|
|
153
|
+
|
|
154
|
+
### Public Getters (Operating System):
|
|
155
|
+
|
|
156
|
+
- ## windows, macos, ios, android, linux
|
|
157
|
+
Use Case: Identify the user's operating system.
|
|
158
|
+
|
|
159
|
+
### Public Getters (Browser):
|
|
160
|
+
|
|
161
|
+
- ## edge, chrome, firefox, safari, opera, ie, vivaldi
|
|
162
|
+
Use Case: Determine the user's browser.
|
|
163
|
+
|
|
164
|
+
### Public Boolean Getters:
|
|
165
|
+
|
|
166
|
+
- ## isWindows, isMacos, isLinux, isUnix, isAndroid, isIos, isDesktop, isMobile
|
|
167
|
+
- ## isEdge, isChrome, isFirefox, isSafari, isOpera, isVivaldi, isIe
|
|
168
|
+
Use Case: Quickly check the device type and browser for conditional logic in the application.
|
|
169
|
+
|
|
170
|
+
### Computed Getters:
|
|
171
|
+
|
|
172
|
+
- ## os
|
|
173
|
+
Use Case: Returns a string representing the detected operating system.
|
|
174
|
+
|
|
175
|
+
- ## browser
|
|
176
|
+
Use Case: Returns a string representing the detected browser.
|
|
177
|
+
|
|
178
|
+
### Usage
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
181
|
+
if (UserAgent.isMobile) {
|
|
182
|
+
console.log('Operating System:', UserAgent.os);
|
|
183
|
+
} else {
|
|
184
|
+
console.log('Browser:', UserAgent.browser);
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
---
|
|
189
|
+
|
|
190
|
+
# Public Standalone Functions
|
|
191
|
+
|
|
192
|
+
## loadScript
|
|
193
|
+
|
|
194
|
+
### Description
|
|
195
|
+
Dynamically loads an external JavaScript file by appending a script element to the document head, ensuring the same script is not loaded multiple times. The return type of this call is a promise, making it ideal for checks where you need to make sure that the script was loaded successfully or unsuccessfully.
|
|
196
|
+
|
|
197
|
+
### Parameters:
|
|
198
|
+
|
|
199
|
+
- #### src: string – URL of the script.
|
|
200
|
+
- #### attrs: ScriptParameters – Optional attributes to add to the script element.
|
|
201
|
+
|
|
202
|
+
### Usage
|
|
203
|
+
|
|
204
|
+
```typescript
|
|
205
|
+
loadScript('https://example.com/some-library.js', { async: 'true' })
|
|
206
|
+
.then(() => console.log('Script loaded successfully'))
|
|
207
|
+
.catch(error => console.error('Error loading script:', error));
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
---
|
|
211
|
+
|
|
212
|
+
## throttle
|
|
213
|
+
|
|
214
|
+
### Description
|
|
215
|
+
Creates a throttled version of a function, ensuring it is executed at most once every specified delay period.
|
|
216
|
+
|
|
217
|
+
### Parameters:
|
|
218
|
+
|
|
219
|
+
- #### callback: Function – The function to throttle.
|
|
220
|
+
- #### delay: number – Delay in milliseconds (default is 250ms).
|
|
221
|
+
|
|
222
|
+
### Usage
|
|
223
|
+
|
|
224
|
+
```typescript
|
|
225
|
+
const throttledFunction = throttle(() => {
|
|
226
|
+
console.log('Throttled function executed');
|
|
227
|
+
}, 500);
|
|
228
|
+
|
|
229
|
+
window.addEventListener('resize', throttledFunction);
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
---
|
|
233
|
+
|
|
234
|
+
## debounce
|
|
235
|
+
|
|
236
|
+
### Description
|
|
237
|
+
Creates a debounced version of a function that delays its execution until a specified time has elapsed since the last invocation. Especially usefull when you know that there will be a lot of clicks on a button for example.
|
|
238
|
+
|
|
239
|
+
### Parameters:
|
|
240
|
+
|
|
241
|
+
- #### callback: Function – The function to throttle.
|
|
242
|
+
- #### delay: number – Delay in milliseconds (default is 250ms).
|
|
243
|
+
- #### immediate: boolean – If true, triggers the function on the leading edge instead of the trailing edge.
|
|
244
|
+
|
|
245
|
+
### Usage
|
|
246
|
+
|
|
247
|
+
```typescript
|
|
248
|
+
const debouncedFunction = debounce(() => {
|
|
249
|
+
console.log('Debounced function executed');
|
|
250
|
+
}, 300);
|
|
251
|
+
|
|
252
|
+
document.querySelector('input')?.addEventListener('input', debouncedFunction);
|
|
253
|
+
```
|
package/dist/src/user.d.ts
CHANGED
package/dist/src/user.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Constants } from
|
|
2
|
-
import Cookies from
|
|
3
|
-
import UserAgent from
|
|
1
|
+
import { Constants } from './constants.js';
|
|
2
|
+
import Cookies from './cookies.js';
|
|
3
|
+
import UserAgent from './user-agent/index.js';
|
|
4
4
|
export default class User {
|
|
5
5
|
static _info = this.staticInitialise();
|
|
6
6
|
static _fingerprint = this.getFingerprint();
|
|
@@ -35,13 +35,13 @@ export default class User {
|
|
|
35
35
|
if (!fingerprintNotExist && UserAgent.isWindows) {
|
|
36
36
|
try {
|
|
37
37
|
const fingerprintReq = await fetch(`${Constants.WWW_ONLY_ORIGIN}/site/Main/dummyPost?${Math.random()}`, {
|
|
38
|
-
method:
|
|
38
|
+
method: 'POST',
|
|
39
39
|
headers: {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
'Content-type': 'application/x-www-form-urlencoded',
|
|
41
|
+
'Pragma': 'no-cache',
|
|
42
|
+
'Cache-Control': 'no-store, no-cache, must-revalidate, max-age=0',
|
|
43
|
+
'Expires': 'Tue, 01 Jan 1971 02:00:00 GMT',
|
|
44
|
+
'BDUS_A312C09A2666456D9F2B2AA5D6B463D6': 'check.bitdefender'
|
|
45
45
|
}
|
|
46
46
|
});
|
|
47
47
|
if (fingerprintReq.ok && fingerprintReq.headers.has(Constants.FINGERPRINT_HEADER)) {
|
|
@@ -50,7 +50,7 @@ export default class User {
|
|
|
50
50
|
return fingerprint;
|
|
51
51
|
}
|
|
52
52
|
else {
|
|
53
|
-
Cookies.set(Constants.NO_FINGERPRINT_COOKIE_NAME,
|
|
53
|
+
Cookies.set(Constants.NO_FINGERPRINT_COOKIE_NAME, 'true', { expires: 1 });
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
catch { /* empty */ }
|
|
@@ -65,16 +65,16 @@ export default class User {
|
|
|
65
65
|
try {
|
|
66
66
|
const response = await fetch(`${Constants.PUBLIC_URL_ORIGIN}/geoip`);
|
|
67
67
|
if (!response.ok) {
|
|
68
|
-
return
|
|
68
|
+
return 'us';
|
|
69
69
|
}
|
|
70
70
|
const country = await response.json();
|
|
71
71
|
if (country.error_code) {
|
|
72
|
-
return
|
|
72
|
+
return 'us';
|
|
73
73
|
}
|
|
74
|
-
return country[
|
|
74
|
+
return country['country'].toLowerCase();
|
|
75
75
|
}
|
|
76
76
|
catch {
|
|
77
|
-
return
|
|
77
|
+
return 'us';
|
|
78
78
|
}
|
|
79
79
|
}
|
|
80
80
|
/** Getting the User's Locale */
|
|
@@ -86,7 +86,7 @@ export default class User {
|
|
|
86
86
|
return userGeoIpData[0].locale.toLowerCase();
|
|
87
87
|
}
|
|
88
88
|
catch {
|
|
89
|
-
return
|
|
89
|
+
return 'en-us';
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
92
|
/** attempt to log the user in.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@repobit/dex-utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Client for Utils",
|
|
5
5
|
"author": "Constantin Ioan Mihai <iconstantin@bitdefender.com>",
|
|
6
6
|
"homepage": "https://github.com/bitdefender/dex-core#readme",
|
|
@@ -38,5 +38,5 @@
|
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@types/js-cookie": "^3.0.6"
|
|
40
40
|
},
|
|
41
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "561794201a63fcd69042da59b6bc642f03d0dcd3"
|
|
42
42
|
}
|