inibase 1.0.0-rc.5 → 1.0.0-rc.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inibase",
3
- "version": "1.0.0-rc.5",
3
+ "version": "1.0.0-rc.6",
4
4
  "description": "File-based Relational Database for large data",
5
5
  "main": "index.ts",
6
6
  "type": "module",
@@ -30,7 +30,7 @@
30
30
  },
31
31
  "homepage": "https://github.com/inicontent/inibase#readme",
32
32
  "devDependencies": {
33
- "@types/node": "^20.6.0"
33
+ "@types/node": "^20.8.6"
34
34
  },
35
35
  "scripts": {
36
36
  "test": "npx tsx watch ./index.test.ts"
package/utils.ts CHANGED
@@ -4,7 +4,9 @@ import {
4
4
  timingSafeEqual,
5
5
  createDecipheriv,
6
6
  createCipheriv,
7
- } from "crypto";
7
+ Cipher,
8
+ Decipher,
9
+ } from "node:crypto";
8
10
  import { FieldType } from ".";
9
11
 
10
12
  export const isArrayOfObjects = (arr: any) => {
@@ -40,7 +42,7 @@ export const combineObjects = (objectArray: Record<string, any>[]) => {
40
42
  return combinedValues;
41
43
  };
42
44
 
43
- export const isNumber = (input: any): boolean =>
45
+ export const isNumber = (input: any | any[]): boolean =>
44
46
  Array.isArray(input)
45
47
  ? input.every(isNumber)
46
48
  : !isNaN(parseFloat(input)) && !isNaN(input - 0);
@@ -48,11 +50,30 @@ export const isNumber = (input: any): boolean =>
48
50
  export const isEmail = (input: any) =>
49
51
  /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(String(input));
50
52
 
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
- );
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;
56
77
 
57
78
  export const isPassword = (input: any) => input.length === 161;
58
79
 
@@ -80,16 +101,43 @@ export const comparePassword = (
80
101
  return timingSafeEqual(hashedPasswordBuf, suppliedPasswordBuf);
81
102
  };
82
103
 
83
- export const encodeID = (id: number, secretKey: string | number): string => {
84
- const salt = scryptSync(secretKey.toString(), "salt", 32),
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);
85
118
  cipher = createCipheriv("aes-256-cbc", salt, salt.subarray(0, 16));
119
+ }
86
120
 
87
121
  return cipher.update(id.toString(), "utf8", "hex") + cipher.final("hex");
88
122
  };
89
123
 
90
- export const decodeID = (input: string, secretKey: string | number): number => {
91
- const salt = scryptSync(secretKey.toString(), "salt", 32),
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);
92
138
  decipher = createDecipheriv("aes-256-cbc", salt, salt.subarray(0, 16));
139
+ }
140
+
93
141
  return Number(
94
142
  decipher.update(input as string, "hex", "utf8") + decipher.final("utf8")
95
143
  );
@@ -118,30 +166,34 @@ export const detectFieldType = (
118
166
  input: any,
119
167
  availableTypes: FieldType[]
120
168
  ): 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;
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;
145
197
  };
146
198
 
147
199
  export default class Utils {
@@ -162,4 +214,8 @@ export default class Utils {
162
214
  static findChangedProperties = findChangedProperties;
163
215
  static detectFieldType = detectFieldType;
164
216
  static isArrayOfArrays = isArrayOfArrays;
217
+ static isBoolean = isBoolean;
218
+ static isString = isString;
219
+ static isHTML = isHTML;
220
+ static isIP = isIP;
165
221
  }