Skip to content

Commit

Permalink
Move base normalization to RuntimeConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Aug 28, 2020
1 parent 86d5f36 commit 741356a
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 19 deletions.
2 changes: 1 addition & 1 deletion bin/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const setup = new Setup(httpServer, store, aclManager, runtimeConfig);

runtimeConfig.reset({ port: argv.port });
setup.setup().then((): void => {
process.stdout.write(`Running at ${runtimeConfig.base}\n`);
process.stdout.write(`Running at ${runtimeConfig.getBase()}\n`);
}).catch((error): void => {
process.stderr.write(`${error}\n`);
process.exit(1);
Expand Down
8 changes: 5 additions & 3 deletions src/init/RuntimeConfig.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ensureTrailingSlash, trimTrailingSlashes } from '../util/Util';

/**
* This class holds all configuration options that can be defined by the user via the command line.
*
Expand All @@ -14,12 +16,12 @@ export class RuntimeConfig implements RuntimeConfigData {

public reset(data: RuntimeConfigData): void {
this.pport = data.port ?? 3000;
this.pbase = data.base ?? `http://localhost:${this.port}/`;
this.pbase = ensureTrailingSlash(data.base ?? `http://localhost:${this.port}/`);
this.prootFilepath = data.rootFilepath ?? process.cwd();
}

public get base(): string {
return this.pbase;
public getBase(trailingSlash = true): string {
return trailingSlash ? this.pbase : trimTrailingSlashes(this.pbase);
}

public get port(): number {
Expand Down
6 changes: 3 additions & 3 deletions src/init/Setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ export class Setup {
acl:mode acl:Append;
acl:mode acl:Delete;
acl:mode acl:Control;
acl:accessTo <${this.runtimeConfig.base}>;
acl:default <${this.runtimeConfig.base}>.`;
acl:accessTo <${this.runtimeConfig.getBase()}>;
acl:default <${this.runtimeConfig.getBase()}>.`;
await this.store.setRepresentation(
await this.aclManager.getAcl({ path: this.runtimeConfig.base }),
await this.aclManager.getAcl({ path: this.runtimeConfig.getBase() }),
{
dataType: DATA_TYPE_BINARY,
data: streamifyArray([ acl ]),
Expand Down
2 changes: 1 addition & 1 deletion src/storage/FileResourceStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class FileResourceStore implements ResourceStore {
}

public get baseRequestURI(): string {
return trimTrailingSlashes(this.runtimeConfig.base);
return this.runtimeConfig.getBase(false);
}

public get rootFilepath(): string {
Expand Down
4 changes: 2 additions & 2 deletions src/storage/SimpleResourceStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ export class SimpleResourceStore implements ResourceStore {
* @returns A string representing the relative path.
*/
private parseIdentifier(identifier: ResourceIdentifier): string {
const path = identifier.path.slice(this.runtimeConfig.base.length);
if (!identifier.path.startsWith(this.runtimeConfig.base)) {
const path = identifier.path.slice(this.runtimeConfig.getBase().length);
if (!identifier.path.startsWith(this.runtimeConfig.getBase())) {
throw new NotFoundHttpError();
}
return path;
Expand Down
4 changes: 2 additions & 2 deletions src/storage/UrlContainerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class UrlContainerManager implements ContainerManager {

public async getContainer(id: ResourceIdentifier): Promise<ResourceIdentifier> {
const path = this.canonicalUrl(id.path);
if (this.canonicalUrl(this.runtimeConfig.base) === path) {
if (this.runtimeConfig.getBase() === path) {
throw new Error('Root does not have a container.');
}

Expand All @@ -30,6 +30,6 @@ export class UrlContainerManager implements ContainerManager {
}

private canonicalUrl(path: string): string {
return ensureTrailingSlash(new URL(path).toString());
return ensureTrailingSlash(path.toString());
}
}
24 changes: 17 additions & 7 deletions test/unit/init/RuntimeConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,50 @@ describe('RuntimeConfig', (): void => {
it('handles undefined args.', async(): Promise<void> => {
const config = new RuntimeConfig();
expect(config.port).toEqual(3000);
expect(config.base).toEqual('http://localhost:3000/');
expect(config.getBase()).toEqual('http://localhost:3000/');
});

it('handles empty args.', async(): Promise<void> => {
const config = new RuntimeConfig({});
expect(config.port).toEqual(3000);
expect(config.base).toEqual('http://localhost:3000/');
expect(config.getBase()).toEqual('http://localhost:3000/');
});

it('handles args with port.', async(): Promise<void> => {
const config = new RuntimeConfig({ port: 1234 });
expect(config.port).toEqual(1234);
expect(config.base).toEqual('http://localhost:1234/');
expect(config.getBase()).toEqual('http://localhost:1234/');
});

it('handles args with base.', async(): Promise<void> => {
const config = new RuntimeConfig({ base: 'http://example.org/' });
expect(config.port).toEqual(3000);
expect(config.base).toEqual('http://example.org/');
expect(config.getBase()).toEqual('http://example.org/');
});

it('handles args with port and base.', async(): Promise<void> => {
const config = new RuntimeConfig({ port: 1234, base: 'http://example.org/' });
expect(config.port).toEqual(1234);
expect(config.base).toEqual('http://example.org/');
expect(config.getBase()).toEqual('http://example.org/');
});

it('handles resetting data.', async(): Promise<void> => {
const config = new RuntimeConfig({});
expect(config.port).toEqual(3000);
expect(config.base).toEqual('http://localhost:3000/');
expect(config.getBase()).toEqual('http://localhost:3000/');

config.reset({ port: 1234, base: 'http://example.org/' });
expect(config.port).toEqual(1234);
expect(config.base).toEqual('http://example.org/');
expect(config.getBase()).toEqual('http://example.org/');
});

it('ensures trailing slash in base.', async(): Promise<void> => {
const config = new RuntimeConfig({ base: 'http://example.org' });
expect(config.getBase()).toEqual('http://example.org/');
});

it('allows base to be retrieved without trailing slash.', async(): Promise<void> => {
expect(new RuntimeConfig({ base: 'http://example.org' }).getBase(false)).toEqual('http://example.org');
expect(new RuntimeConfig({ base: 'http://example.org/' }).getBase(false)).toEqual('http://example.org');
});
});

0 comments on commit 741356a

Please sign in to comment.