1. BackgroundIn the team, we need to use desktop technology due to business development, such as offline availability and calling desktop system capabilities. What is desktop development? In one sentence, it is: software development based on Windows, macOS and Linux operating systems. We have conducted a detailed technical survey on this. The main desktop development methods are Native, QT, Flutter, NW, Electron, and Tarui. Their respective advantages and disadvantages are shown in the following table: Our final desktop technology choice is Electron, which is a development framework that can use Web technology to develop cross-platform desktop applications. Its technical components are as follows: Electron = Chromium + Node.js + Native API The technical capabilities are shown in the figure below: The overall architecture is shown in the figure below: Electron is a multi-process architecture with the following features:
Here is the principle of Electron inter-process communication technology: Electron uses IPC (interprocess communication) to communicate between processes, as shown below: It provides IPC communication modules, ipcMain for the main process and ipcRenderer for the rendering process. From the electron source code, we can see that ipcMain and ipcRenderer are both EventEmitter objects. The source code is shown in the figure below: After seeing the source code implementation, do you think IPC is not difficult to understand? Only by knowing its essence can you handle it with ease. Seeing this, we look back at the technical table above and see that the Electron application package is large in size. What is the root cause of the large size? In fact, this is related to the framework design of Chromium. It has no macro control for many functions, which makes it difficult to remove large and complex detailed functions. It also causes the packages based on Chromium, such as electron and nwjs, to start at more than 100M. In summary, electron has the advantages of being cross-terminal, web-based, and having a strong ecosystem, and is one of the excellent solutions for desktop development. The following article will introduce the practical experience of electron application development, including application technology selection and common functions. 2. Application Technology Selection2.1 Programming language TypescriptHere are the reasons:
A large number of community type definition files improve development efficiency 2.2 Build Tool Electron-ForgeReason: Simple and powerful, one of the best building tools for electron applications. Here is the introduction and difference between electron-builder and electron-forge, as shown in the figure below: The biggest difference between the two is the degree of freedom. There is basically no difference in capabilities between the two. Judging from the ranking in the official organization, electron-forge is intentionally recommended first. 2.3 Web Solution Vue3 + ViteWe use Vue3 and Vite as the construction tool. For specific advantages, you can check the official website introduction. This combination is the current mainstream Web development solution. 2.4 monorepo solution pnpm + turboThe current monorepo ecosystem is flourishing, and the correct practice method should be the integration method, that is, taking the strengths of each. This is also the current trend. Various open source monorepo tools have reached a tacit understanding and focused on their own strengths. For example, pnpm is good at dependency management, and turbo is good at building task orchestration. So in the selection of monorepo technology, I chose pnpm and turbo. pnpm reasons are as follows:
Compared to the vue official website, when using pnpm, I added workspace. Turbo reasons are as follows:
2.5 Database lowdbThere are many choices for electron application databases, such as lowdb, sqlite3, electron-store, pouchdb, dedb, rxdb, dexie, ImmortalDB, etc. These databases have one feature, that is, serverless. The main considerations for electron application database technology selection are as follows:
We conducted relevant research through the following channels
Four optimal choices are given, namely lowdb, sqlite3, nedb, and electron-store, for the following reasons:
The database we use is the lowdb solution. PS: Let me mention pouchdb. If you need to synchronize local data to a remote database, you can use pouchdb, which can easily complete synchronization with couchdb. 2.6 Script tool zxDuring the software development process, completing some processes and operations through scripts can effectively improve development efficiency and happiness. There are two excellent choices for node runtime: shelljs and zx. The reasons for choosing zx are as follows:
So far, the technical selection has been introduced. Next, I will introduce the common functions of electron applications. 3. ConstructionThis section mainly introduces the following 5 points:
3.1 Application Icon GenerationThere are the following methods to generate icons of different sizes: Windows
Web page generation:https://tool.520101.com/diannao/ico/(opens new window) MacOS
Web page generation:https://tool.520101.com/diannao/ico/(opens new window)
3.2 Binary file buildingThe content of this chapter is based on electron-forge, but the principle is the same. When developing desktop applications, there are scenarios where you need to use third-party binary programs, such as ffmpeg. When building binary programs, pay attention to the following two points: (1) Binary programs cannot be packaged into asar. You can set the following in the build configuration file (forge.config.js): const os = require ( 'os' ) (2) The methods for obtaining binary program paths are different in development and production environments. You can use the following code to dynamically obtain the path: import { app } from 'electron' 3.3 Build on DemandHow to build cross-platform binaries on demand? For example, ffmpeg is used in desktop applications, which requires downloadable binaries for Windows, Mac, and Linux. When packaging, if on-demand building is not done, all three binary files will be included in the build, which will increase the size of the application a lot. You can configure the forge.config.js configuration file as follows to complete the on-demand build. The code is as follows: const platform = os . platform () By using the platform variable to add the binary of the corresponding system to the build, you can complete the on-demand build of the binary file. 3.4 Performance OptimizationThe main thing is to optimize the build speed and build volume. The build speed is not easy to optimize. This article focuses on the build volume optimization. Here we take the Mac system as an example. After the electron application is packaged, check the application package content, as shown in the following figure: You can see that there is an app.asar file. After decompressing this file with asar, you can see the following content: It can be seen that the files in asar are the project codes after we build. From the picture, we can see that there is a node_modules directory. This is because in the electron build mechanism, all dependencies of dependencies will be automatically packaged into asar. Therefore, based on the above analysis, our optimization measures are as follows:
Here is the fourth point: how to clean and streamline node_modules? If it is a dependency installed by yarn, we can use the following command in the root directory to streamline it: yarn autoclean - I If the dependency is installed by pnpm, point 4 should not work. I used yarn to install the dependency in my project, and then after executing the above command, I found that the package size was reduced by 6M, which is not much, but it is still okay. This concludes the introduction to the build function. 4. UpdateThis chapter is mainly divided into the following two aspects:
The following will introduce the above two updates in turn 4.1 Full UpdateTo update the software, you need to replace all files by downloading the latest package or zip file. The overall design flow chart is as follows: To implement it according to the flowchart, we need to do the following:
4.2 Incremental UpdateThe software update is completed by pulling the latest rendering layer package file and overwriting the previous rendering layer code. This solution only needs to replace the rendering layer code, without replacing all files. To implement it according to the flowchart, we need to do the following things
Full updates and incremental updates each have their own advantages. In most cases, incremental updates are used to improve the user update experience, while full updates are used as a backup update solution. This concludes the introduction to the update function. 5. Performance OptimizationIt is divided into the following three aspects:
Build optimization has been introduced in detail above, so it will not be introduced here. The following will introduce startup optimization and runtime optimization. 5.1 Startup Optimization
5.1.1 Use v8-compile-cache to cache compiled code Use V8 to cache data. Why do this? Because electorn uses the V8 engine to run js, when V8 runs js, it needs to parse and compile before executing the code. The parsing and compiling process consumes a lot of time and often leads to performance bottlenecks. The V8 cache function can cache the compiled bytecode, saving the time of the next parsing and compilation. Mainly use v8-compile-cache to cache compiled code. The method is very simple: add a line where you need to cache require ( 'v8-compile-cache' ) For other usage methods, please refer to this link document https://www.npmjs.com/package/v8-compile-cache(opens new window) 5.1.2 Prioritize core functions and dynamically load non-core functionsThe pseudo code is as follows: export function share () { 5.2 Runtime Optimization
5.2.1 Web performance optimization for rendering process Use a mind map to fully explain how to optimize Web performance, as shown below: The above picture basically contains the core key points and contents of performance optimization. You can use it as a reference to perform performance optimization. 5.2.2 Lightweighting the main process The core solution is to delegate the time-consuming and computationally intensive functions to the newly opened node process for execution. The pseudo code is as follows: const { fork } = require ( 'child_process' ) Through the above code, the time-consuming and computationally intensive functions are placed in server.js, and then forked to the newly opened node process for processing. This concludes the introduction to performance optimization. VI. Quality AssuranceThe full process of quality assurance measures is shown in the figure below: This chapter mainly introduces the following three aspects:
The above contents will be introduced one by one below. 6.1 Automated TestingWhat is Automation Testing? The above picture shows a complete step of automated testing. You can understand it by looking at the picture. Automated testing is mainly divided into unit testing, integration testing, and end-to-end testing. The relationship between the three is shown in the following figure: Generally speaking, as software engineers, we only need to do some unit testing. And from my current experience, if you are writing a business project, you basically won’t write test-related code. Automated testing is mainly used to write libraries, frameworks, components, etc. that need to be provided to others as individual units. The recommended testing tools for electron are vitest and spectron. Please refer to the official website for specific usage. There are no special techniques. 6.2 Crash MonitoringFor GUI software, especially desktop software, the crash rate is very important, so crashes need to be monitored. The crash monitoring principle is shown in the following figure: Crash Monitoring Tips
After crash monitoring is done, if a crash occurs, how to manage the crash? 6.3 Crash ManagementDifficulties in collapse management:
Crash management tips:
VII. SafetyAs the saying goes, safety is paramount. Ensuring the safety of electron applications is also an important matter. This chapter divides safety into the following five aspects:
The above contents will be introduced one by one below. 7.1 Source code leakageCurrently, electron does not do well in source code security. The official only uses asar to do some useless source code protection. How useless is it? You just need to download the asar tool and then decompress the asar file to get the source code inside, as shown below: You can see the source code of the Yuque application by following the operation shown in the picture. What is the asar mentioned above? 7.2 asarasar is a tar-like archive format that combines multiple files into one file. Electron can read any file content from it without decompressing the entire file. ASAR technical principle: You can directly look at the electron source code, which is all ts code, easy to read, the source code is shown in the figure below: As can be seen from the figure, the core implementation of asar is to rewrite the fs module of nodejs. 7.3 Source Code ProtectionTo avoid source code leakage, source code security can be divided into the following levels from low to high.
Among them, language bindings are the highest source code security measure. In fact, using C++ or Rust code to write electron application code will make it more difficult to decipher after compiling C++ or Rust code into binary code. Here I will talk about how to use Rust to write electron application code. Solution: Use napi-rs as a tool to write, as shown below: We use pnpm-workspace to manage Rust code and use napi-rs. For example, we write a sum function. The rs code is as follows: fn sum ( a : f64 , b : f64 ) - > f64 { At this point we add the napi decoration code, as shown below: use napi_derive :: napi ; Compile the above code into binary code that can be called by node through napi-cli. After compilation, use the above code in electron as follows: import { sum as rsSum } from '@rebebuca/native' For more information about napi-rs, please read the official documentation at: https://napi.rs/(opens new window) So far, the description of language bindings is complete. In this way, we can protect the source code of important functions. 7.4 Application SecurityA well-known security issue is cloning attack. The mainstream solution to this problem is to bind user authentication information and application device fingerprint. The overall process is shown in the following figure:
7.5 Coding SecurityThe main measures are as follows:
The above specific details will not be introduced here, you can search for the above solutions by yourself. In addition, there is an official recommended best security practice, you can take a look at it when you have time, the address is as follows: https://www.electronjs.org/docs/latest/tutorial/security(opens new window) This concludes the introduction to security. 8. ConclusionThis article introduces our research on desktop technology, the technology selection, and the practical experience summarized in the process of developing with electron, such as construction, performance optimization, quality assurance, security, etc. I hope it will be helpful to readers in the process of developing desktop applications. The article is bound to have shortcomings and errors, and readers are welcome to communicate in the comment area. |
<<: How big is the difference between iOS 15.4.1 and iOS 15.6? Is it worth upgrading?
>>: Android development board serial communication - in-depth analysis and detailed use
DXY is China's leading medical connector and ...
Promoters should all have a feeling that the budg...
The SEM bidding promotion team concluded that a g...
A good slogan, in addition to being closely relat...
If you experience these symptoms during the autum...
Welcome to the 52nd issue of the Nature Trumpet c...
To do SEO, you need to follow the trend. Baidu se...
Two years ago, knowledge payment became popular, ...
On February 27, the eighth day of the first lunar...
In the increasingly competitive Internet, it is c...
[[153510]] Three days ago was my first anniversar...
1. If a business is not growing, it is dying I ha...
The differences between iOS and Android are the r...
Recently, as Hunan Radio and Television Station p...
Traditional enterprises are inevitably aging in th...