Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code refactor in main.ts #1348

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions desktop-app/src/common/deviceList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,45 @@ export const defaultDevices: Device[] = [
isTouchCapable: true,
isMobileCapable: true,
},
{
id: '10022',
name: 'iPhone 16',
width: 393,
height: 852,
dpr: 3,
capabilities: ['touch', 'mobile'],
userAgent:
'Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1',
type: 'phone',
isTouchCapable: true,
isMobileCapable: true,
},
{
id: '10023',
name: 'iPhone 16 Plus',
width: 430,
height: 932,
dpr: 3,
capabilities: ['touch', 'mobile'],
userAgent:
'Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1',
type: 'phone',
isTouchCapable: true,
isMobileCapable: true,
},
{
id: '10024',
name: 'iPhone 16 Pro',
width: 393,
height: 852,
dpr: 3,
capabilities: ['touch', 'mobile'],
userAgent:
'Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 Mobile/15E148 Safari/604.1',
type: 'phone',
isTouchCapable: true,
isMobileCapable: true,
},
{
id: '20001',
name: 'Nexus 4',
Expand Down
50 changes: 21 additions & 29 deletions desktop-app/src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,37 +141,29 @@ const createWindow = async () => {

// Add BROWSER_SYNC_HOST to the allowed Content-Security-Policy origins
mainWindow.webContents.session.webRequest.onHeadersReceived(
async (details, callback) => {
if (details.responseHeaders?.['content-security-policy']) {
let cspHeader = details.responseHeaders['content-security-policy'][0];

cspHeader = cspHeader.replace(
'default-src',
`default-src ${BROWSER_SYNC_HOST}`
);
cspHeader = cspHeader.replace(
'script-src',
`script-src ${BROWSER_SYNC_HOST}`
);
cspHeader = cspHeader.replace(
'script-src-elem',
`script-src-elem ${BROWSER_SYNC_HOST}`
(details, callback) => {
const cspKey = 'content-security-policy';
// Ensure responseHeaders and cspKey exist
if (details.responseHeaders?.[cspKey]) {
const cspHeader = details.responseHeaders[cspKey]?.[0] || '';
// Define the rules to replace dynamically
const replacements: Record<string, string> = {
'default-src': `default-src ${BROWSER_SYNC_HOST}`,
'script-src': `script-src ${BROWSER_SYNC_HOST}`,
'script-src-elem': `script-src-elem ${BROWSER_SYNC_HOST}`,
'connect-src': `connect-src ${BROWSER_SYNC_HOST} wss://${BROWSER_SYNC_HOST} ws://${BROWSER_SYNC_HOST}`,
'child-src': `child-src ${BROWSER_SYNC_HOST}`,
'worker-src': `worker-src ${BROWSER_SYNC_HOST}`,
};
// Apply replacements dynamically
const updatedCSPHeader = Object.entries(replacements).reduce(
(header, [key, value]) => (header ? header.replace(key, value) : ''),
cspHeader
);
cspHeader = cspHeader.replace(
'connect-src',
`connect-src ${BROWSER_SYNC_HOST} wss://${BROWSER_SYNC_HOST} ws://${BROWSER_SYNC_HOST}`
);
cspHeader = cspHeader.replace(
'child-src',
`child-src ${BROWSER_SYNC_HOST}`
);
cspHeader = cspHeader.replace(
'worker-src',
`worker-src ${BROWSER_SYNC_HOST}`
); // Required when/if the browser-sync script is eventually relocated to a web worker

details.responseHeaders['content-security-policy'][0] = cspHeader;
// Update the response headers
details.responseHeaders[cspKey][0] = updatedCSPHeader;
}
// Callback with updated headers
callback({ responseHeaders: details.responseHeaders });
}
);
Expand Down
Loading