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/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 = (compareAtType, fieldType, fieldChildrenType) => {
288
- if (fieldType) {
289
- if (Array.isArray(fieldType)) {
290
- if (fieldType.some((type) => Array.isArray(compareAtType)
291
- ? compareAtType.includes(type)
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 (fieldChildrenType) {
300
- if (Array.isArray(fieldChildrenType)) {
301
- if (!isArrayOfObjects(fieldChildrenType)) {
302
- if (fieldChildrenType.some((type) => Array.isArray(compareAtType)
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(fieldChildrenType)) ||
310
- compareAtType === fieldChildrenType)
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 fieldType - The expected field type or an array of possible field types.
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, fieldType, fieldChildrenType) => {
345
+ export const validateFieldType = (value, field) => {
349
346
  if (value === null)
350
347
  return true;
351
- if (Array.isArray(fieldType)) {
352
- const detectedFieldType = detectFieldType(value, fieldType);
348
+ if (Array.isArray(field.type)) {
349
+ const detectedFieldType = detectFieldType(value, field.type);
353
350
  if (!detectedFieldType)
354
351
  return false;
355
- fieldType = detectedFieldType;
352
+ field.type = detectedFieldType;
356
353
  }
357
- if (fieldType === "array" && fieldChildrenType)
354
+ if (field.type === "array" && field.children)
358
355
  return (Array.isArray(value) &&
359
- value.every((v) => {
360
- let _fieldChildrenType = fieldChildrenType;
361
- if (Array.isArray(_fieldChildrenType)) {
362
- const detectedFieldType = detectFieldType(v, _fieldChildrenType);
363
- if (!detectedFieldType)
364
- return false;
365
- _fieldChildrenType = detectedFieldType;
366
- }
367
- return validateFieldType(v, _fieldChildrenType);
368
- }));
369
- switch (fieldType) {
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":
@@ -1,7 +1,7 @@
1
- import { exec as execSync, execFile as execFileSync } from "node:child_process";
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, secretKeyOrSalt: string | number | Buffer) => string;
25
- export declare const decodeID: (input: string, secretKeyOrSalt: string | number | Buffer) => number;
26
- export declare const extractIdsFromSchema: (schema: Schema, secretKeyOrSalt: string | number | Buffer) => number[];
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, secretKeyOrSalt: string | number | Buffer) => number;
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
- }, secretKeyOrSalt: string | number | Buffer, encodeIDs?: boolean) => Field[];
47
- export declare const encodeSchemaID: (schema: Schema, secretKeyOrSalt: string | number | Buffer) => 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 to guide comparison (e.g., 'password', 'boolean').
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 fieldType - Type of the field.
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;
@@ -1,9 +1,10 @@
1
- import { exec as execSync, execFile as execFileSync } from "node:child_process";
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 { detectFieldType, isArrayOfObjects, isNumber, isPassword, isValidID, } from "./utils.js";
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 = (secretKeyOrSalt) => {
42
- if (Buffer.isBuffer(secretKeyOrSalt)) {
43
- return { key: secretKeyOrSalt, iv: secretKeyOrSalt.subarray(0, 16) };
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 = secretKeyOrSalt.toString();
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, secretKeyOrSalt) => {
57
- const { key, iv } = getKeyAndIv(secretKeyOrSalt);
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, secretKeyOrSalt) => {
63
- const { key, iv } = getKeyAndIv(secretKeyOrSalt);
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, secretKeyOrSalt) => {
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, secretKeyOrSalt));
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, secretKeyOrSalt) => Math.max(...extractIdsFromSchema(schema, secretKeyOrSalt));
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, secretKeyOrSalt, encodeIDs) => {
93
+ export const addIdToSchema = (schema, startWithID) => {
98
94
  function _addIdToField(field) {
99
95
  if (!field.id) {
100
96
  startWithID.value++;
101
- field.id = encodeIDs
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, secretKeyOrSalt) => schema.map((field) => ({
109
+ export const encodeSchemaID = (schema) => schema.map((field) => ({
122
110
  ...field,
123
- id: isNumber(field.id) ? encodeID(field.id, secretKeyOrSalt) : 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, secretKeyOrSalt),
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 to guide comparison (e.g., 'password', 'boolean').
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 fieldType - Type of the field.
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) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inibase",
3
- "version": "1.1.21",
3
+ "version": "1.1.23",
4
4
  "type": "module",
5
5
  "author": {
6
6
  "name": "Karim Amahtil",