@intlayer/config
Version:
Retrieve Intlayer configurations and manage environment variables for both server-side and client-side environments.
102 lines (100 loc) • 3.1 kB
JavaScript
import { getProjectRequire } from "../utils/ESMxCJSHelpers.mjs";
import { loadEnvFile } from "../loadEnvFile.mjs";
import { runInNewContext } from "node:vm";
import * as esbuild from "esbuild";
//#region src/loadExternalFile/parseFileContent.ts
const NODE_GLOBALS = [
"Buffer",
"setTimeout",
"clearTimeout",
"setInterval",
"clearInterval",
"setImmediate",
"clearImmediate",
"queueMicrotask",
"URL",
"URLSearchParams",
"TextEncoder",
"TextDecoder",
"AbortController",
"AbortSignal",
"performance",
"fetch",
"crypto",
"structuredClone"
];
const getSandBoxContext = (options) => {
const { envVarOptions, projectRequire, additionalEnvVars, mocks, aliases } = options ?? {};
let additionalGlobalVar = {};
const baseRequire = typeof projectRequire === "function" ? projectRequire : getProjectRequire();
const mockedRequire = (() => {
const mockTable = Object.assign({ esbuild }, mocks);
const aliasTable = Object.assign({}, aliases);
const wrappedRequire = function mockableRequire(id) {
const target = aliasTable?.[id] ? aliasTable[id] : id;
if (mockTable && Object.hasOwn(mockTable, target)) return mockTable[target];
if (target !== id && mockTable && Object.hasOwn(mockTable, id)) return mockTable[id];
return baseRequire(target);
};
wrappedRequire.resolve = baseRequire.resolve.bind(baseRequire);
wrappedRequire.main = baseRequire.main;
wrappedRequire.extensions = baseRequire.extensions;
wrappedRequire.cache = baseRequire.cache;
return wrappedRequire;
})();
try {
additionalGlobalVar = { React: baseRequire("react") };
} catch (_err) {
additionalGlobalVar = { React: {
createElement: (type, props, ...children) => ({
type,
props: {
...props,
children: children.length <= 1 ? children[0] : children
}
}),
Fragment: Symbol.for("react.fragment")
} };
}
const sandboxContext = {
exports: { default: {} },
module: { exports: {} },
process: {
...process,
env: {
...process.env,
...loadEnvFile(envVarOptions),
...additionalEnvVars
}
},
console,
require: mockedRequire,
...additionalGlobalVar
};
for (const key of NODE_GLOBALS) if (!(key in sandboxContext) && key in globalThis) sandboxContext[key] = globalThis[key];
return sandboxContext;
};
const parseFileContent = (fileContentString, options) => {
const sandboxContext = getSandBoxContext(options);
runInNewContext(`"use strict";\n${fileContentString}`, sandboxContext);
const candidates = [
sandboxContext.exports?.default,
sandboxContext.module?.exports?.defaults,
sandboxContext.module?.exports?.default,
sandboxContext.module?.exports
];
let result;
for (const candidate of candidates) if (candidate && typeof candidate === "object" && Object.keys(candidate).length > 0) {
result = candidate;
break;
}
sandboxContext.require = void 0;
sandboxContext.process = void 0;
sandboxContext.React = void 0;
sandboxContext.module = void 0;
sandboxContext.exports = void 0;
return result;
};
//#endregion
export { getSandBoxContext, parseFileContent };
//# sourceMappingURL=parseFileContent.mjs.map