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