powr-sdk-api 4.3.14 โ†’ 4.5.0

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.
@@ -1,229 +0,0 @@
1
- "use strict";
2
-
3
- const {
4
- getDb
5
- } = require("./mongo");
6
- const crypto = require('crypto');
7
- const config = require('../config');
8
- class FunctionsManager {
9
- constructor() {
10
- this.compiledFunctions = new Map();
11
- this.isInitialized = false;
12
- this.isCentralService = false;
13
- this.isEnabled = false; // Default disabled
14
- }
15
- async initialize(options = {}) {
16
- this.isCentralService = options.isCentralService || false;
17
- this.isEnabled = options.enableFunctions === true; // Default false unless explicitly enabled
18
-
19
- if (!this.isEnabled) {
20
- console.log("๐Ÿšซ Functions is disabled");
21
- this.isInitialized = true;
22
- return;
23
- }
24
- if (this.isCentralService) {
25
- // Central service: Don't pre-load, load dynamically
26
- console.log("๐Ÿ”„ Central service mode - loading functions dynamically");
27
- this.isInitialized = true;
28
- } else {
29
- // Individual API: Load only this project's functions
30
- const projectId = config.projectId;
31
- console.log(`๐Ÿ“ฆ Loading functions for project: ${projectId}`);
32
- try {
33
- const db = await getDb();
34
- const routes = await db.collection("functions").find({
35
- projectId
36
- }).toArray();
37
- routes.forEach(route => {
38
- this.compileAndCache(route);
39
- });
40
- this.isInitialized = true;
41
- console.log(`โœ… Pre-compiled ${routes.length} functions for project ${projectId}`);
42
- } catch (error) {
43
- console.error("โŒ Failed to initialize functions:", error);
44
- this.isInitialized = true; // Still mark as initialized to prevent blocking
45
- }
46
- }
47
- }
48
- compileAndCache(route) {
49
- const cacheKey = `${route.projectId}:${route.route}`;
50
- try {
51
- // Security validation
52
- if (!this.validateCode(route.code)) {
53
- console.error(`โŒ Invalid code in function: ${route.route}`);
54
- return;
55
- }
56
-
57
- // Pre-compile function
58
- const compiledFunction = new Function("params", route.code);
59
- this.compiledFunctions.set(cacheKey, {
60
- function: compiledFunction,
61
- metadata: {
62
- route: route.route,
63
- projectId: route.projectId,
64
- compiledAt: new Date(),
65
- codeHash: this.generateHash(route.code)
66
- }
67
- });
68
- console.log(`โœ… Compiled: ${route.route}`);
69
- } catch (error) {
70
- console.error(`โŒ Failed to compile ${route.route}:`, error);
71
- }
72
- }
73
- async execute(projectId, route, params) {
74
- if (!this.isEnabled) {
75
- return {
76
- success: false,
77
- message: "Functions is disabled",
78
- data: null
79
- };
80
- }
81
- const cacheKey = `${projectId}:${route}`;
82
- const cached = this.compiledFunctions.get(cacheKey);
83
- if (!cached) {
84
- if (this.isCentralService) {
85
- // Try to load only the specific function dynamically
86
- await this.loadSpecificRoute(projectId, route);
87
- const retryCached = this.compiledFunctions.get(cacheKey);
88
- if (retryCached) {
89
- return retryCached.function(params);
90
- }
91
- }
92
- return {
93
- success: false,
94
- message: `Function not found: ${route}`,
95
- data: null
96
- };
97
- }
98
- try {
99
- const result = cached.function(params);
100
- return {
101
- success: true,
102
- message: "Function executed successfully",
103
- data: result
104
- };
105
- } catch (error) {
106
- return {
107
- success: false,
108
- message: `Error executing function: ${error.message}`,
109
- data: null,
110
- error: error.message
111
- };
112
- }
113
- }
114
-
115
- // Load specific function for central service
116
- async loadSpecificRoute(projectId, route) {
117
- if (!this.isCentralService) {
118
- throw new Error("Dynamic loading only available in central service mode");
119
- }
120
- try {
121
- const db = await getDb();
122
- const routeData = await db.collection("functions").findOne({
123
- projectId,
124
- route
125
- });
126
- if (routeData) {
127
- this.compileAndCache(routeData);
128
- console.log(`โœ… Loaded function: ${route} for project ${projectId}`);
129
- } else {
130
- console.log(`โŒ Function not found: ${route} for project ${projectId}`);
131
- }
132
- } catch (error) {
133
- console.error(`โŒ Failed to load function ${route} for project ${projectId}:`, error);
134
- throw error;
135
- }
136
- }
137
-
138
- // Dynamic loading for central service
139
- async loadProjectRoutes(projectId) {
140
- if (!this.isCentralService) {
141
- throw new Error("Dynamic loading only available in central service mode");
142
- }
143
- try {
144
- const db = await getDb();
145
- const routes = await db.collection("functions").find({
146
- projectId
147
- }).toArray();
148
- routes.forEach(route => {
149
- this.compileAndCache(route);
150
- });
151
- console.log(`โœ… Loaded ${routes.length} functions for project ${projectId}`);
152
- } catch (error) {
153
- console.error(`โŒ Failed to load functions for project ${projectId}:`, error);
154
- throw error;
155
- }
156
- }
157
-
158
- // Security validation
159
- validateCode(code) {
160
- const dangerousPatterns = [/process\./, /require\(/, /eval\(/, /setTimeout\(/, /setInterval\(/, /global\./, /__dirname/, /__filename/];
161
- return !dangerousPatterns.some(pattern => pattern.test(code));
162
- }
163
-
164
- // Generate hash for change detection
165
- generateHash(code) {
166
- return crypto.createHash('md5').update(code).digest('hex');
167
- }
168
-
169
- // Update function (re-compile)
170
- async updateRoute(projectId, route, newCode) {
171
- const routeData = {
172
- projectId,
173
- route,
174
- code: newCode
175
- };
176
- this.compileAndCache(routeData);
177
-
178
- // Optionally save to database
179
- try {
180
- const db = await getDb();
181
- await db.collection("functions").updateOne({
182
- projectId,
183
- route
184
- }, {
185
- $set: {
186
- code: newCode,
187
- updatedAt: new Date()
188
- }
189
- });
190
- } catch (error) {
191
- console.error(`โŒ Failed to update function in database: ${route}`, error);
192
- }
193
- }
194
-
195
- // Get function statistics
196
- getStats() {
197
- return {
198
- totalFunctions: this.compiledFunctions.size,
199
- isInitialized: this.isInitialized,
200
- isCentralService: this.isCentralService,
201
- isEnabled: this.isEnabled,
202
- cacheKeys: Array.from(this.compiledFunctions.keys())
203
- };
204
- }
205
-
206
- // Clear cache (for testing or maintenance)
207
- clearCache() {
208
- this.compiledFunctions.clear();
209
- console.log("๐Ÿงน Function cache cleared");
210
- }
211
-
212
- // Enable/Disable Functions
213
- enable() {
214
- this.isEnabled = true;
215
- console.log("โœ… Functions enabled");
216
- }
217
- disable() {
218
- this.isEnabled = false;
219
- console.log("๐Ÿšซ Functions disabled");
220
- }
221
-
222
- // Toggle Functions
223
- toggle() {
224
- this.isEnabled = !this.isEnabled;
225
- console.log(this.isEnabled ? "โœ… Functions enabled" : "๐Ÿšซ Functions disabled");
226
- return this.isEnabled;
227
- }
228
- }
229
- module.exports = new FunctionsManager();
@@ -1,229 +0,0 @@
1
- "use strict";
2
-
3
- const {
4
- getDb
5
- } = require("./mongo");
6
- const crypto = require('crypto');
7
- const config = require('../config');
8
- class PlexxManager {
9
- constructor() {
10
- this.compiledFunctions = new Map();
11
- this.isInitialized = false;
12
- this.isCentralService = false;
13
- this.isEnabled = false; // Default disabled
14
- }
15
- async initialize(options = {}) {
16
- this.isCentralService = options.isCentralService || false;
17
- this.isEnabled = options.enablePlexx === true; // Default false unless explicitly enabled
18
-
19
- if (!this.isEnabled) {
20
- console.log("๐Ÿšซ Plexx is disabled");
21
- this.isInitialized = true;
22
- return;
23
- }
24
- if (this.isCentralService) {
25
- // Central service: Don't pre-load, load dynamically
26
- console.log("๐Ÿ”„ Central service mode - loading routes dynamically");
27
- this.isInitialized = true;
28
- } else {
29
- // Individual API: Load only this project's routes
30
- const projectId = config.projectId;
31
- console.log(`๐Ÿ“ฆ Loading routes for project: ${projectId}`);
32
- try {
33
- const db = await getDb();
34
- const routes = await db.collection("routes").find({
35
- projectId
36
- }).toArray();
37
- routes.forEach(route => {
38
- this.compileAndCache(route);
39
- });
40
- this.isInitialized = true;
41
- console.log(`โœ… Pre-compiled ${routes.length} routes for project ${projectId}`);
42
- } catch (error) {
43
- console.error("โŒ Failed to initialize routes:", error);
44
- this.isInitialized = true; // Still mark as initialized to prevent blocking
45
- }
46
- }
47
- }
48
- compileAndCache(route) {
49
- const cacheKey = `${route.projectId}:${route.route}`;
50
- try {
51
- // Security validation
52
- if (!this.validateCode(route.code)) {
53
- console.error(`โŒ Invalid code in route: ${route.route}`);
54
- return;
55
- }
56
-
57
- // Pre-compile function
58
- const compiledFunction = new Function("params", route.code);
59
- this.compiledFunctions.set(cacheKey, {
60
- function: compiledFunction,
61
- metadata: {
62
- route: route.route,
63
- projectId: route.projectId,
64
- compiledAt: new Date(),
65
- codeHash: this.generateHash(route.code)
66
- }
67
- });
68
- console.log(`โœ… Compiled: ${route.route}`);
69
- } catch (error) {
70
- console.error(`โŒ Failed to compile ${route.route}:`, error);
71
- }
72
- }
73
- async execute(projectId, route, params) {
74
- if (!this.isEnabled) {
75
- return {
76
- success: false,
77
- message: "Plexx is disabled",
78
- data: null
79
- };
80
- }
81
- const cacheKey = `${projectId}:${route}`;
82
- const cached = this.compiledFunctions.get(cacheKey);
83
- if (!cached) {
84
- if (this.isCentralService) {
85
- // Try to load only the specific route dynamically
86
- await this.loadSpecificRoute(projectId, route);
87
- const retryCached = this.compiledFunctions.get(cacheKey);
88
- if (retryCached) {
89
- return retryCached.function(params);
90
- }
91
- }
92
- return {
93
- success: false,
94
- message: `Route not found: ${route}`,
95
- data: null
96
- };
97
- }
98
- try {
99
- const result = cached.function(params);
100
- return {
101
- success: true,
102
- message: "Route executed successfully",
103
- data: result
104
- };
105
- } catch (error) {
106
- return {
107
- success: false,
108
- message: `Error executing route: ${error.message}`,
109
- data: null,
110
- error: error.message
111
- };
112
- }
113
- }
114
-
115
- // Load specific route for central service
116
- async loadSpecificRoute(projectId, route) {
117
- if (!this.isCentralService) {
118
- throw new Error("Dynamic loading only available in central service mode");
119
- }
120
- try {
121
- const db = await getDb();
122
- const routeData = await db.collection("routes").findOne({
123
- projectId,
124
- route
125
- });
126
- if (routeData) {
127
- this.compileAndCache(routeData);
128
- console.log(`โœ… Loaded route: ${route} for project ${projectId}`);
129
- } else {
130
- console.log(`โŒ Route not found: ${route} for project ${projectId}`);
131
- }
132
- } catch (error) {
133
- console.error(`โŒ Failed to load route ${route} for project ${projectId}:`, error);
134
- throw error;
135
- }
136
- }
137
-
138
- // Dynamic loading for central service
139
- async loadProjectRoutes(projectId) {
140
- if (!this.isCentralService) {
141
- throw new Error("Dynamic loading only available in central service mode");
142
- }
143
- try {
144
- const db = await getDb();
145
- const routes = await db.collection("routes").find({
146
- projectId
147
- }).toArray();
148
- routes.forEach(route => {
149
- this.compileAndCache(route);
150
- });
151
- console.log(`โœ… Loaded ${routes.length} routes for project ${projectId}`);
152
- } catch (error) {
153
- console.error(`โŒ Failed to load routes for project ${projectId}:`, error);
154
- throw error;
155
- }
156
- }
157
-
158
- // Security validation
159
- validateCode(code) {
160
- const dangerousPatterns = [/process\./, /require\(/, /eval\(/, /setTimeout\(/, /setInterval\(/, /global\./, /__dirname/, /__filename/];
161
- return !dangerousPatterns.some(pattern => pattern.test(code));
162
- }
163
-
164
- // Generate hash for change detection
165
- generateHash(code) {
166
- return crypto.createHash('md5').update(code).digest('hex');
167
- }
168
-
169
- // Update route (re-compile)
170
- async updateRoute(projectId, route, newCode) {
171
- const routeData = {
172
- projectId,
173
- route,
174
- code: newCode
175
- };
176
- this.compileAndCache(routeData);
177
-
178
- // Optionally save to database
179
- try {
180
- const db = await getDb();
181
- await db.collection("routes").updateOne({
182
- projectId,
183
- route
184
- }, {
185
- $set: {
186
- code: newCode,
187
- updatedAt: new Date()
188
- }
189
- });
190
- } catch (error) {
191
- console.error(`โŒ Failed to update route in database: ${route}`, error);
192
- }
193
- }
194
-
195
- // Get route statistics
196
- getStats() {
197
- return {
198
- totalRoutes: this.compiledFunctions.size,
199
- isInitialized: this.isInitialized,
200
- isCentralService: this.isCentralService,
201
- isEnabled: this.isEnabled,
202
- cacheKeys: Array.from(this.compiledFunctions.keys())
203
- };
204
- }
205
-
206
- // Clear cache (for testing or maintenance)
207
- clearCache() {
208
- this.compiledFunctions.clear();
209
- console.log("๐Ÿงน Route cache cleared");
210
- }
211
-
212
- // Enable/Disable Plexx
213
- enable() {
214
- this.isEnabled = true;
215
- console.log("โœ… Plexx enabled");
216
- }
217
- disable() {
218
- this.isEnabled = false;
219
- console.log("๐Ÿšซ Plexx disabled");
220
- }
221
-
222
- // Toggle Plexx
223
- toggle() {
224
- this.isEnabled = !this.isEnabled;
225
- console.log(this.isEnabled ? "โœ… Plexx enabled" : "๐Ÿšซ Plexx disabled");
226
- return this.isEnabled;
227
- }
228
- }
229
- module.exports = new PlexxManager();