inibase 1.0.0-rc.4 → 1.0.0-rc.40

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/tsconfig.json DELETED
@@ -1,6 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2021",
4
- "moduleResolution": "node"
5
- }
6
- }
package/utils.ts DELETED
@@ -1,110 +0,0 @@
1
- import { FieldType } from ".";
2
- import { scryptSync, randomBytes, timingSafeEqual } from "crypto";
3
-
4
- export const encode = (
5
- input: string | number | boolean | null | (string | number | boolean | null)[]
6
- ) => {
7
- const secureString = (input: string | number | boolean | null) => {
8
- if (["true", "false"].includes((input ?? "").toString()))
9
- return input ? 1 : 0;
10
- return typeof input === "string"
11
- ? decodeURIComponent(input)
12
- .replaceAll("<", "&lt;")
13
- .replaceAll(">", "&gt;")
14
- .replaceAll(",", "%2C")
15
- .replaceAll("\n", "\\n")
16
- .replaceAll("\r", "\\r")
17
- : input;
18
- };
19
- return Array.isArray(input)
20
- ? input.map(secureString).join(",")
21
- : secureString(input);
22
- };
23
-
24
- export const decode = (
25
- input: string | null | number,
26
- fieldType?: FieldType
27
- ): string | number | boolean | null | (string | number | null | boolean)[] => {
28
- const unSecureString = (input: string) =>
29
- decodeURIComponent(input)
30
- .replaceAll("&lt;", "<")
31
- .replaceAll("&gt;", ">")
32
- .replaceAll("%2C", ",")
33
- .replaceAll("\\n", "\n")
34
- .replaceAll("\\r", "\r") || null;
35
-
36
- if (input === null || input === "") return null;
37
- if (!isNaN(Number(input)) && isFinite(Number(input)))
38
- return fieldType === "boolean" ? Boolean(Number(input)) : Number(input);
39
- return (input as string).includes(",")
40
- ? (input as string).split(",").map(unSecureString)
41
- : unSecureString(input as string);
42
- };
43
-
44
- export const isArrayOfObjects = (arr: any) => {
45
- return Array.isArray(arr) && (arr.length === 0 || arr.every(isObject));
46
- };
47
-
48
- export const isObject = (obj: any) =>
49
- obj != null &&
50
- (obj.constructor.name === "Object" ||
51
- (typeof obj === "object" && !Array.isArray(obj)));
52
-
53
- export const deepMerge = (target: any, source: any): any => {
54
- for (const key in source) {
55
- if (source.hasOwnProperty(key)) {
56
- if (source[key] instanceof Object && target[key] instanceof Object)
57
- target[key] = deepMerge(target[key], source[key]);
58
- else target[key] = source[key];
59
- }
60
- }
61
- return target;
62
- };
63
-
64
- export const combineObjects = (objectArray: Record<string, any>[]) => {
65
- const combinedValues: Record<string, any> = {};
66
-
67
- for (const obj of objectArray as any)
68
- for (const key in obj)
69
- if (!combinedValues.hasOwnProperty(key)) combinedValues[key] = obj[key];
70
-
71
- return combinedValues;
72
- };
73
-
74
- export const isNumber = (input: any): boolean =>
75
- Array.isArray(input)
76
- ? input.every(isNumber)
77
- : !isNaN(parseFloat(input)) && !isNaN(input - 0);
78
-
79
- export const hashPassword = (password: string) => {
80
- const salt = randomBytes(16).toString("hex");
81
- const buf = scryptSync(password, salt, 64);
82
- // return "161" length string
83
- return `${buf.toString("hex")}.${salt}`;
84
- };
85
-
86
- export const comparePassword = (
87
- storedPassword: string,
88
- suppliedPassword: string
89
- ) => {
90
- // split() returns array
91
- const [hashedPassword, salt] = storedPassword.split(".");
92
- // we need to pass buffer values to timingSafeEqual
93
- const hashedPasswordBuf = Buffer.from(hashedPassword, "hex");
94
- // we hash the new sign-in password
95
- const suppliedPasswordBuf = scryptSync(suppliedPassword, salt, 64);
96
- // compare the new supplied password with the stored hashed password
97
- return timingSafeEqual(hashedPasswordBuf, suppliedPasswordBuf);
98
- };
99
-
100
- export default class Utils {
101
- static encode = encode;
102
- static decode = decode;
103
- static isNumber = isNumber;
104
- static isObject = isObject;
105
- static deepMerge = deepMerge;
106
- static hashPassword = hashPassword;
107
- static combineObjects = combineObjects;
108
- static comparePassword = comparePassword;
109
- static isArrayOfObjects = isArrayOfObjects;
110
- }