mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Create SubdomainIdentifierGenerator
To be used when creating pods.
This commit is contained in:
@@ -117,6 +117,7 @@ export * from './pods/agent/AgentParser';
|
||||
export * from './pods/generate/HandlebarsTemplateEngine';
|
||||
export * from './pods/generate/IdentifierGenerator';
|
||||
export * from './pods/generate/ResourcesGenerator';
|
||||
export * from './pods/generate/SubdomainIdentifierGenerator';
|
||||
export * from './pods/generate/SuffixIdentifierGenerator';
|
||||
export * from './pods/generate/TemplateEngine';
|
||||
export * from './pods/generate/TemplatedResourcesGenerator';
|
||||
|
||||
@@ -5,8 +5,8 @@ import type { ResourceIdentifier } from '../../ldp/representation/ResourceIdenti
|
||||
*/
|
||||
export interface IdentifierGenerator {
|
||||
/**
|
||||
* Generates container identifiers based on an input slug.
|
||||
* Generates container identifiers based on an input name.
|
||||
* This is simply string generation, no resource-related checks are run.
|
||||
*/
|
||||
generate: (slug: string) => ResourceIdentifier;
|
||||
generate: (name: string) => ResourceIdentifier;
|
||||
}
|
||||
|
||||
21
src/pods/generate/SubdomainIdentifierGenerator.ts
Normal file
21
src/pods/generate/SubdomainIdentifierGenerator.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import type { ResourceIdentifier } from '../../ldp/representation/ResourceIdentifier';
|
||||
import { ensureTrailingSlash, extractScheme } from '../../util/PathUtil';
|
||||
import type { IdentifierGenerator } from './IdentifierGenerator';
|
||||
|
||||
/**
|
||||
* Generates identifiers by using the name as a subdomain on the base URL.
|
||||
* Non-alphanumeric characters will be replaced with `-`.
|
||||
*/
|
||||
export class SubdomainIdentifierGenerator implements IdentifierGenerator {
|
||||
private readonly baseParts: { scheme: string; rest: string };
|
||||
|
||||
public constructor(baseUrl: string) {
|
||||
this.baseParts = extractScheme(ensureTrailingSlash(baseUrl));
|
||||
}
|
||||
|
||||
public generate(name: string): ResourceIdentifier {
|
||||
// Using the punycode converter is a risk as it doesn't convert slashes for example
|
||||
const cleanName = name.replace(/\W/gu, '-');
|
||||
return { path: `${this.baseParts.scheme}${cleanName}.${this.baseParts.rest}` };
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ import { ensureTrailingSlash } from '../../util/PathUtil';
|
||||
import type { IdentifierGenerator } from './IdentifierGenerator';
|
||||
|
||||
/**
|
||||
* Generates identifiers by appending the slug to a stored base identifier.
|
||||
* Generates identifiers by appending the name to a stored base identifier.
|
||||
* Non-alphanumeric characters will be replaced with `-`.
|
||||
*/
|
||||
export class SuffixIdentifierGenerator implements IdentifierGenerator {
|
||||
@@ -13,8 +13,8 @@ export class SuffixIdentifierGenerator implements IdentifierGenerator {
|
||||
this.base = base;
|
||||
}
|
||||
|
||||
public generate(slug: string): ResourceIdentifier {
|
||||
const cleanSlug = slug.replace(/\W/gu, '-');
|
||||
return { path: ensureTrailingSlash(new URL(cleanSlug, this.base).href) };
|
||||
public generate(name: string): ResourceIdentifier {
|
||||
const cleanName = name.replace(/\W/gu, '-');
|
||||
return { path: ensureTrailingSlash(new URL(cleanName, this.base).href) };
|
||||
}
|
||||
}
|
||||
|
||||
14
test/unit/pods/generate/SubdomainIdentifierGenerator.test.ts
Normal file
14
test/unit/pods/generate/SubdomainIdentifierGenerator.test.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { SubdomainIdentifierGenerator } from '../../../../src/pods/generate/SubdomainIdentifierGenerator';
|
||||
|
||||
describe('A SubdomainIdentifierGenerator', (): void => {
|
||||
const base = 'http://test.com/';
|
||||
const generator = new SubdomainIdentifierGenerator(base);
|
||||
|
||||
it('generates identifiers by using the slug as subdomain.', async(): Promise<void> => {
|
||||
expect(generator.generate('slug')).toEqual({ path: 'http://slug.test.com/' });
|
||||
});
|
||||
|
||||
it('converts slugs using punycode.', async(): Promise<void> => {
|
||||
expect(generator.generate('sàl/u㋡g')).toEqual({ path: 'http://s-l-u-g.test.com/' });
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user