node-telegram-util 0.0.1-security → 0.69.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.

Potentially problematic release.


This version of node-telegram-util might be problematic. Click here for more details.

@@ -0,0 +1,237 @@
1
+ 'use strict';
2
+
3
+ var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
4
+
5
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
6
+
7
+ var errors = require('./errors');
8
+ var debug = require('debug')('node-telegram-bot-api');
9
+ var deprecate = require('./utils').deprecate;
10
+ var ANOTHER_WEB_HOOK_USED = 409;
11
+
12
+ var TelegramBotPolling = (function () {
13
+ /**
14
+ * Handles polling against the Telegram servers.
15
+ * @param {TelegramBot} bot
16
+ * @see https://core.telegram.org/bots/api#getting-updates
17
+ */
18
+
19
+ function TelegramBotPolling(bot) {
20
+ _classCallCheck(this, TelegramBotPolling);
21
+
22
+ this.bot = bot;
23
+ this.options = typeof bot.options.polling === 'boolean' ? {} : bot.options.polling;
24
+ this.options.interval = typeof this.options.interval === 'number' ? this.options.interval : 300;
25
+ this.options.params = typeof this.options.params === 'object' ? this.options.params : {};
26
+ this.options.params.offset = typeof this.options.params.offset === 'number' ? this.options.params.offset : 0;
27
+ this.options.params.timeout = typeof this.options.params.timeout === 'number' ? this.options.params.timeout : 10;
28
+ if (typeof this.options.timeout === 'number') {
29
+ deprecate('`options.polling.timeout` is deprecated. Use `options.polling.params` instead.');
30
+ this.options.params.timeout = this.options.timeout;
31
+ }
32
+ this._lastUpdate = 0;
33
+ this._lastRequest = null;
34
+ this._abort = false;
35
+ this._pollingTimeout = null;
36
+ }
37
+
38
+ /**
39
+ * Start polling
40
+ * @param {Object} [options]
41
+ * @param {Object} [options.restart]
42
+ * @return {Promise}
43
+ */
44
+
45
+ _createClass(TelegramBotPolling, [{
46
+ key: 'start',
47
+ value: function start() {
48
+ var _this = this;
49
+
50
+ var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
51
+
52
+ if (this._lastRequest) {
53
+ if (!options.restart) {
54
+ return Promise.resolve();
55
+ }
56
+ return this.stop({
57
+ cancel: true,
58
+ reason: 'Polling restart'
59
+ }).then(function () {
60
+ return _this._polling();
61
+ });
62
+ }
63
+ return this._polling();
64
+ }
65
+
66
+ /**
67
+ * Stop polling
68
+ * @param {Object} [options] Options
69
+ * @param {Boolean} [options.cancel] Cancel current request
70
+ * @param {String} [options.reason] Reason for stopping polling
71
+ * @return {Promise}
72
+ */
73
+ }, {
74
+ key: 'stop',
75
+ value: function stop() {
76
+ var _this2 = this;
77
+
78
+ var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
79
+
80
+ if (!this._lastRequest) {
81
+ return Promise.resolve();
82
+ }
83
+ var lastRequest = this._lastRequest;
84
+ this._lastRequest = null;
85
+ clearTimeout(this._pollingTimeout);
86
+ if (options.cancel) {
87
+ var reason = options.reason || 'Polling stop';
88
+ lastRequest.cancel(reason);
89
+ return Promise.resolve();
90
+ }
91
+ this._abort = true;
92
+ return lastRequest['finally'](function () {
93
+ _this2._abort = false;
94
+ });
95
+ }
96
+
97
+ /**
98
+ * Return `true` if is polling. Otherwise, `false`.
99
+ */
100
+ }, {
101
+ key: 'isPolling',
102
+ value: function isPolling() {
103
+ return !!this._lastRequest;
104
+ }
105
+
106
+ /**
107
+ * Handle error thrown during polling.
108
+ * @private
109
+ * @param {Error} error
110
+ */
111
+ }, {
112
+ key: '_error',
113
+ value: function _error(error) {
114
+ if (!this.bot.listeners('polling_error').length) {
115
+ return console.error('error: [polling_error] %j', error); // eslint-disable-line no-console
116
+ }
117
+ return this.bot.emit('polling_error', error);
118
+ }
119
+
120
+ /**
121
+ * Invokes polling (with recursion!)
122
+ * @return {Promise} promise of the current request
123
+ * @private
124
+ */
125
+ }, {
126
+ key: '_polling',
127
+ value: function _polling() {
128
+ var _this3 = this;
129
+
130
+ this._lastRequest = this._getUpdates().then(function (updates) {
131
+ _this3._lastUpdate = Date.now();
132
+ debug('polling data %j', updates);
133
+ updates.forEach(function (update) {
134
+ _this3.options.params.offset = update.update_id + 1;
135
+ debug('updated offset: %s', _this3.options.params.offset);
136
+ try {
137
+ _this3.bot.processUpdate(update);
138
+ } catch (err) {
139
+ err._processing = true;
140
+ throw err;
141
+ }
142
+ });
143
+ return null;
144
+ })['catch'](function (err) {
145
+ debug('polling error: %s', err.message);
146
+ if (!err._processing) {
147
+ return _this3._error(err);
148
+ }
149
+ delete err._processing;
150
+ /*
151
+ * An error occured while processing the items,
152
+ * i.e. in `this.bot.processUpdate()` above.
153
+ * We need to mark the already-processed items
154
+ * to avoid fetching them again once the application
155
+ * is restarted, or moves to next polling interval
156
+ * (in cases where unhandled rejections do not terminate
157
+ * the process).
158
+ * See https://github.com/yagop/node-telegram-bot-api/issues/36#issuecomment-268532067
159
+ */
160
+ if (!_this3.bot.options.badRejection) {
161
+ return _this3._error(err);
162
+ }
163
+ var opts = {
164
+ offset: _this3.options.params.offset,
165
+ limit: 1,
166
+ timeout: 0
167
+ };
168
+ return _this3.bot.getUpdates(opts).then(function () {
169
+ return _this3._error(err);
170
+ })['catch'](function (requestErr) {
171
+ /*
172
+ * We have been unable to handle this error.
173
+ * We have to log this to stderr to ensure devops
174
+ * understands that they may receive already-processed items
175
+ * on app restart.
176
+ * We simply can not rescue this situation, emit "error"
177
+ * event, with the hope that the application exits.
178
+ */
179
+ /* eslint-disable no-console */
180
+ var bugUrl = 'https://github.com/yagop/node-telegram-bot-api/issues/36#issuecomment-268532067';
181
+ console.error('error: Internal handling of The Offset Infinite Loop failed');
182
+ console.error('error: Due to error \'' + requestErr + '\'');
183
+ console.error('error: You may receive already-processed updates on app restart');
184
+ console.error('error: Please see ' + bugUrl + ' for more information');
185
+ /* eslint-enable no-console */
186
+ return _this3.bot.emit('error', new errors.FatalError(err));
187
+ });
188
+ })['finally'](function () {
189
+ if (_this3._abort) {
190
+ debug('Polling is aborted!');
191
+ } else {
192
+ debug('setTimeout for %s miliseconds', _this3.options.interval);
193
+ _this3._pollingTimeout = setTimeout(function () {
194
+ return _this3._polling();
195
+ }, _this3.options.interval);
196
+ }
197
+ });
198
+ return this._lastRequest;
199
+ }
200
+
201
+ /**
202
+ * Unset current webhook. Used when we detect that a webhook has been set
203
+ * and we are trying to poll. Polling and WebHook are mutually exclusive.
204
+ * @see https://core.telegram.org/bots/api#getting-updates
205
+ * @private
206
+ */
207
+ }, {
208
+ key: '_unsetWebHook',
209
+ value: function _unsetWebHook() {
210
+ debug('unsetting webhook');
211
+ return this.bot._request('setWebHook');
212
+ }
213
+
214
+ /**
215
+ * Retrieve updates
216
+ */
217
+ }, {
218
+ key: '_getUpdates',
219
+ value: function _getUpdates() {
220
+ var _this4 = this;
221
+
222
+ debug('polling with options: %j', this.options.params);
223
+ return this.bot.getUpdates(this.options.params)['catch'](function (err) {
224
+ if (err.response && err.response.statusCode === ANOTHER_WEB_HOOK_USED) {
225
+ return _this4._unsetWebHook().then(function () {
226
+ return _this4.bot.getUpdates(_this4.options.params);
227
+ });
228
+ }
229
+ throw err;
230
+ });
231
+ }
232
+ }]);
233
+
234
+ return TelegramBotPolling;
235
+ })();
236
+
237
+ module.exports = TelegramBotPolling;
@@ -0,0 +1,187 @@
1
+ 'use strict';
2
+
3
+ var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
4
+
5
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
6
+
7
+ var errors = require('./errors');
8
+ var debug = require('debug')('node-telegram-bot-api');
9
+ var https = require('https');
10
+ var http = require('http');
11
+ var fs = require('fs');
12
+ var bl = require('bl');
13
+
14
+ var TelegramBotWebHook = (function () {
15
+ /**
16
+ * Sets up a webhook to receive updates
17
+ * @param {TelegramBot} bot
18
+ * @see https://core.telegram.org/bots/api#getting-updates
19
+ */
20
+
21
+ function TelegramBotWebHook(bot) {
22
+ _classCallCheck(this, TelegramBotWebHook);
23
+
24
+ this.bot = bot;
25
+ this.options = typeof bot.options.webHook === 'boolean' ? {} : bot.options.webHook;
26
+ this.options.host = this.options.host || '0.0.0.0';
27
+ this.options.port = this.options.port || 8443;
28
+ this.options.https = this.options.https || {};
29
+ this.options.healthEndpoint = this.options.healthEndpoint || '/healthz';
30
+ this._healthRegex = new RegExp(this.options.healthEndpoint);
31
+ this._webServer = null;
32
+ this._open = false;
33
+ this._requestListener = this._requestListener.bind(this);
34
+ this._parseBody = this._parseBody.bind(this);
35
+
36
+ if (this.options.key && this.options.cert) {
37
+ debug('HTTPS WebHook enabled (by key/cert)');
38
+ this.options.https.key = fs.readFileSync(this.options.key);
39
+ this.options.https.cert = fs.readFileSync(this.options.cert);
40
+ this._webServer = https.createServer(this.options.https, this._requestListener);
41
+ } else if (this.options.pfx) {
42
+ debug('HTTPS WebHook enabled (by pfx)');
43
+ this.options.https.pfx = fs.readFileSync(this.options.pfx);
44
+ this._webServer = https.createServer(this.options.https, this._requestListener);
45
+ } else if (Object.keys(this.options.https).length) {
46
+ debug('HTTPS WebHook enabled by (https)');
47
+ this._webServer = https.createServer(this.options.https, this._requestListener);
48
+ } else {
49
+ debug('HTTP WebHook enabled');
50
+ this._webServer = http.createServer(this._requestListener);
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Open WebHook by listening on the port
56
+ * @return {Promise}
57
+ */
58
+
59
+ _createClass(TelegramBotWebHook, [{
60
+ key: 'open',
61
+ value: function open() {
62
+ var _this = this;
63
+
64
+ if (this.isOpen()) {
65
+ return Promise.resolve();
66
+ }
67
+ return new Promise(function (resolve, reject) {
68
+ _this._webServer.listen(_this.options.port, _this.options.host, function () {
69
+ debug('WebHook listening on port %s', _this.options.port);
70
+ _this._open = true;
71
+ return resolve();
72
+ });
73
+
74
+ _this._webServer.once('error', function (err) {
75
+ reject(err);
76
+ });
77
+ });
78
+ }
79
+
80
+ /**
81
+ * Close the webHook
82
+ * @return {Promise}
83
+ */
84
+ }, {
85
+ key: 'close',
86
+ value: function close() {
87
+ var _this2 = this;
88
+
89
+ if (!this.isOpen()) {
90
+ return Promise.resolve();
91
+ }
92
+ return new Promise(function (resolve, reject) {
93
+ _this2._webServer.close(function (error) {
94
+ if (error) return reject(error);
95
+ _this2._open = false;
96
+ return resolve();
97
+ });
98
+ });
99
+ }
100
+
101
+ /**
102
+ * Return `true` if server is listening. Otherwise, `false`.
103
+ */
104
+ }, {
105
+ key: 'isOpen',
106
+ value: function isOpen() {
107
+ // NOTE: Since `http.Server.listening` was added in v5.7.0
108
+ // and we still need to support Node v4,
109
+ // we are going to fallback to 'this._open'.
110
+ // The following LOC would suffice for newer versions of Node.js
111
+ // return this._webServer.listening;
112
+ return this._open;
113
+ }
114
+
115
+ /**
116
+ * Handle error thrown during processing of webhook request.
117
+ * @private
118
+ * @param {Error} error
119
+ */
120
+ }, {
121
+ key: '_error',
122
+ value: function _error(error) {
123
+ if (!this.bot.listeners('webhook_error').length) {
124
+ return console.error('error: [webhook_error] %j', error); // eslint-disable-line no-console
125
+ }
126
+ return this.bot.emit('webhook_error', error);
127
+ }
128
+
129
+ /**
130
+ * Handle request body by passing it to 'callback'
131
+ * @private
132
+ */
133
+ }, {
134
+ key: '_parseBody',
135
+ value: function _parseBody(error, body) {
136
+ if (error) {
137
+ return this._error(new errors.FatalError(error));
138
+ }
139
+
140
+ var data = undefined;
141
+ try {
142
+ data = JSON.parse(body.toString());
143
+ } catch (parseError) {
144
+ return this._error(new errors.ParseError(parseError.message));
145
+ }
146
+
147
+ return this.bot.processUpdate(data);
148
+ }
149
+
150
+ /**
151
+ * Listener for 'request' event on server
152
+ * @private
153
+ * @see https://nodejs.org/docs/latest/api/http.html#http_http_createserver_requestlistener
154
+ * @see https://nodejs.org/docs/latest/api/https.html#https_https_createserver_options_requestlistener
155
+ */
156
+ }, {
157
+ key: '_requestListener',
158
+ value: function _requestListener(req, res) {
159
+ debug('WebHook request URL: %s', req.url);
160
+ debug('WebHook request headers: %j', req.headers);
161
+
162
+ if (req.url.indexOf(this.bot.token) !== -1) {
163
+ if (req.method !== 'POST') {
164
+ debug('WebHook request isn\'t a POST');
165
+ res.statusCode = 418; // I'm a teabot!
166
+ res.end();
167
+ } else {
168
+ req.pipe(bl(this._parseBody)).on('finish', function () {
169
+ return res.end('OK');
170
+ });
171
+ }
172
+ } else if (this._healthRegex.test(req.url)) {
173
+ debug('WebHook health check passed');
174
+ res.statusCode = 200;
175
+ res.end('OK');
176
+ } else {
177
+ debug('WebHook request unauthorized');
178
+ res.statusCode = 401;
179
+ res.end();
180
+ }
181
+ }
182
+ }]);
183
+
184
+ return TelegramBotWebHook;
185
+ })();
186
+
187
+ module.exports = TelegramBotWebHook;
package/lib/utils.js ADDED
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ var util = require('util');
4
+ // Native deprecation warning
5
+ exports.deprecate = function (msg) {
6
+ return util.deprecate(function () {}, msg, 'node-telegram-bot-api')();
7
+ };
package/package.json CHANGED
@@ -1,6 +1,80 @@
1
1
  {
2
2
  "name": "node-telegram-util",
3
- "version": "0.0.1-security",
4
- "description": "security holding package",
5
- "repository": "npm/security-holder"
3
+ "version": "0.69.0",
4
+ "description": "Telegram Bot API",
5
+ "main": "./index.js",
6
+ "directories": {
7
+ "example": "examples",
8
+ "test": "test"
9
+ },
10
+ "keywords": [
11
+ "telegram",
12
+ "telegram bot",
13
+ "telegram bot api",
14
+ "bot"
15
+ ],
16
+ "scripts": {
17
+ "gen-doc": "echo 'WARNING: `npm run gen-doc` is deprecated. Use `npm run doc` instead.' && npm run doc",
18
+ "doc": "jsdoc2md --files src/telegram.js --template doc/api.hbs > doc/api.md",
19
+ "build": "babel -d ./lib src",
20
+ "prepublishOnly": "npm run build",
21
+ "eslint": "eslint ./src ./test ./examples",
22
+ "mocha": "mocha",
23
+ "pretest": "npm run build",
24
+ "test": "npm run eslint && istanbul cover ./node_modules/mocha/bin/_mocha"
25
+ },
26
+ "author": "Yago Pérez <yagoperezs@gmail.com>",
27
+ "license": "MIT",
28
+ "engines": {
29
+ "node": ">=0.12"
30
+ },
31
+ "dependencies": {
32
+ "@cypress/request": "^3.0.1",
33
+ "@cypress/request-promise": "^5.0.0",
34
+ "array.prototype.findindex": "^2.0.2",
35
+ "babel": "^5.8.38",
36
+ "bl": "^1.2.3",
37
+ "debug": "^3.2.7",
38
+ "eventemitter3": "^3.0.0",
39
+ "file-type": "^3.9.0",
40
+ "fs": "^0.0.1-security",
41
+ "https": "^1.0.0",
42
+ "mime": "^1.6.0",
43
+ "node-fetch": "^2.7.0",
44
+ "os": "^0.1.2",
45
+ "path": "^0.12.7",
46
+ "pump": "^2.0.0"
47
+ },
48
+ "devDependencies": {
49
+ "babel-cli": "^6.26.0",
50
+ "babel-eslint": "^8.0.3",
51
+ "babel-plugin-transform-class-properties": "^6.24.1",
52
+ "babel-plugin-transform-es2015-destructuring": "^6.23.0",
53
+ "babel-plugin-transform-es2015-parameters": "^6.24.1",
54
+ "babel-plugin-transform-es2015-shorthand-properties": "^6.24.1",
55
+ "babel-plugin-transform-es2015-spread": "^6.22.0",
56
+ "babel-plugin-transform-object-rest-spread": "^6.26.0",
57
+ "babel-plugin-transform-strict-mode": "^6.24.1",
58
+ "babel-preset-es2015": "^6.24.1",
59
+ "babel-register": "^6.26.0",
60
+ "concat-stream": "^1.6.0",
61
+ "eslint": "^2.13.1",
62
+ "eslint-config-airbnb": "^6.2.0",
63
+ "eslint-plugin-mocha": "^4.11.0",
64
+ "is": "^3.2.1",
65
+ "is-ci": "^1.0.10",
66
+ "istanbul": "^1.1.0-alpha.1",
67
+ "jsdoc-to-markdown": "^3.0.3",
68
+ "mocha": "^3.5.3",
69
+ "mocha-lcov-reporter": "^1.3.0",
70
+ "node-static": "^0.7.10"
71
+ },
72
+ "repository": {
73
+ "type": "git",
74
+ "url": "https://github.com/yagop/node-telegram-bot-api.git"
75
+ },
76
+ "bugs": {
77
+ "url": "https://github.com/yagop/node-telegram-bot-api/issues"
78
+ },
79
+ "homepage": "https://github.com/yagop/node-telegram-bot-api"
6
80
  }
package/src/errors.js ADDED
@@ -0,0 +1,65 @@
1
+ exports.BaseError = class BaseError extends Error {
2
+ /**
3
+ * @class BaseError
4
+ * @constructor
5
+ * @private
6
+ * @param {String} code Error code
7
+ * @param {String} message Error message
8
+ */
9
+ constructor(code, message) {
10
+ super(`${code}: ${message}`);
11
+ this.code = code;
12
+ }
13
+ toJSON() {
14
+ return {
15
+ code: this.code,
16
+ message: this.message,
17
+ };
18
+ }
19
+ };
20
+
21
+
22
+ exports.FatalError = class FatalError extends exports.BaseError {
23
+ /**
24
+ * Fatal Error. Error code is `"EFATAL"`.
25
+ * @class FatalError
26
+ * @constructor
27
+ * @param {String|Error} data Error object or message
28
+ */
29
+ constructor(data) {
30
+ const error = (typeof data === 'string') ? null : data;
31
+ const message = error ? error.message : data;
32
+ super('EFATAL', message);
33
+ if (error) this.stack = error.stack;
34
+ }
35
+ };
36
+
37
+
38
+ exports.ParseError = class ParseError extends exports.BaseError {
39
+ /**
40
+ * Error during parsing. Error code is `"EPARSE"`.
41
+ * @class ParseError
42
+ * @constructor
43
+ * @param {String} message Error message
44
+ * @param {http.IncomingMessage} response Server response
45
+ */
46
+ constructor(message, response) {
47
+ super('EPARSE', message);
48
+ this.response = response;
49
+ }
50
+ };
51
+
52
+
53
+ exports.TelegramError = class TelegramError extends exports.BaseError {
54
+ /**
55
+ * Error returned from Telegram. Error code is `"ETELEGRAM"`.
56
+ * @class TelegramError
57
+ * @constructor
58
+ * @param {String} message Error message
59
+ * @param {http.IncomingMessage} response Server response
60
+ */
61
+ constructor(message, response) {
62
+ super('ETELEGRAM', message);
63
+ this.response = response;
64
+ }
65
+ };