From d2b340d800b489eafbc4c12c8534626fa6f81bfe Mon Sep 17 00:00:00 2001 From: MrSheepSheep Date: Thu, 18 Feb 2021 10:40:00 +0100 Subject: [PATCH] Swapped request with node-fetch, removed Bluebird --- lib/msfrpc.js | 58 ++++++++++++++++++++------------------------------- package.json | 5 ++--- 2 files changed, 25 insertions(+), 38 deletions(-) diff --git a/lib/msfrpc.js b/lib/msfrpc.js index be4e18c..e112631 100644 --- a/lib/msfrpc.js +++ b/lib/msfrpc.js @@ -8,9 +8,8 @@ process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; */ const URL = require('url'); const decamelize = require('decamelize'); -const request = require('request'); +const fetch = require('node-fetch'); const MsgPack5 = require('msgpack5'); -const Promise = require('bluebird'); /** * Instance MsgPack5. @@ -106,41 +105,30 @@ module.exports = class MsfRpc { // Encode method and arguments as msgpack buffer. const buffer = msgpack.encode([method, ...args]); - return new Promise((resolve) => { - // Send post request to the MsfRpc server. - request({ - method: 'POST', - uri: `${this.ssl ? 'https' : 'http'}://${this.host}:${this.port}/api/1.0`, - body: buffer, - headers: { - 'content-type': 'binary/message-pack' - }, - // This translates to encode body as binary data. - encoding: null - }, (error, response, body) => { - if(error) { - throw error; - } else { - // Parse body. - body = this._parseBody(body) + // Send post request to the MsfRpc server. + return fetch(`${this.ssl ? 'https' : 'http'}://${this.host}:${this.port}/api/1.0`, { + method: 'POST', + body: buffer, + headers: { + 'content-type': 'binary/message-pack' + } + }).then(result => result.buffer()).then(body => { + // Parse body. + body = this._parseBody(body) - // If server responded with an exception, build and throw error. - if(body.error) { - const errorLines = []; - errorLines.push(`${body.error_message}`); - errorLines.push(` Backtrace:`); - body.error_backtrace.forEach((trace) => { - errorLines.push(` ${trace}`); - }); - errorLines.push(''); - throw new Error(errorLines.join('\n')); - } else { - // Else, resolve with parsed body. - resolve(body); - } + // If server responded with an exception, build and throw error. + if(body.error) { + const errorLines = []; + errorLines.push(`${body.error_message}`); + errorLines.push(` Backtrace:`); + body.error_backtrace.forEach((trace) => { + errorLines.push(` ${trace}`); + }); + errorLines.push(''); + throw new Error(errorLines.join('\n')); } - }); - }); + return body + }) } /** diff --git a/package.json b/package.json index 503a10b..8965dc7 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,6 @@ "bluebird": "^3.5.0", "decamelize": "^1.2.0", "msgpack5": "^3.5.0", - "request": "^2.81.0" - }, - "devDependencies": {} + "node-fetch": "^2.6.1" + } }