@via-profit/ability 2.1.0 → 3.1.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.
Files changed (36) hide show
  1. package/CHANGELOG.md +129 -0
  2. package/CONTRIBUTING.md +14 -0
  3. package/LICENSE +21 -0
  4. package/README.md +1058 -319
  5. package/SECURITY.md +33 -0
  6. package/dist/cache/AbilityCacheAdapter.d.ts +8 -0
  7. package/dist/cache/AbilityInMemoryCache.d.ts +12 -0
  8. package/dist/core/AbilityCondition.d.ts +21 -0
  9. package/dist/core/AbilityExplain.d.ts +27 -0
  10. package/dist/core/AbilityParser.d.ts +61 -0
  11. package/dist/core/AbilityPolicy.d.ts +84 -0
  12. package/dist/core/AbilityResolver.d.ts +35 -0
  13. package/dist/core/AbilityResult.d.ts +27 -0
  14. package/dist/core/AbilityRule.d.ts +77 -0
  15. package/dist/{AbilityRuleSet.d.ts → core/AbilityRuleSet.d.ts} +14 -11
  16. package/dist/index.d.ts +19 -11
  17. package/dist/index.js +2097 -303
  18. package/dist/parsers/dsl/AbilityDSLLexer.d.ts +24 -0
  19. package/dist/parsers/dsl/AbilityDSLParser.d.ts +86 -0
  20. package/dist/parsers/dsl/AbilityDSLSyntaxError.d.ts +13 -0
  21. package/dist/parsers/dsl/AbilityDSLToken.d.ts +55 -0
  22. package/dist/parsers/json/AbilityJSONParser.d.ts +22 -0
  23. package/package.json +15 -4
  24. package/assets/ability-01.drawio.png +0 -0
  25. package/build/playground.js +0 -456
  26. package/build/playground.js.map +0 -1
  27. package/dist/AbilityCondition.d.ts +0 -16
  28. package/dist/AbilityParser.d.ts +0 -18
  29. package/dist/AbilityPolicy.d.ts +0 -65
  30. package/dist/AbilityResolver.d.ts +0 -30
  31. package/dist/AbilityRule.d.ts +0 -70
  32. /package/dist/{AbilityCode.d.ts → core/AbilityCode.d.ts} +0 -0
  33. /package/dist/{AbilityCompare.d.ts → core/AbilityCompare.d.ts} +0 -0
  34. /package/dist/{AbilityError.d.ts → core/AbilityError.d.ts} +0 -0
  35. /package/dist/{AbilityMatch.d.ts → core/AbilityMatch.d.ts} +0 -0
  36. /package/dist/{AbilityPolicyEffect.d.ts → core/AbilityPolicyEffect.d.ts} +0 -0
@@ -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,26 +1,36 @@
1
1
  {
2
2
  "name": "@via-profit/ability",
3
3
  "support": "https://via-profit.ru",
4
- "version": "2.1.0",
4
+ "version": "3.1.0",
5
5
  "description": "Via-Profit Ability service",
6
6
  "keywords": [
7
7
  "ability",
8
8
  "access",
9
9
  "via-profit"
10
- ],
10
+ ],
11
11
  "main": "./dist/index.js",
12
12
  "engines": {
13
13
  "node": ">= 17.0.0",
14
14
  "npm": ">= 8.19.3"
15
15
  },
16
+ "files": [
17
+ "dist",
18
+ "README.md",
19
+ "LICENSE",
20
+ "CHANGELOG.md",
21
+ "CONTRIBUTING.md",
22
+ "SECURITY.md"
23
+ ],
16
24
  "scripts": {
17
25
  "lint": "tsc --noEmit && eslint --fix .",
18
26
  "pretty": "prettier --write ./src",
27
+ "build": "npm run build:dist",
19
28
  "build:dev": "cross-env NODE_ENV=development webpack --config ./webpack/webpack-config.ts --color --progress",
20
29
  "build:dist": "cross-env NODE_ENV=production webpack --config ./webpack/webpack-config.ts --color --progress",
21
30
  "build:playground": "cross-env NODE_ENV=development webpack --config ./webpack/webpack-config-playground.ts --color --progress",
22
31
  "playground": "npm run build:playground -- --watch",
23
- "test": "jest"
32
+ "test": "jest",
33
+ "bench": "npm run build && node ./bench/benchmark.js"
24
34
  },
25
35
  "repository": {
26
36
  "type": "git",
@@ -36,7 +46,6 @@
36
46
  ],
37
47
  "license": "MIT",
38
48
  "devDependencies": {
39
- "cross-env": "^7.0.3",
40
49
  "@eslint/js": "^9.13.0",
41
50
  "@jagi/jest-transform-graphql": "^1.0.2",
42
51
  "@jest/types": "^29.6.3",
@@ -46,6 +55,7 @@
46
55
  "@types/webpack": "^5.28.5",
47
56
  "@types/webpack-bundle-analyzer": "^4.7.0",
48
57
  "concurrently": "^9.0.1",
58
+ "cross-env": "^7.0.3",
49
59
  "eslint": "^9.13.0",
50
60
  "globals": "^15.11.0",
51
61
  "jest": "^29.7.0",
@@ -54,6 +64,7 @@
54
64
  "nodemon": "^3.1.7",
55
65
  "nodemon-webpack-plugin": "^4.8.2",
56
66
  "prettier": "^3.3.3",
67
+ "tinybench": "^6.0.0",
57
68
  "ts-jest": "^29.2.5",
58
69
  "ts-loader": "^9.5.1",
59
70
  "ts-node": "^10.9.2",
Binary file