This repository was archived by the owner on Aug 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
46 lines (40 loc) · 1.39 KB
/
index.js
File metadata and controls
46 lines (40 loc) · 1.39 KB
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
const path = require('path');
const { which, exec } = require('shelljs');
const semver = require('semver');
const filePath = path.join(__dirname, 'dkimverify.py');
// ensure python installed
if (!which('python3')) throw new Error(`Python v3.5+ is required`);
const silent = process.env.NODE_ENV !== 'test';
const options = { silent, async: true };
// ensure python v3.5+
let version = exec('python3 --version', { silent });
version = semver.coerce(
(version.stdout || version.stderr).split(' ')[1].trim()
);
if (!semver.satisfies(version, '>= 3.5'))
throw new Error(
`Python v3.5+ is required, you currently have v${version} installed`
);
module.exports = function(rawEmail, index = 0) {
return new Promise((resolve, reject) => {
const child = exec(`python3 ${filePath} --index ${index}`, options);
const stdout = [];
const stderr = [];
child.stderr.on('data', data => {
stderr.push(data);
});
child.stdout.on('data', data => {
stdout.push(data);
});
child.stdin.write(rawEmail);
child.stdin.end();
child.on('close', code => {
// exits with code 1 if failed
if (code === 1) return resolve(false);
if (stderr.length > 0) return reject(new Error(stderr.join('').trim()));
const output = stdout.join('').trim();
if (output && output === 'signature ok') return resolve(true);
reject(new Error(output));
});
});
};