mirror of
https://github.com/yagop/node-telegram-bot-api
synced 2025-08-30 05:48:00 +00:00
[webhook] Do not rely on http.Server#listening property
Bug: The 'http.Server#listening' property was added in Node.js v5.7.0! Support the older Node.js versions!
This commit is contained in:
parent
6753f7befd
commit
e009b60efa
@ -28,6 +28,7 @@ class TelegramBotWebHook {
|
||||
this.callback = callback;
|
||||
this._regex = new RegExp(this.token);
|
||||
this._webServer = null;
|
||||
this._open = false;
|
||||
this._requestListener = this._requestListener.bind(this);
|
||||
this._parseBody = this._parseBody.bind(this);
|
||||
|
||||
@ -54,12 +55,13 @@ class TelegramBotWebHook {
|
||||
* @return {Promise}
|
||||
*/
|
||||
open() {
|
||||
if (this._webServer.listening) {
|
||||
if (this.isOpen()) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
return new Promise(resolve => {
|
||||
this._webServer.listen(this.options.port, this.options.host, () => {
|
||||
debug('WebHook listening on port %s', this.options.port);
|
||||
this._open = true;
|
||||
return resolve();
|
||||
});
|
||||
});
|
||||
@ -70,12 +72,13 @@ class TelegramBotWebHook {
|
||||
* @return {Promise}
|
||||
*/
|
||||
close() {
|
||||
if (!this._webServer.listening) {
|
||||
if (!this.isOpen()) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
this._webServer.close(error => {
|
||||
if (error) return reject(error);
|
||||
this._open = false;
|
||||
return resolve();
|
||||
});
|
||||
});
|
||||
@ -85,7 +88,12 @@ class TelegramBotWebHook {
|
||||
* Return `true` if server is listening. Otherwise, `false`.
|
||||
*/
|
||||
isOpen() {
|
||||
return this._webServer.listening;
|
||||
// NOTE: Since `http.Server.listening` was added in v5.7.0
|
||||
// and we still need to support Node v4,
|
||||
// we are going to fallback to 'this._open'.
|
||||
// The following LOC would suffice for newer versions of Node.js
|
||||
// return this._webServer.listening;
|
||||
return this._open;
|
||||
}
|
||||
|
||||
// used so that other funcs are not non-optimizable
|
||||
|
@ -54,6 +54,7 @@ describe('module.exports', function moduleExportsSuite() {
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('TelegramBot', function telegramSuite() {
|
||||
let bot;
|
||||
let testbot;
|
||||
|
Loading…
x
Reference in New Issue
Block a user