Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid or incompatible cached data (cachedDataRejected) with Vue and Electron #63

Closed
alexsnkr opened this issue Jun 28, 2020 · 18 comments

Comments

@alexsnkr
Copy link

alexsnkr commented Jun 28, 2020

Hi,

I am using Vuejs and Electron using this plugin: https://github.com/nklayman/vue-cli-plugin-electron-builder

I'm trying to protect the electron code using bytenode but I'm running onto the Invalid or incompatible cached data error when running the packaged application.

I have created a basic repository to replicate the error https://github.com/alexsnkr/vue-electron-bytenode

If I run the builder to only generate the package directory I can run electron . from the bundled directory and it runs using the .jsc file just fine, but when I package it for distribution it errors.

I have gone through the issues from people that have ran into the same error, but I've had not luck fixing it.

Any help is appreciated,

Thanks.

@OsamaAbbas
Copy link
Collaborator

Thank you for reporting the issue. I will look into it tonight.

@alexsnkr
Copy link
Author

Hey @OsamaAbbas, did you manage to have a look into this?

@OsamaAbbas
Copy link
Collaborator

I did as promised. However, there are many moving parts here (electron, vue, the whole build system, your own implementation of bytenode protection.. etc). It will take some time to pin where the issue lie exactly.

cachedDataRejected is always because the runtime executable differs from the executable that was used to generate the .jsc file. Also, any JavaScript code that will be run as a «browser javascript» (which is the case with Vue) cannot be compiled by bytenode. Bytenode only deals with «Node.js javascript». Your problem might be here (still not sure because I have not finished reading your code).

If you want to protect your «browser javascript» code, try using NW.js instead of Electron. They have a tool called nwjc that does just that.

@mapleby
Copy link

mapleby commented Jun 30, 2020

I did as promised. However, there are many moving parts here (electron, vue, the whole build system, your own implementation of bytenode protection.. etc). It will take some time to pin where the issue lie exactly.

cachedDataRejected is always because the runtime executable differs from the executable that was used to generate the .jsc file. Also, any JavaScript code that will be run as a «browser javascript» (which is the case with Vue) cannot be compiled by bytenode. Bytenode only deals with «Node.js javascript». Your problem might be here (still not sure because I have not finished reading your code).

If you want to protect your «browser javascript» code, try using NW.js instead of Electron. They have a tool called nwjc that does just that.

I'm also Vue + electron. Recently, I can't run after converting ts to JS

@alexsnkr
Copy link
Author

cachedDataRejected is always because the runtime executable differs from the executable that was used to generate the .jsc file.

I believe this is the issue, there must be something in the build process that changes the executable as it works if I run the application in the bundled directory.

Also, I'm only trying to compile the background electron code, not the frontend Vue code.

@mapleby
Copy link

mapleby commented Jul 1, 2020

cachedDataRejected始终是因为运行时可执行文件不同于用于生成.jsc文件的可执行文件。

我相信这是问题所在,如果我在捆绑目录中运行应用程序,则在构建过程中必须有某些更改可执行文件的方法。

另外,我仅尝试编译背景电子代码,而不是前端Vue代码。

I have solved this problem. You should compile with the same version of electron, not nodejs.
image
Uploading image.png…
byte_compile is js file.

@mapleby
Copy link

mapleby commented Jul 1, 2020

ele

@sagism
Copy link

sagism commented Jul 1, 2020

I'm having the same issue with the simple example from the README:

echo 'console.log("Hello");' | bytenode --compile - > hello.jsc

in the html file loaded into a BrowserWindow;

<script> const bytenode = require('bytenode'); bytenode.runBytecodeFile('./hello.jsc'); </script>

Then in the console:
Uncaught Error: Invalid or incompatible cached data (cachedDataRejected) at runBytecode (/Users/XXXX/projects/YYYY/node_modules/bytenode/index.js:100) at Object.runBytecodeFile (/Users/XXXX/projects/YYYY/node_modules/bytenode/index.js:165) at index.html:16

I am using electron 9.0.4
I insteadd bytenode both globally (so I have the command line bytenode) and locally. Maybe that's the issue?

@s100
Copy link
Contributor

s100 commented Jul 1, 2020

bytenode doesn't run in a web browser!

@mapleby
Copy link

mapleby commented Jul 2, 2020

我在README中的简单示例中也遇到了同样的问题:

echo 'console.log("Hello");' | bytenode --compile - > hello.jsc

在加载到BrowserWindow中的html文件中;

<script> const bytenode = require('bytenode'); bytenode.runBytecodeFile('./hello.jsc'); </script>

然后在控制台中:
Uncaught Error: Invalid or incompatible cached data (cachedDataRejected) at runBytecode (/Users/XXXX/projects/YYYY/node_modules/bytenode/index.js:100) at Object.runBytecodeFile (/Users/XXXX/projects/YYYY/node_modules/bytenode/index.js:165) at index.html:16

我正在使用电子9.0.4
,而不是全局(所以我有命令行字节节点)和局部都使用bytenode。也许这就是问题吗?

1
2
3
4
5
In fact, it can.
My electron:8.3.1.
You should use electronic to compile these files. If it's a render layer, I suggest that you compile them in the render layer. Such a comparison is guaranteed.

@mapleby
Copy link

mapleby commented Jul 2, 2020

const { app, BrowserWindow } = require('electron')
function createWindow () {
// 创建浏览器窗口
let win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})

// 加载index.html文件
win.webContents.openDevTools()
win.loadFile('index.html')
}
app.whenReady().then(createWindow)

const bytenode = require("bytenode"); // 编译二进制文件
let compiledFilename = bytenode.compileFile({
filename: './test.js',
output: './test.jsc' // if omitted, it defaults to '/path/to/your/file.jsc'
});
require("./test.jsc")

window.onload = () => {
const div = document.querySelector("div");

div.innerText = "哈哈"
}

@mapleby
Copy link

mapleby commented Jul 2, 2020

Compile with nodejs. The version of nodejs V8 engine is different from that of electron V8 engine. That's what happened.

@OsamaAbbas
Copy link
Collaborator

I'm having the same issue with the simple example from the README:

echo 'console.log("Hello");' | bytenode --compile - > hello.jsc

in the html file loaded into a BrowserWindow;

<script> const bytenode = require('bytenode'); bytenode.runBytecodeFile('./hello.jsc'); </script>

Then in the console:
Uncaught Error: Invalid or incompatible cached data (cachedDataRejected) at runBytecode (/Users/XXXX/projects/YYYY/node_modules/bytenode/index.js:100) at Object.runBytecodeFile (/Users/XXXX/projects/YYYY/node_modules/bytenode/index.js:165) at index.html:16

I am using electron 9.0.4
I insteadd bytenode both globally (so I have the command line bytenode) and locally. Maybe that's the issue?

As explained by others, if you want to run .jsc file inside an Electron app, you have to generate it using the exact same executable. That's why in all examples (Electron or NW.js) I prefer to do the "compiled-code generation" step using the production environment (using if-you-find-plain-files-please-compile-them-on-the-fly method), then I would delete all plain javascript files and ship the app to the users.


@alexsnkr Have you figured out how to solve the issue?

@OsamaAbbas
Copy link
Collaborator

I have closed this issue due to inactivity. If you have an update or question, feel free to comment.

@petef19
Copy link

petef19 commented May 1, 2021

@mapleby could you please take a look at #134 and tell me if you have a solution for Electron with preload script and Bytenode encoded renderer ?

Thanks !

@yyccQQu
Copy link

yyccQQu commented Aug 25, 2021

comlplie terminal

"complie:main": "bytenode --electron --compile --no-module ./src/renderer/main:pre.js",

then you coule get your exception
main:pre.js-main:pre.jsc

var path = require('path');
var paths = path.join(__dirname, "../", "main:pre.jsc");
console.log(paths);
bytenode.runBytecodeFile(paths);

@OsamaAbbas
Copy link
Collaborator

OsamaAbbas commented Aug 25, 2021

It's always better to open a new issue than replying in an old, closed one.

The --electron flag will use the locally installed Electron (i.e. from your node_modules folder). Make sure that you use the same binary (the same Electron) when you run your app. Otherwise you will hit that error.

@thanhle7
Copy link

thanhle7 commented Aug 17, 2023

const { app, BrowserWindow } = require('electron') function createWindow () { // 创建浏览器窗口 let win = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: true } })

// 加载index.html文件 win.webContents.openDevTools() win.loadFile('index.html') } app.whenReady().then(createWindow)

const bytenode = require("bytenode"); // 编译二进制文件 let compiledFilename = bytenode.compileFile({ filename: './test.js', output: './test.jsc' // if omitted, it defaults to '/path/to/your/file.jsc' }); require("./test.jsc")

window.onload = () => { const div = document.querySelector("div");

div.innerText = "哈哈" }

This is geneous. I use this script in my app's entry script to prepare compiled scripts if they are not available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants