I have the following script
const WebpackDevServer = require('webpack-dev-server')
const webpack = require('webpack')
const electroner = require('electroner')
const packageJson = require('../package')
const config = require('./webpack.config')
const options = {
port: 3000,
contentBase: './dist',
hot: true,
overlay: true,
after: function (app, server) {
const window = electroner(`${__dirname}/../${packageJson.main}`, {
'enable-transparent-visuals': true,
'disable-cpu': true
})
window.on('close', () => {
server.close()
})
},
stats: 'errors-only'
}
WebpackDevServer.addDevServerEntrypoints(config, options)
const compiler = webpack(config)
const server = new WebpackDevServer(compiler, options)
server.listen(3000, 'localhost', () => {
console.log('dev server listening on port 3000')
})
The "config" variable contains/points to two webpack configuration (one for main and another for renderer script for an electron app).
What I'd like to achieve is, I want to restart the electron process only when the recompilation of the main script is successful.
I've read for a week about hooks but I believe they do not help in achieving the above.
I am looking for a solution that goes along like this:
compiler.on('compile', (stats) => {
if(stats.details === 'main.js'){
// logic to restart electron process here.
}
})
Any hints would be appreciated.
Achieved the behavior I was looking for with the below
const webpackDevServer = require('webpack-dev-server')
const webpack = require('webpack')
const electroner = require('electroner')
const path = require('path')
const packageJson = require('./package')
const config = require('./webpack.config')
let window
let server
const options = {
contentBase: './dist',
hot: true,
host: 'localhost',
after: function (app, server) {
window = electroner(path.resolve(__dirname, packageJson.main), {
'enable-transparent-visuals': true,
'disable-cpu': true
})
window.on('close', () => {
server.close()
})
},
port: 3000,
overlay: true,
stats: 'errors-only',
writeToDisk: true
}
let done = false
function initCompiler () {
done = false
webpackDevServer.addDevServerEntrypoints(config, options)
const compiler = webpack(config, (error, stats) => {
if (error) {
console.error(error.stack || error)
if (error.details) {
console.error(error.details)
}
return
}
const info = stats.toJson()
if (stats.hasErrors()) {
console.error(info.errors)
}
if (stats.hasWarnings()) {
console.warn(info.warnings)
}
done = true
})
let check
let start = () => {
if (!done) {
check = setTimeout(start, 100)
return
}
server = new webpackDevServer(compiler, options)
server.listen(3000, 'localhost', () => {
console.log('dev server listening on port 3000')
})
}
setTimeout(start, 100)
let files = []
compiler.hooks.watchRun.tap('MainProcess', compiler => {
files = Object.keys(compiler.watchFileSystem.watcher.mtimes).map(file => {
return path.parse(file).dir
})
})
compiler.hooks.done.tap('MainProcess', () => {
const search = path.resolve(__dirname, 'src', 'main')
if (files.length && files.filter(file => file.indexOf(search) > -1).length) {
files = []
window.kill()
server.close()
initCompiler()
}
})
}
initCompiler()
Edit
My previous answer had some issues. On compile, if the main.js file wasn't already present, it would error and stop the compilation and not start the app.
The edit ensures that the compile is first completed before starting the server and then start the electron app.
Related
Through the merge function, in case the version of the web park is development, I try to add a server file to it only in this case. Writes an error. He writes that the types are not correct, but I indicated the same type in both interfaces
An argument of type "Devserver" cannot be assigned to a parameter of type "IgetConfig". The "entry" property is missing in the "I dev Server" type and is mandatory in the "I get Config" type.
return merge(mainConfig, serverTuning);
file dev.config.ts
module.exports = {
devServer: {
port: 3000,
},
};
file webpack config
import { merge } from 'webpack-merge';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import { PlatformPath } from 'path';
const path: PlatformPath = require('path');
interface IdevServer {
devServer: {
port: number;
};
}
const serverTuning: IdevServer = require('./dev.config');
const test = 'main';
const ROOT_DIRECTORY = path.resolve(__dirname, '..');
interface IgetConfig {
entry: {
main: string[];
};
devServer?: {
port: number;
};
}
const getConfig = (mode): IgetConfig => {
const isProduction = mode === 'production';
const isDevelopment = mode === 'development';
return {
entry: {
[test]: ['./src/index.js'],
},
};
};
module.exports = (env, { mode }) => {
const mainConfig = getConfig(mode);
if (mode === 'development') {
return merge(mainConfig, serverTuning);
}
return mainConfig;
};
module.exports = (env, { mode }) => {
const mainConfig = getConfig(mode);
if (mode === 'development') {
return merge(mainConfig, serverTuning);
}
return mainConfig;
};
I'm trying to build an application using Electron with Monaco editor. I used monaco language client to integrate the editor with clangd. But somehow it didn't work... After manually connecting to the LSP server through web socket, I get the following error in the terminal:
TypeError: node.exports.StreamMessageReader is not a constructor
at createStreamConnection (/Volumes/Data/Develop/cpcode/dist/electron/main.js:3100:18)
at createProcessStreamConnection (/Volumes/Data/Develop/cpcode/dist/electron/main.js:3094:12)
at createServerProcess (/Volumes/Data/Develop/cpcode/dist/electron/main.js:3090:10)
at launch (/Volumes/Data/Develop/cpcode/dist/electron/main.js:3114:28)
at /Volumes/Data/Develop/cpcode/dist/electron/main.js:3160:13
at WebSocketServer.completeUpgrade (/Volumes/Data/Develop/cpcode/node_modules/ws/lib/websocket-server.js:431:5)
at WebSocketServer.handleUpgrade (/Volumes/Data/Develop/cpcode/node_modules/ws/lib/websocket-server.js:339:10)
at Server.<anonymous> (/Volumes/Data/Develop/cpcode/dist/electron/main.js:3147:13)
at Server.emit (node:events:526:28)
at onParserExecuteCommon (node:_http_server:727:14)
I am starting the server in Electron main process. Here's the code for it:
process.env.DIST = join(__dirname, '..')
process.env.PUBLIC = app.isPackaged ? process.env.DIST : join(process.env.DIST, '../public')
import { join } from 'path'
import { app, BrowserWindow } from 'electron'
import ws from "ws";
import http from "http";
import url from "url";
import net from "net";
import express from "express";
import * as rpc from "vscode-ws-jsonrpc";
import * as server from "vscode-ws-jsonrpc/server";
import * as lsp from "vscode-languageserver";
import { Message } from "vscode-languageserver";
export function launch(socket: rpc.IWebSocket) {
const reader = new rpc.WebSocketMessageReader(socket);
const writer = new rpc.WebSocketMessageWriter(socket);
// start the language server as an external process
const socketConnection = server.createConnection(reader, writer, () =>
socket.dispose()
);
const serverConnection = server.createServerProcess("CPP", "clangd");
if (serverConnection) {
server.forward(socketConnection, serverConnection, (message) => {
if (Message.isRequest(message)) {
console.log("server request!!!");
if (message.method === lsp.InitializeRequest.type.method) {
const initializeParams = message.params as lsp.InitializeParams;
initializeParams.processId = process.pid;
}
}
return message;
});
}
}
process.on("uncaughtException", function (err: any) {
console.error("Uncaught Exception: ", err.toString());
if (err.stack) {
console.error(err.stack);
}
});
export const startLSP = () => {
// create the express application
const app = express();
// server the static content, i.e. index.html
app.use(express.static(__dirname));
// start the server
const server = app.listen(3000);
// create the web socket
const wss = new ws.Server({
noServer: true,
perMessageDeflate: false
});
server.on(
"upgrade",
(request: http.IncomingMessage, socket: net.Socket, head: Buffer) => {
// eslint-disable-next-line n/no-deprecated-api
const pathname = request.url
? url.parse(request.url).pathname
: undefined;
if (pathname === "/lsp") {
wss.handleUpgrade(request, socket, head, (webSocket) => {
const socket: rpc.IWebSocket = {
send: (content) =>
webSocket.send(content, (error) => {
if (error) {
throw error;
}
}),
onMessage: (cb) => webSocket.on("message", cb),
onError: (cb) => webSocket.on("error", cb),
onClose: (cb) => webSocket.on("close", cb),
dispose: () => webSocket.close()
};
// launch the server when the web socket is opened
if (webSocket.readyState === webSocket.OPEN) {
launch(socket);
} else {
webSocket.on("open", () => launch(socket));
}
});
}
}
);
};
let win: BrowserWindow | null
// Here, you can also use other preload
const preload = join(__dirname, './preload.js')
// 🚧 Use ['ENV_NAME'] avoid vite:define plugin - Vite#2.x
const serverURL = process.env['VITE_DEV_SERVER_URL']
function createWindow() {
win = new BrowserWindow({
icon: join(process.env.PUBLIC, 'logo.svg'),
webPreferences: {
contextIsolation: false,
nodeIntegration: true,
preload,
},
titleBarStyle: process.platform === 'darwin' ? 'hiddenInset' : 'hidden',
})
startLSP();
// Test active push message to Renderer-process.
win.webContents.on('did-finish-load', () => {
win?.webContents.send('main-process-message', (new Date).toLocaleString())
})
if (app.isPackaged) {
win.loadFile(join(process.env.DIST, 'index.html'))
} else {
win.loadURL(serverURL)
}
}
app.on('window-all-closed', () => {
win = null
})
app.whenReady().then(createWindow)
I am using Vite as a bundler and I'm not sure if it has anything to do with this. Anyway, here's my vite.config.ts:
import fs from 'fs'
import path from 'path'
import { defineConfig } from 'vite'
import electron from 'vite-plugin-electron'
import renderer from 'vite-plugin-electron-renderer';
import { nodePolyfills } from 'vite-plugin-node-polyfills'
fs.rmSync('dist', { recursive: true, force: true }) // v14.14.0
export default defineConfig({
plugins: [
electron({
main: {
entry: 'electron/main.ts',
},
preload: {
input: {
// Must be use absolute path, this is the restrict of Rollup
preload: path.join(__dirname, 'electron/preload.ts'),
},
},
// Enables use of Node.js API in the Renderer-process
// https://github.com/electron-vite/vite-plugin-electron/tree/main/packages/electron-renderer#electron-renderervite-serve
renderer: {},
}),
nodePolyfills({
// Whether to polyfill `node:` protocol imports.
protocolImports: true,
}),
],
})
After Googling, I tried to install vscode-languageserver and vscode-languageserver-protocol, but no luck. The problem still continues.
Does anyone has a solution to this? Thanks in advance!
Our original code to create redis clients
import Redis, { Redis as RedisClient, Cluster, ClusterOptions } from 'ioredis';
import config from '../../../config';
const {
port, host, cluster, transitEncryption,
} = config.redis;
const retryStrategy = (times: number): number => Math.min(times * 50, 10000);
function GetRedisClient(): RedisClient {
return new Redis({
host,
port: port as number,
retryStrategy,
});
}
function GetRedisClusterClient(): Cluster {
const clusterOptions: ClusterOptions = {
clusterRetryStrategy: retryStrategy,
redisOptions: { db: 0 },
};
if (transitEncryption) {
clusterOptions.dnsLookup = (address, callback) => callback(null, address);
clusterOptions.redisOptions!.tls = {};
}
return new Redis.Cluster([
{
host,
port: port as number,
},
], clusterOptions);
}
function getClient() {
return cluster ? GetRedisClusterClient() : GetRedisClient();
}
export default getClient();
has become this as we upgrade to version 5.0.1 of ioredis:
import Redis, { Cluster, ClusterOptions } from 'ioredis';
import config from '../../../config';
const {
port, host, cluster, transitEncryption,
} = config.redis;
const retryStrategy = (times: number): number => Math.min(times * 50, 10000);
function GetRedisClient(): Redis {
return new Redis({
host,
port: port as number,
retryStrategy,
});
}
function GetRedisClusterClient(): Cluster {
const clusterOptions: ClusterOptions = {
clusterRetryStrategy: retryStrategy,
redisOptions: { db: 0 },
};
if (transitEncryption) {
clusterOptions.dnsLookup = (address, callback) => callback(undefined, address);
clusterOptions.redisOptions!.tls = {};
}
return new Cluster([
{
host,
port: port as number,
},
], clusterOptions);
}
function getClient() {
return cluster ? GetRedisClusterClient() : GetRedisClient();
}
export default getClient();
The dev dependency #types/ioredis has been removed from our package.json.
I'm getting a semantic error when I compile the project typescript.
xxx/node_modules/#types/connect-redis/index.d.ts(22,51): error TS2694: Namespace '"xxx/node_modules/ioredis/built/index"' has no exported member 'Redis'.
And the jest tests no longer run, flagging up the line return new Cluster, failing with the following
TypeError: _ioredis.Cluster is not a constructor
The tests:
import Redis, { Cluster, ClusterOptions } from 'ioredis';
import config from '../../../config';
jest.mock('ioredis');
jest.mock('../../../config', () => ({
redis: {
host: 'redis',
port: 1234,
cluster: false,
transitEncryption: false,
},
service_name: 'app name',
version: 'test',
}));
describe('redis client', () => {
const clusterTest = (clusterOptions: ClusterOptions) => {
// eslint-disable-next-line global-require
require('./client');
expect(Cluster)
.toHaveBeenCalled();
};
const clusterOptions: ClusterOptions = {
clusterRetryStrategy: expect.any(Function),
redisOptions: { db: 0 },
};
test('if the cluster variable is set, then the module should return a clustered client', () => {
config.redis.cluster = true;
clusterTest(clusterOptions);
});
test('if the cluster variable is set and transit encryption is set, then the module should return a clustered client with encryption', () => {
config.redis.cluster = true;
config.redis.transitEncryption = true;
clusterOptions.dnsLookup = expect.any(Function);
clusterOptions.redisOptions = { db: 0, tls: {} };
clusterTest(clusterOptions);
});
test('if the cluster variable is not set, then the module should return a standalone client', () => {
config.redis.cluster = false;
// eslint-disable-next-line global-require
require('./client');
expect(Redis).toHaveBeenCalled();
});
});
My colleague seems to get the tests running individually, but using code branch, I get the above problem.
Any suggestions about where I've gone wrong?
I was trying to connect Redis (v4.0.1) to my express server with typescript but having a bit issue. Am learning typescript. It's showing redlines on host inside redis.createClient() Can anyone help me out?
const host = process.env.REDIS_HOST;
const port = process.env.REDIS_PORT;
const redisClient = redis.createClient({
host,
port,
});
Argument of type '{ host: string | undefined; port: string | undefined; }' is not assignable to parameter of type 'Omit<RedisClientOptions<never, RedisScripts>, "modules">'.
Object literal may only specify known properties, and 'host' does not exist in type 'Omit<RedisClientOptions<never, RedisScripts>, "modules">'.ts(2345)
Options have changed when redis updated to 4.0.1. This should help you.
This works as expected (redis v4.1.0)
const url = process.env.REDIS_URL || 'redis://localhost:6379';
const redisClient = redis.createClient({
url
});
what I did in my project was this
file: services/internal/cache.ts
/* eslint-disable no-inline-comments */
import type { RedisClientType } from 'redis'
import { createClient } from 'redis'
import { config } from '#app/config'
import { logger } from '#app/utils/logger'
let redisClient: RedisClientType
let isReady: boolean
const cacheOptions = {
url: config.redis.tlsFlag ? config.redis.urlTls : config.redis.url,
}
if (config.redis.tlsFlag) {
Object.assign(cacheOptions, {
socket: {
// keepAlive: 300, // 5 minutes DEFAULT
tls: false,
},
})
}
async function getCache(): Promise<RedisClientType> {
if (!isReady) {
redisClient = createClient({
...cacheOptions,
})
redisClient.on('error', err => logger.error(`Redis Error: ${err}`))
redisClient.on('connect', () => logger.info('Redis connected'))
redisClient.on('reconnecting', () => logger.info('Redis reconnecting'))
redisClient.on('ready', () => {
isReady = true
logger.info('Redis ready!')
})
await redisClient.connect()
}
return redisClient
}
getCache().then(connection => {
redisClient = connection
}).catch(err => {
// eslint-disable-next-line #typescript-eslint/no-unsafe-assignment
logger.error({ err }, 'Failed to connect to Redis')
})
export {
getCache,
}
then you just import where you need:
import { getCache } from '#services/internal/cache'
const cache = await getCache()
cache.setEx(accountId, 60, JSON.stringify(account))
The option to add a host, port in redis.createClient is no longer supported by redis. So it is not inside type createClient. use URL instead.
import { createClient } from 'redis';
const client = createClient({
socket: {
host: process.env.REDIS_HOST,
port: parseInt(process.env.REDIS_PORT)
},
password: process.env.REDIS_PW
});
client.on('error', (err) => console.error(err));
client.connect();
export { client };
Im using Auth0 in an electron app to manage a log-in system. I referenced this tutorial here:
https://auth0.com/blog/securing-electron-applications-with-openid-connect-and-oauth-2/
to get started with using it.
Auth0 has been working great when I've been working in development but for some reason fails after I call "yarn package" to build the app. My electron app used electron-react-boilerplate (https://github.com/electron-react-boilerplate/electron-react-boilerplate).
Here are the important files:
// imports
...
import {
getAuthenticationURL,
refreshTokens,
loadTokens,
logout,
getLogOutUrl,
getProfile,
getResponse,
} from './services/authservice';
export default class AppUpdater {
constructor() {
...
}
}
let mainWindow: BrowserWindow | null = null;
if (process.env.NODE_ENV === 'production') {
const sourceMapSupport = require('source-map-support');
sourceMapSupport.install();
}
if (
process.env.NODE_ENV === 'development' ||
process.env.DEBUG_PROD === 'true'
) {
require('electron-debug')();
}
const installExtensions = async () => {
...
};
const createWindow = async () => {
console.log('now starting the main process');
if (
process.env.NODE_ENV === 'development' ||
process.env.DEBUG_PROD === 'true'
) {
await installExtensions();
}
const RESOURCES_PATH = app.isPackaged
? path.join(process.resourcesPath, 'assets')
: path.join(__dirname, '../assets');
const getAssetPath = (...paths: string[]): string => {
return path.join(RESOURCES_PATH, ...paths);
};
mainWindow = new BrowserWindow({
show: true,
width: 1024,
height: 728,
titleBarStyle: 'hidden', // add this line
frame: false,
//icon: getAssetPath('icon.png'),
webPreferences: {
nodeIntegration: true,
},
});
mainWindow.loadURL(`file://${__dirname}/index.html`);
const devtools = new BrowserWindow();
mainWindow.webContents.setDevToolsWebContents(devtools.webContents);
mainWindow.webContents.openDevTools({ mode: 'detach' });
};
/**
* Add event listeners...
*/
let win = null;
function createAuthWindow() {
destroyAuthWin();
win = new BrowserWindow({
width: 1000,
height: 600,
webPreferences: {
nodeIntegration: false,
enableRemoteModule: false,
},
});
console.log(getAuthenticationURL());
win.loadURL(getAuthenticationURL());
const {
session: { webRequest },
} = win.webContents;
const filter = {
urls: [
'file:///callback*',
],
};
webRequest.onBeforeRequest(filter, async ({ url }) => {
console.log(url);
await loadTokens(url)
.then((res) => {
console.log(res);
})
.catch(console.log);
console.log('from web request');
createWindow();
return destroyAuthWin();
});
win.on('authenticated', () => {
console.log('WE HAVE AUTHENTICATED');
destroyAuthWin();
});
win.on('closed', () => {
win = null;
});
}
function destroyAuthWin() {
if (!win) return;
win.close();
win = null;
}
// logout logic: removed for simplicity
ipcMain.on('profileRequest', (event, arg) => {
//event.reply('profileResponse', getProfile());
event.returnValue = getProfile();
});
const showWindow = async () => {
try {
await refreshTokens();
return createWindow();
} catch (err) {
createAuthWindow();
}
};
this is my auth services file:
let accessToken = null;
let profile = {};
let refreshToken = null;
export function getAccessToken() {
return accessToken;
}
export function getProfile() {
return profile;
}
export function getAuthenticationURL() {
return (
'https://' +
auth0Domain +
'/authorize?' +
'scope=openid%20profile%20offline_access&' +
'response_type=code&' +
'client_id=' +
clientId +
'&' +
'redirect_uri=' +
redirectUri
);
}
export async function refreshTokens() {
const refreshToken = await keytar.getPassword(keytarService, keytarAccount);
if (refreshToken) {
const refreshOptions = {
method: 'POST',
url: `https://${auth0Domain}/oauth/token`,
headers: { 'content-type': 'application/json' },
data: {
grant_type: 'refresh_token',
client_id: clientId,
refresh_token: refreshToken,
},
};
try {
const response = await axios(refreshOptions);
res = response;
accessToken = response.data.access_token;
profile = jwtDecode(response.data.id_token);
} catch (error) {
await logout();
throw error;
}
} else {
throw new Error('No available refresh token.');
}
}
export async function loadTokens(callbackURL) {
console.log('loading tokens:');
console.log(callbackURL);
res = callbackURL;
const urlParts = url.parse(callbackURL, true);
const query = urlParts.query;
console.log(query);
const exchangeOptions = {
grant_type: 'authorization_code',
client_id: clientId,
code: query.code,
redirect_uri: redirectUri,
};
const options = {
method: 'POST',
url: `https://${auth0Domain}/oauth/token`,
headers: {
'content-type': 'application/json',
},
data: JSON.stringify(exchangeOptions),
};
try {
const response = await axios(options);
console.log('from token:');
console.log(response);
res = response;
accessToken = response.data.access_token;
profile = jwtDecode(response.data.id_token);
refreshToken = response.data.refresh_token;
console.log(getProfile());
if (refreshToken) {
await keytar.setPassword(keytarService, keytarAccount, refreshToken);
}
} catch (error) {
await logout();
throw error;
}
}
I have a file in my components folder called "Auth.jsx" which has a "get profile" methods which interacts with the main process to get the profile
const getProfile = () => {
return ipcRenderer.sendSync('profileRequest', true);
};
After I package the electron app, the getProfile method always returns null/undefined.
Here are the auth0 logs:
Auth0 Logs
It shows that there is a successful Login and Exchange.
Finally, here's my webpack file: "webpack.config.main.prod.babel"
/**
* Webpack config for production electron main process
*/
import path from 'path';
import webpack from 'webpack';
import { merge } from 'webpack-merge';
import TerserPlugin from 'terser-webpack-plugin';
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
import baseConfig from './webpack.config.base';
import CheckNodeEnv from '../scripts/CheckNodeEnv';
import DeleteSourceMaps from '../scripts/DeleteSourceMaps';
import dotenv from 'dotenv';
CheckNodeEnv('production');
DeleteSourceMaps();
const devtoolsConfig =
process.env.DEBUG_PROD === 'true'
? {
devtool: 'source-map',
}
: {};
export default merge(baseConfig, {
...devtoolsConfig,
mode: 'production',
target: 'electron-main',
entry: './src/main.dev.ts',
output: {
path: path.join(__dirname, '../../'),
filename: './src/main.prod.js',
},
optimization: {
minimizer: [
new TerserPlugin({
parallel: true,
}),
],
},
plugins: [
new BundleAnalyzerPlugin({
analyzerMode:
process.env.OPEN_ANALYZER === 'true' ? 'server' : 'disabled',
openAnalyzer: process.env.OPEN_ANALYZER === 'true',
}),
new webpack.EnvironmentPlugin({
NODE_ENV: 'production',
DEBUG_PROD: true,
START_MINIMIZED: false,
/*
other environment variables, including auth0 domain name and clientID
*/
}),
],
/**
* Disables webpack processing of __dirname and __filename.
* If you run the bundle in node.js it falls back to these values of node.js.
* https://github.com/webpack/webpack/issues/2010
*/
node: {
__dirname: false,
__filename: false,
},
});
Im suspecting the problem might have something to do with webpack since it's only the packaged version of the application that doesn't work properly. Im not sure exactly what the problem is, whether is a problem in the code or if I need to specifically change something within my Auth0 dashboard. If you have any suggestions or any ideas on how to debug let me know!
I had the exact same issue! I fixed it by changing the import method for jwt-decode from require to import
import jwtDecode from 'jwt-decode'