inibase 1.0.0-rc.5 → 1.0.0-rc.50

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,7 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "module": "ES2022",
4
- "target": "ES2022",
5
- "moduleResolution": "node"
6
- }
7
- }
package/utils.ts DELETED
@@ -1,165 +0,0 @@
1
- import {
2
- scryptSync,
3
- randomBytes,
4
- timingSafeEqual,
5
- createDecipheriv,
6
- createCipheriv,
7
- } from "crypto";
8
- import { FieldType } from ".";
9
-
10
- export const isArrayOfObjects = (arr: any) => {
11
- return Array.isArray(arr) && (arr.length === 0 || arr.every(isObject));
12
- };
13
- export const isArrayOfArrays = (arr: any) => {
14
- return Array.isArray(arr) && (arr.length === 0 || arr.every(Array.isArray));
15
- };
16
-
17
- export const isObject = (obj: any) =>
18
- obj != null &&
19
- (obj.constructor.name === "Object" ||
20
- (typeof obj === "object" && !Array.isArray(obj)));
21
-
22
- export const deepMerge = (target: any, source: any): any => {
23
- for (const key in source) {
24
- if (source.hasOwnProperty(key)) {
25
- if (source[key] instanceof Object && target[key] instanceof Object)
26
- target[key] = deepMerge(target[key], source[key]);
27
- else target[key] = source[key];
28
- }
29
- }
30
- return target;
31
- };
32
-
33
- export const combineObjects = (objectArray: Record<string, any>[]) => {
34
- const combinedValues: Record<string, any> = {};
35
-
36
- for (const obj of objectArray as any)
37
- for (const key in obj)
38
- if (!combinedValues.hasOwnProperty(key)) combinedValues[key] = obj[key];
39
-
40
- return combinedValues;
41
- };
42
-
43
- export const isNumber = (input: any): boolean =>
44
- Array.isArray(input)
45
- ? input.every(isNumber)
46
- : !isNaN(parseFloat(input)) && !isNaN(input - 0);
47
-
48
- export const isEmail = (input: any) =>
49
- /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(String(input));
50
-
51
- export const isURL = (input: any) =>
52
- input[0] === "#" ||
53
- /^((https?|www):\/\/)?[a-z0-9-]+(\.[a-z0-9-]+)*\.[a-z]+(\/[^\s]*)?$/.test(
54
- input
55
- );
56
-
57
- export const isPassword = (input: any) => input.length === 161;
58
-
59
- export const isDate = (input: any) =>
60
- !isNaN(Date.parse(String(input))) && Date.parse(String(input)) >= 0;
61
-
62
- export const hashPassword = (password: string) => {
63
- const salt = randomBytes(16).toString("hex");
64
- const buf = scryptSync(password, salt, 64);
65
- // return "161" length string
66
- return `${buf.toString("hex")}.${salt}`;
67
- };
68
-
69
- export const comparePassword = (
70
- storedPassword: string,
71
- suppliedPassword: string
72
- ) => {
73
- // split() returns array
74
- const [hashedPassword, salt] = storedPassword.split(".");
75
- // we need to pass buffer values to timingSafeEqual
76
- const hashedPasswordBuf = Buffer.from(hashedPassword, "hex");
77
- // we hash the new sign-in password
78
- const suppliedPasswordBuf = scryptSync(suppliedPassword, salt, 64);
79
- // compare the new supplied password with the stored hashed password
80
- return timingSafeEqual(hashedPasswordBuf, suppliedPasswordBuf);
81
- };
82
-
83
- export const encodeID = (id: number, secretKey: string | number): string => {
84
- const salt = scryptSync(secretKey.toString(), "salt", 32),
85
- cipher = createCipheriv("aes-256-cbc", salt, salt.subarray(0, 16));
86
-
87
- return cipher.update(id.toString(), "utf8", "hex") + cipher.final("hex");
88
- };
89
-
90
- export const decodeID = (input: string, secretKey: string | number): number => {
91
- const salt = scryptSync(secretKey.toString(), "salt", 32),
92
- decipher = createDecipheriv("aes-256-cbc", salt, salt.subarray(0, 16));
93
- return Number(
94
- decipher.update(input as string, "hex", "utf8") + decipher.final("utf8")
95
- );
96
- };
97
-
98
- export const isValidID = (input: any): boolean => {
99
- return Array.isArray(input)
100
- ? input.every(isValidID)
101
- : typeof input === "string" && input.length === 32;
102
- };
103
-
104
- export const findChangedProperties = (
105
- obj1: Record<string, string>,
106
- obj2: Record<string, string>
107
- ): Record<string, string> | null => {
108
- const result: Record<string, string> = {};
109
-
110
- for (const key1 in obj1)
111
- if (obj2.hasOwnProperty(key1) && obj1[key1] !== obj2[key1])
112
- result[obj1[key1]] = obj2[key1];
113
-
114
- return Object.keys(result).length ? result : null;
115
- };
116
-
117
- export const detectFieldType = (
118
- input: any,
119
- availableTypes: FieldType[]
120
- ): FieldType | undefined => {
121
- if (
122
- (input === "0" || input === "1" || input === "true" || input === "false") &&
123
- availableTypes.includes("boolean")
124
- )
125
- return "boolean";
126
- else if (Utils.isNumber(input)) {
127
- if (availableTypes.includes("table")) return "table";
128
- else if (availableTypes.includes("number")) return "number";
129
- else if (availableTypes.includes("date")) return "date";
130
- } else if (
131
- (Array.isArray(input) || input.includes(",")) &&
132
- availableTypes.includes("array")
133
- )
134
- return "array";
135
- else if (Utils.isEmail(input) && availableTypes.includes("email"))
136
- return "email";
137
- else if (Utils.isURL(input) && availableTypes.includes("url")) return "url";
138
- else if (Utils.isPassword(input) && availableTypes.includes("password"))
139
- return "password";
140
- else if (Utils.isDate(input) && availableTypes.includes("date"))
141
- return "date";
142
- else if (!Utils.isNumber(input) && availableTypes.includes("string"))
143
- return "string";
144
- else return undefined;
145
- };
146
-
147
- export default class Utils {
148
- static encodeID = encodeID;
149
- static decodeID = decodeID;
150
- static isNumber = isNumber;
151
- static isObject = isObject;
152
- static isEmail = isEmail;
153
- static isDate = isDate;
154
- static isURL = isURL;
155
- static isValidID = isValidID;
156
- static isPassword = isPassword;
157
- static hashPassword = hashPassword;
158
- static deepMerge = deepMerge;
159
- static combineObjects = combineObjects;
160
- static comparePassword = comparePassword;
161
- static isArrayOfObjects = isArrayOfObjects;
162
- static findChangedProperties = findChangedProperties;
163
- static detectFieldType = detectFieldType;
164
- static isArrayOfArrays = isArrayOfArrays;
165
- }