dff-util 1.0.2 → 1.0.4
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/README.md +266 -10
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -2
- package/dist/main/const-type.d.ts +2 -0
- package/dist/main/http.d.ts +2 -2
- package/dist/main/http.js +4 -4
- package/dist/main/jwt-util.d.ts +27 -3
- package/dist/main/jwt-util.js +47 -5
- package/dist/main/util.d.ts +2 -2
- package/dist/main/util.js +5 -5
- package/package.json +1 -1
- package/dist/main/crypto-decrypt.d.ts +0 -1
- package/dist/main/crypto-decrypt.js +0 -28
- package/dist/main/crypto-encrypt.d.ts +0 -1
- package/dist/main/crypto-encrypt.js +0 -35
package/README.md
CHANGED
|
@@ -1,3 +1,172 @@
|
|
|
1
|
+
## Http
|
|
2
|
+
|
|
3
|
+
```
|
|
4
|
+
Http calls
|
|
5
|
+
```
|
|
6
|
+
|
|
7
|
+
## util
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
=> Random number generate.
|
|
11
|
+
AppRandomString(length, charset);
|
|
12
|
+
|
|
13
|
+
=> Generate code with Upper case without special char
|
|
14
|
+
AppUniqueCode();
|
|
15
|
+
|
|
16
|
+
=> Key give Upper case without special char by type
|
|
17
|
+
AppCodeByType(name, type);
|
|
18
|
+
|
|
19
|
+
=> Key give Upper case without special char
|
|
20
|
+
AppCode(name);
|
|
21
|
+
|
|
22
|
+
AppDaysBack(date, addDays, isDays);
|
|
23
|
+
|
|
24
|
+
AppAddDays(date, addDays, isDays);
|
|
25
|
+
|
|
26
|
+
AppUUID4();
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## JWT
|
|
30
|
+
|
|
31
|
+
#### Encode JWT Token with secret key, expiresIn in seconds (86400 = 1 day) default
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
JwtEncode(data, secret, expiresIn);
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
#### Deocde JWT Token
|
|
38
|
+
|
|
39
|
+
```
|
|
40
|
+
JwtDecode(token);
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
#### Verify JWT token with secret
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
JwtVerify(data, secret);
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
#### Check token expires or not
|
|
50
|
+
|
|
51
|
+
```
|
|
52
|
+
JwtValid(token);
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Constant Values
|
|
56
|
+
|
|
57
|
+
```
|
|
58
|
+
ConstValue = {
|
|
59
|
+
// Logical operators
|
|
60
|
+
AND: '&&',
|
|
61
|
+
OR: '||',
|
|
62
|
+
NOT: '!',
|
|
63
|
+
|
|
64
|
+
// Common special characters
|
|
65
|
+
AT: '@',
|
|
66
|
+
HASH: '#',
|
|
67
|
+
DOLLAR: '$',
|
|
68
|
+
PERCENT: '%',
|
|
69
|
+
CARET: '^',
|
|
70
|
+
AMPERSAND: '&',
|
|
71
|
+
ASTERISK: '*',
|
|
72
|
+
TILDE: '~',
|
|
73
|
+
COLON: ':',
|
|
74
|
+
SEMICOLON: ';',
|
|
75
|
+
DOUBLE_QUOTE: '"',
|
|
76
|
+
SINGLE_QUOTE: "'",
|
|
77
|
+
COMMA: ',',
|
|
78
|
+
PERIOD: '.',
|
|
79
|
+
SLASH: '/',
|
|
80
|
+
QUESTION_MARK: '?',
|
|
81
|
+
PLUS: '+',
|
|
82
|
+
UNDERSCORE: '_',
|
|
83
|
+
HYPHEN: '-',
|
|
84
|
+
EQUALS: '=',
|
|
85
|
+
BACKSLASH: '\\',
|
|
86
|
+
PIPE: '|',
|
|
87
|
+
|
|
88
|
+
// Space
|
|
89
|
+
SPACE: ' ',
|
|
90
|
+
EMPTY: '',
|
|
91
|
+
|
|
92
|
+
// Other
|
|
93
|
+
TRUE: 'true',
|
|
94
|
+
FALSE: 'false',
|
|
95
|
+
NULL: 'null',
|
|
96
|
+
UNDEFINED: 'undefined',
|
|
97
|
+
|
|
98
|
+
STR_AND: 'AND',
|
|
99
|
+
STR_OR: 'OR',
|
|
100
|
+
STR_NOT: 'NOT',
|
|
101
|
+
};
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Regx
|
|
105
|
+
|
|
106
|
+
```
|
|
107
|
+
RegExp = {
|
|
108
|
+
// Matches any non-alphanumeric characters
|
|
109
|
+
NON_ALPHA_NUMERIC: /[^\w\s]/g,
|
|
110
|
+
|
|
111
|
+
// Matches only numeric digits (0-9)
|
|
112
|
+
DIGITS_ONLY: /^\d+$/,
|
|
113
|
+
|
|
114
|
+
// Matches only alphabets (case-insensitive)
|
|
115
|
+
ALPHABETS_ONLY: /^[a-zA-Z]+$/,
|
|
116
|
+
|
|
117
|
+
// Matches alphanumeric characters (letters and digits)
|
|
118
|
+
ALPHA_NUMERIC: /^[a-zA-Z0-9]+$/,
|
|
119
|
+
|
|
120
|
+
// Matches an email address
|
|
121
|
+
EMAIL: /^[\w.%+-]+@[a-zA-Z\d.-]+\.[a-zA-Z]{2,}$/,
|
|
122
|
+
|
|
123
|
+
// Matches a URL
|
|
124
|
+
URL: /^(https?:\/\/)?([\w.-]+)\.([a-z]{2,6}\.?)(\/[\w.-]*)*\/?$/i,
|
|
125
|
+
|
|
126
|
+
// Matches a US phone number (formats like (123) 456-7890, 123-456-7890, etc.)
|
|
127
|
+
PHONE_US: /^(\(\d{3}\)\s?|\d{3}[-.\s]?)?\d{3}[-.\s]?\d{4}$/,
|
|
128
|
+
|
|
129
|
+
// Matches a password with at least one lowercase, one uppercase, one digit, and one special character
|
|
130
|
+
STRONG_PASSWORD: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[\W_]).{8,}$/,
|
|
131
|
+
|
|
132
|
+
// Matches whitespace characters
|
|
133
|
+
WHITESPACE: /\s+/g,
|
|
134
|
+
|
|
135
|
+
// Matches any HTML tags
|
|
136
|
+
HTML_TAG: /<\/?[\w\s="/.':;#-\/\?]+>/gi,
|
|
137
|
+
|
|
138
|
+
// Matches IPv4 addresses
|
|
139
|
+
IPV4: /^(25[0-5]|2[0-4]\d|1?\d?\d)(\.(25[0-5]|2[0-4]\d|1?\d?\d)){3}$/,
|
|
140
|
+
|
|
141
|
+
// Matches dates in YYYY-MM-DD format
|
|
142
|
+
DATE_YYYY_MM_DD: /^\d{4}-\d{2}-\d{2}$/,
|
|
143
|
+
|
|
144
|
+
// Matches time in HH:MM 24-hour format
|
|
145
|
+
TIME_HH_MM: /^([01]\d|2[0-3]):([0-5]\d)$/,
|
|
146
|
+
|
|
147
|
+
// Matches ISO 8601 datetime format (e.g., 2023-08-15T13:45:30Z, 2023-08-15T13:45:30+05:30)
|
|
148
|
+
ISO_DATETIME: /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/,
|
|
149
|
+
|
|
150
|
+
// Matches a hexadecimal color code
|
|
151
|
+
HEX_COLOR: /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/,
|
|
152
|
+
|
|
153
|
+
// Matches a UUID (version 4)
|
|
154
|
+
UUID_V4: /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
|
|
155
|
+
|
|
156
|
+
// Matches non-ASCII characters
|
|
157
|
+
NON_ASCII: /[^\x00-\x7F]/g,
|
|
158
|
+
|
|
159
|
+
// Matches line breaks (newlines)
|
|
160
|
+
LINE_BREAK: /\r?\n/g,
|
|
161
|
+
|
|
162
|
+
// Matches amounts in decimal format (e.g., 123.45, 0.99, 1000)
|
|
163
|
+
AMOUNT_DECIMAL: /^\d+(\.\d{1,2})?$/,
|
|
164
|
+
|
|
165
|
+
// Matches percentages from 0-100 or in decimal format (e.g., 25%, 99.99%, 100, 0.5)
|
|
166
|
+
PERCENTAGE: /^(100(\.0{1,2})?|0|0?\.\d{1,2}|[1-9]?\d(\.\d{1,2})?)%?$/,
|
|
167
|
+
};
|
|
168
|
+
```
|
|
169
|
+
|
|
1
170
|
## Session Info
|
|
2
171
|
|
|
3
172
|
```
|
|
@@ -9,7 +178,21 @@ export type SessionInfo = {
|
|
|
9
178
|
role: string;
|
|
10
179
|
requestId?: string;
|
|
11
180
|
token?: string;
|
|
181
|
+
iat?: number;
|
|
12
182
|
exp?: number;
|
|
183
|
+
key: string;
|
|
184
|
+
};
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## SessionUser
|
|
188
|
+
|
|
189
|
+
```
|
|
190
|
+
export type SessionUser = {
|
|
191
|
+
id: string;
|
|
192
|
+
email?: string;
|
|
193
|
+
mobile?: string;
|
|
194
|
+
name?: string;
|
|
195
|
+
role: string;
|
|
13
196
|
};
|
|
14
197
|
```
|
|
15
198
|
|
|
@@ -23,24 +206,97 @@ new ErrorResponse(message): {
|
|
|
23
206
|
}
|
|
24
207
|
```
|
|
25
208
|
|
|
26
|
-
|
|
209
|
+
## SuccessResponse
|
|
210
|
+
|
|
211
|
+
```
|
|
212
|
+
export const SuccessResponse = (data: any) => {
|
|
213
|
+
return {
|
|
214
|
+
status: 1,
|
|
215
|
+
type: 'SUCCESS',
|
|
216
|
+
data: data,
|
|
217
|
+
};
|
|
218
|
+
};
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
## file upload test
|
|
27
222
|
|
|
28
223
|
```
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
224
|
+
// ts-node test/fileupload.ts
|
|
225
|
+
|
|
226
|
+
import fs from 'fs/promises';
|
|
227
|
+
import { Http } from '../src';
|
|
228
|
+
import path from 'path';
|
|
229
|
+
|
|
230
|
+
const url = '/file';
|
|
231
|
+
const filename = 'testing2.xlsx';
|
|
232
|
+
const filePath = path.join(__dirname, filename);
|
|
233
|
+
console.log(filePath);
|
|
234
|
+
const main = async () => {
|
|
235
|
+
const data = await fs.readFile(filePath);
|
|
236
|
+
const resp = await Http.Upload(
|
|
237
|
+
url,
|
|
238
|
+
new File([data], filePath, {
|
|
239
|
+
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
240
|
+
lastModified: Date.now(),
|
|
241
|
+
}),
|
|
242
|
+
);
|
|
243
|
+
console.log('Http.Upload :: ', resp);
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
main();
|
|
34
247
|
```
|
|
35
248
|
|
|
36
|
-
##
|
|
249
|
+
## crypto
|
|
37
250
|
|
|
38
251
|
```
|
|
39
|
-
|
|
252
|
+
const hexStringToArrayBuffer = (hex: string) => {
|
|
253
|
+
const hexMatches = hex.match(/.{1,2}/g) || [];
|
|
254
|
+
const typedArray = new Uint8Array(hexMatches.map((byte) => parseInt(byte, 16)));
|
|
255
|
+
return typedArray.buffer;
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
export const Decrypt = async (text: string, key: string) => {
|
|
259
|
+
const [ivHex, dataHex] = text.split(':');
|
|
260
|
+
const iv = hexStringToArrayBuffer(ivHex);
|
|
261
|
+
const data = hexStringToArrayBuffer(dataHex);
|
|
262
|
+
const alg = { name: 'AES-CBC', iv: iv };
|
|
263
|
+
const cryptoKey = await crypto.subtle.importKey('raw', hexStringToArrayBuffer(key), alg, false, ['decrypt']);
|
|
264
|
+
const decrypted = await crypto.subtle.decrypt(alg, cryptoKey, data);
|
|
265
|
+
const dec = new TextDecoder();
|
|
266
|
+
return dec.decode(decrypted);
|
|
267
|
+
};
|
|
40
268
|
```
|
|
41
269
|
|
|
42
|
-
|
|
270
|
+
```
|
|
271
|
+
import * as crypto from 'crypto';
|
|
272
|
+
export const Encrypt = (text: string, key: string) => {
|
|
273
|
+
const iv = crypto.randomBytes(16);
|
|
274
|
+
const cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(key, 'hex'), iv);
|
|
275
|
+
let encrypted = cipher.update(text, 'utf8', 'hex');
|
|
276
|
+
encrypted += cipher.final('hex');
|
|
277
|
+
return iv.toString('hex') + ':' + encrypted;
|
|
278
|
+
};
|
|
279
|
+
```
|
|
43
280
|
|
|
44
281
|
```
|
|
45
|
-
|
|
282
|
+
// ts-node test/test.ts
|
|
283
|
+
import crypto from 'crypto';
|
|
284
|
+
|
|
285
|
+
import { Encrypt, Decrypt } from '../src/index';
|
|
286
|
+
|
|
287
|
+
//const key = crypto.randomBytes(32).toString('hex');
|
|
288
|
+
const key = '7e0a0ef142de22eb787b5001b36f188f3e7378abf250fc87bf38a4bda197236ef';
|
|
289
|
+
|
|
290
|
+
console.log(key);
|
|
291
|
+
|
|
292
|
+
const password = Encrypt('Admin!Admin', key);
|
|
293
|
+
|
|
294
|
+
console.log(password);
|
|
295
|
+
|
|
296
|
+
const main = async () => {
|
|
297
|
+
const resp = await Decrypt(password, key);
|
|
298
|
+
console.log(resp);
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
main();
|
|
46
302
|
```
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -15,8 +15,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./main/util"), exports);
|
|
18
|
-
__exportStar(require("./main/crypto-decrypt"), exports);
|
|
19
|
-
__exportStar(require("./main/crypto-encrypt"), exports);
|
|
20
18
|
__exportStar(require("./main/jwt-util"), exports);
|
|
21
19
|
__exportStar(require("./main/http"), exports);
|
|
22
20
|
__exportStar(require("./main/regexr"), exports);
|
package/dist/main/http.d.ts
CHANGED
|
@@ -23,6 +23,6 @@ export declare class Http {
|
|
|
23
23
|
static Put(url: string, data: any, headers: any, type?: RespType): Promise<any>;
|
|
24
24
|
static Delete(url: string, params: any, headers: any, type?: RespType): Promise<any>;
|
|
25
25
|
static Token(sessionInfo: any, appKey: string, apiKey?: string, type?: RespType): Promise<any>;
|
|
26
|
-
static Upload(fullUrl: string, file: File, isHeadersAddMultiForm?: boolean, data?: Record<string, any>, headers?: any, type?: RespType): Promise<any>;
|
|
27
|
-
static Download(fullUrl: string, params: any, headers?: any): Promise<Buffer>;
|
|
26
|
+
static Upload(fullUrl: string, file: File, isHeadersAddMultiForm?: boolean, data?: Record<string, any>, headers?: any, method?: 'POST' | 'PUT', type?: RespType): Promise<any>;
|
|
27
|
+
static Download(fullUrl: string, params: any, headers?: any, method?: string): Promise<Buffer>;
|
|
28
28
|
}
|
package/dist/main/http.js
CHANGED
|
@@ -127,7 +127,7 @@ class Http {
|
|
|
127
127
|
});
|
|
128
128
|
}
|
|
129
129
|
static Upload(fullUrl_1, file_1) {
|
|
130
|
-
return __awaiter(this, arguments, void 0, function* (fullUrl, file, isHeadersAddMultiForm = false, data = {}, headers = exports.BaseHeaders, type = RespType.JSON) {
|
|
130
|
+
return __awaiter(this, arguments, void 0, function* (fullUrl, file, isHeadersAddMultiForm = false, data = {}, headers = exports.BaseHeaders, method = 'PUT', type = RespType.JSON) {
|
|
131
131
|
const formData = new FormData();
|
|
132
132
|
console.log(file);
|
|
133
133
|
formData.append('file', file);
|
|
@@ -141,7 +141,7 @@ class Http {
|
|
|
141
141
|
}
|
|
142
142
|
try {
|
|
143
143
|
const requestOptions = {
|
|
144
|
-
method:
|
|
144
|
+
method: method,
|
|
145
145
|
body: formData,
|
|
146
146
|
headers: Object.assign({}, headers),
|
|
147
147
|
};
|
|
@@ -156,10 +156,10 @@ class Http {
|
|
|
156
156
|
});
|
|
157
157
|
}
|
|
158
158
|
static Download(fullUrl_1, params_1) {
|
|
159
|
-
return __awaiter(this, arguments, void 0, function* (fullUrl, params, headers = exports.HttpHeaders) {
|
|
159
|
+
return __awaiter(this, arguments, void 0, function* (fullUrl, params, headers = exports.HttpHeaders, method = 'GET') {
|
|
160
160
|
fullUrl = params ? `${fullUrl}?${new URLSearchParams(params).toString()}` : fullUrl;
|
|
161
161
|
try {
|
|
162
|
-
const requestOptions = { method:
|
|
162
|
+
const requestOptions = { method: method, headers: headers || exports.HttpHeaders };
|
|
163
163
|
const response = yield fetch(fullUrl, requestOptions);
|
|
164
164
|
const blob = yield response.blob();
|
|
165
165
|
const buffer = Buffer.from(yield blob.arrayBuffer());
|
package/dist/main/jwt-util.d.ts
CHANGED
|
@@ -1,4 +1,28 @@
|
|
|
1
1
|
import * as jwt from 'jsonwebtoken';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Encode JWT Token with secret key, expiresIn in seconds (86400 = 1 day) default
|
|
4
|
+
* @param data
|
|
5
|
+
* @param token
|
|
6
|
+
* @param expiresIn
|
|
7
|
+
* @returns
|
|
8
|
+
*/
|
|
9
|
+
export declare const JwtEncode: (data: any, secret: string, expiresIn?: number) => string;
|
|
10
|
+
/**
|
|
11
|
+
* Verify JWT token with secret
|
|
12
|
+
* @param data
|
|
13
|
+
* @param token
|
|
14
|
+
* @returns
|
|
15
|
+
*/
|
|
16
|
+
export declare const JwtVerify: (data: string, secret: string) => string | jwt.JwtPayload;
|
|
17
|
+
/**
|
|
18
|
+
* Deocde JWT Token
|
|
19
|
+
* @param token
|
|
20
|
+
* @returns
|
|
21
|
+
*/
|
|
22
|
+
export declare const JwtDecode: (token: string) => string | jwt.JwtPayload | null;
|
|
23
|
+
/**
|
|
24
|
+
* checking token expires or not
|
|
25
|
+
* @param token
|
|
26
|
+
* @returns
|
|
27
|
+
*/
|
|
28
|
+
export declare const JwtValid: (token: string) => boolean;
|
package/dist/main/jwt-util.js
CHANGED
|
@@ -23,20 +23,33 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.JwtDecode = exports.JwtVerify = exports.JwtEncode = void 0;
|
|
26
|
+
exports.JwtValid = exports.JwtDecode = exports.JwtVerify = exports.JwtEncode = void 0;
|
|
27
27
|
const jwt = __importStar(require("jsonwebtoken"));
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
/**
|
|
29
|
+
* Encode JWT Token with secret key, expiresIn in seconds (86400 = 1 day) default
|
|
30
|
+
* @param data
|
|
31
|
+
* @param token
|
|
32
|
+
* @param expiresIn
|
|
33
|
+
* @returns
|
|
34
|
+
*/
|
|
35
|
+
const JwtEncode = (data, secret, expiresIn = 86400) => {
|
|
36
|
+
return jwt.sign(data, secret, {
|
|
30
37
|
expiresIn: expiresIn,
|
|
31
38
|
});
|
|
32
39
|
};
|
|
33
40
|
exports.JwtEncode = JwtEncode;
|
|
34
|
-
|
|
41
|
+
/**
|
|
42
|
+
* Verify JWT token with secret
|
|
43
|
+
* @param data
|
|
44
|
+
* @param token
|
|
45
|
+
* @returns
|
|
46
|
+
*/
|
|
47
|
+
const JwtVerify = (data, secret) => {
|
|
35
48
|
if (data) {
|
|
36
49
|
try {
|
|
37
50
|
data = data.replace('jwt ', '').replace('JWT ', '');
|
|
38
51
|
data = data.replace('Bearer ', '').replace('bearer ', '').replace('BEARER', '');
|
|
39
|
-
return jwt.verify(data,
|
|
52
|
+
return jwt.verify(data, secret);
|
|
40
53
|
}
|
|
41
54
|
catch (err) {
|
|
42
55
|
throw { name: 'error', message: 'Token is not valid' };
|
|
@@ -47,6 +60,11 @@ const JwtVerify = (data, token) => {
|
|
|
47
60
|
}
|
|
48
61
|
};
|
|
49
62
|
exports.JwtVerify = JwtVerify;
|
|
63
|
+
/**
|
|
64
|
+
* Deocde JWT Token
|
|
65
|
+
* @param token
|
|
66
|
+
* @returns
|
|
67
|
+
*/
|
|
50
68
|
const JwtDecode = (token) => {
|
|
51
69
|
if (token) {
|
|
52
70
|
try {
|
|
@@ -64,3 +82,27 @@ const JwtDecode = (token) => {
|
|
|
64
82
|
}
|
|
65
83
|
};
|
|
66
84
|
exports.JwtDecode = JwtDecode;
|
|
85
|
+
/**
|
|
86
|
+
* checking token expires or not
|
|
87
|
+
* @param token
|
|
88
|
+
* @returns
|
|
89
|
+
*/
|
|
90
|
+
const JwtValid = (token) => {
|
|
91
|
+
try {
|
|
92
|
+
if (!token) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
const payloadBase64 = token.split('.')[1];
|
|
96
|
+
const payload = JSON.parse(atob(payloadBase64));
|
|
97
|
+
const currentTime = Math.floor(Date.now() / 1000);
|
|
98
|
+
if (payload.exp && payload.exp < currentTime) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
console.error('Invalid JWT:', error);
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
exports.JwtValid = JwtValid;
|
package/dist/main/util.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export declare const AppRandomString: (length: number, charset: string) => string;
|
|
2
2
|
export declare const AppUniqueCode: () => string;
|
|
3
|
-
export declare const
|
|
4
|
-
export declare const
|
|
3
|
+
export declare const AppCodeByType: (name: string, type?: null) => string;
|
|
4
|
+
export declare const AppCode: (name: string) => string | null;
|
|
5
5
|
export declare const AppDaysBack: (date: Date, backValue: number, isDays?: boolean) => Date;
|
|
6
6
|
export declare const AppAddDays: (date: Date, addDays: number, isDays?: boolean) => Date;
|
|
7
7
|
export declare const AppUUID4: () => string;
|
package/dist/main/util.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AppUUID4 = exports.AppAddDays = exports.AppDaysBack = exports.
|
|
3
|
+
exports.AppUUID4 = exports.AppAddDays = exports.AppDaysBack = exports.AppCode = exports.AppCodeByType = exports.AppUniqueCode = exports.AppRandomString = void 0;
|
|
4
4
|
const randomstring_1 = require("randomstring");
|
|
5
5
|
const regexr_1 = require("./regexr");
|
|
6
6
|
const AppRandomString = (length, charset) => {
|
|
@@ -18,7 +18,7 @@ const AppUniqueCode = () => {
|
|
|
18
18
|
return time.toString(36).toUpperCase();
|
|
19
19
|
};
|
|
20
20
|
exports.AppUniqueCode = AppUniqueCode;
|
|
21
|
-
const
|
|
21
|
+
const AppCodeByType = (name, type = null) => {
|
|
22
22
|
let str = '';
|
|
23
23
|
if (type) {
|
|
24
24
|
str = type + '_' + name;
|
|
@@ -31,8 +31,8 @@ const AppUniqueCodeByType = (name, type = null) => {
|
|
|
31
31
|
str = str.substr(0, 128);
|
|
32
32
|
return str.toUpperCase();
|
|
33
33
|
};
|
|
34
|
-
exports.
|
|
35
|
-
const
|
|
34
|
+
exports.AppCodeByType = AppCodeByType;
|
|
35
|
+
const AppCode = (name) => {
|
|
36
36
|
if (name == null)
|
|
37
37
|
return null;
|
|
38
38
|
let str = '';
|
|
@@ -41,7 +41,7 @@ const AppKey = (name) => {
|
|
|
41
41
|
str = str.replace(/\s/g, '_');
|
|
42
42
|
return str.toUpperCase();
|
|
43
43
|
};
|
|
44
|
-
exports.
|
|
44
|
+
exports.AppCode = AppCode;
|
|
45
45
|
const AppDaysBack = (date, backValue, isDays = true) => {
|
|
46
46
|
date = new Date(date);
|
|
47
47
|
if (isDays) {
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const Decrypt: (text: string, key: string) => Promise<string>;
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.Decrypt = void 0;
|
|
13
|
-
const hexStringToArrayBuffer = (hex) => {
|
|
14
|
-
const hexMatches = hex.match(/.{1,2}/g) || [];
|
|
15
|
-
const typedArray = new Uint8Array(hexMatches.map((byte) => parseInt(byte, 16)));
|
|
16
|
-
return typedArray.buffer;
|
|
17
|
-
};
|
|
18
|
-
const Decrypt = (text, key) => __awaiter(void 0, void 0, void 0, function* () {
|
|
19
|
-
const [ivHex, dataHex] = text.split(':');
|
|
20
|
-
const iv = hexStringToArrayBuffer(ivHex);
|
|
21
|
-
const data = hexStringToArrayBuffer(dataHex);
|
|
22
|
-
const alg = { name: 'AES-CBC', iv: iv };
|
|
23
|
-
const cryptoKey = yield crypto.subtle.importKey('raw', hexStringToArrayBuffer(key), alg, false, ['decrypt']);
|
|
24
|
-
const decrypted = yield crypto.subtle.decrypt(alg, cryptoKey, data);
|
|
25
|
-
const dec = new TextDecoder();
|
|
26
|
-
return dec.decode(decrypted);
|
|
27
|
-
});
|
|
28
|
-
exports.Decrypt = Decrypt;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const Encrypt: (text: string, key: string) => string;
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.Encrypt = void 0;
|
|
27
|
-
const crypto = __importStar(require("crypto"));
|
|
28
|
-
const Encrypt = (text, key) => {
|
|
29
|
-
const iv = crypto.randomBytes(16);
|
|
30
|
-
const cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(key, 'hex'), iv);
|
|
31
|
-
let encrypted = cipher.update(text, 'utf8', 'hex');
|
|
32
|
-
encrypted += cipher.final('hex');
|
|
33
|
-
return iv.toString('hex') + ':' + encrypted;
|
|
34
|
-
};
|
|
35
|
-
exports.Encrypt = Encrypt;
|