@flink-app/debug-plugin 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/dist/handlers/Debug/Get.d.ts +5 -0
  2. package/dist/handlers/Debug/Get.js +56 -0
  3. package/dist/handlers/Debug/PostDisable.d.ts +5 -0
  4. package/dist/handlers/Debug/PostDisable.js +51 -0
  5. package/dist/handlers/Debug/PostEnable.d.ts +5 -0
  6. package/dist/handlers/Debug/PostEnable.js +51 -0
  7. package/dist/index.d.ts +19 -0
  8. package/dist/index.js +150 -0
  9. package/dist/models/request.d.ts +11 -0
  10. package/dist/models/request.js +2 -0
  11. package/dist/schemas/Debug/GetReq.d.ts +2 -0
  12. package/dist/schemas/Debug/GetReq.js +2 -0
  13. package/dist/schemas/Debug/GetRes.d.ts +5 -0
  14. package/dist/schemas/Debug/GetRes.js +2 -0
  15. package/dist/schemas/Debug/PostDisableReq.d.ts +2 -0
  16. package/dist/schemas/Debug/PostDisableReq.js +2 -0
  17. package/dist/schemas/Debug/PostDisableRes.d.ts +2 -0
  18. package/dist/schemas/Debug/PostDisableRes.js +2 -0
  19. package/dist/schemas/Debug/PostEnableReq.d.ts +2 -0
  20. package/dist/schemas/Debug/PostEnableReq.js +2 -0
  21. package/dist/schemas/Debug/PostEnableRes.d.ts +2 -0
  22. package/dist/schemas/Debug/PostEnableRes.js +2 -0
  23. package/package.json +29 -0
  24. package/readme.md +74 -0
  25. package/src/handlers/Debug/Get.ts +19 -0
  26. package/src/handlers/Debug/PostDisable.ts +13 -0
  27. package/src/handlers/Debug/PostEnable.ts +13 -0
  28. package/src/index.ts +154 -0
  29. package/src/models/request.ts +12 -0
  30. package/src/schemas/Debug/GetReq.ts +1 -0
  31. package/src/schemas/Debug/GetRes.ts +6 -0
  32. package/src/schemas/Debug/PostDisableReq.ts +1 -0
  33. package/src/schemas/Debug/PostDisableRes.ts +1 -0
  34. package/src/schemas/Debug/PostEnableReq.ts +3 -0
  35. package/src/schemas/Debug/PostEnableRes.ts +3 -0
  36. package/tsconfig.json +23 -0
@@ -0,0 +1,5 @@
1
+ import { Handler, FlinkContext } from "@flink-app/flink";
2
+ import { GetReq } from "../../schemas/Debug/GetReq";
3
+ import { GetRes } from "../../schemas/Debug/GetRes";
4
+ declare const Get: Handler<FlinkContext, GetReq, GetRes>;
5
+ export default Get;
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ var Get = function (_a) {
40
+ var ctx = _a.ctx, req = _a.req, origin = _a.origin;
41
+ return __awaiter(void 0, void 0, void 0, function () {
42
+ var enabled, requests;
43
+ return __generator(this, function (_b) {
44
+ enabled = ctx.plugins.debugPlugin.enabled;
45
+ requests = ctx.plugins.debugPlugin.requests;
46
+ return [2 /*return*/, {
47
+ data: {
48
+ requests: requests,
49
+ enabled: enabled,
50
+ },
51
+ status: 200,
52
+ }];
53
+ });
54
+ });
55
+ };
56
+ exports.default = Get;
@@ -0,0 +1,5 @@
1
+ import { Handler, FlinkContext } from "@flink-app/flink";
2
+ import { PostDebugDisableReq } from "../../schemas/Debug/PostDisableReq";
3
+ import { PostDebugDisableRes } from "../../schemas/Debug/PostDisableRes";
4
+ declare const PostDebugEnable: Handler<FlinkContext, PostDebugDisableReq, PostDebugDisableRes>;
5
+ export default PostDebugEnable;
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ var PostDebugEnable = function (_a) {
40
+ var ctx = _a.ctx, req = _a.req, origin = _a.origin;
41
+ return __awaiter(void 0, void 0, void 0, function () {
42
+ return __generator(this, function (_b) {
43
+ ctx.plugins.debugPlugin.enabled = false;
44
+ return [2 /*return*/, {
45
+ data: {},
46
+ status: 200,
47
+ }];
48
+ });
49
+ });
50
+ };
51
+ exports.default = PostDebugEnable;
@@ -0,0 +1,5 @@
1
+ import { Handler, FlinkContext } from "@flink-app/flink";
2
+ import { PostDebugEnableReq } from "../../schemas/Debug/PostEnableReq";
3
+ import { PostDebugEnableRes } from "../../schemas/Debug/PostEnableRes";
4
+ declare const PostDebugEnable: Handler<FlinkContext, PostDebugEnableReq, PostDebugEnableRes>;
5
+ export default PostDebugEnable;
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ var PostDebugEnable = function (_a) {
40
+ var ctx = _a.ctx, req = _a.req, origin = _a.origin;
41
+ return __awaiter(void 0, void 0, void 0, function () {
42
+ return __generator(this, function (_b) {
43
+ ctx.plugins.debugPlugin.enabled = true;
44
+ return [2 /*return*/, {
45
+ data: {},
46
+ status: 200,
47
+ }];
48
+ });
49
+ });
50
+ };
51
+ exports.default = PostDebugEnable;
@@ -0,0 +1,19 @@
1
+ import { FlinkPlugin } from "@flink-app/flink";
2
+ import { ManagementApiModule } from "@flink-app/management-api-plugin";
3
+ export declare type StaticOptions = {
4
+ /**
5
+ * Base url
6
+ */
7
+ logToConsole: boolean;
8
+ enabledAtStart: boolean;
9
+ keepLogs?: number;
10
+ };
11
+ export declare const debugPlugin: (options: StaticOptions) => FlinkPlugin;
12
+ export interface GetManagementModuleConfig {
13
+ pluginId?: string;
14
+ ui: boolean;
15
+ uiSettings?: {
16
+ title: string;
17
+ };
18
+ }
19
+ export declare const GetManagementModule: (config: GetManagementModuleConfig) => ManagementApiModule;
package/dist/index.js ADDED
@@ -0,0 +1,150 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ var __importDefault = (this && this.__importDefault) || function (mod) {
22
+ return (mod && mod.__esModule) ? mod : { "default": mod };
23
+ };
24
+ Object.defineProperty(exports, "__esModule", { value: true });
25
+ exports.GetManagementModule = exports.debugPlugin = void 0;
26
+ var flink_1 = require("@flink-app/flink");
27
+ var management_api_plugin_1 = require("@flink-app/management-api-plugin");
28
+ var node_color_log_1 = __importDefault(require("node-color-log"));
29
+ var GetHandler = __importStar(require("./handlers/Debug/Get"));
30
+ var PostDisableHandler = __importStar(require("./handlers/Debug/PostDisable"));
31
+ var PostEnableHandler = __importStar(require("./handlers/Debug/PostEnable"));
32
+ var debugPlugin = function (options) {
33
+ var requests = [];
34
+ var enabled = options.enabledAtStart;
35
+ return {
36
+ id: "debugPlugin",
37
+ init: function (app) { return init(app, options); },
38
+ ctx: {
39
+ requests: requests,
40
+ enabled: enabled,
41
+ },
42
+ };
43
+ };
44
+ exports.debugPlugin = debugPlugin;
45
+ function init(app, options) {
46
+ var expressApp = app.expressApp;
47
+ var keep = 100;
48
+ if (options.keepLogs != null) {
49
+ keep = options.keepLogs;
50
+ }
51
+ if (!expressApp) {
52
+ throw new Error("Express app not initialized");
53
+ }
54
+ if (options.enabledAtStart) {
55
+ node_color_log_1.default.info("Debug enabled");
56
+ }
57
+ expressApp.use(function (req, res, next) {
58
+ var requests = app.ctx.plugins.debugPlugin.requests;
59
+ var enabled = app.ctx.plugins.debugPlugin.enabled;
60
+ if (!enabled) {
61
+ if (next) {
62
+ next();
63
+ }
64
+ return;
65
+ }
66
+ if (req.originalUrl.startsWith("/managementapi")) {
67
+ if (next) {
68
+ next();
69
+ }
70
+ return;
71
+ }
72
+ var requestItem = {
73
+ start: new Date(),
74
+ method: req.method,
75
+ path: req.originalUrl,
76
+ body: req.body,
77
+ headers: req.headers,
78
+ };
79
+ requests.unshift(requestItem);
80
+ requests = requests.splice(0, keep);
81
+ app.ctx.plugins.debugPlugin.requests = requests;
82
+ var oldWrite = res.write;
83
+ var oldEnd = res.end;
84
+ var chunks = [];
85
+ res.write = function (chunk) {
86
+ chunks.push(Buffer.from(chunk));
87
+ oldWrite.apply(res, arguments);
88
+ };
89
+ res.end = function (chunk) {
90
+ if (chunk) {
91
+ chunks.push(Buffer.from(chunk));
92
+ }
93
+ var body = Buffer.concat(chunks).toString("utf8");
94
+ requestItem.end = new Date();
95
+ requestItem.response = body;
96
+ if (options.logToConsole) {
97
+ node_color_log_1.default.debug(requestItem);
98
+ }
99
+ oldEnd.apply(res, arguments);
100
+ };
101
+ if (next) {
102
+ next();
103
+ }
104
+ });
105
+ }
106
+ var GetManagementModule = function (config) {
107
+ var _a;
108
+ if (config.pluginId == null)
109
+ config.pluginId = "debug";
110
+ var endpoints = [];
111
+ endpoints.push({
112
+ routeProps: {
113
+ path: "/",
114
+ method: flink_1.HttpMethod.get,
115
+ origin: config.pluginId,
116
+ },
117
+ handler: GetHandler,
118
+ });
119
+ endpoints.push({
120
+ routeProps: {
121
+ path: "/enable",
122
+ method: flink_1.HttpMethod.post,
123
+ origin: config.pluginId,
124
+ },
125
+ handler: PostEnableHandler,
126
+ });
127
+ endpoints.push({
128
+ routeProps: {
129
+ path: "/disable",
130
+ method: flink_1.HttpMethod.post,
131
+ origin: config.pluginId,
132
+ },
133
+ handler: PostDisableHandler,
134
+ });
135
+ var features = [];
136
+ var module = {
137
+ id: config.pluginId || "debug",
138
+ uiSettings: {
139
+ title: ((_a = config.uiSettings) === null || _a === void 0 ? void 0 : _a.title) || "Debug",
140
+ icon: "",
141
+ features: features,
142
+ },
143
+ ui: config.ui,
144
+ type: management_api_plugin_1.ManagementApiType.debug,
145
+ endpoints: endpoints,
146
+ data: {},
147
+ };
148
+ return module;
149
+ };
150
+ exports.GetManagementModule = GetManagementModule;
@@ -0,0 +1,11 @@
1
+ /// <reference types="node" />
2
+ import { IncomingHttpHeaders } from "http";
3
+ export default interface request {
4
+ start: Date;
5
+ end?: Date;
6
+ method: string;
7
+ path: string;
8
+ headers?: IncomingHttpHeaders;
9
+ body?: any;
10
+ response?: string;
11
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ export interface GetReq {
2
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,5 @@
1
+ import request from "../../models/request";
2
+ export interface GetRes {
3
+ requests: request[];
4
+ enabled: boolean;
5
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ export interface PostDebugDisableReq {
2
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ export interface PostDebugDisableRes {
2
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ export interface PostDebugEnableReq {
2
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ export interface PostDebugEnableRes {
2
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@flink-app/debug-plugin",
3
+ "version": "0.3.3",
4
+ "description": "Flink plugin that make it possbile to debug requests",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\"",
7
+ "prepublish": "tsc"
8
+ },
9
+ "author": "johan@frost.se",
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "license": "MIT",
14
+ "types": "dist/index.d.ts",
15
+ "main": "dist/index.js",
16
+ "dependencies": {
17
+ "@flink-app/management-api-plugin": "^0.3.3",
18
+ "express": "^4.17.1",
19
+ "node-color-log": "^5.3.1"
20
+ },
21
+ "devDependencies": {
22
+ "@flink-app/flink": "^0.3.0",
23
+ "@types/express": "4.17.13",
24
+ "@types/node": "^15.6.2",
25
+ "ts-node": "^9.1.1",
26
+ "typescript": "^4.2.4"
27
+ },
28
+ "gitHead": "31ca42f9020b2cfbc3ecbb7308d683135b35b49e"
29
+ }
package/readme.md ADDED
@@ -0,0 +1,74 @@
1
+ # Flink API Docs
2
+
3
+ A FLINK plugin that makes it possible to debug requests in FLINK.
4
+
5
+ ## Usage
6
+
7
+ Install plugin to your flink app project:
8
+
9
+ ```
10
+ npm i -S @flink-app/debug-plugin
11
+ ```
12
+
13
+ Add and configure plugin in your app startup (probable the `index.ts` in root project):
14
+
15
+ ```
16
+ import { debugPlugin } from '@flink-app/debug-plugin';
17
+
18
+ function start() {
19
+ new FlinkApp<AppContext>({
20
+ name: "My app",
21
+ plugins: [
22
+ // Register plugin
23
+ debugPlugin({ enabledAtStart: false, logToConsole: false, keepLogs : 100 }),
24
+ ],
25
+ }).start();
26
+ }
27
+
28
+ ```
29
+
30
+ ## Use as a management-api-module
31
+
32
+ This plugin can be exposed as a management-api-module and could after setup be used both from the management-api and from flink-admin.
33
+
34
+ To enable management-api capabilities, simply craete a managment module and supply it to the managementApiPlugin.
35
+
36
+
37
+ Example `index.ts`
38
+
39
+ ```
40
+ import { FlinkApp } from '@flink-app/flink';
41
+ import { Ctx } from './Ctx';
42
+ import {
43
+ debugPlugin,
44
+ GetManagementModule as GetDebugManagementModule,
45
+ } from '@flink-app/debug-plugin';
46
+ import { managementApiPlugin } from '@flink-app/management-api-plugin';
47
+
48
+ const debugManagementModule = GetDebugManagementModule({
49
+ ui: true,
50
+ });
51
+
52
+ function start() {
53
+ new FlinkApp<Ctx>({
54
+ name: 'My flink app',
55
+ debug: true,
56
+ db: {
57
+ uri: 'mongodb://localhost:27017/my-flink-app',
58
+ },
59
+ plugins: [
60
+ debugPlugin({ enabledAtStart: false, logToConsole: false, keepLogs : 100 }),
61
+ managementApiPlugin({
62
+ token: '123',
63
+ jwtSecret: '123',
64
+ modules: [debugManagementModule],
65
+ }),
66
+ ],
67
+ }).start();
68
+ }
69
+
70
+ start();
71
+
72
+ ```
73
+
74
+ This way all static files in the `src/public` folder will be copied into dist.
@@ -0,0 +1,19 @@
1
+ import { Handler, HttpMethod, notFound, RouteProps, FlinkContext } from "@flink-app/flink";
2
+ import request from "../../models/request";
3
+
4
+ import { GetReq } from "../../schemas/Debug/GetReq";
5
+ import { GetRes } from "../../schemas/Debug/GetRes";
6
+
7
+ const Get: Handler<FlinkContext, GetReq, GetRes> = async ({ ctx, req, origin }) => {
8
+ let enabled = ctx.plugins.debugPlugin.enabled as boolean;
9
+ let requests = ctx.plugins.debugPlugin.requests as request[];
10
+
11
+ return {
12
+ data: {
13
+ requests,
14
+ enabled,
15
+ },
16
+ status: 200,
17
+ };
18
+ };
19
+ export default Get;
@@ -0,0 +1,13 @@
1
+ import { Handler, HttpMethod, notFound, RouteProps, FlinkContext } from "@flink-app/flink";
2
+
3
+ import { PostDebugDisableReq } from "../../schemas/Debug/PostDisableReq";
4
+ import { PostDebugDisableRes } from "../../schemas/Debug/PostDisableRes";
5
+
6
+ const PostDebugEnable: Handler<FlinkContext, PostDebugDisableReq, PostDebugDisableRes> = async ({ ctx, req, origin }) => {
7
+ ctx.plugins.debugPlugin.enabled = false;
8
+ return {
9
+ data: {},
10
+ status: 200,
11
+ };
12
+ };
13
+ export default PostDebugEnable;
@@ -0,0 +1,13 @@
1
+ import { Handler, HttpMethod, notFound, RouteProps, FlinkContext } from "@flink-app/flink";
2
+
3
+ import { PostDebugEnableReq } from "../../schemas/Debug/PostEnableReq";
4
+ import { PostDebugEnableRes } from "../../schemas/Debug/PostEnableRes";
5
+
6
+ const PostDebugEnable: Handler<FlinkContext, PostDebugEnableReq, PostDebugEnableRes> = async ({ ctx, req, origin }) => {
7
+ ctx.plugins.debugPlugin.enabled = true;
8
+ return {
9
+ data: {},
10
+ status: 200,
11
+ };
12
+ };
13
+ export default PostDebugEnable;
package/src/index.ts ADDED
@@ -0,0 +1,154 @@
1
+ import { FlinkApp, FlinkPlugin, HttpMethod } from "@flink-app/flink";
2
+ import { ManagementApiModule, ManagementApiType } from "@flink-app/management-api-plugin";
3
+ import express from "express";
4
+ import log from "node-color-log";
5
+ import request from "./models/request";
6
+ import * as GetHandler from "./handlers/Debug/Get";
7
+ import * as PostDisableHandler from "./handlers/Debug/PostDisable";
8
+ import * as PostEnableHandler from "./handlers/Debug/PostEnable";
9
+
10
+ export type StaticOptions = {
11
+ /**
12
+ * Base url
13
+ */
14
+ logToConsole: boolean;
15
+ enabledAtStart: boolean;
16
+ keepLogs?: number;
17
+ };
18
+
19
+ export const debugPlugin = (options: StaticOptions): FlinkPlugin => {
20
+ let requests: request[] = [];
21
+ let enabled = options.enabledAtStart;
22
+ return {
23
+ id: "debugPlugin",
24
+ init: (app) => init(app, options),
25
+
26
+ ctx: {
27
+ requests,
28
+ enabled,
29
+ },
30
+ };
31
+ };
32
+
33
+ function init(app: FlinkApp<any>, options: StaticOptions) {
34
+ const { expressApp } = app;
35
+ let keep = 100;
36
+ if (options.keepLogs != null) {
37
+ keep = options.keepLogs;
38
+ }
39
+ if (!expressApp) {
40
+ throw new Error("Express app not initialized");
41
+ }
42
+
43
+ if (options.enabledAtStart) {
44
+ log.info(`Debug enabled`);
45
+ }
46
+
47
+ expressApp.use((req: express.Request, res: express.Response, next: express.NextFunction) => {
48
+ let requests = app.ctx.plugins.debugPlugin.requests as request[];
49
+ let enabled = app.ctx.plugins.debugPlugin.enabled as boolean;
50
+ if (!enabled) {
51
+ if (next) {
52
+ next();
53
+ }
54
+ return;
55
+ }
56
+ if (req.originalUrl.startsWith("/managementapi")) {
57
+ if (next) {
58
+ next();
59
+ }
60
+ return;
61
+ }
62
+ let requestItem: request = {
63
+ start: new Date(),
64
+ method: req.method,
65
+ path: req.originalUrl,
66
+ body: req.body,
67
+ headers: req.headers,
68
+ };
69
+
70
+ requests.unshift(requestItem);
71
+
72
+ requests = requests.splice(0, keep);
73
+ app.ctx.plugins.debugPlugin.requests = requests;
74
+ let oldWrite = res.write;
75
+ let oldEnd = res.end;
76
+ const chunks: Buffer[] = [];
77
+
78
+ (res.write as unknown) = function (chunk: any) {
79
+ chunks.push(Buffer.from(chunk));
80
+ (oldWrite as Function).apply(res, arguments);
81
+ };
82
+
83
+ res.end = function (chunk: any) {
84
+ if (chunk) {
85
+ chunks.push(Buffer.from(chunk));
86
+ }
87
+ const body = Buffer.concat(chunks).toString("utf8");
88
+ requestItem.end = new Date();
89
+ requestItem.response = body;
90
+ if (options.logToConsole) {
91
+ log.debug(requestItem);
92
+ }
93
+ (oldEnd as Function).apply(res, arguments);
94
+ };
95
+ if (next) {
96
+ next();
97
+ }
98
+ });
99
+ }
100
+
101
+ export interface GetManagementModuleConfig {
102
+ pluginId?: string;
103
+ ui: boolean;
104
+ uiSettings?: {
105
+ title: string;
106
+ };
107
+ }
108
+
109
+ export const GetManagementModule = (config: GetManagementModuleConfig): ManagementApiModule => {
110
+ if (config.pluginId == null) config.pluginId = "debug";
111
+
112
+ let endpoints: ManagementApiModule["endpoints"] = [];
113
+ endpoints.push({
114
+ routeProps: {
115
+ path: "/",
116
+ method: HttpMethod.get,
117
+ origin: config.pluginId,
118
+ },
119
+ handler: GetHandler,
120
+ });
121
+ endpoints.push({
122
+ routeProps: {
123
+ path: "/enable",
124
+ method: HttpMethod.post,
125
+ origin: config.pluginId,
126
+ },
127
+ handler: PostEnableHandler,
128
+ });
129
+ endpoints.push({
130
+ routeProps: {
131
+ path: "/disable",
132
+ method: HttpMethod.post,
133
+ origin: config.pluginId,
134
+ },
135
+ handler: PostDisableHandler,
136
+ });
137
+
138
+ let features: string[] = [];
139
+
140
+ let module: ManagementApiModule = {
141
+ id: config.pluginId || "debug",
142
+ uiSettings: {
143
+ title: config.uiSettings?.title || "Debug",
144
+ icon: "",
145
+ features,
146
+ },
147
+ ui: config.ui,
148
+ type: ManagementApiType.debug,
149
+ endpoints: endpoints,
150
+ data: {},
151
+ };
152
+
153
+ return module;
154
+ };
@@ -0,0 +1,12 @@
1
+ import { HttpMethod } from "@flink-app/flink";
2
+ import { IncomingHttpHeaders } from "http";
3
+
4
+ export default interface request {
5
+ start: Date;
6
+ end?: Date;
7
+ method: string;
8
+ path: string;
9
+ headers?: IncomingHttpHeaders;
10
+ body?: any;
11
+ response?: string;
12
+ }
@@ -0,0 +1 @@
1
+ export interface GetReq {}
@@ -0,0 +1,6 @@
1
+ import request from "../../models/request";
2
+
3
+ export interface GetRes {
4
+ requests: request[];
5
+ enabled: boolean;
6
+ }
@@ -0,0 +1 @@
1
+ export interface PostDebugDisableReq {}
@@ -0,0 +1 @@
1
+ export interface PostDebugDisableRes {}
@@ -0,0 +1,3 @@
1
+ export interface PostDebugEnableReq{
2
+
3
+ }
@@ -0,0 +1,3 @@
1
+ export interface PostDebugEnableRes{
2
+
3
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es5",
4
+ "lib": ["esnext", "es2016"],
5
+ "allowJs": true,
6
+ "skipLibCheck": true,
7
+ "esModuleInterop": true,
8
+ "allowSyntheticDefaultImports": true,
9
+ "strict": true,
10
+ "forceConsistentCasingInFileNames": true,
11
+ "module": "commonjs",
12
+ "moduleResolution": "node",
13
+ "resolveJsonModule": true,
14
+ "isolatedModules": true,
15
+ "noEmit": false,
16
+ "declaration": true,
17
+ "experimentalDecorators": true,
18
+ "checkJs": true,
19
+ "outDir": "dist"
20
+ },
21
+ "include": ["./src/*"],
22
+ "exclude": ["./node_modules/*"]
23
+ }