Yep. That "Failed to initialize WebGL" is known issue for us. We already have solution for that.
Looking forward to the next release. Thanks a lot! @bjrmatos
Yep. That "Failed to initialize WebGL" is known issue for us. We already have solution for that.
Looking forward to the next release. Thanks a lot! @bjrmatos
https://github.com/77chuachua/jsreport-test Please try this one.
Sorry! Just realized that repo is private, that might because our company's policy. @bjrmatos
Hi Jan,
I found the root cause. Seems jsreport does not support "require('..');" in new version.
If I add "console.log('error: ', e);" at node_modules@jsreport\jsreport-core\lib\worker\sandbox\requireSandbox.js ln144.
It will print the error message clearly. "Error: Cannot find module '..'"
Loopback use require('..') everywhere.
And Jsreport eat that error message. So, I can only see error "TypeError: test.printTest is not a function".
I had a workaround.
In node_modules@jsreport\jsreport-core\lib\worker\sandbox\isolatedRequire.js ln35. Change the code like this.
let newModuleId = moduleId === '..' ? '../index' : moduleId;
const fullModulePath = resolveFilename(ISOLATED_REQUIRE_RESOLVE_CACHE, requireFromRootDirectory.resolve, newModuleId, { parentModulePath: parentModule?.path })
It is not a solution, but I did not figure out why your isolate require does not support require('..').
Please do provide us a solution. Thank you!
@admin @jan_blaha @bjrmatos
"The allowModules config is duplicated when you use trustUserCode"
-- Yep, I saw that mentioned in upgrade documentation. Upgrade job contains too many errors and I have no idea which one take effect. So, I just accept both in my config file.
I finally locate the error. It is because failed to require a js file. That file is compiled from a typescript file. We had build a demo project to reproduce the issue. https://github.com/Davidliu11/jsreport-test
data\scripts\test\content.js -> require('./data/scripts/test1.js');
test1.js -> require('../../dist/application') Then error occurred: "'uncaughtException: Error when evaluating custom script /scripts/test"
I had required same file in server.js and it works fine.
You can either clone or download the demo and just npm install and npm start. You will see the error.
Our Node version v18.16.0.
Why same js file could be required by normal js file but failed in JSreport helper file? Please do help me. Thank you!
"The jsreport.assets.require should be used primarily to require asset entities. In your case, you should use the normal require."
Yep, you are right. I can only make it work by "jsreport.assets.require" at the very beginning of upgrade. And that might because I did not use "'allowModules': "*"" at the very beginning. It complains about "can not find module" or "does not have permission..." every time. Previously, I use relative path for each script in allowModules array. something like: "./server/test1.js" etc.
Now, I had reverted all the code and just add "'allowModules': "*"" . It will not complain about require issue anymore. Just hung there, I am going to trouble shooting the new issue.
Useful tips while upgrade:
Thank you! @jan_blaha
I did more test today. I can make part of the old code work.
First, allow all modules in config.
'sandbox': {
'allowModules': "*"
},
'trustUserCode': true,
Second, use jsreport.assets.require
in beforeRender
function. Like this.
//const test1 = require('./server/test1.js'); //directly use require here does not work.
async function beforeRender(req, res, done) {
const jsreport = require('jsreport-proxy');
const test1= await jsreport.assets.require('./server/test1.js');
if (test1['test1']) {
test1.test1(req, res, done);
}
}
And I have 3 js files like this: ./server/test1.js, ./server/test2.js, ./server/test3.js
// test1.js
const handler = module.exports;
const test2 = require('./server/test2.js');
handler.test1 = function(req, res, done) {
test2 .test2 ();
}
// test2.js
const handler = module.exports;
const test3 = require('./server/test3.js'); **// It will report test2 .test2 is not a function.**
const test3 = require('./test3.js'); **// It works if I require it like this.**
const _ = require('lodash');
handler.test2 = function() {
test3.printTestData('Hello world');
}
// test3.js
const handler = module.exports;
handler.printTestData = function(var) {
console.log(var);
}
Why I could not require a file from another folder in test2.js? There is not enough error message, I did a lot of testing. And find out this wired issue. And that blocked me since we have lots of code in different folders.
Please help! Thank you!
Hi Jan,
I am trying to upgrade our project from V2 -> V3 or V4.
Although "'trustUserCode': true," and "'allowedFiles': '/.'," were defined in the jsreport config file, I still have to change all the "require" to "jsreport.assets.require". Otherwise, it will throw errors.
I was blocked here for two days. You know, we have couples of custom scripts, and some of the JS files are compiled result of typescript files. It is impossible to change all the files. And sorry, I could not paste our code here.
Is there any way to allow directly "require" a javascript file?
I just noticed that require('lodash') or require('moment') is okay. If they are listed in 'allowModules' array. Does that mean I need to wrap up all my custom code into a module and then require that module instead in my helper js file? Thank you!
@jan_blaha @bjrmatos
Hahaha! Your reply is almost the same as I can expect. However, it is reasonable. The security issue can not be just ignored due to code check tool. I can try to use "overrides" of NPM to upgrade vm2 directly. Maybe we could upgrade to Jsreport V3 as well later.
Thank you! Have a nice day! @jan_blaha
Hi Jan,
vm2 issue: https://gist.github.com/arkark/e9f5cf5782dec8321095be3e52acf5ac
This one seems affect jsreport-core V2. I know V3 is ready, but just as you said in the V3 release note. V2 works fine for us, and we are not going to upgrade to V3 recently. Could you please make a new release of V2 to upgrade vm2 to 3.9.18? Thank you very much!
@bjrmatos , Understood. Unfortunately, it is almost impossible to just pass data page by page. If Jsreport could add some configuration in future, maybe something like output one PDF or output page by page. That would be great.
However, many thanks! I will try to find other way out.