Skip to content

Commit 620eb6b

Browse files
committed
build: migrate to pnpm 12, oxc tools, native test runner, and upgrade dependencies
🤖 Generated with [OpenCode](https://opencode.ai) (Smart-router)
1 parent ee4b5a2 commit 620eb6b

16 files changed

Lines changed: 1592 additions & 4339 deletions

.eslintrc.json

Lines changed: 0 additions & 22 deletions
This file was deleted.

.github/workflows/release.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,32 @@ jobs:
2323
runs-on: ubuntu-latest
2424
steps:
2525
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
26+
- uses: pnpm/action-setup@d9184bf108216479bc5a137cc391f4d7b14c870b # v6.1.0
27+
with:
28+
version: 12.3.4
2629
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
2730
with:
2831
node-version: 26
2932
registry-url: https://registry.npmjs.org/
30-
package-manager-cache: false
33+
cache: pnpm
3134

3235
- name: Bump and push version
3336
if: inputs.upgrade != 'existing'
3437
run: |-
3538
git config --global user.name "github-actions[bot]"
3639
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
37-
npm version ${{ inputs.upgrade }} -m "chore: release %s"
40+
pnpm version ${{ inputs.upgrade }} -m "chore: release %s"
3841
git push --follow-tags
3942
40-
- run: npm publish
43+
- run: pnpm publish
4144

4245
- name: Build and create release
4346
env:
4447
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4548
run: |-
4649
name=$(node -p "require('./package.json').name")
4750
version=$(node -p "require('./package.json').version")
48-
npm i -g pkg
51+
pnpm add --global pkg
4952
pkg --out-path build .
5053
gh release create "v$version" \
5154
--title "Release v$version" \

.github/workflows/stale.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ jobs:
1212
stale:
1313
runs-on: ubuntu-latest
1414
steps:
15-
- uses: actions/stale@4391f3da665fdf50b6810c1a66712fb9ba21aa93 # v11.0.0
16-
with:
17-
delete-branch: true
15+
- uses: actions/stale@4391f3da665fdf50b6810c1a66712fb9ba21aa93 # v11.0.0
16+
with:
17+
delete-branch: true

.github/workflows/test.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ jobs:
2525

2626
steps:
2727
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
28+
- uses: pnpm/action-setup@d9184bf108216479bc5a137cc391f4d7b14c870b # v6.1.0
29+
with:
30+
version: 12.3.4
2831
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
2932
with:
3033
node-version: ${{ matrix.node }}
31-
- run: npm ci
32-
- run: npm test
33-
- uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
34-
if: matrix.os == 'ubuntu-latest' && matrix.node == 26
35-
with:
36-
name: coverage-report
37-
path: coverage/lcov.info
34+
cache: pnpm
35+
- run: pnpm install --frozen-lockfile
36+
- run: pnpm run lint
37+
- run: pnpm test

.oxfmtrc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"$schema": "./node_modules/oxfmt/configuration_schema.json",
3+
"semi": false,
4+
"arrowParens": "avoid",
5+
"singleQuote": false
6+
}

CONTRIBUTING.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Contributing
2+
3+
Thanks for reading this document!
4+
5+
Contributions are welcome, but I have little time to invest in this project. I ask you to test the change extensively to make my review as easier and possible, and I apologize in advance if it will take me some time to review it.
6+
7+
## Helpful commands
8+
9+
```sh
10+
# install dependencies
11+
pnpm install
12+
13+
# type checking and style fixes
14+
pnpm lint
15+
16+
# run tests
17+
pnpm test
18+
```

certs.js

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,24 @@ function checkUpdates() {
1717
host: "api.github.com",
1818
path: "/repos/daquinoaldo/https-localhost/releases/latest",
1919
method: "GET",
20-
headers: { "User-Agent": "node.js" }
20+
headers: { "User-Agent": "node.js" },
2121
}
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.")
22+
https
23+
.request(options, res => {
24+
let body = ""
25+
res.on("data", chunk => {
26+
body += chunk.toString("utf8")
27+
})
28+
res.on("end", () => {
29+
const currentVersion = JSON.parse(
30+
fs.readFileSync(path.resolve(__dirname, "package.json")),
31+
).version
32+
const latestVersion = JSON.parse(body).tag_name.replace("v", "")
33+
if (currentVersion !== latestVersion)
34+
console.warn("[https-localhost] New update available.")
35+
})
3136
})
32-
}).end()
37+
.end()
3338
} catch (e) {
3439
// Just catch everything, this is not a critic part and can fail.
3540
// It is important to not affect the script behavior.
@@ -50,8 +55,10 @@ function getExe() {
5055
case "win32":
5156
return "mkcert-" + MKCERT_VERSION + "-windows-amd64.exe"
5257
default:
53-
console.warn("Cannot generate the localhost certificate on your " +
54-
"platform. Please, consider contacting the developer if you can help.")
58+
console.warn(
59+
"Cannot generate the localhost certificate on your " +
60+
"platform. Please, consider contacting the developer if you can help.",
61+
)
5562
process.exit(0)
5663
}
5764
}
@@ -62,7 +69,7 @@ function download(url, path) {
6269
const file = fs.createWriteStream(path)
6370
return new Promise(resolve => {
6471
function get(url, file) {
65-
https.get(url, (response) => {
72+
https.get(url, response => {
6673
if (response.statusCode === 302) get(response.headers.location, file)
6774
else response.pipe(file).on("finish", resolve)
6875
})
@@ -75,26 +82,23 @@ function download(url, path) {
7582
async function mkcert(appDataPath, exe, domain) {
7683
// fix problems with spaces
7784
/* istanbul ignore next: platform dependent */
78-
const escapeSpaces = function(path) {
85+
const escapeSpaces = function (path) {
7986
// escape spaces (not already escaped)
8087
if (process.platform === "darwin" || process.platform === "linux")
8188
return path.replace(/(?<!\\) /g, "\\ ")
8289
// use apex on Windows
83-
if (process.platform === "win32")
84-
return "\"" + path + "\""
90+
if (process.platform === "win32") return '"' + path + '"'
8591
return path
8692
}
8793

8894
const exePath = escapeSpaces(path.join(appDataPath, exe))
8995
const crtPath = escapeSpaces(path.join(appDataPath, domain + ".crt"))
9096
const keyPath = escapeSpaces(path.join(appDataPath, domain + ".key"))
91-
const cmd = `${exePath} -install -cert-file ${crtPath}` +
92-
` -key-file ${keyPath} ${domain}`
97+
const cmd = `${exePath} -install -cert-file ${crtPath}` + ` -key-file ${keyPath} ${domain}`
9398

9499
// sleep on windows due to issue #28
95100
/* istanbul ignore if: cannot be tested */
96-
if (process.platform === "win32")
97-
await new Promise(resolve => setTimeout(resolve, 3000))
101+
if (process.platform === "win32") await new Promise(resolve => setTimeout(resolve, 3000))
98102

99103
return new Promise((resolve, reject) => {
100104
console.log("Running mkcert to generate certificates...")
@@ -117,15 +121,12 @@ async function mkcert(appDataPath, exe, domain) {
117121
async function generate(appDataPath = CERT_PATH, customDomain = undefined) {
118122
const domain = customDomain || "localhost"
119123
console.info("Generating certificates...")
120-
console.log("Certificates path: " + appDataPath +
121-
". Never modify nor share this files.")
124+
console.log("Certificates path: " + appDataPath + ". Never modify nor share this files.")
122125
// mkdir if not exists
123126
/* istanbul ignore else: not relevant */
124-
if (!fs.existsSync(appDataPath))
125-
fs.mkdirSync(appDataPath, { recursive: true })
127+
if (!fs.existsSync(appDataPath)) fs.mkdirSync(appDataPath, { recursive: true })
126128
// build the executable url and path
127-
const url = "https://github.com/FiloSottile/mkcert/releases/download/" +
128-
MKCERT_VERSION + "/"
129+
const url = "https://github.com/FiloSottile/mkcert/releases/download/" + MKCERT_VERSION + "/"
129130
const exe = getExe()
130131
const exePath = path.join(appDataPath, exe)
131132
// download the executable
@@ -144,19 +145,19 @@ async function getCerts(customDomain = undefined) {
144145
/* istanbul ignore if: cannot test pkg */
145146
if (process.pkg) checkUpdates()
146147
// check if a reinstall is forced or needed by a mkcert update
147-
if (process.env.REINSTALL ||
148-
!fs.existsSync(path.join(certPath, getExe())))
148+
if (process.env.REINSTALL || !fs.existsSync(path.join(certPath, getExe())))
149149
await generate(certPath, domain)
150150
try {
151151
return {
152152
key: fs.readFileSync(path.join(certPath, domain + ".key")),
153-
cert: fs.readFileSync(path.join(certPath, domain + ".crt"))
153+
cert: fs.readFileSync(path.join(certPath, domain + ".crt")),
154154
}
155155
} catch (e) {
156156
/* istanbul ignore else: should never occur */
157157
if (certPath !== CERT_PATH) {
158-
console.error("Cannot find localhost.key and localhost.crt in the" +
159-
" specified path: " + certPath)
158+
console.error(
159+
"Cannot find localhost.key and localhost.crt in the" + " specified path: " + certPath,
160+
)
160161
process.exit(1)
161162
} else {
162163
// Missing certificates (first run)
@@ -171,8 +172,7 @@ async function getCerts(customDomain = undefined) {
171172
// delete a folder and the file inside it
172173
function remove(appDataPath = CERT_PATH) {
173174
if (fs.existsSync(appDataPath)) {
174-
fs.readdirSync(appDataPath)
175-
.forEach(file => fs.unlinkSync(path.join(appDataPath, file)))
175+
fs.readdirSync(appDataPath).forEach(file => fs.unlinkSync(path.join(appDataPath, file)))
176176
fs.rmdirSync(appDataPath)
177177
}
178178
}
@@ -181,17 +181,23 @@ function remove(appDataPath = CERT_PATH) {
181181
/* istanbul ignore if: cannot be tested */
182182
if (require.main === module)
183183
// if run with -u or --uninstall
184-
if (process.argv.length === 3 &&
185-
(process.argv[2] === "-u" || process.argv[2] === "--uninstall")) {
184+
if (
185+
process.argv.length === 3 &&
186+
(process.argv[2] === "-u" || process.argv[2] === "--uninstall")
187+
) {
186188
remove()
187189
console.info("Certificates removed.")
188-
} else try { // install
189-
generate()
190-
} catch (err) { console.error("\nExec error: " + err) }
190+
} else
191+
try {
192+
// install
193+
generate()
194+
} catch (err) {
195+
console.error("\nExec error: " + err)
196+
}
191197

192198
// export as module
193199
module.exports = {
194200
getCerts,
195201
generate,
196-
remove
202+
remove,
197203
}

index.js

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ const createServer = (domain = process.env.HOST || "localhost") => {
2222
app.getCerts = getCerts
2323

2424
// override the default express listen method to use our server
25-
app.listen = async function(port = process.env.PORT ||
26-
/* istanbul ignore next: cannot be tested on Travis */ 443) {
27-
app.server = https.createServer(await getCerts(domain), app)
28-
.listen(port)
25+
app.listen = async function (
26+
port = process.env.PORT || /* istanbul ignore next: cannot be tested on Travis */ 443,
27+
) {
28+
app.server = https.createServer(await getCerts(domain), app).listen(port)
2929
console.info("Server running on port " + port + ".")
3030
return app.server
3131
}
@@ -42,36 +42,41 @@ const createServer = (domain = process.env.HOST || "localhost") => {
4242
/* SETUP USEFUL FUNCTIONS */
4343

4444
// redirect http to https, usage `app.redirect()`
45-
app.redirect = function(
45+
app.redirect = function (
4646
/* istanbul ignore next: cannot be tested on Travis */ httpPort = 80,
47-
httpsPort = process.env.PORT || 443) {
48-
app.http = http.createServer((req, res) => {
49-
const reqHost = req.headers.host
50-
? req.headers.host.replace(":" + httpPort, "")
51-
: /* istanbul ignore next: cannot be tested */ "localhost"
52-
res.writeHead(301, {
53-
Location: "https://" + reqHost +
54-
(httpsPort !== 443 ? ":" + httpsPort : "") + (req.url ||
55-
/* istanbul ignore next: cannot be tested */ "")
47+
httpsPort = process.env.PORT || 443,
48+
) {
49+
app.http = http
50+
.createServer((req, res) => {
51+
const reqHost = req.headers.host
52+
? req.headers.host.replace(":" + httpPort, "")
53+
: /* istanbul ignore next: cannot be tested */ "localhost"
54+
res.writeHead(301, {
55+
Location:
56+
"https://" +
57+
reqHost +
58+
(httpsPort !== 443 ? ":" + httpsPort : "") +
59+
(req.url || /* istanbul ignore next: cannot be tested */ ""),
60+
})
61+
res.end()
5662
})
57-
res.end()
58-
}).listen(httpPort)
63+
.listen(httpPort)
5964
console.info("http to https redirection active.")
6065
}
6166

6267
// serve static content, usage `app.serve([path])`
63-
app.serve = function(staticPath = process.cwd(), port = process.env.PORT ||
64-
/* istanbul ignore next: cannot be tested on Travis */ 443) {
68+
app.serve = function (
69+
staticPath = process.cwd(),
70+
port = process.env.PORT || /* istanbul ignore next: cannot be tested on Travis */ 443,
71+
) {
6572
app.use(express.static(staticPath))
6673
// redirect 404 to 404.html or to index.html
6774
app.use((req, res) => {
6875
const p404 = staticPath + "/404.html"
6976
const index = staticPath + "/index.html"
7077
// istanbul ignore else: not interesting
71-
if (fs.existsSync(p404))
72-
res.status(404).sendFile(path.resolve(p404))
73-
else if (fs.existsSync(index))
74-
res.status(200).sendFile(path.resolve(index))
78+
if (fs.existsSync(p404)) res.status(404).sendFile(path.resolve(p404))
79+
else if (fs.existsSync(index)) res.status(200).sendFile(path.resolve(index))
7580
else res.status(404).send(req.path + " not found.")
7681
})
7782
console.info("Serving static path: " + staticPath)
@@ -94,17 +99,20 @@ if (require.main === module) {
9499
if (!process.env.PORT) app.redirect()
95100

96101
/* istanbul ignore next: cannot be tested */
97-
process.on("uncaughtException", function(err) {
102+
process.on("uncaughtException", function (err) {
98103
switch (err.errno) {
99104
case "EACCES":
100105
console.error(
101106
"EACCES: run as administrator to use the default ports 443 and 80. " +
102-
"You can also change port with: `PORT=4433 serve ~/myproj`.")
107+
"You can also change port with: `PORT=4433 serve ~/myproj`.",
108+
)
103109
break
104110
case "EADDRINUSE":
105-
console.error("EADDRINUSE: another service on your machine is using " +
106-
"the current port.\nStop it or change port with:" +
107-
"`PORT=4433 serve ~/myproj`.")
111+
console.error(
112+
"EADDRINUSE: another service on your machine is using " +
113+
"the current port.\nStop it or change port with:" +
114+
"`PORT=4433 serve ~/myproj`.",
115+
)
108116
break
109117
default:
110118
console.error("Unexpected error " + err.errno + ":\n\n" + err)

0 commit comments

Comments
 (0)