From b2429f21fb8bf08ea3270ae49af8b9b5474a6df8 Mon Sep 17 00:00:00 2001 From: Max Stepanov Date: Sun, 9 Aug 2020 21:46:04 -0400 Subject: [PATCH] Socket hangup on GetUpdates Details of change: - added new parameter into api._call() method allowing to configure connection related properties - added parameter timeout to request calls Issue #74 --- lib/api.js | 6 ++++-- lib/providers/update.js | 2 ++ package.json | 2 +- test/api.methods.test.js | 3 ++- test/provider.getupdate.test.js | 12 ++++++------ 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/api.js b/lib/api.js index 4b0702d..6684d56 100644 --- a/lib/api.js +++ b/lib/api.js @@ -158,7 +158,7 @@ class Api extends EventEmitter { } } - _call(methodName, params) { + _call(methodName, params, config) { return new Promise((resolve, reject) => { for (const key in params) { @@ -172,10 +172,12 @@ class Api extends EventEmitter { debug('Do (%s) with params => %o', methodName, params) + const timeout = config && config.timeout ? config.timeout : 2000 this._request({ method: 'POST', uri: this._getBaseUrl() + methodName, - formData: params ? params : null + formData: params ? params : null, + timeout: timeout }, (err, response, body) => { if (err) { debug('Telegram error: %o', err) diff --git a/lib/providers/update.js b/lib/providers/update.js index 997a31f..793ee1f 100644 --- a/lib/providers/update.js +++ b/lib/providers/update.js @@ -75,6 +75,8 @@ class UpdateProvider { limit: this._limit ? this._limit: undefined, timeout: this._timeout ? this._timeout: undefined, allowed_updates: this._allowed_updates ? this._allowed_updates: undefined + }, { + timeout: 1000*this._timeout + 1000 }) .then(json => { if (!Array.isArray(json)) { diff --git a/package.json b/package.json index fcf9d7e..bcb181a 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "telegram-bot-api", "description": "First Telegram Bot API node.js library", - "version": "2.0.0", + "version": "2.0.1", "repository": { "type": "git", "url": "https://github.com/mast/telegram-bot-api.git" diff --git a/test/api.methods.test.js b/test/api.methods.test.js index 9bdc6c5..fff5db6 100644 --- a/test/api.methods.test.js +++ b/test/api.methods.test.js @@ -112,7 +112,8 @@ describe('Api methods', () => { obj: "{\"a\":1}", file: this.file }, - uri: 'https://api.telegram.org/bot123/' + method + uri: 'https://api.telegram.org/bot123/' + method, + timeout: 2000 } this.api[method](callParams) diff --git a/test/provider.getupdate.test.js b/test/provider.getupdate.test.js index 689fedd..70fc49d 100644 --- a/test/provider.getupdate.test.js +++ b/test/provider.getupdate.test.js @@ -110,7 +110,7 @@ describe('Provider => GetUpdate', () => { limit: undefined, offset: 0, timeout: 60 - }) + }, {timeout: 61000}) expect(mp._offset).toBe(0) expect(setTimeout).toHaveBeenCalledTimes(2) @@ -146,7 +146,7 @@ describe('Provider => GetUpdate', () => { limit: undefined, offset: 0, timeout: 60 - }) + }, {timeout: 61000}) expect(mp._offset).toBe(2) expect(api._onUpdate).toHaveBeenLastCalledWith({update_id: 1, data: {}}) @@ -169,7 +169,7 @@ describe('Provider => GetUpdate', () => { limit: undefined, offset: 2, timeout: 60 - }) + }, {timeout: 61000}) expect(mp._offset).toBe(51) expect(setTimeout).toHaveBeenCalledTimes(3) @@ -209,7 +209,7 @@ describe('Provider => GetUpdate', () => { limit: undefined, offset: 0, timeout: 60 - }) + }, {timeout: 61000}) expect(mp._offset).toBe(3) expect(api._onUpdate.mock.calls.length).toBe(2) @@ -244,7 +244,7 @@ describe('Provider => GetUpdate', () => { limit: undefined, offset: 0, timeout: 60 - }) + }, {timeout: 61000}) expect(mp._offset).toBe(0) expect(setTimeout).toHaveBeenCalledTimes(2) @@ -277,7 +277,7 @@ describe('Provider => GetUpdate', () => { limit: undefined, offset: 0, timeout: 60 - }) + }, {timeout: 61000}) expect(mp._offset).toBe(0) expect(setTimeout).toHaveBeenCalledTimes(2)