Skip to content

Commit

Permalink
feat(registry): support custom registry and timeout (#117)
Browse files Browse the repository at this point in the history
* feat(registry): provide new option '--registry' to set a custom registry url #116

* feat(registry): fix passing options correctly into fetchVersions #116

* feat(check-version-timeout): define custom timeout for version check #116

* feat(registry/check-version-timeout): clean up code #116

* feat(registry/check-version-timeout): reset version to 0.0.0 #116
  • Loading branch information
xpure-sklatt authored and bahmutov committed Nov 6, 2017
1 parent 9205cc6 commit ad2cf7b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
/cover
/test/cover
/src/test/npm-debug.log
.idea/
*.eml
*.iml
.DS_Store
node_modules/
npm-debug.log
4 changes: 3 additions & 1 deletion bin/next-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ if (program.available) {
allow: allow,
type: program.type,
tldr: program.tldr,
without: program.without
without: program.without,
registry: program.registry,
checkVersionTimeout: program['check-version-timeout'] || 10000
}

var checkCurrent = nextUpdate.checkCurrentInstall.bind(null, opts)
Expand Down
10 changes: 10 additions & 0 deletions src/cli-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ var program = optimist
alias: 'L',
description: 'print commit changes between working versions'
})
.options('registry', {
string: true,
default: false,
description: 'use a custom registry url'
})
.options('check-version-timeout', {
number: true,
default: 10000,
description: 'define a custom timeout value for checking next versions'
})
.usage(info)
.argv

Expand Down
13 changes: 6 additions & 7 deletions src/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ function isNotFound (str) {
// fetching versions inspired by
// https://github.com/jprichardson/npm-latest
// returns a promise
function fetchVersions (nameVersion) {
function fetchVersions (options, nameVersion) {
// console.log(nameVersion);
// TODO use check.schema
check.verify.object(nameVersion, 'expected name, version object')
Expand All @@ -161,7 +161,7 @@ function fetchVersions (nameVersion) {
}

// console.log('fetching versions for', name, 'current version', version);
var MAX_WAIT_TIMEOUT = 25000
var MAX_WAIT_TIMEOUT = options.checkVersionTimeout || 25000
var deferred = q.defer()

function rejectOnTimeout () {
Expand All @@ -180,7 +180,7 @@ function fetchVersions (nameVersion) {
la(check.webUrl(npmUrl), 'need npm registry url, got', npmUrl)

npmUrl = npmUrl.replace(/^https:/, 'http:').trim()
var url = npmUrl + escapeName(name)
var url = (options.registry || npmUrl) + escapeName(name)

// TODO how to detect if the registry is not responding?

Expand Down Expand Up @@ -310,14 +310,13 @@ function nextVersions (options, nameVersionPairs, checkLatestOnly) {
check.verify.array(nameVersionPairs, 'expected array')
nameVersionPairs = cleanVersions(nameVersionPairs)

var MAX_CHECK_TIMEOUT = 10000

const verbose = verboseLog(options)
verbose('checking NPM registry')
var MAX_CHECK_TIMEOUT = options.checkVersionTimeout || 10000

var fetchPromises = nameVersionPairs.map(fetchVersions)
var fetchPromises = nameVersionPairs.map(fetchVersions.bind(null, options))
var fetchAllPromise = q.all(fetchPromises)
.timeout(MAX_CHECK_TIMEOUT, 'timed out waiting for NPM')
.timeout(MAX_CHECK_TIMEOUT, 'timed out waiting for NPM after ' + MAX_CHECK_TIMEOUT + 'ms')

return fetchAllPromise.then(
_.partial(filterFetchedVersions, checkLatestOnly),
Expand Down
12 changes: 6 additions & 6 deletions src/test/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ function onError (error) {

gt.test('basic of fetch', function () {
gt.func(fetchVersions)
gt.arity(fetchVersions, 1)
gt.arity(fetchVersions, 2)
})

gt.async('fetch non existent module', 2, function () {
var promise = fetchVersions({
var promise = fetchVersions({}, {
name: 'this-module-should-not-exist-at-all',
version: '0.2.0'
})
Expand All @@ -31,8 +31,8 @@ gt.async('fetch non existent module', 2, function () {

gt.async('fetch gt later versions', function () {
gt.func(fetchVersions)
gt.arity(fetchVersions, 1)
var promise = fetchVersions({ name: 'gt', version: '0.5.0' })
gt.arity(fetchVersions, 2)
var promise = fetchVersions({}, { name: 'gt', version: '0.5.0' })
gt.func(promise.then, 'return object has then method')
promise.then(function (results) {
gt.object(results, 'returns an object')
Expand All @@ -44,8 +44,8 @@ gt.async('fetch gt later versions', function () {

gt.async('fetch module later versions', function () {
gt.func(fetchVersions)
gt.arity(fetchVersions, 1)
var promise = fetchVersions({ name: 'lodash', version: '0.7.0' })
gt.arity(fetchVersions, 2)
var promise = fetchVersions({}, { name: 'lodash', version: '0.7.0' })
gt.func(promise.then, 'return object has then method')
promise.then(function (results) {
gt.object(results, 'returns an object')
Expand Down

0 comments on commit ad2cf7b

Please sign in to comment.