@simtlix/simfinity-js 2.4.6 → 2.5.0
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/.claude/worktrees/agitated-kepler/.claude/settings.local.json +23 -0
- package/.claude/worktrees/agitated-kepler/AGGREGATION_CHANGES_SUMMARY.md +235 -0
- package/.claude/worktrees/agitated-kepler/AGGREGATION_EXAMPLE.md +567 -0
- package/.claude/worktrees/agitated-kepler/LICENSE +201 -0
- package/.claude/worktrees/agitated-kepler/README.md +3941 -0
- package/.claude/worktrees/agitated-kepler/eslint.config.mjs +71 -0
- package/.claude/worktrees/agitated-kepler/package-lock.json +4740 -0
- package/.claude/worktrees/agitated-kepler/package.json +41 -0
- package/.claude/worktrees/agitated-kepler/src/auth/errors.js +44 -0
- package/.claude/worktrees/agitated-kepler/src/auth/expressions.js +273 -0
- package/.claude/worktrees/agitated-kepler/src/auth/index.js +391 -0
- package/.claude/worktrees/agitated-kepler/src/auth/rules.js +274 -0
- package/.claude/worktrees/agitated-kepler/src/const/QLOperator.js +39 -0
- package/.claude/worktrees/agitated-kepler/src/const/QLSort.js +28 -0
- package/.claude/worktrees/agitated-kepler/src/const/QLValue.js +39 -0
- package/.claude/worktrees/agitated-kepler/src/errors/internal-server.error.js +11 -0
- package/.claude/worktrees/agitated-kepler/src/errors/simfinity.error.js +15 -0
- package/.claude/worktrees/agitated-kepler/src/index.js +2412 -0
- package/.claude/worktrees/agitated-kepler/src/plugins.js +53 -0
- package/.claude/worktrees/agitated-kepler/src/scalars.js +188 -0
- package/.claude/worktrees/agitated-kepler/src/validators.js +250 -0
- package/.claude/worktrees/agitated-kepler/yarn.lock +1154 -0
- package/.cursor/rules/simfinity-core-functions.mdc +3 -1
- package/README.md +202 -0
- package/package.json +1 -1
- package/src/index.js +235 -21
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@simtlix/simfinity-js",
|
|
3
|
+
"version": "2.4.6",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "vitest run",
|
|
9
|
+
"test:watch": "vitest",
|
|
10
|
+
"test:coverage": "vitest --coverage",
|
|
11
|
+
"lint": "eslint '**/*.js'",
|
|
12
|
+
"lint-fix": "eslint --fix '**/*.js'"
|
|
13
|
+
},
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=18.18.0"
|
|
16
|
+
},
|
|
17
|
+
"author": "Simtlix",
|
|
18
|
+
"license": "Apache-2.0",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/simtlix/simfinity.git"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"graphql": "^16.11.0",
|
|
25
|
+
"mongoose": "^8.16.2"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@eslint/js": "^9.30.1",
|
|
29
|
+
"eslint": "^9.30.1",
|
|
30
|
+
"globals": "^16.3.0",
|
|
31
|
+
"vitest": "^3.2.4"
|
|
32
|
+
},
|
|
33
|
+
"config": {
|
|
34
|
+
"owner": "simtlix"
|
|
35
|
+
},
|
|
36
|
+
"optionalDependencies": {
|
|
37
|
+
"graphql": "^16.11.0",
|
|
38
|
+
"graphql-middleware": "^6.1.35",
|
|
39
|
+
"mongoose": "^8.16.2"
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import SimfinityError from '../errors/simfinity.error.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Authentication error - thrown when user is not authenticated
|
|
5
|
+
* Uses code: UNAUTHENTICATED, status: 401
|
|
6
|
+
*/
|
|
7
|
+
export class UnauthenticatedError extends SimfinityError {
|
|
8
|
+
constructor(message = 'Authentication required') {
|
|
9
|
+
super(message, 'UNAUTHENTICATED', 401);
|
|
10
|
+
this.name = 'UnauthenticatedError';
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Authorization error - thrown when user lacks permission
|
|
16
|
+
* Uses code: FORBIDDEN, status: 403
|
|
17
|
+
*/
|
|
18
|
+
export class ForbiddenError extends SimfinityError {
|
|
19
|
+
constructor(message = 'Access denied') {
|
|
20
|
+
super(message, 'FORBIDDEN', 403);
|
|
21
|
+
this.name = 'ForbiddenError';
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Generic auth error factory for custom auth failures
|
|
27
|
+
* @param {string} message - Error message
|
|
28
|
+
* @param {string} code - Error code (UNAUTHENTICATED or FORBIDDEN)
|
|
29
|
+
* @returns {SimfinityError}
|
|
30
|
+
*/
|
|
31
|
+
export const createAuthError = (message, code = 'FORBIDDEN') => {
|
|
32
|
+
const status = code === 'UNAUTHENTICATED' ? 401 : 403;
|
|
33
|
+
return new SimfinityError(message, code, status);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// Export all errors as an object for convenience
|
|
37
|
+
const errors = {
|
|
38
|
+
UnauthenticatedError,
|
|
39
|
+
ForbiddenError,
|
|
40
|
+
createAuthError,
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export default errors;
|
|
44
|
+
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON AST Policy Expression Evaluator
|
|
3
|
+
*
|
|
4
|
+
* Safely evaluates declarative policy expressions without using eval() or Function().
|
|
5
|
+
* Supports logical operators (allOf, anyOf, not), comparison operators (eq, in),
|
|
6
|
+
* boolean literals, and references to parent/args/ctx.
|
|
7
|
+
*
|
|
8
|
+
* Unknown operators or invalid references fail closed (deny).
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Resolves a reference path like "parent.authorId" or "ctx.user.id"
|
|
13
|
+
* @param {string} refPath - The reference path (e.g., "ctx.user.id")
|
|
14
|
+
* @param {Object} context - The evaluation context { parent, args, ctx }
|
|
15
|
+
* @returns {*} The resolved value or undefined if not found
|
|
16
|
+
*/
|
|
17
|
+
const resolveRef = (refPath, context) => {
|
|
18
|
+
if (typeof refPath !== 'string') {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const parts = refPath.split('.');
|
|
23
|
+
const root = parts[0];
|
|
24
|
+
|
|
25
|
+
// Only allow parent, args, ctx as root references
|
|
26
|
+
if (!['parent', 'args', 'ctx'].includes(root)) {
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let value = context[root];
|
|
31
|
+
|
|
32
|
+
for (let i = 1; i < parts.length; i++) {
|
|
33
|
+
if (value === null || value === undefined) {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
value = value[parts[i]];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return value;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Resolves a value which may be a literal or a reference
|
|
44
|
+
* @param {*} value - The value to resolve (could be { ref: "..." } or a literal)
|
|
45
|
+
* @param {Object} context - The evaluation context { parent, args, ctx }
|
|
46
|
+
* @returns {*} The resolved value
|
|
47
|
+
*/
|
|
48
|
+
const resolveValue = (value, context) => {
|
|
49
|
+
// Check if it's a reference object
|
|
50
|
+
if (value !== null && typeof value === 'object' && 'ref' in value) {
|
|
51
|
+
return resolveRef(value.ref, context);
|
|
52
|
+
}
|
|
53
|
+
// Return literal value
|
|
54
|
+
return value;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Evaluates an 'eq' expression: { eq: [left, right] }
|
|
59
|
+
* @param {Array} operands - Array of two operands to compare
|
|
60
|
+
* @param {Object} context - The evaluation context
|
|
61
|
+
* @returns {boolean}
|
|
62
|
+
*/
|
|
63
|
+
const evaluateEq = (operands, context) => {
|
|
64
|
+
if (!Array.isArray(operands) || operands.length !== 2) {
|
|
65
|
+
return false; // Fail closed
|
|
66
|
+
}
|
|
67
|
+
const left = resolveValue(operands[0], context);
|
|
68
|
+
const right = resolveValue(operands[1], context);
|
|
69
|
+
return left === right;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Evaluates an 'in' expression: { in: [value, array] }
|
|
74
|
+
* @param {Array} operands - [value, array] where value should be in array
|
|
75
|
+
* @param {Object} context - The evaluation context
|
|
76
|
+
* @returns {boolean}
|
|
77
|
+
*/
|
|
78
|
+
const evaluateIn = (operands, context) => {
|
|
79
|
+
if (!Array.isArray(operands) || operands.length !== 2) {
|
|
80
|
+
return false; // Fail closed
|
|
81
|
+
}
|
|
82
|
+
const value = resolveValue(operands[0], context);
|
|
83
|
+
const array = resolveValue(operands[1], context);
|
|
84
|
+
|
|
85
|
+
if (!Array.isArray(array)) {
|
|
86
|
+
return false; // Fail closed
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return array.includes(value);
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Evaluates an 'allOf' expression: { allOf: [...expressions] }
|
|
94
|
+
* All expressions must evaluate to true (logical AND)
|
|
95
|
+
* @param {Array} expressions - Array of expressions to evaluate
|
|
96
|
+
* @param {Object} context - The evaluation context
|
|
97
|
+
* @returns {boolean}
|
|
98
|
+
*/
|
|
99
|
+
const evaluateAllOf = (expressions, context) => {
|
|
100
|
+
if (!Array.isArray(expressions)) {
|
|
101
|
+
return false; // Fail closed
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
for (const expr of expressions) {
|
|
105
|
+
if (!evaluateExpression(expr, context)) {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return true;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Evaluates an 'anyOf' expression: { anyOf: [...expressions] }
|
|
114
|
+
* At least one expression must evaluate to true (logical OR)
|
|
115
|
+
* @param {Array} expressions - Array of expressions to evaluate
|
|
116
|
+
* @param {Object} context - The evaluation context
|
|
117
|
+
* @returns {boolean}
|
|
118
|
+
*/
|
|
119
|
+
const evaluateAnyOf = (expressions, context) => {
|
|
120
|
+
if (!Array.isArray(expressions)) {
|
|
121
|
+
return false; // Fail closed
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
for (const expr of expressions) {
|
|
125
|
+
if (evaluateExpression(expr, context)) {
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return false;
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Evaluates a 'not' expression: { not: expression }
|
|
134
|
+
* Negates the result of the inner expression
|
|
135
|
+
* @param {*} expression - Expression to negate
|
|
136
|
+
* @param {Object} context - The evaluation context
|
|
137
|
+
* @returns {boolean}
|
|
138
|
+
*/
|
|
139
|
+
const evaluateNot = (expression, context) => {
|
|
140
|
+
return !evaluateExpression(expression, context);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Main expression evaluator
|
|
145
|
+
* @param {*} expression - The expression to evaluate
|
|
146
|
+
* @param {Object} context - The evaluation context { parent, args, ctx }
|
|
147
|
+
* @returns {boolean} The result of the expression evaluation
|
|
148
|
+
*/
|
|
149
|
+
export const evaluateExpression = (expression, context) => {
|
|
150
|
+
// Handle boolean literals
|
|
151
|
+
if (typeof expression === 'boolean') {
|
|
152
|
+
return expression;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Handle null/undefined - fail closed
|
|
156
|
+
if (expression === null || expression === undefined) {
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Expression must be an object with exactly one operator key
|
|
161
|
+
if (typeof expression !== 'object') {
|
|
162
|
+
return false; // Fail closed for non-object expressions
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const keys = Object.keys(expression);
|
|
166
|
+
|
|
167
|
+
// Empty object fails closed
|
|
168
|
+
if (keys.length === 0) {
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// Handle single operator expressions
|
|
173
|
+
if (keys.length === 1) {
|
|
174
|
+
const operator = keys[0];
|
|
175
|
+
const operand = expression[operator];
|
|
176
|
+
|
|
177
|
+
switch (operator) {
|
|
178
|
+
case 'eq':
|
|
179
|
+
return evaluateEq(operand, context);
|
|
180
|
+
case 'in':
|
|
181
|
+
return evaluateIn(operand, context);
|
|
182
|
+
case 'allOf':
|
|
183
|
+
return evaluateAllOf(operand, context);
|
|
184
|
+
case 'anyOf':
|
|
185
|
+
return evaluateAnyOf(operand, context);
|
|
186
|
+
case 'not':
|
|
187
|
+
return evaluateNot(operand, context);
|
|
188
|
+
default:
|
|
189
|
+
// Unknown operator - fail closed
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Multiple keys - treat as implicit allOf
|
|
195
|
+
// This allows { eq: [...], in: [...] } to mean both must pass
|
|
196
|
+
for (const operator of keys) {
|
|
197
|
+
const operand = expression[operator];
|
|
198
|
+
let result;
|
|
199
|
+
|
|
200
|
+
switch (operator) {
|
|
201
|
+
case 'eq':
|
|
202
|
+
result = evaluateEq(operand, context);
|
|
203
|
+
break;
|
|
204
|
+
case 'in':
|
|
205
|
+
result = evaluateIn(operand, context);
|
|
206
|
+
break;
|
|
207
|
+
case 'allOf':
|
|
208
|
+
result = evaluateAllOf(operand, context);
|
|
209
|
+
break;
|
|
210
|
+
case 'anyOf':
|
|
211
|
+
result = evaluateAnyOf(operand, context);
|
|
212
|
+
break;
|
|
213
|
+
case 'not':
|
|
214
|
+
result = evaluateNot(operand, context);
|
|
215
|
+
break;
|
|
216
|
+
default:
|
|
217
|
+
// Unknown operator - fail closed
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (!result) {
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return true;
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Checks if a value is a policy expression (object with operator keys)
|
|
231
|
+
* @param {*} value - The value to check
|
|
232
|
+
* @returns {boolean} True if the value appears to be a policy expression
|
|
233
|
+
*/
|
|
234
|
+
export const isPolicyExpression = (value) => {
|
|
235
|
+
if (value === null || typeof value !== 'object') {
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// Check if it's a function (not an expression)
|
|
240
|
+
if (typeof value === 'function') {
|
|
241
|
+
return false;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Check if it has any known operator keys
|
|
245
|
+
const operatorKeys = ['eq', 'in', 'allOf', 'anyOf', 'not'];
|
|
246
|
+
const keys = Object.keys(value);
|
|
247
|
+
|
|
248
|
+
return keys.some(key => operatorKeys.includes(key));
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Creates a rule function from a policy expression
|
|
253
|
+
* @param {Object} expression - The policy expression
|
|
254
|
+
* @returns {Function} A rule function (parent, args, ctx, info) => boolean
|
|
255
|
+
*/
|
|
256
|
+
export const createRuleFromExpression = (expression) => {
|
|
257
|
+
return (parent, args, ctx) => {
|
|
258
|
+
const context = { parent, args, ctx };
|
|
259
|
+
return evaluateExpression(expression, context);
|
|
260
|
+
};
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
// Export all expression utilities as an object for convenience
|
|
264
|
+
const expressions = {
|
|
265
|
+
evaluateExpression,
|
|
266
|
+
isPolicyExpression,
|
|
267
|
+
createRuleFromExpression,
|
|
268
|
+
resolveRef,
|
|
269
|
+
resolveValue,
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
export default expressions;
|
|
273
|
+
|