inibase 1.1.21 → 1.1.23
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/dist/cli.js +7 -2
- package/dist/file.d.ts +17 -14
- package/dist/file.js +57 -51
- package/dist/index.d.ts +8 -3
- package/dist/index.js +106 -100
- package/dist/utils.d.ts +3 -4
- package/dist/utils.js +35 -34
- package/dist/utils.server.d.ts +10 -14
- package/dist/utils.server.js +25 -38
- package/package.json +1 -1
package/dist/utils.js
CHANGED
|
@@ -284,30 +284,28 @@ export const detectFieldType = (input, availableTypes) => {
|
|
|
284
284
|
return "array";
|
|
285
285
|
return undefined;
|
|
286
286
|
};
|
|
287
|
-
export const isFieldType = (
|
|
288
|
-
if (
|
|
289
|
-
if (Array.isArray(
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
: compareAtType === type))
|
|
293
|
-
return true;
|
|
294
|
-
}
|
|
295
|
-
else if ((Array.isArray(compareAtType) && compareAtType.includes(fieldType)) ||
|
|
296
|
-
compareAtType === fieldType)
|
|
287
|
+
export const isFieldType = (field, compareAtType) => {
|
|
288
|
+
if (Array.isArray(field.type)) {
|
|
289
|
+
if (field.type.some((type) => Array.isArray(compareAtType)
|
|
290
|
+
? compareAtType.includes(type)
|
|
291
|
+
: compareAtType === type))
|
|
297
292
|
return true;
|
|
298
293
|
}
|
|
299
|
-
if (
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
294
|
+
else if ((Array.isArray(compareAtType) && compareAtType.includes(field.type)) ||
|
|
295
|
+
compareAtType === field.type)
|
|
296
|
+
return true;
|
|
297
|
+
if (field.children) {
|
|
298
|
+
if (Array.isArray(field.children)) {
|
|
299
|
+
if (!isArrayOfObjects(field.children)) {
|
|
300
|
+
if (field.children.some((type) => Array.isArray(compareAtType)
|
|
303
301
|
? compareAtType.includes(type)
|
|
304
302
|
: compareAtType === type))
|
|
305
303
|
return true;
|
|
306
304
|
}
|
|
307
305
|
}
|
|
308
306
|
else if ((Array.isArray(compareAtType) &&
|
|
309
|
-
compareAtType.includes(
|
|
310
|
-
compareAtType ===
|
|
307
|
+
compareAtType.includes(field.children)) ||
|
|
308
|
+
compareAtType === field.children)
|
|
311
309
|
return true;
|
|
312
310
|
}
|
|
313
311
|
return false;
|
|
@@ -341,32 +339,35 @@ export const filterSchema = (schema, callback) => schema.filter((field) => {
|
|
|
341
339
|
* Validates if the given value matches the specified field type(s).
|
|
342
340
|
*
|
|
343
341
|
* @param value - The value to be validated.
|
|
344
|
-
* @param
|
|
345
|
-
* @param fieldChildrenType - Optional; the expected type(s) of children elements, used if the field type is an array.
|
|
342
|
+
* @param field - Field object config.
|
|
346
343
|
* @returns A boolean indicating whether the value matches the specified field type(s).
|
|
347
344
|
*/
|
|
348
|
-
export const validateFieldType = (value,
|
|
345
|
+
export const validateFieldType = (value, field) => {
|
|
349
346
|
if (value === null)
|
|
350
347
|
return true;
|
|
351
|
-
if (Array.isArray(
|
|
352
|
-
const detectedFieldType = detectFieldType(value,
|
|
348
|
+
if (Array.isArray(field.type)) {
|
|
349
|
+
const detectedFieldType = detectFieldType(value, field.type);
|
|
353
350
|
if (!detectedFieldType)
|
|
354
351
|
return false;
|
|
355
|
-
|
|
352
|
+
field.type = detectedFieldType;
|
|
356
353
|
}
|
|
357
|
-
if (
|
|
354
|
+
if (field.type === "array" && field.children)
|
|
358
355
|
return (Array.isArray(value) &&
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
356
|
+
(isArrayOfObjects(field.children) ||
|
|
357
|
+
value.every((v) => {
|
|
358
|
+
let _fieldChildrenType = field.children;
|
|
359
|
+
if (Array.isArray(_fieldChildrenType)) {
|
|
360
|
+
const detectedFieldType = detectFieldType(v, _fieldChildrenType);
|
|
361
|
+
if (!detectedFieldType)
|
|
362
|
+
return false;
|
|
363
|
+
_fieldChildrenType = detectedFieldType;
|
|
364
|
+
}
|
|
365
|
+
return validateFieldType(v, {
|
|
366
|
+
key: "BLABLA",
|
|
367
|
+
type: _fieldChildrenType,
|
|
368
|
+
});
|
|
369
|
+
})));
|
|
370
|
+
switch (field.type) {
|
|
370
371
|
case "string":
|
|
371
372
|
return isString(value);
|
|
372
373
|
case "password":
|
package/dist/utils.server.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { execFile as execFileSync, exec as execSync } from "node:child_process";
|
|
2
2
|
import { gunzip as gunzipSync, gzip as gzipSync } from "node:zlib";
|
|
3
|
-
import type { ComparisonOperator, Field, FieldType, Schema } from "./index.js";
|
|
4
3
|
import RE2 from "re2";
|
|
4
|
+
import type { ComparisonOperator, Field, FieldType, Schema } from "./index.js";
|
|
5
5
|
export declare const exec: typeof execSync.__promisify__;
|
|
6
6
|
export declare const execFile: typeof execFileSync.__promisify__;
|
|
7
7
|
export declare const gzip: typeof gzipSync.__promisify__;
|
|
@@ -21,30 +21,27 @@ export declare const hashPassword: (password: string) => string;
|
|
|
21
21
|
* @returns A boolean indicating whether the input password matches the hashed password.
|
|
22
22
|
*/
|
|
23
23
|
export declare const comparePassword: (hash: string, password: string) => boolean;
|
|
24
|
-
export declare const encodeID: (id: number | string
|
|
25
|
-
export declare const decodeID: (input: string
|
|
26
|
-
export declare const extractIdsFromSchema: (schema: Schema
|
|
24
|
+
export declare const encodeID: (id: number | string) => string;
|
|
25
|
+
export declare const decodeID: (input: string) => number;
|
|
26
|
+
export declare const extractIdsFromSchema: (schema: Schema) => number[];
|
|
27
27
|
/**
|
|
28
28
|
* Finds the last ID number in a schema, potentially decoding it if encrypted.
|
|
29
29
|
*
|
|
30
30
|
* @param schema - The schema to search, defined as an array of schema objects.
|
|
31
|
-
* @param secretKeyOrSalt - The secret key or salt for decoding an encrypted ID, can be a string, number, or Buffer.
|
|
32
31
|
* @returns The last ID number in the schema, decoded if necessary.
|
|
33
32
|
*/
|
|
34
|
-
export declare const findLastIdNumber: (schema: Schema
|
|
33
|
+
export declare const findLastIdNumber: (schema: Schema) => number;
|
|
35
34
|
/**
|
|
36
35
|
* Adds or updates IDs in a schema, encoding them using a provided secret key or salt.
|
|
37
36
|
*
|
|
38
37
|
* @param schema - The schema to update, defined as an array of schema objects.
|
|
39
38
|
* @param startWithID - An object containing the starting ID for generating new IDs.
|
|
40
|
-
* @param secretKeyOrSalt - The secret key or salt for encoding IDs, can be a string, number, or Buffer.
|
|
41
|
-
* @param encodeIDs - If true, IDs will be encoded, else they will remain as numbers.
|
|
42
39
|
* @returns The updated schema with encoded IDs.
|
|
43
40
|
*/
|
|
44
41
|
export declare const addIdToSchema: (schema: Schema, startWithID: {
|
|
45
42
|
value: number;
|
|
46
|
-
}
|
|
47
|
-
export declare const encodeSchemaID: (schema: Schema
|
|
43
|
+
}) => Field[];
|
|
44
|
+
export declare const encodeSchemaID: (schema: Schema) => Schema;
|
|
48
45
|
export declare const hashString: (str: string) => string;
|
|
49
46
|
/**
|
|
50
47
|
* Evaluates a comparison between two values based on a specified operator and field types.
|
|
@@ -52,8 +49,7 @@ export declare const hashString: (str: string) => string;
|
|
|
52
49
|
* @param operator - The comparison operator (e.g., '=', '!=', '>', '<', '>=', '<=', '[]', '![]', '*', '!*').
|
|
53
50
|
* @param originalValue - The value to compare, can be a single value or an array of values.
|
|
54
51
|
* @param comparedValue - The value or values to compare against.
|
|
55
|
-
* @param fieldType - Optional type of the field
|
|
56
|
-
* @param fieldChildrenType - Optional type for child elements in array inputs.
|
|
52
|
+
* @param fieldType - Optional type of the field.
|
|
57
53
|
* @returns boolean - Result of the comparison operation.
|
|
58
54
|
*
|
|
59
55
|
* Note: Handles various data types and comparison logic, including special handling for passwords and regex patterns.
|
|
@@ -64,7 +60,7 @@ export declare const compare: (operator: ComparisonOperator, originalValue: stri
|
|
|
64
60
|
*
|
|
65
61
|
* @param originalValue - The original value.
|
|
66
62
|
* @param comparedValue - The value to compare against.
|
|
67
|
-
* @param
|
|
63
|
+
* @param field - Field object config.
|
|
68
64
|
* @returns boolean - Result of the equality check.
|
|
69
65
|
*/
|
|
70
66
|
export declare const isEqual: (originalValue: string | number | boolean | null | (string | number | boolean | null)[], comparedValue: string | number | boolean | null | (string | number | boolean | null)[], fieldType?: FieldType) => boolean;
|
package/dist/utils.server.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { execFile as execFileSync, exec as execSync } from "node:child_process";
|
|
2
2
|
import { createCipheriv, createDecipheriv, createHash, randomBytes, scryptSync, } from "node:crypto";
|
|
3
|
-
import { gunzip as gunzipSync, gzip as gzipSync } from "node:zlib";
|
|
4
3
|
import { promisify } from "node:util";
|
|
5
|
-
import {
|
|
4
|
+
import { gunzip as gunzipSync, gzip as gzipSync } from "node:zlib";
|
|
6
5
|
import RE2 from "re2";
|
|
6
|
+
import { globalConfig } from "./index.js";
|
|
7
|
+
import { detectFieldType, isArrayOfObjects, isNumber, isPassword, isValidID, } from "./utils.js";
|
|
7
8
|
export const exec = promisify(execSync);
|
|
8
9
|
export const execFile = promisify(execFileSync);
|
|
9
10
|
export const gzip = promisify(gzipSync);
|
|
@@ -38,11 +39,11 @@ export const comparePassword = (hash, password) => {
|
|
|
38
39
|
// Cache for derived keys if using scrypt
|
|
39
40
|
const derivedKeyCache = new Map();
|
|
40
41
|
// Helper function to create cipher or decipher
|
|
41
|
-
const getKeyAndIv = (
|
|
42
|
-
if (Buffer.isBuffer(
|
|
43
|
-
return { key:
|
|
42
|
+
const getKeyAndIv = () => {
|
|
43
|
+
if (Buffer.isBuffer(globalConfig.salt)) {
|
|
44
|
+
return { key: globalConfig.salt, iv: globalConfig.salt.subarray(0, 16) };
|
|
44
45
|
}
|
|
45
|
-
const cacheKey =
|
|
46
|
+
const cacheKey = globalConfig.salt.toString();
|
|
46
47
|
let key = derivedKeyCache.get(cacheKey);
|
|
47
48
|
if (!key) {
|
|
48
49
|
key = scryptSync(cacheKey, `${INIBASE_SECRET}`, 32);
|
|
@@ -53,27 +54,25 @@ const getKeyAndIv = (secretKeyOrSalt) => {
|
|
|
53
54
|
// Ensure the environment variable is read once
|
|
54
55
|
const INIBASE_SECRET = process.env.INIBASE_SECRET ?? "inibase";
|
|
55
56
|
// Optimized encodeID
|
|
56
|
-
export const encodeID = (id
|
|
57
|
-
const { key, iv } = getKeyAndIv(
|
|
57
|
+
export const encodeID = (id) => {
|
|
58
|
+
const { key, iv } = getKeyAndIv();
|
|
58
59
|
const cipher = createCipheriv("aes-256-cbc", key, iv);
|
|
59
60
|
return cipher.update(id.toString(), "utf8", "hex") + cipher.final("hex");
|
|
60
61
|
};
|
|
61
62
|
// Optimized decodeID
|
|
62
|
-
export const decodeID = (input
|
|
63
|
-
const { key, iv } = getKeyAndIv(
|
|
63
|
+
export const decodeID = (input) => {
|
|
64
|
+
const { key, iv } = getKeyAndIv();
|
|
64
65
|
const decipher = createDecipheriv("aes-256-cbc", key, iv);
|
|
65
66
|
return Number(decipher.update(input, "hex", "utf8") + decipher.final("utf8"));
|
|
66
67
|
};
|
|
67
68
|
// Function to recursively flatten an array of objects and their nested children
|
|
68
|
-
export const extractIdsFromSchema = (schema
|
|
69
|
+
export const extractIdsFromSchema = (schema) => {
|
|
69
70
|
const result = [];
|
|
70
71
|
for (const field of schema) {
|
|
71
72
|
if (field.id)
|
|
72
|
-
result.push(typeof field.id === "number"
|
|
73
|
-
? field.id
|
|
74
|
-
: decodeID(field.id, secretKeyOrSalt));
|
|
73
|
+
result.push(typeof field.id === "number" ? field.id : decodeID(field.id));
|
|
75
74
|
if (field.children && isArrayOfObjects(field.children))
|
|
76
|
-
result.push(...extractIdsFromSchema(field.children
|
|
75
|
+
result.push(...extractIdsFromSchema(field.children));
|
|
77
76
|
}
|
|
78
77
|
return result;
|
|
79
78
|
};
|
|
@@ -81,35 +80,24 @@ export const extractIdsFromSchema = (schema, secretKeyOrSalt) => {
|
|
|
81
80
|
* Finds the last ID number in a schema, potentially decoding it if encrypted.
|
|
82
81
|
*
|
|
83
82
|
* @param schema - The schema to search, defined as an array of schema objects.
|
|
84
|
-
* @param secretKeyOrSalt - The secret key or salt for decoding an encrypted ID, can be a string, number, or Buffer.
|
|
85
83
|
* @returns The last ID number in the schema, decoded if necessary.
|
|
86
84
|
*/
|
|
87
|
-
export const findLastIdNumber = (schema
|
|
85
|
+
export const findLastIdNumber = (schema) => Math.max(...extractIdsFromSchema(schema));
|
|
88
86
|
/**
|
|
89
87
|
* Adds or updates IDs in a schema, encoding them using a provided secret key or salt.
|
|
90
88
|
*
|
|
91
89
|
* @param schema - The schema to update, defined as an array of schema objects.
|
|
92
90
|
* @param startWithID - An object containing the starting ID for generating new IDs.
|
|
93
|
-
* @param secretKeyOrSalt - The secret key or salt for encoding IDs, can be a string, number, or Buffer.
|
|
94
|
-
* @param encodeIDs - If true, IDs will be encoded, else they will remain as numbers.
|
|
95
91
|
* @returns The updated schema with encoded IDs.
|
|
96
92
|
*/
|
|
97
|
-
export const addIdToSchema = (schema, startWithID
|
|
93
|
+
export const addIdToSchema = (schema, startWithID) => {
|
|
98
94
|
function _addIdToField(field) {
|
|
99
95
|
if (!field.id) {
|
|
100
96
|
startWithID.value++;
|
|
101
|
-
field.id =
|
|
102
|
-
? encodeID(startWithID.value, secretKeyOrSalt)
|
|
103
|
-
: startWithID.value;
|
|
104
|
-
}
|
|
105
|
-
else {
|
|
106
|
-
if (isValidID(field.id)) {
|
|
107
|
-
if (!encodeIDs)
|
|
108
|
-
field.id = decodeID(field.id, secretKeyOrSalt);
|
|
109
|
-
}
|
|
110
|
-
else if (encodeIDs)
|
|
111
|
-
field.id = encodeID(field.id, secretKeyOrSalt);
|
|
97
|
+
field.id = encodeID(startWithID.value);
|
|
112
98
|
}
|
|
99
|
+
else
|
|
100
|
+
field.id = isValidID(field.id) ? decodeID(field.id) : encodeID(field.id);
|
|
113
101
|
if ((field.type === "array" || field.type === "object") &&
|
|
114
102
|
isArrayOfObjects(field.children))
|
|
115
103
|
field.children = _addIdToSchema(field.children);
|
|
@@ -118,13 +106,13 @@ export const addIdToSchema = (schema, startWithID, secretKeyOrSalt, encodeIDs) =
|
|
|
118
106
|
const _addIdToSchema = (schema) => schema.map(_addIdToField);
|
|
119
107
|
return _addIdToSchema(schema);
|
|
120
108
|
};
|
|
121
|
-
export const encodeSchemaID = (schema
|
|
109
|
+
export const encodeSchemaID = (schema) => schema.map((field) => ({
|
|
122
110
|
...field,
|
|
123
|
-
id: isNumber(field.id) ? encodeID(field.id
|
|
111
|
+
id: isNumber(field.id) ? encodeID(field.id) : field.id,
|
|
124
112
|
...(field.children
|
|
125
113
|
? isArrayOfObjects(field.children)
|
|
126
114
|
? {
|
|
127
|
-
children: encodeSchemaID(field.children
|
|
115
|
+
children: encodeSchemaID(field.children),
|
|
128
116
|
}
|
|
129
117
|
: { children: field.children }
|
|
130
118
|
: {}),
|
|
@@ -136,8 +124,7 @@ export const hashString = (str) => createHash("sha256").update(str).digest("hex"
|
|
|
136
124
|
* @param operator - The comparison operator (e.g., '=', '!=', '>', '<', '>=', '<=', '[]', '![]', '*', '!*').
|
|
137
125
|
* @param originalValue - The value to compare, can be a single value or an array of values.
|
|
138
126
|
* @param comparedValue - The value or values to compare against.
|
|
139
|
-
* @param fieldType - Optional type of the field
|
|
140
|
-
* @param fieldChildrenType - Optional type for child elements in array inputs.
|
|
127
|
+
* @param fieldType - Optional type of the field.
|
|
141
128
|
* @returns boolean - Result of the comparison operation.
|
|
142
129
|
*
|
|
143
130
|
* Note: Handles various data types and comparison logic, including special handling for passwords and regex patterns.
|
|
@@ -199,7 +186,7 @@ const compareNonNullValues = (originalValue, comparedValue, comparator) => {
|
|
|
199
186
|
*
|
|
200
187
|
* @param originalValue - The original value.
|
|
201
188
|
* @param comparedValue - The value to compare against.
|
|
202
|
-
* @param
|
|
189
|
+
* @param field - Field object config.
|
|
203
190
|
* @returns boolean - Result of the equality check.
|
|
204
191
|
*/
|
|
205
192
|
export const isEqual = (originalValue, comparedValue, fieldType) => {
|