node-poppler 8.0.2 → 8.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +39 -39
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-poppler",
3
- "version": "8.0.2",
3
+ "version": "8.0.3",
4
4
  "description": "Asynchronous Node.js wrapper for the Poppler PDF rendering library",
5
5
  "keywords": [
6
6
  "async",
package/src/index.js CHANGED
@@ -2,13 +2,14 @@
2
2
 
3
3
  const { execFile, spawn, spawnSync } = require("node:child_process");
4
4
  const { normalize, resolve: pathResolve } = require("node:path");
5
+ const { platform } = require("node:process");
5
6
  const { promisify } = require("node:util");
6
7
  const camelCase = require("camelcase");
7
8
  const { lt } = require("semver");
8
9
 
9
10
  const execFileAsync = promisify(execFile);
10
11
 
11
- const errorMessages = {
12
+ const ERROR_MSGS = {
12
13
  0: "No Error",
13
14
  1: "Error opening a PDF file",
14
15
  2: "Error opening an output file",
@@ -19,8 +20,9 @@ const errorMessages = {
19
20
  };
20
21
 
21
22
  // Cache immutable regex as they are expensive to create and garbage collect
22
- const popplerVersionRegex = /(\d{1,2}\.\d{1,2}\.\d{1,2})/u;
23
- const pdfInfoFileSizesRegex = /(File\s+size:\s+)0(\s+)bytes/u;
23
+ const POPPLER_VERSION_REG = /(\d{1,2}\.\d{1,2}\.\d{1,2})/u;
24
+ const PDF_INFO_FILE_SIZES_REG = /(File\s+size:\s+)0(\s+)bytes/u;
25
+ const PDF_INFO_PATH_REG = /(.+)pdfinfo/u;
24
26
 
25
27
  /**
26
28
  * @typedef {object} OptionDetails
@@ -119,12 +121,10 @@ class Poppler {
119
121
  /** @type {string|undefined} */
120
122
  this.#popplerPath = binPath;
121
123
  } else {
122
- const { platform } = process;
123
-
124
124
  const which = spawnSync(platform === "win32" ? "where" : "which", [
125
125
  "pdfinfo",
126
126
  ]).stdout.toString();
127
- const popplerPath = /(.+)pdfinfo/u.exec(which)?.[1];
127
+ const popplerPath = PDF_INFO_PATH_REG.exec(which)?.[1];
128
128
 
129
129
  if (popplerPath) {
130
130
  this.#popplerPath = popplerPath;
@@ -144,7 +144,7 @@ class Poppler {
144
144
  /* istanbul ignore next: unable to test due to https://github.com/jestjs/jest/pull/14297 */
145
145
  if (!this.#popplerPath) {
146
146
  throw new Error(
147
- `Unable to find ${process.platform} Poppler binaries, please pass the installation directory as a parameter to the Poppler instance.`
147
+ `Unable to find ${platform} Poppler binaries, please pass the installation directory as a parameter to the Poppler instance.`
148
148
  );
149
149
  }
150
150
  this.#popplerPath = normalize(this.#popplerPath);
@@ -271,7 +271,7 @@ class Poppler {
271
271
  );
272
272
 
273
273
  // @ts-ignore: parseOptions checks if falsy
274
- const versionInfo = popplerVersionRegex.exec(stderr)[1];
274
+ const versionInfo = POPPLER_VERSION_REG.exec(stderr)[1];
275
275
 
276
276
  const args = parseOptions(acceptedOptions, options, versionInfo);
277
277
 
@@ -304,14 +304,14 @@ class Poppler {
304
304
  if (stdOut !== "") {
305
305
  resolve(stdOut.trim());
306
306
  } else if (code === 0) {
307
- resolve(errorMessages[code]);
307
+ resolve(ERROR_MSGS[code]);
308
308
  } else if (stdErr !== "") {
309
309
  reject(new Error(stdErr.trim()));
310
310
  } else {
311
311
  reject(
312
312
  new Error(
313
- // @ts-ignore: Second operand used if code is not in errorMessages
314
- errorMessages[code] ||
313
+ // @ts-ignore: Second operand used if code is not in ERROR_MSGS
314
+ ERROR_MSGS[code] ||
315
315
  `pdffonts ${args.join(
316
316
  " "
317
317
  )} exited with code ${code}`
@@ -370,7 +370,7 @@ class Poppler {
370
370
  );
371
371
 
372
372
  // @ts-ignore: parseOptions checks if falsy
373
- const versionInfo = popplerVersionRegex.exec(stderr)[1];
373
+ const versionInfo = POPPLER_VERSION_REG.exec(stderr)[1];
374
374
 
375
375
  const args = parseOptions(acceptedOptions, options, versionInfo);
376
376
 
@@ -407,14 +407,14 @@ class Poppler {
407
407
  if (stdOut !== "") {
408
408
  resolve(stdOut.trim());
409
409
  } else if (code === 0) {
410
- resolve(errorMessages[code]);
410
+ resolve(ERROR_MSGS[code]);
411
411
  } else if (stdErr !== "") {
412
412
  reject(new Error(stdErr.trim()));
413
413
  } else {
414
414
  reject(
415
415
  new Error(
416
- // @ts-ignore: Second operand used if code is not in errorMessages
417
- errorMessages[code] ||
416
+ // @ts-ignore: Second operand used if code is not in ERROR_MSGS
417
+ ERROR_MSGS[code] ||
418
418
  `pdfimages ${args.join(
419
419
  " "
420
420
  )} exited with code ${code}`
@@ -487,7 +487,7 @@ class Poppler {
487
487
  );
488
488
 
489
489
  // @ts-ignore: parseOptions checks if falsy
490
- const versionInfo = popplerVersionRegex.exec(stderr)[1];
490
+ const versionInfo = POPPLER_VERSION_REG.exec(stderr)[1];
491
491
 
492
492
  const args = parseOptions(acceptedOptions, options, versionInfo);
493
493
 
@@ -529,7 +529,7 @@ class Poppler {
529
529
  if (stdOut !== "") {
530
530
  if (fileSize) {
531
531
  stdOut = stdOut.replace(
532
- pdfInfoFileSizesRegex,
532
+ PDF_INFO_FILE_SIZES_REG,
533
533
  `$1${fileSize}$2bytes`
534
534
  );
535
535
  }
@@ -551,14 +551,14 @@ class Poppler {
551
551
  resolve(stdOut.trim());
552
552
  }
553
553
  } else if (code === 0) {
554
- resolve(errorMessages[code]);
554
+ resolve(ERROR_MSGS[code]);
555
555
  } else if (stdErr !== "") {
556
556
  reject(new Error(stdErr.trim()));
557
557
  } else {
558
558
  reject(
559
559
  new Error(
560
- // @ts-ignore: Second operand used if code is not in errorMessages
561
- errorMessages[code] ||
560
+ // @ts-ignore: Second operand used if code is not in ERROR_MSGS
561
+ ERROR_MSGS[code] ||
562
562
  `pdfinfo ${args.join(
563
563
  " "
564
564
  )} exited with code ${code}`
@@ -600,7 +600,7 @@ class Poppler {
600
600
  );
601
601
 
602
602
  // @ts-ignore: parseOptions checks if falsy
603
- const versionInfo = popplerVersionRegex.exec(stderr)[1];
603
+ const versionInfo = POPPLER_VERSION_REG.exec(stderr)[1];
604
604
 
605
605
  const args = parseOptions(acceptedOptions, options, versionInfo);
606
606
  args.push(file, outputPattern);
@@ -782,7 +782,7 @@ class Poppler {
782
782
  );
783
783
 
784
784
  // @ts-ignore: parseOptions checks if falsy
785
- const versionInfo = popplerVersionRegex.exec(stderr)[1];
785
+ const versionInfo = POPPLER_VERSION_REG.exec(stderr)[1];
786
786
 
787
787
  const args = parseOptions(acceptedOptions, options, versionInfo);
788
788
 
@@ -825,14 +825,14 @@ class Poppler {
825
825
  if (stdOut !== "") {
826
826
  resolve(stdOut.trim());
827
827
  } else if (code === 0) {
828
- resolve(errorMessages[code]);
828
+ resolve(ERROR_MSGS[code]);
829
829
  } else if (stdErr !== "") {
830
830
  reject(new Error(stdErr.trim()));
831
831
  } else {
832
832
  reject(
833
833
  new Error(
834
- // @ts-ignore: Second operand used if code is not in errorMessages
835
- errorMessages[code] ||
834
+ // @ts-ignore: Second operand used if code is not in ERROR_MSGS
835
+ ERROR_MSGS[code] ||
836
836
  `pdftocairo ${args.join(
837
837
  " "
838
838
  )} exited with code ${code}`
@@ -924,7 +924,7 @@ class Poppler {
924
924
  );
925
925
 
926
926
  // @ts-ignore: parseOptions checks if falsy
927
- const versionInfo = popplerVersionRegex.exec(stderr)[1];
927
+ const versionInfo = POPPLER_VERSION_REG.exec(stderr)[1];
928
928
 
929
929
  const args = parseOptions(acceptedOptions, options, versionInfo);
930
930
 
@@ -1119,7 +1119,7 @@ class Poppler {
1119
1119
  );
1120
1120
 
1121
1121
  // @ts-ignore: parseOptions checks if falsy
1122
- const versionInfo = popplerVersionRegex.exec(stderr)[1];
1122
+ const versionInfo = POPPLER_VERSION_REG.exec(stderr)[1];
1123
1123
 
1124
1124
  const args = parseOptions(acceptedOptions, options, versionInfo);
1125
1125
 
@@ -1147,12 +1147,12 @@ class Poppler {
1147
1147
  if (stdErr !== "") {
1148
1148
  reject(new Error(stdErr.trim()));
1149
1149
  } else if (code === 0) {
1150
- resolve(errorMessages[code]);
1150
+ resolve(ERROR_MSGS[code]);
1151
1151
  } else {
1152
1152
  reject(
1153
1153
  new Error(
1154
- // @ts-ignore: Second operand used if code is not in errorMessages
1155
- errorMessages[code] ||
1154
+ // @ts-ignore: Second operand used if code is not in ERROR_MSGS
1155
+ ERROR_MSGS[code] ||
1156
1156
  `pdftoppm ${args.join(
1157
1157
  " "
1158
1158
  )} exited with code ${code}`
@@ -1349,7 +1349,7 @@ class Poppler {
1349
1349
  );
1350
1350
 
1351
1351
  // @ts-ignore: parseOptions checks if falsy
1352
- const versionInfo = popplerVersionRegex.exec(stderr)[1];
1352
+ const versionInfo = POPPLER_VERSION_REG.exec(stderr)[1];
1353
1353
 
1354
1354
  const args = parseOptions(acceptedOptions, options, versionInfo);
1355
1355
 
@@ -1382,14 +1382,14 @@ class Poppler {
1382
1382
  if (stdOut !== "") {
1383
1383
  resolve(stdOut.trim());
1384
1384
  } else if (code === 0) {
1385
- resolve(errorMessages[code]);
1385
+ resolve(ERROR_MSGS[code]);
1386
1386
  } else if (stdErr !== "") {
1387
1387
  reject(new Error(stdErr.trim()));
1388
1388
  } else {
1389
1389
  reject(
1390
1390
  new Error(
1391
- // @ts-ignore: Second operand used if code is not in errorMessages
1392
- errorMessages[code] ||
1391
+ // @ts-ignore: Second operand used if code is not in ERROR_MSGS
1392
+ ERROR_MSGS[code] ||
1393
1393
  `pdftops ${args.join(
1394
1394
  " "
1395
1395
  )} exited with code ${code}`
@@ -1494,7 +1494,7 @@ class Poppler {
1494
1494
  );
1495
1495
 
1496
1496
  // @ts-ignore: parseOptions checks if falsy
1497
- const versionInfo = popplerVersionRegex.exec(stderr)[1];
1497
+ const versionInfo = POPPLER_VERSION_REG.exec(stderr)[1];
1498
1498
 
1499
1499
  const args = parseOptions(acceptedOptions, options, versionInfo);
1500
1500
 
@@ -1527,14 +1527,14 @@ class Poppler {
1527
1527
  if (stdOut !== "") {
1528
1528
  resolve(options.maintainLayout ? stdOut : stdOut.trim());
1529
1529
  } else if (code === 0) {
1530
- resolve(errorMessages[code]);
1530
+ resolve(ERROR_MSGS[code]);
1531
1531
  } else if (stdErr !== "") {
1532
1532
  reject(new Error(stdErr.trim()));
1533
1533
  } else {
1534
1534
  reject(
1535
1535
  new Error(
1536
- // @ts-ignore: Second operand used if code is not in errorMessages
1537
- errorMessages[code] ||
1536
+ // @ts-ignore: Second operand used if code is not in ERROR_MSGS
1537
+ ERROR_MSGS[code] ||
1538
1538
  `pdftotext ${args.join(
1539
1539
  " "
1540
1540
  )} exited with code ${code}`
@@ -1568,7 +1568,7 @@ class Poppler {
1568
1568
  );
1569
1569
 
1570
1570
  // @ts-ignore: parseOptions checks if falsy
1571
- const versionInfo = popplerVersionRegex.exec(stderr)[1];
1571
+ const versionInfo = POPPLER_VERSION_REG.exec(stderr)[1];
1572
1572
 
1573
1573
  const args = parseOptions(acceptedOptions, options, versionInfo);
1574
1574
  args.push(...files, outputFile);