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

Feat/v5 #4

Open
wants to merge 3 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 66 additions & 0 deletions config/getHttpsConfig.js
@@ -0,0 +1,66 @@
"use strict"

const fs = require("fs")
const path = require("path")
const crypto = require("crypto")
const chalk = require("react-dev-utils/chalk")
const paths = require("./paths")

// Ensure the certificate and key provided are valid and if not
// throw an easy to debug error
function validateKeyAndCerts({ cert, key, keyFile, crtFile }) {
let encrypted
try {
// publicEncrypt will throw an error with an invalid cert
encrypted = crypto.publicEncrypt(cert, Buffer.from("test"))
} catch (err) {
throw new Error(
`The certificate "${chalk.yellow(crtFile)}" is invalid.\n${err.message}`
)
}

try {
// privateDecrypt will throw an error with an invalid key
crypto.privateDecrypt(key, encrypted)
} catch (err) {
throw new Error(
`The certificate key "${chalk.yellow(keyFile)}" is invalid.\n${
err.message
}`
)
}
}

// Read file and throw an error if it doesn't exist
function readEnvFile(file, type) {
if (!fs.existsSync(file)) {
throw new Error(
`You specified ${chalk.cyan(
type
)} in your env, but the file "${chalk.yellow(file)}" can't be found.`
)
}
return fs.readFileSync(file)
}

// Get the https config
// Return cert files if provided in env, otherwise just true or false
function getHttpsConfig() {
const { SSL_CRT_FILE, SSL_KEY_FILE, HTTPS } = process.env
const isHttps = HTTPS === "true"

if (isHttps && SSL_CRT_FILE && SSL_KEY_FILE) {
const crtFile = path.resolve(paths.appPath, SSL_CRT_FILE)
const keyFile = path.resolve(paths.appPath, SSL_KEY_FILE)
const config = {
cert: readEnvFile(crtFile, "SSL_CRT_FILE"),
key: readEnvFile(keyFile, "SSL_KEY_FILE"),
}

validateKeyAndCerts({ ...config, keyFile, crtFile })
return config
}
return isHttps
}

module.exports = getHttpsConfig
21 changes: 14 additions & 7 deletions config/paths.js
Expand Up @@ -4,6 +4,7 @@
const path = require("path")
const fs = require("fs")
const url = require("url")
const getPublicUrlOrPath = require("react-dev-utils/getPublicUrlOrPath")

// let args = process.argv.slice(2)[0]
// Make sure any symlinks in the project folder are resolved:
Expand Down Expand Up @@ -33,12 +34,17 @@ const getPublicUrl = appPackageJson =>
// single-page apps that may serve index.html for nested URLs like /todos/42.
// We can't use a relative path in HTML because we don't want to load something
// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
function getServedPath(appPackageJson) {
const publicUrl = getPublicUrl(appPackageJson)
const servedUrl =
envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : "/")
return ensureSlash(servedUrl, true)
}
const publicUrlOrPath = getPublicUrlOrPath(
process.env.NODE_ENV === "development",
require(resolveApp("package.json")).homepage,
process.env.PUBLIC_URL
)
// function getServedPath(appPackageJson) {
// const publicUrl = getPublicUrl(appPackageJson)
// const servedUrl =
// envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : "/")
// return ensureSlash(servedUrl, true)
// }

// config after eject: we're in ./config/
module.exports = {
Expand All @@ -59,5 +65,6 @@ module.exports = {
testsSetup: resolveApp("src/setupTests.js"),
appNodeModules: resolveApp("node_modules"),
publicUrl: getPublicUrl(resolveApp("package.json")),
servedPath: getServedPath(resolveApp("package.json")),
// servedPath: getServedPath(resolveApp("package.json")),
publicUrlOrPath,
}
2 changes: 1 addition & 1 deletion config/webpack.config.prod.js
Expand Up @@ -122,7 +122,7 @@ function saveManifest(assets = {}) {
}
// Webpack uses `publicPath` to determine where the app is being served from.
// It requires a trailing slash, or the file assets will get an incorrect path.
const publicPath = paths.servedPath
const publicPath = paths.publicUrlOrPath
// `publicUrl` is just like `publicPath`, but we will provide it to our app
// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript.
// Omit trailing slash as %PUBLIC_URL%/xyz looks better than %PUBLIC_URL%xyz.
Expand Down