Comments (15)
Please provide a reproducible example for your issue.
from bytenode.
@OsamaAbbas It is difficult for me to explain my situation, but I will share a similar situation as an example.
The following code causes the Electron project compiled with Bytenode to close:
const test = null;
const example = test.obj.example;
from bytenode.
I cannot reproduce the issue.
How do you use bytenode with electron? Can you make a very simple setup using the getting-started electron example?
from bytenode.
Sure. I use it like this:
"use strict";
const bytenode = require("bytenode");
const fs = require("fs");
const v8 = require("v8");
const path = require("path");
v8.setFlagsFromString("--no-lazy");
if (!fs.existsSync(path.join(__dirname, "./background.jsc"))) {
bytenode.compileFile(
path.join(__dirname, "./background.src.js"),
path.join(__dirname, "./background.jsc")
);
}
require(path.join(__dirname, "./background.jsc"));
from bytenode.
Here is my complete setup that can be run by: npm install
, npm run compile
, and npm run start
.
// package.json
{
"name": "electron-issue-212",
"version": "1.0.0",
"description": "",
"private": true,
"main": "main.js",
"scripts": {
"start": "electron .",
"compile": "bytenode -c -e protected.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bytenode": "^1.4.0",
"electron": "^24.0.0"
}
}
// main.js
'use strict';
const path = require('path');
const { app, BrowserWindow } = require('electron');
const createWindow = () => {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true,
contextIsolation: false,
sandbox: false
}
});
win.loadFile('index.html');
};
app.whenReady().then(() => {
createWindow();
});
<!-- index.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
We are using Node.js <span id="node-version"></span>,
Chromium <span id="chrome-version"></span>,
and Electron <span id="electron-version"></span>.
</body>
</html>
// preload.js
'use strict';
require('bytenode');
// run the source file
// require('./protected.js')(window);
// run the compiled file
require('./protected.jsc')(window);
// protected.js
'use strict';
module.exports = function (window) {
window.addEventListener('DOMContentLoaded', () => {
const replaceText = (selector, text) => {
const element = document.getElementById(selector);
if (element) element.innerText = text;
};
for (const dependency of ['chrome', 'node', 'electron']) {
replaceText(`${dependency}-version`, process.versions[dependency]);
}
// here is your code
const test = null;
const example = test.obj.example;
});
};
Now, if you run the app and open the Developer Tools, you will find the uncaught TypeError
in the console. The program does not crash. It's the same outcome with protected.jsc
or protected.js
files.
from bytenode.
Thanks for your example, I'll try it right away. I don't just use electron, I use this:
https://blog.logrocket.com/building-app-electron-vue/
Is there any difference between them?
from bytenode.
Well, there is no "standard" way to integrate bytenode with electron. Let me check and see how to protect your background.js
file.
In that setup, are there any other files or components you want to protect?
from bytenode.
I did not fully explain the problem. I will explain all of them now. When I get the build, the whole electron project is in the background.js file.
Before I get the build in the Vue.config.js file, I run the build function I wrote for the bytenode.
The working logic compiles the background.js file with Bytenode. In order to run the resulting background.jsc file, it creates a background.js file again and writes the following code in it:
"use strict";
const bytenode = require("bytenode");
const fs = require("fs");
const v8 = require("v8");
const path = require("path");
v8.setFlagsFromString("--no-lazy");
if (!fs.existsSync(path.join(__dirname, "./background.jsc"))) {
bytenode.compileFile(
path.join(__dirname, "./background.src.js"),
path.join(__dirname, "./background.jsc")
);
}
require(path.join(__dirname, "./background.jsc"));
The problem I'm having is only valid for build. And I don't have this problem in renderer. I just live in the backend. You should try the example I gave for you in the backend, not the renderer. And you should try it after you get the build. In dev mode it prints the error to the screen and there is no problem. But when I build, the program crashes directly.
Change the example you gave me above to:
// main.js
'use strict';
const path = require('path');
const { app, BrowserWindow } = require('electron');
const createWindow = () => {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true,
contextIsolation: false,
sandbox: false
}
});
win.loadFile('index.html');
const test = null; // Exception
const example = test.obj.example; // Exception
};
app.whenReady().then(() => {
createWindow();
});
Get Build and run the program. Your program will most likely crash.
from bytenode.
Again, I cannot reproduce the issue.
Here are the changes on my example:
// package.json
...
"main": "main.jsc",
"scripts": {
"start": "electron -r bytenode .",
"compile": "bytenode -c -e main.js protected.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
...
'use strict';
const path = require('path');
const { app, BrowserWindow } = require('electron');
const createWindow = () => {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true,
contextIsolation: false,
sandbox: false
}
});
win.loadFile('index.html');
const test = null;
const example = test.obj.example;
};
app.whenReady().then(() => {
createWindow();
});
Run npm run compile
and npm run start
. Both main.js
and main.jsc
have the same outcome. The error is logged on my terminal, no crashes.
from bytenode.
So it's clearly specific to the vue+electron setup. I will have a look and get back to you. Probably tomorrow though.
from bytenode.
I have identified the problem. When it is inside an async function, the program is closing. @OsamaAbbas
from bytenode.
There is no problem when ipcMain is not asynchronous:
ipcMain.on('testEvent', (_event) => {
const test = null;
const example = test.obj.example;
});
But if ipcMain is run asynchronously, the program will close:
ipcMain.on('testEvent', async (_event) => {
const test = null;
const example = test.obj.example;
})
As I mentioned, this problem only occurs in the compiled version.
from bytenode.
Arrow functions are not compatible with bytenode, especially async arrow functions. This is mentioned in the README.md
file.
I'm closing this issue as I believe changing your arrow functions into regular functions will solve it.
from bytenode.
I believe the problem is not limited to ipcMain, but affects all asynchronous functions. Please paste the following code into your main.js file, compile it with Bytenode, build it, and try to run it:
// main.js
'use strict';
const path = require('path');
const { app, BrowserWindow } = require('electron');
const createWindow = () => {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true,
contextIsolation: false,
sandbox: false
}
});
win.loadFile('index.html');
const test = null;
const example = test.obj.example;
};
setTimeout(async () => {
const test = null;
const example = test.obj.example;
}, 1);
app.whenReady().then(() => {
createWindow();
});
from bytenode.
I believe the problem is not limited to ipcMain, but affects all asynchronous functions.
It affects all arrow functions. Sometimes they work, but your program will crash eventually. V8 engine inspects arrow functions' source code under certain conditions, and when the source code isn't there (because bytenode removes it), it crashes.
from bytenode.
Related Issues (20)
- Is there any to use it by Tampermonkey? HOT 1
- Invalid or incompatible cached data (cachedDataRejected) - NodeJS to NodeJS Test HOT 4
- cli no exitcode when compile HOT 2
- Node.js version, or v8 version? HOT 2
- load compressed bytecode HOT 11
- 开发含有native插件打包加密时,会有问题,检查过代码,怀疑是bytenode的问题 HOT 1
- Error: Invalid or incompatible cached data (cachedDataRejected) HOT 8
- ncc
- feat: Accept path to Electron as a compile argument
- bug: Electron builds use 2 subprocesses
- Source map support to trace production errors HOT 5
- Error: cannot find module 'node:assert/strict' HOT 11
- WDIO8/Bytenode compatibility regression
- 1.5.2 bug ': No such file or directory HOT 1
- toString issue clarification HOT 1
- Can "fixBytecode" done when complie?
- BIG problem: v1.5.4 CLI doesn't work on linux! HOT 11
- Compiling with a file pattern *.js does not work on Windows HOT 2
- 这也许是一个bug
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bytenode.