forked from npm/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: remove dead code from get-identity
- Loading branch information
Showing
11 changed files
with
159 additions
and
375 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,24 @@ | ||
const npmFetch = require('npm-registry-fetch') | ||
|
||
const needsAuthError = (msg) => | ||
Object.assign(new Error(msg), { code: 'ENEEDAUTH' }) | ||
|
||
module.exports = async (npm, opts = {}) => { | ||
module.exports = async (npm, opts) => { | ||
const { registry } = opts | ||
if (!registry) { | ||
throw Object.assign(new Error('No registry specified.'), { code: 'ENOREGISTRY' }) | ||
} | ||
|
||
// First, check if we have a user/pass-based auth | ||
const creds = npm.config.getCredentialsByURI(registry) | ||
const { username: usernameFromURI, token } = creds | ||
if (creds.username) { | ||
return creds.username | ||
} | ||
|
||
if (usernameFromURI) { | ||
// Found username; return it | ||
return usernameFromURI | ||
} else if (token) { | ||
// No username, but we have a token; fetch the username from registry | ||
const registryData = await npmFetch.json('/-/whoami', { | ||
...opts, | ||
}) | ||
const { username: usernameFromRegistry } = registryData | ||
// Retrieved username from registry; return it | ||
if (usernameFromRegistry) { | ||
return usernameFromRegistry | ||
} else { | ||
// Didn't get username from registry; bad token | ||
throw needsAuthError( | ||
'Your auth token is no longer valid. Please login again.' | ||
) | ||
} | ||
} else { | ||
// At this point, if they have a credentials object, it doesn't have a | ||
// token or auth in it. Probably just the default registry. | ||
throw needsAuthError('This command requires you to be logged in.') | ||
// No username, but we have a token; fetch the username from registry | ||
if (creds.token) { | ||
const registryData = await npmFetch.json('/-/whoami', { ...opts }) | ||
return registryData.username | ||
} | ||
|
||
// At this point, even if they have a credentials object, it doesn't have a | ||
// valid token. | ||
throw Object.assign( | ||
new Error('This command requires you to be logged in.'), | ||
{ code: 'ENEEDAUTH' } | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,113 +1,67 @@ | ||
const t = require('tap') | ||
const { fake: mockNpm } = require('../../fixtures/mock-npm') | ||
const { load: loadMockNpm } = require('../../fixtures/mock-npm.js') | ||
const MockRegistry = require('../../fixtures/mock-registry.js') | ||
|
||
t.test('pings', async t => { | ||
t.plan(6) | ||
|
||
const registry = 'https://registry.npmjs.org' | ||
let noticeCalls = 0 | ||
const Ping = t.mock('../../../lib/commands/ping.js', { | ||
'../../../lib/utils/ping.js': function (spec) { | ||
t.equal(spec.registry, registry, 'passes flatOptions') | ||
return {} | ||
}, | ||
'proc-log': { | ||
notice: (type, spec) => { | ||
++noticeCalls | ||
if (noticeCalls === 1) { | ||
t.equal(type, 'PING', 'should log a PING') | ||
t.equal(spec, registry, 'should log the registry url') | ||
} else { | ||
t.equal(type, 'PONG', 'should log a PONG') | ||
t.match(spec, /\d+ms/, 'should log the elapsed milliseconds') | ||
} | ||
}, | ||
}, | ||
}) | ||
const npm = mockNpm({ | ||
config: { registry }, | ||
flatOptions: { registry }, | ||
t.test('no details', async t => { | ||
const { npm, logs, joinedOutput } = await loadMockNpm(t) | ||
const registry = new MockRegistry({ | ||
tap: t, | ||
registry: npm.config.get('registry'), | ||
}) | ||
const ping = new Ping(npm) | ||
|
||
await ping.exec([]) | ||
t.equal(noticeCalls, 2, 'should have logged 2 lines') | ||
registry.ping() | ||
await npm.exec('ping', []) | ||
t.match(logs.notice, [['PING', 'https://registry.npmjs.org/'], ['PONG', /[0-9]+ms/]]) | ||
t.equal(joinedOutput(), '') | ||
}) | ||
|
||
t.test('pings and logs details', async t => { | ||
t.plan(8) | ||
t.test('with details', async t => { | ||
const { npm, logs, joinedOutput } = await loadMockNpm(t) | ||
const registry = new MockRegistry({ | ||
tap: t, | ||
registry: npm.config.get('registry'), | ||
}) | ||
registry.ping({ body: { test: true } }) | ||
await npm.exec('ping', []) | ||
t.match(logs.notice, [ | ||
['PING', 'https://registry.npmjs.org/'], | ||
['PONG', /[0-9]+ms/], | ||
['PONG', '{\n "test": true\n}'], | ||
]) | ||
t.match(joinedOutput(), '') | ||
}) | ||
|
||
const registry = 'https://registry.npmjs.org' | ||
const details = { extra: 'data' } | ||
let noticeCalls = 0 | ||
const Ping = t.mock('../../../lib/commands/ping.js', { | ||
'../../../lib/utils/ping.js': function (spec) { | ||
t.equal(spec.registry, registry, 'passes flatOptions') | ||
return details | ||
}, | ||
'proc-log': { | ||
notice: (type, spec) => { | ||
++noticeCalls | ||
if (noticeCalls === 1) { | ||
t.equal(type, 'PING', 'should log a PING') | ||
t.equal(spec, registry, 'should log the registry url') | ||
} else if (noticeCalls === 2) { | ||
t.equal(type, 'PONG', 'should log a PONG') | ||
t.match(spec, /\d+ms/, 'should log the elapsed milliseconds') | ||
} else { | ||
t.equal(type, 'PONG', 'should log a PONG') | ||
const parsed = JSON.parse(spec) | ||
t.match(parsed, details, 'should log JSON stringified details') | ||
} | ||
}, | ||
}, | ||
t.test('valid json', async t => { | ||
const { npm, logs, joinedOutput } = await loadMockNpm(t, { | ||
config: { json: true }, | ||
}) | ||
const npm = mockNpm({ | ||
config: { registry }, | ||
flatOptions: { registry }, | ||
const registry = new MockRegistry({ | ||
tap: t, | ||
registry: npm.config.get('registry'), | ||
}) | ||
registry.ping() | ||
await npm.exec('ping', []) | ||
t.match(logs.notice, [['PING', 'https://registry.npmjs.org/'], ['PONG', /[0-9]+ms/]]) | ||
t.match(JSON.parse(joinedOutput()), { | ||
registry: npm.config.get('registry'), | ||
time: /[0-9]+/, | ||
details: {}, | ||
}) | ||
const ping = new Ping(npm) | ||
|
||
await ping.exec([]) | ||
t.equal(noticeCalls, 3, 'should have logged 3 lines') | ||
}) | ||
|
||
t.test('pings and returns json', async t => { | ||
t.plan(9) | ||
|
||
const registry = 'https://registry.npmjs.org' | ||
const details = { extra: 'data' } | ||
let noticeCalls = 0 | ||
const Ping = t.mock('../../../lib/commands/ping.js', { | ||
'../../../lib/utils/ping.js': function (spec) { | ||
t.equal(spec.registry, registry, 'passes flatOptions') | ||
return details | ||
}, | ||
'proc-log': { | ||
notice: (type, spec) => { | ||
++noticeCalls | ||
if (noticeCalls === 1) { | ||
t.equal(type, 'PING', 'should log a PING') | ||
t.equal(spec, registry, 'should log the registry url') | ||
} else { | ||
t.equal(type, 'PONG', 'should log a PONG') | ||
t.match(spec, /\d+ms/, 'should log the elapsed milliseconds') | ||
} | ||
}, | ||
}, | ||
t.test('invalid json', async t => { | ||
const { npm, logs, joinedOutput } = await loadMockNpm(t, { | ||
config: { json: true }, | ||
}) | ||
const npm = mockNpm({ | ||
config: { registry, json: true }, | ||
flatOptions: { registry }, | ||
output: function (spec) { | ||
const parsed = JSON.parse(spec) | ||
t.equal(parsed.registry, registry, 'returns the correct registry url') | ||
t.match(parsed.details, details, 'prints returned details') | ||
t.type(parsed.time, 'number', 'returns time as a number') | ||
}, | ||
const registry = new MockRegistry({ | ||
tap: t, | ||
registry: npm.config.get('registry'), | ||
}) | ||
registry.ping({ body: '{not: real"json]' }) | ||
await npm.exec('ping', []) | ||
t.match(logs.notice, [['PING', 'https://registry.npmjs.org/'], ['PONG', /[0-9]+ms/]]) | ||
t.match(JSON.parse(joinedOutput()), { | ||
registry: npm.config.get('registry'), | ||
time: /[0-9]+/, | ||
details: {}, | ||
}) | ||
const ping = new Ping(npm) | ||
|
||
await ping.exec([]) | ||
t.equal(noticeCalls, 2, 'should have logged 2 lines') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.