Skip to content

Commit a46106b

Browse files
committed
fixes and improvements, using dsn instead of appId passed into sdk options, renaming to Traceo
1 parent db6f498 commit a46106b

15 files changed

Lines changed: 99 additions & 130 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# klepper.io-node
2-
Library for klepper.io written in Typescript
1+
# traceo-sdk
2+
Library for Traceo written in Typescript

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "klepper",
3-
"version": "0.0.61-alpha",
2+
"name": "traceo-sdk",
3+
"version": "0.0.72-alpha",
44
"license": "MIT",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/core/global.ts

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

3-
export const getGlobalClientData = (): KlepperGlobal =>
4-
global.__KLEPPER__ || {};
3+
export const getGlobalClientData = (): TraceoGlobal =>
4+
global.__TRACEO__ || {};
55

6-
export const setGlobalClientData = (data: KlepperGlobal): void => {
7-
global.__KLEPPER__ = { ...data };
6+
export const setGlobalClientData = (data: TraceoGlobal): void => {
7+
global.__TRACEO__ = { ...data };
88
};

src/core/http.ts

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
11
import * as http from "http";
2+
import { sanitizeDsn } from "../node/helpers";
23
import { RequestStatus } from "../transport/enums";
34
import {
45
EventResponse,
5-
KlepperEvent,
6-
KlepperReleaseEvent,
6+
TraceoEvent,
7+
TraceoReleaseEvent,
78
} from "../transport/events";
8-
import { KlepperIncomingMessage, RequestOptions } from "../transport/http";
9+
import { TraceoIncomingMessage, RequestOptions } from "../transport/http";
910
import { getGlobalClientData } from "./global";
1011
import { isClientConnected } from "./is";
1112

12-
const KLEPPER_HOST = process.env.KLEPPER_HOST || "localhost";
13-
const KLEPPER_API = process.env.KLEPPER_API || "/sdk/incident";
14-
const KLEPPER_PORT = process.env.KLEPPER_PORT || 8080;
15-
1613
const createHttpOptions = ({
1714
event,
18-
api = KLEPPER_API,
15+
api,
1916
}: {
20-
event?: KlepperEvent | KlepperReleaseEvent;
17+
event?: TraceoEvent | TraceoReleaseEvent;
2118
api?: string;
2219
}): http.RequestOptions => {
2320
const client = getGlobalClientData();
2421

25-
const { privateKey, appId } = client;
22+
const { dsn } = client;
23+
const { host, secretKey, appId } = sanitizeDsn(dsn);
24+
25+
api = api === "release" ? `/release/${appId}` : `/${appId}`;
26+
2627
const baseOptions: RequestOptions = {
27-
hostname: KLEPPER_HOST,
28-
port: +KLEPPER_PORT,
28+
hostname: host,
2929
method: "POST",
3030
headers: {
3131
"Content-Type": "application/json",
3232
"Content-Length": `${Buffer.byteLength(JSON.stringify(event))}`,
33-
"klepper-private-key": String(privateKey),
34-
"klepper-app-id": String(appId),
33+
"traceo-secret-key": secretKey
3534
},
3635
};
3736

@@ -44,26 +43,25 @@ const createHttpOptions = ({
4443
const statusFromCode = (code: number) =>
4544
code >= 200 && code <= 299 ? RequestStatus.SUCCESS : RequestStatus.ERROR;
4645

47-
export const sendConnection = (connectionData: KlepperReleaseEvent): void => {
46+
export const sendConnection = (connectionData: TraceoReleaseEvent): void => {
4847
const httpOptions = createHttpOptions({
4948
event: connectionData,
50-
api: "/sdk/release",
49+
api: "release",
5150
});
5251

53-
const request = http.request(httpOptions, (res: KlepperIncomingMessage) => {
52+
const request = http.request(httpOptions, (res: TraceoIncomingMessage) => {
5453
res.setEncoding("utf8");
5554
});
5655
request.write(JSON.stringify(connectionData));
5756
request.end();
5857
};
5958

6059
export const sendEvent = async (
61-
event: KlepperEvent
60+
event: TraceoEvent
6261
): Promise<EventResponse> => {
6362
const client = getGlobalClientData();
6463

6564
const baseData = {
66-
env: client?.environment,
6765
version: client?.version,
6866
};
6967

@@ -75,19 +73,19 @@ export const sendEvent = async (
7573
reject({
7674
statusCode: 400,
7775
statusMessage:
78-
"[Klepper] Error during sending event to Klepper. No HTTP options.",
76+
"[Traceo] Error during sending event to Traceo. No HTTP options.",
7977
});
8078
}
8179

8280
if (!isClientConnected()) {
8381
reject({
8482
statusCode: 400,
8583
statusMessage:
86-
"[Klepper] Error during sending event to Klepper. No client global data in NodeJS scope.",
84+
"[Traceo] Error during sending event to Traceo. No client global data in NodeJS scope.",
8785
});
8886
}
8987

90-
const request = http.request(httpOptions, (res: KlepperIncomingMessage) => {
88+
const request = http.request(httpOptions, (res: TraceoIncomingMessage) => {
9189
res.setEncoding("utf8");
9290

9391
const status = statusFromCode(res?.statusCode as number);
@@ -96,12 +94,12 @@ export const sendEvent = async (
9694
if (!isSuccess) {
9795
reject({
9896
statusCode: res?.statusCode as number,
99-
statusMessage: "[KLEPPER] Error during sending event to Klepper.",
97+
statusMessage: "[Traceo] Error during sending event to Traceo.",
10098
});
10199
} else {
102100
resolve({
103101
statusCode: res?.statusCode as number,
104-
statusMessage: "[KLEPPER] Event successfully sended to Klepper.",
102+
statusMessage: "[Traceo] Event successfully sended to Traceo.",
105103
});
106104
}
107105

@@ -115,7 +113,7 @@ export const sendEvent = async (
115113
reject({
116114
statusCode: 400,
117115
statusMessage:
118-
"[Klepper] Error during sending event to Klepper. Connection timeout.",
116+
"[Traceo] Error during sending event to Traceo. Connection timeout.",
119117
});
120118
});
121119

src/core/types/global.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { KlepperGlobal } from "../../transport/base";
1+
import { TraceoGlobal } from "../../transport/base";
22

33
declare global {
4-
var __KLEPPER__: KlepperGlobal;
4+
var __TRACEO__: TraceoGlobal;
55
}
66
export {};

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { Middleware, catchException } from "./node/middlewares";
2-
import * as Klepper from "./node/sdk";
2+
import * as Traceo from "./node/sdk";
33
import { ExceptionPriority } from "./transport/enums";
44
import { CatchExceptionsOptions } from "./transport/options";
55

66
export {
77
Middleware,
8-
Klepper,
8+
Traceo,
99
catchException,
1010
CatchExceptionsOptions,
1111
ExceptionPriority,

src/node/helpers.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { BaseObject } from "../transport/base";
22
import { Platform } from "../transport/events";
3-
import { KlepperIncomingMessage, KlepperRequest } from "../transport/http";
3+
import { TraceoIncomingMessage, TraceoRequest } from "../transport/http";
44
import * as os from "os";
55

6-
export const mapRequestData = (req: BaseObject): KlepperRequest => {
6+
export const mapRequestData = (req: BaseObject): TraceoRequest => {
77
const headersData = req.headers || req.header || {};
88

99
const method = req.method;
@@ -16,7 +16,7 @@ export const mapRequestData = (req: BaseObject): KlepperRequest => {
1616
const origin = headersData["origin"];
1717
const query = req.query;
1818
const payload = req.body || {};
19-
const ip = getIp(req as KlepperIncomingMessage);
19+
const ip = getIp(req as TraceoIncomingMessage);
2020

2121
const connections = {
2222
absoluteUrl,
@@ -43,20 +43,29 @@ export const mapRequestData = (req: BaseObject): KlepperRequest => {
4343
};
4444

4545
export const getIp = (
46-
req: KlepperIncomingMessage
46+
req: TraceoIncomingMessage
4747
): string | string[] | undefined => {
4848
return req.headers["x-forwarded-for"] || req.socket.remoteAddress;
4949
};
5050

51-
export const getProtocol = (req: KlepperIncomingMessage): string => {
51+
export const getProtocol = (req: TraceoIncomingMessage): string => {
5252
return req.protocol === "https" || req.secure ? "https" : "http";
5353
};
5454

5555
export const getOsPlatform = (): Platform => {
5656
return {
57-
arch: os.arch(),
58-
platform: os.platform(),
59-
release: os.release(),
60-
version: os.version(),
57+
arch: os.arch(),
58+
platform: os.platform(),
59+
release: os.release(),
60+
version: os.version(),
61+
};
62+
};
63+
64+
export const sanitizeDsn = (dsn: string) => {
65+
const [secretKey, rest] = dsn.replace("https://", "").split(":");
66+
const [host, appId] = rest.split("/");
67+
68+
return {
69+
secretKey, host, appId
6170
}
6271
}

src/node/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import { Middleware } from "..";
2-
import * as Klepper from "./sdk";
2+
import * as Traceo from "./sdk";
33

4-
export { Middleware, Klepper };
4+
export { Middleware, Traceo };

src/node/middlewares.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { sendEvent } from "../core/http";
22
import { isClientConnected, isLocalhost } from "../core/is";
3-
import { KlepperError } from "../transport/base";
4-
import { KlepperEvent } from "../transport/events";
3+
import { TraceoError } from "../transport/base";
4+
import { TraceoEvent } from "../transport/events";
55
import {
6-
KlepperIncomingMessage,
7-
KlepperServerResponse,
6+
TraceoIncomingMessage,
7+
TraceoServerResponse,
88
} from "../transport/http";
99
import {
1010
CatchExceptionsOptions,
@@ -38,10 +38,10 @@ import { prepareException } from "./parse";
3838
*/
3939
const errorMiddleware = (options: ErrorMiddlewareOptions = {}) => {
4040
return async function errorMiddleware(
41-
error: KlepperError,
42-
req: KlepperIncomingMessage,
43-
_res: KlepperServerResponse,
44-
next: (error: KlepperError) => void
41+
error: TraceoError,
42+
req: TraceoIncomingMessage,
43+
_res: TraceoServerResponse,
44+
next: (error: TraceoError) => void
4545
): Promise<void> {
4646
if (!isClientConnected()) {
4747
next(error);
@@ -57,7 +57,7 @@ const errorMiddleware = (options: ErrorMiddlewareOptions = {}) => {
5757
};
5858

5959
const isToCatch = (
60-
req: KlepperIncomingMessage,
60+
req: TraceoIncomingMessage,
6161
options: ErrorMiddlewareOptions = {}
6262
): boolean => {
6363
if (options.allowHttp !== undefined && !options.allowHttp) {
@@ -77,7 +77,7 @@ const isToCatch = (
7777
};
7878

7979
interface Catch {
80-
options?: CatchExceptionsOptions;
80+
// options?: CatchExceptionsOptions;
8181
shouldBeCatched?: (error: any) => boolean;
8282
}
8383

@@ -113,19 +113,19 @@ export const catchException = async (error: any, catchOptions?: Catch) => {
113113
}
114114

115115
if (isClientConnected()) {
116-
await handleException(error, undefined, catchOptions?.options);
116+
await handleException(error, undefined);
117117
}
118118

119119
return;
120120
};
121121

122122
const handleException = async (
123-
error: KlepperError,
124-
req?: KlepperIncomingMessage,
123+
error: TraceoError,
124+
req?: TraceoIncomingMessage,
125125
options?: CatchExceptionsOptions
126126
) => {
127127
try {
128-
const event: KlepperEvent = await prepareException(error, options, req);
128+
const event: TraceoEvent = await prepareException(error, options, req);
129129
await sendEvent(event);
130130
} catch (err) {
131131
//

src/node/parse.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { Trace } from "../transport/trace";
22
import { promises, existsSync } from "fs";
3-
import { KlepperError } from "../transport/base";
3+
import { TraceoError } from "../transport/base";
44
import { CatchExceptionsOptions } from "../transport/options";
5-
import { KlepperEvent } from "../transport/events";
6-
import { KlepperIncomingMessage } from "../transport/http";
5+
import { TraceoEvent } from "../transport/events";
6+
import { TraceoIncomingMessage } from "../transport/http";
77
import { getOsPlatform, mapRequestData } from "./helpers";
8-
import * as os from "os";
98

109
const FULL_MATCH =
1110
/at (?:async )?(?:(.+?)\s+\()?(?:(.+?):(\d+)(?::(\d+))?|([^)]+))\)?/;
@@ -125,22 +124,22 @@ const readFileAsync = async (path: string): Promise<string> => {
125124
};
126125

127126
export const prepareException = async (
128-
error: KlepperError,
127+
error: TraceoError,
129128
options?: CatchExceptionsOptions,
130-
req?: KlepperIncomingMessage
131-
): Promise<KlepperEvent> => {
129+
req?: TraceoIncomingMessage
130+
): Promise<TraceoEvent> => {
132131
const { message, name } = error;
133132

134133
const platform = getOsPlatform();
135134

136135
const traces = await parseStackTraces(String(error?.stack));
137-
const event: KlepperEvent = {
136+
const event: TraceoEvent = {
138137
type: name,
139138
message,
140139
traces,
141140
stack: String(error.stack),
142141
options,
143-
platform
142+
platform,
144143
};
145144

146145
if (req !== undefined) {

0 commit comments

Comments
 (0)