From 172f46d2cd276c659bca6d3c742b5cecb66d0ff5 Mon Sep 17 00:00:00 2001 From: m4bwav Date: Tue, 16 Aug 2016 22:38:54 -0500 Subject: [PATCH] Removed stackexchange dependency because that package had a vulernability and didn't update their request version. Replaced it with a direct request to the 2 urls the lib needs to get the markdown. --- index.js | 73 +++++++++++++++++++++++++++++++++++----------------- package.json | 2 +- test.js | 14 ++++++++++ 3 files changed, 65 insertions(+), 24 deletions(-) diff --git a/index.js b/index.js index 68b8409..de255cd 100644 --- a/index.js +++ b/index.js @@ -1,38 +1,38 @@ 'use strict'; -var Stackexchange = require('stackexchange'); -var exports = module.exports = {}; - -function initializeStackExchangeContext(options) { - var stackApiOptions = { - version: 2.2, - site: options.site - }; +var url = require('url'); +var zlib = require('zlib'); +var request = require('request'); - return new Stackexchange(stackApiOptions); -} +var exports = module.exports = {}; -function retrieveMarkdown(options, callback) { - var stackContext = initializeStackExchangeContext(options); +function generateRequestUrl(options) { + var baseRequestUrl = options.isForAnswer ? + '/2.2/answers/' + options.entityId : + '/2.2/questions/' + options.entityId; - if (!options.entityId) { - throw new Error('Need an entity id to read'); - } - - var filter = { + var queryParams = { // key: 'YOUR_API_KEY', order: 'asc', filter: '!L_(I6pMIzdXP-hC1clc9EY' }; if (options.apiKey) { - filter.key = options.apiKey; + queryParams.key = options.apiKey; } - var stackApiMethod = options.isForAnswer ? - stackContext.answers.answers : - stackContext.questions.questions; - stackApiMethod(filter, function (err, results) { + queryParams.site = options.site ? options.site : 'stackoverflow'; + + return url.format({ + protocol: 'https:', + host: 'api.stackexchange.com', + pathname: baseRequestUrl, + query: queryParams + }); +} + +function performRetrieval(options, callback) { + function prepareToCallback(err, results) { var markdown = results && results.items && results.items[0] ? results.items[0].body_markdown : null; @@ -41,7 +41,34 @@ function retrieveMarkdown(options, callback) { throw new Error(results.error_message); } callback(markdown, err); - }, [options.entityId]); + } + + var requestUrl = generateRequestUrl(options); + + request({url: requestUrl, encoding: null}, function processResponse(error, result) { + if (error) { + prepareToCallback(error); + } else { + var body = result.body; + zlib.unzip(body, function handleUnzip(error, body) { + try { + var bodyObj = JSON.parse(body.toString()); + + prepareToCallback(error, bodyObj); + } catch (error) { + prepareToCallback(error); + } + }); + } + }); +} + +function retrieveMarkdown(options, callback) { + if (!options.entityId) { + throw new Error('Need an entity id to read'); + } + + performRetrieval(options, callback); } exports.retrieveMarkdown = retrieveMarkdown; diff --git a/package.json b/package.json index 3afd973..8b3bca2 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,6 @@ }, "dependencies": { "commander": "^2.9.0", - "stackexchange": "^0.4.0" + "request": "2.74.x" } } diff --git a/test.js b/test.js index ec6c819..e30993a 100644 --- a/test.js +++ b/test.js @@ -3,6 +3,20 @@ import stackExchangeMarkdownRetriever from './'; global.Promise = Promise; +test.cb('Can retrieve an average answer\'s markdown', function (t) { + var options = { + site: 'scifi.stackexchange.com', + isForAnswer: true, + entityId: 1010 + }; + + stackExchangeMarkdownRetriever.retrieveMarkdown(options, function (markdown) { + t.true(Boolean(markdown)); + t.true(markdown.length > 0); + t.end(); + }); +}); + test.cb('Can retrieve an average question\'s markdown', function (t) { var options = { site: 'scifi.stackexchange.com',