Skip to content

Commit

Permalink
Merge pull request #695 from magieno/move-request-hash-to-networking-…
Browse files Browse the repository at this point in the history
…to-get-rid-of-crypto-dependencies

- Update.
  • Loading branch information
etiennenoel authored May 17, 2024
2 parents 1a446e1 + f106e75 commit e732c43
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 54 deletions.
38 changes: 0 additions & 38 deletions packages/common/src/utils/request.util.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/common/src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from "./date.util";
export * from "./metadata.util";
export * from "./request.util";
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand All @@ -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);
})
Expand Down Expand Up @@ -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);
})
})
})
43 changes: 37 additions & 6 deletions packages/networking/src/cache/cached.router-route.ts
Original file line number Diff line number Diff line change
@@ -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[] } = {};
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -36,4 +67,4 @@ export class CachedRouterRoute {
// Save the method arguments
this.cachedControllerMethodArguments[hash] = methodArguments;
}
}
}

0 comments on commit e732c43

Please sign in to comment.