Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(index): remove blocking fs.existsSync() call #261

Merged
merged 1 commit into from
Mar 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable security/detect-child-process */
const fs = require("fs");
const fs = require("fs/promises");
const path = require("upath");
const semver = require("semver");
const { execFile, spawn } = require("child_process");
Expand Down Expand Up @@ -152,19 +152,16 @@ class UnRTF {
};

/**
* UnRTF will attempt to convert empty strings/files, and non-RTF files
* so catch them here
* UnRTF will attempt to convert empty strings, missing files,
* and non-RTF files. Catch before passing to binary
*/
if (
file === undefined ||
let buff;
try {
// eslint-disable-next-line security/detect-non-literal-fs-filename
fs.existsSync(path.normalizeTrim(file)) === false
) {
buff = await fs.readFile(path.normalizeTrim(file));
} catch {
throw new Error("File missing");
}

// eslint-disable-next-line security/detect-non-literal-fs-filename
const buff = await fs.promises.readFile(path.normalizeTrim(file));
// Check for RTF specific magic number
if (!/^{\\rtf/m.test(buff.toString())) {
throw new Error(
Expand Down