Skip to content

Commit

Permalink
Ensure malformedRegistryResponse errors always have a dependency name (
Browse files Browse the repository at this point in the history
  • Loading branch information
greysteil authored and arcanis committed Oct 2, 2018
1 parent 49a157c commit b07a19b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
19 changes: 19 additions & 0 deletions __tests__/registries/npm-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -866,4 +866,23 @@ describe('checkOutdated functional test', () => {

expect(message).toEqual(expect.stringContaining('No valid versions'));
});

test('package with an empty response', async () => {
const testCwd = '.';
const {mockRequestManager, mockRegistries, mockReporter} = createMocks();
const npmRegistry = new NpmRegistry(testCwd, mockRegistries, mockRequestManager, mockReporter, true, []);

mockRequestManager.request = () => {
return {};
};

let message;
try {
await npmRegistry.checkOutdated(mockConfig, 'left-pad', '2.0.0');
} catch (err) {
message = err.message;
}

expect(message).toEqual(expect.stringContaining('malformed response from registry for "left-pad"'));
});
});
5 changes: 3 additions & 2 deletions src/registries/npm-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,15 @@ export default class NpmRegistry extends Registry {
}

async checkOutdated(config: Config, name: string, range: string): CheckOutdatedReturn {
const req = await this.request(NpmRegistry.escapeName(name), {unfiltered: true});
const escapedName = NpmRegistry.escapeName(name);
const req = await this.request(escapedName, {unfiltered: true});
if (!req) {
throw new Error('couldnt find ' + name);
}

// By default use top level 'repository' and 'homepage' values
let {repository, homepage} = req;
const wantedPkg = await NpmResolver.findVersionInRegistryResponse(config, range, req);
const wantedPkg = await NpmResolver.findVersionInRegistryResponse(config, escapedName, range, req);

// But some local repositories like Verdaccio do not return 'repository' nor 'homepage'
// in top level data structure, so we fallback to wanted package manifest
Expand Down
9 changes: 6 additions & 3 deletions src/resolvers/registries/npm-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default class NpmResolver extends RegistryResolver {

static async findVersionInRegistryResponse(
config: Config,
name: string,
range: string,
body: RegistryResponse,
request: ?PackageRequest,
Expand All @@ -40,7 +41,7 @@ export default class NpmResolver extends RegistryResolver {
}

if (!body['dist-tags'] || !body.versions) {
throw new MessageError(config.reporter.lang('malformedRegistryResponse', body.name));
throw new MessageError(config.reporter.lang('malformedRegistryResponse', name));
}

if (range in body['dist-tags']) {
Expand Down Expand Up @@ -91,10 +92,12 @@ export default class NpmResolver extends RegistryResolver {
}
}

const body = await this.config.registries.npm.request(NpmRegistry.escapeName(this.name));
const escapedName = NpmRegistry.escapeName(this.name);
const desiredRange = desiredVersion || this.range;
const body = await this.config.registries.npm.request(escapedName);

if (body) {
return NpmResolver.findVersionInRegistryResponse(this.config, desiredVersion || this.range, body, this.request);
return NpmResolver.findVersionInRegistryResponse(this.config, escapedName, desiredRange, body, this.request);
} else {
return null;
}
Expand Down

0 comments on commit b07a19b

Please sign in to comment.