|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/* |
| 4 | + * Wire |
| 5 | + * Copyright (C) 2025 Wire Swiss GmbH |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program. If not, see http://www.gnu.org/licenses/. |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +const fs = require('node:fs'); |
| 23 | +const path = require('node:path'); |
| 24 | + |
| 25 | +/** |
| 26 | + * Automated Event Type Synchronization |
| 27 | + * |
| 28 | + * This script automatically synchronizes EVENT_TYPE constants from the main process |
| 29 | + * to preload scripts that cannot import them directly due to context isolation. |
| 30 | + * |
| 31 | + * Source of truth: electron/src/lib/eventType.ts |
| 32 | + * Targets: |
| 33 | + * - electron/src/shared/contextIsolationConstants.ts |
| 34 | + * - electron/src/preload/preload-app.ts |
| 35 | + * - electron/src/preload/preload-webview.ts (if needed) |
| 36 | + */ |
| 37 | + |
| 38 | +const SOURCE_FILE = path.resolve(__dirname, '../electron/src/lib/eventType.ts'); |
| 39 | +const SHARED_CONSTANTS_FILE = path.resolve(__dirname, '../electron/src/shared/contextIsolationConstants.ts'); |
| 40 | +const PRELOAD_APP_FILE = path.resolve(__dirname, '../electron/src/preload/preload-app.ts'); |
| 41 | + |
| 42 | +/** |
| 43 | + * Extract EVENT_TYPE object from the source TypeScript file |
| 44 | + * @param {string} sourceContent - Content of the source file |
| 45 | + * @returns {string} The EVENT_TYPE object as a string |
| 46 | + */ |
| 47 | +function extractEventType(sourceContent) { |
| 48 | + const regex = /export const EVENT_TYPE = \{/; |
| 49 | + const startMatch = regex.exec(sourceContent); |
| 50 | + if (!startMatch) { |
| 51 | + throw new Error('Could not find EVENT_TYPE export in source file'); |
| 52 | + } |
| 53 | + |
| 54 | + const startIndex = startMatch.index; |
| 55 | + let braceCount = 0; |
| 56 | + let endIndex = startIndex; |
| 57 | + |
| 58 | + for (let i = startIndex; i < sourceContent.length; i++) { |
| 59 | + const char = sourceContent[i]; |
| 60 | + if (char === '{') { |
| 61 | + braceCount++; |
| 62 | + } else if (char === '}') { |
| 63 | + braceCount--; |
| 64 | + if (braceCount === 0) { |
| 65 | + endIndex = i + 1; |
| 66 | + break; |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if (braceCount !== 0) { |
| 72 | + throw new Error('Could not find matching closing brace for EVENT_TYPE'); |
| 73 | + } |
| 74 | + |
| 75 | + return sourceContent.substring(startIndex, endIndex); |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Generate the shared constants file content |
| 80 | + * @param {string} eventTypeObject - The EVENT_TYPE object string |
| 81 | + * @returns {string} Complete file content |
| 82 | + */ |
| 83 | +function generateSharedConstantsContent(eventTypeObject) { |
| 84 | + return `/* |
| 85 | + * Wire |
| 86 | + * Copyright (C) 2018 Wire Swiss GmbH |
| 87 | + * |
| 88 | + * This program is free software: you can redistribute it and/or modify |
| 89 | + * it under the terms of the GNU General Public License as published by |
| 90 | + * the Free Software Foundation, either version 3 of the License, or |
| 91 | + * (at your option) any later version. |
| 92 | + * |
| 93 | + * This program is distributed in the hope that it will be useful, |
| 94 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 95 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 96 | + * GNU General Public License for more details. |
| 97 | + * |
| 98 | + * You should have received a copy of the GNU General Public License |
| 99 | + * along with this program. If not, see http://www.gnu.org/licenses/. |
| 100 | + * |
| 101 | + */ |
| 102 | +
|
| 103 | +/** |
| 104 | + * Shared Context Isolation Constants |
| 105 | + * |
| 106 | + * This file contains constants and utilities that are shared between renderer and preload |
| 107 | + * processes due to context isolation security requirements. With context isolation enabled, |
| 108 | + * these processes cannot directly import modules from the main process, so we need to |
| 109 | + * redefine these constants locally. |
| 110 | + * |
| 111 | + * Security Context: These constants are safe to duplicate because they are just string |
| 112 | + * literals that define event types and logging interfaces. No sensitive functionality |
| 113 | + * is exposed. |
| 114 | + * |
| 115 | + * IMPORTANT: These constants are automatically generated from their main process |
| 116 | + * counterparts using the 'yarn sync:events' command. Do not edit manually. |
| 117 | + * - EVENT_TYPE is automatically generated from electron/src/lib/eventType.ts |
| 118 | + * - WebAppEvents must match @wireapp/webapp-events |
| 119 | + */ |
| 120 | +
|
| 121 | +/** |
| 122 | + * Event type constants for IPC communication between main and renderer/preload processes. |
| 123 | + * These are automatically generated from electron/src/lib/eventType.ts using 'yarn sync:events'. |
| 124 | + * |
| 125 | + * Context Isolation Security: Due to context isolation, we cannot import the main process |
| 126 | + * eventType module directly in renderer/preload, so we maintain this automatically generated copy. |
| 127 | + */ |
| 128 | +// NOSONAR - Duplication required for context isolation, automatically generated |
| 129 | +${eventTypeObject}; |
| 130 | +
|
| 131 | +/** |
| 132 | + * WebApp events constants for preload scripts. |
| 133 | + * These must match the constants from @wireapp/webapp-events. |
| 134 | + */ |
| 135 | +export const WebAppEvents = { |
| 136 | + CONVERSATION: { |
| 137 | + JOIN: 'wire.webapp.conversation.join', |
| 138 | + }, |
| 139 | + LIFECYCLE: { |
| 140 | + CHANGE_ENVIRONMENT: 'wire.webapp.lifecycle.change_environment', |
| 141 | + SSO_WINDOW_CLOSED: 'wire.webapp.lifecycle.sso_window_closed', |
| 142 | + }, |
| 143 | + PROPERTIES: { |
| 144 | + UPDATE: { |
| 145 | + INTERFACE: { |
| 146 | + THEME: 'wire.webapp.properties.update.interface.theme', |
| 147 | + }, |
| 148 | + }, |
| 149 | + UPDATED: 'wire.webapp.properties.updated', |
| 150 | + }, |
| 151 | +} as const; |
| 152 | +
|
| 153 | +/** |
| 154 | + * Simple logger implementation for preload scripts. |
| 155 | + * |
| 156 | + * Context Isolation Security: Main process getLogger cannot be imported in preload |
| 157 | + * scripts. This provides a safe logging interface using console methods. |
| 158 | + * |
| 159 | + * @param {string} name - The name/prefix for the logger |
| 160 | + * @returns {Object} Logger object with info, log, warn, and error methods |
| 161 | + */ |
| 162 | +export const createSandboxLogger = (name: string) => ({ |
| 163 | + info: (message: string, ...args: any[]) => |
| 164 | + // eslint-disable-next-line no-console |
| 165 | + console.info(\`[\${name}] \${message}\`, ...args), |
| 166 | + log: (message: string, ...args: any[]) => |
| 167 | + // eslint-disable-next-line no-console |
| 168 | + console.log(\`[\${name}] \${message}\`, ...args), |
| 169 | + warn: (message: string, ...args: any[]) => |
| 170 | + // eslint-disable-next-line no-console |
| 171 | + console.warn(\`[\${name}] \${message}\`, ...args), |
| 172 | + error: (message: string, ...args: any[]) => |
| 173 | + // eslint-disable-next-line no-console |
| 174 | + console.error(\`[\${name}] \${message}\`, ...args), |
| 175 | +}); |
| 176 | +
|
| 177 | +// Export this to make the file a module and prevent global scope pollution |
| 178 | +export {}; |
| 179 | +`; |
| 180 | +} |
| 181 | + |
| 182 | +/** |
| 183 | + * Update preload-app.ts to sync the duplicated EVENT_TYPE constants |
| 184 | + * Note: Preload scripts cannot import from relative paths due to Electron sandbox limitations, |
| 185 | + * so we maintain synchronized duplicates instead. |
| 186 | + * @param {string} filePath - Path to the preload file |
| 187 | + * @param {string} eventTypeObject - The EVENT_TYPE object string |
| 188 | + */ |
| 189 | +function updatePreloadAppFile(filePath, eventTypeObject) { |
| 190 | + let content = fs.readFileSync(filePath, 'utf8'); |
| 191 | + |
| 192 | + const eventTypeStart = content.indexOf('const EVENT_TYPE = {'); |
| 193 | + if (eventTypeStart === -1) { |
| 194 | + console.log('No EVENT_TYPE duplication found in preload-app.ts'); |
| 195 | + return; |
| 196 | + } |
| 197 | + |
| 198 | + let braceCount = 0; |
| 199 | + let eventTypeEnd = eventTypeStart; |
| 200 | + for (let i = eventTypeStart; i < content.length; i++) { |
| 201 | + const char = content[i]; |
| 202 | + if (char === '{') { |
| 203 | + braceCount++; |
| 204 | + } else if (char === '}') { |
| 205 | + braceCount--; |
| 206 | + if (braceCount === 0) { |
| 207 | + let endPos = i; |
| 208 | + while (endPos < content.length && content[endPos] !== '\n') { |
| 209 | + endPos++; |
| 210 | + } |
| 211 | + eventTypeEnd = endPos; |
| 212 | + break; |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + const beforeEventType = content.substring(0, eventTypeStart); |
| 218 | + const afterEventType = content.substring(eventTypeEnd + 1); |
| 219 | + |
| 220 | + const updatedEventType = `/** |
| 221 | + * Event type constants for IPC communication |
| 222 | + * |
| 223 | + * IMPORTANT: This is automatically synchronized from ../shared/contextIsolationConstants.ts |
| 224 | + * using 'yarn sync:events'. Do not edit manually - run the sync command instead. |
| 225 | + * |
| 226 | + * Context Isolation Security: Due to Electron sandbox limitations, preload scripts cannot |
| 227 | + * import from relative paths, so we maintain this synchronized duplicate. |
| 228 | + */ |
| 229 | +const EVENT_TYPE = ${eventTypeObject.replace('export const EVENT_TYPE = ', '')};`; |
| 230 | + |
| 231 | + content = beforeEventType + updatedEventType + afterEventType; |
| 232 | + |
| 233 | + fs.writeFileSync(filePath, content, 'utf8'); |
| 234 | + console.log('Updated preload-app.ts with synchronized EVENT_TYPE constants'); |
| 235 | +} |
| 236 | + |
| 237 | +function syncEvents() { |
| 238 | + try { |
| 239 | + console.log('Synchronizing EVENT_TYPE constants...'); |
| 240 | + |
| 241 | + const sourceContent = fs.readFileSync(SOURCE_FILE, 'utf8'); |
| 242 | + |
| 243 | + const eventTypeObject = extractEventType(sourceContent); |
| 244 | + |
| 245 | + const sharedContent = generateSharedConstantsContent(eventTypeObject); |
| 246 | + fs.writeFileSync(SHARED_CONSTANTS_FILE, sharedContent, 'utf8'); |
| 247 | + console.log('✓ Updated shared/contextIsolationConstants.ts'); |
| 248 | + |
| 249 | + updatePreloadAppFile(PRELOAD_APP_FILE, eventTypeObject); |
| 250 | + console.log('✓ Updated preload/preload-app.ts'); |
| 251 | + |
| 252 | + console.log('\nEvent type synchronization completed successfully!'); |
| 253 | + console.log('All EVENT_TYPE constants are now in sync with the main process.'); |
| 254 | + |
| 255 | + } catch (error) { |
| 256 | + console.error('Error synchronizing events:', error.message); |
| 257 | + process.exit(1); |
| 258 | + } |
| 259 | +} |
| 260 | + |
| 261 | +syncEvents(); |
0 commit comments