Skip to content

Commit

Permalink
v4.2.0 Catilina support, check for updates, bug fix (#19)
Browse files Browse the repository at this point in the history
- Update to mkcert v1.4.0: macOS Catalina compatibility and more. https://github.com/FiloSottile/mkcert/releases/tag/v1.4.0
- Add `REINSTALL=true serve` env variable to force reinstall certs.
- Check for updates if running as compiled executable (downloaded from GitHub releases).
- Update dependencies.
- Add troubleshooting for #13.
  • Loading branch information
daquinoaldo authored Aug 18, 2019
1 parent de4eb74 commit 1ad80b5
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 37 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Checkout the updated list [here](https://github.com/FiloSottile/mkcert/blob/mast
## Troubleshooting
### Node.js version
https-localhost requires Node.js 8 or higher.
<sub>If you need compatibility with previously Node.js versions let me know, I'll try to rearrange the code.</sub>
<sub>If you need compatibility with previously Node.js versions let me know, we'll try to rearrange the code.</sub>

### root required
- **At first run** this tool generate a trusted certificate. The sudo password may be required. If you cannot provide the sudo password generate a `localhost.key` and `localhost.crt` and specify its path with `CERT_PATH=/diractory/containing/certificates/ serve ~/myproj`.
Expand All @@ -123,6 +123,13 @@ It should be present only with `NODE_ENV=production`, hence the easiest fix is t

I've tried to reproduce this error without any success (checkout the [Travis build logs](https://travis-ci.org/daquinoaldo/https-localhost)). If you can help please open an issue and describe as better as you can how to reproduce it, I'll be happy to help you.

### ERR_SSL_PROTOCOL_ERROR
And in general all the cases when the script runs but the connection is marked as untrusted.

Force a reinstall of the certificate with `REINSTALL=true serve`. `sudo` may be required on linux and MacOS.

If the problem is solved you should be able to use https-localhost also as module.


## Contributing
Each contribute is welcome!
Expand Down
39 changes: 38 additions & 1 deletion certs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,36 @@ const exec = require("child_process").exec
const https = require("https")
const getAppDataPath = require("appdata-path")

const MKCERT_VERSION = "v1.3.0"
const MKCERT_VERSION = "v1.4.0"
const CERT_PATH = getAppDataPath("https-localhost")

// check for updates
/* istanbul ignore next: cannot test pkg */
function checkUpdates() {
try {
const options = {
host: "api.github.com",
path: "/repos/daquinoaldo/https-localhost/releases/latest",
method: "GET",
headers: { "User-Agent": "node.js" }
}
https.request(options, res => {
let body = ""
res.on("data", chunk => { body += chunk.toString("utf8") })
res.on("end", () => {
const currentVersion = JSON.parse(fs.readFileSync(
path.resolve(__dirname, "package.json"))).version
const latestVersion = JSON.parse(body).tag_name.replace("v", "")
if (currentVersion !== latestVersion)
console.warn("[https-localhost] New update available.")
})
}).end()
} catch (e) {
// Just catch everything, this is not a critic part and can fail.
// It is important to not affect the script behavior.
}
}

// get the executable name
function getExe() {
/* istanbul ignore next: tested on all platform on travis */
Expand Down Expand Up @@ -71,6 +98,8 @@ function mkcert(appDataPath, exe) {

async function generate(appDataPath = CERT_PATH) {
console.info("Generating certificates...")
console.log("Certificates path: " + appDataPath +
". Never modify nor share this files.")
// mkdir if not exists
/* istanbul ignore else: not relevant */
if (!fs.existsSync(appDataPath))
Expand All @@ -91,12 +120,20 @@ async function generate(appDataPath = CERT_PATH) {

async function getCerts() {
const certPath = process.env.CERT_PATH || CERT_PATH
// check for updates if running as executable
/* istanbul ignore if: cannot test pkg */
if (process.pkg) checkUpdates()
// check if a reinstall is forced or needed by a mkcert update
if (process.env.REINSTALL ||
!fs.existsSync(path.join(certPath, getExe())))
await generate(certPath)
try {
return {
key: fs.readFileSync(path.join(certPath, "localhost.key")),
cert: fs.readFileSync(path.join(certPath, "localhost.crt"))
}
} catch (e) {
/* istanbul ignore else: should never occur */
if (certPath !== CERT_PATH) {
console.error("Cannot find localhost.key and localhost.crt in the" +
" specified path: " + certPath)
Expand Down
54 changes: 24 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "https-localhost",
"version": "4.1.3",
"version": "4.2.0",
"description": "HTTPS server running on localhost",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -51,7 +51,7 @@
"spdy": "^4.0.1"
},
"devDependencies": {
"coveralls": "^3.0.5",
"coveralls": "^3.0.6",
"eslint": "^6.1.0",
"eslint-config-standard": "^13.0.1",
"eslint-plugin-import": "^2.18.2",
Expand All @@ -61,6 +61,6 @@
"mocha": "^6.2.0",
"nyc": "^14.1.1",
"pkg": "^4.4.0",
"sinon": "^7.3.2"
"sinon": "^7.4.1"
}
}
29 changes: 27 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,34 @@ describe("Testing certs", function() {
})()
})

it("can be installed in custom folder", function(done) {
// inner async function
(async() => {
// set a custom cert path
process.env.CERT_PATH = "test/custom-folder"
// prepare the server with a mock response
app.get("/test/module", (req, res) => res.send("TEST"))
// start the server
await app.listen(HTTPS_PORT)
// make the request and check the output
await makeRequest("/test/module")
.then(res => assert(res.data === "TEST"))
// close the server
app.server.close()
// restore the CERT_PATH to undefined
delete process.env.CERT_PATH
done()
})()
})

it("crashes if certs doesn't exists in custom folder", function(done) {
// inner async function
(async() => {
// set a non-existent custom cert path
process.env.CERT_PATH = "does-not-exist"
// set a custom cert path
process.env.CERT_PATH = "test/custom-folder"
// remove the certificates
fs.unlinkSync("test/custom-folder/localhost.crt")
fs.unlinkSync("test/custom-folder/localhost.key")
// stub the exit function
sinon.stub(process, "exit")
// listen
Expand All @@ -86,6 +109,8 @@ describe("Testing certs", function() {
process.exit.restore()
// close the server
app.server.close()
// delete the custom folder
certs.remove(process.env.CERT_PATH)
// restore the CERT_PATH to undefined
delete process.env.CERT_PATH
done()
Expand Down

0 comments on commit 1ad80b5

Please sign in to comment.