Skip to content

Commit

Permalink
[0.6.x] Support loading certificates from environment variables (#151)
Browse files Browse the repository at this point in the history
* feat: support loading certificates from environment

* refactor: improve code structure

* spaces instead of tabs

* error messages

* wording

Co-authored-by: Tim MacDonald <[email protected]>
  • Loading branch information
innocenzi and timacdonald authored Oct 11, 2022
1 parent c35b819 commit 8fbb885
Showing 1 changed file with 51 additions and 6 deletions.
57 changes: 51 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ function resolveLaravelPlugin(pluginConfig: Required<PluginConfig>): LaravelPlug
const ssr = !! userConfig.build?.ssr
const env = loadEnv(mode, userConfig.envDir || process.cwd(), '')
const assetUrl = env.ASSET_URL ?? ''
const valetServerConfig = command === 'serve'
? resolveValetServerConfig(pluginConfig.valetTls)
const serverConfig = command === 'serve'
? (resolveValetServerConfig(pluginConfig.valetTls) ?? resolveEnvironmentServerConfig(env))
: undefined

ensureCommandShouldRunInEnvironment(command, env)
Expand All @@ -138,14 +138,14 @@ function resolveLaravelPlugin(pluginConfig: Required<PluginConfig>): LaravelPlug
port: userConfig.server?.port ?? (env.VITE_PORT ? parseInt(env.VITE_PORT) : 5173),
strictPort: userConfig.server?.strictPort ?? true,
} : undefined),
...(valetServerConfig ? {
host: userConfig.server?.host ?? valetServerConfig.host,
...(serverConfig ? {
host: userConfig.server?.host ?? serverConfig.host,
hmr: userConfig.server?.hmr === false ? false : {
...valetServerConfig.hmr,
...serverConfig.hmr,
...(userConfig.server?.hmr === true ? {} : userConfig.server?.hmr),
},
https: userConfig.server?.https === false ? false : {
...valetServerConfig.https,
...serverConfig.https,
...(userConfig.server?.https === true ? {} : userConfig.server?.https),
},
} : undefined),
Expand Down Expand Up @@ -439,6 +439,51 @@ function noExternalInertiaHelpers(config: UserConfig): true|Array<string|RegExp>
]
}

/**
* Resolve the server config from the environment.
*/
function resolveEnvironmentServerConfig(env: Record<string, string>): {
hmr?: { host: string }
host?: string,
https?: { cert: Buffer, key: Buffer }
}|undefined {
if (! env.VITE_DEV_SERVER_KEY && ! env.VITE_DEV_SERVER_CERT) {
return
}

if (! fs.existsSync(env.VITE_DEV_SERVER_KEY) || ! fs.existsSync(env.VITE_DEV_SERVER_CERT)) {
throw Error(`Unable to find the certificate files specified in your environment. Ensure you have correctly configured VITE_DEV_SERVER_KEY: [${env.VITE_DEV_SERVER_KEY}] and VITE_DEV_SERVER_CERT: [${env.VITE_DEV_SERVER_CERT}].`)
}

const host = resolveHostFromEnv(env)

if (! host) {
throw Error(`Unable to determine the host from the environment's APP_URL: [${env.APP_URL}].`)
}

return {
hmr: { host },
host,
https: {
key: fs.readFileSync(env.VITE_DEV_SERVER_KEY),
cert: fs.readFileSync(env.VITE_DEV_SERVER_CERT),
},
}
}

/**
* Resolve the host name from the environment.
*/
function resolveHostFromEnv(env: Record<string, string>): string|undefined
{
try {
return new URL(env.APP_URL).host
} catch {
return
}
}


/**
* Resolve the valet server config for the given host.
*/
Expand Down

0 comments on commit 8fbb885

Please sign in to comment.