Skip to content

Instantly share code, notes, and snippets.

@ravicious
Created January 12, 2024 11:10
Show Gist options
  • Save ravicious/b00233be9a808bc267b739ccf7490f94 to your computer and use it in GitHub Desktop.
Save ravicious/b00233be9a808bc267b739ccf7490f94 to your computer and use it in GitHub Desktop.
Electron #40798 repro attempt
const {setTimeout} = require('node:timers/promises')
process.on('SIGTERM', () => {
console.log('got SIGTERM')
setTimeout(100).then(() => {
console.log('Exiting')
process.exit(0)
})
})
console.log('running')
setTimeout(10000).then(() => {
console.log('Timeout ended')
})
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'">
<link href="./styles.css" rel="stylesheet">
<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>.
<!-- You can also require other files to run in this process -->
<script src="./renderer.js"></script>
</body>
</html>
// Modules to control application life and create native browser window
const { app, BrowserWindow, globalShortcut } = require('electron')
const path = require('node:path')
const {setTimeout} = require('node:timers/promises')
const {fork, spawn, exec} = require('node:child_process')
const childProcess = fork(path.join(__dirname, 'child.js'), [], {stdio: 'pipe'})
childProcess.stdout.on('data', (data) => {
console.log('child: ' + data)
})
function createWindow () {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
x: 250,
y: 250,
backgroundColor: 'green',
minWidth: 490,
minHeight:300,
autoHideMenuBar: true,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
webgl: false,
enableWebSQL: false,
safeDialogs: true,
contextIsolation: true,
nodeIntegration: false,
sandbox: false,
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
mainWindow.once('close', () => {
const windowState = mainWindow.getNormalBounds()
setTimeout(500).then(() => {
console.log(windowState)
})
})
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
app.on('will-quit', async event => {
event.preventDefault();
globalShortcut.unregisterAll()
await setTimeout(200)
childProcess.kill('SIGTERM')
await setTimeout(25)
app.exit();
});
app.on('quit', () => {
});
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
{
"name": "electron-fiddle",
"productName": "electron-fiddle",
"description": "My Electron application description",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"author": "rav",
"scripts": {
"start": "electron ."
},
"dependencies": {},
"devDependencies": {
"electron": "28.0.0"
}
}
/**
* The preload script runs before. It has access to web APIs
* as well as Electron's renderer process modules and some
* polyfilled Node.js functions.
*
* https://www.electronjs.org/docs/latest/tutorial/sandbox
*/
window.addEventListener('DOMContentLoaded', () => {
const replaceText = (selector, text) => {
const element = document.getElementById(selector)
if (element) element.innerText = text
}
for (const type of ['chrome', 'node', 'electron']) {
replaceText(`${type}-version`, process.versions[type])
}
})
/**
* This file is loaded via the <script> tag in the index.html file and will
* be executed in the renderer process for that window. No Node.js APIs are
* available in this process because `nodeIntegration` is turned off and
* `contextIsolation` is turned on. Use the contextBridge API in `preload.js`
* to expose Node.js functionality from the main process.
*/
/* styles.css */
/* Add styles here to customize the appearance of your app */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment