Javet
Javet copied to clipboard
Limits around requireRootDirectory
Hi, I wonder if there are limits around setRequireRootDirectory. I have initialized a nodejs project under /tmp/jyktest.
{
"name": "jyk-test",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"crypto-js": "^4.2.0",
"sharp": "^0.34.2"
}
}
Both libraries are present under node_modules. I then initialized my pool like so
NodeFlags v8Flags = NodeRuntimeOptions.NODE_FLAGS;
v8Flags.setExperimentalPermission(true);
String fsReadAllow = "--allow-fs-read=" + new File(ROOT_DIRECTORY, "jyktest").getAbsolutePath() + "/";
String fsWriteAllow = "--allow-fs-write=" + new File(ROOT_DIRECTORY, "jyktest").getAbsolutePath() + "/";
System.out.println(fsReadAllow);
System.out.println(fsWriteAllow);
v8Flags.setCustomFlags(new String[] { "--allow-addons", "--permission",
fsReadAllow, fsWriteAllow });
JavetEngineConfig javetEngineConfigNode = new JavetEngineConfig();
javetEngineConfigNode.setAllowEval(false);
javetEngineConfigNode.setJSRuntimeType(JSRuntimeType.Node);
javetEngineConfigNode.setPoolMaxSize(JavetOSUtils.getCPUCount());
javetEngineConfigNode.setPoolMinSize(JavetOSUtils.getCPUCount());
javetEngineConfigNode.setAutoSendGCNotification(true);
JavetEnginePool<NodeRuntime> pool = new JavetEnginePool<>(javetEngineConfigNode);
When I tried to execute some js, I get an error. The code to execute the js is:
try (IJavetEngine<NodeRuntime> javetEngine = pool.getEngine()) {
try (NodeRuntime runtime = javetEngine.getV8Runtime()) {
File workingDirectory = new File("/tmp/jyktest");
runtime.getNodeModule(NodeModuleModule.class).setRequireRootDirectory(workingDirectory);
JavetProxyConverter javetProxyConverter = new JavetProxyConverter();
runtime.setConverter(javetProxyConverter);
String customCode = """
const CryptoJS = require('crypto-js');
const message = 'This is a test message.';
const md5Hash = CryptoJS.MD5(message).toString();
md5Hash;
""";
V8Value result = runtime.getExecutor(customCode).execute();
runtime.await();
System.out.println(result);
}
} catch (JavetException e) {
e.printStackTrace();
}
}
I get an error coming from fs_permissions (looks like)
com.caoccao.javet.exceptions.JavetExecutionException: Error: Access to this API has been restricted
at com.caoccao.javet.interop.V8Native.scriptExecute(Native Method)
at com.caoccao.javet.interop.V8Runtime.execute(V8Runtime.java:1089)
at com.caoccao.javet.interop.executors.V8StringExecutor.execute(V8StringExecutor.java:107)
at com.caoccao.javet.interop.IV8Executable.execute(IV8Executable.java:44)
at com.zion.backend.support.JavetTest.main(JavetTest.java:168)
Am I doing something wrong? Or is loading from somewhere not under JavetOSUtils.WORKING_DIRECTORY not supported?
I think it's better to change your working directory to the parent directory of your node_modules.
Please leave a repo with the issue so that I can troubleshoot.