MCPcopy Create free account
hub / github.com/subquery/subql / loadChainTypesFromJs

Function loadChainTypesFromJs

packages/node/src/utils/project.ts:81–119  ·  view source on GitHub ↗
(
  filePath: string,
  requireRoot?: string,
)

Source from the content-addressed store, hash-verified

79}
80
81export function loadChainTypesFromJs(
82 filePath: string,
83 requireRoot?: string,
84): unknown {
85 const { base, ext } = path.parse(filePath);
86 const root = requireRoot ?? path.dirname(filePath);
87 const vm = new NodeVM({
88 console: 'redirect',
89 wasm: false,
90 sandbox: {},
91 require: {
92 context: 'sandbox',
93 external: true,
94 builtin: SANDBOX_DEFAULT_BUILTINS,
95 root: root,
96 resolve: (moduleName: string) => {
97 return require.resolve(moduleName, { paths: [root] });
98 },
99 },
100 wrapper: 'commonjs',
101 sourceExtensions: ['js', 'cjs'],
102 });
103 let rawContent: unknown;
104 try {
105 const script = new VMScript(
106 `module.exports = require('${filePath}').default;`,
107 path.join(root, 'sandbox'),
108 ).compile();
109 rawContent = vm.run(script) as unknown;
110 } catch (err) {
111 throw new Error(`\n NodeVM error: ${err}`);
112 }
113 if (rawContent === undefined) {
114 throw new Error(
115 `There was no default export found from required ${base} file`,
116 );
117 }
118 return rawContent;
119}
120
121function dsContainsNonEventHandlers(ds: SubstrateProjectDs): boolean {
122 if (isRuntimeDs(ds)) {

Callers 2

getChainTypesFunction · 0.85
loadChainTypesFunction · 0.85

Calls 1

runMethod · 0.45

Tested by

no test coverage detected