Skip to content

Commit de202e4

Browse files
committed
Add certificate support for Electron
1 parent fd715eb commit de202e4

File tree

3 files changed

+78
-10
lines changed

3 files changed

+78
-10
lines changed

packages/core/assets/services/index.ts

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isAbsolute, extname, join, resolve, sep } from 'node:path'
1+
import { isAbsolute, extname, join, resolve, sep, relative } from 'node:path'
22
import { getFreePorts } from './network.js'
33

44
import { spawn, fork } from 'node:child_process'
@@ -221,9 +221,25 @@ export function resolveServiceBuildInfo(service, name, opts: ServiceOptions) {
221221

222222
// Only include SSL if both files exist
223223
if (existsSync(keyPath) && existsSync(certPath)) {
224+
// For desktop targets being built, adjust paths for ASAR packaging
225+
// Store paths that will work at runtime
226+
const adjustPathForDesktop = (path: string) => {
227+
if (!isDesktopTarget || !isBuildProcess) return path
228+
229+
// Make path relative to root for packaging
230+
const relativePath = relative(root, path)
231+
232+
// At runtime in Electron, these will be in extraResources
233+
// so we return a marker that will be resolved at runtime
234+
return `__RUNTIME_SSL__/${relativePath}`
235+
}
236+
224237
resolvedSSL = {
225-
key: keyPath,
226-
cert: certPath
238+
key: adjustPathForDesktop(keyPath),
239+
cert: adjustPathForDesktop(certPath),
240+
// Store original paths for build-time asset collection
241+
__keySource: keyPath,
242+
__certSource: certPath,
227243
}
228244
} else {
229245
logger.warn(`SSL configuration provided but certificate files not found:`)
@@ -393,12 +409,44 @@ export async function start(
393409
// Get service-specific env variables
394410
const serviceEnv = config.env && typeof config.env === 'object' ? config.env : {}
395411

412+
// Helper to resolve runtime SSL paths
413+
function resolveRuntimePath(path: string): string {
414+
// If path contains runtime marker, resolve it
415+
if (path.startsWith('__RUNTIME_SSL__/')) {
416+
const relativePath = path.replace('__RUNTIME_SSL__/', '')
417+
418+
// In Electron production, resolve from extraResources
419+
if (typeof process !== 'undefined' && process.resourcesPath) {
420+
const resolvedPath = resolve(process.resourcesPath, 'ssl', relativePath)
421+
logger.debug(`[${label}] SSL path resolved from resources: ${path} -> ${resolvedPath}`)
422+
return resolvedPath
423+
}
424+
425+
// Fallback to original resolution (shouldn't happen)
426+
const resolvedPath = resolve(root, relativePath)
427+
logger.debug(`[${label}] SSL path resolved from root: ${path} -> ${resolvedPath}`)
428+
return resolvedPath
429+
}
430+
431+
// In dev mode, paths should already be absolute
432+
logger.debug(`[${label}] SSL path used as-is: ${path}`)
433+
return path
434+
}
435+
396436
// Add SSL certificate paths to environment if configured
397437
const sslEnv = config.ssl ? {
398-
SSL_KEY_PATH: config.ssl.key,
399-
SSL_CERT_PATH: config.ssl.cert,
438+
SSL_KEY_PATH: resolveRuntimePath(config.ssl.key),
439+
SSL_CERT_PATH: resolveRuntimePath(config.ssl.cert),
400440
} : {}
401441

442+
if (config.ssl) {
443+
logger.debug(`[${label}] SSL configuration:`)
444+
logger.debug(` - SSL_KEY_PATH: ${sslEnv.SSL_KEY_PATH}`)
445+
logger.debug(` - SSL_CERT_PATH: ${sslEnv.SSL_CERT_PATH}`)
446+
logger.debug(` - Key exists: ${existsSync(sslEnv.SSL_KEY_PATH)}`)
447+
logger.debug(` - Cert exists: ${existsSync(sslEnv.SSL_CERT_PATH)}`)
448+
}
449+
402450
// Share environment variables with the child process
403451
const env = {
404452
...userEnv,

packages/core/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,15 @@ export type PackageBuildInfo = {
145145

146146
type UserBuildCommand = string | ((info: PackageBuildInfo) => string | Promise<string>) // e.g. could respond to platform or manually build the executable
147147

148-
type ResolvedServices = { [x: string]: ResolvedService }
149-
150148
type SSLConfiguration = {
151149
key: string
152150
cert: string
151+
__keySource?: string // Original source path for build-time asset collection
152+
__certSource?: string // Original source path for build-time asset collection
153153
}
154154

155+
type ResolvedServices = { [x: string]: ResolvedService }
156+
155157
type _ExtraServiceMetadata = {
156158
public?: boolean
157159
port?: number
@@ -419,8 +421,6 @@ export type ServiceBuildOptions = {
419421
hooks?: HooksInterface // Hooks interface for CLI integration
420422
}
421423

422-
type ResolvedServices = { [x: string]: ResolvedService }
423-
424424
export type ResolvedConfig =Omit<BaseConfig, 'hooks'> & {
425425
build?: BuildOptions
426426
hooks: HooksInterface // Resolved hooks interface

packages/core/utils/assets.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,27 @@ export const getServiceAssets = (
402402
__autobuild?: boolean
403403
}
404404

405-
const { build, base, filepath, __src, __autobuild } = resolvedService
405+
const { build, base, filepath, __src, __autobuild, ssl } = resolvedService
406+
407+
// Include SSL certificate files as extra resources for desktop builds
408+
if (ssl && !dev) {
409+
if (ssl.__keySource) {
410+
assets.copy.push({
411+
input: ssl.__keySource,
412+
// Output relative to build dir, will be placed in extraResources
413+
output: join('ssl', basename(ssl.__keySource)),
414+
extraResource: true,
415+
})
416+
}
417+
if (ssl.__certSource) {
418+
assets.copy.push({
419+
input: ssl.__certSource,
420+
// Output relative to build dir, will be placed in extraResources
421+
output: join('ssl', basename(ssl.__certSource)),
422+
extraResource: true,
423+
})
424+
}
425+
}
406426

407427
const allowCompilation = !(dev && __autobuild)
408428

0 commit comments

Comments
 (0)