Skip to content

Commit

Permalink
fix sonarlint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
imolorhe committed Nov 4, 2023
1 parent 8021d25 commit 277729f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
4 changes: 4 additions & 0 deletions packages/altair-electron/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
root: true,
extends: ['altair'],
};
3 changes: 3 additions & 0 deletions packages/altair-electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@playwright/test": "1.31.2",
"@types/jest": "^28.1.7",
"@types/mime-types": "^2.1.1",
"altair-graphql-core": "^5.2.5",
"devtron": "^1.4.0",
"dotenv": "^8.1.0",
"electron": "^26.2.2",
Expand All @@ -43,6 +44,8 @@
"electron-chromedriver": "^14.0.0",
"electron-reloader": "^1.2.1",
"eslint": "^7.3.1",
"eslint-config-altair": "^5.0.22",
"eslint-config-prettier": "8.5.0",
"jest": "29.4.1",
"jest-circus": "28.0.0",
"playwright": "^1.18.1",
Expand Down
55 changes: 25 additions & 30 deletions packages/altair-electron/src/app/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import { ActionManager } from './actions';
import { TouchbarManager } from './touchbar';
import { handleWithCustomErrors } from '../utils/index';
import { AuthServer } from '../auth/server/index';
import ElectronStore from 'electron-store';
import { getAutobackup, setAutobackup } from '../utils/backup';
import {
IPC_EVENT_NAMES,
ELECTRON_ALLOWED_FORBIDDEN_HEADERS,
} from '@altairgraphql/electron-interop';
import { HeaderState } from 'altair-graphql-core/build/types/state/header.interfaces';
import { log } from '../utils/log';

export class WindowManager {
Expand Down Expand Up @@ -108,7 +108,6 @@ export class WindowManager {
slashes: true,
})
);
// instance.loadURL('http://localhost:4200/');

this.manageEvents();
}
Expand All @@ -124,11 +123,7 @@ export class WindowManager {
initSettingsStoreEvents();
initUpdateAvailableEvent(this.instance.webContents);
// Prevent the app from navigating away from the app
this.instance.webContents.on('will-navigate', e => e.preventDefault());

// instance.webContents.once('dom-ready', () => {
// instance.webContents.openDevTools();
// });
this.instance.webContents.on('will-navigate', (e) => e.preventDefault());

// Emitted when the window is closed.
this.instance.on('closed', () => {
Expand All @@ -151,7 +146,7 @@ export class WindowManager {
session.defaultSession.webRequest.onBeforeRequest((details, callback) => {
log('Before request:', details);
if (details.uploadData) {
details.uploadData.forEach(uploadData => {
details.uploadData.forEach((uploadData) => {
log('Data sent:', uploadData.bytes.toString());
});
}
Expand Down Expand Up @@ -179,9 +174,9 @@ export class WindowManager {
);

if (process.env.NODE_ENV /* === 'test'*/) {
session.defaultSession.webRequest.onSendHeaders(details => {
session.defaultSession.webRequest.onSendHeaders((details) => {
if (details.requestHeaders) {
Object.keys(details.requestHeaders).forEach(headerKey => {
Object.keys(details.requestHeaders).forEach((headerKey) => {
log('Header sent:', headerKey, details.requestHeaders[headerKey]);
});
}
Expand All @@ -193,8 +188,6 @@ export class WindowManager {
details.resourceType === 'mainFrame' ||
details.resourceType === 'subFrame'
) {
// log('received headers..', details.responseHeaders);

// Set the CSP
const scriptSrc = [
`'self'`,
Expand All @@ -207,14 +200,14 @@ export class WindowManager {
];

return callback({
responseHeaders: Object.assign({}, details.responseHeaders, {
// Setting CSP
responseHeaders: {
...details.responseHeaders, // Setting CSP
// TODO: Figure out why an error from this breaks devtools
'Content-Security-Policy': [
`script-src ${scriptSrc.join(' ')}; object-src 'self';`,
// `script-src 'self' 'sha256-1Sj1x3xsk3UVwnakQHbO0yQ3Xm904avQIfGThrdrjcc=' '${createSha256CspHash(renderInitialOptions())}' https://cdn.jsdelivr.net localhost:*; object-src 'self';`
],
}),
},
});
}

Expand All @@ -226,14 +219,13 @@ export class WindowManager {
app.exit();
});

// TODO: Get type from altair-app as a devDependency
// Get 'set headers' instruction from app
ipcMain.on(
IPC_EVENT_NAMES.RENDERER_SET_HEADERS_SYNC,
(e, headers: { key: string; value: string; enabled?: boolean }[]) => {
(e, headers: HeaderState) => {
this.requestHeaders = {};

headers.forEach(header => {
headers.forEach((header) => {
const normalizedKey = header.key.toLowerCase();
if (
ELECTRON_ALLOWED_FORBIDDEN_HEADERS.includes(normalizedKey) &&
Expand Down Expand Up @@ -270,7 +262,7 @@ export class WindowManager {
this.electronApp.store.delete('opened-file-data');
});

ipcMain.handle('reload-window', e => {
ipcMain.handle('reload-window', (e) => {
e.sender.reload();
});

Expand All @@ -282,18 +274,21 @@ export class WindowManager {
);

// TODO: Create an electron-interop package and move this there
handleWithCustomErrors(IPC_EVENT_NAMES.RENDERER_GET_AUTH_TOKEN, async e => {
if (!e.sender || e.sender !== this.instance?.webContents) {
throw new Error('untrusted source trying to get auth token');
}
handleWithCustomErrors(
IPC_EVENT_NAMES.RENDERER_GET_AUTH_TOKEN,
async (e) => {
if (!e.sender || e.sender !== this.instance?.webContents) {
throw new Error('untrusted source trying to get auth token');
}

const authServer = new AuthServer();
return authServer.getCustomToken();
});
const authServer = new AuthServer();
return authServer.getCustomToken();
}
);

handleWithCustomErrors(
IPC_EVENT_NAMES.RENDERER_GET_AUTOBACKUP_DATA,
async e => {
async (e) => {
if (!e.sender || e.sender !== this.instance?.webContents) {
throw new Error('untrusted source');
}
Expand All @@ -307,7 +302,7 @@ export class WindowManager {
/**
* Using a custom buffer protocol, instead of a file protocol because of restrictions with the file protocol.
*/
protocol.handle('altair', async request => {
protocol.handle('altair', async (request) => {
const requestDirectory = getDistDirectory();
const originalFilePath = path.join(
requestDirectory,
Expand Down Expand Up @@ -358,7 +353,7 @@ export class WindowManager {
if (!filePath) {
filePath = fallbackPath;
}
if (filePath && filePath.endsWith('.map')) {
if (filePath?.endsWith('.map')) {
return {
mimeType: 'text/plain',
data: Buffer.from(
Expand All @@ -370,7 +365,7 @@ export class WindowManager {
// some files are binary files, eg. font, so don't encode utf8
let data = await fs.readFile(filePath);

if (filePath && filePath.includes('index.html')) {
if (filePath?.includes('index.html')) {
data = Buffer.from(renderAltair(), 'utf-8');
}

Expand Down

0 comments on commit 277729f

Please sign in to comment.