Skip to content

Commit

Permalink
fix(index): stdout/stderr usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Frazer Smith committed Apr 21, 2021
1 parent 4e658b6 commit a5fa54c
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 50 deletions.
179 changes: 131 additions & 48 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ const util = require("util");
const execFileAsync = util.promisify(execFile);
const platform = os.platform();

const errorMessages = {
0: "No Error.",
1: "Error opening a PDF file.",
2: "Error opening an output file.",
3: "Error related to PDF permissions",
99: "Other error.",
};

/**
* @author Frazer Smith
* @description Check each option provided is valid, of the correct type, and can be used by specified
Expand Down Expand Up @@ -258,12 +266,23 @@ class Poppler {
child.stdin.end();
}

let stdOut = "";
let stdErr = "";

child.stdout.on("data", async (data) => {
resolve(data.toString());
stdOut += data;
});

child.stderr.on("data", async (data) => {
reject(new Error(data));
stdErr += data;
});

child.on("close", async () => {
if (stdOut !== "") {
resolve(stdOut.trim());
} else {
reject(new Error(stdErr.trim()));
}
});
});
} catch (err) {
Expand Down Expand Up @@ -352,19 +371,30 @@ class Poppler {
if (outputPrefix) {
child.on("close", async (code) => {
if (code === 0) {
resolve("Code: 0, no error");
resolve(errorMessages[code]);
} else {
reject(new Error(`Error Code: ${code.toString()}`));
reject(new Error(errorMessages[code]));
}
});
} else {
child.stdout.on("data", async (data) => {
resolve(data);
});
}

let stdOut = "";
let stdErr = "";

child.stdout.on("data", async (data) => {
stdOut += data;
});

child.stderr.on("data", async (data) => {
reject(new Error(data));
stdErr += data;
});

child.on("close", async () => {
if (stdOut !== "") {
resolve(stdOut.trim());
} else {
reject(new Error(stdErr.trim()));
}
});
});
} catch (err) {
Expand Down Expand Up @@ -455,12 +485,23 @@ class Poppler {
child.stdin.end();
}

let stdOut = "";
let stdErr = "";

child.stdout.on("data", async (data) => {
resolve(data.toString());
stdOut += data;
});

child.stderr.on("data", async (data) => {
reject(new Error(data));
stdErr += data;
});

child.on("close", async () => {
if (stdOut !== "") {
resolve(stdOut.trim());
} else {
reject(new Error(stdErr.trim()));
}
});
});
} catch (err) {
Expand Down Expand Up @@ -714,19 +755,30 @@ class Poppler {
if (outputFile) {
child.on("close", async (code) => {
if (code === 0) {
resolve("Code: 0, no error");
resolve(errorMessages[code]);
} else {
reject(new Error(`Error Code: ${code.toString()}`));
reject(new Error(errorMessages[code]));
}
});
} else {
child.stdout.on("data", async (data) => {
resolve(data);
});
}

let stdOut = "";
let stdErr = "";

child.stdout.on("data", async (data) => {
stdOut += data;
});

child.stderr.on("data", async (data) => {
reject(new Error(data));
stdErr += data;
});

child.on("close", async () => {
if (stdOut !== "") {
resolve(stdOut.trim());
} else {
reject(new Error(stdErr.trim()));
}
});
});
} catch (err) {
Expand Down Expand Up @@ -835,12 +887,23 @@ class Poppler {
child.stdin.end();
}

let stdOut = "";
let stdErr = "";

child.stdout.on("data", async (data) => {
resolve(data.toString());
stdOut += data;
});

child.stderr.on("data", async (data) => {
reject(new Error(data));
stdErr += data;
});

child.on("close", async () => {
if (stdOut !== "") {
resolve(stdOut.trim());
} else {
reject(new Error(stdErr.trim()));
}
});
});
} catch (err) {
Expand Down Expand Up @@ -1028,22 +1091,20 @@ class Poppler {
child.stdin.end();
}

if (outputPath) {
child.on("close", async (code) => {
if (code === 0) {
resolve("Code: 0, no error");
} else {
reject(new Error(`Error Code: ${code.toString()}`));
}
});
} else {
child.stdout.on("data", async (data) => {
resolve(data);
});
}
let stdErr = "";

child.stderr.on("data", async (data) => {
reject(new Error(data));
stdErr += data;
});

child.on("close", async (code) => {
if (stdErr !== "") {
reject(new Error(stdErr.trim()));
} else if (code === 0) {
resolve(errorMessages[code]);
} else {
reject(new Error(errorMessages[code]));
}
});
});
} catch (err) {
Expand Down Expand Up @@ -1272,19 +1333,30 @@ class Poppler {
if (outputFile) {
child.on("close", async (code) => {
if (code === 0) {
resolve("Code: 0, no error");
resolve(errorMessages[code]);
} else {
reject(new Error(`Error Code: ${code.toString()}`));
reject(new Error(errorMessages[code]));
}
});
} else {
child.stdout.on("data", async (data) => {
resolve(data);
});
}

let stdOut = "";
let stdErr = "";

child.stdout.on("data", async (data) => {
stdOut += data;
});

child.stderr.on("data", async (data) => {
reject(new Error(data));
stdErr += data;
});

child.on("close", async () => {
if (stdOut !== "") {
resolve(stdOut.trim());
} else {
reject(new Error(stdErr.trim()));
}
});
});
} catch (err) {
Expand Down Expand Up @@ -1418,19 +1490,30 @@ class Poppler {
if (outputFile) {
child.on("close", async (code) => {
if (code === 0) {
resolve("Code: 0, no error");
resolve(errorMessages[code]);
} else {
reject(new Error(`Error Code: ${code.toString()}`));
reject(new Error(errorMessages[code]));
}
});
} else {
child.stdout.on("data", async (data) => {
resolve(data);
});
}

let stdOut = "";
let stdErr = "";

child.stdout.on("data", async (data) => {
stdOut += data;
});

child.stderr.on("data", async (data) => {
reject(new Error(data));
stdErr += data;
});

child.on("close", async () => {
if (stdOut !== "") {
resolve(stdOut.trim());
} else {
reject(new Error(stdErr.trim()));
}
});
});
} catch (err) {
Expand Down
26 changes: 24 additions & 2 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,17 @@ describe("pdfImages Function", () => {
expect(typeof res).toBe("string");
});

test("Should accept options and save images from PDF file", async () => {
const poppler = new Poppler(testBinaryPath);
const options = {
pngFile: true,
};

const res = await poppler.pdfImages(file, "file_prefix", options);

expect(typeof res).toBe("string");
});

test("Should accept options and list all images in PDF file as Buffer", async () => {
const poppler = new Poppler(testBinaryPath);
const attachmentFile = fs.readFileSync(file);
Expand All @@ -301,7 +312,7 @@ describe("pdfImages Function", () => {

expect.assertions(1);
await poppler.pdfImages(testTxtFile, `file_prefix`).catch((err) => {
expect(err.message.substring(0, 15)).toBe("Syntax Warning:");
expect(err.message).toBe("Error opening a PDF file.");
});
});

Expand All @@ -310,7 +321,7 @@ describe("pdfImages Function", () => {

expect.assertions(1);
await poppler.pdfImages(undefined, `file_prefix`).catch((err) => {
expect(err.message.substring(0, 10)).toBe("I/O Error:");
expect(err.message).toBe("Error opening a PDF file.");
});
});

Expand Down Expand Up @@ -505,6 +516,17 @@ describe("pdfToCairo Function", () => {
).toBe(true);
});

test("Should convert PDF file to SVG file and send to stdout", async () => {
const poppler = new Poppler(testBinaryPath);
const options = {
svgFile: true,
};

const res = await poppler.pdfToCairo(file, undefined, options);

expect(typeof res).toBe("string");
});

test("Should convert PDF file as Buffer to SVG file", async () => {
const poppler = new Poppler(testBinaryPath);
const attachmentFile = fs.readFileSync(file);
Expand Down

0 comments on commit a5fa54c

Please sign in to comment.