Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

libesvm

Package Overview
Dependencies
Maintainers
2
Versions
61
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

libesvm - npm Package Compare versions

Comparing version 0.0.12 to 0.0.15

test/fetchAll.js

87

lib/fetchAllTags.js
var _ = require('lodash');
var cache = require('./cache');
var Promise = require('bluebird');
var utils = require('./utils');
var querystring = require('querystring');
/**
* Fetches a page of tags from the GitHub tags endpoint
* @param {object} opts The options object { page: 1, per_page: 100 }
* @param {function} cb The node style callback
* @returns {Promise}
*/
var fetchTags = function (queryString, cb) {
querysString = _.defaults(queryString || {}, { page: 1, per_page: 100 });
var options = {
url: 'https://api.github.com/repos/elastic/elasticsearch/tags',
json: true,
headers: {
'User-Agent': 'ccowan/elastic-loader',
'Accept': '*/*'
}
};
options.url += '?' + querystring.stringify(queryString);
return utils.request(options).then(function (res) {
return res[1];
}).nodeify(cb);
};
/**
* Fetches all the tags from GitHub

@@ -33,21 +12,53 @@ * @param {function} cb The node style callback

module.exports = function (cb) {
var results = [];
var perPage = 100;
var page = 1;
var processResp = function (tags) {
return Promise.props({
tags: cache.get('tags'),
etag: cache.get('tagsEtag'),
etagRefreshedAt: cache.get('tagsEtagRefreshedAt')
})
.then(function (props) {
var reqOpts = {
url: 'https://download.elasticsearch.org/esvm/elasticsearch.tags.json',
json: true,
headers: {}
};
// Add the tags to the results
results = results.concat(tags);
if (props.etag && props.tags) {
var sinceRefresh = Date.now() - props.etagRefreshedAt;
var fiveMinutes = 1000 * 60 * 5;
// If the returned length is not equal to the number of tags per page then
// return the results;
if (tags.length !== perPage) return results;
if (sinceRefresh <= fiveMinutes) {
// etag refreshed within the last 5 minutes, resolving with cache
return props.tags;
}
// Increment the page and create another requests
var opts = { page: ++page, per_page: perPage };
return fetchTags(opts).then(processResp);
};
reqOpts.headers['If-None-Match'] = props.etag;
}
// Kick off the fetches
return fetchTags({ page: page, per_page: perPage }).then(processResp);
return utils.request(reqOpts)
.then(function (args) {
var resp = args[0];
var body = args[1];
switch (resp && resp.statusCode) {
case 200:
return Promise.all([
cache.set('tags', body),
cache.set('tagsEtag', resp.headers.etag),
cache.set('tagsEtagRefreshedAt', Date.now())
])
.thenReturn(body);
case 304:
return Promise.all([
cache.set('tagsEtag', resp.headers.etag),
cache.set('tagsEtagRefreshedAt', Date.now())
])
.thenReturn(body);
default:
throw new Error('Unable to fetch tags, got response ' + (resp ? resp.statusCode : 0) + ' - ' + body);
}
});
})
.nodeify(cb);
};
var _ = require('lodash');
var getLatestTags = require('./getLatestTags');
var fetchAllTags = require('./fetchAllTags');
var semver = require('semver');
// match valid tag names and extract that parts that we want semver to consider
var TAG_PARTS_RE = /^v?(\d+\.\d+\.\d+)(?:\.([a-zA-Z0-9_\-]+))?$/;
function mapTagToSemver(version) {
var matches = version.match(TAG_PARTS_RE);
if (!matches) return undefined;
return matches[1] + (matches[2] ? '-' + matches[2] : '');
}
/**

@@ -12,24 +21,22 @@ * Resolve the latest version

module.exports = function (options, cb) {
return getLatestTags().then(function (tags) {
tags = _.map(tags, function (tag) {
if (tag.name) tag.name = tag.name.replace(/\.(beta|rc)/i, '-$1');
return tag;
return fetchAllTags().then(function (tags) {
var versions = _(tags).pluck('name').map(String).invoke('trim').value();
var validVersions = versions.map(mapTagToSemver).filter(Boolean).sort(semver.rcompare);
var version = _.find(validVersions, function (tag) {
return semver.satisfies(tag, options.version);
});
tags.sort(function (v1, v2) {
return semver.gt(v1.name, v2.name);
}).reverse();
var length = tags.length;
for (var i = 0; i<length; i++) {
if (!tags[i].name) continue;
var matches = tags[i].name.match(/^v(\d+\.\d+\.\d+)(\.(.+))?/);
var name = matches[1];
if (matches[3]) name += '-' + matches[3];
if (semver.satisfies(name, options.version)) {
return tags[i].name.replace('v', '');
}
if (version) return version;
var error = 'Unable to find a version for "' + options.version + '"';
var invalidVersions = _.difference(versions, validVersions);
if (validVersions.length) {
error += ' in "' + validVersions.join('", "') + '"';
}
if (invalidVersions.length) {
error += ' with invalid versions "' + invalidVersions.join('", "') + '"';
}
throw new Error('A suitable version was not found.');
throw new Error(error);
});
};
};

@@ -5,10 +5,6 @@ var utils = module.exports;

var _ = require('lodash');
var getAsync = Promise.promisify(request.get);
utils.cache = require('./cache');
utils.request = Promise.promisify(request.get);
utils.request = function (options) {
return getAsync(options);
};
utils.flatten = function (object) {

@@ -15,0 +11,0 @@ var flatten = function (memo, val, key, collection, parents) {

var temp = require('temp');
var fs = require('fs');
var Promises = require('bluebird');
temp.track();
var writeFile = require('bluebird').promisify(fs.writeFile);
module.exports = function (config, cb) {
return new Promises(function (resolve, reject) {
var options = { prefix: 'libesvm-', suffix: '.json' };
temp.open(options, function (err, info) {
fs.write(info.fd, JSON.stringify(config));
fs.close(info.fd, function (err) {
if (err) reject(err);
resolve(info.path);
});
});
}).nodeify(cb);
var trash = [];
process.on('exit', function () {
trash.forEach(function (p) {
fs.unlinkSync(p);
});
});
module.exports = function (config) {
var path = temp.path({ prefix: 'libesvm-', suffix: '.json' });
trash.push(path);
return writeFile(path, JSON.stringify(config), 'utf8').thenReturn(path);
};
{
"name": "libesvm",
"version": "0.0.12",
"version": "0.0.15",
"description": "libesvm is a library for managning an Elasticsearch process for development and testing.",

@@ -41,6 +41,8 @@ "main": "index.js",

"cli-color": "^0.3.2",
"connect": "^3.3.5",
"mocha": "*",
"moment": "^2.7.0",
"progress": "^1.1.7"
"progress": "^1.1.7",
"sinon": "^1.14.1"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc