From be49b69219a93c3c5385ed03265e49939f35ee24 Mon Sep 17 00:00:00 2001 From: GochoMugo Date: Fri, 7 Oct 2016 19:52:00 +0300 Subject: [PATCH] Fix handling fs.readStream.path if it's a buffer Bug: The (private) method TelegramBot#_formatSendData(), used by public methods, such as TelegramBot#sendPhoto(), throws an error if the stream passed (fs.readStream) has the property 'path', being an instance of Buffer. For example, const stream = fs.createReadStream(Buffer.from('cat.png')); bot.sendPhoto(chatId, stream); Would throw an error, like TypeError: Path must be a string. Received This is because of this line: src/telegram.js:297 fileName = URL.parse(path.basename(data.path)).pathname; path.basename() can not handle buffer (non-string) paths. From the docs, "A TypeError is thrown if path is not a string...". Fix: Ensure path.basename() receives a string, by converting the buffer to string. References: * fs.ReadStream: https://nodejs.org/docs/latest/api/fs.html#fs_class_fs_readstream --- src/telegram.js | 2 +- test/index.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/telegram.js b/src/telegram.js index 7359306..df0105c 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -294,7 +294,7 @@ class TelegramBot extends EventEmitter { let fileName; let fileId; if (data instanceof stream.Stream) { - fileName = URL.parse(path.basename(data.path)).pathname; + fileName = URL.parse(path.basename(data.path.toString())).pathname; formData = {}; formData[type] = { value: data, diff --git a/test/index.js b/test/index.js index f7ebb01..803dc43 100644 --- a/test/index.js +++ b/test/index.js @@ -162,6 +162,16 @@ describe('Telegram', function telegramSuite() { }); }); + describe('#_formatSendData', function _formatSendData() { + it('should handle buffer path from fs.readStream', function test() { + const bot = new Telegram(TOKEN); + const photo = fs.createReadStream(Buffer.from(`${__dirname}/bot.gif`)); + return bot.sendPhoto(USERID, photo).then(resp => { + assert.ok(is.object(resp)); + }); + }); + }); + describe('#sendPhoto', function sendPhotoSuite() { let photoId; it('should send a photo from file', function test() {