mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Add contains function to IdentifierStrategy
This commit is contained in:
@@ -1,12 +1,16 @@
|
||||
import { URL } from 'url';
|
||||
import type { ResourceIdentifier } from '../../http/representation/ResourceIdentifier';
|
||||
import { InternalServerError } from '../errors/InternalServerError';
|
||||
import { ensureTrailingSlash } from '../PathUtil';
|
||||
import { ensureTrailingSlash, isContainerIdentifier } from '../PathUtil';
|
||||
import type { IdentifierStrategy } from './IdentifierStrategy';
|
||||
|
||||
/**
|
||||
* Provides a default implementation for `getParentContainer`
|
||||
* which checks if the identifier is supported and not a root container.
|
||||
* If not, the last part before the first relevant slash will be removed to find the parent.
|
||||
*
|
||||
* Provides a default implementation for `contains`
|
||||
* which does standard slash-semantics based string comparison.
|
||||
*/
|
||||
export abstract class BaseIdentifierStrategy implements IdentifierStrategy {
|
||||
public abstract supportsIdentifier(identifier: ResourceIdentifier): boolean;
|
||||
@@ -27,4 +31,22 @@ export abstract class BaseIdentifierStrategy implements IdentifierStrategy {
|
||||
}
|
||||
|
||||
public abstract isRootContainer(identifier: ResourceIdentifier): boolean;
|
||||
|
||||
public contains(container: ResourceIdentifier, identifier: ResourceIdentifier, transitive: boolean): boolean {
|
||||
if (!isContainerIdentifier(container)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!identifier.path.startsWith(container.path)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (transitive) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const tail = identifier.path.slice(container.path.length);
|
||||
// If there is at least one `/` followed by a char this is not a direct parent container
|
||||
return !/\/./u.test(tail);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,4 +23,14 @@ export interface IdentifierStrategy {
|
||||
* This does not check if this identifier actually exists.
|
||||
*/
|
||||
isRootContainer: (identifier: ResourceIdentifier) => boolean;
|
||||
|
||||
/**
|
||||
* Checks if the given container would contain the given identifier.
|
||||
* This does not check that either of these identifiers actually exist.
|
||||
* This is similar to calling {@link getParentContainer} on an identifier
|
||||
* and comparing the result.
|
||||
*
|
||||
* If `transitive` is `false` this only checks if `container` is the direct parent container of `identifier`.
|
||||
*/
|
||||
contains: (container: ResourceIdentifier, identifier: ResourceIdentifier, transitive: boolean) => boolean;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user