feat: Create SubdomainIdentifierGenerator

To be used when creating pods.
This commit is contained in:
Joachim Van Herwegen
2021-02-12 12:52:47 +01:00
parent a28fb0258f
commit 6e2a4b5c2b
5 changed files with 42 additions and 6 deletions

View File

@@ -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';

View File

@@ -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;
}

View 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}` };
}
}

View File

@@ -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) };
}
}

View 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/' });
});
});