mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Warn users when they change the base URL
This commit is contained in:
parent
e604c0c2e4
commit
62e2210023
@ -1,6 +1,7 @@
|
||||
{
|
||||
"@context": "https://linkedsoftwaredependencies.org/bundles/npm/@solid/community-server/^2.0.0/components/context.jsonld",
|
||||
"import": [
|
||||
"files-scs:config/app/init/initializers/base-url.json",
|
||||
"files-scs:config/app/init/initializers/logger.json",
|
||||
"files-scs:config/app/init/initializers/server.json"
|
||||
],
|
||||
@ -11,6 +12,7 @@
|
||||
"@type": "SequenceHandler",
|
||||
"handlers": [
|
||||
{ "@id": "urn:solid-server:default:LoggerInitializer" },
|
||||
{ "@id": "urn:solid-server:default:BaseUrlVerifier" },
|
||||
{ "@id": "urn:solid-server:default:ParallelInitializer" },
|
||||
{ "@id": "urn:solid-server:default:ServerInitializer" }
|
||||
]
|
||||
|
13
config/app/init/initializers/base-url.json
Normal file
13
config/app/init/initializers/base-url.json
Normal file
@ -0,0 +1,13 @@
|
||||
{
|
||||
"@context": "https://linkedsoftwaredependencies.org/bundles/npm/@solid/community-server/^2.0.0/components/context.jsonld",
|
||||
"@graph": [
|
||||
{
|
||||
"comment": "Logs a warning if the base URL changes.",
|
||||
"@id": "urn:solid-server:default:BaseUrlVerifier",
|
||||
"@type": "BaseUrlVerifier",
|
||||
"baseUrl": { "@id": "urn:solid-server:default:variable:baseUrl" },
|
||||
"storageKey": "current-base-url",
|
||||
"storage": { "@id": "urn:solid-server:default:SetupStorage" }
|
||||
}
|
||||
]
|
||||
}
|
@ -201,6 +201,7 @@ export * from './init/variables/SettingsResolver';
|
||||
// Init
|
||||
export * from './init/App';
|
||||
export * from './init/AppRunner';
|
||||
export * from './init/BaseUrlVerifier';
|
||||
export * from './init/CliResolver';
|
||||
export * from './init/ConfigPodInitializer';
|
||||
export * from './init/ContainerInitializer';
|
||||
|
32
src/init/BaseUrlVerifier.ts
Normal file
32
src/init/BaseUrlVerifier.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import { getLoggerFor } from '../logging/LogUtil';
|
||||
import type { KeyValueStorage } from '../storage/keyvalue/KeyValueStorage';
|
||||
import { Initializer } from './Initializer';
|
||||
|
||||
/**
|
||||
* Stores the `baseUrl` value that was used to start the server
|
||||
* and warns the user in case it differs from the previous one.
|
||||
*/
|
||||
export class BaseUrlVerifier extends Initializer {
|
||||
private readonly baseUrl: string;
|
||||
private readonly storageKey: string;
|
||||
private readonly storage: KeyValueStorage<string, string>;
|
||||
|
||||
private readonly logger = getLoggerFor(this);
|
||||
|
||||
public constructor(baseUrl: string, storageKey: string, storage: KeyValueStorage<string, string>) {
|
||||
super();
|
||||
this.baseUrl = baseUrl;
|
||||
this.storageKey = storageKey;
|
||||
this.storage = storage;
|
||||
}
|
||||
|
||||
public async handle(): Promise<void> {
|
||||
const previousValue = await this.storage.get(this.storageKey);
|
||||
if (previousValue && this.baseUrl !== previousValue) {
|
||||
this.logger.warn(`The server is being started with a base URL of ${this.baseUrl
|
||||
} while it was previously started with ${previousValue
|
||||
}. Resources generated with the previous server instance, such as a WebID, might no longer work correctly.`);
|
||||
}
|
||||
await this.storage.set(this.storageKey, this.baseUrl);
|
||||
}
|
||||
}
|
38
test/unit/init/BaseUrlVerifier.test.ts
Normal file
38
test/unit/init/BaseUrlVerifier.test.ts
Normal file
@ -0,0 +1,38 @@
|
||||
import { BaseUrlVerifier } from '../../../src/init/BaseUrlVerifier';
|
||||
import type { Logger } from '../../../src/logging/Logger';
|
||||
import { getLoggerFor } from '../../../src/logging/LogUtil';
|
||||
import type { KeyValueStorage } from '../../../src/storage/keyvalue/KeyValueStorage';
|
||||
|
||||
jest.mock('../../../src/logging/LogUtil', (): any => {
|
||||
const logger: Logger = { warn: jest.fn() } as any;
|
||||
return { getLoggerFor: (): Logger => logger };
|
||||
});
|
||||
|
||||
describe('A BaseUrlVerifier', (): void => {
|
||||
const logger: jest.Mocked<Logger> = getLoggerFor(BaseUrlVerifier) as any;
|
||||
const baseUrl1 = 'http://base1.example.com/';
|
||||
const baseUrl2 = 'http://base2.example.com/';
|
||||
const storageKey = 'uniqueKey';
|
||||
let storage: KeyValueStorage<string, string>;
|
||||
|
||||
beforeEach(async(): Promise<void> => {
|
||||
storage = new Map<string, string>() as any;
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('stores the value if no value was stored yet.', async(): Promise<void> => {
|
||||
const initializer = new BaseUrlVerifier(baseUrl1, storageKey, storage);
|
||||
await expect(initializer.handle()).resolves.toBeUndefined();
|
||||
expect(logger.warn).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
|
||||
it('logs a warning in case the value changes.', async(): Promise<void> => {
|
||||
let initializer = new BaseUrlVerifier(baseUrl1, storageKey, storage);
|
||||
await expect(initializer.handle()).resolves.toBeUndefined();
|
||||
expect(logger.warn).toHaveBeenCalledTimes(0);
|
||||
|
||||
initializer = new BaseUrlVerifier(baseUrl2, storageKey, storage);
|
||||
await expect(initializer.handle()).resolves.toBeUndefined();
|
||||
expect(logger.warn).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user