inibase 1.0.0-rc.6 → 1.0.0-rc.61

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,221 +0,0 @@
1
- import {
2
- scryptSync,
3
- randomBytes,
4
- timingSafeEqual,
5
- createDecipheriv,
6
- createCipheriv,
7
- Cipher,
8
- Decipher,
9
- } from "node:crypto";
10
- import { FieldType } from ".";
11
-
12
- export const isArrayOfObjects = (arr: any) => {
13
- return Array.isArray(arr) && (arr.length === 0 || arr.every(isObject));
14
- };
15
- export const isArrayOfArrays = (arr: any) => {
16
- return Array.isArray(arr) && (arr.length === 0 || arr.every(Array.isArray));
17
- };
18
-
19
- export const isObject = (obj: any) =>
20
- obj != null &&
21
- (obj.constructor.name === "Object" ||
22
- (typeof obj === "object" && !Array.isArray(obj)));
23
-
24
- export const deepMerge = (target: any, source: any): any => {
25
- for (const key in source) {
26
- if (source.hasOwnProperty(key)) {
27
- if (source[key] instanceof Object && target[key] instanceof Object)
28
- target[key] = deepMerge(target[key], source[key]);
29
- else target[key] = source[key];
30
- }
31
- }
32
- return target;
33
- };
34
-
35
- export const combineObjects = (objectArray: Record<string, any>[]) => {
36
- const combinedValues: Record<string, any> = {};
37
-
38
- for (const obj of objectArray as any)
39
- for (const key in obj)
40
- if (!combinedValues.hasOwnProperty(key)) combinedValues[key] = obj[key];
41
-
42
- return combinedValues;
43
- };
44
-
45
- export const isNumber = (input: any | any[]): boolean =>
46
- Array.isArray(input)
47
- ? input.every(isNumber)
48
- : !isNaN(parseFloat(input)) && !isNaN(input - 0);
49
-
50
- export const isEmail = (input: any) =>
51
- /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(String(input));
52
-
53
- export const isURL = (input: any) => input[0] === "#" || URL.canParse(input);
54
-
55
- export const isHTML = (input: any) =>
56
- /<\/?\s*[a-z-][^>]*\s*>|(\&(?:[\w\d]+|#\d+|#x[a-f\d]+);)/g.test(input);
57
-
58
- export const isString = (input: any) =>
59
- Object.prototype.toString.call(input) === "[object String]" &&
60
- !isNumber(input) &&
61
- !isBoolean(input) &&
62
- !isEmail(input) &&
63
- !isDate(input) &&
64
- !isURL(input) &&
65
- !isIP(input) &&
66
- !isHTML(input);
67
-
68
- export const isIP = (input: any) =>
69
- /^(?:(?:^|\.)(?:2(?:5[0-5]|[0-4]\d)|1?\d?\d)){4}$/.test(input);
70
-
71
- export const isBoolean = (input: any) =>
72
- typeof input === "boolean" ||
73
- input === "true" ||
74
- input === "false" ||
75
- input === true ||
76
- input === false;
77
-
78
- export const isPassword = (input: any) => input.length === 161;
79
-
80
- export const isDate = (input: any) =>
81
- !isNaN(Date.parse(String(input))) && Date.parse(String(input)) >= 0;
82
-
83
- export const hashPassword = (password: string) => {
84
- const salt = randomBytes(16).toString("hex");
85
- const buf = scryptSync(password, salt, 64);
86
- // return "161" length string
87
- return `${buf.toString("hex")}.${salt}`;
88
- };
89
-
90
- export const comparePassword = (
91
- storedPassword: string,
92
- suppliedPassword: string
93
- ) => {
94
- // split() returns array
95
- const [hashedPassword, salt] = storedPassword.split(".");
96
- // we need to pass buffer values to timingSafeEqual
97
- const hashedPasswordBuf = Buffer.from(hashedPassword, "hex");
98
- // we hash the new sign-in password
99
- const suppliedPasswordBuf = scryptSync(suppliedPassword, salt, 64);
100
- // compare the new supplied password with the stored hashed password
101
- return timingSafeEqual(hashedPasswordBuf, suppliedPasswordBuf);
102
- };
103
-
104
- export const encodeID = (
105
- id: number,
106
- secretKey: string | number | Buffer
107
- ): string => {
108
- let cipher: Cipher, ret: string;
109
-
110
- if (Buffer.isBuffer(secretKey))
111
- cipher = createCipheriv(
112
- "aes-256-cbc",
113
- secretKey,
114
- secretKey.subarray(0, 16)
115
- );
116
- else {
117
- const salt = scryptSync(secretKey.toString(), "salt", 32);
118
- cipher = createCipheriv("aes-256-cbc", salt, salt.subarray(0, 16));
119
- }
120
-
121
- return cipher.update(id.toString(), "utf8", "hex") + cipher.final("hex");
122
- };
123
-
124
- export const decodeID = (
125
- input: string,
126
- secretKey: string | number | Buffer
127
- ): number => {
128
- let decipher: Decipher;
129
-
130
- if (Buffer.isBuffer(secretKey))
131
- decipher = createDecipheriv(
132
- "aes-256-cbc",
133
- secretKey,
134
- secretKey.subarray(0, 16)
135
- );
136
- else {
137
- const salt = scryptSync(secretKey.toString(), "salt", 32);
138
- decipher = createDecipheriv("aes-256-cbc", salt, salt.subarray(0, 16));
139
- }
140
-
141
- return Number(
142
- decipher.update(input as string, "hex", "utf8") + decipher.final("utf8")
143
- );
144
- };
145
-
146
- export const isValidID = (input: any): boolean => {
147
- return Array.isArray(input)
148
- ? input.every(isValidID)
149
- : typeof input === "string" && input.length === 32;
150
- };
151
-
152
- export const findChangedProperties = (
153
- obj1: Record<string, string>,
154
- obj2: Record<string, string>
155
- ): Record<string, string> | null => {
156
- const result: Record<string, string> = {};
157
-
158
- for (const key1 in obj1)
159
- if (obj2.hasOwnProperty(key1) && obj1[key1] !== obj2[key1])
160
- result[obj1[key1]] = obj2[key1];
161
-
162
- return Object.keys(result).length ? result : null;
163
- };
164
-
165
- export const detectFieldType = (
166
- input: any,
167
- availableTypes: FieldType[]
168
- ): FieldType | undefined => {
169
- if (!Array.isArray(input)) {
170
- if (
171
- (input === "0" ||
172
- input === "1" ||
173
- input === "true" ||
174
- input === "false") &&
175
- availableTypes.includes("boolean")
176
- )
177
- return "boolean";
178
- else if (Utils.isNumber(input)) {
179
- if (availableTypes.includes("table")) return "table";
180
- else if (availableTypes.includes("date")) return "date";
181
- else if (availableTypes.includes("number")) return "number";
182
- } else if (input.includes(",") && availableTypes.includes("array"))
183
- return "array";
184
- else if (availableTypes.includes("email") && Utils.isEmail(input))
185
- return "email";
186
- else if (availableTypes.includes("url") && Utils.isURL(input)) return "url";
187
- else if (availableTypes.includes("password") && Utils.isPassword(input))
188
- return "password";
189
- else if (availableTypes.includes("date") && Utils.isDate(input))
190
- return "date";
191
- else if (availableTypes.includes("string") && Utils.isString(input))
192
- return "string";
193
- else if (availableTypes.includes("ip") && Utils.isIP(input)) return "ip";
194
- } else return "array";
195
-
196
- return undefined;
197
- };
198
-
199
- export default class Utils {
200
- static encodeID = encodeID;
201
- static decodeID = decodeID;
202
- static isNumber = isNumber;
203
- static isObject = isObject;
204
- static isEmail = isEmail;
205
- static isDate = isDate;
206
- static isURL = isURL;
207
- static isValidID = isValidID;
208
- static isPassword = isPassword;
209
- static hashPassword = hashPassword;
210
- static deepMerge = deepMerge;
211
- static combineObjects = combineObjects;
212
- static comparePassword = comparePassword;
213
- static isArrayOfObjects = isArrayOfObjects;
214
- static findChangedProperties = findChangedProperties;
215
- static detectFieldType = detectFieldType;
216
- static isArrayOfArrays = isArrayOfArrays;
217
- static isBoolean = isBoolean;
218
- static isString = isString;
219
- static isHTML = isHTML;
220
- static isIP = isIP;
221
- }