feat: 动态载入node-serialport和node-usb避免nwjs下server异常
This commit is contained in:
@@ -29,9 +29,10 @@ export const MICROPYTHON = processConfig(CONFIG.micropython);
|
||||
export const PYTHON = processConfig(CONFIG.python);
|
||||
export const CURRENT_PLANTFORM = os.platform();
|
||||
export const TEMP_PATH = path.resolve(process.cwd(), CONFIG.server.path.temp);
|
||||
export const CLIENT_PATH = path.resolve(process.cwd(), CONFIG.client.path);
|
||||
export const CERTS_PATH = path.resolve(process.cwd(), 'certs');
|
||||
export const CLIENT_PORT = CONFIG.client.port;
|
||||
export const CLIENT_PATH = path.resolve(process.cwd(), CONFIG.client.path);
|
||||
export const CLIENT_PROTOCOL = CONFIG.client.protocol;
|
||||
export const SERVER_PORT = CONFIG.server.protocol === 'wss' ? CLIENT_PORT : CONFIG.server.port;
|
||||
export const SERVER_MODE = CONFIG.server.mode;
|
||||
export const SERVER_PROTOCOL = CONFIG.server.protocol;
|
||||
@@ -1,12 +1,17 @@
|
||||
import { exec } from 'node:child_process';
|
||||
import {
|
||||
SerialPort,
|
||||
ReadlineParser,
|
||||
ByteLengthParser
|
||||
} from 'serialport';
|
||||
import _ from 'lodash';
|
||||
import EventsBase from './events-base.js';
|
||||
import { CURRENT_PLANTFORM } from './config.js';
|
||||
import { CURRENT_PLANTFORM, SERVER_MODE } from './config.js';
|
||||
|
||||
|
||||
let SerialPort, ReadlineParser, ByteLengthParser;
|
||||
|
||||
if (SERVER_MODE === 'all') {
|
||||
const serial = await import('serialport');
|
||||
SerialPort = serial.SerialPort;
|
||||
ReadlineParser = serial.ReadlineParser;
|
||||
ByteLengthParser = serial.ByteLengthParser;
|
||||
}
|
||||
|
||||
|
||||
export default class Serial extends EventsBase {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { Server } from 'socket.io';
|
||||
import { to } from 'await-to-js';
|
||||
import { usb } from 'usb';
|
||||
import path from 'node:path';
|
||||
import fsExtra from 'fs-extra';
|
||||
import Serial from './serial.js';
|
||||
@@ -11,7 +10,7 @@ import ShellMicroPython from './shell-micropython.js';
|
||||
import ShellAmpy from './shell-ampy.js';
|
||||
import MString from './mstring.js';
|
||||
import Boards from './boards.js';
|
||||
import { TEMP_PATH, CLIENT_PATH } from './config.js';
|
||||
import { TEMP_PATH, CLIENT_PATH, SERVER_MODE } from './config.js';
|
||||
|
||||
|
||||
export default class Socket {
|
||||
@@ -37,12 +36,16 @@ export default class Socket {
|
||||
this.#shellAmpy_.register(socket.id, new ShellAmpy());
|
||||
this.#addEventsListenerForMode2_(socket);
|
||||
});
|
||||
usb.on('attach', (device) => {
|
||||
this.#namespaceAll_.emit('serial.attachEvent', device);
|
||||
});
|
||||
usb.on('detach', (device) => {
|
||||
this.#namespaceAll_.emit('serial.detachEvent', device);
|
||||
});
|
||||
if (SERVER_MODE === 'all') {
|
||||
import('usb').then(({ usb }) => {
|
||||
usb.on('attach', (device) => {
|
||||
this.#namespaceAll_.emit('serial.attachEvent', device);
|
||||
});
|
||||
usb.on('detach', (device) => {
|
||||
this.#namespaceAll_.emit('serial.detachEvent', device);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#addEventsListenerForMode1_(socket) {
|
||||
|
||||
@@ -2,9 +2,9 @@ import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import fsExtra from 'fs-extra';
|
||||
import fsPlus from 'fs-plus';
|
||||
import forge from 'node-forge';
|
||||
// import forge from 'node-forge';
|
||||
import shell from 'shelljs';
|
||||
import _ from 'lodash';
|
||||
// import _ from 'lodash';
|
||||
import { CERTS_PATH } from './config.js';
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ export function getCertificate() {
|
||||
data.cert = fsExtra.readFileSync(crtPath);
|
||||
data.key = fsExtra.readFileSync(keyPath);
|
||||
}
|
||||
const cert = forge.pki.certificateFromPem(data.cert);
|
||||
/* const cert = forge.pki.certificateFromPem(data.cert);
|
||||
const now = new Date();
|
||||
const notBefore = cert.validity.notBefore;
|
||||
const notAfter = cert.validity.notAfter;
|
||||
@@ -73,6 +73,6 @@ export function getCertificate() {
|
||||
}
|
||||
} else {
|
||||
data = generateCertificate();
|
||||
}
|
||||
} */
|
||||
return data;
|
||||
}
|
||||
54
src/index.js
54
src/index.js
@@ -5,21 +5,43 @@ import fsExtra from 'fs-extra';
|
||||
import express from 'express';
|
||||
import Socket from './common/socket.js';
|
||||
import { getCertificate } from './common/utils.js';
|
||||
import { CLIENT_PATH, CLIENT_PORT, SERVER_PORT, SERVER_MODE, SERVER_PROTOCOL } from './common/config.js';
|
||||
import {
|
||||
CLIENT_PATH, CLIENT_PORT, CLIENT_PROTOCOL,
|
||||
SERVER_MODE, SERVER_PORT, SERVER_PROTOCOL
|
||||
} from './common/config.js';
|
||||
|
||||
|
||||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
|
||||
const app = express();
|
||||
|
||||
if (CLIENT_PATH) {
|
||||
app.use(express.static(CLIENT_PATH));
|
||||
if (typeof nw === 'object') {
|
||||
app.use(express.static(path.resolve(CLIENT_PATH, '../')));
|
||||
} else {
|
||||
app.use(express.static(CLIENT_PATH));
|
||||
}
|
||||
}
|
||||
const httpsServer = https.createServer(getCertificate(), app);
|
||||
let httpServer = null;
|
||||
if (CLIENT_PORT !== SERVER_PORT) {
|
||||
httpServer = http.createServer()
|
||||
|
||||
let staticServer = null;
|
||||
let socketServer = null;
|
||||
|
||||
if (CLIENT_PROTOCOL === 'https:') {
|
||||
staticServer = https.createServer(getCertificate(), app);
|
||||
} else {
|
||||
staticServer = http.createServer(app);
|
||||
}
|
||||
|
||||
if (SERVER_MODE !== 'static') {
|
||||
const socket = new Socket(httpServer ? httpServer : httpsServer, {
|
||||
if (CLIENT_PORT === SERVER_PORT) {
|
||||
socketServer = staticServer;
|
||||
} else {
|
||||
if (SERVER_PROTOCOL === 'wss:') {
|
||||
socketServer = https.createServer(getCertificate());
|
||||
} else {
|
||||
socketServer = http.createServer();
|
||||
}
|
||||
}
|
||||
const socket = new Socket(socketServer, {
|
||||
path: '/mixly-socket/',
|
||||
maxHttpBufferSize: 1e8,
|
||||
cors: {
|
||||
@@ -52,11 +74,11 @@ const mixlyConfig = fsExtra.readJSONSync(mixlyConfigPath);
|
||||
|
||||
if (SERVER_MODE === 'compiler') {
|
||||
mixlyConfig['webCompiler']['enabled'] = true;
|
||||
mixlyConfig['webCompiler']['url'] = `${SERVER_PROTOCOL}://default:${SERVER_PORT}`;
|
||||
mixlyConfig['webCompiler']['url'] = `${SERVER_PROTOCOL}//default:${SERVER_PORT}`;
|
||||
mixlyConfig['webSocket']['enabled'] = false;
|
||||
} else if (SERVER_MODE === 'all') {
|
||||
mixlyConfig['webCompiler']['enabled'] = false;
|
||||
mixlyConfig['webSocket']['url'] = `${SERVER_PROTOCOL}://default:${SERVER_PORT}`;
|
||||
mixlyConfig['webSocket']['url'] = `${SERVER_PROTOCOL}//default:${SERVER_PORT}`;
|
||||
mixlyConfig['webSocket']['enabled'] = true;
|
||||
} else {
|
||||
mixlyConfig['webCompiler']['enabled'] = false;
|
||||
@@ -67,13 +89,17 @@ fsExtra.writeJSONSync(mixlyConfigPath, mixlyConfig, {
|
||||
spaces: ' '
|
||||
});
|
||||
|
||||
httpsServer.listen(CLIENT_PORT);
|
||||
if (httpServer) {
|
||||
httpServer.listen(SERVER_PORT);
|
||||
if (CLIENT_PORT === SERVER_PORT) {
|
||||
staticServer?.listen(CLIENT_PORT);
|
||||
} else {
|
||||
staticServer?.listen(CLIENT_PORT);
|
||||
socketServer?.listen(SERVER_PORT);
|
||||
}
|
||||
|
||||
if (CLIENT_PATH) {
|
||||
console.log(`Static服务器正在运行: https://127.0.0.1:${CLIENT_PORT}`);
|
||||
console.log(`Static服务器正在运行: ${CLIENT_PROTOCOL}//127.0.0.1:${CLIENT_PORT}`);
|
||||
}
|
||||
|
||||
if (SERVER_MODE !== 'static') {
|
||||
console.log(`Socket.io服务器正在运行: ${SERVER_PROTOCOL}://127.0.0.1:${SERVER_PORT}`);
|
||||
console.log(`Socket.io服务器正在运行: ${SERVER_PROTOCOL}//127.0.0.1:${SERVER_PORT}`);
|
||||
}
|
||||
Reference in New Issue
Block a user