Skip to content

Commit

Permalink
Reuse deep merge method from shared options class in handler config c…
Browse files Browse the repository at this point in the history
…lass
  • Loading branch information
t-ski committed Sep 19, 2024
1 parent 1bf1059 commit ed1280e
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 227 deletions.
21 changes: 15 additions & 6 deletions packages/rjs-handler/src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { existsSync, readFileSync } from "fs";
import { resolve } from "path";

import { TJSON } from "./.shared/global.types";
import { Options } from "./.shared/Options";

import _config from "./_config.json";

Expand All @@ -15,23 +16,31 @@ export class Config {

continue;
}
sourceObj[key] = Config.deepMergeObjects(
targetObj[key] as TJSON, sourceObj[key] as TJSON
);
}
return { ...targetObj, ...sourceObj };
}

private readonly obj: TJSON;

constructor(path: string, name?: string, forDev: boolean = false, defaultsObj: TJSON = {}) {
this.obj = defaultsObj;
this.obj = Config.deepMergeObjects(this.obj, this.parseFile(path, name));
this.obj = Config.deepMergeObjects(this.obj, this.parseFile(path, name, forDev ? _config.configNameInfixDev : _config.configNameInfixProd));
this.obj = new Options(
this.parseFile(path, name),
this.obj
).object;
this.obj = new Options(
this.parseFile(
path, name, forDev ? _config.configNameInfixDev : _config.configNameInfixProd
),
this.obj
).object;
}

private parseFile(path: string, name: string, modeInfix?: string): TJSON {
const configFilePath: string = resolve(
path,
Expand Down
6 changes: 6 additions & 0 deletions packages/rjs-handler/test/unit/_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@ module.exports.request = async (sReq, headerFilters = null, hideBody = false, me
sRes.headers = filteredHeaders;
}
}

if(hideBody) {
delete sRes.body;
} else if(metaBody) {
sRes.body = {
length: sRes.body ? Buffer.byteLength(sRes.body) : 0
};
} else {
try {
sRes.body = sRes.body.toString();
sRes.body = JSON.parse(sRes.body);
} catch {}
}

return sRes;
Expand Down
4 changes: 2 additions & 2 deletions packages/rjs-handler/test/unit/get.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ new UnitTest("GET /")
"Server": "rapidJS",
"Content-Length": 12
},
body: Buffer.from("TEST (index)")
body: "TEST (index)"
});

new UnitTest("GET /compress.txt")
Expand Down Expand Up @@ -60,5 +60,5 @@ new UnitTest("GET /out.txt (built)")
}, []))
.expect({
status: 200,
body: Buffer.from("O\nP1\nP2")
body: "O\nP1\nP2"
});
16 changes: 8 additions & 8 deletions packages/rjs-handler/test/unit/post.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ new UnitTest("POST /route1")
headers: {
"Content-Type": "application/json"
},
body: Buffer.from(JSON.stringify({
body: {
data: 3
}))
}
});

new UnitTest("POST /route1")
Expand All @@ -29,9 +29,9 @@ new UnitTest("POST /route1")
}, []))
.expect({
status: 200,
body: Buffer.from(JSON.stringify({
data: "value"
}))
body: {
data: "value1"
}
});

new UnitTest("POST /route2")
Expand All @@ -44,9 +44,9 @@ new UnitTest("POST /route2")
}, []))
.expect({
status: 200,
body: Buffer.from(JSON.stringify({
data: "value"
}))
body: {
data: "value2"
}
});

new UnitTest("POST /route2 (no body)")
Expand Down
4 changes: 3 additions & 1 deletion packages/rjs-server/src/Instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { Logger } from "./Logger";
import { Cluster } from "@rapidjs.org/server-cluster";
import { IHandlerOptions } from "@rapidjs.org/rjs-handler";

import _config from "./_config.json";


export function createInstance(options?: Partial<IHandlerOptions>, clusterSize?: IClusterSize): Promise<Instance> {
return new Promise((resolve) => {
Expand All @@ -25,7 +27,7 @@ export class Instance extends Cluster {
baseSize: options.dev ? 1 : (clusterSize ?? {}).threads
});

const logger: Logger = new Logger(options.cwd);
const logger: Logger = new Logger(join(options.cwd, _config.logsDirName));

this.on("stdout", (message: string) => logger.info(message));
this.on("stderr", (message: string) => logger.error(message));
Expand Down
2 changes: 1 addition & 1 deletion packages/rjs-server/src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class Logger {

constructor(logsDirPath: string|null = null, silent: boolean = false) {
this.silent = silent;

if(!logsDirPath) return;

this.logsDirPath = resolve(logsDirPath);
Expand Down
3 changes: 3 additions & 0 deletions packages/rjs-server/src/_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"logsDirName": ".logs",
}
2 changes: 1 addition & 1 deletion packages/server-cluster/test/unit/cluster.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ new UnitTest("Cluster setup")

const tids = new Set();
for(let i = 0; i < ROUNDTRIP_AMOUNT; i++) {
new UnitTest(`Thread roundtrip (${i})`)
new UnitTest(`Cluster roundtrip (${i})`)
.actual(async () => {
const sRes = await cluster.handleRequest({
sReq: `test:request:${i}`
Expand Down
4 changes: 2 additions & 2 deletions packages/shared/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export class Options<T> {
}
return targetObj;
};

private obj: T;

private obj: T;

constructor(obj: Partial<T>, defaultsObj: T) {
this.obj = Options.deepMerge(defaultsObj, obj) as T;
}
Expand Down
50 changes: 0 additions & 50 deletions test-app/24-09-16

This file was deleted.

156 changes: 0 additions & 156 deletions test-app/24-09-17

This file was deleted.

0 comments on commit ed1280e

Please sign in to comment.