Skip to content

Commit 8f20c3e

Browse files
authored
refactor: use async API (#3719)
1 parent 13cd267 commit 8f20c3e

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

lib/Server.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -545,11 +545,18 @@ class Server {
545545
if (!options.https.key || !options.https.cert) {
546546
const certificateDir = Server.findCacheDir();
547547
const certificatePath = path.join(certificateDir, "server.pem");
548-
let certificateExists = fs.existsSync(certificatePath);
548+
let certificateExists;
549+
550+
try {
551+
const certificate = await fs.promises.stat(certificatePath);
552+
certificateExists = certificate.isFile();
553+
} catch {
554+
certificateExists = false;
555+
}
549556

550557
if (certificateExists) {
551558
const certificateTtl = 1000 * 60 * 60 * 24;
552-
const certificateStat = fs.statSync(certificatePath);
559+
const certificateStat = await fs.promises.stat(certificatePath);
553560

554561
const now = new Date();
555562

@@ -561,7 +568,7 @@ class Server {
561568
"SSL Certificate is more than 30 days old. Removing..."
562569
);
563570

564-
del.sync([certificatePath], { force: true });
571+
await del([certificatePath], { force: true });
565572

566573
certificateExists = false;
567574
}
@@ -634,13 +641,18 @@ class Server {
634641
],
635642
});
636643

637-
fs.mkdirSync(certificateDir, { recursive: true });
638-
fs.writeFileSync(certificatePath, pems.private + pems.cert, {
639-
encoding: "utf8",
640-
});
644+
await fs.promises.mkdir(certificateDir, { recursive: true });
645+
646+
await fs.promises.writeFile(
647+
certificatePath,
648+
pems.private + pems.cert,
649+
{
650+
encoding: "utf8",
651+
}
652+
);
641653
}
642654

643-
fakeCert = fs.readFileSync(certificatePath);
655+
fakeCert = await fs.promises.readFile(certificatePath);
644656

645657
this.logger.info(`SSL certificate: ${certificatePath}`);
646658
}
@@ -1158,7 +1170,7 @@ class Server {
11581170
app.get("/__webpack_dev_server__/sockjs.bundle.js", (req, res) => {
11591171
res.setHeader("Content-Type", "application/javascript");
11601172

1161-
const { createReadStream } = require("graceful-fs");
1173+
const { createReadStream } = fs;
11621174
const clientPath = path.join(__dirname, "..", "client");
11631175

11641176
createReadStream(
@@ -2075,7 +2087,7 @@ class Server {
20752087
// chmod 666 (rw rw rw)
20762088
const READ_WRITE = 438;
20772089

2078-
fs.chmodSync(this.options.ipc, READ_WRITE);
2090+
await fs.promises.chmod(this.options.ipc, READ_WRITE);
20792091
}
20802092

20812093
if (this.options.webSocketServer) {

0 commit comments

Comments
 (0)