-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (51 loc) · 1.71 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
const http = require("http");
const fs = require("fs");
const camelize = (str) => str.replace(/_(\w)/g, (m) => m.slice(1).toUpperCase());
const isUpperCase = /^[A-Z]/;
const promisify = require("util").promisify;
const parseResult = (collect) => {
collect = JSON.parse(collect);
let result = {};
for (let key in collect.default) {
if (!isUpperCase.test(key))
result[camelize(key)] = collect.default[key];
}
return result;
};
const defaultOptions = {
host: 'localhost',
port: 11333,
};
const request = (options, source, done) => {
const req = http.request(options, (res) => {
let collect = "";
res.setEncoding('utf8');
res.on('data', (chunk) => collect += chunk);
res.on('end', () => {
done(null, parseResult(collect), collect);
});
});
source.on('error', (err) => done(err));
req.on('error', (err) => done(err));
source.pipe(req);
};
const post = (path, options, source, done) => {
options = Object.assign({
method: "POST",
path: path
}, options);
if (typeof source == "string")
source = fs.createReadStream(source);
if (done) return request(options, source, done);
return promisify(request)(options, source);
};
function RspamdClient (options) {
options = Object.assign({}, defaultOptions, options);
return {
check: (source, done) => post("/check", options, source, done),
symbols: (source, done) => post("/symbols", options, source, done),
learnSpam: (source, done) => post("/learnspam", options, source, done),
learnHam: (source, done) => post("/learnham", options, source, done)
};
}
module.exports = RspamdClient;