Skip to content

Commit

Permalink
Remove express
Browse files Browse the repository at this point in the history
  • Loading branch information
rekkyrosso committed Sep 12, 2024
1 parent 27a8edc commit 9dc4bdc
Show file tree
Hide file tree
Showing 17 changed files with 681 additions and 1,470 deletions.
1,242 changes: 524 additions & 718 deletions app/package-lock.json

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@
"devDependencies": {
"@electron/notarize": "^2.2.1",
"electron": "github:castlabs/electron-releases#v31.3.1+wvcus",
"electron-builder": "24.6.3"
"electron-builder": "^24.13.3"
},
"dependencies": {
"electron-context-menu": "^3.6.1",
"electron-log": "^5.1.2",
"electron-store": "^8.1.0",
"electron-unhandled": "^4.0.1",
"electron-updater": "^6.3.2",
"electron-window-state": "^5.0.3",
"express": "^4.18.1"
"electron-window-state": "^5.0.3"
},
"devEngines": {
"node": ">=20.x",
Expand Down
13 changes: 8 additions & 5 deletions app/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,17 @@ function createBridge() {
mainWindow.setTitleBarOverlay({height});
});

// Switch port
ipcMain.handle('setPort', async (_, newPort) => {
// Preferred port.
ipcMain.handle('getPreferredPort', () => {
return store.port;
});
ipcMain.handle('setPreferredPort', async (_, newPort) => {
parsedPort = parseInt(newPort, 10);
if (parsedPort) {
store.port = parsedPort; // possibly confirmation of change of port from the ui
if (port !== parsedPort) {
if (server.port !== parsedPort) {
await server.stop();
port = await server.start();
const port = await server.start();
url = `http://localhost:${port}/`;
mainWindow.loadURL(url);
}
Expand All @@ -138,7 +141,7 @@ function createBridge() {
}
});

// Credentials
// Credentials.
const credentials = new Store({
name: 'ampcast-credentials',
});
Expand Down
3 changes: 2 additions & 1 deletion app/src/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ contextBridge.exposeInMainWorld('ampcastElectron', {
setFontSize: (fontSize) => ipcRenderer.send('setFontSize', fontSize),
setFrameColor: (color) => ipcRenderer.send('setFrameColor', color),
setFrameTextColor: (color) => ipcRenderer.send('setFrameTextColor', color),
setPort: (port) => ipcRenderer.invoke('setPort', port),
getPreferredPort: () => ipcRenderer.invoke('getPreferredPort'),
setPreferredPort: (port) => ipcRenderer.invoke('setPreferredPort', port),
});
89 changes: 54 additions & 35 deletions app/src/server.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,58 @@
const {resolve} = require('path');
const {createServer} = require('net');
const express = require('express');
const path = require('path');
const fs = require('fs');
const net = require('net');
const http = require('http');
const store = require('./store');

const staticDir = path.resolve(__dirname, '../www');
const host = 'localhost';
const defaultPort = 29292;
const app = express();
const wwwDir = resolve(__dirname, '../www');
const webIndex = resolve(wwwDir, './index.html');

express.static.mime.define({
'application/javascript': ['js'],
'application/json': ['json'],
'image/vnd.microsoft.icon': ['ico'],
'image/png': ['png'],
'image/svg+xml': ['svg'],
'text/css': ['css'],
'text/html': ['html'],
});
const mimeTypes = {
js: 'application/javascript',
json: 'application/json',
ico: 'image/vnd.microsoft.icon',
png: 'image/png',
svg: 'image/svg+xml',
css: 'text/css',
html: 'text/html',
text: 'text/plain',
};

app.get('/', (_, res) => res.sendFile(webIndex));
app.use('/apple-touch-icon.png', express.static(resolve(wwwDir, './apple-touch-icon.png')));
app.use('/favicon.ico', express.static(resolve(wwwDir, './favicon.ico')));
app.use('/favicon.svg', express.static(resolve(wwwDir, './favicon.svg')));
app.use('/icon-192.png', express.static(resolve(wwwDir, './icon-192.png')));
app.use('/icon-512.png', express.static(resolve(wwwDir, './icon-512.png')));
app.use('/manifest.json', express.static(resolve(wwwDir, './manifest.json')));
app.use('/auth', express.static(resolve(wwwDir, './auth')));
app.get('/:version/:id.css', async (req, res) =>
res.sendFile(resolve(wwwDir, `./${req.params.version}/${req.params.id}.css`))
);
app.get('/:version/:id.js', async (req, res) =>
res.sendFile(resolve(wwwDir, `./${req.params.version}/${req.params.id}.js`))
);
app.get('/:version/lib/:id.js', async (req, res) =>
res.sendFile(resolve(wwwDir, `./${req.params.version}/lib/${req.params.id}.js`))
);
app.get('*', (_, res) => res.redirect('/'));
const app = http.createServer(async (req, res) => {
try {
const method = req.method;
const url = req.url.replace(/[?#].*$/, '');
let pathname = url;
if (pathname.endsWith('/')) {
pathname += 'index.html';
}
const filePath = path.join(staticDir, pathname);
const pathTraversal = !filePath.startsWith(staticDir);
const exists =
!pathTraversal &&
(await fs.promises.access(filePath).then(
() => true,
() => false
));
if (exists) {
const ext = path.extname(pathname).substring(1).toLowerCase();
const mimeType = mimeTypes[ext] || mimeTypes.text;
const stream = fs.createReadStream(filePath);
res.writeHead(200, {'Content-Type': mimeType});
stream.pipe(res);
console.info(`${method} ${url} OK`);
} else {
console.warn(`${method} ${url} NOT FOUND`);
res.writeHead(404, {'Content-Type': mimeTypes.text});
res.end('Not found');
}
} catch (err) {
console.error(`${method} ${url} ERROR`);
console.error(err);
res.writeHead(500, {'Content-Type': mimeTypes.text});
res.end('Internal server error');
}
});

let server = null;

Expand Down Expand Up @@ -113,7 +129,7 @@ async function getFreePort(port = defaultPort, endPort = port + 20) {

async function checkPort(port) {
return new Promise((resolve, reject) => {
const server = createServer();
const server = net.createServer();
server.unref();
server.on('error', reject);
server.listen({host, port}, () => {
Expand All @@ -123,6 +139,9 @@ async function checkPort(port) {
}

module.exports = {
get port() {
return server?.address().port || 0;
},
start,
stop,
};
Loading

0 comments on commit 9dc4bdc

Please sign in to comment.