Skip to content

Commit

Permalink
Prepared deep merge improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Aug 22, 2022
1 parent abbb91a commit 936308d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Removed support for Node.js below 12
- Added support for asynchronous creators (#54)
- Added optional `direction` parameter to middlewares (#53)
- Added a deep merge of injector settings (#52)
- Updated dependencies

## 0.14.3
Expand Down
35 changes: 20 additions & 15 deletions src/server/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,25 @@ export function readConfiguration(path: string): ConfigurationFile {
return {};
}

function deepMerge(obj: any, value: any) {
Object.keys(value).forEach((key) => {
const oldItem = obj[key];
const newItem = value[key];

if (newItem === undefined) {
delete obj[key];
} else if (Array.isArray(oldItem) && Array.isArray(newItem)) {
obj[key] = [...oldItem, ...newItem] as any;
} else if (typeof oldItem === 'object') {
obj[key] = deepMerge({ ...oldItem }, newItem);
} else {
obj[key] = newItem;
}
});

return obj;
}

function mergeObjects<T>(
sources: Array<Partial<KrasConfiguration>>,
select: (config: Partial<KrasConfiguration>) => Dict<T>,
Expand All @@ -99,21 +118,7 @@ function mergeObjects<T>(
const value = select(source);

if (value && typeof value === 'object') {
Object.keys(value).forEach((key) => {
const oldItem = obj[key];
const newItem = value[key];

if (newItem === undefined) {
delete obj[key];
} else if (typeof oldItem === 'object') {
obj[key] = {
...oldItem,
...newItem,
};
} else {
obj[key] = newItem;
}
});
deepMerge(obj, value);
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/server/core/webserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,11 @@ export class WebServer extends EventEmitter implements BaseKrasServer {
return api;
}

start() {
async setup() {}

async start() {
await this.setup();

this.app.all('*', (req: Request, res: Response) => {
if (req.method !== 'OPTIONS') {
const hook = findHook(this.hooks, req);
Expand All @@ -239,7 +243,7 @@ export class WebServer extends EventEmitter implements BaseKrasServer {
return corsHandler(req, res);
});

return new Promise<void>((resolve) => {
return await new Promise<void>((resolve) => {
this.server.listen(this.port, () => {
this.emit('open', {
port: this.port,
Expand Down
33 changes: 17 additions & 16 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class MockServer extends MockServerCore implements KrasServer {
readonly logs: Array<LogEntry> = [];
readonly logLevel: LogLevel;

constructor(config: KrasConfiguration) {
constructor(private config: KrasConfiguration) {
super(config);

this.logLevel = config.logLevel || 'error';
Expand All @@ -66,15 +66,18 @@ export class MockServer extends MockServerCore implements KrasServer {
}
}

async setup(config: KrasConfiguration) {
async setup() {
const config = this.config;
await super.setup();
await withManagement(this, config);
await withInjectors(this, config);
await withMiddlewares(this, config);
await withFiles(this, config);
}

stop() {
return super.stop().then(() => this.injectors.forEach(disposeInjector));
async stop() {
await super.stop();
this.injectors.forEach(disposeInjector);
}

private log(type: LogEntryType, data: any) {
Expand All @@ -98,30 +101,28 @@ export function readKrasConfig(options?: ConfigurationOptions, ...files: Array<s
return mergeConfiguration(options, ...configurations);
}

export async function buildKras(config?: Partial<KrasConfiguration>) {
export function buildKras(config?: Partial<KrasConfiguration>) {
const options = buildConfiguration(config);
const server = new MockServer(options);
await server.setup(options);
return server;
return new MockServer(options);
}

export async function buildKrasWithCli(config: KrasConfiguration) {
const server = await buildKras(config);
export function buildKrasWithCli(config: KrasConfiguration) {
const server = buildKras(config);
connectToCli(server, config.api !== false);
return server;
}

export async function runKras(config?: Partial<KrasConfiguration>) {
const server = await buildKras(config);
server.start();
const server = buildKras(config);
await server.start();
return server;
}

export type KrasRuntimeConfiguration = Partial<KrasConfiguration> & KrasHandlerConfiguration;

export function withKras(config?: KrasRuntimeConfiguration) {
return async (callback: KrasRunner) => {
const server = await buildKras(config);
return (callback: KrasRunner) => {
const server = buildKras(config);
configureHandler(server, config);
return runWith(server, callback);
};
Expand Down Expand Up @@ -205,8 +206,8 @@ export function connectToCli(server: MockServer, canManage = true) {

export async function runFromCli(options: ConfigurationOptions, rcfile: string) {
const config = readKrasConfig(options, resolve(homedir(), krasrc), resolve(currentDir, krasrc), rcfile);
const server = await buildKrasWithCli(config);
const server = buildKrasWithCli(config);
console.log(`Starting kras v${version} ...`);
server.start();
await server.start();
return server;
}

0 comments on commit 936308d

Please sign in to comment.