-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
87 lines (75 loc) · 2.19 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
'use strict';
const fs = require('fs');
const tmp = require('tmp');
const path = require('path');
const spawn = require('child_process').spawn;
const params = require('./lib/params');
const parseResult = require('./lib/parse-result');
const corePath = path.resolve(__dirname, 'jar/', 'core-3.1.0.jar');
const javasePath = path.resolve(__dirname, 'jar/', 'javase-3.1.0.jar');
const FORMAT = require('./lib/supported-formats.js');
function access(filepath, mode) {
if (!mode) mode = fs.F_OK;
return new Promise((resolve, reject) => {
fs.access(filepath, mode, err => {
if (err) reject(err);
else resolve(filepath);
})
})
}
function writeBufferToTemporaryFile(buffer) {
return new Promise((resolve, reject) => {
tmp.file((err, path, fd) => {
if (err) return reject(err);
fs.write(fd, buffer, 0, buffer.length, err => {
if (err) return reject(err);
return resolve(path);
})
})
});
}
function read(file, options) {
let ensureFile;
if (Buffer.isBuffer(file)) {
ensureFile = writeBufferToTemporaryFile(file);
} else {
ensureFile = access(file, fs.R_OK);
}
// Check if the file is readable before handing the path off to zxing,
// because it will take ages otherwise.
return ensureFile.then((filepath) => new Promise((resolve, reject) => {
const args = [
'-cp',
javasePath + path.delimiter + corePath,
'com.google.zxing.client.j2se.CommandLineRunner',
filepath,
].concat(params(options));
const java = spawn('java', args);
const stdout = [];
const stderr = [];
let isOpen = true;
let closeTimeout;
java.stdout.on('data', data => {
if (!isOpen) return;
clearTimeout(closeTimeout);
stdout.push(data);
closeTimeout = setTimeout(close, 20);
})
java.stderr.on('data', data => {
if (!isOpen) return;
stderr.push(data);
})
function close() {
isOpen = false;
const output = Buffer.concat(stdout);
const parsed = parseResult(output);
if (!options || !options.multi) resolve(parsed[0] || null);
else resolve(parsed);
}
java.on('close', code => {
if (!isOpen) return;
if (code !== 0 || stderr.length) reject(new Error(`Exited with status code ${code}`, Buffer.concat(stderr)))
})
}))
}
module.exports = { read, FORMAT };