Permalink
Browse files

created utils and refactorized urlEncodeRemoPane

  • Loading branch information...
DiegoTUI committed Nov 27, 2017
1 parent 82f63c6 commit 9ec476b2329749c799e9e6b7b8320a01bbeeda66
Showing with 50 additions and 9 deletions.
  1. +1 −1 bin/app.js
  2. +3 −2 lib/factors/downloads.js
  3. +3 −2 lib/factors/issues.js
  4. +3 −2 lib/factors/versions.js
  5. +2 −1 lib/packages.js
  6. +37 −0 lib/utils.js
  7. +1 −1 test.js
View
@@ -112,7 +112,7 @@ function serve (request, response) {
var mainStream = [];
// look for the package in the registry
mainStream.push(function (callback) {
packages.findInNpmRegistry(npmPackage.replace(/\//g, '%2F'), function (error, result) {
packages.findInNpmRegistry(npmPackage, function (error, result) {
if (error) {
log.debug('npm registry error', error);
return callback(error);
View
@@ -8,6 +8,7 @@
// requires
var Log = require('log');
var config = require('../../config.js');
var utils = require('../utils.js');
var request = require('request');
var moment = require('moment');
var testing = require('testing');
@@ -38,7 +39,7 @@ exports.estimate = function(entry, callback)
};
var now = moment();
var aYearAgo = moment(now).subtract(1,'year');
var downloadsUrl = 'http://npm-stat.com/downloads/range/' + aYearAgo.format('YYYY-MM-DD') + ':' + now.format('YYYY-MM-DD') + '/' + name;
var downloadsUrl = 'http://npm-stat.com/downloads/range/' + aYearAgo.format('YYYY-MM-DD') + ':' + now.format('YYYY-MM-DD') + '/' + utils.urlEncodeRepoName(name);
log.debug('Downloads URL: %s', downloadsUrl);
request.get(downloadsUrl, function(error, response, body) {
if (error || !body)
@@ -145,4 +146,4 @@ exports.test = function(callback)
if (__filename == process.argv[1])
{
exports.test(testing.show);
}
}
View
@@ -9,6 +9,7 @@
require('prototypes');
var Log = require('log');
var config = require('../../config.js');
var utils = require('../utils.js');
var request = require('request');
var url = require('url');
var querystring = require('querystring');
@@ -279,7 +280,7 @@ function getIssuesForPageLastYear (repoOwner, repoName, page, callback)
{
var result = {issues:[]};
var aYearAgo = moment().subtract(1,'year').format();
var issuesUrl = 'https://api.github.com/repos/' + repoOwner + '/' + repoName + '/issues?per_page=100&state=all&since=' + aYearAgo + '&page=' + page;
var issuesUrl = 'https://api.github.com/repos/' + repoOwner + '/' + utils.urlEncodeRepoName(repoName) + '/issues?per_page=100&state=all&since=' + aYearAgo + '&page=' + page;
var pageIssuesUrl = issuesUrl + '&page=' + page;
request({method:'GET', uri:pageIssuesUrl, headers:{'User-Agent':'node.js'}, auth:{user:config.githubToken}}, function(error, response, body)
{
@@ -674,4 +675,4 @@ exports.test = function(callback)
if (__filename == process.argv[1])
{
exports.test(testing.show);
}
}
View
@@ -8,6 +8,7 @@
// requires
require('prototypes');
var Log = require('log');
var utils = require('../utils.js');
var config = require('../../config.js');
var cache = require('../cache.js');
var requestLib = require('../cached-request.js');
@@ -37,7 +38,7 @@ exports.estimate = function(entry, callback)
var estimation = {
versions: [0, 1]
};
var registryUrl = 'http://registry.npmjs.org/' + name;
var registryUrl = 'http://registry.npmjs.org/' + utils.urlEncodeRepoName(name);
log.debug('Estimating versions with URL: %s', registryUrl);
requestLib.get(registryUrl, true, function(error, response, body)
{
@@ -163,4 +164,4 @@ exports.test = function(callback)
if (__filename == process.argv[1])
{
exports.test(testing.show);
}
}
View
@@ -4,6 +4,7 @@
var Log = require('log');
var testing = require('testing');
var db = require('./db.js');
var utils = require('./utils.js');
var cache = require('./cache.js');
var requestLib = require('./cached-request.js');
var config = require('../config.js');
@@ -276,7 +277,7 @@ exports.findInNpmRegistry = function (name, callback)
{
return callback('name is null or empty');
}
var registryUrl = 'http://registry.npmjs.org/' + name;
var registryUrl = 'http://registry.npmjs.org/' + utils.urlEncodeRepoName(name);
// perform request
requestLib.get(registryUrl, true, function(error, getResponse, body)
{
View
@@ -0,0 +1,37 @@
'use strict';
var testing = require('testing');
exports.urlEncodeRepoName = function(name) {
return name.replace(/\//g, '%2F');
};
/************************************************
**************** UNIT TESTS ********************
************************************************/
function testUrlEncodeRepoName(callback)
{
var result = exports.urlEncodeRepoName('kkfu');
testing.assertEquals(result, 'kkfu', 'wrong result for regular string');
result = exports.urlEncodeRepoName('@storybook/react');
testing.assertEquals(result, '@storybook%2Freact', 'wrong result for scoped string');
result = exports.urlEncodeRepoName('@storybook/react/kkfu');
testing.assertEquals(result, '@storybook%2Freact%2Fkkfu', 'wrong result for multi-scoped string');
testing.success(callback);
}
/**
* * Run all tests.
* */
exports.test = function(callback)
{
testing.run([
testUrlEncodeRepoName
], 10000, callback);
};
// run tests if invoked directly
if (__filename == process.argv[1])
{
exports.test(testing.show);
}
View
@@ -20,7 +20,7 @@ exports.test = function(callback)
{
log.debug('Running tests');
var tests = {};
var libs = ['estimation', 'db', 'badges', 'packages', 'update', 'cache', 'cached-request'];
var libs = ['estimation', 'db', 'badges', 'packages', 'update', 'cache', 'cached-request', 'utils'];
var factors = ['issues', 'versions', 'downloads'];
libs.forEach(function(lib)
{

0 comments on commit 9ec476b

Please sign in to comment.