Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom registry and timeout #117

Merged
merged 5 commits into from
Nov 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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