Commit f8fc782a2e1ac06bac97cdcb86d0cb912678f9d9
1 parent
f383485f11
Exists in
master
Fix eslint on http-listener
Showing 2 changed files with 55 additions and 44 deletions Side-by-side Diff
.eslintrc.js
1 | 1 | module.exports = { |
2 | - env: { | |
3 | - commonjs: true, | |
4 | - es2021: true, | |
5 | - node: true, | |
6 | - }, | |
7 | - extends: [ | |
8 | - 'airbnb-base', | |
9 | - ], | |
10 | - parserOptions: { | |
11 | - ecmaVersion: 12, | |
12 | - }, | |
13 | - rules: { | |
14 | - }, | |
2 | + env: { | |
3 | + commonjs: true, | |
4 | + es2021: true, | |
5 | + node: true, | |
6 | + }, | |
7 | + extends: [ | |
8 | + 'airbnb-base', | |
9 | + ], | |
10 | + parserOptions: { | |
11 | + ecmaVersion: 12, | |
12 | + }, | |
13 | + rules: { | |
14 | + indent: ['error', 4], | |
15 | + }, | |
15 | 16 | }; |
lib/http-listener.js
1 | 1 | const express = require('express'); |
2 | -const bodyParser = require('body-parser'); | |
3 | 2 | const ipfilter = require('express-ipfilter').IpFilter; |
4 | 3 | const removeAccents = require('remove-accents'); |
5 | 4 | |
... | ... | @@ -10,7 +9,9 @@ const commandHandler = require('./command-handler'); |
10 | 9 | const messagesArchive = require('./messages-archive'); |
11 | 10 | |
12 | 11 | const app = express(); |
13 | -const port = config && config.listener && config.listener.http && config.listener.http.port ? config.listener.http.port : 32979; | |
12 | +const port = (config && config.listener && config.listener.http | |
13 | + && config.listener.http.port && config.listener.http.port | |
14 | +) || 32979; | |
14 | 15 | |
15 | 16 | /** |
16 | 17 | * Handler utama. |
... | ... | @@ -19,7 +20,7 @@ const port = config && config.listener && config.listener.http && config.listene |
19 | 20 | * @param {object} req.query - Express query string object |
20 | 21 | * @param {string} [req.query.partner] - Partner (pengirim atau penerima) |
21 | 22 | * @param {string} [req.query.from] - Pengirim, OBSOLETED: gunakan parameter partner |
22 | - * @param {string} [req.query.from_raw] - Pengirim tanpa suffix (raw), OBSOLETED: gunakan parameter partner_raw | |
23 | + * @param {string} [req.query.from_raw] - Pengirim tanpa suffix (raw), OBSOLETED by partner_raw | |
23 | 24 | * @param {string} [req.query.to] - Tujuan, jika is_outgoing, OBSOLETED: gunakan parameter partner |
24 | 25 | * @param {string} req.query.msg - Isi pesan |
25 | 26 | * @param {string} req.query.origin - Nama origin |
... | ... | @@ -32,12 +33,10 @@ function mainHandler(req, res) { |
32 | 33 | if (!req.body) req.body = {}; |
33 | 34 | |
34 | 35 | if ( |
35 | - ( !req.body.partner && !req.query.partner ) | |
36 | - && | |
37 | - ( | |
38 | - ( (!req.query.is_outgoing && !req.body.is_outgoing) && (!req.query.from && !req.body.from) ) | |
39 | - || | |
40 | - ( (req.query.is_outgoing || req.body.is_outgoing) && (!req.query.to && !req.body.to) ) | |
36 | + (!req.body.partner && !req.query.partner) | |
37 | + && ( | |
38 | + (!req.query.is_outgoing && !req.body.is_outgoing && !req.query.from && !req.body.from) | |
39 | + || ((req.query.is_outgoing || req.body.is_outgoing) && !req.query.to && !req.body.to) | |
41 | 40 | ) |
42 | 41 | ) { |
43 | 42 | logger.warn('Undefined parameter partner or from or to. #D254B7B454DB', { |
... | ... | @@ -55,38 +54,45 @@ function mainHandler(req, res) { |
55 | 54 | .replace(/[\u{0080}-\u{FFFF}]/gu, '').trim(); |
56 | 55 | |
57 | 56 | if (!msg) { |
58 | - logger.warn('Undefined parameter msg. #92996A497D12') | |
57 | + logger.warn('Undefined parameter msg. #92996A497D12'); | |
59 | 58 | res.end('ERROR. Undefined parameter: msg'); |
60 | 59 | return; |
61 | 60 | } |
62 | 61 | |
63 | 62 | logger.verbose( |
64 | - `Saving ${req.body.is_outgoing || req.query.is_outgoing ? 'outgoing' : 'incoming' } message history`, | |
63 | + `Saving ${req.body.is_outgoing || req.query.is_outgoing ? 'outgoing' : 'incoming'} message history`, | |
65 | 64 | { |
66 | 65 | transport: req.body.origin_transport || req.query.origin_transport, |
67 | - partner: req.body.partner || req.query.partner || req.body.from || req.query.from || req.body.to || req.query.to, | |
66 | + partner: req.body.partner || req.query.partner || req.body.from || req.query.from | |
67 | + || req.body.to || req.query.to, | |
68 | 68 | msg, |
69 | - } | |
69 | + }, | |
70 | 70 | ); |
71 | 71 | |
72 | 72 | messagesArchive.insert( |
73 | 73 | { |
74 | - origin_label: req.body.origin_label || req.query.origin_label || req.body.origin || req.query.origin, | |
74 | + origin_label: req.body.origin_label || req.query.origin_label | |
75 | + || req.body.origin || req.query.origin, | |
75 | 76 | origin_transport: req.body.origin_transport || req.query.origin_transport, |
76 | - partner: req.body.partner_raw || req.query.partner_raw || req.body.from_raw || req.query.from_raw || req.body.from || req.query.from || req.body.to || req.query.to || req.body.partner || req.query.partner, | |
77 | + partner: req.body.partner_raw || req.query.partner_raw | |
78 | + || req.body.from_raw || req.query.from_raw | |
79 | + || req.body.from || req.query.from | |
80 | + || req.body.to || req.query.to || req.body.partner || req.query.partner, | |
77 | 81 | msg, |
78 | - }, | |
79 | - ( req.body.is_outgoing || req.query.is_outgoing ) ? messagesArchive.DIRECTION_OUTGOING : messagesArchive.DIRECTION_INCOMING | |
82 | + }, | |
83 | + (req.body.is_outgoing || req.query.is_outgoing) ? messagesArchive.DIRECTION_OUTGOING | |
84 | + : messagesArchive.DIRECTION_INCOMING, | |
80 | 85 | ); |
81 | 86 | |
82 | 87 | if ( |
83 | - req.body.do_not_forward_to_core || req.query.do_not_forward_to_core | |
88 | + req.body.do_not_forward_to_core || req.query.do_not_forward_to_core | |
84 | 89 | || req.body.is_outgoing || req.query.is_outgoing |
85 | 90 | ) { |
86 | - logger.verbose( 'Ignoring message', { | |
87 | - from: req.body.from || req.query.from, | |
88 | - msg, | |
89 | - do_not_forward_to_core: req.body.do_not_forward_to_core || req.query.do_not_forward_to_core | |
91 | + logger.verbose('Ignoring message', { | |
92 | + from: req.body.from || req.query.from, | |
93 | + msg, | |
94 | + do_not_forward_to_core: req.body.do_not_forward_to_core | |
95 | + || req.query.do_not_forward_to_core, | |
90 | 96 | }); |
91 | 97 | |
92 | 98 | res.end('OK'); |
... | ... | @@ -105,24 +111,28 @@ function mainHandler(req, res) { |
105 | 111 | report_port: req.body.report_port || req.query.report_port, |
106 | 112 | from: req.body.partner || req.query.partner || req.body.from || req.query.from, |
107 | 113 | msg, |
108 | - } | |
114 | + }; | |
109 | 115 | |
110 | - commandHandler(msg, params, function(err, coreResponseObject, responseParams) { | |
116 | + commandHandler(msg, params, (err, coreResponseObject, responseParams) => { | |
111 | 117 | if (err) { |
112 | - res.end('ERROR. ' + err); | |
118 | + res.end(`ERROR. ${err.message || err}`); | |
113 | 119 | } else if (coreResponseObject) { |
114 | 120 | res.json(coreResponseObject); |
115 | 121 | } else { |
116 | 122 | res.end(responseParams.body); |
117 | 123 | } |
118 | - }) | |
124 | + }); | |
119 | 125 | } |
120 | 126 | |
121 | -config.ip_whitelist && config.ip_whitelist.length && app.use(ipfilter(config.ip_whitelist, {mode: 'allow', log: false})); | |
127 | +if (config.ip_whitelist && config.ip_whitelist.length) { | |
128 | + app.use(ipfilter(config.ip_whitelist, { mode: 'allow', log: false })); | |
129 | +} | |
122 | 130 | |
123 | 131 | app.get('/', mainHandler); |
124 | -app.post('/', bodyParser.urlencoded({extended: true}), mainHandler); | |
132 | +app.post('/', express.urlencoded({ extended: true }), mainHandler); | |
125 | 133 | |
126 | -app.listen(port, function() { | |
127 | - logger.info('HTTP-LISTENER: started', {port: port, app_env: app.get('env')}); | |
128 | -}); | |
129 | 134 | \ No newline at end of file |
135 | +app.listen(port, () => { | |
136 | + logger.info('HTTP-LISTENER: started', { | |
137 | + port, app_env: app.get('env'), | |
138 | + }); | |
139 | +}); |