@knsdev330/node-utils 1.0.1
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/LICENSE +21 -0
- package/README.md +85 -0
- package/dist/Cookies.d.ts +33 -0
- package/dist/Cookies.d.ts.map +1 -0
- package/dist/Cookies.js +54 -0
- package/dist/Cookies.js.map +1 -0
- package/dist/EApiCodes.d.ts +2 -0
- package/dist/EApiCodes.d.ts.map +1 -0
- package/dist/EApiCodes.js +2 -0
- package/dist/EApiCodes.js.map +1 -0
- package/dist/ErrorClasses.d.ts +78 -0
- package/dist/ErrorClasses.d.ts.map +1 -0
- package/dist/ErrorClasses.js +118 -0
- package/dist/ErrorClasses.js.map +1 -0
- package/dist/FBUtils.d.ts +22 -0
- package/dist/FBUtils.d.ts.map +1 -0
- package/dist/FBUtils.js +68 -0
- package/dist/FBUtils.js.map +1 -0
- package/dist/GoogleUtils.d.ts +43 -0
- package/dist/GoogleUtils.d.ts.map +1 -0
- package/dist/GoogleUtils.js +80 -0
- package/dist/GoogleUtils.js.map +1 -0
- package/dist/HMACUtils.d.ts +39 -0
- package/dist/HMACUtils.d.ts.map +1 -0
- package/dist/HMACUtils.js +61 -0
- package/dist/HMACUtils.js.map +1 -0
- package/dist/HttpCodes.d.ts +26 -0
- package/dist/HttpCodes.d.ts.map +1 -0
- package/dist/HttpCodes.js +25 -0
- package/dist/HttpCodes.js.map +1 -0
- package/dist/IDUtils.d.ts +28 -0
- package/dist/IDUtils.d.ts.map +1 -0
- package/dist/IDUtils.js +47 -0
- package/dist/IDUtils.js.map +1 -0
- package/dist/JwtUtils.d.ts +79 -0
- package/dist/JwtUtils.d.ts.map +1 -0
- package/dist/JwtUtils.js +94 -0
- package/dist/JwtUtils.js.map +1 -0
- package/dist/Nums.d.ts +101 -0
- package/dist/Nums.d.ts.map +1 -0
- package/dist/Nums.js +141 -0
- package/dist/Nums.js.map +1 -0
- package/dist/PasswordUtils.d.ts +58 -0
- package/dist/PasswordUtils.d.ts.map +1 -0
- package/dist/PasswordUtils.js +82 -0
- package/dist/PasswordUtils.js.map +1 -0
- package/dist/Rand.d.ts +44 -0
- package/dist/Rand.d.ts.map +1 -0
- package/dist/Rand.js +72 -0
- package/dist/Rand.js.map +1 -0
- package/dist/TurnstileValidator.d.ts +57 -0
- package/dist/TurnstileValidator.d.ts.map +1 -0
- package/dist/TurnstileValidator.js +99 -0
- package/dist/TurnstileValidator.js.map +1 -0
- package/dist/bnUtils.d.ts +15 -0
- package/dist/bnUtils.d.ts.map +1 -0
- package/dist/bnUtils.js +67 -0
- package/dist/bnUtils.js.map +1 -0
- package/dist/getEnvv.d.ts +21 -0
- package/dist/getEnvv.d.ts.map +1 -0
- package/dist/getEnvv.js +68 -0
- package/dist/getEnvv.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/tryCatch.d.ts +23 -0
- package/dist/tryCatch.d.ts.map +1 -0
- package/dist/tryCatch.js +29 -0
- package/dist/tryCatch.js.map +1 -0
- package/dist/types.d.ts +8 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/validators.d.ts +7 -0
- package/dist/validators.d.ts.map +1 -0
- package/dist/validators.js +11 -0
- package/dist/validators.js.map +1 -0
- package/package.json +80 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HMAC utilities for use across projects.
|
|
3
|
+
* Import via: @sajjat/utils/HMACUtils
|
|
4
|
+
*/
|
|
5
|
+
declare class hMACUtils {
|
|
6
|
+
/**
|
|
7
|
+
* Hash a token using a secret key.
|
|
8
|
+
* @param secret - The secret key to use for hashing.
|
|
9
|
+
* @param token - The token to hash.
|
|
10
|
+
* @returns The hashed token.
|
|
11
|
+
*/
|
|
12
|
+
readonly hash: (secret: string, token: string) => {
|
|
13
|
+
data: string;
|
|
14
|
+
error: null;
|
|
15
|
+
} | {
|
|
16
|
+
data: null;
|
|
17
|
+
error: Error;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Hash a token using a secret key.
|
|
21
|
+
* @param secret - The secret key to use for hashing.
|
|
22
|
+
* @param token - The token to hash.
|
|
23
|
+
* @returns The hashed token.
|
|
24
|
+
*
|
|
25
|
+
* @throws {Error} If the hashing fails.
|
|
26
|
+
*/
|
|
27
|
+
readonly hashOrThrow: (secret: string, token: string) => string;
|
|
28
|
+
/**
|
|
29
|
+
* Timing-safe comparison of two strings or buffers.
|
|
30
|
+
* Prevents timing attacks by comparing the full length of the values.
|
|
31
|
+
* @param a - First value.
|
|
32
|
+
* @param b - Second value.
|
|
33
|
+
* @returns True if values match, false otherwise.
|
|
34
|
+
*/
|
|
35
|
+
readonly compare: (a: string | Buffer, b: string | Buffer) => boolean;
|
|
36
|
+
}
|
|
37
|
+
export declare const HMACUtils: hMACUtils;
|
|
38
|
+
export {};
|
|
39
|
+
//# sourceMappingURL=HMACUtils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HMACUtils.d.ts","sourceRoot":"","sources":["../src/HMACUtils.ts"],"names":[],"mappings":"AAGA;;;GAGG;AACH,cAAM,SAAS;IAEd;;;;;OAKG;IACH,QAAQ,CAAC,IAAI,GAAI,QAAQ,MAAM,EAAE,OAAO,MAAM,KAAG;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,IAAI,CAAA;KAAE,GAAG;QAAE,IAAI,EAAE,IAAI,CAAC;QAAC,KAAK,EAAE,KAAK,CAAA;KAAE,CAa5G;IAED;;;;;;;OAOG;IACH,QAAQ,CAAC,WAAW,GAAI,QAAQ,MAAM,EAAE,OAAO,MAAM,KAAG,MAAM,CAE7D;IAED;;;;;;OAMG;IACH,QAAQ,CAAC,OAAO,GAAI,GAAG,MAAM,GAAG,MAAM,EAAE,GAAG,MAAM,GAAG,MAAM,KAAG,OAAO,CAcnE;CACD;AAED,eAAO,MAAM,SAAS,WAAkB,CAAC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { createHmac, timingSafeEqual } from "crypto";
|
|
2
|
+
/**
|
|
3
|
+
* HMAC utilities for use across projects.
|
|
4
|
+
* Import via: @sajjat/utils/HMACUtils
|
|
5
|
+
*/
|
|
6
|
+
class hMACUtils {
|
|
7
|
+
/**
|
|
8
|
+
* Hash a token using a secret key.
|
|
9
|
+
* @param secret - The secret key to use for hashing.
|
|
10
|
+
* @param token - The token to hash.
|
|
11
|
+
* @returns The hashed token.
|
|
12
|
+
*/
|
|
13
|
+
hash = (secret, token) => {
|
|
14
|
+
try {
|
|
15
|
+
return {
|
|
16
|
+
data: this.hashOrThrow(secret, token),
|
|
17
|
+
error: null
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
return {
|
|
22
|
+
data: null,
|
|
23
|
+
error: error
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Hash a token using a secret key.
|
|
29
|
+
* @param secret - The secret key to use for hashing.
|
|
30
|
+
* @param token - The token to hash.
|
|
31
|
+
* @returns The hashed token.
|
|
32
|
+
*
|
|
33
|
+
* @throws {Error} If the hashing fails.
|
|
34
|
+
*/
|
|
35
|
+
hashOrThrow = (secret, token) => {
|
|
36
|
+
return createHmac("sha256", secret).update(token, "utf8").digest("hex");
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Timing-safe comparison of two strings or buffers.
|
|
40
|
+
* Prevents timing attacks by comparing the full length of the values.
|
|
41
|
+
* @param a - First value.
|
|
42
|
+
* @param b - Second value.
|
|
43
|
+
* @returns True if values match, false otherwise.
|
|
44
|
+
*/
|
|
45
|
+
compare = (a, b) => {
|
|
46
|
+
try {
|
|
47
|
+
const aBuf = Buffer.isBuffer(a) ? a : Buffer.from(a, 'utf8');
|
|
48
|
+
const bBuf = Buffer.isBuffer(b) ? b : Buffer.from(b, 'utf8');
|
|
49
|
+
// timingSafeEqual throws if lengths do not match
|
|
50
|
+
if (aBuf.length !== bBuf.length) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
return timingSafeEqual(aBuf, bBuf);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
export const HMACUtils = new hMACUtils();
|
|
61
|
+
//# sourceMappingURL=HMACUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HMACUtils.js","sourceRoot":"","sources":["../src/HMACUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAC;AAGrD;;;GAGG;AACH,MAAM,SAAS;IAEd;;;;;OAKG;IACM,IAAI,GAAG,CAAC,MAAc,EAAE,KAAa,EAAgE,EAAE;QAC/G,IAAI,CAAC;YACJ,OAAO;gBACN,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC,MAAM,EAAE,KAAK,CAAC;gBACrC,KAAK,EAAE,IAAI;aACX,CAAC;QACH,CAAC;QACD,OAAO,KAAK,EAAE,CAAC;YACd,OAAO;gBACN,IAAI,EAAE,IAAI;gBACV,KAAK,EAAE,KAAc;aACrB,CAAC;QACH,CAAC;IACF,CAAC,CAAA;IAED;;;;;;;OAOG;IACM,WAAW,GAAG,CAAC,MAAc,EAAE,KAAa,EAAU,EAAE;QAChE,OAAO,UAAU,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACzE,CAAC,CAAA;IAED;;;;;;OAMG;IACM,OAAO,GAAG,CAAC,CAAkB,EAAE,CAAkB,EAAW,EAAE;QACtE,IAAI,CAAC;YACJ,MAAM,IAAI,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;YAC7D,MAAM,IAAI,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;YAE7D,iDAAiD;YACjD,IAAI,IAAI,CAAC,MAAM,KAAK,IAAI,CAAC,MAAM,EAAE,CAAC;gBACjC,OAAO,KAAK,CAAC;YACd,CAAC;YAED,OAAO,eAAe,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACpC,CAAC;QAAC,MAAM,CAAC;YACR,OAAO,KAAK,CAAC;QACd,CAAC;IACF,CAAC,CAAA;CACD;AAED,MAAM,CAAC,MAAM,SAAS,GAAG,IAAI,SAAS,EAAE,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export declare const HttpCodes: {
|
|
2
|
+
readonly UNKNOWN_ERROR: 0;
|
|
3
|
+
readonly OK: 200;
|
|
4
|
+
readonly CREATED: 201;
|
|
5
|
+
readonly NO_CONTENT: 204;
|
|
6
|
+
readonly BAD_REQUEST: 400;
|
|
7
|
+
readonly UNAUTHORIZED: 401;
|
|
8
|
+
readonly FORBIDDEN: 403;
|
|
9
|
+
readonly NOT_FOUND: 404;
|
|
10
|
+
readonly INTERNAL_SERVER_ERROR: 500;
|
|
11
|
+
readonly SERVICE_UNAVAILABLE: 503;
|
|
12
|
+
readonly BAD_GATEWAY: 502;
|
|
13
|
+
readonly TIMEOUT: 504;
|
|
14
|
+
readonly ACCEPTED: 202;
|
|
15
|
+
readonly PARTIAL_CONTENT: 206;
|
|
16
|
+
readonly MOVED_PERMANENTLY: 301;
|
|
17
|
+
readonly FOUND: 302;
|
|
18
|
+
readonly SEE_OTHER: 303;
|
|
19
|
+
readonly NOT_MODIFIED: 304;
|
|
20
|
+
readonly USE_PROXY: 305;
|
|
21
|
+
readonly TEMPORARY_REDIRECT: 307;
|
|
22
|
+
readonly PERMANENT_REDIRECT: 308;
|
|
23
|
+
readonly TOO_MANY_REQUESTS: 429;
|
|
24
|
+
};
|
|
25
|
+
export type THttpCodes = typeof HttpCodes;
|
|
26
|
+
//# sourceMappingURL=HttpCodes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HttpCodes.d.ts","sourceRoot":"","sources":["../src/HttpCodes.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,SAAS;;;;;;;;;;;;;;;;;;;;;;;CAuBZ,CAAC;AAEX,MAAM,MAAM,UAAU,GAAG,OAAO,SAAS,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export const HttpCodes = {
|
|
2
|
+
UNKNOWN_ERROR: 0,
|
|
3
|
+
OK: 200,
|
|
4
|
+
CREATED: 201,
|
|
5
|
+
NO_CONTENT: 204,
|
|
6
|
+
BAD_REQUEST: 400,
|
|
7
|
+
UNAUTHORIZED: 401,
|
|
8
|
+
FORBIDDEN: 403,
|
|
9
|
+
NOT_FOUND: 404,
|
|
10
|
+
INTERNAL_SERVER_ERROR: 500,
|
|
11
|
+
SERVICE_UNAVAILABLE: 503,
|
|
12
|
+
BAD_GATEWAY: 502,
|
|
13
|
+
TIMEOUT: 504,
|
|
14
|
+
ACCEPTED: 202,
|
|
15
|
+
PARTIAL_CONTENT: 206,
|
|
16
|
+
MOVED_PERMANENTLY: 301,
|
|
17
|
+
FOUND: 302,
|
|
18
|
+
SEE_OTHER: 303,
|
|
19
|
+
NOT_MODIFIED: 304,
|
|
20
|
+
USE_PROXY: 305,
|
|
21
|
+
TEMPORARY_REDIRECT: 307,
|
|
22
|
+
PERMANENT_REDIRECT: 308,
|
|
23
|
+
TOO_MANY_REQUESTS: 429,
|
|
24
|
+
};
|
|
25
|
+
//# sourceMappingURL=HttpCodes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HttpCodes.js","sourceRoot":"","sources":["../src/HttpCodes.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,SAAS,GAAG;IACxB,aAAa,EAAE,CAAC;IAChB,EAAE,EAAE,GAAG;IACP,OAAO,EAAE,GAAG;IACZ,UAAU,EAAE,GAAG;IACf,WAAW,EAAE,GAAG;IAChB,YAAY,EAAE,GAAG;IACjB,SAAS,EAAE,GAAG;IACd,SAAS,EAAE,GAAG;IACd,qBAAqB,EAAE,GAAG;IAC1B,mBAAmB,EAAE,GAAG;IACxB,WAAW,EAAE,GAAG;IAChB,OAAO,EAAE,GAAG;IACZ,QAAQ,EAAE,GAAG;IACb,eAAe,EAAE,GAAG;IACpB,iBAAiB,EAAE,GAAG;IACtB,KAAK,EAAE,GAAG;IACV,SAAS,EAAE,GAAG;IACd,YAAY,EAAE,GAAG;IACjB,SAAS,EAAE,GAAG;IACd,kBAAkB,EAAE,GAAG;IACvB,kBAAkB,EAAE,GAAG;IACvB,iBAAiB,EAAE,GAAG;CACb,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utilities for generating and validating IDs.
|
|
3
|
+
*/
|
|
4
|
+
declare class idUtils {
|
|
5
|
+
/**
|
|
6
|
+
* Generates a crypto-random RFC4122 v4 UUID.
|
|
7
|
+
* @returns A UUID string (e.g. `xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx`).
|
|
8
|
+
*/
|
|
9
|
+
readonly gen: () => string;
|
|
10
|
+
/**
|
|
11
|
+
* Validates a UUID.
|
|
12
|
+
* @param id - The UUID to validate.
|
|
13
|
+
* @returns True if the UUID is valid, false otherwise.
|
|
14
|
+
*/
|
|
15
|
+
readonly validateOrThrow: (id: unknown) => id is string;
|
|
16
|
+
/**
|
|
17
|
+
* Validates that the provided value is a UUID string.
|
|
18
|
+
* @param id - Value to validate.
|
|
19
|
+
* @returns `true` when `id` is a valid UUID string.
|
|
20
|
+
*/
|
|
21
|
+
readonly validate: (id: unknown) => id is string;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Singleton instance of ID utilities.
|
|
25
|
+
*/
|
|
26
|
+
export declare const IDUtils: idUtils;
|
|
27
|
+
export {};
|
|
28
|
+
//# sourceMappingURL=IDUtils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IDUtils.d.ts","sourceRoot":"","sources":["../src/IDUtils.ts"],"names":[],"mappings":"AAKA;;GAEG;AACH,cAAM,OAAO;IAEZ;;;OAGG;IACH,SAAgB,GAAG,eAElB;IAED;;;;OAIG;IACH,SAAgB,eAAe,GAAI,IAAI,OAAO,KAAG,EAAE,IAAI,MAAM,CAO5D;IAED;;;;OAIG;IACH,SAAgB,QAAQ,GAAI,IAAI,OAAO,KAAG,EAAE,IAAI,MAAM,CAUrD;CACD;AAED;;GAEG;AACH,eAAO,MAAM,OAAO,SAAgB,CAAC"}
|
package/dist/IDUtils.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { BadRequest } from "./ErrorClasses.js";
|
|
2
|
+
import { randUuid } from "./Rand.js";
|
|
3
|
+
import { validateUUID } from "./validators.js";
|
|
4
|
+
/**
|
|
5
|
+
* Utilities for generating and validating IDs.
|
|
6
|
+
*/
|
|
7
|
+
class idUtils {
|
|
8
|
+
/**
|
|
9
|
+
* Generates a crypto-random RFC4122 v4 UUID.
|
|
10
|
+
* @returns A UUID string (e.g. `xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx`).
|
|
11
|
+
*/
|
|
12
|
+
gen = () => {
|
|
13
|
+
return randUuid();
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Validates a UUID.
|
|
17
|
+
* @param id - The UUID to validate.
|
|
18
|
+
* @returns True if the UUID is valid, false otherwise.
|
|
19
|
+
*/
|
|
20
|
+
validateOrThrow = (id) => {
|
|
21
|
+
if (this.validate(id)) {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
throw new BadRequest('ID__INVALID');
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Validates that the provided value is a UUID string.
|
|
30
|
+
* @param id - Value to validate.
|
|
31
|
+
* @returns `true` when `id` is a valid UUID string.
|
|
32
|
+
*/
|
|
33
|
+
validate = (id) => {
|
|
34
|
+
if (typeof id === 'string' &&
|
|
35
|
+
validateUUID(id)) {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Singleton instance of ID utilities.
|
|
45
|
+
*/
|
|
46
|
+
export const IDUtils = new idUtils();
|
|
47
|
+
//# sourceMappingURL=IDUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IDUtils.js","sourceRoot":"","sources":["../src/IDUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AACrC,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAG/C;;GAEG;AACH,MAAM,OAAO;IAEZ;;;OAGG;IACa,GAAG,GAAG,GAAG,EAAE;QAC1B,OAAO,QAAQ,EAAE,CAAC;IACnB,CAAC,CAAA;IAED;;;;OAIG;IACa,eAAe,GAAG,CAAC,EAAW,EAAgB,EAAE;QAC/D,IAAI,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC;YACvB,OAAO,IAAI,CAAC;QACb,CAAC;aACI,CAAC;YACL,MAAM,IAAI,UAAU,CAAC,aAAa,CAAC,CAAC;QACrC,CAAC;IACF,CAAC,CAAA;IAED;;;;OAIG;IACa,QAAQ,GAAG,CAAC,EAAW,EAAgB,EAAE;QACxD,IACC,OAAO,EAAE,KAAK,QAAQ;YACtB,YAAY,CAAC,EAAE,CAAC,EACf,CAAC;YACF,OAAO,IAAI,CAAC;QACb,CAAC;aACI,CAAC;YACL,OAAO,KAAK,CAAC;QACd,CAAC;IACF,CAAC,CAAA;CACD;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,IAAI,OAAO,EAAE,CAAC"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JWT utilities for use across projects.
|
|
3
|
+
* Import via: @sajjat/utils/JwtUtils
|
|
4
|
+
*/
|
|
5
|
+
declare class jwtUtils {
|
|
6
|
+
/**
|
|
7
|
+
* Signs a JWT (HS256) and returns the compact token.
|
|
8
|
+
* @template PayloadType - The type of the payload to sign.
|
|
9
|
+
* @param secret - Symmetric signing secret.
|
|
10
|
+
* @param data - Payload (object, string, or Buffer).
|
|
11
|
+
* @param expiresMs - Milliseconds until expiry (ignored if payload already sets `exp`).
|
|
12
|
+
* @returns Compact JWT string.
|
|
13
|
+
* @throws {InternalServerError} If signing fails.
|
|
14
|
+
*/
|
|
15
|
+
readonly signOrThrow: <PayloadType extends object | string | Buffer = "Error: a type template is required">(secret: string, data: NoInfer<PayloadType>, expiresMs: number) => string;
|
|
16
|
+
/**
|
|
17
|
+
* @deprecated Use `signOrThrow` — this is JWT signing, not encryption.
|
|
18
|
+
*/
|
|
19
|
+
readonly encryptOrThrow: <PayloadType extends object | string | Buffer = "Error: a type template is required">(secret: string, data: NoInfer<PayloadType>, expiresMs: number) => string;
|
|
20
|
+
/**
|
|
21
|
+
* Signs a JWT (HS256); returns result or error object.
|
|
22
|
+
* @template PayloadType - The type of the payload to sign.
|
|
23
|
+
*/
|
|
24
|
+
readonly sign: <PayloadType extends object | string | Buffer = "Error: a type template is required">(secret: string, data: NoInfer<PayloadType>, expiresMs: number) => {
|
|
25
|
+
data: PayloadType;
|
|
26
|
+
error: null;
|
|
27
|
+
} | {
|
|
28
|
+
data: null;
|
|
29
|
+
error: Error;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* @deprecated Use `sign`.
|
|
33
|
+
*/
|
|
34
|
+
readonly encrypt: <PayloadType extends object | string | Buffer = "Error: a type template is required">(secret: string, data: NoInfer<PayloadType>, expiresMs: number) => {
|
|
35
|
+
data: PayloadType;
|
|
36
|
+
error: null;
|
|
37
|
+
} | {
|
|
38
|
+
data: null;
|
|
39
|
+
error: Error;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Verifies a JWT (HS256) and returns the payload.
|
|
43
|
+
* @template PayloadType - Expected payload shape after verification.
|
|
44
|
+
* @param secret - Symmetric signing secret.
|
|
45
|
+
* @param token - Compact JWT string.
|
|
46
|
+
* @returns Decoded payload.
|
|
47
|
+
* @throws {BadRequest} If `token` is not a string.
|
|
48
|
+
* @throws {Unauthorized} If the token is invalid or expired.
|
|
49
|
+
*/
|
|
50
|
+
readonly verifyOrThrow: <PayloadType extends object | string | Buffer = "Error: a type template is required">(secret: string, token: unknown) => PayloadType;
|
|
51
|
+
/**
|
|
52
|
+
* @deprecated Use `verifyOrThrow` — this is JWT verification, not decryption.
|
|
53
|
+
*/
|
|
54
|
+
readonly decryptOrThrow: <PayloadType extends object | string | Buffer = "Error: a type template is required">(secret: string, token: unknown) => PayloadType;
|
|
55
|
+
/**
|
|
56
|
+
* Verifies a JWT (HS256); returns payload or error object.
|
|
57
|
+
* @template PayloadType - Expected payload shape after verification.
|
|
58
|
+
*/
|
|
59
|
+
readonly verify: <PayloadType extends object | string | Buffer = "Error: a type template is required">(secret: string, token: unknown) => {
|
|
60
|
+
data: PayloadType;
|
|
61
|
+
error: null;
|
|
62
|
+
} | {
|
|
63
|
+
data: null;
|
|
64
|
+
error: Error;
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* @deprecated Use `verify`.
|
|
68
|
+
*/
|
|
69
|
+
readonly decrypt: <PayloadType extends object | string | Buffer = "Error: a type template is required">(secret: string, token: unknown) => {
|
|
70
|
+
data: PayloadType;
|
|
71
|
+
error: null;
|
|
72
|
+
} | {
|
|
73
|
+
data: null;
|
|
74
|
+
error: Error;
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
export declare const JwtUtils: jwtUtils;
|
|
78
|
+
export {};
|
|
79
|
+
//# sourceMappingURL=JwtUtils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JwtUtils.d.ts","sourceRoot":"","sources":["../src/JwtUtils.ts"],"names":[],"mappings":"AAGA;;;GAGG;AACH,cAAM,QAAQ;IAEb;;;;;;;;OAQG;IACH,QAAQ,CAAC,WAAW,GAAI,WAAW,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,oCAAoC,EAC1G,QAAQ,MAAM,EACd,MAAM,OAAO,CAAC,WAAW,CAAC,EAC1B,WAAW,MAAM,KACf,MAAM,CAaR;IAED;;OAEG;IACH,QAAQ,CAAC,cAAc,GAAI,WAAW,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,oCAAoC,EAC7G,QAAQ,MAAM,EACd,MAAM,OAAO,CAAC,WAAW,CAAC,EAC1B,WAAW,MAAM,KACf,MAAM,CAA2D;IAEpE;;;OAGG;IACH,QAAQ,CAAC,IAAI,GAAI,WAAW,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,oCAAoC,EACnG,QAAQ,MAAM,EACd,MAAM,OAAO,CAAC,WAAW,CAAC,EAC1B,WAAW,MAAM,KACf;QAAE,IAAI,EAAE,WAAW,CAAC;QAAC,KAAK,EAAE,IAAI,CAAA;KAAE,GAAG;QAAE,IAAI,EAAE,IAAI,CAAC;QAAC,KAAK,EAAE,KAAK,CAAA;KAAE,CAOnE;IAED;;OAEG;IACH,QAAQ,CAAC,OAAO,GAAI,WAAW,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,oCAAoC,EACtG,QAAQ,MAAM,EACd,MAAM,OAAO,CAAC,WAAW,CAAC,EAC1B,WAAW,M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ken";
|
|
3
|
+
/**
|
|
4
|
+
* JWT utilities for use across projects.
|
|
5
|
+
* Import via: @sajjat/utils/JwtUtils
|
|
6
|
+
*/
|
|
7
|
+
class jwtUtils {
|
|
8
|
+
/**
|
|
9
|
+
* Signs a JWT (HS256) and returns the compact token.
|
|
10
|
+
* @template PayloadType - The type of the payload to sign.
|
|
11
|
+
* @param secret - Symmetric signing secret.
|
|
12
|
+
* @param data - Payload (object, string, or Buffer).
|
|
13
|
+
* @param expiresMs - Milliseconds until expiry (ignored if payload already sets `exp`).
|
|
14
|
+
* @returns Compact JWT string.
|
|
15
|
+
* @throws {InternalServerError} If signing fails.
|
|
16
|
+
*/
|
|
17
|
+
signOrThrow = (secret, data, expiresMs) => {
|
|
18
|
+
try {
|
|
19
|
+
const payload = data;
|
|
20
|
+
const options = { algorithm: 'HS256' };
|
|
21
|
+
// Do not pass expiresIn when payload already has exp (jsonwebtoken v9 throws otherwise)
|
|
22
|
+
if (payload.exp === undefined) {
|
|
23
|
+
options.expiresIn = `${expiresMs}ms`;
|
|
24
|
+
}
|
|
25
|
+
return JWT.sign(data, secret, options);
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
throw new InternalServerError('JWT__ENCRYPTION_FAILED');
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* @deprecated Use `signOrThrow` — this is JWT signing, not encryption.
|
|
33
|
+
*/
|
|
34
|
+
encryptOrThrow = (secret, data, expiresMs) => this.signOrThrow(secret, data, expiresMs);
|
|
35
|
+
/**
|
|
36
|
+
* Signs a JWT (HS256); returns result or error object.
|
|
37
|
+
* @template PayloadType - The type of the payload to sign.
|
|
38
|
+
*/
|
|
39
|
+
sign = (secret, data, expiresMs) => {
|
|
40
|
+
try {
|
|
41
|
+
return { data: this.signOrThrow(secret, data, expiresMs), error: null };
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
return { data: null, error: error };
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* @deprecated Use `sign`.
|
|
49
|
+
*/
|
|
50
|
+
encrypt = (secret, data, expiresMs) => this.sign(secret, data, expiresMs);
|
|
51
|
+
/**
|
|
52
|
+
* Verifies a JWT (HS256) and returns the payload.
|
|
53
|
+
* @template PayloadType - Expected payload shape after verification.
|
|
54
|
+
* @param secret - Symmetric signing secret.
|
|
55
|
+
* @param token - Compact JWT string.
|
|
56
|
+
* @returns Decoded payload.
|
|
57
|
+
* @throws {BadRequest} If `token` is not a string.
|
|
58
|
+
* @throws {Unauthorized} If the token is invalid or expired.
|
|
59
|
+
*/
|
|
60
|
+
verifyOrThrow = (secret, token) => {
|
|
61
|
+
if (typeof token !== 'string')
|
|
62
|
+
throw new BadRequest('JWT__INVALID_OR_EXPIRED');
|
|
63
|
+
try {
|
|
64
|
+
return JWT.verify(token, secret, {
|
|
65
|
+
algorithms: ['HS256'],
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
throw new Unauthorized('JWT__INVALID_OR_EXPIRED');
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* @deprecated Use `verifyOrThrow` — this is JWT verification, not decryption.
|
|
74
|
+
*/
|
|
75
|
+
decryptOrThrow = (secret, token) => this.verifyOrThrow(secret, token);
|
|
76
|
+
/**
|
|
77
|
+
* Verifies a JWT (HS256); returns payload or error object.
|
|
78
|
+
* @template PayloadType - Expected payload shape after verification.
|
|
79
|
+
*/
|
|
80
|
+
verify = (secret, token) => {
|
|
81
|
+
try {
|
|
82
|
+
return { data: this.verifyOrThrow(secret, token), error: null };
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
return { data: null, error: error };
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* @deprecated Use `verify`.
|
|
90
|
+
*/
|
|
91
|
+
decrypt = (secret, token) => this.verify(secret, token);
|
|
92
|
+
}
|
|
93
|
+
export const JwtUtils = new jwtUtils();
|
|
94
|
+
//# sourceMappingURL=JwtUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JwtUtils.js","sourceRoot":"","sources":["../src/JwtUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,mBAAmB,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAClF,OAAO,GAAG,MAAM,cAAc,CAAC;AAE/B;;;GAGG;AACH,MAAM,QAAQ;IAEb;;;;;;;;OAQG;IACM,WAAW,GAAG,CACtB,MAAc,EACd,IAA0B,EAC1B,SAAiB,EACR,EAAE;QACX,IAAI,CAAC;YACJ,MAAM,OAAO,GAAG,IAA+B,CAAC;YAChD,MAAM,OAAO,GAAoB,EAAE,SAAS,EAAE,OAAO,EAAE,CAAC;YACxD,wFAAwF;YACxF,IAAI,OAAO,CAAC,GAAG,KAAK,SAAS,EAAE,CAAC;gBAC/B,OAAO,CAAC,SAAS,GAAG,GAAG,SAAS,IAAI,CAAC;YACtC,CAAC;YACD,OAAO,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;QACxC,CAAC;QACD,MAAM,CAAC;YACN,MAAM,IAAI,mBAAmB,CAAC,wBAAwB,CAAC,CAAC;QACzD,CAAC;IACF,CAAC,CAAA;IAED;;OAEG;IACM,cAAc,GAAG,CACzB,MAAc,EACd,IAA0B,EAC1B,SAAiB,EACR,EAAE,CAAC,IAAI,CAAC,WAAW,CAAc,MAAM,EAAE,IAAI,EAAE,SAAS,CAAC,CAAC;IAEpE;;;OAGG;IACM,IAAI,GAAG,CACf,MAAc,EACd,IAA0B,EAC1B,SAAiB,EACmD,EAAE;QACtE,IAAI,CAAC;YACJ,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,WAAW,CAAc,MAAM,EAAE,IAAI,EAAE,SAAS,CAAgB,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;QACrG,CAAC;Q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,IAAI,CAAC,MAAM,CAAc,MAAM,EAAE,KAAK,CAAC,CAAC;CACzC;AAED,MAAM,CAAC,MAAM,QAAQ,GAAG,IAAI,QAAQ,EAAE,CAAC"}
|
package/dist/Nums.d.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Clamp a number to an inclusive range.
|
|
3
|
+
* @param value - Input number.
|
|
4
|
+
* @param min - Inclusive lower bound.
|
|
5
|
+
* @param max - Inclusive upper bound.
|
|
6
|
+
* @returns `value` constrained to `[min, max]`.
|
|
7
|
+
*/
|
|
8
|
+
export declare const clamp: (value: number, min: number, max: number) => number;
|
|
9
|
+
/**
|
|
10
|
+
* Check whether a number falls within an inclusive range.
|
|
11
|
+
* @param value - Number to test.
|
|
12
|
+
* @param min - Inclusive lower bound.
|
|
13
|
+
* @param max - Inclusive upper bound.
|
|
14
|
+
* @returns `true` when `min <= value <= max`; otherwise `false`.
|
|
15
|
+
*/
|
|
16
|
+
export declare const isBetween: (value: number, min: number, max: number) => boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Round a number to a fixed number of decimal places.
|
|
19
|
+
* @param value - Number to round.
|
|
20
|
+
* @param decimals - Decimal places to keep.
|
|
21
|
+
* @returns Rounded number.
|
|
22
|
+
*/
|
|
23
|
+
export declare const roundTo: (value: number, decimals: number) => number;
|
|
24
|
+
/**
|
|
25
|
+
* Round a number down to a fixed number of decimal places.
|
|
26
|
+
* @param value - Number to floor.
|
|
27
|
+
* @param decimals - Decimal places to keep.
|
|
28
|
+
* @returns Floored number.
|
|
29
|
+
*/
|
|
30
|
+
export declare const floorTo: (value: number, decimals: number) => number;
|
|
31
|
+
/**
|
|
32
|
+
* Round a number up to a fixed number of decimal places.
|
|
33
|
+
* @param value - Number to ceil.
|
|
34
|
+
* @param decimals - Decimal places to keep.
|
|
35
|
+
* @returns Ceiled number.
|
|
36
|
+
*/
|
|
37
|
+
export declare const ceilTo: (value: number, decimals: number) => number;
|
|
38
|
+
/**
|
|
39
|
+
* Parse a value into an integer safely.
|
|
40
|
+
* @param value - Value to parse.
|
|
41
|
+
* @param radix - Numeric base for parsing string inputs.
|
|
42
|
+
* @returns Parsed integer, or `null` for `null`/`undefined`/`NaN`.
|
|
43
|
+
*/
|
|
44
|
+
export declare const parseIntSafe: (value: string | number | null | undefined, radix?: number) => number | null;
|
|
45
|
+
/**
|
|
46
|
+
* Parse a value into a floating-point number safely.
|
|
47
|
+
* @param value - Value to parse.
|
|
48
|
+
* @returns Parsed number, or `null` for `null`/`undefined`/`NaN`.
|
|
49
|
+
*/
|
|
50
|
+
export declare const parseFloatSafe: (value: string | number | null | undefined) => number | null;
|
|
51
|
+
/**
|
|
52
|
+
* Type guard for finite numbers.
|
|
53
|
+
* @param value - Unknown input.
|
|
54
|
+
* @returns `true` when `value` is a finite `number`.
|
|
55
|
+
*/
|
|
56
|
+
export declare const isFiniteNumber: (value: unknown) => value is number;
|
|
57
|
+
/**
|
|
58
|
+
* Type guard for integer numbers.
|
|
59
|
+
* @param value - Unknown input.
|
|
60
|
+
* @returns `true` when `value` is an integer `number`.
|
|
61
|
+
*/
|
|
62
|
+
export declare const isInteger: (value: unknown) => value is number;
|
|
63
|
+
/**
|
|
64
|
+
* Format a number using `Intl.NumberFormat`.
|
|
65
|
+
* @param value - Number to format.
|
|
66
|
+
* @param locale - BCP47 locale string.
|
|
67
|
+
* @param options - Number format options.
|
|
68
|
+
* @returns Formatted number string.
|
|
69
|
+
*/
|
|
70
|
+
export declare const formatNumber: (value: number, locale?: string, options?: Intl.NumberFormatOptions) => string;
|
|
71
|
+
/**
|
|
72
|
+
* Format a number with group separators.
|
|
73
|
+
* @param value - Number to format.
|
|
74
|
+
* @param locale - BCP47 locale string. (default: 'en')
|
|
75
|
+
* @param decimals - Optional fixed fraction digits.
|
|
76
|
+
* @returns Formatted string with commas/grouping.
|
|
77
|
+
*/
|
|
78
|
+
export declare const formatWithCommas: (value: number, locale?: string, decimals?: number) => string;
|
|
79
|
+
/**
|
|
80
|
+
* Calculate percentage of `part` out of `total`.
|
|
81
|
+
* @param part - Numerator.
|
|
82
|
+
* @param total - Denominator.
|
|
83
|
+
* @param decimals - Decimal places in the result.
|
|
84
|
+
* @returns Percentage value. Returns `0` if `total` is `0`.
|
|
85
|
+
*/
|
|
86
|
+
export declare const percentage: (part: number, total: number, decimals?: number) => number;
|
|
87
|
+
/**
|
|
88
|
+
* Convert unknown input to a finite number with fallback.
|
|
89
|
+
* @param value - Value to convert.
|
|
90
|
+
* @param fallback - Value used when conversion is not finite.
|
|
91
|
+
* @returns Finite number from input, or `fallback`.
|
|
92
|
+
*/
|
|
93
|
+
export declare const toNumber: (value: unknown, fallback: number) => number;
|
|
94
|
+
/**
|
|
95
|
+
* Convert unknown input to an integer with fallback.
|
|
96
|
+
* @param value - Value to convert.
|
|
97
|
+
* @param fallback - Value used when conversion is not an integer.
|
|
98
|
+
* @returns Integer from input, or `fallback`.
|
|
99
|
+
*/
|
|
100
|
+
export declare const toInt: (value: unknown, fallback: number) => number;
|
|
101
|
+
//# sourceMappingURL=Nums.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Nums.d.ts","sourceRoot":"","sources":["../src/Nums.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,eAAO,MAAM,KAAK,GAAI,OAAO,MAAM,EAAE,KAAK,MAAM,EAAE,KAAK,MAAM,KAAG,MAC5B,CAAC;AAErC;;;;;;GAMG;AACH,eAAO,MAAM,SAAS,GAAI,OAAO,MAAM,EAAE,KAAK,MAAM,EAAE,KAAK,MAAM,KAAG,OACvC,CAAC;AAE9B;;;;;GAKG;AACH,eAAO,MAAM,OAAO,GAAI,OAAO,MAAM,EAAE,UAAU,MAAM,KAAG,MAGzD,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,OAAO,GAAI,OAAO,MAAM,EAAE,UAAU,MAAM,KAAG,MAGzD,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,MAAM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