Skip to content

Commit 5049d19

Browse files
committed
refactor: use async API
1 parent 768064b commit 5049d19

File tree

1 file changed

+9
-29
lines changed

1 file changed

+9
-29
lines changed

lib/Server.js

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -545,11 +545,11 @@ 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.exists(certificatePath);
549549

550550
if (certificateExists) {
551551
const certificateTtl = 1000 * 60 * 60 * 24;
552-
const certificateStat = fs.statSync(certificatePath);
552+
const certificateStat = await fs.promises.stat(certificatePath);
553553

554554
const now = new Date();
555555

@@ -634,34 +634,18 @@ class Server {
634634
],
635635
});
636636

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

643-
await fs.writeFile(
639+
await fs.promises.writeFile(
644640
certificatePath,
645641
pems.private + pems.cert,
646642
{
647643
encoding: "utf8",
648-
},
649-
(error) => {
650-
if (error) {
651-
this.logger.error(error);
652-
}
653644
}
654645
);
655646
}
656647

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-
});
648+
fakeCert = await fs.promises.readFile(certificatePath);
665649

666650
this.logger.info(`SSL certificate: ${certificatePath}`);
667651
}
@@ -1179,7 +1163,7 @@ class Server {
11791163
app.get("/__webpack_dev_server__/sockjs.bundle.js", (req, res) => {
11801164
res.setHeader("Content-Type", "application/javascript");
11811165

1182-
const { createReadStream } = require("graceful-fs");
1166+
const { createReadStream } = fs;
11831167
const clientPath = path.join(__dirname, "..", "client");
11841168

11851169
createReadStream(
@@ -2054,10 +2038,10 @@ class Server {
20542038
const net = require("net");
20552039
const socket = new net.Socket();
20562040

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

20622046
resolve();
20632047

@@ -2096,11 +2080,7 @@ class Server {
20962080
// chmod 666 (rw rw rw)
20972081
const READ_WRITE = 438;
20982082

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

21062086
if (this.options.webSocketServer) {

0 commit comments

Comments
 (0)