@via-profit/ability 3.0.1 → 3.1.1

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.
@@ -0,0 +1,24 @@
1
+ import { AbilityDSLToken } from '../../parsers/dsl/AbilityDSLToken';
2
+ export declare class AbilityDSLLexer {
3
+ private readonly input;
4
+ private pos;
5
+ private tokens;
6
+ private line;
7
+ private column;
8
+ private readonly keywords;
9
+ constructor(input: string);
10
+ tokenize(): AbilityDSLToken[];
11
+ private readComment;
12
+ private readString;
13
+ private readNumber;
14
+ private readSymbol;
15
+ private readWord;
16
+ private skipWhitespace;
17
+ private isDigit;
18
+ private isAlpha;
19
+ private isSymbol;
20
+ private isNewline;
21
+ private peek;
22
+ private advance;
23
+ private isAtEnd;
24
+ }
@@ -0,0 +1,86 @@
1
+ import AbilityPolicy from '../../core/AbilityPolicy';
2
+ import { ResourceObject } from '../../core/AbilityParser';
3
+ /**
4
+ * Parser for the Ability DSL.
5
+ *
6
+ * Converts a DSL string into one or more AbilityPolicy instances.
7
+ * The grammar follows the structure:
8
+ *
9
+ * <effect> <permission> if <group> [ <group> ... ]
10
+ *
11
+ * where <group> is either "all of:" or "any of:", followed by a colon,
12
+ * and then a list of rules (one per line).
13
+ *
14
+ * Each rule is: <identifier> <operator> <value>
15
+ *
16
+ * Operators can be simple (equals, contains, in) or
17
+ * composed (is null, is not null, greater than, less than or equal, etc.).
18
+ */
19
+ export declare class AbilityDSLParser<Resource extends ResourceObject = Record<string, unknown>, Environment = unknown> {
20
+ private dsl;
21
+ private tokens;
22
+ private pos;
23
+ private annotationBuffer;
24
+ constructor(dsl: string);
25
+ /**
26
+ * Main entry point: tokenize the input and parse all policies.
27
+ * @returns Array of AbilityPolicy instances.
28
+ */
29
+ parse(): readonly AbilityPolicy<Resource, Environment>[];
30
+ /**
31
+ * Parses a single policy from the current token position.
32
+ *
33
+ * Grammar:
34
+ * policy = EFFECT PERMISSION IF (ALL | ANY) COLON ruleSets
35
+ */
36
+ private parsePolicy;
37
+ /**
38
+ * Parses a sequence of rule sets (groups) until a new policy starts or EOF.
39
+ */
40
+ private parseRuleSets;
41
+ /**
42
+ * Parses a single group, e.g. "all of:" or "any of:", and returns a RuleSet.
43
+ */
44
+ private parseGroup;
45
+ /**
46
+ * Parses a single rule: subject operator value
47
+ */
48
+ private parseRule;
49
+ /**
50
+ * Parses the comparison operator part of a rule.
51
+ * Returns both the resulting AbilityCondition and the token type that was consumed.
52
+ */
53
+ private parseConditionOperator;
54
+ /**
55
+ * Helper to match and consume a specific word token (KEYWORD or IDENTIFIER).
56
+ * @param word The exact string to look for.
57
+ * @returns True if the next token has that value.
58
+ */
59
+ private matchWord;
60
+ private matchSymbol;
61
+ /**
62
+ * Parses a resource value. Can be a string literal, number, boolean,
63
+ * null, a path (identifier), or an array.
64
+ */
65
+ private parseValue;
66
+ /**
67
+ * Parses an array literal: [ <value>, <value>, ... ]
68
+ * The opening bracket has already been consumed.
69
+ */
70
+ private parseArray;
71
+ private consumeLeadingComments;
72
+ private processCommentToken;
73
+ private takeAnnotations;
74
+ private syntaxError;
75
+ private getLine;
76
+ private suggest;
77
+ private levenshteinDistance;
78
+ private consumeOneOf;
79
+ private consume;
80
+ private check;
81
+ private isStartOfPolicy;
82
+ private isStartOfGroup;
83
+ private advance;
84
+ private peek;
85
+ private isAtEnd;
86
+ }
@@ -0,0 +1,13 @@
1
+ export declare class AbilityDSLSyntaxError extends Error {
2
+ readonly line: number;
3
+ readonly column: number;
4
+ readonly context: string;
5
+ readonly details: string;
6
+ private readonly _formattedMessage;
7
+ private readonly _originalStack?;
8
+ constructor(line: number, column: number, context: string, // строка DSL + ^ + соседние строки
9
+ details: string);
10
+ private static supportsColor;
11
+ private formatMessage;
12
+ toString(): string;
13
+ }
@@ -0,0 +1,55 @@
1
+ import AbilityCode from '../../core/AbilityCode';
2
+ export type TokenType = 'EFFECT' | 'IF' | 'PERMISSION' | 'IDENTIFIER' | 'COLON' | 'COMMA' | 'DOT' | 'LBRACKET' | 'RBRACKET' | 'ALL' | 'ANY' | 'OF' | 'EOF' | 'COMMENT' | 'EQ' | 'CONTAINS' | 'IN' | 'NOT_IN' | 'NOT_CONTAINS' | 'GT' | 'GTE' | 'LT' | 'LTE' | 'NULL' | 'EQ_NULL' | 'NOT_EQ_NULL' | 'NOT_EQ' | 'LEN_GT' | 'LEN_LT' | 'LEN_EQ' | 'STRING' | 'NUMBER' | 'BOOLEAN' | 'SYMBOL' | 'KEYWORD' | 'UNKNOWN';
3
+ /**
4
+ * Represents a single token produced by the Ability DSL lexer.
5
+ * Each token carries a type (e.g., EFFECT, IDENTIFIER, STRING) and its raw string value.
6
+ */
7
+ export declare class AbilityDSLToken<Code extends TokenType = TokenType> extends AbilityCode<Code> {
8
+ /** The literal text of the token as it appeared in the input (e.g., "permit", "user.roles", "admin"). */
9
+ readonly value: string;
10
+ /** The line number in DSL */
11
+ readonly line: number;
12
+ /** The column in dsl */
13
+ readonly column: number;
14
+ constructor(type: Code, value: string, line: number, column: number);
15
+ /**
16
+ * Returns a human-readable representation of the token, useful for debugging.
17
+ * Example output: "AbilityDSLToken([EFFECT] permit"
18
+ */
19
+ toString(): string;
20
+ static readonly EFFECT: TokenType;
21
+ static readonly IF: TokenType;
22
+ static readonly PERMISSION: TokenType;
23
+ static readonly IDENTIFIER: TokenType;
24
+ static readonly COLON: TokenType;
25
+ static readonly COMMA: TokenType;
26
+ static readonly DOT: TokenType;
27
+ static readonly LBRACKET: TokenType;
28
+ static readonly RBRACKET: TokenType;
29
+ static readonly ALL: TokenType;
30
+ static readonly ANY: TokenType;
31
+ static readonly OF: TokenType;
32
+ static readonly EOF: TokenType;
33
+ static readonly COMMENT: TokenType;
34
+ static readonly EQ: TokenType;
35
+ static readonly CONTAINS: TokenType;
36
+ static readonly IN: TokenType;
37
+ static readonly NOT_IN: TokenType;
38
+ static readonly NOT_CONTAINS: TokenType;
39
+ static readonly GT: TokenType;
40
+ static readonly GTE: TokenType;
41
+ static readonly LT: TokenType;
42
+ static readonly LTE: TokenType;
43
+ static readonly NULL: TokenType;
44
+ static readonly EQ_NULL: TokenType;
45
+ static readonly NOT_EQ_NULL: TokenType;
46
+ static readonly LEN_GT: TokenType;
47
+ static readonly LEN_LT: TokenType;
48
+ static readonly LEN_EQ: TokenType;
49
+ static readonly NOT_EQ: TokenType;
50
+ static readonly STRING: TokenType;
51
+ static readonly NUMBER: TokenType;
52
+ static readonly BOOLEAN: TokenType;
53
+ static readonly SYMBOL: TokenType;
54
+ static readonly KEYWORD: TokenType;
55
+ }
@@ -0,0 +1,22 @@
1
+ import { AbilityRule, AbilityRuleConfig } from '../../core/AbilityRule';
2
+ import { AbilityRuleSet, AbilityRuleSetConfig } from '../../core/AbilityRuleSet';
3
+ import { ResourceObject } from '../../core/AbilityParser';
4
+ import { AbilityPolicy, AbilityPolicyConfig } from '../../core/AbilityPolicy';
5
+ export declare class AbilityJSONParser {
6
+ /**
7
+ * Parses an array of policy configurations into an array of AbilityPolicy instances.
8
+ * @param configs - Array of policy configurations
9
+ * @returns Array of AbilityPolicy instances
10
+ */
11
+ static parse<Resource extends ResourceObject, Environment = unknown>(configs: readonly AbilityPolicyConfig[]): AbilityPolicy<Resource, Environment>[];
12
+ static parsePolicy<Resource extends ResourceObject = Record<string, unknown>, Environment = unknown>(config: AbilityPolicyConfig): AbilityPolicy<Resource, Environment>;
13
+ static parseRule<Resources extends object, Environment = unknown>(config: AbilityRuleConfig): AbilityRule<Resources, Environment>;
14
+ /**
15
+ * Parse the config JSON format to Group class instance
16
+ */
17
+ static parseRuleSet<Resource extends ResourceObject = Record<string, unknown>, Environment = unknown>(config: AbilityRuleSetConfig): AbilityRuleSet<Resource, Environment>;
18
+ static ruleToJSON(rule: AbilityRule): AbilityRuleConfig;
19
+ static ruleSetToJSON(ruleSet: AbilityRuleSet): AbilityRuleSetConfig;
20
+ static policyToJSON(policy: AbilityPolicy): AbilityPolicyConfig;
21
+ static toJSON(policies: readonly AbilityPolicy[]): AbilityPolicyConfig[];
22
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@via-profit/ability",
3
3
  "support": "https://via-profit.ru",
4
- "version": "3.0.1",
4
+ "version": "3.1.1",
5
5
  "description": "Via-Profit Ability service",
6
6
  "keywords": [
7
7
  "ability",
@@ -29,7 +29,8 @@
29
29
  "build:dist": "cross-env NODE_ENV=production webpack --config ./webpack/webpack-config.ts --color --progress",
30
30
  "build:playground": "cross-env NODE_ENV=development webpack --config ./webpack/webpack-config-playground.ts --color --progress",
31
31
  "playground": "npm run build:playground -- --watch",
32
- "test": "jest"
32
+ "test": "jest",
33
+ "bench": "npm run build && node ./bench/benchmark.js"
33
34
  },
34
35
  "repository": {
35
36
  "type": "git",
@@ -45,7 +46,6 @@
45
46
  ],
46
47
  "license": "MIT",
47
48
  "devDependencies": {
48
- "cross-env": "^7.0.3",
49
49
  "@eslint/js": "^9.13.0",
50
50
  "@jagi/jest-transform-graphql": "^1.0.2",
51
51
  "@jest/types": "^29.6.3",
@@ -55,6 +55,7 @@
55
55
  "@types/webpack": "^5.28.5",
56
56
  "@types/webpack-bundle-analyzer": "^4.7.0",
57
57
  "concurrently": "^9.0.1",
58
+ "cross-env": "^7.0.3",
58
59
  "eslint": "^9.13.0",
59
60
  "globals": "^15.11.0",
60
61
  "jest": "^29.7.0",
@@ -63,6 +64,7 @@
63
64
  "nodemon": "^3.1.7",
64
65
  "nodemon-webpack-plugin": "^4.8.2",
65
66
  "prettier": "^3.3.3",
67
+ "tinybench": "^6.0.0",
66
68
  "ts-jest": "^29.2.5",
67
69
  "ts-loader": "^9.5.1",
68
70
  "ts-node": "^10.9.2",
@@ -1,16 +0,0 @@
1
- import AbilityCode from './AbilityCode';
2
- export type AbilityConditionCodeType = '=' | '<>' | '>' | '<' | '>=' | '<=' | 'in' | 'not in';
3
- export type AbilityConditionLiteralType = 'equal' | 'not_equal' | 'more_than' | 'less_than' | 'less_or_equal' | 'more_or_equal' | 'in' | 'not_in';
4
- export declare class AbilityCondition extends AbilityCode<AbilityConditionCodeType> {
5
- static equal: AbilityCondition;
6
- static not_equal: AbilityCondition;
7
- static more_than: AbilityCondition;
8
- static less_than: AbilityCondition;
9
- static less_or_equal: AbilityCondition;
10
- static more_or_equal: AbilityCondition;
11
- static in: AbilityCondition;
12
- static not_in: AbilityCondition;
13
- static fromLiteral(literal: AbilityConditionLiteralType): AbilityCondition;
14
- get literal(): AbilityConditionLiteralType;
15
- }
16
- export default AbilityCondition;
@@ -1,32 +0,0 @@
1
- import AbilityPolicy from './AbilityPolicy';
2
- import AbilityPolicyEffect from './AbilityPolicyEffect';
3
- import { AbilityExplain } from '~/AbilityExplain';
4
- export declare class AbilityResolver<Resources extends object = object> {
5
- policies: readonly AbilityPolicy<Resources>[];
6
- constructor(policyOrListOfPolicies: readonly AbilityPolicy<Resources>[] | AbilityPolicy<Resources>);
7
- /**
8
- * Resolve policy for the resource and action
9
- *
10
- @param action - Action
11
- * @param resource - Resource
12
- */
13
- resolve<Action extends keyof Resources>(action: Action, resource: Resources[Action]): this;
14
- resolveWithExplain<Action extends keyof Resources>(action: Action, resource: Resources[Action]): readonly AbilityExplain[];
15
- enforce<Action extends keyof Resources>(action: Action, resource: Resources[Action]): void | never;
16
- /**
17
- * Get the last effect of the policy
18
- *
19
- * @returns {AbilityPolicyEffect | null}
20
- */
21
- getEffect(): AbilityPolicyEffect | null;
22
- isPermit(): boolean;
23
- isDeny(): boolean;
24
- getMatchedPolicy(): AbilityPolicy<Resources> | null;
25
- /**
26
- * Check if the action is contained in another action
27
- * @param actionA - The first action to check
28
- * @param actionB - The second action to check
29
- */
30
- static isInActionContain(actionA: string, actionB: string): boolean;
31
- }
32
- export default AbilityResolver;
@@ -1,78 +0,0 @@
1
- import AbilityMatch from './AbilityMatch';
2
- import AbilityCondition, { AbilityConditionCodeType } from './AbilityCondition';
3
- export type AbilityRuleConfig = {
4
- readonly id?: string | null;
5
- readonly name?: string | null;
6
- /**
7
- * Subject key path like a 'user.name'
8
- */
9
- readonly subject: string;
10
- /**
11
- * Resource key path like a 'user.name' or value
12
- */
13
- readonly resource: string | number | boolean | (string | number)[];
14
- readonly condition: AbilityConditionCodeType;
15
- };
16
- export type AbilityRuleConstructorProps = Omit<AbilityRuleConfig, 'condition'> & {
17
- readonly condition: AbilityCondition;
18
- };
19
- /**
20
- * Represents a rule that defines a condition to be checked against a subject and resource.
21
- */
22
- export declare class AbilityRule<Resources extends object = object> {
23
- /**
24
- * Subject key path like a 'user.name'
25
- */
26
- subject: string;
27
- /**
28
- * Resource key path like a 'user.name' or value
29
- */
30
- resource: string | number | boolean | (string | number)[];
31
- condition: AbilityCondition;
32
- name: string;
33
- id: string;
34
- state: AbilityMatch;
35
- /**
36
- * Creates an instance of AbilityRule.
37
- * @param {string} options.id - The unique identifier of the rule.
38
- * @param {string} options.name - The name of the rule.
39
- * @param {AbilityCondition} options.condition - The condition to evaluate.
40
- * @param {string} options.subject - The subject of the rule.
41
- * @param {string} options.resource - The resource to compare against.
42
- * @param params
43
- */
44
- constructor(params: AbilityRuleConstructorProps);
45
- /**
46
- * Check if the rule is matched
47
- * @param resource - The resource to check
48
- */
49
- check(resource: Resources | null): AbilityMatch;
50
- /**
51
- * Extract values from the resourceData
52
- * @param resourceData - The resourceData to extract values from
53
- */
54
- extractValues(resourceData: Resources | null): [
55
- string | number | boolean | (string | number)[] | null | undefined,
56
- string | number | boolean | (string | number)[] | null | undefined
57
- ];
58
- /**
59
- * Get the value of the object by dot notation
60
- * @param resource - The object to get the value from
61
- * @param desc - The dot notation string
62
- */
63
- getDotNotationValue<T = unknown>(resource: unknown, desc: string): T | undefined;
64
- static parse<Resources extends object>(config: AbilityRuleConfig): AbilityRule<Resources>;
65
- /**
66
- * Export the rule to config object
67
- */
68
- export(): AbilityRuleConfig;
69
- static equal<Resources extends object = object>(subject: string, resource: AbilityRuleConfig['resource']): AbilityRule<Resources>;
70
- static notIn<Resources extends object = object>(subject: string, resource: AbilityRuleConfig['resource']): AbilityRule<Resources>;
71
- static in<Resources extends object = object>(subject: string, resource: AbilityRuleConfig['resource']): AbilityRule<Resources>;
72
- static notEqual<Resources extends object = object>(subject: string, resource: AbilityRuleConfig['resource']): AbilityRule<Resources>;
73
- static lessThan<Resources extends object = object>(subject: string, resource: AbilityRuleConfig['resource']): AbilityRule<Resources>;
74
- static lessOrEqual<Resources extends object = object>(subject: string, resource: AbilityRuleConfig['resource']): AbilityRule<Resources>;
75
- static moreThan<Resources extends object = object>(subject: string, resource: AbilityRuleConfig['resource']): AbilityRule<Resources>;
76
- static moreOrEqual<Resources extends object = object>(subject: string, resource: AbilityRuleConfig['resource']): AbilityRule<Resources>;
77
- }
78
- export default AbilityRule;