Skip to content

Commit

Permalink
refactor: use async API (#3719)
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Aug 25, 2021
1 parent 13cd267 commit 8f20c3e
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,11 +545,18 @@ class Server {
if (!options.https.key || !options.https.cert) {
const certificateDir = Server.findCacheDir();
const certificatePath = path.join(certificateDir, "server.pem");
let certificateExists = fs.existsSync(certificatePath);
let certificateExists;

try {
const certificate = await fs.promises.stat(certificatePath);
certificateExists = certificate.isFile();
} catch {
certificateExists = false;
}

if (certificateExists) {
const certificateTtl = 1000 * 60 * 60 * 24;
const certificateStat = fs.statSync(certificatePath);
const certificateStat = await fs.promises.stat(certificatePath);

const now = new Date();

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

del.sync([certificatePath], { force: true });
await del([certificatePath], { force: true });

certificateExists = false;
}
Expand Down Expand Up @@ -634,13 +641,18 @@ class Server {
],
});

fs.mkdirSync(certificateDir, { recursive: true });
fs.writeFileSync(certificatePath, pems.private + pems.cert, {
encoding: "utf8",
});
await fs.promises.mkdir(certificateDir, { recursive: true });

await fs.promises.writeFile(
certificatePath,
pems.private + pems.cert,
{
encoding: "utf8",
}
);
}

fakeCert = fs.readFileSync(certificatePath);
fakeCert = await fs.promises.readFile(certificatePath);

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

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

createReadStream(
Expand Down Expand Up @@ -2075,7 +2087,7 @@ class Server {
// chmod 666 (rw rw rw)
const READ_WRITE = 438;

fs.chmodSync(this.options.ipc, READ_WRITE);
await fs.promises.chmod(this.options.ipc, READ_WRITE);
}

if (this.options.webSocketServer) {
Expand Down

0 comments on commit 8f20c3e

Please sign in to comment.