@logdrop/node
Advanced tools
+5
-1
@@ -1,1 +0,5 @@ | ||
| export declare const record: (apiKey: string) => (req: any, res: any, next: any) => Promise<void>; | ||
| interface Config { | ||
| exclude?: string | string[]; | ||
| } | ||
| export declare const record: (apiKey: string, config?: Config) => (req: any, res: any, next: any) => Promise<void>; | ||
| export {}; |
+65
-51
@@ -19,3 +19,3 @@ "use strict"; | ||
| const body_parser_1 = __importDefault(require("body-parser")); | ||
| const record = (apiKey) => { | ||
| const record = (apiKey, config) => { | ||
| return (req, res, next) => __awaiter(void 0, void 0, void 0, function* () { | ||
@@ -26,2 +26,14 @@ body_parser_1.default.json()(req, res, () => { | ||
| const clientIp = request_ip_1.default.getClientIp(req); | ||
| const shouldExclude = (endpoint) => { | ||
| if (!config || !config.exclude) { | ||
| return false; | ||
| } | ||
| const exclude = Array.isArray(config.exclude) | ||
| ? config.exclude | ||
| : [config.exclude]; | ||
| return exclude.some((pattern) => { | ||
| const regex = new RegExp(`^${pattern.replace(/\*/g, ".*")}$`); | ||
| return regex.test(endpoint); | ||
| }); | ||
| }; | ||
| const recordData = { | ||
@@ -42,57 +54,59 @@ statusCode: 0, | ||
| }; | ||
| const originalSetHeader = res.setHeader; | ||
| //@ts-ignore | ||
| res.setHeader = function (name, value) { | ||
| if (!recordData.responseHeaders) { | ||
| recordData.responseHeaders = {}; | ||
| } | ||
| if (!shouldExclude(recordData.endpoint)) { | ||
| const originalSetHeader = res.setHeader; | ||
| //@ts-ignore | ||
| recordData.responseHeaders[name] = value; | ||
| originalSetHeader.apply(this, arguments); | ||
| }; | ||
| const originalEnd = res.end; | ||
| const originalWrite = res.write; | ||
| //@ts-ignore | ||
| res.write = function (chunk, encodingOrCallback, callback) { | ||
| if (typeof encodingOrCallback === "function") { | ||
| return originalWrite.call(this, chunk, "utf8", encodingOrCallback); | ||
| } | ||
| const encoding = encodingOrCallback || "utf8"; | ||
| return originalWrite.call(this, chunk, encoding, callback); | ||
| }; | ||
| //@ts-ignore | ||
| res.end = function (...args | ||
| //@ts-ignore | ||
| ) { | ||
| //@ts-ignore | ||
| recordData.statusCode = this.statusCode; | ||
| if (args && | ||
| args.length > 0 && | ||
| args[0] !== null && | ||
| args[0] !== undefined) { | ||
| res.setHeader = function (name, value) { | ||
| if (!recordData.responseHeaders) { | ||
| recordData.responseHeaders = {}; | ||
| } | ||
| //@ts-ignore | ||
| recordData.responseBody = args[0].toString("utf8"); | ||
| } | ||
| recordData.responseHeaders[name] = value; | ||
| originalSetHeader.apply(this, arguments); | ||
| }; | ||
| const originalEnd = res.end; | ||
| const originalWrite = res.write; | ||
| //@ts-ignore | ||
| const responseTime = process.hrtime(requestStartTime); | ||
| res.write = function (chunk, encodingOrCallback, callback) { | ||
| if (typeof encodingOrCallback === "function") { | ||
| return originalWrite.call(this, chunk, "utf8", encodingOrCallback); | ||
| } | ||
| const encoding = encodingOrCallback || "utf8"; | ||
| return originalWrite.call(this, chunk, encoding, callback); | ||
| }; | ||
| //@ts-ignore | ||
| const responseTimeMs = responseTime[0] * 1e3 + responseTime[1] / 1e6; | ||
| res.end = function (...args | ||
| //@ts-ignore | ||
| recordData.responseTime = new Date().toISOString(); | ||
| //@ts-ignore | ||
| recordData.elapsedDuration = responseTimeMs; | ||
| axios_1.default | ||
| .post("https://logdrop.co/api/v1/log", recordData, { | ||
| headers: { | ||
| Authorization: `Bearer ${apiKey}`, | ||
| }, | ||
| }) | ||
| .then(() => { }) | ||
| .catch((error) => { | ||
| var _a; | ||
| console.error("LOGDROP FAILED: " + | ||
| (((_a = error.response) === null || _a === void 0 ? void 0 : _a.data.message) || "Something went wrong")); | ||
| }); | ||
| return originalEnd.apply(this, args); | ||
| }; | ||
| ) { | ||
| //@ts-ignore | ||
| recordData.statusCode = this.statusCode; | ||
| if (args && | ||
| args.length > 0 && | ||
| args[0] !== null && | ||
| args[0] !== undefined) { | ||
| //@ts-ignore | ||
| recordData.responseBody = args[0].toString("utf8"); | ||
| } | ||
| //@ts-ignore | ||
| const responseTime = process.hrtime(requestStartTime); | ||
| //@ts-ignore | ||
| const responseTimeMs = responseTime[0] * 1e3 + responseTime[1] / 1e6; | ||
| //@ts-ignore | ||
| recordData.responseTime = new Date().toISOString(); | ||
| //@ts-ignore | ||
| recordData.elapsedDuration = responseTimeMs; | ||
| axios_1.default | ||
| .post("https://logdrop.co/api/v1/log", recordData, { | ||
| headers: { | ||
| Authorization: `Bearer ${apiKey}`, | ||
| }, | ||
| }) | ||
| .then(() => { }) | ||
| .catch((error) => { | ||
| var _a; | ||
| console.error("LOGDROP FAILED: " + | ||
| (((_a = error.response) === null || _a === void 0 ? void 0 : _a.data.message) || "Something went wrong")); | ||
| }); | ||
| return originalEnd.apply(this, args); | ||
| }; | ||
| } | ||
| next(); | ||
@@ -99,0 +113,0 @@ }); |
+1
-1
| { | ||
| "name": "@logdrop/node", | ||
| "version": "0.0.4", | ||
| "version": "0.0.5", | ||
| "description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
+93
-74
@@ -5,3 +5,7 @@ import requestIp from "request-ip"; | ||
| export const record = (apiKey: string) => { | ||
| interface Config { | ||
| exclude?: string | string[]; | ||
| } | ||
| export const record = (apiKey: string, config?: Config) => { | ||
| return async (req: any, res: any, next: any) => { | ||
@@ -12,2 +16,15 @@ bodyParser.json()(req, res, () => { | ||
| const shouldExclude = (endpoint: string) => { | ||
| if (!config || !config.exclude) { | ||
| return false; | ||
| } | ||
| const exclude = Array.isArray(config.exclude) | ||
| ? config.exclude | ||
| : [config.exclude]; | ||
| return exclude.some((pattern) => { | ||
| const regex = new RegExp(`^${pattern.replace(/\*/g, ".*")}$`); | ||
| return regex.test(endpoint); | ||
| }); | ||
| }; | ||
| const recordData = { | ||
@@ -29,88 +46,90 @@ statusCode: 0, | ||
| const originalSetHeader = res.setHeader; | ||
| //@ts-ignore | ||
| (res.setHeader as any) = function ( | ||
| this: Response, | ||
| name: string, | ||
| value: string | string[] | ||
| ): void { | ||
| if (!recordData.responseHeaders) { | ||
| recordData.responseHeaders = {}; | ||
| } | ||
| if (!shouldExclude(recordData.endpoint)) { | ||
| const originalSetHeader = res.setHeader; | ||
| //@ts-ignore | ||
| recordData.responseHeaders[name] = value; | ||
| originalSetHeader.apply(this, arguments); | ||
| }; | ||
| (res.setHeader as any) = function ( | ||
| this: Response, | ||
| name: string, | ||
| value: string | string[] | ||
| ): void { | ||
| if (!recordData.responseHeaders) { | ||
| recordData.responseHeaders = {}; | ||
| } | ||
| //@ts-ignore | ||
| recordData.responseHeaders[name] = value; | ||
| originalSetHeader.apply(this, arguments); | ||
| }; | ||
| const originalEnd = res.end as ( | ||
| this: Response, | ||
| ...args: any[] | ||
| const originalEnd = res.end as ( | ||
| this: Response, | ||
| ...args: any[] | ||
| //@ts-ignore | ||
| ) => Response<any, Record<string, any>>; | ||
| const originalWrite = res.write; | ||
| //@ts-ignore | ||
| ) => Response<any, Record<string, any>>; | ||
| res.write = function ( | ||
| this: Response, | ||
| chunk: any, | ||
| encodingOrCallback?: | ||
| | BufferEncoding | ||
| | ((error: Error | null | undefined) => void), | ||
| callback?: (error: Error | null | undefined) => void | ||
| ): boolean { | ||
| if (typeof encodingOrCallback === "function") { | ||
| return originalWrite.call(this, chunk, "utf8", encodingOrCallback); | ||
| } | ||
| const originalWrite = res.write; | ||
| //@ts-ignore | ||
| res.write = function ( | ||
| this: Response, | ||
| chunk: any, | ||
| encodingOrCallback?: | ||
| | BufferEncoding | ||
| | ((error: Error | null | undefined) => void), | ||
| callback?: (error: Error | null | undefined) => void | ||
| ): boolean { | ||
| if (typeof encodingOrCallback === "function") { | ||
| return originalWrite.call(this, chunk, "utf8", encodingOrCallback); | ||
| } | ||
| const encoding = encodingOrCallback || "utf8"; | ||
| const encoding = encodingOrCallback || "utf8"; | ||
| return originalWrite.call(this, chunk, encoding, callback); | ||
| }; | ||
| return originalWrite.call(this, chunk, encoding, callback); | ||
| }; | ||
| //@ts-ignore | ||
| res.end = function ( | ||
| this: Response, | ||
| ...args: any[] | ||
| //@ts-ignore | ||
| ): Response<any, Record<string, any>> { | ||
| //@ts-ignore | ||
| recordData.statusCode = this.statusCode; | ||
| res.end = function ( | ||
| this: Response, | ||
| ...args: any[] | ||
| //@ts-ignore | ||
| ): Response<any, Record<string, any>> { | ||
| //@ts-ignore | ||
| recordData.statusCode = this.statusCode; | ||
| if ( | ||
| args && | ||
| args.length > 0 && | ||
| args[0] !== null && | ||
| args[0] !== undefined | ||
| ) { | ||
| if ( | ||
| args && | ||
| args.length > 0 && | ||
| args[0] !== null && | ||
| args[0] !== undefined | ||
| ) { | ||
| //@ts-ignore | ||
| recordData.responseBody = args[0].toString("utf8"); | ||
| } | ||
| //@ts-ignore | ||
| recordData.responseBody = args[0].toString("utf8"); | ||
| } | ||
| const responseTime = process.hrtime(requestStartTime); | ||
| //@ts-ignore | ||
| const responseTimeMs = responseTime[0] * 1e3 + responseTime[1] / 1e6; | ||
| //@ts-ignore | ||
| const responseTime = process.hrtime(requestStartTime); | ||
| //@ts-ignore | ||
| const responseTimeMs = responseTime[0] * 1e3 + responseTime[1] / 1e6; | ||
| //@ts-ignore | ||
| recordData.responseTime = new Date().toISOString(); | ||
| //@ts-ignore | ||
| recordData.elapsedDuration = responseTimeMs; | ||
| //@ts-ignore | ||
| recordData.responseTime = new Date().toISOString(); | ||
| //@ts-ignore | ||
| recordData.elapsedDuration = responseTimeMs; | ||
| axios | ||
| .post("https://logdrop.co/api/v1/log", recordData, { | ||
| headers: { | ||
| Authorization: `Bearer ${apiKey}`, | ||
| }, | ||
| }) | ||
| .then(() => {}) | ||
| .catch((error) => { | ||
| console.error( | ||
| "LOGDROP FAILED: " + | ||
| (error.response?.data.message || "Something went wrong") | ||
| ); | ||
| }); | ||
| axios | ||
| .post("https://logdrop.co/api/v1/log", recordData, { | ||
| headers: { | ||
| Authorization: `Bearer ${apiKey}`, | ||
| }, | ||
| }) | ||
| .then(() => {}) | ||
| .catch((error) => { | ||
| console.error( | ||
| "LOGDROP FAILED: " + | ||
| (error.response?.data.message || "Something went wrong") | ||
| ); | ||
| }); | ||
| return originalEnd.apply(this, args); | ||
| }; | ||
| } | ||
| return originalEnd.apply(this, args); | ||
| }; | ||
| next(); | ||
@@ -117,0 +136,0 @@ }); |
AI-detected potential code anomaly
Supply chain riskAI has identified unusual behaviors that may pose a security risk.
Found 1 instance in 1 package
AI-detected potential code anomaly
Supply chain riskAI has identified unusual behaviors that may pose a security risk.
Found 1 instance in 1 package
10540
17.9%248
16.43%