Skip to content
Open
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
10 changes: 10 additions & 0 deletions lib/verify-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const getError = require('./get-error');
const getRegistry = require('./get-registry');
const setNpmrcAuth = require('./set-npmrc-auth');

const memo = {};

module.exports = async (npmrc, pkg, context) => {
const {
cwd,
Expand All @@ -17,12 +19,20 @@ module.exports = async (npmrc, pkg, context) => {
await setNpmrcAuth(npmrc, registry, context);

if (normalizeUrl(registry) === normalizeUrl(DEFAULT_NPM_REGISTRY)) {
const key = npmrc + registry;
if (memo[key]) {
return memo[key];
}

try {
const whoamiResult = execa('npm', ['whoami', '--userconfig', npmrc, '--registry', registry], {cwd, env});
whoamiResult.stdout.pipe(stdout, {end: false});
whoamiResult.stderr.pipe(stderr, {end: false});

memo[key] = whoamiResult;
await whoamiResult;
} catch {
memo[key] = undefined;
throw new AggregateError([getError('EINVALIDNPMTOKEN', {registry})]);
}
}
Expand Down
43 changes: 43 additions & 0 deletions test/verify-auth.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const test = require('ava');
const {stub} = require('sinon');
const tempy = require('tempy');

const getRegistryPath = require.resolve('../lib/get-registry');
const verifyAuthPath = require.resolve('../lib/verify-auth');
const setNmprcAuthPath = require.resolve('../lib/set-npmrc-auth');
const execaPath = require.resolve('execa');

const resetModuleCache = () => {
require.cache[getRegistryPath] = undefined;
require.cache[verifyAuthPath] = undefined;
require.cache[setNmprcAuthPath] = undefined;
require.cache[execaPath] = undefined;
};

test.before(resetModuleCache);
test.after(resetModuleCache);

test('Verify `npm-whoami` calls memoization', async (t) => {
const pkg = {};
const context = {cwd: tempy.directory(), env: {}};
const fakeExeca = stub().returns({stdout: {pipe() {}}, stderr: {pipe() {}}});

require.cache[getRegistryPath] = {id: getRegistryPath, exports: () => 'https://registry.npmjs.org/'};
require.cache[setNmprcAuthPath] = {id: setNmprcAuthPath, exports: () => {}};
require.cache[execaPath] = {id: execaPath, exports: fakeExeca};

const verifyAuth = require('../lib/verify-auth');

await verifyAuth('foo', pkg, context);
await verifyAuth('foo', pkg, context);
await verifyAuth('foo', pkg, context);

t.assert(fakeExeca.calledOnce);

fakeExeca.resetHistory();

await verifyAuth('foo', pkg, context);
await verifyAuth('bar', pkg, context);

t.assert(fakeExeca.calledTwice);
});