@memberjunction/code-execution 2.108.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.
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Library Provider for isolated-vm
3
+ *
4
+ * This module provides allowlisted utility libraries to sandboxed code.
5
+ * Libraries are provided as inline implementations or bundled source code.
6
+ *
7
+ * SECURITY NOTE: Only add libraries here that are safe for untrusted code execution.
8
+ */
9
+ /**
10
+ * Allowed module names
11
+ */
12
+ export declare const ALLOWED_MODULES: readonly ["lodash", "date-fns", "uuid", "validator"];
13
+ export type AllowedModule = typeof ALLOWED_MODULES[number];
14
+ /**
15
+ * Check if a module is allowed
16
+ */
17
+ export declare function isModuleAllowed(moduleName: string): moduleName is AllowedModule;
18
+ /**
19
+ * Get list of all allowed module names
20
+ */
21
+ export declare function getAllowedModuleNames(): string[];
22
+ /**
23
+ * Get the source code for a library module
24
+ * Returns JavaScript code that evaluates to the module exports
25
+ */
26
+ export declare function getLibrarySource(moduleName: string): string | null;
27
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/libraries/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH;;GAEG;AACH,eAAO,MAAM,eAAe,sDAKlB,CAAC;AAEX,MAAM,MAAM,aAAa,GAAG,OAAO,eAAe,CAAC,MAAM,CAAC,CAAC;AAE3D;;GAEG;AACH,wBAAgB,eAAe,CAAC,UAAU,EAAE,MAAM,GAAG,UAAU,IAAI,aAAa,CAE/E;AAED;;GAEG;AACH,wBAAgB,qBAAqB,IAAI,MAAM,EAAE,CAEhD;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAiBlE"}
@@ -0,0 +1,355 @@
1
+ "use strict";
2
+ /**
3
+ * Library Provider for isolated-vm
4
+ *
5
+ * This module provides allowlisted utility libraries to sandboxed code.
6
+ * Libraries are provided as inline implementations or bundled source code.
7
+ *
8
+ * SECURITY NOTE: Only add libraries here that are safe for untrusted code execution.
9
+ */
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ exports.getLibrarySource = exports.getAllowedModuleNames = exports.isModuleAllowed = exports.ALLOWED_MODULES = void 0;
12
+ /**
13
+ * Allowed module names
14
+ */
15
+ exports.ALLOWED_MODULES = [
16
+ 'lodash',
17
+ 'date-fns',
18
+ 'uuid',
19
+ 'validator'
20
+ ];
21
+ /**
22
+ * Check if a module is allowed
23
+ */
24
+ function isModuleAllowed(moduleName) {
25
+ return exports.ALLOWED_MODULES.includes(moduleName);
26
+ }
27
+ exports.isModuleAllowed = isModuleAllowed;
28
+ /**
29
+ * Get list of all allowed module names
30
+ */
31
+ function getAllowedModuleNames() {
32
+ return [...exports.ALLOWED_MODULES];
33
+ }
34
+ exports.getAllowedModuleNames = getAllowedModuleNames;
35
+ /**
36
+ * Get the source code for a library module
37
+ * Returns JavaScript code that evaluates to the module exports
38
+ */
39
+ function getLibrarySource(moduleName) {
40
+ if (!isModuleAllowed(moduleName)) {
41
+ return null;
42
+ }
43
+ switch (moduleName) {
44
+ case 'lodash':
45
+ return getLodashSource();
46
+ case 'date-fns':
47
+ return getDateFnsSource();
48
+ case 'uuid':
49
+ return getUuidSource();
50
+ case 'validator':
51
+ return getValidatorSource();
52
+ default:
53
+ return null;
54
+ }
55
+ }
56
+ exports.getLibrarySource = getLibrarySource;
57
+ /**
58
+ * Lodash subset implementation
59
+ * Provides most commonly used lodash functions inline
60
+ */
61
+ function getLodashSource() {
62
+ return `
63
+ (function() {
64
+ const _ = {
65
+ // Array functions
66
+ chunk: function(array, size = 1) {
67
+ const result = [];
68
+ for (let i = 0; i < array.length; i += size) {
69
+ result.push(array.slice(i, i + size));
70
+ }
71
+ return result;
72
+ },
73
+
74
+ compact: function(array) {
75
+ return array.filter(Boolean);
76
+ },
77
+
78
+ flatten: function(array) {
79
+ return array.flat();
80
+ },
81
+
82
+ uniq: function(array) {
83
+ return [...new Set(array)];
84
+ },
85
+
86
+ difference: function(array, ...values) {
87
+ const others = new Set(values.flat());
88
+ return array.filter(x => !others.has(x));
89
+ },
90
+
91
+ intersection: function(...arrays) {
92
+ if (arrays.length === 0) return [];
93
+ const first = new Set(arrays[0]);
94
+ return arrays[0].filter(x => arrays.every(arr => arr.includes(x)));
95
+ },
96
+
97
+ union: function(...arrays) {
98
+ return [...new Set(arrays.flat())];
99
+ },
100
+
101
+ zip: function(...arrays) {
102
+ const maxLength = Math.max(...arrays.map(a => a.length));
103
+ return Array.from({ length: maxLength }, (_, i) => arrays.map(arr => arr[i]));
104
+ },
105
+
106
+ // Collection functions
107
+ groupBy: function(collection, iteratee) {
108
+ const fn = typeof iteratee === 'function' ? iteratee : x => x[iteratee];
109
+ return collection.reduce((result, item) => {
110
+ const key = fn(item);
111
+ (result[key] = result[key] || []).push(item);
112
+ return result;
113
+ }, {});
114
+ },
115
+
116
+ keyBy: function(collection, iteratee) {
117
+ const fn = typeof iteratee === 'function' ? iteratee : x => x[iteratee];
118
+ return collection.reduce((result, item) => {
119
+ result[fn(item)] = item;
120
+ return result;
121
+ }, {});
122
+ },
123
+
124
+ sortBy: function(collection, iteratees) {
125
+ const fns = Array.isArray(iteratees) ? iteratees : [iteratees];
126
+ return [...collection].sort((a, b) => {
127
+ for (const fn of fns) {
128
+ const iteratee = typeof fn === 'function' ? fn : x => x[fn];
129
+ const aVal = iteratee(a);
130
+ const bVal = iteratee(b);
131
+ if (aVal !== bVal) return aVal < bVal ? -1 : 1;
132
+ }
133
+ return 0;
134
+ });
135
+ },
136
+
137
+ // Object functions
138
+ pick: function(object, ...paths) {
139
+ const keys = paths.flat();
140
+ return keys.reduce((result, key) => {
141
+ if (key in object) result[key] = object[key];
142
+ return result;
143
+ }, {});
144
+ },
145
+
146
+ omit: function(object, ...paths) {
147
+ const keys = new Set(paths.flat());
148
+ return Object.keys(object).reduce((result, key) => {
149
+ if (!keys.has(key)) result[key] = object[key];
150
+ return result;
151
+ }, {});
152
+ },
153
+
154
+ mapValues: function(object, iteratee) {
155
+ const fn = typeof iteratee === 'function' ? iteratee : () => iteratee;
156
+ return Object.keys(object).reduce((result, key) => {
157
+ result[key] = fn(object[key], key, object);
158
+ return result;
159
+ }, {});
160
+ },
161
+
162
+ // Utility functions
163
+ get: function(object, path, defaultValue) {
164
+ const paths = Array.isArray(path) ? path : path.split('.');
165
+ let result = object;
166
+ for (const p of paths) {
167
+ result = result?.[p];
168
+ if (result === undefined) return defaultValue;
169
+ }
170
+ return result;
171
+ },
172
+
173
+ set: function(object, path, value) {
174
+ const paths = Array.isArray(path) ? path : path.split('.');
175
+ let current = object;
176
+ for (let i = 0; i < paths.length - 1; i++) {
177
+ const p = paths[i];
178
+ current[p] = current[p] || {};
179
+ current = current[p];
180
+ }
181
+ current[paths[paths.length - 1]] = value;
182
+ return object;
183
+ },
184
+
185
+ sum: function(array) {
186
+ return array.reduce((sum, n) => sum + n, 0);
187
+ },
188
+
189
+ sumBy: function(array, iteratee) {
190
+ const fn = typeof iteratee === 'function' ? iteratee : x => x[iteratee];
191
+ return array.reduce((sum, item) => sum + fn(item), 0);
192
+ },
193
+
194
+ mean: function(array) {
195
+ return array.length ? _.sum(array) / array.length : 0;
196
+ },
197
+
198
+ cloneDeep: function(value) {
199
+ return JSON.parse(JSON.stringify(value));
200
+ }
201
+ };
202
+
203
+ return _;
204
+ })()
205
+ `.trim();
206
+ }
207
+ /**
208
+ * date-fns subset implementation
209
+ * Provides essential date manipulation functions
210
+ */
211
+ function getDateFnsSource() {
212
+ return `
213
+ (function() {
214
+ const dateFns = {
215
+ format: function(date, formatStr) {
216
+ const d = new Date(date);
217
+ const tokens = {
218
+ 'yyyy': d.getFullYear(),
219
+ 'MM': String(d.getMonth() + 1).padStart(2, '0'),
220
+ 'dd': String(d.getDate()).padStart(2, '0'),
221
+ 'HH': String(d.getHours()).padStart(2, '0'),
222
+ 'mm': String(d.getMinutes()).padStart(2, '0'),
223
+ 'ss': String(d.getSeconds()).padStart(2, '0')
224
+ };
225
+ return formatStr.replace(/yyyy|MM|dd|HH|mm|ss/g, match => tokens[match]);
226
+ },
227
+
228
+ addDays: function(date, days) {
229
+ const d = new Date(date);
230
+ d.setDate(d.getDate() + days);
231
+ return d;
232
+ },
233
+
234
+ subDays: function(date, days) {
235
+ return dateFns.addDays(date, -days);
236
+ },
237
+
238
+ addMonths: function(date, months) {
239
+ const d = new Date(date);
240
+ d.setMonth(d.getMonth() + months);
241
+ return d;
242
+ },
243
+
244
+ differenceInDays: function(dateLeft, dateRight) {
245
+ const msPerDay = 1000 * 60 * 60 * 24;
246
+ return Math.floor((new Date(dateLeft) - new Date(dateRight)) / msPerDay);
247
+ },
248
+
249
+ isAfter: function(date, dateToCompare) {
250
+ return new Date(date) > new Date(dateToCompare);
251
+ },
252
+
253
+ isBefore: function(date, dateToCompare) {
254
+ return new Date(date) < new Date(dateToCompare);
255
+ },
256
+
257
+ startOfDay: function(date) {
258
+ const d = new Date(date);
259
+ d.setHours(0, 0, 0, 0);
260
+ return d;
261
+ },
262
+
263
+ endOfDay: function(date) {
264
+ const d = new Date(date);
265
+ d.setHours(23, 59, 59, 999);
266
+ return d;
267
+ },
268
+
269
+ parseISO: function(dateString) {
270
+ return new Date(dateString);
271
+ }
272
+ };
273
+
274
+ return dateFns;
275
+ })()
276
+ `.trim();
277
+ }
278
+ /**
279
+ * uuid implementation
280
+ * Provides UUID v4 generation
281
+ */
282
+ function getUuidSource() {
283
+ return `
284
+ (function() {
285
+ const uuid = {
286
+ v4: function() {
287
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
288
+ const r = Math.random() * 16 | 0;
289
+ const v = c === 'x' ? r : (r & 0x3 | 0x8);
290
+ return v.toString(16);
291
+ });
292
+ }
293
+ };
294
+
295
+ return uuid;
296
+ })()
297
+ `.trim();
298
+ }
299
+ /**
300
+ * validator subset implementation
301
+ * Provides common validation functions
302
+ */
303
+ function getValidatorSource() {
304
+ return `
305
+ (function() {
306
+ const validator = {
307
+ isEmail: function(str) {
308
+ const emailRegex = /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/;
309
+ return emailRegex.test(str);
310
+ },
311
+
312
+ isURL: function(str) {
313
+ try {
314
+ new URL(str);
315
+ return true;
316
+ } catch {
317
+ return false;
318
+ }
319
+ },
320
+
321
+ isNumeric: function(str) {
322
+ return /^-?\\d+(\\.\\d+)?$/.test(str);
323
+ },
324
+
325
+ isAlpha: function(str) {
326
+ return /^[A-Za-z]+$/.test(str);
327
+ },
328
+
329
+ isAlphanumeric: function(str) {
330
+ return /^[A-Za-z0-9]+$/.test(str);
331
+ },
332
+
333
+ isEmpty: function(str) {
334
+ return str.length === 0;
335
+ },
336
+
337
+ isLength: function(str, options) {
338
+ const len = str.length;
339
+ return (!options.min || len >= options.min) && (!options.max || len <= options.max);
340
+ },
341
+
342
+ isIn: function(str, values) {
343
+ return values.includes(str);
344
+ },
345
+
346
+ matches: function(str, pattern) {
347
+ return new RegExp(pattern).test(str);
348
+ }
349
+ };
350
+
351
+ return validator;
352
+ })()
353
+ `.trim();
354
+ }
355
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/libraries/index.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;AAEH;;GAEG;AACU,QAAA,eAAe,GAAG;IAC3B,QAAQ;IACR,UAAU;IACV,MAAM;IACN,WAAW;CACL,CAAC;AAIX;;GAEG;AACH,SAAgB,eAAe,CAAC,UAAkB;IAC9C,OAAO,uBAAe,CAAC,QAAQ,CAAC,UAA2B,CAAC,CAAC;AACjE,CAAC;AAFD,0CAEC;AAED;;GAEG;AACH,SAAgB,qBAAqB;IACjC,OAAO,CAAC,GAAG,uBAAe,CAAC,CAAC;AAChC,CAAC;AAFD,sDAEC;AAED;;;GAGG;AACH,SAAgB,gBAAgB,CAAC,UAAkB;IAC/C,IAAI,CAAC,eAAe,CAAC,UAAU,CAAC,EAAE,CAAC;QAC/B,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,QAAQ,UAAU,EAAE,CAAC;QACjB,KAAK,QAAQ;YACT,OAAO,eAAe,EAAE,CAAC;QAC7B,KAAK,UAAU;YACX,OAAO,gBAAgB,EAAE,CAAC;QAC9B,KAAK,MAAM;YACP,OAAO,aAAa,EAAE,CAAC;QAC3B,KAAK,WAAW;YACZ,OAAO,kBAAkB,EAAE,CAAC;QAChC;YACI,OAAO,IAAI,CAAC;IACpB,CAAC;AACL,CAAC;AAjBD,4CAiBC;AAED;;;GAGG;AACH,SAAS,eAAe;IACpB,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KA+IN,CAAC,IAAI,EAAE,CAAC;AACb,CAAC;AAED;;;GAGG;AACH,SAAS,gBAAgB;IACrB,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KAgEN,CAAC,IAAI,EAAE,CAAC;AACb,CAAC;AAED;;;GAGG;AACH,SAAS,aAAa;IAClB,OAAO;;;;;;;;;;;;;;KAcN,CAAC,IAAI,EAAE,CAAC;AACb,CAAC;AAED;;;GAGG;AACH,SAAS,kBAAkB;IACvB,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KAiDN,CAAC,IAAI,EAAE,CAAC;AACb,CAAC"}
@@ -0,0 +1,95 @@
1
+ /**
2
+ * Parameters for code execution
3
+ */
4
+ export interface CodeExecutionParams {
5
+ /**
6
+ * The code to execute
7
+ */
8
+ code: string;
9
+ /**
10
+ * Programming language (currently only 'javascript' supported)
11
+ */
12
+ language: 'javascript';
13
+ /**
14
+ * Input data available to the code via 'input' variable
15
+ */
16
+ inputData?: any;
17
+ /**
18
+ * Maximum execution time in seconds (default: 30)
19
+ */
20
+ timeoutSeconds?: number;
21
+ /**
22
+ * Memory limit in MB (default: 128)
23
+ * Note: Memory limiting requires Node.js flags, enforced at process level
24
+ */
25
+ memoryLimitMB?: number;
26
+ }
27
+ /**
28
+ * Result of code execution
29
+ */
30
+ export interface CodeExecutionResult {
31
+ /**
32
+ * Whether execution was successful
33
+ */
34
+ success: boolean;
35
+ /**
36
+ * The output value set by the code (value of 'output' variable)
37
+ */
38
+ output?: any;
39
+ /**
40
+ * Console logs captured during execution
41
+ */
42
+ logs?: string[];
43
+ /**
44
+ * Error message if execution failed
45
+ */
46
+ error?: string;
47
+ /**
48
+ * Type of error that occurred
49
+ */
50
+ errorType?: 'TIMEOUT' | 'MEMORY_LIMIT' | 'SYNTAX_ERROR' | 'RUNTIME_ERROR' | 'SECURITY_ERROR';
51
+ /**
52
+ * Execution time in milliseconds
53
+ */
54
+ executionTimeMs?: number;
55
+ }
56
+ /**
57
+ * Options for JavaScript sandbox execution
58
+ */
59
+ export interface JavaScriptExecutionOptions {
60
+ /**
61
+ * Timeout in seconds
62
+ */
63
+ timeout: number;
64
+ /**
65
+ * Memory limit in MB
66
+ */
67
+ memoryLimit: number;
68
+ /**
69
+ * List of allowed npm packages that can be required
70
+ */
71
+ allowedLibraries?: string[];
72
+ }
73
+ /**
74
+ * Sandbox context provided to executed code
75
+ */
76
+ export interface SandboxContext {
77
+ /**
78
+ * Input data for the code
79
+ */
80
+ input: any;
81
+ /**
82
+ * Output data set by the code
83
+ */
84
+ output: any;
85
+ /**
86
+ * Console API for logging
87
+ */
88
+ console: {
89
+ log: (...args: any[]) => void;
90
+ error: (...args: any[]) => void;
91
+ warn: (...args: any[]) => void;
92
+ info: (...args: any[]) => void;
93
+ };
94
+ }
95
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.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EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;QAC/B,IAAI,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;KAClC,CAAC;CACL"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@memberjunction/code-execution",
3
+ "version": "2.108.0",
4
+ "description": "Sandboxed code execution service for MemberJunction actions and agents",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc",
9
+ "watch": "tsc --watch"
10
+ },
11
+ "dependencies": {
12
+ "@memberjunction/core": "2.100.3",
13
+ "@memberjunction/global": "2.100.3",
14
+ "isolated-vm": "^5.0.1",
15
+ "lodash": "^4.17.21",
16
+ "date-fns": "^3.0.0",
17
+ "mathjs": "^12.0.0",
18
+ "papaparse": "^5.4.1",
19
+ "uuid": "^9.0.1",
20
+ "validator": "^13.11.0"
21
+ },
22
+ "devDependencies": {
23
+ "@types/lodash": "^4.14.202",
24
+ "@types/node": "^20.10.0",
25
+ "@types/papaparse": "^5.3.14",
26
+ "@types/uuid": "^9.0.7",
27
+ "@types/validator": "^13.11.7",
28
+ "typescript": "^5.3.3"
29
+ },
30
+ "keywords": [
31
+ "memberjunction",
32
+ "code-execution",
33
+ "sandbox",
34
+ "javascript",
35
+ "actions"
36
+ ],
37
+ "author": "MemberJunction.com",
38
+ "license": "MIT"
39
+ }