Skip to content

Commit 8bf0d46

Browse files
committed
full information in CHANGELOG.md - 0.0.3-alpha
1 parent fdd3062 commit 8bf0d46

21 files changed

Lines changed: 376 additions & 350 deletions

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
22

3+
## 0.0.3-alpha - 2022-03-23
4+
- Split transport file into few small files
5+
- removing try/catch from various functions
6+
- added options in parameters to `catchException` function
7+
- fixes for middleware and process of handling exceptions
8+
- fix for build script
39
## 0.0.2-alpha - 2022-03-23
410
- Migration from monorepo in lerna to single monolith
511
- Bugfixes

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"name": "klepper",
3-
"version": "0.0.25-alpha",
3+
"version": "0.0.41-alpha",
44
"license": "MIT",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
77
"files": [
88
"dist"
99
],
1010
"scripts": {
11-
"build": "yarn prebuild && tsc -p ./tsconfig.json --outDir dist",
11+
"build": "tsc -p ./tsconfig.json --outDir dist",
1212
"prebuild": "rimraf ./dist",
1313
"lint": "run-s lint:prettier",
1414
"lint:prettier": "prettier */**/*.{js,ts} --write",

src/core/global.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
import { KlepperGlobal } from "../transport";
1+
import { KlepperGlobal } from "../transport/base";
22

3-
/**
4-
* Using internal NodeJS global object to persist information
5-
*
6-
* @returns {KlepperGlobal}
7-
*/
83
export const getGlobalClientData = (): KlepperGlobal =>
94
global.__KLEPPER__ || {};
105

@@ -14,7 +9,6 @@ export const setGlobalClientData = (data: KlepperGlobal): void => {
149

1510
export const clearGlobalClientData = (): void => {
1611
setGlobalClientData({
17-
environment: undefined,
1812
privateKey: undefined,
1913
});
2014
};

src/core/http.ts

Lines changed: 53 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
1-
import {
2-
RequestPayload,
3-
RequestOptions,
4-
KlepperResponse,
5-
KlepperIncomingMessage,
6-
RequestStatus,
7-
} from "../transport";
81
import * as http from "http";
2+
import { RequestStatus } from "../transport/enums";
3+
import { EventResponse, KlepperEvent } from "../transport/events";
4+
import { KlepperIncomingMessage, RequestOptions } from "../transport/http";
95
import { getGlobalClientData } from "./global";
6+
import { isClientConnected } from "./is";
107

118
const KLEPPER_HOST = process.env.KLEPPER_HOST || "localhost";
129
const KLEPPER_API = process.env.KLEPPER_API || "/test";
1310
const KLEPPER_PORT = process.env.KLEPPER_PORT || 3000;
1411

15-
const createHttpOptions = (payload: RequestPayload): http.RequestOptions => {
12+
const createHttpOptions = (event: KlepperEvent): http.RequestOptions => {
1613
const client = getGlobalClientData();
1714

1815
const { privateKey } = client;
19-
const { data } = payload;
2016
const baseOptions: RequestOptions = {
2117
hostname: KLEPPER_HOST,
2218
port: +KLEPPER_PORT,
2319
method: "POST",
2420
headers: {
2521
"Content-Type": "application/json",
26-
"Content-Length": `${Buffer.byteLength(JSON.stringify(data))}`,
22+
"Content-Length": `${Buffer.byteLength(JSON.stringify(event))}`,
2723
"Klepper-Project-Key": privateKey as string,
2824
},
2925
};
@@ -37,60 +33,60 @@ const createHttpOptions = (payload: RequestPayload): http.RequestOptions => {
3733
const statusFromCode = (code: number) =>
3834
code >= 200 && code <= 299 ? RequestStatus.SUCCESS : RequestStatus.ERROR;
3935

40-
/**
41-
* Send data to Klepper server
42-
*
43-
* @param payload
44-
*/
4536
export const sendEvent = async (
46-
payload: RequestPayload
47-
): Promise<KlepperResponse> => {
48-
const { data } = payload;
49-
50-
try {
51-
return new Promise<KlepperResponse>((resolve, reject) => {
52-
const httpOptions = createHttpOptions(payload);
53-
const client = getGlobalClientData();
54-
55-
if (!httpOptions) {
56-
reject(new Error("NO HTTP OPTIONS"));
57-
}
37+
event: KlepperEvent
38+
): Promise<EventResponse> => {
39+
return new Promise<EventResponse>((resolve, reject) => {
40+
const httpOptions = createHttpOptions(event);
41+
if (!httpOptions) {
42+
reject({
43+
statusCode: 400,
44+
statusMessage:
45+
"[Klepper] Error during sending event to Klepper. No HTTP options.",
46+
});
47+
}
5848

59-
if (!client) {
60-
reject(new Error("NO CLIENT DATA IN GLOBAL OBJECT"));
61-
}
49+
if (!isClientConnected()) {
50+
reject({
51+
statusCode: 400,
52+
statusMessage:
53+
"[Klepper] Error during sending event to Klepper. No client global data in NodeJS scope.",
54+
});
55+
}
6256

63-
const request = http.request(
64-
httpOptions,
65-
(res: KlepperIncomingMessage) => {
66-
res.setEncoding("utf8");
57+
const request = http.request(httpOptions, (res: KlepperIncomingMessage) => {
58+
res.setEncoding("utf8");
6759

68-
const status = statusFromCode(res?.statusCode as number);
69-
const isSuccess = status === RequestStatus.SUCCESS;
60+
const status = statusFromCode(res?.statusCode as number);
61+
const isSuccess = status === RequestStatus.SUCCESS;
7062

71-
if (!isSuccess) {
72-
reject(new Error("HTTP ERROR: ${res.statusCode}"));
73-
} else {
74-
resolve({
75-
statusCode: res?.statusCode as number,
76-
statusMessage: "Event successfully sended to Klepper",
77-
});
78-
}
63+
if (!isSuccess) {
64+
reject({
65+
statusCode: res?.statusCode as number,
66+
statusMessage: "[KLEPPER] Error during sending event to Klepper.",
67+
});
68+
} else {
69+
resolve({
70+
statusCode: res?.statusCode as number,
71+
statusMessage: "[KLEPPER] Event successfully sended to Klepper.",
72+
});
73+
}
7974

80-
res.on("error", reject);
81-
}
82-
);
83-
request.on("error", reject);
75+
res.on("error", reject);
76+
});
77+
78+
request.on("error", reject);
8479

85-
request.on("timeout", () => {
86-
request.destroy();
87-
reject(new Error("TIMEOUT"));
80+
request.on("timeout", () => {
81+
request.destroy();
82+
reject({
83+
statusCode: 400,
84+
statusMessage:
85+
"[Klepper] Error during sending event to Klepper. Connection timeout.",
8886
});
89-
90-
request.write(JSON.stringify(data));
91-
request.end();
9287
});
93-
} catch (error) {
94-
throw new Error(""); //to properly handling
95-
}
88+
89+
request.write(JSON.stringify(event));
90+
request.end();
91+
});
9692
};

src/core/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as http from "./http";
21
import * as global from "./global";
32

4-
export { http, global };
3+
export { global };

src/core/is.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { getGlobalClientData } from "./global";
2+
3+
export const isInternal = (fileName: string): boolean =>
4+
!!fileName &&
5+
!fileName.includes("node_modules") &&
6+
!fileName.startsWith("/") &&
7+
!fileName.startsWith("node:") &&
8+
fileName.includes(":\\");
9+
10+
export const isEmpty = (obj?: any): boolean => Object.keys(obj).length === 0;
11+
12+
export const isLocalhost = (ip: string): boolean => {
13+
return ip === "::1" || ip === "127.0.0.1" ? true : false;
14+
};
15+
16+
export const isClientConnected = (): boolean => !isEmpty(getGlobalClientData());

src/core/types/global.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { KlepperGlobal } from "../../transport";
1+
import { KlepperGlobal } from "../../transport/base";
22

33
declare global {
44
var __KLEPPER__: KlepperGlobal;

src/index.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
import * as Middleware from "./node/middlewares";
1+
import { Middleware, catchException } from "./node/middlewares";
22
import * as Klepper from "./node/sdk";
3-
import { catchException } from "./node/middlewares/error";
3+
import { ExceptionPriority } from "./transport/enums";
4+
import { CatchExceptionsOptions } from "./transport/options";
45

5-
export { Middleware, Klepper, catchException };
6+
export {
7+
Middleware,
8+
Klepper,
9+
catchException,
10+
CatchExceptionsOptions,
11+
ExceptionPriority,
12+
};

src/node/helpers.ts

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,7 @@
1-
import {
2-
BaseObject,
3-
KlepperIncomingMessage,
4-
KlepperRequest,
5-
} from "../transport";
6-
// import { StackFrame } from "stack-trace";
1+
import { BaseObject } from "../transport/base";
2+
import { KlepperIncomingMessage, KlepperRequest } from "../transport/http";
73

8-
// const prepareStackTraces = (stackFrames: StackFrame[]): Trace[] => {
9-
// const parseTraces = (frame: StackFrame): Trace => {
10-
// return {
11-
// functionName: frame.getFunctionName(),
12-
// rowNo: frame.getLineNumber(),
13-
// colNo: frame.getColumnNumber(),
14-
// fileName: frame.getFileName(),
15-
// absolutePath: frame.getFunctionName(),
16-
// isInternal: isInternal(frame.getFileName()),
17-
// }
18-
// }
19-
20-
// const traces = stackFrames.map((frame) => parseTraces(frame)) || [];
21-
// return traces;
22-
// }
23-
24-
const isInternal = (fileName: string): boolean =>
25-
!!fileName &&
26-
!fileName.includes("node_modules") &&
27-
!fileName.startsWith("/") &&
28-
!fileName.startsWith("node:") &&
29-
fileName.includes(":\\");
30-
31-
const mapRequestData = (req: BaseObject): KlepperRequest => {
4+
export const mapRequestData = (req: BaseObject): KlepperRequest => {
325
const headersData = req.headers || req.header || {};
336

347
const method = req.method;
@@ -67,26 +40,12 @@ const mapRequestData = (req: BaseObject): KlepperRequest => {
6740
return request;
6841
};
6942

70-
const getIp = (req: KlepperIncomingMessage): string | string[] | undefined => {
43+
export const getIp = (
44+
req: KlepperIncomingMessage
45+
): string | string[] | undefined => {
7146
return req.headers["x-forwarded-for"] || req.socket.remoteAddress;
7247
};
7348

74-
const isLocalhost = (req: KlepperIncomingMessage): boolean => {
75-
const ip = getIp(req);
76-
return ip === "::1" || ip === "127.0.0.1" ? true : false;
77-
};
78-
79-
const getProtocol = (req: KlepperIncomingMessage): string => {
49+
export const getProtocol = (req: KlepperIncomingMessage): string => {
8050
return req.protocol === "https" || req.secure ? "https" : "http";
8151
};
82-
83-
const isEmpty = (obj: any) => Object.keys(obj).length === 0;
84-
85-
export const helpers = {
86-
getIp,
87-
mapRequestData,
88-
isEmpty,
89-
// prepareStackTraces,
90-
getProtocol,
91-
isLocalhost,
92-
};

src/node/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as Middleware from "./middlewares/index";
1+
import { Middleware } from "..";
22
import * as Klepper from "./sdk";
33

44
export { Middleware, Klepper };

0 commit comments

Comments
 (0)