UNPKG

@sebastianwessel/quickjs

Version:

A typescript package to execute JavaScript and TypeScript code in a WebAssembly QuickJS sandbox

35 lines (34 loc) 1.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAsyncModuleLoader = void 0; const node_path_1 = require("node:path"); const getAsyncModuleLoader = (fs, _runtimeOptions) => { const moduleLoader = (inputName, _context) => { let name = inputName; // if it does not exist if (!fs.existsSync(name)) { // try to add the .js extension if (fs.existsSync(`${name}.js`)) { name = `${name}.js`; } else { return { error: new Error(`Module '${inputName}' not installed or available`) }; } } // if it is a folder, we need to use the index.js file if (fs.lstatSync(name).isDirectory()) { name = (0, node_path_1.join)(name, 'index.js'); if (!fs.existsSync(name)) { return { error: new Error(`Module '${inputName}' not installed or available`) }; } } // workaround: as we can not provide the real import.meta.url functionality, we replace it dynamically with the current value string const value = fs.readFileSync(name)?.toString().replaceAll('import.meta.url', `'file://${name}'`); if (!value) { return { error: new Error(`Module '${name}' not installed or available`) }; } return { value }; }; return moduleLoader; }; exports.getAsyncModuleLoader = getAsyncModuleLoader;