Skip to content

Commit 6b5ae94

Browse files
committed
refactor: use async API
1 parent 768064b commit 6b5ae94

File tree

1 file changed

+12
-29
lines changed

1 file changed

+12
-29
lines changed

lib/Server.js

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -545,11 +545,14 @@ 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 = await fs.promises.access(
549+
certificatePath,
550+
fs.constants.F_OK
551+
);
549552

550553
if (certificateExists) {
551554
const certificateTtl = 1000 * 60 * 60 * 24;
552-
const certificateStat = fs.statSync(certificatePath);
555+
const certificateStat = await fs.promises.stat(certificatePath);
553556

554557
const now = new Date();
555558

@@ -634,34 +637,18 @@ class Server {
634637
],
635638
});
636639

637-
await fs.mkdir(certificateDir, { recursive: true }, (error) => {
638-
if (error) {
639-
this.logger.error(error);
640-
}
641-
});
640+
await fs.promises.mkdir(certificateDir, { recursive: true });
642641

643-
await fs.writeFile(
642+
await fs.promises.writeFile(
644643
certificatePath,
645644
pems.private + pems.cert,
646645
{
647646
encoding: "utf8",
648-
},
649-
(error) => {
650-
if (error) {
651-
this.logger.error(error);
652-
}
653647
}
654648
);
655649
}
656650

657-
fakeCert = await new Promise((resolve, reject) => {
658-
fs.readFile(certificatePath, (error, data) => {
659-
if (error) {
660-
reject(error);
661-
}
662-
resolve(data);
663-
});
664-
});
651+
fakeCert = await fs.promises.readFile(certificatePath);
665652

666653
this.logger.info(`SSL certificate: ${certificatePath}`);
667654
}
@@ -1179,7 +1166,7 @@ class Server {
11791166
app.get("/__webpack_dev_server__/sockjs.bundle.js", (req, res) => {
11801167
res.setHeader("Content-Type", "application/javascript");
11811168

1182-
const { createReadStream } = require("graceful-fs");
1169+
const { createReadStream } = fs;
11831170
const clientPath = path.join(__dirname, "..", "client");
11841171

11851172
createReadStream(
@@ -2054,10 +2041,10 @@ class Server {
20542041
const net = require("net");
20552042
const socket = new net.Socket();
20562043

2057-
socket.on("error", (error) => {
2044+
socket.on("error", async (error) => {
20582045
if (error.code === "ECONNREFUSED") {
20592046
// No other server listening on this socket so it can be safely removed
2060-
fs.unlinkSync(this.options.ipc);
2047+
await fs.promises.unlink(this.options.ipc);
20612048

20622049
resolve();
20632050

@@ -2096,11 +2083,7 @@ class Server {
20962083
// chmod 666 (rw rw rw)
20972084
const READ_WRITE = 438;
20982085

2099-
await fs.chmod(this.options.ipc, READ_WRITE, (error) => {
2100-
if (error) {
2101-
this.logger.error(error);
2102-
}
2103-
});
2086+
await fs.promises.chmod(this.options.ipc, READ_WRITE);
21042087
}
21052088

21062089
if (this.options.webSocketServer) {

0 commit comments

Comments
 (0)