@scout9/app 1.0.0-alpha.0.1.94 → 1.0.0-alpha.0.1.96

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,92 +0,0 @@
1
- import moment from 'moment';
2
- import vm from 'node:vm';
3
- import path from 'node:path';
4
- import fs from 'node:fs';
5
- import {globSync} from 'glob';
6
-
7
- import * as Platform from '../exports.js';
8
-
9
- const allowedModules = {
10
- moment: moment,
11
- platform: Platform
12
- // Add other allowed modules here
13
- }
14
-
15
- function customRequire(folder, moduleName) {
16
- if (allowedModules.hasOwnProperty(moduleName)) {
17
- return allowedModules[moduleName];
18
- }
19
- let modulePath = path.resolve(folder, moduleName);
20
- if (moduleName.startsWith('../') || moduleName.startsWith('./')) {
21
-
22
- // @TODO remove this
23
- if (moduleName.includes('platform/src')) {
24
- return allowedModules['platform'];
25
- }
26
-
27
-
28
- // Relative file path, resolve it relative to the current file
29
- // Use glob to guess the file absolute url
30
- const modulePaths = globSync(`${folder}/**/${moduleName}*`);
31
- if (modulePaths.length !== 1) {
32
- throw new Error(`Could not resolve module "${moduleName}" absolute path`);
33
- }
34
- modulePath = modulePaths[0];
35
- }
36
-
37
- // Check for .js extension @TODO idk if we need to do this
38
- const resolvedPath = modulePath.endsWith('.js') ? modulePath : `${modulePath}.js`;
39
-
40
- // Read and execute the module file
41
- const moduleCode = fs.readFileSync(resolvedPath, 'utf8');
42
- const moduleScript = new vm.Script(moduleCode, { filename: moduleName });
43
-
44
- // Prepare a module-specific context
45
- const moduleExports = {};
46
- const moduleContext = vm.createContext({
47
- require: (name) => customRequire(folder, name),
48
- console,
49
- module: { exports: moduleExports },
50
- exports: moduleExports,
51
- __filename: resolvedPath,
52
- __dirname: path.dirname(resolvedPath),
53
- });
54
-
55
- try {
56
- moduleScript.runInContext(moduleContext);
57
- } catch (e) {
58
- throw e;
59
- }
60
-
61
- return moduleContext.module.exports;
62
- }
63
-
64
- export async function runInVM(
65
- event,
66
- {src, filePath, fileName}
67
- ) {
68
- // Prepare the script context
69
- const scriptExports = {};
70
- const context = vm.createContext({
71
- require: (moduleName) => customRequire(src, moduleName),
72
- console,
73
- module: { exports: scriptExports },
74
- exports: scriptExports,
75
- __filename: filePath,
76
- __dirname: path.dirname(filePath),
77
- });
78
-
79
- const scriptCode = fs.readFileSync(filePath, 'utf8');
80
- const entryScript = new vm.Script(scriptCode, { filename: fileName });
81
-
82
- // Run the script in the VM
83
- entryScript.runInContext(context);
84
-
85
- const exportedFunction = context.module.exports.default;
86
-
87
- if (typeof exportedFunction !== 'function') {
88
- throw new Error(`The script did not export a function`);
89
- }
90
-
91
- return exportedFunction(event);
92
- }