libreoffice-convert 1.6.1 → 1.8.0

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/index.js CHANGED
@@ -61,13 +61,18 @@ const convertWithOptions = (document, format, filter, options, callback) => {
61
61
  args.push(tempDir.name);
62
62
  args.push(path.join(tempDir.name, fileName));
63
63
 
64
- return execFile(results.soffice, args, execOptions, callback);
64
+ return execFile(results.soffice, args, execOptions, (err, stdout, stderr) => {
65
+ // warnings might also be emitted to stderr
66
+ if (stderr && stderr.toLowerCase().includes('error'))
67
+ callback(new Error('Error calling soffice: ' + stderr));
68
+ else callback(err, stdout, stderr);
69
+ });
65
70
  }],
66
71
  loadDestination: ['convert', (results, callback) =>
67
72
  async.retry({
68
73
  times: asyncOptions.times || 3,
69
74
  interval: asyncOptions.interval || 200
70
- }, (callback) => fs.readFile(path.join(tempDir.name, `${fileName}.${format.split(":")[0]}`), callback), callback)
75
+ }, (callback) => fs.readFile(path.join(tempDir.name, `${fileName.slice(0, fileName.length - path.extname(fileName).length)}.${format.split(":")[0]}`), callback), callback)
71
76
  ]
72
77
  }).then( (res) => {
73
78
  return callback(null, res.loadDestination);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libreoffice-convert",
3
- "version": "1.6.1",
3
+ "version": "1.8.0",
4
4
  "description": "A simple and fast node.js module for converting office documents to different formats",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -2,7 +2,8 @@ var _jest = require('jest'),
2
2
  _fs = require('fs'),
3
3
  _path = require('path'),
4
4
  { exec } = require('child_process'),
5
- convert = require('../index').convert;
5
+ convert = require('../index').convert
6
+ convertWithOptions = require('../index').convertWithOptions;
6
7
 
7
8
  describe('convert', () => {
8
9
  function expectHello(done) {
@@ -22,6 +23,12 @@ describe('convert', () => {
22
23
  });
23
24
 
24
25
 
26
+ it('should convert a word document to text with options', (done) => {
27
+ const docx = _fs.readFileSync(_path.join(__dirname, '/resources/hello.docx'));
28
+ convertWithOptions(docx, 'txt', undefined, { fileName: 'hello.docx' }, expectHello(done));
29
+ });
30
+
31
+
25
32
  it('if an another instance of soffice exists, should convert a word document to text', (done) => {
26
33
  exec("soffice --headless")
27
34
  // this command create an instance of soffice. This instance will get a failure "Error: source file could not be loaded"
@@ -31,4 +38,14 @@ describe('convert', () => {
31
38
  convert(docx, 'txt', undefined, expectHello(done));
32
39
  }, 100);
33
40
  });
41
+
42
+ it('should fail when libreoffice encounters an error', (done) => {
43
+ // call with invalid document: a buffer starting with PNG header but data is missing
44
+ const badPng = Buffer.from([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]);
45
+ convert(badPng, 'txt', undefined, (err, result) => {
46
+ expect(err).toBeInstanceOf(Error);
47
+ expect(err.message).toMatch(/Error calling soffice:/);
48
+ done();
49
+ });
50
+ });
34
51
  });