diff --git a/packages/common/src/utils/request.util.ts b/packages/common/src/utils/request.util.ts deleted file mode 100644 index e894db3e5..000000000 --- a/packages/common/src/utils/request.util.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { createHash } from 'crypto'; -import {Request} from "../models/request"; -import { URL } from 'url'; - -function sort(obj: any) { - const ret: any = {}; - - Object.keys(obj).sort().forEach(function (key) { - ret[key] = obj[key]; - }); - - return ret; -} - -export class RequestUtil { - static hash(request: Request): string | null { - const hash = createHash("md5"); - - const parsedUrl = new URL(request.url); - - parsedUrl.searchParams.sort(); - - hash.update(parsedUrl.pathname); - hash.update(request.httpMethod); - hash.update(parsedUrl.searchParams.toString()); - hash.update(parsedUrl.hash); - hash.update(JSON.stringify(sort(request.headers))); - - try { - hash.write(JSON.stringify(request.body)); - } catch (e) { - return null; - } - - - return hash.digest("hex"); - } -} \ No newline at end of file diff --git a/packages/common/src/utils/utils.ts b/packages/common/src/utils/utils.ts index 8b73a190b..dd1e502ce 100644 --- a/packages/common/src/utils/utils.ts +++ b/packages/common/src/utils/utils.ts @@ -1,3 +1,2 @@ export * from "./date.util"; export * from "./metadata.util"; -export * from "./request.util"; \ No newline at end of file diff --git a/packages/common/src/utils/request.util.spec.ts b/packages/networking/src/cache/cached.router-node.spec.ts similarity index 82% rename from packages/common/src/utils/request.util.spec.ts rename to packages/networking/src/cache/cached.router-node.spec.ts index 5af2704c2..4c6ce8aa1 100644 --- a/packages/common/src/utils/request.util.spec.ts +++ b/packages/networking/src/cache/cached.router-node.spec.ts @@ -1,6 +1,5 @@ -import {Request} from "../models/request"; -import {RequestUtil} from "./request.util"; -import {HttpMethod} from "../enums/http-method.enum"; +import {HttpMethod, Request} from "@pristine-ts/common"; +import {CachedRouterRoute} from "./cached.router-route"; describe("Request Util", () => { it("should hash the same request twice", () => { @@ -22,8 +21,8 @@ describe("Request Util", () => { "header1": "value1", }); - const request1Hash = RequestUtil.hash(request); - const request2Hash = RequestUtil.hash(request2); + const request1Hash = CachedRouterRoute.hashRequest(request); + const request2Hash = CachedRouterRoute.hashRequest(request2); expect(request1Hash).toBe(request2Hash); }) @@ -55,12 +54,12 @@ describe("Request Util", () => { "header1": "value1", }); - const request1Hash = RequestUtil.hash(request); - const request2Hash = RequestUtil.hash(request2); - const request3Hash = RequestUtil.hash(request3); + const request1Hash = CachedRouterRoute.hashRequest(request); + const request2Hash = CachedRouterRoute.hashRequest(request2); + const request3Hash = CachedRouterRoute.hashRequest(request3); expect(request1Hash).not.toBe(request2Hash); expect(request1Hash).not.toBe(request3Hash); expect(request2Hash).not.toBe(request3Hash); }) -}) \ No newline at end of file +}) diff --git a/packages/networking/src/cache/cached.router-route.ts b/packages/networking/src/cache/cached.router-route.ts index 361a6ce25..f2449c61a 100644 --- a/packages/networking/src/cache/cached.router-route.ts +++ b/packages/networking/src/cache/cached.router-route.ts @@ -1,5 +1,7 @@ import {MethodRouterNode} from "../nodes/method-router.node"; -import {Request, RequestUtil} from "@pristine-ts/common"; +import {Request} from "@pristine-ts/common"; +import {createHash} from "crypto"; +import {URL} from "url"; export class CachedRouterRoute { private cachedControllerMethodArguments: { [requestHash: string]: any[] } = {}; @@ -9,13 +11,42 @@ export class CachedRouterRoute { ) { } - private hashRequest(request: Request): string | null { - return RequestUtil.hash(request); + public static hashRequest(request: Request): string | null { + const sort = (obj: any) => { + const ret: any = {}; + + Object.keys(obj).sort().forEach(function (key) { + ret[key] = obj[key]; + }); + + return ret; + }; + + const hash = createHash("md5"); + + const parsedUrl = new URL(request.url); + + parsedUrl.searchParams.sort(); + + hash.update(parsedUrl.pathname); + hash.update(request.httpMethod); + hash.update(parsedUrl.searchParams.toString()); + hash.update(parsedUrl.hash); + hash.update(JSON.stringify(sort(request.headers))); + + try { + hash.write(JSON.stringify(request.body)); + } catch (e) { + return null; + } + + + return hash.digest("hex"); } getCachedControllerMethodArguments(request: Request): any[] | undefined { // Hashed the request - const hash = this.hashRequest(request); + const hash = CachedRouterRoute.hashRequest(request); if(hash === null) { return; @@ -27,7 +58,7 @@ export class CachedRouterRoute { cacheControllerMethodArguments(request: Request, methodArguments: any[]): void { // Hashed the request - const hash = this.hashRequest(request); + const hash = CachedRouterRoute.hashRequest(request); if(hash === null) { return; @@ -36,4 +67,4 @@ export class CachedRouterRoute { // Save the method arguments this.cachedControllerMethodArguments[hash] = methodArguments; } -} \ No newline at end of file +}