Skip to content

Commit 1ad80b5

Browse files
authored
v4.2.0 Catilina support, check for updates, bug fix (#19)
- 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.
1 parent de4eb74 commit 1ad80b5

File tree

5 files changed

+100
-37
lines changed

5 files changed

+100
-37
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ Checkout the updated list [here](https://github.com/FiloSottile/mkcert/blob/mast
9898
## Troubleshooting
9999
### Node.js version
100100
https-localhost requires Node.js 8 or higher.
101-
<sub>If you need compatibility with previously Node.js versions let me know, I'll try to rearrange the code.</sub>
101+
<sub>If you need compatibility with previously Node.js versions let me know, we'll try to rearrange the code.</sub>
102102

103103
### root required
104104
- **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`.
@@ -123,6 +123,13 @@ It should be present only with `NODE_ENV=production`, hence the easiest fix is t
123123

124124
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.
125125

126+
### ERR_SSL_PROTOCOL_ERROR
127+
And in general all the cases when the script runs but the connection is marked as untrusted.
128+
129+
Force a reinstall of the certificate with `REINSTALL=true serve`. `sudo` may be required on linux and MacOS.
130+
131+
If the problem is solved you should be able to use https-localhost also as module.
132+
126133

127134
## Contributing
128135
Each contribute is welcome!

certs.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,36 @@ const exec = require("child_process").exec
66
const https = require("https")
77
const getAppDataPath = require("appdata-path")
88

9-
const MKCERT_VERSION = "v1.3.0"
9+
const MKCERT_VERSION = "v1.4.0"
1010
const CERT_PATH = getAppDataPath("https-localhost")
1111

12+
// check for updates
13+
/* istanbul ignore next: cannot test pkg */
14+
function checkUpdates() {
15+
try {
16+
const options = {
17+
host: "api.github.com",
18+
path: "/repos/daquinoaldo/https-localhost/releases/latest",
19+
method: "GET",
20+
headers: { "User-Agent": "node.js" }
21+
}
22+
https.request(options, res => {
23+
let body = ""
24+
res.on("data", chunk => { body += chunk.toString("utf8") })
25+
res.on("end", () => {
26+
const currentVersion = JSON.parse(fs.readFileSync(
27+
path.resolve(__dirname, "package.json"))).version
28+
const latestVersion = JSON.parse(body).tag_name.replace("v", "")
29+
if (currentVersion !== latestVersion)
30+
console.warn("[https-localhost] New update available.")
31+
})
32+
}).end()
33+
} catch (e) {
34+
// Just catch everything, this is not a critic part and can fail.
35+
// It is important to not affect the script behavior.
36+
}
37+
}
38+
1239
// get the executable name
1340
function getExe() {
1441
/* istanbul ignore next: tested on all platform on travis */
@@ -71,6 +98,8 @@ function mkcert(appDataPath, exe) {
7198

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

92121
async function getCerts() {
93122
const certPath = process.env.CERT_PATH || CERT_PATH
123+
// check for updates if running as executable
124+
/* istanbul ignore if: cannot test pkg */
125+
if (process.pkg) checkUpdates()
126+
// check if a reinstall is forced or needed by a mkcert update
127+
if (process.env.REINSTALL ||
128+
!fs.existsSync(path.join(certPath, getExe())))
129+
await generate(certPath)
94130
try {
95131
return {
96132
key: fs.readFileSync(path.join(certPath, "localhost.key")),
97133
cert: fs.readFileSync(path.join(certPath, "localhost.crt"))
98134
}
99135
} catch (e) {
136+
/* istanbul ignore else: should never occur */
100137
if (certPath !== CERT_PATH) {
101138
console.error("Cannot find localhost.key and localhost.crt in the" +
102139
" specified path: " + certPath)

package-lock.json

Lines changed: 24 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "https-localhost",
3-
"version": "4.1.3",
3+
"version": "4.2.0",
44
"description": "HTTPS server running on localhost",
55
"main": "index.js",
66
"scripts": {
@@ -51,7 +51,7 @@
5151
"spdy": "^4.0.1"
5252
},
5353
"devDependencies": {
54-
"coveralls": "^3.0.5",
54+
"coveralls": "^3.0.6",
5555
"eslint": "^6.1.0",
5656
"eslint-config-standard": "^13.0.1",
5757
"eslint-plugin-import": "^2.18.2",
@@ -61,6 +61,6 @@
6161
"mocha": "^6.2.0",
6262
"nyc": "^14.1.1",
6363
"pkg": "^4.4.0",
64-
"sinon": "^7.3.2"
64+
"sinon": "^7.4.1"
6565
}
6666
}

test/test.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,34 @@ describe("Testing certs", function() {
7272
})()
7373
})
7474

75+
it("can be installed in custom folder", function(done) {
76+
// inner async function
77+
(async() => {
78+
// set a custom cert path
79+
process.env.CERT_PATH = "test/custom-folder"
80+
// prepare the server with a mock response
81+
app.get("/test/module", (req, res) => res.send("TEST"))
82+
// start the server
83+
await app.listen(HTTPS_PORT)
84+
// make the request and check the output
85+
await makeRequest("/test/module")
86+
.then(res => assert(res.data === "TEST"))
87+
// close the server
88+
app.server.close()
89+
// restore the CERT_PATH to undefined
90+
delete process.env.CERT_PATH
91+
done()
92+
})()
93+
})
94+
7595
it("crashes if certs doesn't exists in custom folder", function(done) {
7696
// inner async function
7797
(async() => {
78-
// set a non-existent custom cert path
79-
process.env.CERT_PATH = "does-not-exist"
98+
// set a custom cert path
99+
process.env.CERT_PATH = "test/custom-folder"
100+
// remove the certificates
101+
fs.unlinkSync("test/custom-folder/localhost.crt")
102+
fs.unlinkSync("test/custom-folder/localhost.key")
80103
// stub the exit function
81104
sinon.stub(process, "exit")
82105
// listen
@@ -86,6 +109,8 @@ describe("Testing certs", function() {
86109
process.exit.restore()
87110
// close the server
88111
app.server.close()
112+
// delete the custom folder
113+
certs.remove(process.env.CERT_PATH)
89114
// restore the CERT_PATH to undefined
90115
delete process.env.CERT_PATH
91116
done()

0 commit comments

Comments
 (0)