Skip to content

Commit

Permalink
Actually use helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
hansott committed Feb 28, 2024
1 parent caa6d9b commit 6112ce9
Showing 1 changed file with 4 additions and 42 deletions.
46 changes: 4 additions & 42 deletions library/src/agent/Agent.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* eslint-disable max-lines-per-function */
import { hostname, platform, release } from "node:os";
import { convertRequestBodyToString } from "../helpers/convertRequestBodyToString";
import { ip } from "../helpers/ipAddress";
import { isPlainObject } from "../helpers/isPlainObject";
import { normalizeRequestHeaders } from "../helpers/normalizeRequestHeaders";
import { API } from "./api/API";
import { AgentInfo, Kind, Stats } from "./api/Event";
import { Token } from "./api/Token";
Expand Down Expand Up @@ -92,46 +94,6 @@ export class Agent {
}
}

private convertBodyToString(
body: unknown,
maxLength = 16384
): string | undefined {
if (typeof body === "string") {
return body.length > maxLength ? body.slice(0, maxLength) : body;
}

if (isPlainObject(body)) {
try {
const serialized = JSON.stringify(body, null, 2);

return this.convertBodyToString(serialized, maxLength);
} catch {
return undefined;
}
}

return undefined;
}

private normalizeHeaders(
headers: Context["headers"]
): Record<string, string | string[]> {
const normalized: Record<string, string | string[]> = {};
for (const key in headers) {
const value = headers[key];

if (Array.isArray(value) && value.length > 0) {
normalized[key] = value;
}

if (typeof value === "string" && value.length > 0) {
normalized[key] = value;
}
}

return normalized;
}

/**
* This function gets called when an attack is detected, it reports this attack to the API
*/
Expand Down Expand Up @@ -176,8 +138,8 @@ export class Agent {
typeof request.headers["user-agent"] === "string"
? request.headers["user-agent"]
: undefined,
body: this.convertBodyToString(request.body),
headers: this.normalizeHeaders(request.headers),
body: convertRequestBodyToString(request.body),
headers: normalizeRequestHeaders(request.headers),
},
agent: this.getAgentInfo(),
})
Expand Down

0 comments on commit 6112ce9

Please sign in to comment.