Skip to content

Commit

Permalink
Removed stackexchange dependency because that package had a vulernabi…
Browse files Browse the repository at this point in the history
…lity and didn't update their request version. Replaced it with a direct request to the 2 urls the lib needs to get the markdown.
  • Loading branch information
m4bwav committed Aug 17, 2016
1 parent e563f0f commit 172f46d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 24 deletions.
73 changes: 50 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@
},
"dependencies": {
"commander": "^2.9.0",
"stackexchange": "^0.4.0"
"request": "2.74.x"
}
}
14 changes: 14 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 172f46d

Please sign in to comment.