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

Ensure malformedRegistryResponse errors always have a dependency name #6455

Merged
merged 1 commit into from
Oct 2, 2018
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
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