@sebastianwessel/quickjs
Version:
A typescript package to execute JavaScript and TypeScript code in a WebAssembly QuickJS sandbox
234 lines (233 loc) • 9.89 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createVirtualFileSystem = void 0;
const node_fs_1 = require("node:fs");
const node_path_1 = require("node:path");
const node_url_1 = require("node:url");
const memfsPkg = __importStar(require("memfs"));
const assert_js_1 = __importDefault(require("./modules/assert.js"));
const buffer_js_1 = __importDefault(require("./modules/buffer.js"));
const events_js_1 = __importDefault(require("./modules/events.js"));
const fs_js_1 = __importDefault(require("./modules/fs.js"));
const fs_promises_js_1 = __importDefault(require("./modules/fs_promises.js"));
const module_js_1 = __importDefault(require("./modules/module.js"));
const eventTarget_js_1 = __importDefault(require("./modules/nodeCompatibility/eventTarget.js"));
const headers_js_1 = __importDefault(require("./modules/nodeCompatibility/headers.js"));
const request_js_1 = __importDefault(require("./modules/nodeCompatibility/request.js"));
const response_js_1 = __importDefault(require("./modules/nodeCompatibility/response.js"));
const path_js_1 = __importDefault(require("./modules/path.js"));
const process_js_1 = __importDefault(require("./modules/process.js"));
const punycode_js_1 = __importDefault(require("./modules/punycode.js"));
const querystring_js_1 = __importDefault(require("./modules/querystring.js"));
const string_decoder_js_1 = __importDefault(require("./modules/string_decoder.js"));
const timers_js_1 = __importDefault(require("./modules/timers.js"));
const timers_promises_js_1 = __importDefault(require("./modules/timers_promises.js"));
const url_js_1 = __importDefault(require("./modules/url.js"));
const util_js_1 = __importDefault(require("./modules/util.js"));
const getCurrentDirname = () => {
if (typeof __dirname !== 'undefined') {
return __dirname;
}
const stack = new Error().stack ?? '';
const stackUrl = stack.match(/(file:\/\/[^\s)]+createVirtualFileSystem\.[cm]?js)/)?.[1];
if (stackUrl) {
return (0, node_url_1.fileURLToPath)(new URL('.', stackUrl));
}
return process.cwd();
};
const memfsCompat = memfsPkg;
const getMemfs = () => (memfsCompat.memfs ?? memfsCompat.default?.memfs ?? memfsCompat.default);
/**
* Create the virtual file system for the sandbox
* Creates a node_modules folder with packages and ensures the src folder
*
* @param runtimeOptions
* @returns filesystem fs and volume vol
*/
const createVirtualFileSystem = (runtimeOptions = {}) => {
const customFileSystem = runtimeOptions.mountFs;
// setup node_modules folder
const virtualFS = {
'/': {
...customFileSystem,
src: {
// biome-ignore lint/complexity/useLiteralKeys: ok here
...(customFileSystem?.['src'] ? customFileSystem?.['src'] : {}),
},
node_modules: {
...runtimeOptions?.nodeModules,
'@node_compatibility': {
'event-target': eventTarget_js_1.default,
headers: headers_js_1.default,
request: request_js_1.default,
response: response_js_1.default,
},
assert: {
'index.js': assert_js_1.default,
},
async_hooks: {
'index.js': "throw new Error('module async_hooks not implemented')",
},
buffer: {
'index.js': buffer_js_1.default,
},
child_process: {
'index.js': "throw new Error('module child_process not implemented')",
},
cluster: {
'index.js': "throw new Error('module cluster not implemented')",
},
console: {
'index.js': "throw new Error('module console not implemented')",
},
crypto: {
'index.js': "throw new Error('module crypto not implemented')",
},
dgram: {
'index.js': "throw new Error('module dgram not implemented')",
},
dns: {
'index.js': "throw new Error('module dns not implemented')",
},
domain: {
'index.js': "throw new Error('module domain not implemented')",
},
events: {
'index.js': events_js_1.default,
},
fs: {
'index.js': fs_js_1.default,
promises: {
'index.js': fs_promises_js_1.default,
},
},
http: {
'index.js': "throw new Error('module http not implemented')",
},
http2: {
'index.js': "throw new Error('module http2 not implemented')",
},
https: {
'index.js': "throw new Error('module https not implemented')",
},
inspector: {
'index.js': "throw new Error('module inspector not implemented')",
},
module: {
'index.js': module_js_1.default,
},
net: {
'index.js': "throw new Error('module net not implemented')",
},
os: {
'index.js': "throw new Error('module os not implemented')",
},
path: {
'index.js': path_js_1.default,
},
perf_hooks: {
'index.js': "throw new Error('module perf_hooks not implemented')",
},
process: {
'index.js': process_js_1.default,
},
punycode: {
'index.js': punycode_js_1.default,
},
querystring: {
'index.js': querystring_js_1.default,
},
readline: {
'index.js': "throw new Error('module readline not implemented')",
},
repl: {
'index.js': "throw new Error('module repl not implemented')",
},
stream: {
'index.js': "throw new Error('module stream not implemented')",
},
string_decoder: {
'index.js': string_decoder_js_1.default,
},
timers: {
'index.js': timers_js_1.default,
promises: {
'index.js': timers_promises_js_1.default,
},
},
tls: {
'index.js': "throw new Error('module tls not implemented')",
},
trace_events: {
'index.js': "throw new Error('module trace_events not implemented')",
},
tty: {
'index.js': "throw new Error('module tty not implemented')",
},
url: {
'index.js': url_js_1.default,
},
util: {
'index.js': util_js_1.default,
},
v8: {
'index.js': "throw new Error('module v8 not implemented')",
},
vm: {
'index.js': "throw new Error('module vm not implemented')",
},
worker_threads: {
'index.js': "throw new Error('module worker_threads not implemented')",
},
zlib: {
'index.js': "throw new Error('module zlib not implemented')",
},
},
},
};
if (runtimeOptions.enableTestUtils) {
const currentDirname = getCurrentDirname();
virtualFS['/'].node_modules.test = {
'index.js': (0, node_fs_1.readFileSync)((0, node_path_1.join)(currentDirname, 'modules', 'build', 'test-lib.js')),
};
}
const { vol, fs } = getMemfs()(virtualFS, '/');
return { vol, fs };
};
exports.createVirtualFileSystem = createVirtualFileSystem;