Skip to content

Commit fd715eb

Browse files
committed
Add certificate support and allow specified protocols to take effect
1 parent f16ee5d commit fd715eb

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

packages/core/assets/services/index.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ export function resolveServiceBuildInfo(service, name, opts: ServiceOptions) {
195195
public: isPublic,
196196
port,
197197
env,
198+
protocol,
199+
ssl,
198200
__autobuild,
199201
__compile,
200202
} = resolvedWithoutSource
@@ -211,11 +213,32 @@ export function resolveServiceBuildInfo(service, name, opts: ServiceOptions) {
211213
: fullFile
212214
: null // Reference correctly from build Electron application
213215

216+
// Resolve SSL certificate paths
217+
let resolvedSSL = undefined
218+
if (ssl?.key && ssl?.cert) {
219+
const keyPath = resolvePath(root, ssl.key)
220+
const certPath = resolvePath(root, ssl.cert)
221+
222+
// Only include SSL if both files exist
223+
if (existsSync(keyPath) && existsSync(certPath)) {
224+
resolvedSSL = {
225+
key: keyPath,
226+
cert: certPath
227+
}
228+
} else {
229+
logger.warn(`SSL configuration provided but certificate files not found:`)
230+
if (!existsSync(keyPath)) logger.warn(` - Key file not found: ${keyPath}`)
231+
if (!existsSync(certPath)) logger.warn(` - Cert file not found: ${certPath}`)
232+
}
233+
}
234+
214235
return {
215236
src,
216237
url,
217238
build,
218239
env,
240+
protocol,
241+
ssl: resolvedSSL,
219242
base: base && resolvePath(root, base),
220243
filepath: file,
221244

@@ -234,7 +257,7 @@ function getLocalUrl(url) {
234257

235258
async function getServiceUrl(service) {
236259
const resolved = resolveServiceConfiguration(service)
237-
const { url, port, src } = resolved
260+
const { url, port, src, ssl, protocol } = resolved
238261

239262
if (!src) return url // Cannot generate URL without source file
240263

@@ -244,6 +267,12 @@ async function getServiceUrl(service) {
244267
if (_url) {
245268
const resolvedPort = port || (await getFreePorts(1))[0]
246269
if (!_url.port) _url.port = resolvedPort.toString() // Use the specified port
270+
271+
// Auto-update protocol to https when SSL is configured
272+
273+
if (protocol) _url.protocol = protocol // Use custom protocol if provided
274+
else if (ssl?.key && ssl?.cert) _url.protocol = 'https:'
275+
247276
return _url.href
248277
}
249278

@@ -278,12 +307,14 @@ export async function resolveService(config, name, opts: ServiceOptions) {
278307
base,
279308
build,
280309
url,
310+
protocol,
311+
ssl,
281312
__src = src && resolve(root, src),
282313
__compile,
283314
__autobuild,
284315
} = resolvedForBuild
285316

286-
resolvedForBuild.url = await getServiceUrl({ src, url, port })
317+
resolvedForBuild.url = await getServiceUrl({ src, url, port, ssl, protocol })
287318

288319
const isMobileTarget = isMobile(target)
289320

@@ -303,6 +334,7 @@ export async function resolveService(config, name, opts: ServiceOptions) {
303334
base,
304335
build, // Build Info
305336
env: resolvedForBuild.env,
337+
ssl: resolvedForBuild.ssl,
306338
__src,
307339
__compile,
308340
__autobuild, // Flags
@@ -361,11 +393,18 @@ export async function start(
361393
// Get service-specific env variables
362394
const serviceEnv = config.env && typeof config.env === 'object' ? config.env : {}
363395

396+
// Add SSL certificate paths to environment if configured
397+
const sslEnv = config.ssl ? {
398+
SSL_KEY_PATH: config.ssl.key,
399+
SSL_CERT_PATH: config.ssl.cert,
400+
} : {}
401+
364402
// Share environment variables with the child process
365403
const env = {
366404
...userEnv,
367405
...process.env,
368406
...serviceEnv,
407+
...sslEnv,
369408
PORT: resolvedURL.port,
370409
HOST: resolvedURL.hostname,
371410
}

packages/core/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,17 @@ type UserBuildCommand = string | ((info: PackageBuildInfo) => string | Promise<s
147147

148148
type ResolvedServices = { [x: string]: ResolvedService }
149149

150+
type SSLConfiguration = {
151+
key: string
152+
cert: string
153+
}
154+
150155
type _ExtraServiceMetadata = {
151156
public?: boolean
152157
port?: number
153158
build?: UserBuildCommand
154159
env?: Record<string, string> | ((services: ResolvedServices) => Record<string, string> | Promise<Record<string, string>>)
160+
ssl?: SSLConfiguration
155161
}
156162

157163
type _ServiceMetadata = string | false | (BaseServiceMetadata & _ExtraServiceMetadata)
@@ -175,6 +181,7 @@ export type ResolvedService = {
175181
base: string | null
176182
build: ExtraServiceMetadata['build']
177183
env?: ExtraServiceMetadata['env']
184+
ssl?: SSLConfiguration
178185
__src: string
179186
__compile: boolean
180187
__autobuild: boolean

0 commit comments

Comments
 (0)