Skip to content

Commit 60d71ed

Browse files
added based middleware for catch exceptions, http transport logic, helpers
1 parent baaae7c commit 60d71ed

19 files changed

Lines changed: 466 additions & 59 deletions

File tree

packages/commons/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
},
1111
"scripts": {
1212
"build": "tsc -p tsconfig.json",
13+
"lint": "run-s lint:prettier",
14+
"lint:prettier": "prettier */**/*.{js,ts,tsx} --write",
1315
"test": "jest",
1416
"test:watch": "jest --watch --notify"
1517
},

packages/commons/src/http.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import * as http from "http";
2+
3+
type RequestType = "" | "" | "";
4+
5+
type HttpMethodType = "GET" | "POST" | "PATCH" | "PUT" | "DELETE";
6+
7+
const KLEPPER_API = process.env.KLEPPER_API;
8+
const KLEPPER_PORT = process.env.KLEPPER_PORT;
9+
10+
export interface RequestOptions extends http.RequestOptions {
11+
hostname: string;
12+
port: number;
13+
method: HttpMethodType;
14+
path?: string;
15+
headers?: { [key: string]: string };
16+
}
17+
18+
export interface RequestPayload {
19+
data: string;
20+
type: RequestType;
21+
url: string;
22+
}
23+
24+
export const getRequestPath = (path: string) =>
25+
`http://api.klepper.io/api/${path}`;
26+
27+
export const getRequestHeaders = () => {};
28+
29+
export const createHttpOptions = (payload: RequestPayload): RequestOptions => {
30+
const { url } = payload;
31+
const baseOptions: RequestOptions = {
32+
hostname: KLEPPER_API!,
33+
method: "POST",
34+
port: +KLEPPER_PORT!,
35+
};
36+
37+
return {
38+
path: getRequestPath(url),
39+
...baseOptions,
40+
};
41+
};
42+
43+
/**
44+
* Send data to Klepper server
45+
*
46+
* @param payload
47+
*/
48+
export const sendHttpRequest = (payload: RequestPayload) => {
49+
const { data } = payload;
50+
const options = createHttpOptions(payload);
51+
const request = http.request(options, (res: http.IncomingMessage) => {
52+
res.setEncoding("utf8");
53+
54+
res.on("data", () => {});
55+
res.on("end", () => {});
56+
});
57+
request.end(data);
58+
};

packages/commons/tslint.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../../tslint.json"
3+
}

packages/config/tslint.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"extends": ["tslint:all", "tslint-config-prettier"],
3+
"rules": {
4+
"no-unused-variable": false,
5+
"no-submodule-imports": false,
6+
"newline-before-return": false,
7+
"no-any": false,
8+
"no-magic-numbers": false,
9+
"no-parameter-properties": false,
10+
"no-require-imports": false,
11+
"prefer-function-over-method": [false],
12+
"strict-boolean-expressions": false,
13+
"no-inferrable-types": false,
14+
"no-console": [true, "log"]
15+
}
16+
}
17+

packages/node/package.json

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
44
"description": "",
55
"author": "",
66
"license": "MIT",
7-
"engines": {
8-
"node": ">=6"
9-
},
107
"main": "dist/index.js",
118
"types": "dist/index.d.ts",
129
"dependencies": {
10+
"@klepper/commons": "0.0.1",
1311
"@klepper/transport": "0.0.1",
14-
"@klepper/commons": "0.0.1"
12+
"@types/stack-trace": "^0.0.29",
13+
"stack-trace": "0.0.10"
1514
},
1615
"devDependencies": {
17-
"jest": "^22.4.3",
16+
"jest": "^26.5.5",
1817
"npm-run-all": "^4.1.2",
1918
"prettier": "^1.14.0",
2019
"prettier-check": "^2.0.0",
@@ -24,7 +23,23 @@
2423
},
2524
"scripts": {
2625
"build": "tsc -p tsconfig.json",
26+
"lint": "run-s lint:prettier",
27+
"lint:prettier": "prettier */**/*.{js,ts,tsx} --write",
2728
"test": "jest",
2829
"test:watch": "jest --watch --notify"
30+
},
31+
"jest": {
32+
"verbose": false,
33+
"testURL": "http://localhost/",
34+
"transform": {
35+
"^.+\\.ts$": "ts-jest"
36+
},
37+
"moduleFileExtensions": [
38+
"js",
39+
"ts"
40+
],
41+
"testMatch": [
42+
"**/*.test.ts"
43+
]
2944
}
30-
}
45+
}

packages/node/src/handlers.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

packages/node/src/helpers.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { BaseObject, KlepperIncomingMessage, KlepperRequest, KlepperResponse, KlepperStackFrame } from "@klepper/transport";
2+
import { StackFrame } from "stack-trace";
3+
4+
const mapStackFrames = (stackFrames: StackFrame[], onlyInternal: boolean = true) => {
5+
const frames: KlepperStackFrame[] = [];
6+
7+
const parse = (stackFrame: StackFrame): KlepperStackFrame => {
8+
return {
9+
functionName: stackFrame.getFunctionName(),
10+
lineNumber: stackFrame.getLineNumber(),
11+
columnNumber: stackFrame.getColumnNumber(),
12+
fileName: stackFrame.getFileName(),
13+
methodName: stackFrame.getMethodName(),
14+
}
15+
}
16+
17+
stackFrames.map((f) => {
18+
if (onlyInternal) {
19+
const fileName = f.getFileName();
20+
const isInternal = fileName && !fileName.includes('node_modules') && !fileName.startsWith('/') && !fileName.startsWith('node:') && fileName.includes(":\\");
21+
22+
isInternal ? frames.push(parse(f)) : null;
23+
} else {
24+
frames.push(parse(f));
25+
}
26+
});
27+
28+
return frames;
29+
}
30+
31+
const createPayloadFromRequest = (req: KlepperRequest, res: KlepperResponse) => {
32+
return {
33+
request: req,
34+
response: res,
35+
date: new Date().getDate()
36+
}
37+
}
38+
39+
const mapRequestData = (req: BaseObject): KlepperRequest => {
40+
const headersData = req.headers || req.header || {};
41+
42+
const method = req.method;
43+
const host = headersData["host"] || '<no host>';
44+
45+
const protocol = getProtocol(req.protocol)
46+
47+
const originalUrl = (req.originalUrl || req.url) as string;
48+
const absoluteUrl = `${protocol}://${host}${originalUrl}`;
49+
const origin = headersData["origin"];
50+
const query = req.query;
51+
const payload = req.body || {};
52+
const ip = getIp(req as KlepperIncomingMessage);
53+
54+
const connections = {
55+
absoluteUrl,
56+
origin,
57+
protocol
58+
};
59+
60+
const headers = {
61+
host,
62+
connection: headersData["connection"],
63+
origin: headersData["origin"]
64+
}
65+
66+
const request = {
67+
payload,
68+
headers,
69+
method,
70+
query,
71+
ip,
72+
url: connections,
73+
};
74+
75+
return request;
76+
}
77+
78+
const getIp = (req: KlepperIncomingMessage): string | string[] | undefined => {
79+
return req.headers['x-forwarded-for'] || req.socket.remoteAddress;
80+
}
81+
82+
const isLocalhost = (req: KlepperIncomingMessage): boolean => {
83+
const ip = getIp(req);
84+
return ip === "::1" || ip === "127.0.0.1" ? true : false;
85+
}
86+
87+
const getProtocol = (req: KlepperIncomingMessage): string => {
88+
return req.protocol === 'https' || req.secure
89+
? 'https'
90+
: 'http';
91+
}
92+
93+
94+
export const helpers = {
95+
getIp,
96+
createPayloadFromRequest,
97+
mapRequestData,
98+
mapStackFrames,
99+
getProtocol,
100+
isLocalhost
101+
}

packages/node/src/index.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
1-
import { Klepper } from "./klepper";
2-
3-
const klepper = Klepper.connect({ apiKey: "=== API KEY 1111 ===", secretKey: "=== SECRET KEY 2222 ===" });
4-
klepper.errorHandler;
5-
klepper.requestHandler;
1+
import * as Middleware from "./middlewares/index";
2+
import * as Klepper from "./main";
3+
4+
export { Middleware, Klepper }
5+
6+
7+
/**
8+
* TODO:
9+
*
10+
* 1. captureException function which is placed in catch clause in code when we want to capture an error
11+
* 2. communication between library and server, send exceptions to server
12+
* 3. ENDING OF ERROR HANDLER, CREATE EXCEPTION OBJECT
13+
*
14+
*/
15+
16+
/**
17+
* IDEAS:
18+
*
19+
* 1. add parameters of exceptions which client want to handle
20+
* 2.
21+
*/
22+
23+
/**
24+
* PROBLEMS:
25+
*
26+
* 1. do we need to capture also requests and not only exceptions?
27+
* 2. do we need to parse stack objects from exceptions and then send to server or we can send whole exception stack, name and description?
28+
*/

packages/node/src/klepper.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

packages/node/src/main.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { KlepperOptions } from "@klepper/transport";
2+
3+
export const connect = (_options?: KlepperOptions): void => {
4+
//TO IMPLEMENT!
5+
}

0 commit comments

Comments
 (0)