Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2240c93db5 | |||
| 3e9052c4a7 | |||
| 326f6fe851 | |||
| 5b57b67542 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -9,6 +9,7 @@ node_modules
|
|||||||
uploads
|
uploads
|
||||||
public/momentum*js
|
public/momentum*js
|
||||||
junit.xml
|
junit.xml
|
||||||
|
/docs/
|
||||||
|
|
||||||
# Logs
|
# Logs
|
||||||
*.log
|
*.log
|
||||||
|
|||||||
5
.gitmodules
vendored
5
.gitmodules
vendored
@@ -1,5 +0,0 @@
|
|||||||
[submodule "docs"]
|
|
||||||
path = docs
|
|
||||||
url = ../utils.wiki.git
|
|
||||||
branch = master
|
|
||||||
ignore = all
|
|
||||||
1
docs
1
docs
Submodule docs deleted from dbf5d8cd07
18
package.json
18
package.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.27.11",
|
"version": "0.27.14",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
@@ -30,15 +30,15 @@
|
|||||||
"var-persist": "^1.0.1"
|
"var-persist": "^1.0.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/jest": "^29.5.12",
|
"@types/jest": "^30.0.0",
|
||||||
"fake-indexeddb": "^6.0.1",
|
"fake-indexeddb": "^6.2.5",
|
||||||
"jest": "^29.7.0",
|
"jest": "^30.2.0",
|
||||||
"jest-junit": "^16.0.0",
|
"jest-junit": "^16.0.0",
|
||||||
"ts-jest": "^29.1.2",
|
"ts-jest": "^29.4.5",
|
||||||
"typedoc": "^0.26.7",
|
"typedoc": "^0.28.15",
|
||||||
"typescript": "^5.3.3",
|
"typescript": "^5.9.3",
|
||||||
"vite": "^5.0.12",
|
"vite": "^7.2.4",
|
||||||
"vite-plugin-dts": "^4.5.3"
|
"vite-plugin-dts": "^4.5.4"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"dist"
|
"dist"
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ export class PathEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check cache first
|
// Check cache first
|
||||||
if (PathEvent.pathEventCache.has(e)) {
|
if(PathEvent.pathEventCache.has(e)) {
|
||||||
Object.assign(this, PathEvent.pathEventCache.get(e)!);
|
Object.assign(this, PathEvent.pathEventCache.get(e)!);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -119,25 +119,13 @@ export class PathEvent {
|
|||||||
if(!method) method = '*';
|
if(!method) method = '*';
|
||||||
|
|
||||||
// Handle special cases
|
// Handle special cases
|
||||||
if(p === '' || p === undefined) {
|
if(p === '' || p === undefined || p === '*') {
|
||||||
// Empty string matches nothing
|
// Empty string with methods (e.g., ":*") matches the root level/everything
|
||||||
this.module = '';
|
|
||||||
this.path = '';
|
|
||||||
this.fullPath = '';
|
|
||||||
this.name = '';
|
|
||||||
this.methods = new ASet<Method>(['n']);
|
|
||||||
this.hasGlob = false;
|
|
||||||
PathEvent.pathEventCache.set(e, this);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(p === '*') {
|
|
||||||
// Wildcard means any path any event
|
|
||||||
this.module = '';
|
this.module = '';
|
||||||
this.path = '';
|
this.path = '';
|
||||||
this.fullPath = '**';
|
this.fullPath = '**';
|
||||||
this.name = '';
|
this.name = '';
|
||||||
this.methods = new ASet<Method>(['*']);
|
this.methods = new ASet<Method>(p === '*' ? ['*'] : <any>method.split(''));
|
||||||
this.hasGlob = true;
|
this.hasGlob = true;
|
||||||
PathEvent.pathEventCache.set(e, this);
|
PathEvent.pathEventCache.set(e, this);
|
||||||
return;
|
return;
|
||||||
@@ -174,7 +162,7 @@ export class PathEvent {
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
private static scoreSpecificity(path: string): number {
|
private static scoreSpecificity(path: string): number {
|
||||||
if (path === '**' || path === '') return Number.MAX_SAFE_INTEGER; // Least specific
|
if(path === '**' || path === '') return Number.MAX_SAFE_INTEGER; // Least specific
|
||||||
|
|
||||||
const segments = path.split('/').filter(p => !!p);
|
const segments = path.split('/').filter(p => !!p);
|
||||||
// Base score: number of segments (more segments = more specific = lower score)
|
// Base score: number of segments (more segments = more specific = lower score)
|
||||||
@@ -183,8 +171,8 @@ export class PathEvent {
|
|||||||
// Penalty for wildcards (makes them less specific than exact matches)
|
// Penalty for wildcards (makes them less specific than exact matches)
|
||||||
// ADD to score to make it HIGHER/WORSE
|
// ADD to score to make it HIGHER/WORSE
|
||||||
segments.forEach(seg => {
|
segments.forEach(seg => {
|
||||||
if (seg === '**') score += 0.5;
|
if(seg === '**') score += 0.5;
|
||||||
else if (seg === '*') score += 0.25;
|
else if(seg === '*') score += 0.25;
|
||||||
});
|
});
|
||||||
|
|
||||||
return score;
|
return score;
|
||||||
@@ -196,7 +184,7 @@ export class PathEvent {
|
|||||||
*/
|
*/
|
||||||
private static pathMatchesGlob(path: string, pattern: string): boolean {
|
private static pathMatchesGlob(path: string, pattern: string): boolean {
|
||||||
// Handle exact match
|
// Handle exact match
|
||||||
if (pattern === path) return true;
|
if(pattern === path) return true;
|
||||||
|
|
||||||
const pathParts = path.split('/').filter(p => !!p);
|
const pathParts = path.split('/').filter(p => !!p);
|
||||||
const patternParts = pattern.split('/').filter(p => !!p);
|
const patternParts = pattern.split('/').filter(p => !!p);
|
||||||
@@ -207,28 +195,28 @@ export class PathEvent {
|
|||||||
while (patternIdx < patternParts.length && pathIdx < pathParts.length) {
|
while (patternIdx < patternParts.length && pathIdx < pathParts.length) {
|
||||||
const patternPart = patternParts[patternIdx];
|
const patternPart = patternParts[patternIdx];
|
||||||
|
|
||||||
if (patternPart === '**') {
|
if(patternPart === '**') {
|
||||||
// ** matches zero or more path segments
|
// ** matches zero or more path segments
|
||||||
if (patternIdx === patternParts.length - 1) {
|
if(patternIdx === patternParts.length - 1) {
|
||||||
// ** at the end matches everything
|
// ** at the end matches everything
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
// Try matching from next pattern part onwards
|
// Try matching from next pattern part onwards
|
||||||
const nextPattern = patternParts[patternIdx + 1];
|
const nextPattern = patternParts[patternIdx + 1];
|
||||||
while (pathIdx < pathParts.length) {
|
while (pathIdx < pathParts.length) {
|
||||||
if (PathEvent.pathMatchesGlob(pathParts.slice(pathIdx).join('/'), patternParts.slice(patternIdx + 1).join('/'))) {
|
if(PathEvent.pathMatchesGlob(pathParts.slice(pathIdx).join('/'), patternParts.slice(patternIdx + 1).join('/'))) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
pathIdx++;
|
pathIdx++;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
} else if (patternPart === '*') {
|
} else if(patternPart === '*') {
|
||||||
// * matches exactly one segment
|
// * matches exactly one segment
|
||||||
pathIdx++;
|
pathIdx++;
|
||||||
patternIdx++;
|
patternIdx++;
|
||||||
} else {
|
} else {
|
||||||
// Exact match required
|
// Exact match required
|
||||||
if (patternPart !== pathParts[pathIdx]) {
|
if(patternPart !== pathParts[pathIdx]) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
pathIdx++;
|
pathIdx++;
|
||||||
@@ -237,7 +225,7 @@ export class PathEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check if we've consumed all pattern parts
|
// Check if we've consumed all pattern parts
|
||||||
if (patternIdx < patternParts.length) {
|
if(patternIdx < patternParts.length) {
|
||||||
// Remaining pattern parts must all be ** to match
|
// Remaining pattern parts must all be ** to match
|
||||||
return patternParts.slice(patternIdx).every(p => p === '**');
|
return patternParts.slice(patternIdx).every(p => p === '**');
|
||||||
}
|
}
|
||||||
@@ -265,13 +253,13 @@ export class PathEvent {
|
|||||||
let result: PathEvent | null = null;
|
let result: PathEvent | null = null;
|
||||||
|
|
||||||
for (const p of sorted) {
|
for (const p of sorted) {
|
||||||
if (!result) {
|
if(!result) {
|
||||||
result = p;
|
result = p;
|
||||||
} else {
|
} else {
|
||||||
// Only combine if current result's path starts with or matches the new permission's path
|
// Only combine if current result's path starts with or matches the new permission's path
|
||||||
if (result.fullPath.startsWith(p.fullPath)) {
|
if(result.fullPath.startsWith(p.fullPath)) {
|
||||||
// If we hit a none at a parent level, stop here
|
// If we hit a none at a parent level, stop here
|
||||||
if (p.none) {
|
if(p.none) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// Combine methods for permissions in the same hierarchy
|
// Combine methods for permissions in the same hierarchy
|
||||||
@@ -293,11 +281,7 @@ export class PathEvent {
|
|||||||
static filter(target: string | PathEvent | (string | PathEvent)[], ...filter: (string | PathEvent)[]): PathEvent[] {
|
static filter(target: string | PathEvent | (string | PathEvent)[], ...filter: (string | PathEvent)[]): PathEvent[] {
|
||||||
const parsedTarget = makeArray(target).map(pe => pe instanceof PathEvent ? pe : new PathEvent(pe));
|
const parsedTarget = makeArray(target).map(pe => pe instanceof PathEvent ? pe : new PathEvent(pe));
|
||||||
const parsedFilter = makeArray(filter).map(pe => pe instanceof PathEvent ? pe : new PathEvent(pe));
|
const parsedFilter = makeArray(filter).map(pe => pe instanceof PathEvent ? pe : new PathEvent(pe));
|
||||||
|
return parsedTarget.filter(t => !!parsedFilter.find(r => PathEvent.matches(r, t)));
|
||||||
return parsedTarget.filter(t => {
|
|
||||||
const combined = PathEvent.combine(t);
|
|
||||||
return !!parsedFilter.find(r => PathEvent.matches(r, combined));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -306,20 +290,21 @@ export class PathEvent {
|
|||||||
*/
|
*/
|
||||||
private static matches(pattern: PathEvent, target: PathEvent): boolean {
|
private static matches(pattern: PathEvent, target: PathEvent): boolean {
|
||||||
// Handle special cases
|
// Handle special cases
|
||||||
if (pattern.fullPath === '' || target.fullPath === '') return false;
|
if(pattern.fullPath === '' || target.fullPath === '') return false;
|
||||||
if (pattern.fullPath === '*' || target.fullPath === '*') return pattern.methods.has('*') || target.methods.has('*') || pattern.methods.intersection(target.methods).length > 0;
|
if(pattern.fullPath === '**') return pattern.methods.has('*') || pattern.methods.intersection(target.methods).length > 0;
|
||||||
|
if(target.fullPath === '**') return pattern.methods.has('*') || target.methods.has('*') || pattern.methods.intersection(target.methods).length > 0;
|
||||||
|
|
||||||
// Check methods
|
// Check methods
|
||||||
const methodsMatch = pattern.all || target.all || pattern.methods.intersection(target.methods).length > 0;
|
const methodsMatch = pattern.all || target.all || pattern.methods.intersection(target.methods).length > 0;
|
||||||
if (!methodsMatch) return false;
|
if(!methodsMatch) return false;
|
||||||
|
|
||||||
// Check paths
|
// Check paths
|
||||||
if (!pattern.hasGlob && !target.hasGlob) {
|
if(!pattern.hasGlob && !target.hasGlob) {
|
||||||
// Fast path: no globs, use string comparison
|
// Fast path: no globs, use string comparison
|
||||||
return pattern.fullPath === target.fullPath;
|
return pattern.fullPath === target.fullPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pattern.hasGlob) {
|
if(pattern.hasGlob) {
|
||||||
// Pattern has glob, match target against it
|
// Pattern has glob, match target against it
|
||||||
return this.pathMatchesGlob(target.fullPath, pattern.fullPath);
|
return this.pathMatchesGlob(target.fullPath, pattern.fullPath);
|
||||||
}
|
}
|
||||||
@@ -339,10 +324,8 @@ export class PathEvent {
|
|||||||
const parsedTarget = makeArray(target).map(pe => pe instanceof PathEvent ? pe : new PathEvent(pe));
|
const parsedTarget = makeArray(target).map(pe => pe instanceof PathEvent ? pe : new PathEvent(pe));
|
||||||
const parsedRequired = makeArray(has).map(pe => pe instanceof PathEvent ? pe : new PathEvent(pe));
|
const parsedRequired = makeArray(has).map(pe => pe instanceof PathEvent ? pe : new PathEvent(pe));
|
||||||
|
|
||||||
// If target is a single item, check directly; if multiple, combine first
|
// Check if any target permission matches any required permission
|
||||||
const effectiveTarget = parsedTarget.length === 1 ? parsedTarget[0] : PathEvent.combine(...parsedTarget);
|
return !!parsedRequired.find(r => !!parsedTarget.find(t => PathEvent.matches(r, t)));
|
||||||
|
|
||||||
return !!parsedRequired.find(r => PathEvent.matches(r, effectiveTarget));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -45,12 +45,12 @@ describe('Path Events', () => {
|
|||||||
expect(pe.all).toBe(true);
|
expect(pe.all).toBe(true);
|
||||||
expect(pe.fullPath).toBe('**');
|
expect(pe.fullPath).toBe('**');
|
||||||
expect(pe.methods.has('*')).toBe(true);
|
expect(pe.methods.has('*')).toBe(true);
|
||||||
|
expect(pe.hasAll('module:c', 'some/path:ud')).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('parses empty string as none', () => {
|
it('parses empty string as wildcard', () => {
|
||||||
const pe = new PathEvent('');
|
const pe = new PathEvent('');
|
||||||
expect(pe.none).toBe(true);
|
expect(pe.has('any/path:c')).toBe(true);
|
||||||
expect(pe.fullPath).toBe('');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('parses none method', () => {
|
it('parses none method', () => {
|
||||||
@@ -80,6 +80,7 @@ describe('Path Events', () => {
|
|||||||
expect(c.methods.has('c')).toBe(true);
|
expect(c.methods.has('c')).toBe(true);
|
||||||
expect(c.methods.has('r')).toBe(true);
|
expect(c.methods.has('r')).toBe(true);
|
||||||
expect(c.methods.has('u')).toBe(true);
|
expect(c.methods.has('u')).toBe(true);
|
||||||
|
expect(c.delete).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('combine stops at none', () => {
|
it('combine stops at none', () => {
|
||||||
@@ -94,6 +95,17 @@ describe('Path Events', () => {
|
|||||||
expect(d.none).toBe(false);
|
expect(d.none).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('combine works with wildcards', () => {
|
||||||
|
expect(PathEvent.has([
|
||||||
|
'payments/anonymous:d',
|
||||||
|
'payments/anonymous:c',
|
||||||
|
'payments/system:cr',
|
||||||
|
'logs/Momentum:c',
|
||||||
|
'products:r',
|
||||||
|
'*'
|
||||||
|
], 'actions/692a92d18afa11e6722e9f2e:r')).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
it('filter finds overlap by path and methods', () => {
|
it('filter finds overlap by path and methods', () => {
|
||||||
const events = [
|
const events = [
|
||||||
new PathEvent('users/sys:cr'),
|
new PathEvent('users/sys:cr'),
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ describe('Time Utilities', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('throws for unknown timezone', () => {
|
it('throws for unknown timezone', () => {
|
||||||
expect(() => formatDate('YYYY', new Date(), '???')).toThrowError(/Invalid timezone/);
|
expect(() => formatDate('YYYY', new Date(), '???')).toThrow(/Invalid timezone/);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('handles timezone by offset number', () => {
|
it('handles timezone by offset number', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user