node-poppler 7.0.0 → 7.0.2
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.
- package/package.json +1 -1
- package/src/index.js +64 -22
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -16,9 +16,12 @@ const errorMessages = {
|
|
|
16
16
|
4: "Error related to ICC profile",
|
|
17
17
|
99: "Other error",
|
|
18
18
|
3221226505: "Internal process error",
|
|
19
|
-
unk: "Unknown error",
|
|
20
19
|
};
|
|
21
20
|
|
|
21
|
+
// 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;
|
|
24
|
+
|
|
22
25
|
/**
|
|
23
26
|
* @author Frazer Smith
|
|
24
27
|
* @description Checks each option provided is valid, of the correct type, and can be used by specified
|
|
@@ -37,7 +40,7 @@ function parseOptions(acceptedOptions, options, version) {
|
|
|
37
40
|
const invalidArgs = [];
|
|
38
41
|
Object.keys(options).forEach((key) => {
|
|
39
42
|
if (Object.hasOwn(acceptedOptions, key)) {
|
|
40
|
-
// eslint-disable-next-line valid-typeof
|
|
43
|
+
// eslint-disable-next-line valid-typeof -- `type` is a string
|
|
41
44
|
if (typeof options[key] === acceptedOptions[key].type) {
|
|
42
45
|
// Skip boolean options if false
|
|
43
46
|
|
|
@@ -223,7 +226,7 @@ class Poppler {
|
|
|
223
226
|
);
|
|
224
227
|
|
|
225
228
|
// @ts-ignore: parseOptions checks if falsy
|
|
226
|
-
const versionInfo =
|
|
229
|
+
const versionInfo = popplerVersionRegex.exec(stderr)[1];
|
|
227
230
|
|
|
228
231
|
const args = parseOptions(acceptedOptions, options, versionInfo);
|
|
229
232
|
|
|
@@ -255,11 +258,23 @@ class Poppler {
|
|
|
255
258
|
stdErr += data;
|
|
256
259
|
});
|
|
257
260
|
|
|
258
|
-
child.on("close", () => {
|
|
261
|
+
child.on("close", (code) => {
|
|
262
|
+
/* istanbul ignore else */
|
|
259
263
|
if (stdOut !== "") {
|
|
260
264
|
resolve(stdOut.trim());
|
|
265
|
+
} else if (code === 0) {
|
|
266
|
+
resolve(errorMessages[code]);
|
|
267
|
+
} else if (stdErr !== "") {
|
|
268
|
+
reject(new Error(stdErr.trim()));
|
|
261
269
|
} else {
|
|
262
|
-
reject(
|
|
270
|
+
reject(
|
|
271
|
+
new Error(
|
|
272
|
+
errorMessages[code] ||
|
|
273
|
+
`pdffonts ${args.join(
|
|
274
|
+
" "
|
|
275
|
+
)} exited with code ${code}`
|
|
276
|
+
)
|
|
277
|
+
);
|
|
263
278
|
}
|
|
264
279
|
});
|
|
265
280
|
});
|
|
@@ -316,7 +331,7 @@ class Poppler {
|
|
|
316
331
|
);
|
|
317
332
|
|
|
318
333
|
// @ts-ignore: parseOptions checks if falsy
|
|
319
|
-
const versionInfo =
|
|
334
|
+
const versionInfo = popplerVersionRegex.exec(stderr)[1];
|
|
320
335
|
|
|
321
336
|
const args = parseOptions(acceptedOptions, options, versionInfo);
|
|
322
337
|
|
|
@@ -363,7 +378,10 @@ class Poppler {
|
|
|
363
378
|
} else {
|
|
364
379
|
reject(
|
|
365
380
|
new Error(
|
|
366
|
-
|
|
381
|
+
errorMessages[code] ||
|
|
382
|
+
`pdfimages ${args.join(
|
|
383
|
+
" "
|
|
384
|
+
)} exited with code ${code}`
|
|
367
385
|
)
|
|
368
386
|
);
|
|
369
387
|
}
|
|
@@ -437,7 +455,7 @@ class Poppler {
|
|
|
437
455
|
);
|
|
438
456
|
|
|
439
457
|
// @ts-ignore: parseOptions checks if falsy
|
|
440
|
-
const versionInfo =
|
|
458
|
+
const versionInfo = popplerVersionRegex.exec(stderr)[1];
|
|
441
459
|
|
|
442
460
|
const args = parseOptions(acceptedOptions, options, versionInfo);
|
|
443
461
|
|
|
@@ -477,11 +495,12 @@ class Poppler {
|
|
|
477
495
|
stdErr += data;
|
|
478
496
|
});
|
|
479
497
|
|
|
480
|
-
child.on("close", () => {
|
|
498
|
+
child.on("close", (code) => {
|
|
499
|
+
/* istanbul ignore else */
|
|
481
500
|
if (stdOut !== "") {
|
|
482
501
|
if (fileSize) {
|
|
483
502
|
stdOut = stdOut.replace(
|
|
484
|
-
|
|
503
|
+
pdfInfoFileSizesRegex,
|
|
485
504
|
`$1${fileSize}$2bytes`
|
|
486
505
|
);
|
|
487
506
|
}
|
|
@@ -503,8 +522,19 @@ class Poppler {
|
|
|
503
522
|
} else {
|
|
504
523
|
resolve(stdOut.trim());
|
|
505
524
|
}
|
|
525
|
+
} else if (code === 0) {
|
|
526
|
+
resolve(errorMessages[code]);
|
|
527
|
+
} else if (stdErr !== "") {
|
|
528
|
+
reject(new Error(stdErr.trim()));
|
|
506
529
|
} else {
|
|
507
|
-
reject(
|
|
530
|
+
reject(
|
|
531
|
+
new Error(
|
|
532
|
+
errorMessages[code] ||
|
|
533
|
+
`pdfinfo ${args.join(
|
|
534
|
+
" "
|
|
535
|
+
)} exited with code ${code}`
|
|
536
|
+
)
|
|
537
|
+
);
|
|
508
538
|
}
|
|
509
539
|
});
|
|
510
540
|
});
|
|
@@ -544,7 +574,7 @@ class Poppler {
|
|
|
544
574
|
);
|
|
545
575
|
|
|
546
576
|
// @ts-ignore: parseOptions checks if falsy
|
|
547
|
-
const versionInfo =
|
|
577
|
+
const versionInfo = popplerVersionRegex.exec(stderr)[1];
|
|
548
578
|
|
|
549
579
|
const args = parseOptions(acceptedOptions, options, versionInfo);
|
|
550
580
|
args.push(file);
|
|
@@ -722,7 +752,7 @@ class Poppler {
|
|
|
722
752
|
);
|
|
723
753
|
|
|
724
754
|
// @ts-ignore: parseOptions checks if falsy
|
|
725
|
-
const versionInfo =
|
|
755
|
+
const versionInfo = popplerVersionRegex.exec(stderr)[1];
|
|
726
756
|
|
|
727
757
|
const args = parseOptions(acceptedOptions, options, versionInfo);
|
|
728
758
|
|
|
@@ -778,7 +808,10 @@ class Poppler {
|
|
|
778
808
|
} else {
|
|
779
809
|
reject(
|
|
780
810
|
new Error(
|
|
781
|
-
|
|
811
|
+
errorMessages[code] ||
|
|
812
|
+
`pdftocairo ${args.join(
|
|
813
|
+
" "
|
|
814
|
+
)} exited with code ${code}`
|
|
782
815
|
)
|
|
783
816
|
);
|
|
784
817
|
}
|
|
@@ -867,7 +900,7 @@ class Poppler {
|
|
|
867
900
|
);
|
|
868
901
|
|
|
869
902
|
// @ts-ignore: parseOptions checks if falsy
|
|
870
|
-
const versionInfo =
|
|
903
|
+
const versionInfo = popplerVersionRegex.exec(stderr)[1];
|
|
871
904
|
|
|
872
905
|
const args = parseOptions(acceptedOptions, options, versionInfo);
|
|
873
906
|
|
|
@@ -1072,7 +1105,7 @@ class Poppler {
|
|
|
1072
1105
|
);
|
|
1073
1106
|
|
|
1074
1107
|
// @ts-ignore: parseOptions checks if falsy
|
|
1075
|
-
const versionInfo =
|
|
1108
|
+
const versionInfo = popplerVersionRegex.exec(stderr)[1];
|
|
1076
1109
|
|
|
1077
1110
|
const args = parseOptions(acceptedOptions, options, versionInfo);
|
|
1078
1111
|
|
|
@@ -1110,7 +1143,10 @@ class Poppler {
|
|
|
1110
1143
|
} else {
|
|
1111
1144
|
reject(
|
|
1112
1145
|
new Error(
|
|
1113
|
-
|
|
1146
|
+
errorMessages[code] ||
|
|
1147
|
+
`pdftoppm ${args.join(
|
|
1148
|
+
" "
|
|
1149
|
+
)} exited with code ${code}`
|
|
1114
1150
|
)
|
|
1115
1151
|
);
|
|
1116
1152
|
}
|
|
@@ -1307,7 +1343,7 @@ class Poppler {
|
|
|
1307
1343
|
);
|
|
1308
1344
|
|
|
1309
1345
|
// @ts-ignore: parseOptions checks if falsy
|
|
1310
|
-
const versionInfo =
|
|
1346
|
+
const versionInfo = popplerVersionRegex.exec(stderr)[1];
|
|
1311
1347
|
|
|
1312
1348
|
const args = parseOptions(acceptedOptions, options, versionInfo);
|
|
1313
1349
|
|
|
@@ -1356,7 +1392,10 @@ class Poppler {
|
|
|
1356
1392
|
} else {
|
|
1357
1393
|
reject(
|
|
1358
1394
|
new Error(
|
|
1359
|
-
|
|
1395
|
+
errorMessages[code] ||
|
|
1396
|
+
`pdftops ${args.join(
|
|
1397
|
+
" "
|
|
1398
|
+
)} exited with code ${code}`
|
|
1360
1399
|
)
|
|
1361
1400
|
);
|
|
1362
1401
|
}
|
|
@@ -1461,7 +1500,7 @@ class Poppler {
|
|
|
1461
1500
|
);
|
|
1462
1501
|
|
|
1463
1502
|
// @ts-ignore: parseOptions checks if falsy
|
|
1464
|
-
const versionInfo =
|
|
1503
|
+
const versionInfo = popplerVersionRegex.exec(stderr)[1];
|
|
1465
1504
|
|
|
1466
1505
|
const args = parseOptions(acceptedOptions, options, versionInfo);
|
|
1467
1506
|
|
|
@@ -1510,7 +1549,10 @@ class Poppler {
|
|
|
1510
1549
|
} else {
|
|
1511
1550
|
reject(
|
|
1512
1551
|
new Error(
|
|
1513
|
-
|
|
1552
|
+
errorMessages[code] ||
|
|
1553
|
+
`pdftotext ${args.join(
|
|
1554
|
+
" "
|
|
1555
|
+
)} exited with code ${code}`
|
|
1514
1556
|
)
|
|
1515
1557
|
);
|
|
1516
1558
|
}
|
|
@@ -1544,7 +1586,7 @@ class Poppler {
|
|
|
1544
1586
|
);
|
|
1545
1587
|
|
|
1546
1588
|
// @ts-ignore: parseOptions checks if falsy
|
|
1547
|
-
const versionInfo =
|
|
1589
|
+
const versionInfo = popplerVersionRegex.exec(stderr)[1];
|
|
1548
1590
|
|
|
1549
1591
|
const args = parseOptions(acceptedOptions, options, versionInfo);
|
|
1550
1592
|
files.forEach((element) => {
|