Skip to content

Commit

Permalink
refactor(verify-auth): apply memoization directly to npm-whoami calls
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Nov 19, 2021
1 parent ac7ffc8 commit 683f0da
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async function verifyConditions(pluginConfig, context) {
const pkg = await getPkg(pluginConfig, context);

// Verify the npm authentication only if `npmPublish` is not false and `pkg.private` is not `true`
if (!verified && pluginConfig.npmPublish !== false && pkg.private !== true) {
if (pluginConfig.npmPublish !== false && pkg.private !== true) {
await verifyNpmAuth(npmrc, pkg, context);
}
} catch (error) {
Expand Down
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
45 changes: 45 additions & 0 deletions test/verify-auth.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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 cwd = tempy.directory();
const pkg = {};
const env = {};
const context = {cwd, 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);
});

0 comments on commit 683f0da

Please sign in to comment.